Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for DatabaseFileLookup #1

Merged
merged 2 commits into from
Mar 29, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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());
}
}