Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ public List<String> getFileDirectories(String fieldName, FileDirectoryPreference

// 4. BIB file directory
getDatabasePath().ifPresent(dbPath -> {
String parentDir = dbPath.getParent().toAbsolutePath().toString();
Objects.requireNonNull(dbPath, "dbPath is null");
Path parentPath = dbPath.getParent();
if (parentPath == null) {
parentPath = Paths.get(System.getProperty("user.dir"));
}
Objects.requireNonNull(parentPath, "BibTex database parent path is null");
String parentDir = parentPath.toAbsolutePath().toString();
// Check if we should add it as primary file dir (first in the list) or not:
if (preferences.isBibLocationAsPrimary()) {
fileDirs.add(0, parentDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.jabref.model.database;

import java.io.File;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jabref.model.metadata.FileDirectoryPreferences;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

public class BibDatabaseContextTest {

// The 'currentWorkingDir' variable is used to re-create the part
// of the JabRef internal state that we get when the 'jabref
// biblio.bib' command is invoked from a command line (on
// Unix/Linux, but I guess on Windows clones as well). In the
// above-mentioned command, the current working directory must be
// used as a 'biblio.bib' parent directory. Since the current
// working directory is different on various computers that invoke
// the test, we can not hard-code it into the test but must
// determine it at run-time:
private String currentWorkingDir;

// Store the minimal preferences for the
// BibDatabaseContext.getFileDirectories(File,
// FileDirectoryPreferences) incocation:
private FileDirectoryPreferences preferences;

@Rule
public ExpectedException thrown = ExpectedException.none();

@Before
public void setUp() {
Map<String, String> mapFieldDirs = new HashMap<>();
preferences = new FileDirectoryPreferences("saulius", mapFieldDirs, true);
currentWorkingDir = Paths.get(System.getProperty("user.dir")).toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is preferable to use temporary folder rule of junit here, so that the test files are automatically deleted afterwards.

Copy link
Contributor Author

@sauliusg sauliusg Apr 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The directory named in the 'currentWorkingDir' value is not intended to hold any created files; actually, these tests should not create any files (I'll double check that).

The 'currentWorkingDir' variable is used to re-create the part of the JabRef internal state that we get when the 'jabref biblio.bib' command is invoked from a command line (on Unix/Linux, but I guess on Windows clones as well). Since the directory is different on various computers that invoke the test, I can not hard-code it into the test but must determine it at run-time.

Should I comment this in the code, or find a better name for the 'currentWorkingDir' variable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for commenting in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked: the tests do not leave the 'biblio.bib' file behind. The File object, as I understand, holds an abstract system-independent path to a file, but does not open or create the file unless exlicitely requested.

Copy link
Contributor Author

@sauliusg sauliusg Apr 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for commenting in the code.

Commenting the use of the variables in the test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can influence the fileDirectoryPreferences by mocking them, otherwise you always depend on the the settings of the user and can't have a reliable value to test.
In the CleanupTests for Renaming/Moving Files I mocked the relevant options like this:
fileDirPrefs = mock(FileDirectoryPreferences.class); when(fileDirPrefs.isBibLocationAsPrimary()).thenReturn(false);

Have a look at MoveFilesCleanupTest

}

@Test
public void getFileDirectoriesWithEmptyDbParent() {
BibDatabaseContext dbContext = new BibDatabaseContext();
dbContext.setDatabaseFile(new File("biblio.bib"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here also please use Paths.get().toFile()

List<String> fileDirectories = dbContext.getFileDirectories( "file", preferences );
assertEquals(Collections.singletonList(currentWorkingDir),
fileDirectories);
}

@Test
public void getFileDirectoriesWithRelativeDbParent() {
String dbDirectory = "relative/subdir";
BibDatabaseContext dbContext = new BibDatabaseContext();
dbContext.setDatabaseFile(new File(dbDirectory + "/" + "biblio.bib"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the methods of the Paths class. You can then just call Paths.get(dbDirectory).resolve("bib")

List<String> fileDirectories = dbContext.getFileDirectories("file", preferences);
assertEquals(Collections.singletonList(currentWorkingDir + "/" + dbDirectory),
fileDirectories);
}

@Test
public void getFileDirectoriesWithRelativeDottedDbParent() {
String dbDirectory = "./relative/subdir";
BibDatabaseContext dbContext = new BibDatabaseContext();
dbContext.setDatabaseFile(new File(dbDirectory + "/" + "biblio.bib"));
List<String> fileDirectories = dbContext.getFileDirectories("file", preferences);
assertEquals(Collections.singletonList(currentWorkingDir + "/" + dbDirectory),
fileDirectories);
}

@Test
public void getFileDirectoriesWithAbsoluteDbParent() {
String dbDirectory = "/absolute/subdir";
BibDatabaseContext dbContext = new BibDatabaseContext();
dbContext.setDatabaseFile(new File(dbDirectory + "/" + "biblio.bib"));
List<String> fileDirectories = dbContext.getFileDirectories("file", preferences);
assertEquals(Collections.singletonList(dbDirectory), fileDirectories);
}
}