|
1 | 1 | package org.jabref.logic.importer;
|
2 | 2 |
|
3 |
| -import java.util.Collections; |
4 |
| -import java.util.Collection; |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
5 | 12 | import java.util.ArrayList;
|
6 |
| -import java.util.Optional; |
7 | 13 | import java.util.Arrays;
|
| 14 | +import java.util.Collection; |
| 15 | +import java.util.Collections; |
8 | 16 | import java.util.List;
|
9 |
| - |
| 17 | +import java.util.Optional; |
10 | 18 | import javafx.collections.FXCollections;
|
11 | 19 | import javafx.collections.ObservableList;
|
12 | 20 | import org.jabref.logic.importer.fileformat.BibtexImporter;
|
|
21 | 29 | import org.junit.jupiter.api.Test;
|
22 | 30 | import org.mockito.Answers;
|
23 | 31 |
|
24 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
25 |
| -import static org.junit.jupiter.api.Assertions.assertNotNull; |
26 |
| -import static org.junit.jupiter.api.Assertions.assertTrue; |
27 |
| -import static org.junit.jupiter.api.Assertions.assertFalse; |
28 |
| -import static org.mockito.Mockito.mock; |
29 |
| -import static org.mockito.Mockito.when; |
30 |
| -import java.nio.file.Files; |
31 |
| -import java.nio.file.Path; |
32 |
| - |
33 | 32 | class DatabaseFileLookupTest {
|
34 | 33 |
|
35 |
| - private BibDatabase database; |
36 |
| - private Collection<BibEntry> entries; |
37 |
| - |
38 |
| - private BibEntry entry1; |
39 |
| - private BibEntry entry2; |
40 |
| - private Path tempDir; |
41 |
| - private Path txtFileDir; |
42 |
| - private FilePreferences filePreferences; |
43 |
| - private DatabaseFileLookup fileLookup; |
44 |
| - |
45 |
| - /** |
46 |
| - * Sets up the test environment before each test case. |
47 |
| - * |
48 |
| - * @throws Exception if an error occurs during setup |
49 |
| - */ |
50 |
| - @BeforeEach |
51 |
| - void setUp() throws Exception { |
52 |
| - ParserResult result = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS), |
53 |
| - new DummyFileUpdateMonitor()) |
54 |
| - .importDatabase(ImportDataTest.UNLINKED_FILES_TEST_BIB); |
55 |
| - database = result.getDatabase(); |
56 |
| - entries = database.getEntries(); |
57 |
| - |
58 |
| - tempDir = Files.createTempDirectory("testDir"); |
59 |
| - txtFileDir = tempDir.resolve("x.txt"); |
60 |
| - |
61 |
| - entry1 = database.getEntryByCitationKey("entry1").get(); |
62 |
| - entry2 = database.getEntryByCitationKey("entry2").get(); |
63 |
| - |
64 |
| - BibEntry entry3 = new BibEntry().withField(StandardField.FILE, txtFileDir.toAbsolutePath().toString()); |
65 |
| - BibEntry entry4 = new BibEntry().withField(StandardField.FILE, ""); |
66 |
| - |
67 |
| - List<BibEntry> entries = new ArrayList<>(Arrays.asList(entry1, entry2, entry3, entry4)); |
68 |
| - ObservableList<BibEntry> observableEntryList = FXCollections |
69 |
| - .synchronizedObservableList(FXCollections.observableArrayList(entries)); |
70 |
| - BibDatabase databaseMock = mock(BibDatabase.class); |
71 |
| - when(databaseMock.getEntries()).thenReturn(observableEntryList); |
72 |
| - |
73 |
| - Files.write(txtFileDir, Collections.singleton("x.txt file contents for test")); |
74 |
| - |
75 |
| - filePreferences = mock(FilePreferences.class); |
76 |
| - when(filePreferences.getMainFileDirectory()).thenReturn(Optional.of(txtFileDir.toAbsolutePath())); |
77 |
| - |
78 |
| - BibDatabaseContext databaseContext = mock(BibDatabaseContext.class); |
79 |
| - when(databaseContext.getFileDirectories(filePreferences)) |
80 |
| - .thenReturn(Collections.singletonList(txtFileDir)); |
81 |
| - when(databaseContext.getDatabase()).thenReturn(databaseMock); |
82 |
| - when(databaseContext.getDatabase().getEntries()).thenReturn(observableEntryList); |
83 |
| - when(databaseContext.getDatabasePath()).thenReturn(Optional.of(txtFileDir.toAbsolutePath())); |
84 |
| - fileLookup = new DatabaseFileLookup(databaseContext, filePreferences); |
85 |
| - } |
86 |
| - |
87 |
| - /** |
88 |
| - * Tests the prerequisites of this test-class itself. |
89 |
| - */ |
90 |
| - @Test |
91 |
| - void prerequisitesFulfilled() { |
92 |
| - assertEquals(2, database.getEntryCount()); |
93 |
| - assertEquals(2, entries.size()); |
94 |
| - assertNotNull(entry1); |
95 |
| - assertNotNull(entry2); |
96 |
| - } |
97 |
| - |
98 |
| - /** |
99 |
| - * x.txt should be found in the given directory. |
100 |
| - */ |
101 |
| - @Test |
102 |
| - void fileShouldBeFound() { |
103 |
| - assertTrue(fileLookup.lookupDatabase(txtFileDir)); |
104 |
| - assertEquals(filePreferences.getMainFileDirectory().orElse(Path.of("")).toString(), |
105 |
| - txtFileDir.toAbsolutePath().toString()); |
106 |
| - assertNotNull(fileLookup.getPathOfDatabase()); |
107 |
| - } |
108 |
| - |
109 |
| - /** |
110 |
| - * |
111 |
| - * y.txt should not be found in the any directory. |
112 |
| - */ |
113 |
| - @Test |
114 |
| - void fileShouldNotBeFound() { |
115 |
| - assertFalse(fileLookup.lookupDatabase(tempDir.resolve("y.txt"))); |
116 |
| - } |
| 34 | + private BibDatabase database; |
| 35 | + private Collection<BibEntry> entries; |
| 36 | + |
| 37 | + private BibEntry entry1; |
| 38 | + private BibEntry entry2; |
| 39 | + private Path tempDir; |
| 40 | + private Path txtFileDir; |
| 41 | + private FilePreferences filePreferences; |
| 42 | + private DatabaseFileLookup fileLookup; |
| 43 | + |
| 44 | + /** |
| 45 | + * Sets up the test environment before each test case. |
| 46 | + * |
| 47 | + * @throws Exception if an error occurs during setup |
| 48 | + */ |
| 49 | + @BeforeEach |
| 50 | + void setUp() throws Exception { |
| 51 | + ParserResult result = new BibtexImporter( |
| 52 | + mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS), |
| 53 | + new DummyFileUpdateMonitor() |
| 54 | + ).importDatabase(ImportDataTest.UNLINKED_FILES_TEST_BIB); |
| 55 | + database = result.getDatabase(); |
| 56 | + entries = database.getEntries(); |
| 57 | + |
| 58 | + tempDir = Files.createTempDirectory("testDir"); |
| 59 | + txtFileDir = tempDir.resolve("x.txt"); |
| 60 | + |
| 61 | + entry1 = database.getEntryByCitationKey("entry1").get(); |
| 62 | + entry2 = database.getEntryByCitationKey("entry2").get(); |
| 63 | + |
| 64 | + BibEntry entry3 = new BibEntry() |
| 65 | + .withField(StandardField.FILE, txtFileDir.toAbsolutePath().toString()); |
| 66 | + BibEntry entry4 = new BibEntry().withField(StandardField.FILE, ""); |
| 67 | + |
| 68 | + List<BibEntry> entries = new ArrayList<>( |
| 69 | + Arrays.asList(entry1, entry2, entry3, entry4) |
| 70 | + ); |
| 71 | + ObservableList<BibEntry> observableEntryList = |
| 72 | + FXCollections.synchronizedObservableList( |
| 73 | + FXCollections.observableArrayList(entries) |
| 74 | + ); |
| 75 | + BibDatabase databaseMock = mock(BibDatabase.class); |
| 76 | + when(databaseMock.getEntries()).thenReturn(observableEntryList); |
| 77 | + |
| 78 | + Files.write( |
| 79 | + txtFileDir, |
| 80 | + Collections.singleton("x.txt file contents for test") |
| 81 | + ); |
| 82 | + |
| 83 | + filePreferences = mock(FilePreferences.class); |
| 84 | + when(filePreferences.getMainFileDirectory()).thenReturn( |
| 85 | + Optional.of(txtFileDir.toAbsolutePath()) |
| 86 | + ); |
| 87 | + |
| 88 | + BibDatabaseContext databaseContext = mock(BibDatabaseContext.class); |
| 89 | + when(databaseContext.getFileDirectories(filePreferences)).thenReturn( |
| 90 | + Collections.singletonList(txtFileDir) |
| 91 | + ); |
| 92 | + when(databaseContext.getDatabase()).thenReturn(databaseMock); |
| 93 | + when(databaseContext.getDatabase().getEntries()).thenReturn( |
| 94 | + observableEntryList |
| 95 | + ); |
| 96 | + when(databaseContext.getDatabasePath()).thenReturn( |
| 97 | + Optional.of(txtFileDir.toAbsolutePath()) |
| 98 | + ); |
| 99 | + fileLookup = new DatabaseFileLookup(databaseContext, filePreferences); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Tests the prerequisites of this test-class itself. |
| 104 | + */ |
| 105 | + @Test |
| 106 | + void prerequisitesFulfilled() { |
| 107 | + assertEquals(2, database.getEntryCount()); |
| 108 | + assertEquals(2, entries.size()); |
| 109 | + assertNotNull(entry1); |
| 110 | + assertNotNull(entry2); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * x.txt should be found in the given directory. |
| 115 | + */ |
| 116 | + @Test |
| 117 | + void fileShouldBeFound() { |
| 118 | + assertTrue(fileLookup.lookupDatabase(txtFileDir)); |
| 119 | + assertEquals( |
| 120 | + filePreferences.getMainFileDirectory().orElse(Path.of("")).toString(), |
| 121 | + txtFileDir.toAbsolutePath().toString() |
| 122 | + ); |
| 123 | + assertNotNull(fileLookup.getPathOfDatabase()); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * |
| 128 | + * y.txt should not be found in the any directory. |
| 129 | + */ |
| 130 | + @Test |
| 131 | + void fileShouldNotBeFound() { |
| 132 | + assertFalse(fileLookup.lookupDatabase(tempDir.resolve("y.txt"))); |
| 133 | + } |
117 | 134 | }
|
0 commit comments