Skip to content

Commit

Permalink
Use parameterized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zesaro committed Apr 7, 2016
1 parent 6805aca commit c82ff37
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
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.BeforeClass;
import org.junit.Test;

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

public class CopacImporterTest {

private final List<String> testFiles = getTestFiles();


/**
* 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;

}

@BeforeClass
public static void setUp() {
Globals.prefs = JabRefPreferences.getInstance();
Expand All @@ -39,26 +57,12 @@ public void testGetCLIId() {
Assert.assertEquals("cpc", importer.getCLIId());
}

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

List<String> list = Arrays.asList("CopacImporterTest1.txt", "CopacImporterTest2.txt");

for (String str : list) {
try (InputStream is = CopacImporterTest.class.getResourceAsStream(str)) {
Assert.assertTrue(importer.isRecognizedFormat(is));
}
}
}

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

List<String> list = Arrays.asList("Empty.txt", "IEEEImport1.txt", "IsiImporterTest1.isi",
"IsiImporterTestInspec.isi", "IsiImporterTestWOS.isi", "IsiImporterTestMedline.isi",
"RisImporterTest1.ris");
List<String> list = testFiles.stream().filter(n -> !n.startsWith("CopacImporterTest"))
.collect(Collectors.toList());

for (String str : list) {
try (InputStream is = CopacImporterTest.class.getResourceAsStream(str)) {
Expand All @@ -67,44 +71,6 @@ public void testIsRecognizedFormatReject() throws IOException {
}
}

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

CopacImporter importer = new CopacImporter();

try (InputStream is = CopacImporter.class.getResourceAsStream("CopacImporterTest1.txt");
InputStream bibis = CopacImporter.class.getResourceAsStream("CopacImporterTest1.bib")) {
List<BibEntry> entries = importer.importEntries(is, new OutputPrinterToNull());
Assert.assertFalse(entries.isEmpty());

BibEntry entry = entries.get(0);

BibtexEntryAssert.assertEquals(bibis, entry);
}
}

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

CopacImporter importer = new CopacImporter();

try (InputStream is = CopacImporter.class.getResourceAsStream("CopacImporterTest2.txt");
InputStream bibis1 = CopacImporter.class.getResourceAsStream("CopacImporterTest21.bib");
InputStream bibis2 = CopacImporter.class.getResourceAsStream("CopacImporterTest22.bib")) {
List<BibEntry> entries = importer.importEntries(is, new OutputPrinterToNull());
Assert.assertFalse(entries.isEmpty());

BibEntry entry1 = entries.get(0);
BibEntry entry2 = entries.get(1);

BibtexEntryAssert.assertEquals(bibis1, entry1);
BibtexEntryAssert.assertEquals(bibis2, entry2);
}
}


@Test
public void testImportEmptyEntries() throws IOException {
CopacImporter importer = new CopacImporter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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")).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", "");

int size = copacEntries.size();

if (size != 1) {
for (int i = 1; i <= size; i++) {
BibtexEntryAssert.assertEquals(CopacImporterTest.class, "Bib" + fileName + "-" + i + ".bib",
copacEntries.get(i - 1));
}
} else {
BibtexEntryAssert.assertEquals(CopacImporterTest.class, "Bib" + fileName + ".bib", copacEntries);
}
}
}

}

0 comments on commit c82ff37

Please sign in to comment.