From 74a161d9c7a9f45b7d913367509e534df3ab3473 Mon Sep 17 00:00:00 2001 From: Arfaz Hossain Date: Fri, 29 Mar 2024 00:27:06 -0700 Subject: [PATCH 1/2] Update DatabaseFileLookupTest.java --- .../importer/DatabaseFileLookupTest.java | 67 ++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java b/src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java index e2ed83fc2a0..cf2c9d3b438 100644 --- a/src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java +++ b/src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java @@ -3,18 +3,32 @@ import java.util.Collection; import org.jabref.logic.importer.fileformat.BibtexImporter; +import org.jabref.logic.util.io.DatabaseFileLookup; import org.jabref.model.database.BibDatabase; +import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.field.StandardField; import org.jabref.model.util.DummyFileUpdateMonitor; - +import org.jabref.preferences.FilePreferences; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Answers; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.mockito.Mockito.mock; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; + class DatabaseFileLookupTest { private BibDatabase database; @@ -25,7 +39,8 @@ class DatabaseFileLookupTest { @BeforeEach void setUp() throws Exception { - ParserResult result = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS), new DummyFileUpdateMonitor()) + ParserResult result = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS), + new DummyFileUpdateMonitor()) .importDatabase(ImportDataTest.UNLINKED_FILES_TEST_BIB); database = result.getDatabase(); entries = database.getEntries(); @@ -44,4 +59,52 @@ void prerequisitesFulfilled() { assertNotNull(entry1); assertNotNull(entry2); } + + /** + * Tests the directory path functionality by creating a temporary file + * directory, + * creating a BibDatabaseContext with a BibDatabase containing two entries, + * setting the temporary directory as the default file directory in the + * preferences, + * and creating a DatabaseFileLookup instance. + * + * @param tempDir the temporary directory path + * @throws IOException if there is an error creating the temporary file + * directory + */ + @Test + void directoryPathTests(@TempDir Path tempDir) throws IOException { + Path txtFileDir = tempDir.resolve("x.txt"); // Create a temporary directory for testing + + try { + Files.write(txtFileDir, Collections.singleton("x.txt file contents for test")); + } catch (IOException e) { + fail("Failed to create temporary file directory: " + e.getMessage()); + } + + // Create a BibDatabaseContext with a BibDatabase containing two entries + BibDatabase bibDatabase = new BibDatabase(); + BibEntry entry1 = new BibEntry(); + entry1.setField(StandardField.FILE, txtFileDir.toAbsolutePath().toString()); + BibEntry entry2 = new BibEntry(); + entry2.setField(StandardField.FILE, ""); + bibDatabase.insertEntry(entry1); + bibDatabase.insertEntry(entry2); + + BibDatabaseContext databaseContext = new BibDatabaseContext(bibDatabase); + + // Set the temporary directory as the default file directory + // in the preferences and creating DatabaseFileLookup instance + FilePreferences filePreferences = new FilePreferences("", txtFileDir.toAbsolutePath().toString(), false, "", "", + false, false, null, Collections.emptySet(), false, null); + DatabaseFileLookup fileLookup = new DatabaseFileLookup(databaseContext, filePreferences); + + // Tests + assertTrue(fileLookup.lookupDatabase(txtFileDir)); // x.txt should be found + assertFalse(fileLookup.lookupDatabase(tempDir.resolve("y.txt"))); // y.txt should not be found + assertEquals(filePreferences.getMainFileDirectory().orElse(Path.of("")).toString(), + txtFileDir.toAbsolutePath().toString()); + assertNotNull(fileLookup.getPathOfDatabase()); + assertEquals("", fileLookup.getPathOfDatabase().toString()); + } } From b0f33688d006546b3f96ff56f0733e425f58614a Mon Sep 17 00:00:00 2001 From: Arfaz Hossain Date: Fri, 29 Mar 2024 00:35:08 -0700 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a1b1da4270..3d9232fe26d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - Importer/Exporter for CFF format now supports JabRef `cites` and `related` relationships, as well as all fields from the CFF specification. [#10993](https://github.com/JabRef/jabref/issues/10993) - The XMP-Exporter no longer writes the content of the `file`-field. [#11083](https://github.com/JabRef/jabref/pull/11083) - We added notes, checks and warnings for the case of selection of non-empty directories while starting a new Systematic Literature Review. [#600](https://github.com/koppor/jabref/issues/600) +- We added test cases for DatabaseFileLookup to cover file presence and absence scenarios. [koppor#678](https://github.com/koppor/jabref/issues/678) ### Fixed