forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test BibTeXMLImporter - Remove other field from BibTeXMLHandler - Add .gitignore for backup files ending with '~'
- Loading branch information
1 parent
39724ee
commit 3c5e663
Showing
38 changed files
with
682 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package net.sf.jabref.importer.fileformat; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import net.sf.jabref.Globals; | ||
import net.sf.jabref.JabRefPreferences; | ||
import net.sf.jabref.importer.OutputPrinterToNull; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
|
||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class BibTeXMLImporterTest { | ||
|
||
private final String FILEFORMAT_PATH = "src/test/resources/net/sf/jabref/importer/fileformat"; | ||
|
||
|
||
/** | ||
* Generates a List of all files in the package "/src/test/resources/net/sf/jabref/importer/fileformat" | ||
* @return A list of Names | ||
* @throws IOException | ||
*/ | ||
public List<String> getTestFiles() throws IOException { | ||
List<String> files = new ArrayList<>(); | ||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(FILEFORMAT_PATH))) { | ||
stream.forEach(n -> files.add(n.getFileName().toString())); | ||
} | ||
return files; | ||
|
||
} | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
Globals.prefs = JabRefPreferences.getInstance(); | ||
} | ||
|
||
@Test | ||
public void testExceptionOnInputStream() throws IOException { | ||
try (InputStream is = Mockito.mock(InputStream.class)) { | ||
Mockito.doThrow(new IOException()).when(is).read(); | ||
|
||
BibTeXMLImporter importer = new BibTeXMLImporter(); | ||
List<BibEntry> entry = importer.importEntries(is, new OutputPrinterToNull()); | ||
Assert.assertTrue(entry.isEmpty()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetItemsEmpty() { | ||
BibTeXMLHandler handler = new BibTeXMLHandler(); | ||
Assert.assertEquals(Collections.emptyList(), handler.getItems()); | ||
} | ||
|
||
@Test | ||
public void testGetFormatName() { | ||
BibTeXMLImporter importer = new BibTeXMLImporter(); | ||
Assert.assertEquals("BibTeXML", importer.getFormatName()); | ||
} | ||
|
||
@Test | ||
public void testGetCLIId() { | ||
BibTeXMLImporter importer = new BibTeXMLImporter(); | ||
Assert.assertEquals("bibtexml", importer.getCLIId()); | ||
} | ||
|
||
@Test | ||
public void testIsRecognizedFormatReject() throws IOException { | ||
BibTeXMLImporter importer = new BibTeXMLImporter(); | ||
|
||
List<String> list = getTestFiles().stream().filter(n -> !n.startsWith("BibTeXMLImporterTest")) | ||
.collect(Collectors.toList()); | ||
|
||
for (String str : list) { | ||
try (InputStream is = BibTeXMLImporter.class.getResourceAsStream(str)) { | ||
Assert.assertFalse(importer.isRecognizedFormat(is)); | ||
} | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestFiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package net.sf.jabref.importer.fileformat; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import net.sf.jabref.Globals; | ||
import net.sf.jabref.JabRefPreferences; | ||
import net.sf.jabref.bibtex.BibtexEntryAssert; | ||
import net.sf.jabref.importer.OutputPrinterToNull; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
|
||
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 final static String FILEFORMAT_PATH = "src/test/resources/net/sf/jabref/importer/fileformat"; | ||
|
||
private BibTeXMLImporter bibtexmlImporter; | ||
|
||
@Parameter | ||
public String fileName; | ||
|
||
|
||
@Before | ||
public void setUp() { | ||
Globals.prefs = JabRefPreferences.getInstance(); | ||
bibtexmlImporter = new BibTeXMLImporter(); | ||
} | ||
|
||
@Parameters(name = "{0}") | ||
public static Collection<String> fileNames() throws IOException { | ||
List<String> files = new ArrayList<>(); | ||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(FILEFORMAT_PATH))) { | ||
stream.forEach(n -> files.add(n.getFileName().toString())); | ||
} | ||
return files.stream().filter(n -> n.startsWith("BibTeXMLImporterTest")).filter(n -> n.endsWith(".xml")) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Test | ||
public void testIsRecognizedFormat() throws IOException { | ||
try (InputStream stream = BibTeXMLImporterTest.class.getResourceAsStream(fileName)) { | ||
Assert.assertTrue(bibtexmlImporter.isRecognizedFormat(stream)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testImportEntries() throws IOException { | ||
try (InputStream bitexmlStream = BibTeXMLImporterTest.class.getResourceAsStream(fileName)) { | ||
List<BibEntry> bibtexmlEntries = bibtexmlImporter.importEntries(bitexmlStream, new OutputPrinterToNull()); | ||
|
||
String bibFileName = fileName.replace(".xml", ".bib"); | ||
while (PATTERN.matcher(bibFileName).find()) { | ||
bibFileName = bibFileName.replaceFirst("[0123456789]", ""); | ||
} | ||
if (bibtexmlEntries.isEmpty()) { | ||
Assert.assertEquals(Collections.emptyList(), bibtexmlEntries); | ||
} else { | ||
BibtexEntryAssert.assertEquals(BibTeXMLImporterTest.class, bibFileName, bibtexmlEntries); | ||
} | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package net.sf.jabref.importer.fileformat; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import net.sf.jabref.Globals; | ||
import net.sf.jabref.JabRefPreferences; | ||
import net.sf.jabref.bibtex.BibtexEntryAssert; | ||
import net.sf.jabref.importer.OutputPrinterToNull; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
|
||
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 BibTeXMLImporterTestTypes { | ||
|
||
private BibTeXMLImporter bibteXMLImporter; | ||
|
||
@Parameter(value = 0) | ||
public String bibteXMLType; | ||
|
||
@Parameter(value = 1) | ||
public String expectedBibType; | ||
|
||
|
||
@Parameters | ||
public static Collection<String[]> types() { | ||
return Arrays.asList(new String[][] {{"journal", "article"}, {"book section", "inbook"}, {"book", "book"}, | ||
{"conference", "inproceedings"}, {"proceedings", "inproceedings"}, {"report", "techreport"}, | ||
{"master thesis", "mastersthesis"}, {"thesis", "phdthesis"}, {"master", "misc"}}); | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
Globals.prefs = JabRefPreferences.getInstance(); | ||
bibteXMLImporter = new BibTeXMLImporter(); | ||
} | ||
|
||
@Test | ||
public void importConvertsToCorrectBibType() throws IOException { | ||
String bibteXMLInput = "<?xml version=\"1.0\" ?>\n" + "<bibtex:file xmlns:bibtex=\"http://bibtexml.sf.net/\">\n" | ||
+ "<bibtex:entry>\n" + "<bibtex:" + expectedBibType + ">\n" | ||
+ "<bibtex:author>Max Mustermann</bibtex:author>\n" + "<bibtex:keywords>java</bibtex:keywords>\n" | ||
+ "<bibtex:title>Java tricks</bibtex:title>\n" + "<bibtex:year>2016</bibtex:year>\n" + "</bibtex:" | ||
+ expectedBibType + ">\n" + "</bibtex:entry>\n" + "</bibtex:file>"; | ||
|
||
List<BibEntry> bibEntries = bibteXMLImporter.importEntries( | ||
new ByteArrayInputStream(bibteXMLInput.getBytes(StandardCharsets.UTF_8)), new OutputPrinterToNull()); | ||
|
||
BibEntry entry = new BibEntry(); | ||
entry.setField("author", "Max Mustermann"); | ||
entry.setField("keywords", "java"); | ||
entry.setField("title", "Java tricks"); | ||
entry.setField("year", "2016"); | ||
entry.setType(expectedBibType); | ||
|
||
Assert.assertEquals(1, bibEntries.size()); | ||
BibtexEntryAssert.assertEquals(entry, bibEntries.get(0)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/test/resources/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestArticle.bib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
% Encoding: UTF-8 | ||
@Article{Mustermann2016, | ||
author = {Max Mustermann}, | ||
title = {Java tricks}, | ||
journal = {Java Journal}, | ||
year = {2016}, | ||
pages = {2}, | ||
month = {February}, | ||
keywords = {java} | ||
} |
Oops, something went wrong.