package net.sf.jabref.bibtex;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefPreferences;
import net.sf.jabref.exporter.LatexFieldFormatter;
import net.sf.jabref.model.entry.BibtexEntry;
import net.sf.jabref.model.entry.BibtexEntryType;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.StringWriter;

import static org.junit.Assert.assertEquals;

public class BibtexEntryWriterTest {

    private BibtexEntryWriter writer;

    @BeforeClass
    public static void setUp() {
        Globals.prefs = JabRefPreferences.getInstance();
        // make sure that we use the "new style" serialization
        Globals.prefs.putInt(JabRefPreferences.WRITEFIELD_SORTSTYLE, 0);

    }

    @Before
    public void setUpWriter() {
        writer = new BibtexEntryWriter(new LatexFieldFormatter(), true);
    }

    @Test
    public void testSerialization() throws IOException {
        StringWriter stringWriter = new StringWriter();

        BibtexEntry entry = new BibtexEntry("1234", BibtexEntryType.getType("Article"));
        //set a required field
        entry.setField("author", "Foo Bar");
        entry.setField("journal", "International Journal of Something");
        //set an optional field
        entry.setField("number", "1");
        entry.setField("note", "some note");

        writer.write(entry, stringWriter);

        String actual = stringWriter.toString();

        String expected = "@Article{," + Globals.NEWLINE +
                "  Author                   = {Foo Bar}," + Globals.NEWLINE +
                "  Journal                  = {International Journal of Something}," + Globals.NEWLINE +
                "  Note                     = {some note}," + Globals.NEWLINE +
                "  Number                   = {1}" + Globals.NEWLINE +
                "}" + Globals.NEWLINE;

        assertEquals(expected, actual);
    }
}

@@ -145,16 +145,115 @@
 matter;%0A
+import net.sf.jabref.importer.ParserResult;%0Aimport net.sf.jabref.importer.fileformat.BibtexParser;%0A
 import n
@@ -340,16 +340,41 @@
 ryType;%0A
+import org.junit.Assert;%0A
 import o
@@ -493,22 +493,102 @@
 o.String
-Writer
+Reader;%0Aimport java.io.StringWriter;%0Aimport java.util.Collection;%0Aimport java.util.Set
 ;%0A%0Aimpor
@@ -2131,11 +2131,1333 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void roundTripTest() throws IOException %7B%0A        String bibtexEntry = %22@Article%7Btest,%22 + Globals.NEWLINE +%0A                %22  Author                   = %7BFoo Bar%7D,%22 + Globals.NEWLINE +%0A                %22  Journal                  = %7BInternational Journal of Something%7D,%22 + Globals.NEWLINE +%0A                %22  Note                     = %7Bsome note%7D,%22 + Globals.NEWLINE +%0A                %22  Number                   = %7B1%7D%22 + Globals.NEWLINE +%0A                %22%7D%22 + Globals.NEWLINE;%0A%0A        // read in bibtex string%0A        ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));%0A%0A        Collection%3CBibtexEntry%3E entries = result.getDatabase().getEntries();%0A        Assert.assertEquals(1, entries.size());%0A%0A        BibtexEntry entry = entries.iterator().next();%0A        Assert.assertEquals(%22test%22, entry.getCiteKey());%0A        Assert.assertEquals(5, entry.getAllFields().size());%0A        Set%3CString%3E fields = entry.getAllFields();%0A        Assert.assertTrue(fields.contains(%22author%22));%0A        Assert.assertEquals(%22Foo Bar%22, entry.getField(%22author%22));%0A%0A        //write out bibtex string%0A        StringWriter stringWriter = new StringWriter();%0A        writer.write(entry, stringWriter);%0A        String actual = stringWriter.toString();%0A%0A        assertEquals(bibtexEntry, actual);%0A    %7D
 %0A%7D%0A

Add test for serialization round trip