001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.schematic.internal.document;
017
018import static org.junit.Assert.assertEquals;
019import org.junit.After;
020import org.junit.Before;
021import org.junit.Test;
022import org.modeshape.schematic.Schematic;
023import org.modeshape.schematic.document.Document;
024import org.modeshape.schematic.document.EditableDocument;
025import org.modeshape.schematic.internal.annotation.FixFor;
026
027public class JsonWriterTest {
028
029    protected JsonWriter writer;
030    protected JsonReader reader;
031    protected boolean print;
032    protected EditableDocument doc1;
033    protected Document doc2;
034
035    @Before
036    public void beforeTest() {
037        writer = new PrettyJsonWriter();
038        reader = new JsonReader();
039        print = false;
040    }
041
042    @After
043    public void afterTest() {
044        writer = null;
045        reader = null;
046        doc1 = null;
047        doc2 = null;
048    }
049
050    @Test
051    public void shouldWriteEmptyDocument() throws Exception {
052        doc1 = Schematic.newDocument();
053        String s = writer.write(doc1);
054        doc2 = reader.read(s);
055        assertMatch(doc1, doc2);
056    }
057
058    @Test
059    public void shouldWriteDocumentWithOneField() throws Exception {
060        doc1 = Schematic.newDocument("field1", "value1");
061        String s = writer.write(doc1);
062        doc2 = reader.read(s);
063        assertMatch(doc1, doc2);
064    }
065
066    @Test
067    public void shouldWriteDocumentWithTwoFields() throws Exception {
068        doc1 = Schematic.newDocument("field1", "value1", "field2", 3);
069        String s = writer.write(doc1);
070        doc2 = reader.read(s);
071        assertMatch(doc1, doc2);
072    }
073
074    @Test
075    @FixFor( "MODE-2309" )
076    public void shouldWriteDocumentWithEscapedCharacters() throws Exception {
077        doc1 = Schematic.newDocument("field1", "value1", "field2", 3);
078        doc1.setString("field3", "This has\nmultiple\nlines");
079        doc1.setString("field4", "This has\r\nmultiple\r\n lines");
080        String s = writer.write(doc1);
081        // System.out.println(s);
082        doc2 = reader.read(s);
083        assertMatch(doc1, doc2);
084    }
085
086    @Test
087    @FixFor( "MODE-2309" )
088    public void shouldParseJsonWithNonAsciiCharactersInFields() throws Exception {
089        doc1 = Schematic.newDocument();
090        doc1.setString("field1", "basic ascii");
091        doc1.setString("field2", "basic ascii with some arabic: هذه هي قصة من مكتبة على الشارع الرئيسي");
092        doc1.setString("field3",
093                       "basic ascii with some german: Dies ist die Geschichte von einer Buchhandlung an der Hauptstraße");
094        doc1.setString("field4", "basic ascii with some chinese: 這是在主要街道一家書店的故事");
095        doc1.setString("Hauptstraße", "Main street");
096        String s = writer.write(doc1);
097        // System.out.println(s);
098        doc2 = reader.read(s);
099        assertMatch(doc1, doc2);
100    }
101
102    protected void assertMatch( Document doc1,
103                                Document doc2 ) {
104        assertEquals(doc1.size(), doc2.size());
105        for (Document.Field field1 : doc1.fields()) {
106            if (field1.getValue() instanceof Document) {
107                assertMatch(field1.getValueAsDocument(), doc2.getDocument(field1.getName()));
108            } else {
109                Object value2 = doc2.get(field1.getName());
110                assertEquals(field1.getValue(), value2);
111            }
112        }
113    }
114
115}