Skip to content

Commit

Permalink
Minor CheckStyle Corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
arfazhxss authored Mar 29, 2024
1 parent 4e710c8 commit b810d99
Showing 1 changed file with 112 additions and 95 deletions.
207 changes: 112 additions & 95 deletions src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package org.jabref.logic.importer;

import java.util.Collections;
import java.util.Collection;
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;

import java.nio.file.Files;

Check failure on line 10 in src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[reviewdog] reported by reviewdog 🐶 Wrong order for 'java.nio.file.Files' import. Raw Output: /github/workspace/./src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java:10:1: error: Wrong order for 'java.nio.file.Files' import. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)

Check failure on line 10 in src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[reviewdog] reported by reviewdog 🐶 Wrong order for 'java.nio.file.Files' import. Raw Output: /github/workspace/./src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java:10:1: error: Wrong order for 'java.nio.file.Files' import. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Optional;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import java.util.Optional;
import javafx.collections.FXCollections;

Check failure on line 18 in src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[reviewdog] reported by reviewdog 🐶 'javafx.collections.FXCollections' should be separated from previous imports. Raw Output: /github/workspace/./src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java:18:1: error: 'javafx.collections.FXCollections' should be separated from previous imports. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)

Check failure on line 18 in src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[reviewdog] reported by reviewdog 🐶 'javafx.collections.FXCollections' should be separated from previous imports. Raw Output: /github/workspace/./src/test/java/org/jabref/logic/importer/DatabaseFileLookupTest.java:18:1: error: 'javafx.collections.FXCollections' should be separated from previous imports. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
import javafx.collections.ObservableList;
import org.jabref.logic.importer.fileformat.BibtexImporter;
Expand All @@ -21,97 +29,106 @@
import org.junit.jupiter.api.Test;
import org.mockito.Answers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.nio.file.Files;
import java.nio.file.Path;

class DatabaseFileLookupTest {

private BibDatabase database;
private Collection<BibEntry> entries;

private BibEntry entry1;
private BibEntry entry2;
private Path tempDir;
private Path txtFileDir;
private FilePreferences filePreferences;
private DatabaseFileLookup fileLookup;

/**
* Sets up the test environment before each test case.
*
* @throws Exception if an error occurs during setup
*/
@BeforeEach
void setUp() throws Exception {
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();

tempDir = Files.createTempDirectory("testDir");
txtFileDir = tempDir.resolve("x.txt");

entry1 = database.getEntryByCitationKey("entry1").get();
entry2 = database.getEntryByCitationKey("entry2").get();

BibEntry entry3 = new BibEntry().withField(StandardField.FILE, txtFileDir.toAbsolutePath().toString());
BibEntry entry4 = new BibEntry().withField(StandardField.FILE, "");

List<BibEntry> entries = new ArrayList<>(Arrays.asList(entry1, entry2, entry3, entry4));
ObservableList<BibEntry> observableEntryList = FXCollections
.synchronizedObservableList(FXCollections.observableArrayList(entries));
BibDatabase databaseMock = mock(BibDatabase.class);
when(databaseMock.getEntries()).thenReturn(observableEntryList);

Files.write(txtFileDir, Collections.singleton("x.txt file contents for test"));

filePreferences = mock(FilePreferences.class);
when(filePreferences.getMainFileDirectory()).thenReturn(Optional.of(txtFileDir.toAbsolutePath()));

BibDatabaseContext databaseContext = mock(BibDatabaseContext.class);
when(databaseContext.getFileDirectories(filePreferences))
.thenReturn(Collections.singletonList(txtFileDir));
when(databaseContext.getDatabase()).thenReturn(databaseMock);
when(databaseContext.getDatabase().getEntries()).thenReturn(observableEntryList);
when(databaseContext.getDatabasePath()).thenReturn(Optional.of(txtFileDir.toAbsolutePath()));
fileLookup = new DatabaseFileLookup(databaseContext, filePreferences);
}

/**
* Tests the prerequisites of this test-class itself.
*/
@Test
void prerequisitesFulfilled() {
assertEquals(2, 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));
assertEquals(filePreferences.getMainFileDirectory().orElse(Path.of("")).toString(),
txtFileDir.toAbsolutePath().toString());
assertNotNull(fileLookup.getPathOfDatabase());
}

/**
*
* y.txt should not be found in the any directory.
*/
@Test
void fileShouldNotBeFound() {
assertFalse(fileLookup.lookupDatabase(tempDir.resolve("y.txt")));
}
private BibDatabase database;
private Collection<BibEntry> entries;

private BibEntry entry1;
private BibEntry entry2;
private Path tempDir;
private Path txtFileDir;
private FilePreferences filePreferences;
private DatabaseFileLookup fileLookup;

/**
* Sets up the test environment before each test case.
*
* @throws Exception if an error occurs during setup
*/
@BeforeEach
void setUp() throws Exception {
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();

tempDir = Files.createTempDirectory("testDir");
txtFileDir = tempDir.resolve("x.txt");

entry1 = database.getEntryByCitationKey("entry1").get();
entry2 = database.getEntryByCitationKey("entry2").get();

BibEntry entry3 = new BibEntry()
.withField(StandardField.FILE, txtFileDir.toAbsolutePath().toString());
BibEntry entry4 = new BibEntry().withField(StandardField.FILE, "");

List<BibEntry> entries = new ArrayList<>(
Arrays.asList(entry1, entry2, entry3, entry4)
);
ObservableList<BibEntry> observableEntryList =
FXCollections.synchronizedObservableList(
FXCollections.observableArrayList(entries)
);
BibDatabase databaseMock = mock(BibDatabase.class);
when(databaseMock.getEntries()).thenReturn(observableEntryList);

Files.write(
txtFileDir,
Collections.singleton("x.txt file contents for test")
);

filePreferences = mock(FilePreferences.class);
when(filePreferences.getMainFileDirectory()).thenReturn(
Optional.of(txtFileDir.toAbsolutePath())
);

BibDatabaseContext databaseContext = mock(BibDatabaseContext.class);
when(databaseContext.getFileDirectories(filePreferences)).thenReturn(
Collections.singletonList(txtFileDir)
);
when(databaseContext.getDatabase()).thenReturn(databaseMock);
when(databaseContext.getDatabase().getEntries()).thenReturn(
observableEntryList
);
when(databaseContext.getDatabasePath()).thenReturn(
Optional.of(txtFileDir.toAbsolutePath())
);
fileLookup = new DatabaseFileLookup(databaseContext, filePreferences);
}

/**
* Tests the prerequisites of this test-class itself.
*/
@Test
void prerequisitesFulfilled() {
assertEquals(2, 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));
assertEquals(
filePreferences.getMainFileDirectory().orElse(Path.of("")).toString(),
txtFileDir.toAbsolutePath().toString()
);
assertNotNull(fileLookup.getPathOfDatabase());
}

/**
*
* y.txt should not be found in the any directory.
*/
@Test
void fileShouldNotBeFound() {
assertFalse(fileLookup.lookupDatabase(tempDir.resolve("y.txt")));
}
}

0 comments on commit b810d99

Please sign in to comment.