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.importer.ParserResult;
import net.sf.jabref.importer.fileformat.BibtexParser;
import net.sf.jabref.model.entry.BibtexEntry;
import net.sf.jabref.model.entry.BibtexEntryType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Set;

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);
    }

    @Test
    public void roundTripTest() throws IOException {
        String bibtexEntry = "@Article{test," + 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;

        // read in bibtex string
        ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));

        Collection<BibtexEntry> entries = result.getDatabase().getEntries();
        Assert.assertEquals(1, entries.size());

        BibtexEntry entry = entries.iterator().next();
        Assert.assertEquals("test", entry.getCiteKey());
        Assert.assertEquals(5, entry.getAllFields().size());
        Set<String> fields = entry.getAllFields();
        Assert.assertTrue(fields.contains("author"));
        Assert.assertEquals("Foo Bar", entry.getField("author"));

        //write out bibtex string
        StringWriter stringWriter = new StringWriter();
        writer.write(entry, stringWriter);
        String actual = stringWriter.toString();

        assertEquals(bibtexEntry, actual);
    }
}

@@ -3397,32 +3397,1247 @@
 er.toString();%0A%0A
+        assertEquals(bibtexEntry, actual);%0A    %7D%0A%0A    @Test%0A    public void monthFieldSpecialSyntax() throws IOException %7B%0A        String bibtexEntry = %22@Article%7Btest,%22 + Globals.NEWLINE +%0A                %22  Author                   = %7BFoo Bar%7D,%22 + Globals.NEWLINE +%0A                %22  Month                    = mar,%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(4, entry.getAllFields().size());%0A        Set%3CString%3E fields = entry.getAllFields();%0A        Assert.assertTrue(fields.contains(%22month%22));%0A        Assert.assertEquals(%22#mar#%22, entry.getField(%22month%22));%0A%0A        //write out bibtex string%0A        StringWriter stringWriter = new StringWriter();%0A        writer.write(entry, stringWriter);%0A        String actual = stringWriter.toString();%0A%0A
         assertEq

Add test for correct parsing and serialization of month field special syntax