Skip to content

Commit

Permalink
Fix OutOfBoundsException when importing multiple entries in medline f…
Browse files Browse the repository at this point in the history
…ormat (#1611)

* add test for invalid format and null reader
* change encoding to utf-8
  • Loading branch information
tschechlovdev authored and stefan-kolb committed Aug 3, 2016
1 parent f77daac commit f6ccf27
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
}
}
String entry = current.toString();
if (!checkLineValidity(entry)) {
continue;
}

String label = entry.substring(0, entry.indexOf('-')).trim();
String value = entry.substring(entry.indexOf('-') + 1).trim();
Expand Down Expand Up @@ -227,13 +230,16 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
// create one here
b.setField(fields);
bibitems.add(b);

}

return new ParserResult(bibitems);

}

private boolean checkLineValidity(String line) {
return (line.length() >= 5) && (line.charAt(4) == '-');
}

private String addSourceType(String value, String type) {
String val = value.toLowerCase(Locale.ENGLISH);
String theType = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.codec.Charsets;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

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

public class MedlinePlainImporterTest {

Expand Down Expand Up @@ -197,6 +199,30 @@ public void testWithNbibFile() throws IOException, URISyntaxException {
BibEntryAssert.assertEquals(MedlinePlainImporter.class, "NbibImporterTest.bib", entries);
}

@Test
public void testWithMultipleEntries() throws IOException, URISyntaxException {
Path file = Paths
.get(MedlinePlainImporter.class.getResource("MedlinePlainImporterStringOutOfBounds.txt").toURI());
List<BibEntry> entries = importer.importDatabase(file, Charsets.UTF_8).getDatabase().getEntries();
BibEntryAssert.assertEquals(MedlinePlainImporter.class, "MedlinePlainImporterStringOutOfBounds.bib", entries);
}

@Test
public void testInvalidFormat() throws URISyntaxException, IOException {
Path file = Paths
.get(MedlinePlainImporter.class.getResource("MedlinePlainImporterTestInvalidFormat.xml").toURI());
List<BibEntry> entries = importer.importDatabase(file, Charsets.UTF_8).getDatabase().getEntries();
assertEquals(Collections.emptyList(), entries);
}

@Test(expected = NullPointerException.class)
public void testNullReader() throws IOException {
try (BufferedReader reader = null) {
importer.importDatabase(reader);
}
fail();
}

@Test
public void testAllArticleTypes() throws IOException {
try (BufferedReader reader = readerForString("PMID-22664795" + "\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@misc{,
title = {This is a test title}
}, @misc{,
title = {This is also a test title}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

PMID- 27433151
SO invalid line
TI - This is a test title

PMID- 27394443
SO- another invalid line
TI -This is also a test title
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PMC some PMC
PMID


SO-something stands here

0 comments on commit f6ccf27

Please sign in to comment.