-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 #11108
Changes from 17 commits
74a161d
b0f3368
f281d6c
4f9a9ea
cf41e4d
4e710c8
b810d99
1beaddd
145376e
46627d3
0e9a310
2507209
a92ec91
2e8404f
f79239b
04e220c
cfe1061
db3d60a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,145 @@ | ||
package org.jabref.logic.importer; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
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.metadata.MetaData; | ||
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.junit.jupiter.api.io.TempDir; | ||
import org.mockito.Answers; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class DatabaseFileLookupTest { | ||
|
||
@TempDir | ||
Path tempDir; | ||
private BibDatabase database; | ||
private String txtFileDirPath; | ||
private Collection<BibEntry> entries; | ||
|
||
private BibEntry entry1; | ||
private BibEntry entry2; | ||
private Path txtFileDir; | ||
private MetaData metaData; | ||
private BibDatabaseContext databaseContext; | ||
private FilePreferences filePreferences; | ||
private DatabaseFileLookup fileLookup; | ||
|
||
@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(); | ||
|
||
entry1 = database.getEntryByCitationKey("entry1").get(); | ||
entry2 = database.getEntryByCitationKey("entry2").get(); | ||
txtFileDir = tempDir.resolve("x.txt"); | ||
txtFileDirPath = txtFileDir.toAbsolutePath().toString(); | ||
Files.write(txtFileDir, Collections.singleton("x.txt file contents for test")); | ||
|
||
entry1 = database.getEntryByCitationKey("entry1") | ||
.orElseThrow(() -> new Exception("Entry with citation key 'entry1' not found")); | ||
|
||
entry2 = database.getEntryByCitationKey("entry2") | ||
.orElseThrow(() -> new Exception("Entry with citation key 'entry2' not found")); | ||
|
||
BibEntry entry3 = new BibEntry().withField(StandardField.FILE, txtFileDirPath); | ||
BibEntry entry4 = new BibEntry().withField(StandardField.FILE, ""); | ||
|
||
List<BibEntry> entries = List.of(entry1, entry2, entry3, entry4); | ||
database = new BibDatabase(entries); | ||
|
||
filePreferences = mock(FilePreferences.class); | ||
when(filePreferences.getMainFileDirectory()).thenReturn(Optional.of(txtFileDir.toAbsolutePath())); | ||
when(filePreferences.getUserAndHost()).thenReturn("User124"); | ||
|
||
metaData = new MetaData(); | ||
metaData.setDefaultFileDirectory(txtFileDirPath); | ||
metaData.setUserFileDirectory("User124", txtFileDirPath); | ||
|
||
databaseContext = new BibDatabaseContext(database, metaData, txtFileDir.toAbsolutePath()); | ||
databaseContext.setDatabasePath(txtFileDir); | ||
|
||
fileLookup = new DatabaseFileLookup(databaseContext, filePreferences); | ||
} | ||
|
||
/** | ||
* Tests the prerequisites of this test-class itself. | ||
*/ | ||
@Test | ||
void prerequisitesFulfilled() { | ||
assertEquals(2, database.getEntryCount()); | ||
assertEquals(4, database.getEntryCount()); | ||
assertEquals(2, entries.size()); | ||
assertNotNull(entry1); | ||
assertNotNull(entry2); | ||
} | ||
|
||
/** | ||
* x.txt should be found in the given directory. | ||
*/ | ||
@Test | ||
void fileShouldBeFound() { | ||
assertTrue(fileLookup.lookupDatabase(txtFileDir)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assertion is OK. |
||
assertNotNull(fileLookup.getPathOfDatabase()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this. Tests other aspects. |
||
} | ||
|
||
/** | ||
* y.txt should not be found in any directory. | ||
*/ | ||
@Test | ||
void fileShouldNotBeFound() { | ||
assertFalse(fileLookup.lookupDatabase(tempDir.resolve("y.txt"))); | ||
} | ||
|
||
/** | ||
* x.txt should be found in the user-specific directory | ||
*/ | ||
@Test | ||
void userSpecificDirectory() { | ||
assertEquals(metaData.getUserFileDirectory(filePreferences.getUserAndHost()), Optional.of(txtFileDirPath)); | ||
} | ||
Comment on lines
+114
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. description does not match assertion (does it)? |
||
|
||
/** | ||
* x.txt should be found in the general directory | ||
*/ | ||
@Test | ||
void defaultFileDirectory() { | ||
assertEquals(metaData.getDefaultFileDirectory(), Optional.of(txtFileDirPath)); | ||
} | ||
|
||
/** | ||
* x.txt should be found in the main file directory | ||
*/ | ||
@Test | ||
void mainFileDirectory() { | ||
assertEquals(filePreferences.getMainFileDirectory(), Optional.of(txtFileDir.toAbsolutePath())); | ||
} | ||
|
||
/** | ||
* x.txt should be found in the database file directory | ||
*/ | ||
@Test | ||
void bibFileDirectory() { | ||
assertEquals(databaseContext.getDatabasePath(), Optional.of(txtFileDir.toAbsolutePath())); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A test case should focus on one thing. Not testing that constructors of data classes work. Remove all three assertions.