Skip to content

Commit cf41e4d

Browse files
authored
Update DatabaseFileLookupTest.java
1 parent 4f9a9ea commit cf41e4d

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed
Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.jabref.logic.importer;
22

3-
import java.util.Collection;
3+
import java.util.*;
44

5+
import javafx.collections.FXCollections;
6+
import javafx.collections.ObservableList;
57
import org.jabref.logic.importer.fileformat.BibtexImporter;
68
import org.jabref.logic.util.io.DatabaseFileLookup;
79
import org.jabref.model.database.BibDatabase;
@@ -17,17 +19,11 @@
1719
import static org.junit.jupiter.api.Assertions.assertEquals;
1820
import static org.junit.jupiter.api.Assertions.assertNotNull;
1921
import static org.junit.jupiter.api.Assertions.assertTrue;
20-
import static org.junit.jupiter.api.Assertions.fail;
2122
import static org.junit.jupiter.api.Assertions.assertFalse;
2223
import static org.mockito.Mockito.mock;
23-
24-
import org.junit.jupiter.api.io.TempDir;
25-
26-
import java.io.IOException;
24+
import static org.mockito.Mockito.when;
2725
import java.nio.file.Files;
2826
import java.nio.file.Path;
29-
import java.nio.file.Paths;
30-
import java.util.Collections;
3127

3228
class DatabaseFileLookupTest {
3329

@@ -36,7 +32,16 @@ class DatabaseFileLookupTest {
3632

3733
private BibEntry entry1;
3834
private BibEntry entry2;
35+
private Path tempDir;
36+
private Path txtFileDir;
37+
private FilePreferences filePreferences;
38+
private static DatabaseFileLookup fileLookup;
3939

40+
/**
41+
* Sets up the test environment before each test case.
42+
*
43+
* @throws Exception if an error occurs during setup
44+
*/
4045
@BeforeEach
4146
void setUp() throws Exception {
4247
ParserResult result = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS),
@@ -45,8 +50,33 @@ void setUp() throws Exception {
4550
database = result.getDatabase();
4651
entries = database.getEntries();
4752

53+
tempDir = Files.createTempDirectory("testDir");
54+
txtFileDir = tempDir.resolve("x.txt");
55+
4856
entry1 = database.getEntryByCitationKey("entry1").get();
4957
entry2 = database.getEntryByCitationKey("entry2").get();
58+
59+
BibEntry entry3 = new BibEntry().withField(StandardField.FILE, txtFileDir.toAbsolutePath().toString());
60+
BibEntry entry4 = new BibEntry().withField(StandardField.FILE, "");
61+
62+
List<BibEntry> entries = new ArrayList<>(Arrays.asList(entry1, entry2, entry3, entry4));
63+
ObservableList<BibEntry> observableEntryList = FXCollections
64+
.synchronizedObservableList(FXCollections.observableArrayList(entries));
65+
BibDatabase databaseMock = mock(BibDatabase.class);
66+
when(databaseMock.getEntries()).thenReturn(observableEntryList);
67+
68+
Files.write(txtFileDir, Collections.singleton("x.txt file contents for test"));
69+
70+
filePreferences = mock(FilePreferences.class);
71+
when(filePreferences.getMainFileDirectory()).thenReturn(Optional.of(txtFileDir.toAbsolutePath()));
72+
73+
BibDatabaseContext databaseContext = mock(BibDatabaseContext.class);
74+
when(databaseContext.getFileDirectories(filePreferences))
75+
.thenReturn(Collections.singletonList(txtFileDir));
76+
when(databaseContext.getDatabase()).thenReturn(databaseMock);
77+
when(databaseContext.getDatabase().getEntries()).thenReturn(observableEntryList);
78+
when(databaseContext.getDatabasePath()).thenReturn(Optional.of(txtFileDir.toAbsolutePath()));
79+
fileLookup = new DatabaseFileLookup(databaseContext, filePreferences);
5080
}
5181

5282
/**
@@ -61,50 +91,22 @@ void prerequisitesFulfilled() {
6191
}
6292

6393
/**
64-
* Tests the directory path functionality by creating a temporary file
65-
* directory,
66-
* creating a BibDatabaseContext with a BibDatabase containing two entries,
67-
* setting the temporary directory as the default file directory in the
68-
* preferences,
69-
* and creating a DatabaseFileLookup instance.
70-
*
71-
* @param tempDir the temporary directory path
72-
* @throws IOException if there is an error creating the temporary file
73-
* directory
94+
* x.txt should be found in the given directory.
7495
*/
7596
@Test
76-
void directoryPathTests(@TempDir Path tempDir) throws IOException {
77-
Path txtFileDir = tempDir.resolve("x.txt"); // Create a temporary directory for testing
78-
79-
try {
80-
Files.write(txtFileDir, Collections.singleton("x.txt file contents for test"));
81-
} catch (IOException e) {
82-
fail("Failed to create temporary file directory: " + e.getMessage());
83-
}
84-
85-
// Create a BibDatabaseContext with a BibDatabase containing two entries
86-
BibDatabase bibDatabase = new BibDatabase();
87-
BibEntry entry1 = new BibEntry();
88-
entry1.setField(StandardField.FILE, txtFileDir.toAbsolutePath().toString());
89-
BibEntry entry2 = new BibEntry();
90-
entry2.setField(StandardField.FILE, "");
91-
bibDatabase.insertEntry(entry1);
92-
bibDatabase.insertEntry(entry2);
93-
94-
BibDatabaseContext databaseContext = new BibDatabaseContext(bibDatabase);
95-
96-
// Set the temporary directory as the default file directory
97-
// in the preferences and creating DatabaseFileLookup instance
98-
FilePreferences filePreferences = new FilePreferences("", txtFileDir.toAbsolutePath().toString(), false, "", "",
99-
false, false, null, Collections.emptySet(), false, null);
100-
DatabaseFileLookup fileLookup = new DatabaseFileLookup(databaseContext, filePreferences);
101-
102-
// Tests
103-
assertTrue(fileLookup.lookupDatabase(txtFileDir)); // x.txt should be found
104-
assertFalse(fileLookup.lookupDatabase(tempDir.resolve("y.txt"))); // y.txt should not be found
97+
void fileShouldBeFound() {
98+
assertTrue(fileLookup.lookupDatabase(txtFileDir));
10599
assertEquals(filePreferences.getMainFileDirectory().orElse(Path.of("")).toString(),
106100
txtFileDir.toAbsolutePath().toString());
107101
assertNotNull(fileLookup.getPathOfDatabase());
108-
assertEquals("", fileLookup.getPathOfDatabase().toString());
102+
}
103+
104+
/**
105+
*
106+
* y.txt should not be found in the any directory.
107+
*/
108+
@Test
109+
void fileShouldNotBeFound() {
110+
assertFalse(fileLookup.lookupDatabase(tempDir.resolve("y.txt")));
109111
}
110112
}

0 commit comments

Comments
 (0)