diff --git a/src/main/java/org/jabref/model/database/BibDatabaseContext.java b/src/main/java/org/jabref/model/database/BibDatabaseContext.java index 0bb5e863e407..8a80d5e04cf0 100644 --- a/src/main/java/org/jabref/model/database/BibDatabaseContext.java +++ b/src/main/java/org/jabref/model/database/BibDatabaseContext.java @@ -192,13 +192,17 @@ public List getFileDirectories(String fieldName, FileDirectoryPreference } // 3. preferences directory - preferences.getFileDirectory(fieldName).ifPresent(path -> - fileDirs.add(path.toAbsolutePath().toString()) - ); + preferences.getFileDirectory(fieldName).ifPresent(path -> fileDirs.add(path.toAbsolutePath().toString())); // 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); diff --git a/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java new file mode 100644 index 000000000000..0a944e5e4d38 --- /dev/null +++ b/src/test/java/org/jabref/model/database/BibDatabaseContextTest.java @@ -0,0 +1,78 @@ +package org.jabref.model.database; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; + +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; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class BibDatabaseContextTest { + + private Path currentWorkingDir; + + // Store the minimal preferences for the + // BibDatabaseContext.getFileDirectories(File, + // FileDirectoryPreferences) incocation: + private FileDirectoryPreferences fileDirPrefs; + + @Rule public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + fileDirPrefs = mock(FileDirectoryPreferences.class); + currentWorkingDir = Paths.get(System.getProperty("user.dir")); + when(fileDirPrefs.isBibLocationAsPrimary()).thenReturn(true); + } + + @Test + public void getFileDirectoriesWithEmptyDbParent() { + BibDatabaseContext dbContext = new BibDatabaseContext(); + dbContext.setDatabaseFile(Paths.get("biblio.bib").toFile()); + List fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs); + assertEquals(Collections.singletonList(currentWorkingDir.toString()), + fileDirectories); + } + + @Test + public void getFileDirectoriesWithRelativeDbParent() { + Path file = Paths.get("relative/subdir").resolve("biblio.bib"); + + BibDatabaseContext dbContext = new BibDatabaseContext(); + dbContext.setDatabaseFile(file.toFile()); + List fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs); + assertEquals(Collections.singletonList(currentWorkingDir.resolve(file.getParent()).toString()), + fileDirectories); + } + + @Test + public void getFileDirectoriesWithRelativeDottedDbParent() { + Path file = Paths.get("./relative/subdir").resolve("biblio.bib"); + + BibDatabaseContext dbContext = new BibDatabaseContext(); + dbContext.setDatabaseFile(file.toFile()); + List fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs); + assertEquals(Collections.singletonList(currentWorkingDir.resolve(file.getParent()).toString()), + fileDirectories); + } + + @Test + public void getFileDirectoriesWithAbsoluteDbParent() { + Path file = Paths.get("/absolute/subdir").resolve("biblio.bib"); + + BibDatabaseContext dbContext = new BibDatabaseContext(); + dbContext.setDatabaseFile(file.toFile()); + List fileDirectories = dbContext.getFileDirectories("file", fileDirPrefs); + assertEquals(Collections.singletonList(currentWorkingDir.resolve(file.getParent()).toString()), + fileDirectories); + } +}