Search in sources :

Example 36 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class BibEntryWriterTest method roundTripWithPrecedingCommentAndModificationTest.

@Test
public void roundTripWithPrecedingCommentAndModificationTest() throws IOException {
    // @formatter:off
    String bibtexEntry = "% Some random comment that should stay here" + 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();
    // change the entry
    entry.setField("author", "John Doe");
    //write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();
    // @formatter:off
    String expected = "% Some random comment that should stay here" + OS.NEWLINE + OS.NEWLINE + "@Article{test," + OS.NEWLINE + "  author  = {John Doe}," + 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);
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringWriter(java.io.StringWriter) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 37 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class BibEntryWriterTest method roundTripTest.

@Test
public void roundTripTest() throws IOException {
    // @formatter:off
    String bibtexEntry = "@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();
    //write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();
    assertEquals(bibtexEntry, actual);
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringWriter(java.io.StringWriter) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 38 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class BibEntryWriterTest method roundTripWithCamelCasingInTheOriginalEntryAndResultInLowerCase.

@Test
public void roundTripWithCamelCasingInTheOriginalEntryAndResultInLowerCase() 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 + "  HowPublished             = {asdf}," + 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 + "  howpublished = {asdf}," + OS.NEWLINE + "}" + OS.NEWLINE;
    // @formatter:on
    assertEquals(expected, actual);
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringWriter(java.io.StringWriter) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 39 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class BibEntryWriterTest method testEntryTypeChange.

@Test
public void testEntryTypeChange() throws IOException {
    // @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 + "  howpublished = {asdf}," + OS.NEWLINE + "}" + OS.NEWLINE;
    // @formatter:on
    // read in bibtex string
    ParserResult result = new BibtexParser(importFormatPreferences).parse(new StringReader(expected));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();
    // modify entry
    entry.setType("inproceedings");
    //write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();
    // @formatter:off
    String expectedNewEntry = OS.NEWLINE + "@InProceedings{test," + OS.NEWLINE + "  author       = {BlaBla}," + OS.NEWLINE + "  number       = {1}," + OS.NEWLINE + "  note         = {some note}," + OS.NEWLINE + "  howpublished = {asdf}," + OS.NEWLINE + "  journal      = {International Journal of Something}," + OS.NEWLINE + "}" + OS.NEWLINE;
    // @formatter:on
    assertEquals(expectedNewEntry, actual);
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringWriter(java.io.StringWriter) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 40 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class BibEntryWriterTest method addFieldWithLongerLength.

@Test
public void addFieldWithLongerLength() throws IOException {
    // @formatter:off
    String bibtexEntry = OS.NEWLINE + 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 + "}";
    // @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("howpublished", "asdf");
    //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 + "  howpublished = {asdf}," + OS.NEWLINE + "}" + OS.NEWLINE;
    // @formatter:on
    assertEquals(expected, actual);
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringWriter(java.io.StringWriter) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)43 ParserResult (org.jabref.logic.importer.ParserResult)38 Test (org.junit.Test)28 BibEntry (org.jabref.model.entry.BibEntry)27 StringReader (java.io.StringReader)20 BibDatabase (org.jabref.model.database.BibDatabase)13 InputStreamReader (java.io.InputStreamReader)12 StringWriter (java.io.StringWriter)12 File (java.io.File)8 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 Charset (java.nio.charset.Charset)6 Defaults (org.jabref.model.Defaults)6 BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)6 Scanner (java.util.Scanner)5 IOException (java.io.IOException)4 FileInputStream (java.io.FileInputStream)3 Collection (java.util.Collection)3 Optional (java.util.Optional)3 FetcherException (org.jabref.logic.importer.FetcherException)3