Skip to content

Commit

Permalink
Add roundtrip test (#8425)
Browse files Browse the repository at this point in the history
* Add roundtrip test

* better file handling in test with temp dir

Co-authored-by: Siedlerchr <[email protected]>
  • Loading branch information
koppor and Siedlerchr authored Jan 13, 2022
1 parent b8f710d commit 6dddf93
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.logic.exporter;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
Expand All @@ -23,6 +24,8 @@
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.Importer;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.fileformat.BibtexImporter;
import org.jabref.logic.importer.fileformat.BibtexImporterTest;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.util.OS;
import org.jabref.model.database.BibDatabase;
Expand Down Expand Up @@ -51,6 +54,7 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Answers;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -408,6 +412,26 @@ void roundtripUtf8EncodingHeaderRemoved() throws Exception {
assertEquals(expected, stringWriter.toString());
}

@Test
void roundtripWin1252HeaderKept(@TempDir Path bibFolder) throws Exception {
when(generalPreferences.getDefaultEncoding()).thenReturn(StandardCharsets.UTF_8);
Path testFile = Path.of(BibtexImporterTest.class.getResource("encoding-windows-1252-with-header.bib").toURI());
ParserResult result = new BibtexImporter(importFormatPreferences, fileMonitor).importDatabase(testFile, StandardCharsets.UTF_8);
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData());

Path pathToFile = bibFolder.resolve("JabRef.bib");
Path file = Files.createFile(pathToFile);
Charset charset = Charset.forName("windows-1252");

try (BufferedWriter fileWriter = Files.newBufferedWriter(file, charset)) {
BibWriter bibWriter = new BibWriter(fileWriter, context.getDatabase().getNewLineSeparator());
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(bibWriter, generalPreferences, savePreferences, entryTypesManager);
databaseWriter.saveDatabase(context);
}

assertEquals(Files.readString(testFile, charset), Files.readString(file, charset));
}

@Test
void roundtripWithComplexBib() throws Exception {
Path testBibtexFile = Path.of("src/test/resources/testbib/complex.bib");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
@Article{,
abstract = {25° C},
}

@Comment{jabref-meta: databaseType:bibtex;}

0 comments on commit 6dddf93

Please sign in to comment.