Skip to content

Commit

Permalink
Migrate importer tests to JUnit5 (#3665)
Browse files Browse the repository at this point in the history
* Migrate paramterized RIS tests to JUnit5

* Extract generic importer testing code to new class

* Switch to JUnit5 assertions in BibEntryAssert

* Remove unused imports

* Use static import for Assertion when possible

* Extract file collection from importer test classes

* Refactor biblioscape importer tests

* Refactor BiblioscapeImporterTestTypes to JUnit5 syntax

* Refactor CopacImporterTestFiles

* Migrate medline importer tests to JUnit5

* Convert BibTeXML importer tests to JUnit5

* Move several non-parametric importer test classes to JUnit5

* Refactor Medline importer tests to JUni5

* Migrate parameterized MODS importer tests to JUnit5

* Migrate MrDLib importer tests to JUnit5

* Migrate MsBibImporter tests to JUnit5

* Migrate OvidImporter tests to JUnit5

* Migrate PdfContentImporter tests to JUnit5

* Migrate PdfXmpImporter tests to JUnit5

* Migrate RepecNepImporter tests to JUnit5

* Migrate RISImporter tests to JUnit5

* Migrate SilverPlatterImporter tests to JUnit5

* Fix imports in SilverPlatterImporterTest

* Fix BibTeXMLImporter tests

* Fix and clarify BibTeXMLImporterTestTypes

* Fix medline tests for malformed files

* Remove unused imports

* Fix broken test files that can be fixed and remove the ones with larger syntactic problem

* Fix MODSImporter tests

* Fix test file for MsBibImporter tests

* Convert Before to BeforeEach in OvidImporterTests

* Check starting line of a file for checking whether it is a PDF

* Migrate additional BibTeXML tests to JUnit5

* Refactor and restructure BibTexParser tests

* Refactor CopacImporter tests

* Refactor BibTeXMLImporter tests

* Refactor EndnoteImporter tests

* Refactor FreeCiteImporter tests

* Refactor InspecImporter tests

* Refactor IsiImporter tests

* Refactor MedlineImporter tests

* Refactor MrDLibImporter tests

* Refactor MsBibImporter tests

* Refactor OvidImporter tests

* Refactor PdfXmpImporter tests

* Refactor RepecNepImporter tests

* Refactor SilverPlatterImporter tests

* Fix a bunch of codacy issues
  • Loading branch information
lenhard authored and Siedlerchr committed Jan 31, 2018
1 parent 5189a26 commit 608e415
Show file tree
Hide file tree
Showing 47 changed files with 1,384 additions and 1,868 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jabref.logic.importer.fileformat.bibtexml.Incollection;
import org.jabref.logic.util.FileType;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibtexEntryTypes;
import org.jabref.model.entry.FieldName;

import org.slf4j.Logger;
Expand Down Expand Up @@ -91,50 +92,50 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
for (Entry entry : entries) {
BibEntry bibEntry = new BibEntry();
if (entry.getArticle() != null) {
bibEntry.setType("article");
bibEntry.setType(BibtexEntryTypes.ARTICLE);
parse(entry.getArticle(), fields);
} else if (entry.getBook() != null) {
bibEntry.setType("book");
bibEntry.setType(BibtexEntryTypes.BOOK);
parse(entry.getBook(), fields);
} else if (entry.getBooklet() != null) {
bibEntry.setType("booklet");
bibEntry.setType(BibtexEntryTypes.BOOKLET);
parse(entry.getBooklet(), fields);
} else if (entry.getConference() != null) {
bibEntry.setType("conference");
bibEntry.setType(BibtexEntryTypes.CONFERENCE);
parse(entry.getConference(), fields);
} else if (entry.getInbook() != null) {
bibEntry.setType("inbook");
bibEntry.setType(BibtexEntryTypes.INBOOK);
parseInbook(entry.getInbook(), fields);
} else if (entry.getIncollection() != null) {
bibEntry.setType("incollection");
bibEntry.setType(BibtexEntryTypes.INCOLLECTION);
Incollection incollection = entry.getIncollection();
if (incollection.getChapter() != null) {
fields.put(FieldName.CHAPTER, String.valueOf(incollection.getChapter()));
}
parse(incollection, fields);
} else if (entry.getInproceedings() != null) {
bibEntry.setType("inproceedings");
bibEntry.setType(BibtexEntryTypes.INPROCEEDINGS);
parse(entry.getInproceedings(), fields);
} else if (entry.getManual() != null) {
bibEntry.setType("manual");
bibEntry.setType(BibtexEntryTypes.MANUAL);
parse(entry.getManual(), fields);
} else if (entry.getMastersthesis() != null) {
bibEntry.setType("mastersthesis");
bibEntry.setType(BibtexEntryTypes.MASTERSTHESIS);
parse(entry.getMastersthesis(), fields);
} else if (entry.getMisc() != null) {
bibEntry.setType("misc");
bibEntry.setType(BibtexEntryTypes.MISC);
parse(entry.getMisc(), fields);
} else if (entry.getPhdthesis() != null) {
bibEntry.setType("phdthesis");
bibEntry.setType(BibtexEntryTypes.PHDTHESIS);
parse(entry.getPhdthesis(), fields);
} else if (entry.getProceedings() != null) {
bibEntry.setType("proceedings");
bibEntry.setType(BibtexEntryTypes.PROCEEDINGS);
parse(entry.getProceedings(), fields);
} else if (entry.getTechreport() != null) {
bibEntry.setType("techreport");
bibEntry.setType(BibtexEntryTypes.TECHREPORT);
parse(entry.getTechreport(), fields);
} else if (entry.getUnpublished() != null) {
bibEntry.setType("unpublished");
bibEntry.setType(BibtexEntryTypes.UNPUBLISHED);
parse(entry.getUnpublished(), fields);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,8 @@ private static String streamlineTitle(String title) {
}

@Override
public boolean isRecognizedFormat(BufferedReader reader) throws IOException {
Objects.requireNonNull(reader);
return false;
public boolean isRecognizedFormat(BufferedReader input) throws IOException {
return input.readLine().startsWith("%PDF");
}

@Override
Expand Down
37 changes: 21 additions & 16 deletions src/test/java/org/jabref/logic/bibtex/BibEntryAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.util.DummyFileUpdateMonitor;

import org.junit.Assert;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Assertions;
import org.mockito.Answers;

import static org.mockito.Mockito.mock;
Expand All @@ -36,9 +38,9 @@ public class BibEntryAssert {
*/
public static void assertEquals(Class<?> clazz, String resourceName, BibEntry entry)
throws IOException {
Assert.assertNotNull(clazz);
Assert.assertNotNull(resourceName);
Assert.assertNotNull(entry);
assertNotNull(clazz);
assertNotNull(resourceName);
assertNotNull(entry);
try (InputStream shouldBeIs = clazz.getResourceAsStream(resourceName)) {
BibEntryAssert.assertEquals(shouldBeIs, entry);
}
Expand All @@ -54,9 +56,9 @@ public static void assertEquals(Class<?> clazz, String resourceName, BibEntry en
*/
public static void assertEquals(Class<?> clazz, String resourceName, List<BibEntry> asIsEntries)
throws IOException {
Assert.assertNotNull(clazz);
Assert.assertNotNull(resourceName);
Assert.assertNotNull(asIsEntries);
assertNotNull(clazz);
assertNotNull(resourceName);
assertNotNull(asIsEntries);
try (InputStream shouldBeIs = clazz.getResourceAsStream(resourceName)) {
BibEntryAssert.assertEquals(shouldBeIs, asIsEntries);
}
Expand All @@ -68,8 +70,8 @@ private static List<BibEntry> getListFromInputStream(InputStream is) throws IOEx
BibtexParser parser = new BibtexParser(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS), new DummyFileUpdateMonitor());
result = parser.parse(reader);
}
Assert.assertNotNull(result);
Assert.assertFalse(result.isEmpty());
assertNotNull(result);
assertFalse(result.isEmpty());
return result.getDatabase().getEntries();
}

Expand All @@ -81,16 +83,18 @@ private static List<BibEntry> getListFromInputStream(InputStream is) throws IOEx
*/
public static void assertEquals(InputStream expectedInputStream, List<BibEntry> actualEntries)
throws IOException {
Assert.assertNotNull(expectedInputStream);
Assert.assertNotNull(actualEntries);
Assert.assertEquals(getListFromInputStream(expectedInputStream), actualEntries);
assertNotNull(expectedInputStream);
assertNotNull(actualEntries);
// explicit reference of Assertions is needed here to disambiguate from the methods defined by this class
Assertions.assertEquals(getListFromInputStream(expectedInputStream), actualEntries);
}

public static void assertEquals(List<BibEntry> expectedEntries, InputStream actualInputStream)
throws IOException {
Assert.assertNotNull(actualInputStream);
Assert.assertNotNull(expectedEntries);
Assert.assertEquals(expectedEntries, getListFromInputStream(actualInputStream));
assertNotNull(actualInputStream);
assertNotNull(expectedEntries);
// explicit reference of Assertions is needed here to disambiguate from the methods defined by this class
Assertions.assertEquals(expectedEntries, getListFromInputStream(actualInputStream));
}

/**
Expand Down Expand Up @@ -133,7 +137,8 @@ public static void assertEquals(List<BibEntry> expected, Path fileToImport, Impo
throws IOException {
List<BibEntry> actualEntries = importer.importDatabase(fileToImport, StandardCharsets.UTF_8)
.getDatabase().getEntries();
Assert.assertEquals(expected, actualEntries);
// explicit reference of Assertions is needed here to disambiguate from the methods defined by this class
Assertions.assertEquals(expected, actualEntries);
}

public static void assertEquals(List<BibEntry> expected, URL fileToImport, Importer importer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,18 @@
package org.jabref.logic.importer.fileformat;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jabref.logic.util.FileType;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

@RunWith(MockitoJUnitRunner.class)
public class BibTeXMLImporterTest {

private BibTeXMLImporter importer;


/**
* Generates a List of all files in the package "/src/test/resources/org/jabref/logic/importer/fileformat"
*
* @return A list of Names
* @throws IOException
*/
public List<Path> getTestFiles() throws Exception {
try (Stream<Path> stream = Files.list(Paths.get(BibTeXMLImporterTest.class.getResource("").toURI()))) {
return stream.filter(p -> !Files.isDirectory(p)).collect(Collectors.toList());
}

}

@Before
@BeforeEach
public void setUp() throws Exception {
importer = new BibTeXMLImporter();
}
Expand All @@ -62,15 +36,4 @@ public void testsGetExtensions() {
public void testGetDescription() {
assertEquals("Importer for the BibTeXML format.", importer.getDescription());
}

@Test
public void testIsRecognizedFormatReject() throws Exception {
List<Path> list = getTestFiles().stream()
.filter(n -> !n.getFileName().toString().startsWith("BibTeXMLImporterTest"))
.collect(Collectors.toList());

for (Path file : list) {
assertFalse(file.toString(), importer.isRecognizedFormat(file, StandardCharsets.UTF_8));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,69 +1,43 @@
package org.jabref.logic.importer.fileformat;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.function.Predicate;
import java.util.stream.Stream;

import org.jabref.logic.bibtex.BibEntryAssert;
import org.jabref.model.entry.BibEntry;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class BibTeXMLImporterTestFiles {

private static final Pattern PATTERN = Pattern.compile("\\D*[0123456789]");

private BibTeXMLImporter bibtexmlImporter;

@Parameter
public Path importFile;
private static final String FILE_ENDING = ".xml";


@Before
public void setUp() {
bibtexmlImporter = new BibTeXMLImporter();
private static Stream<String> fileNames() throws IOException {
Predicate<String> fileName = name -> name.startsWith("BibTeXMLImporterTest")
&& name.endsWith(FILE_ENDING);
return ImporterTestEngine.getTestFiles(fileName).stream();
}

@Parameters(name = "{0}")
public static List<Path> files() throws Exception {
try (Stream<Path> stream = Files.list(Paths.get(BibTeXMLImporterTest.class.getResource("").toURI()))) {
return stream.filter(n -> n.getFileName().toString().startsWith("BibTeXMLImporterTest")
&& n.getFileName().toString().endsWith(".xml")).collect(Collectors.toList());
}
private static Stream<String> nonBibTeXMLfileNames() throws IOException {
Predicate<String> fileName = name -> !name.startsWith("BibTeXMLImporterTest");
return ImporterTestEngine.getTestFiles(fileName).stream();
}

@Test
public void testIsRecognizedFormat() throws IOException {
Assert.assertTrue(bibtexmlImporter.isRecognizedFormat(importFile, StandardCharsets.UTF_8));
@ParameterizedTest
@MethodSource("fileNames")
public void testIsRecognizedFormat(String fileName) throws IOException {
ImporterTestEngine.testIsRecognizedFormat(new BibTeXMLImporter(), fileName);
}

@Test
public void testImportEntries() throws IOException {
List<BibEntry> bibtexmlEntries = bibtexmlImporter.importDatabase(importFile, StandardCharsets.UTF_8)
.getDatabase().getEntries();
@ParameterizedTest
@MethodSource("nonBibTeXMLfileNames")
public void testIsNotRecognizedFormat(String fileName) throws IOException {
ImporterTestEngine.testIsNotRecognizedFormat(new BibTeXMLImporter(), fileName);
}

String bibFileName = importFile.getFileName().toString().replace(".xml", ".bib");
while (PATTERN.matcher(bibFileName).find()) {
bibFileName = bibFileName.replaceFirst("[0123456789]", "");
}
if (bibtexmlEntries.isEmpty()) {
Assert.assertEquals(Collections.emptyList(), bibtexmlEntries);
} else {
BibEntryAssert.assertEquals(BibTeXMLImporterTest.class, bibFileName, bibtexmlEntries);
}
@ParameterizedTest
@MethodSource("fileNames")
public void testImportEntries(String fileName) throws IOException {
ImporterTestEngine.testImportEntries(new BibTeXMLImporter(), fileName, FILE_ENDING);
}

}
Loading

0 comments on commit 608e415

Please sign in to comment.