package org.jabref.logic.exporter; import java.io.File; import java.nio.charset.Charset; import java.nio.file.Files; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jabref.logic.layout.LayoutFormatterPreferences; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import com.google.common.base.Charsets; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.mockito.Answers; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; public class CsvExportFormatTest { private IExportFormat exportFormat; public BibDatabaseContext databaseContext; public Charset charset; @Rule public TemporaryFolder testFolder = new TemporaryFolder(); @Before public void setUp() { Map<String, ExportFormat> customFormats = new HashMap<>(); LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS); SavePreferences savePreferences = mock(SavePreferences.class); ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences); exportFormat = ExportFormats.getExportFormat("oocsv"); databaseContext = new BibDatabaseContext(); charset = Charsets.UTF_8; } @After public void tearDown() { exportFormat = null; } @Test public void testPerformExportForSingleAuthor() throws Exception { File tmpFile = testFolder.newFile(); String filename = tmpFile.getCanonicalPath(); BibEntry entry = new BibEntry(); entry.setField("author", "Someone, Van Something"); List<BibEntry> entries = Arrays.asList(entry); exportFormat.performExport(databaseContext, filename, charset, entries); List<String> lines = Files.readAllLines(tmpFile.toPath()); assertEquals(2, lines.size()); assertEquals( "10,\"\",\"\",\"Someone, Van Something\",\"\",\"\",,,\"\",\"\",,\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\"", lines.get(1)); } @Test public void testPerformExportForMultipleAuthors() throws Exception { File tmpFile = testFolder.newFile(); String filename = tmpFile.getCanonicalPath(); BibEntry entry = new BibEntry(); entry.setField("author", "von Neumann, John and Smith, John and Black Brown, Peter"); List<BibEntry> entries = Arrays.asList(entry); exportFormat.performExport(databaseContext, filename, charset, entries); List<String> lines = Files.readAllLines(tmpFile.toPath()); assertEquals(2, lines.size()); assertEquals( "10,\"\",\"\",\"von Neumann, John; Smith, John; Black Brown, Peter\",\"\",\"\",,,\"\",\"\",,\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\"", lines.get(1)); } @Test public void testPerformExportForSingleEditor() throws Exception { File tmpFile = testFolder.newFile(); String filename = tmpFile.getCanonicalPath(); BibEntry entry = new BibEntry(); entry.setField("editor", "Someone, Van Something"); List<BibEntry> entries = Arrays.asList(entry); exportFormat.performExport(databaseContext, filename, charset, entries); List<String> lines = Files.readAllLines(tmpFile.toPath()); assertEquals(2, lines.size()); assertEquals( "10,\"\",\"\",\"\",\"\",\"\",,,\"\",\"\",,\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"Someone, Van Something\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\"", lines.get(1)); } @Test public void testPerformExportForMultipleEditors() throws Exception { File tmpFile = testFolder.newFile(); String filename = tmpFile.getCanonicalPath(); BibEntry entry = new BibEntry(); entry.setField("editor", "von Neumann, John and Smith, John and Black Brown, Peter"); List<BibEntry> entries = Arrays.asList(entry); exportFormat.performExport(databaseContext, filename, charset, entries); List<String> lines = Files.readAllLines(tmpFile.toPath()); assertEquals(2, lines.size()); assertEquals( "10,\"\",\"\",\"\",\"\",\"\",,,\"\",\"\",,\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"von Neumann, John; Smith, John; Black Brown, Peter\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\"", lines.get(1)); } }