Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for InspecImporter #356

Merged
merged 14 commits into from
Apr 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/main/java/net/sf/jabref/importer/fileformat/InspecImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

Expand Down Expand Up @@ -60,22 +61,15 @@ public String getCLIId() {
@Override
public boolean isRecognizedFormat(InputStream stream) throws IOException {
// Our strategy is to look for the "PY <year>" line.
BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream));
//Pattern pat1 = Pattern.compile("PY: \\d{4}");

//was PY \\\\d{4}? before
String str;

while ((str = in.readLine()) != null) {
//Inspec and IEEE seem to have these strange " - " between key and value
//str = str.replace(" - ", "");
//System.out.println(str);
try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) {
String str;

if (INSPEC_PATTERN.matcher(str).find()) {
return true;
while ((str = in.readLine()) != null) {
if (INSPEC_PATTERN.matcher(str).find()) {
return true;
}
}
}

return false;
}

Expand All @@ -84,7 +78,7 @@ public boolean isRecognizedFormat(InputStream stream) throws IOException {
*/
@Override
public List<BibEntry> importEntries(InputStream stream, OutputPrinter status) throws IOException {
ArrayList<BibEntry> bibitems = new ArrayList<>();
List<BibEntry> bibitems = new ArrayList<>();
StringBuilder sb = new StringBuilder();
String str;
try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) {
Expand All @@ -101,7 +95,7 @@ public List<BibEntry> importEntries(InputStream stream, OutputPrinter status) th
}
String[] entries = sb.toString().split("__::__");
String type = "";
HashMap<String, String> h = new HashMap<>();
Map<String, String> h = new HashMap<>();
for (String entry : entries) {
if (entry.indexOf("Record") != 0) {
continue;
Expand All @@ -110,7 +104,6 @@ public List<BibEntry> importEntries(InputStream stream, OutputPrinter status) th

String[] fields = entry.split("__NEWFIELD__");
for (String s : fields) {
//System.out.println(fields[j]);
String f3 = s.substring(0, 2);
String frest = s.substring(5);
if ("TI".equals(f3)) {
Expand All @@ -132,14 +125,15 @@ public List<BibEntry> importEntries(InputStream stream, OutputPrinter status) th
frest = frest.substring(m);
m = frest.indexOf(';');
if (m >= 5) {
String yr = frest.substring(m - 5, m);
String yr = frest.substring(m - 5, m).trim();
h.put("year", yr);
frest = frest.substring(m);
m = frest.indexOf(':');
if (m >= 0) {
String pg = frest.substring(m + 1).trim();
h.put("pages", pg);
h.put("volume", frest.substring(1, m));
String vol = frest.substring(1, m).trim();
h.put("volume", vol);
}
}
}
Expand Down
118 changes: 118 additions & 0 deletions src/test/java/net/sf/jabref/importer/fileformat/InspecImportTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package net.sf.jabref.importer.fileformat;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import net.sf.jabref.bibtex.BibtexEntryAssert;
import net.sf.jabref.importer.OutputPrinterToNull;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.BibtexEntryTypes;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefPreferences;
import org.junit.Before;
import org.junit.Test;

public class InspecImportTest {

private InspecImporter inspecImp;

@Before
public void setUp() throws Exception {
Globals.prefs = JabRefPreferences.getInstance();
this.inspecImp = new InspecImporter();
}

@Test
public void testIsRecognizedFormatAccept() throws IOException {
List<String> testList = Arrays.asList("InspecImportTest.txt", "InspecImportTest2.txt");
for (String str : testList) {
try (InputStream inStream = InspecImportTest.class.getResourceAsStream(str)) {
assertTrue(inspecImp.isRecognizedFormat(inStream));
}
}
}

@Test
public void testIsRecognizedFormatReject() throws IOException {
List<String> testList = Arrays.asList("CopacImporterTest1.txt", "CopacImporterTest2.txt",
"IEEEImport1.txt", "IsiImporterTest1.isi", "IsiImporterTestInspec.isi", "IsiImporterTestWOS.isi",
"IsiImporterTestMedline.isi", "RisImporterTest1.ris", "InspecImportTestFalse.txt");
for (String str : testList) {
try (InputStream inStream = InspecImportTest.class.getResourceAsStream(str)) {
assertFalse(inspecImp.isRecognizedFormat(inStream));
}
}
}

@Test
public void testCompleteBibtexEntryOnJournalPaperImport() throws IOException {

BibEntry shouldBeEntry = new BibEntry();
shouldBeEntry.setType("article");
shouldBeEntry.setField("title", "The SIS project : software reuse with a natural language approach");
shouldBeEntry.setField("author", "Prechelt, Lutz");
shouldBeEntry.setField("year", "1992");
shouldBeEntry.setField("abstract", "Abstrakt");
shouldBeEntry.setField("keywords", "key");
shouldBeEntry.setField("journal", "10000");
shouldBeEntry.setField("pages", "20");
shouldBeEntry.setField("volume", "19");

try (InputStream inStream = InspecImportTest.class.getResourceAsStream("InspecImportTest2.txt")) {
List<BibEntry> entries = inspecImp.importEntries(inStream, new OutputPrinterToNull());
assertEquals(1, entries.size());
BibEntry entry = entries.get(0);
BibtexEntryAssert.assertEquals(shouldBeEntry, entry);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace this with BibtexEntryAssert.assertEquals(Arrays.ListOf(shouldBeEntry), entries).


}
}

@Test
public void importConferencePaperGivesInproceedings() throws IOException {
String testInput = "Record.*INSPEC.*\n" +
"\n" +
"RT ~ Conference-Paper";
BibEntry shouldBeEntry = new BibEntry();
shouldBeEntry.setType("Inproceedings");

try (InputStream inStream = new ByteArrayInputStream(testInput.getBytes())) {
List<BibEntry> entries = inspecImp.importEntries(inStream, new OutputPrinterToNull());
assertEquals(1, entries.size());
BibEntry entry = entries.get(0);
BibtexEntryAssert.assertEquals(shouldBeEntry, entry);
}
}

@Test
public void importMiscGivesMisc() throws IOException {
String testInput = "Record.*INSPEC.*\n" +
"\n" +
"RT ~ Misc";
BibEntry shouldBeEntry = new BibEntry();
shouldBeEntry.setType("Misc");

try (InputStream inStream = new ByteArrayInputStream(testInput.getBytes())) {
List<BibEntry> entries = inspecImp.importEntries(inStream, new OutputPrinterToNull());
assertEquals(1, entries.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use BibtexEntryAssert.assertEquals(Collections.singletonList(shouldbeEntry), entries);

BibEntry entry = entries.get(0);
BibtexEntryAssert.assertEquals(shouldBeEntry, entry);
}
}

@Test
public void testGetFormatName() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"testGetFormatName failed" can be removed (it doesn't add any information...) same below

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix this

assertEquals("INSPEC", inspecImp.getFormatName());
}

@Test
public void testGetCLIId() {
assertEquals("inspec", inspecImp.getCLIId());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Record.testINSPEC.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Record.*INSPEC.*

TI ~ The SIS project : software reuse with a natural language approach
AU ~ Prechelt, Lutz
AB ~ Abstrakt
PU ~ Karlsruhe : Universitat Karlsruhe, Fakultat fur Informatik
PY ~ 10000
SO ~ 10000. 1992; 19: 20
ID ~ key
RT ~ Journal-Paper

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this should fail