Skip to content

Commit

Permalink
Test CopacImporter
Browse files Browse the repository at this point in the history
  • Loading branch information
zesaro committed Apr 13, 2016
1 parent 2e4fdad commit 1e33511
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 62 deletions.
107 changes: 50 additions & 57 deletions src/test/java/net/sf/jabref/importer/fileformat/CopacImporterTest.java
Original file line number Diff line number Diff line change
@@ -1,91 +1,84 @@
package net.sf.jabref.importer.fileformat;

import net.sf.jabref.*;

import net.sf.jabref.importer.OutputPrinterToNull;
import net.sf.jabref.model.entry.BibEntry;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class CopacImporterTest {

@Before
public void setUp() throws Exception {
if (Globals.prefs == null) {
Globals.prefs = JabRefPreferences.getInstance();
}
}

@Test
public void testIsRecognizedFormat() throws IOException {
private final List<String> testFiles = getTestFiles();

CopacImporter importer = new CopacImporter();
try (InputStream stream1 = CopacImporterTest.class
.getResourceAsStream("CopacImporterTest1.txt"); InputStream stream2 = CopacImporterTest.class
.getResourceAsStream("CopacImporterTest2.txt"); InputStream stream3 = CopacImporterTest.class
.getResourceAsStream("IsiImporterTest1.isi"); InputStream stream4 = CopacImporterTest.class
.getResourceAsStream("IsiImporterTestInspec.isi"); InputStream stream5 = CopacImporterTest.class
.getResourceAsStream("IsiImporterTestWOS.isi"); InputStream stream6 = CopacImporterTest.class
.getResourceAsStream("IsiImporterTestMedline.isi")) {

Assert.assertTrue(importer.isRecognizedFormat(stream1));

Assert.assertTrue(importer.isRecognizedFormat(stream2));

Assert.assertFalse(importer.isRecognizedFormat(stream3));
/**
* Generates a List of all files in the package "/src/test/resources/net/sf/jabref/importer/fileformat"
* @return A list of Names
*/
public List<String> getTestFiles() {
List<String> files = new ArrayList<>();
File d = new File(System.getProperty("user.dir") + "/src/test/resources/net/sf/jabref/importer/fileformat");
for (File f : d.listFiles()) {
files.add(f.getName());
}
return files;

Assert.assertFalse(importer.isRecognizedFormat(stream4));
}

Assert.assertFalse(importer.isRecognizedFormat(stream5));
@BeforeClass
public static void setUp() {
Globals.prefs = JabRefPreferences.getInstance();
}

Assert.assertFalse(importer.isRecognizedFormat(stream6));
}
@Test(expected = IOException.class)
public void testImportEntriesException() throws IOException {
CopacImporter importer = new CopacImporter();
importer.importEntries(null, new OutputPrinterToNull());
}

@Test
public void testImportEntries() throws IOException {
Globals.prefs.put("defaultEncoding", StandardCharsets.UTF_8.name());

public void testGetFormatName() {
CopacImporter importer = new CopacImporter();
Assert.assertEquals("Copac", importer.getFormatName());
}

try (InputStream stream = CopacImporterTest.class
.getResourceAsStream("CopacImporterTest1.txt")) {
List<BibEntry> entries = importer.importEntries(stream, new OutputPrinterToNull());
Assert.assertEquals(1, entries.size());
BibEntry entry = entries.get(0);

Assert.assertEquals("The SIS project : software reuse with a natural language approach", entry.getField("title"));
Assert.assertEquals(
"Prechelt, Lutz and Universität Karlsruhe. Fakultät für Informatik",
entry.getField("author"));
Assert.assertEquals("Interner Bericht ; Nr.2/92", entry.getField("series"));
Assert.assertEquals("1992", entry.getField("year"));
Assert.assertEquals("Karlsruhe : Universitat Karlsruhe, Fakultat fur Informatik", entry.getField("publisher"));
Assert.assertEquals("book", entry.getType());
}
@Test
public void testGetCLIId() {
CopacImporter importer = new CopacImporter();
Assert.assertEquals("cpc", importer.getCLIId());
}

@Test
public void testImportEntries2() throws IOException {
public void testIsRecognizedFormatReject() throws IOException {
CopacImporter importer = new CopacImporter();

try (InputStream stream = CopacImporterTest.class
.getResourceAsStream("CopacImporterTest2.txt")) {
List<BibEntry> entries = importer.importEntries(stream, new OutputPrinterToNull());
Assert.assertEquals(2, entries.size());
BibEntry one = entries.get(0);
List<String> list = testFiles.stream().filter(n -> !n.startsWith("CopacImporterTest"))
.filter(n -> !n.startsWith("InspecSilverPlatterImporterTest")).collect(Collectors.toList());

Assert.assertEquals("Computing and operational research at the London Hospital", one.getField("title"));
for (String str : list) {
try (InputStream is = CopacImporterTest.class.getResourceAsStream(str)) {
Assert.assertFalse(importer.isRecognizedFormat(is));
}
}
}

BibEntry two = entries.get(1);
@Test
public void testImportEmptyEntries() throws IOException {
CopacImporter importer = new CopacImporter();

Assert.assertEquals("Real time systems : management and design", two.getField("title"));
try (InputStream is = CopacImporterTest.class.getResourceAsStream("Empty.txt")) {
List<BibEntry> entries = importer.importEntries(is, new OutputPrinterToNull());
Assert.assertEquals(0, entries.size());
Assert.assertEquals(Collections.emptyList(), entries);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package net.sf.jabref.importer.fileformat;

import net.sf.jabref.*;
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;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@RunWith(Parameterized.class)
public class CopacImporterTestFiles {

private CopacImporter copacImporter;

@Parameter
public String fileName;


@Before
public void setUp() {
Globals.prefs = JabRefPreferences.getInstance();
copacImporter = new CopacImporter();
}

@Parameters(name = "{0}")
public static Collection<String> fileNames() {
List<String> files = new ArrayList<>();
File d = new File(System.getProperty("user.dir") + "/src/test/resources/net/sf/jabref/importer/fileformat");
for (File f : d.listFiles()) {
files.add(f.getName());
}
return files.stream().filter(n -> n.startsWith("CopacImporterTest")).filter(n -> n.endsWith(".txt"))
.collect(Collectors.toList());
}

@Test
public void testIsRecognizedFormat() throws IOException {
try (InputStream stream = CopacImporterTest.class.getResourceAsStream(fileName)) {
Assert.assertTrue(copacImporter.isRecognizedFormat(stream));
}
}

@Test
public void testImportEntries() throws IOException {
try (InputStream copacStream = CopacImporterTest.class.getResourceAsStream(fileName)) {

List<BibEntry> copacEntries = copacImporter.importEntries(copacStream, new OutputPrinterToNull());
fileName = fileName.replace(".txt", ".bib");

Assert.assertFalse(copacEntries.isEmpty());

int size = copacEntries.size();

// workaround because BibtexEntryAssert can only test 1 entry
if (size != 1) {
for (int i = 1; i <= size; i++) {
fileName = fileName.replaceAll(".bib", "-" + i + ".bib");
BibtexEntryAssert.assertEquals(CopacImporterTest.class, fileName, copacEntries.get(i - 1));
fileName = fileName.replaceAll("-" + i + ".bib", ".bib");
}
} else {
BibtexEntryAssert.assertEquals(CopacImporterTest.class, fileName, copacEntries);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@Book{,
title = {The SIS project : software reuse with a natural language approach},
publisher = {Karlsruhe : Universitat Karlsruhe, Fakultat fur Informatik},
year = {1992},
author = {Prechelt, Lutz and Universität Karlsruhe. Fakultät für Informatik},
series = {Interner Bericht ; Nr.2/92},
documenttype = {TXT},
hl = {Edinburgh}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TI- The SIS project : software reuse with a natural language approach
AU- Prechelt, Lutz
AU- Universität Karlsruhe. Fakultät für Informatik
SE- Interner Bericht ; Nr.2/92
PU- Karlsruhe : Universitat Karlsruhe, Fakultat fur Informatik
PU- Karlsruhe : Universitat Karlsruhe, Fakultat fur Informatik
PY- 1992
HL- Edinburgh

DT- TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@Book{,
title = {Computing and operational research at the London Hospital},
publisher = {London : Butterworths},
year = {1972},
author = {Barber, Barry and Abbott, W.},
series = {Computers in medicine series},
note = {Bibl.p.94-97. - Index},
documenttype = {TXT, PDF},
hl = {Aberdeen ; Birmingham ; Edinburgh ; Trinity College Dublin ; UCL (University College London)},
isbn = {0407517006 (Pbk)},
keywords = {London Hospital and Medical College, Electronic data processing - Medicine, Computers - Hospital administration, Hospital planning, Operations research, Hospital equipment and supplies, Electronic data processing - Hospitals - Administration, Hospitals, London, London Hospital and Medical College, Records management, Applications of computer systems, to 1971},
physicaldimensions = {x, 102p, leaf : ill., form, port ; 22cm}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@Book{,
title = {Real time systems : management and design},
publisher = {London ; New York : McGraw-Hill},
year = {1977},
author = {Tebbs, David and Collins, Garfield},
note = {Index},
documenttype = {TXT, PDF},
hl = {Aberdeen ; Birmingham ; Edinburgh ; Imperial College ; Liverpool ; Manchester ; Oxford ; Trinity College Dublin},
isbn = {0070844828},
keywords = {Real-time data processing - Management, Real time computer systems, Design},
physicaldimensions = {ix, 357p : ill., forms ; 24cm}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ TI- Computing and operational research at the London Hospital
AU- Barber, Barry
AU- Abbott, W.
SE- Computers in medicine series
PU- London : Butterworths
PU- London : Butterworths
PY- 1972
PD- x, 102p, leaf : ill., form, port ; 22cm
IS- 0407517006 (Pbk)
Expand All @@ -18,12 +18,13 @@ KW- Hospitals, London, London Hospital and Medical College, Records
management, Applications of computer systems, to 1971
HL- Aberdeen ; Birmingham ; Edinburgh ; Trinity College Dublin ;
UCL (University College London)

DT- TXT
DT- PDF

TI- Real time systems : management and design
AU- Tebbs, David
AU- Collins, Garfield
PU- London ; New York : McGraw-Hill
PU- London ; New York : McGraw-Hill
PY- 1977
PD- ix, 357p : ill., forms ; 24cm
IS- 0070844828
Expand All @@ -32,3 +33,4 @@ KW- Real-time data processing - Management
KW- Real time computer systems, Design
HL- Aberdeen ; Birmingham ; Edinburgh ; Imperial College ;
Liverpool ; Manchester ; Oxford ; Trinity College Dublin
DT- TXT, PDF

0 comments on commit 1e33511

Please sign in to comment.