use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class BibEntryWriterTest method testSingleWrite.
private String testSingleWrite(String bibtexEntry) throws IOException {
// read in bibtex string
ParserResult result = new BibtexParser(importFormatPreferences).parse(new StringReader(bibtexEntry));
Collection<BibEntry> entries = result.getDatabase().getEntries();
BibEntry entry = entries.iterator().next();
//write out bibtex string
StringWriter stringWriter = new StringWriter();
writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
String actual = stringWriter.toString();
assertEquals(bibtexEntry, actual);
return actual;
}
use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class BibEntryWriterTest method roundTripWithModification.
@Test
public void roundTripWithModification() throws IOException {
// @formatter:off
String bibtexEntry = OS.NEWLINE + "@Article{test," + OS.NEWLINE + " Author = {Foo Bar}," + OS.NEWLINE + " Journal = {International Journal of Something}," + OS.NEWLINE + " Note = {some note}," + OS.NEWLINE + " Number = {1}," + OS.NEWLINE + "}";
// @formatter:on
// read in bibtex string
ParserResult result = new BibtexParser(importFormatPreferences).parse(new StringReader(bibtexEntry));
Collection<BibEntry> entries = result.getDatabase().getEntries();
BibEntry entry = entries.iterator().next();
// Modify entry
entry.setField("author", "BlaBla");
// write out bibtex string
StringWriter stringWriter = new StringWriter();
writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
String actual = stringWriter.toString();
// @formatter:off
String expected = OS.NEWLINE + "@Article{test," + OS.NEWLINE + " author = {BlaBla}," + OS.NEWLINE + " journal = {International Journal of Something}," + OS.NEWLINE + " number = {1}," + OS.NEWLINE + " note = {some note}," + OS.NEWLINE + "}" + OS.NEWLINE;
// @formatter:on
assertEquals(expected, actual);
}
use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class TestVM method bibtexString2BibtexEntry.
private static BibEntry bibtexString2BibtexEntry(String s) throws IOException {
ParserResult result = new BibtexParser(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS)).parse(new StringReader(s));
Collection<BibEntry> c = result.getDatabase().getEntries();
Assert.assertEquals(1, c.size());
return c.iterator().next();
}
use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class BibtexDatabaseWriterTest method roundtripWithUserComment.
@Test
public void roundtripWithUserComment() throws Exception {
Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib");
Charset encoding = StandardCharsets.UTF_8;
ParserResult result = new BibtexParser(importFormatPreferences).parse(Importer.getReader(testBibtexFile, encoding));
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX));
StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences);
try (Scanner scanner = new Scanner(testBibtexFile, encoding.name())) {
assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue());
}
}
use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class BibtexDatabaseWriterTest method roundtrip.
@Test
public void roundtrip() throws Exception {
Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib");
Charset encoding = StandardCharsets.UTF_8;
ParserResult result = new BibtexParser(importFormatPreferences).parse(Importer.getReader(testBibtexFile, encoding));
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX));
StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences);
try (Scanner scanner = new Scanner(testBibtexFile, encoding.name())) {
assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue());
}
}
Aggregations