diff --git a/src/main/java/net/sf/jabref/importer/fileformat/InspecImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/InspecImporter.java index 15455328216..1e4a883843e 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/InspecImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/InspecImporter.java @@ -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; @@ -60,22 +61,15 @@ public String getCLIId() { @Override public boolean isRecognizedFormat(InputStream stream) throws IOException { // Our strategy is to look for the "PY " 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; } @@ -84,7 +78,7 @@ public boolean isRecognizedFormat(InputStream stream) throws IOException { */ @Override public List importEntries(InputStream stream, OutputPrinter status) throws IOException { - ArrayList bibitems = new ArrayList<>(); + List bibitems = new ArrayList<>(); StringBuilder sb = new StringBuilder(); String str; try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { @@ -101,7 +95,7 @@ public List importEntries(InputStream stream, OutputPrinter status) th } String[] entries = sb.toString().split("__::__"); String type = ""; - HashMap h = new HashMap<>(); + Map h = new HashMap<>(); for (String entry : entries) { if (entry.indexOf("Record") != 0) { continue; @@ -110,7 +104,6 @@ public List 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)) { @@ -132,14 +125,15 @@ public List 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); } } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/InspecImportTest.java b/src/test/java/net/sf/jabref/importer/fileformat/InspecImportTest.java new file mode 100644 index 00000000000..c8d87ef8c35 --- /dev/null +++ b/src/test/java/net/sf/jabref/importer/fileformat/InspecImportTest.java @@ -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 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 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 entries = inspecImp.importEntries(inStream, new OutputPrinterToNull()); + assertEquals(1, entries.size()); + BibEntry entry = entries.get(0); + BibtexEntryAssert.assertEquals(shouldBeEntry, entry); + + } + } + + @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 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 entries = inspecImp.importEntries(inStream, new OutputPrinterToNull()); + assertEquals(1, entries.size()); + BibEntry entry = entries.get(0); + BibtexEntryAssert.assertEquals(shouldBeEntry, entry); + } + } + + @Test + public void testGetFormatName() { + assertEquals("INSPEC", inspecImp.getFormatName()); + } + + @Test + public void testGetCLIId() { + assertEquals("inspec", inspecImp.getCLIId()); + } + +} diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/InspecImportTest.txt b/src/test/resources/net/sf/jabref/importer/fileformat/InspecImportTest.txt new file mode 100644 index 00000000000..faf3e2524bc --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/InspecImportTest.txt @@ -0,0 +1 @@ +Record.testINSPEC.test \ No newline at end of file diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/InspecImportTest2.txt b/src/test/resources/net/sf/jabref/importer/fileformat/InspecImportTest2.txt new file mode 100644 index 00000000000..20d06b683fc --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/InspecImportTest2.txt @@ -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 + diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/InspecImportTestFalse.txt b/src/test/resources/net/sf/jabref/importer/fileformat/InspecImportTestFalse.txt new file mode 100644 index 00000000000..030102a8e7c --- /dev/null +++ b/src/test/resources/net/sf/jabref/importer/fileformat/InspecImportTestFalse.txt @@ -0,0 +1 @@ +this should fail \ No newline at end of file