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 #11108

Closed
wants to merge 18 commits into from
Closed
Changes from 17 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
106 changes: 102 additions & 4 deletions src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java
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);
Comment on lines 92 to 94
Copy link
Member

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.

}

/**
* x.txt should be found in the given directory.
*/
@Test
void fileShouldBeFound() {
assertTrue(fileLookup.lookupDatabase(txtFileDir));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion is OK.

assertNotNull(fileLookup.getPathOfDatabase());
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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()));
}
}
Loading