Skip to content

Commit

Permalink
BibTeXMLImporterTest (JabRef#511)
Browse files Browse the repository at this point in the history
Test BibTeXMLImporter

- Remove other field from BibTeXMLHandler
- Add .gitignore for backup files ending with '~'
  • Loading branch information
zesaro authored and simonharrer committed Apr 15, 2016
1 parent 39724ee commit 3c5e663
Show file tree
Hide file tree
Showing 38 changed files with 682 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ src/main/gen/
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err*

# backup files
*~
# backup files from JabRef itself
*.bak

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void resetImportFormats() {

formats.add(new BiblioscapeImporter());
formats.add(new BibtexImporter());
formats.add(new BibteXMLImporter());
formats.add(new BibTeXMLImporter());
formats.add(new CopacImporter());
formats.add(new EndnoteImporter());
formats.add(new FreeCiteImporter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
*/
class BibTeXMLHandler extends DefaultHandler {

private static final String BIBTEXML_URI = "http://bibtexml.sf.net/";

private List<BibEntry> bibitems;

private BibEntry b; // the entry being read

// XML parsing stuff
private String currentChars;


public List<BibEntry> getItems() {
if (bibitems == null) {
return Collections.emptyList();
Expand All @@ -53,11 +56,6 @@ public void startDocument() {
bibitems = new ArrayList<>();
}

@Override
public void endDocument() {
// Empty method
}

@Override
public void characters(char[] ch, int start, int length) {
String s = new String(ch, start, length).trim();
Expand All @@ -66,41 +64,41 @@ public void characters(char[] ch, int start, int length) {

@Override
public void startElement(String uri, String local, String raw, Attributes atts) {
if ("bibtex:entry".equals(raw)) {
String articleID = null;
for (int i = 0; i < atts.getLength(); i++) {
if ("bibtex:id".equals(atts.getQName(i)) ||
"id".equals(atts.getQName(i))) {
articleID = atts.getValue(i);
if (BIBTEXML_URI.equals(uri)) {
if ("entry".equals(local)) {
b = new BibEntry(IdGenerator.next());
// Determine and-set bibtex key
String bibtexKey = null;
for (int i = 0; i < atts.getLength(); i++) {
String attrURI = atts.getURI(i);
if ((BIBTEXML_URI.equals(attrURI) || "".equals(attrURI)) && "id".equals(atts.getLocalName(i))) {
bibtexKey = atts.getValue(i);
}
}
if (bibtexKey != null) {
b.setField(BibEntry.KEY_FIELD, bibtexKey);
}
} else if ("article".equals(local) || "inbook".equals(local) || "book".equals(local)
|| "booklet".equals(local) || "incollection".equals(local) || "inproceedings".equals(local)
|| "proceedings".equals(local) || "manual".equals(local) || "mastersthesis".equals(local)
|| "phdthesis".equals(local) || "techreport".equals(local) || "unpublished".equals(local)
|| "misc".equals(local)) {
b.setType(local);
}
b = new BibEntry(IdGenerator.next());
b.setField(BibEntry.KEY_FIELD, articleID);
} else if ("bibtex:article".equals(raw) ||
"bibtex:inbook".equals(raw) ||
"bibtex:book".equals(raw) ||
"bibtex:booklet".equals(raw) ||
"bibtex:incollection".equals(raw) ||
"bibtex:inproceedings".equals(raw) ||
"bibtex:proceedings".equals(raw) ||
"bibtex:manual".equals(raw) ||
"bibtex:mastersthesis".equals(raw) ||
"bibtex:phdthesis".equals(raw) ||
"bibtex:techreport".equals(raw) ||
"bibtex:unpublished".equals(raw) ||
"bibtex:misc".equals(raw) ||
"bibtex:other".equals(raw)) {
b.setType(local);
}
currentChars = "";
}

@Override
public void endElement(String uri, String local, String raw) {
if ("bibtex:entry".equals(raw)) {
bibitems.add(b);
} else if (raw.startsWith("bibtex:")) {
b.setField(local, currentChars);
if (BIBTEXML_URI.equals(uri)) {
if ("entry".equals(local)) {
bibitems.add(b);
} else {
if (!currentChars.trim().isEmpty()) {
b.setField(local, currentChars);
}
}
}
currentChars = "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
* check here for details on the format
* http://www.ecst.csuchico.edu/~jacobsd/bib/formats/endnote.html
*/
public class BibteXMLImporter extends ImportFormat {
public class BibTeXMLImporter extends ImportFormat {

private static final Log LOGGER = LogFactory.getLog(BibteXMLImporter.class);
private static final Log LOGGER = LogFactory.getLog(BibTeXMLImporter.class);

private static final Pattern START_PATTERN = Pattern.compile("<bibtex:file .*");
private static final Pattern START_PATTERN = Pattern.compile("<(bibtex:)?file .*");


/**
Expand Down
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));
}
}
}
}
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);
}
}
}
}
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));
}
}
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}
}
Loading

0 comments on commit 3c5e663

Please sign in to comment.