1
1
package org .jabref .logic .importer ;
2
2
3
- import java .util .Collection ;
3
+ import java .util .* ;
4
4
5
+ import javafx .collections .FXCollections ;
6
+ import javafx .collections .ObservableList ;
5
7
import org .jabref .logic .importer .fileformat .BibtexImporter ;
6
8
import org .jabref .logic .util .io .DatabaseFileLookup ;
7
9
import org .jabref .model .database .BibDatabase ;
17
19
import static org .junit .jupiter .api .Assertions .assertEquals ;
18
20
import static org .junit .jupiter .api .Assertions .assertNotNull ;
19
21
import static org .junit .jupiter .api .Assertions .assertTrue ;
20
- import static org .junit .jupiter .api .Assertions .fail ;
21
22
import static org .junit .jupiter .api .Assertions .assertFalse ;
22
23
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 ;
27
25
import java .nio .file .Files ;
28
26
import java .nio .file .Path ;
29
- import java .nio .file .Paths ;
30
- import java .util .Collections ;
31
27
32
28
class DatabaseFileLookupTest {
33
29
@@ -36,7 +32,16 @@ class DatabaseFileLookupTest {
36
32
37
33
private BibEntry entry1 ;
38
34
private BibEntry entry2 ;
35
+ private Path tempDir ;
36
+ private Path txtFileDir ;
37
+ private FilePreferences filePreferences ;
38
+ private static DatabaseFileLookup fileLookup ;
39
39
40
+ /**
41
+ * Sets up the test environment before each test case.
42
+ *
43
+ * @throws Exception if an error occurs during setup
44
+ */
40
45
@ BeforeEach
41
46
void setUp () throws Exception {
42
47
ParserResult result = new BibtexImporter (mock (ImportFormatPreferences .class , Answers .RETURNS_DEEP_STUBS ),
@@ -45,8 +50,33 @@ void setUp() throws Exception {
45
50
database = result .getDatabase ();
46
51
entries = database .getEntries ();
47
52
53
+ tempDir = Files .createTempDirectory ("testDir" );
54
+ txtFileDir = tempDir .resolve ("x.txt" );
55
+
48
56
entry1 = database .getEntryByCitationKey ("entry1" ).get ();
49
57
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 );
50
80
}
51
81
52
82
/**
@@ -61,50 +91,22 @@ void prerequisitesFulfilled() {
61
91
}
62
92
63
93
/**
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.
74
95
*/
75
96
@ 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 ));
105
99
assertEquals (filePreferences .getMainFileDirectory ().orElse (Path .of ("" )).toString (),
106
100
txtFileDir .toAbsolutePath ().toString ());
107
101
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" )));
109
111
}
110
112
}
0 commit comments