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.hamcrest.core.Is.is;
019import static org.junit.Assert.assertThat;
020import java.util.Arrays;
021import java.util.Date;
022import java.util.List;
023import java.util.UUID;
024import java.util.regex.Pattern;
025import org.junit.After;
026import org.junit.Before;
027import org.junit.Test;
028import org.modeshape.schematic.document.Binary;
029import org.modeshape.schematic.document.Bson;
030import org.modeshape.schematic.document.Code;
031import org.modeshape.schematic.document.CodeWithScope;
032import org.modeshape.schematic.document.MaxKey;
033import org.modeshape.schematic.document.MinKey;
034import org.modeshape.schematic.document.ObjectId;
035import org.modeshape.schematic.document.Symbol;
036import org.modeshape.schematic.document.Timestamp;
037
038public class CompactJsonWriterTest {
039
040    protected JsonWriter writer;
041    protected boolean print;
042
043    @Before
044    public void beforeTest() {
045        writer = new CompactJsonWriter();
046        print = false;
047    }
048
049    @After
050    public void afterTest() {
051        writer = null;
052    }
053
054    @Test
055    public void shouldCorrectlyWriteNullValue() {
056        assertSame("null", writer.write(null));
057    }
058
059    @Test
060    public void shouldCorrectlyWriteBooleanValues() {
061        assertSame("true", writer.write(Boolean.TRUE));
062        assertSame("false", writer.write(Boolean.FALSE));
063    }
064
065    @Test
066    public void shouldCorrectlyWriteIntegerValues() {
067        assertSame("10", writer.write(10));
068        assertSame("0", writer.write(0));
069        assertSame("-1", writer.write(-1));
070        assertSame(Integer.toString(Integer.MAX_VALUE), writer.write(Integer.MAX_VALUE));
071        assertSame(Integer.toString(Integer.MIN_VALUE), writer.write(Integer.MIN_VALUE));
072    }
073
074    @Test
075    public void shouldCorrectlyWriteLongValues() {
076        assertSame("10", writer.write(10L));
077        assertSame("0", writer.write(0L));
078        assertSame("-1", writer.write(-1L));
079        assertSame(Long.toString(Integer.MAX_VALUE + 10L), writer.write(Integer.MAX_VALUE + 10L));
080        assertSame(Long.toString(Integer.MIN_VALUE - 10L), writer.write(Integer.MIN_VALUE - 10L));
081        assertSame(Long.toString(Long.MAX_VALUE), writer.write(Long.MAX_VALUE));
082        assertSame(Long.toString(Long.MIN_VALUE), writer.write(Long.MIN_VALUE));
083    }
084
085    @Test
086    public void shouldCorrectlyWriteFloatValues() {
087        assertSame("10.01", writer.write(10.01));
088        assertSame("0.0", writer.write(0.0));
089        assertSame("-1.0135", writer.write(-1.0135));
090        assertSame(Float.toString(Float.MAX_VALUE), writer.write(Float.MAX_VALUE));
091        assertSame(Float.toString(Float.MIN_VALUE), writer.write(Float.MIN_VALUE));
092    }
093
094    @Test
095    public void shouldCorrectlyWriteDoubleValues() {
096        assertSame("10.01", writer.write(10.01d));
097        assertSame("0.0", writer.write(0.0d));
098        assertSame("-1.0135", writer.write(-1.0135d));
099        assertSame(Double.toString(Double.MAX_VALUE), writer.write(Double.MAX_VALUE));
100        assertSame(Double.toString(Double.MIN_VALUE), writer.write(Double.MIN_VALUE));
101    }
102
103    @Test
104    public void shouldCorrectlyWriteStringValues() {
105        assertSame("\"\"", writer.write(""));
106        assertSame("\"10.01\"", writer.write("10.01"));
107        assertSame("\"10.01d\"", writer.write("10.01d"));
108        assertSame("\"null\"", writer.write("null"));
109        assertSame("\"abcdefghijklmnopqrstuvwxyz\"", writer.write("abcdefghijklmnopqrstuvwxyz"));
110    }
111
112    @Test
113    public void shouldCorrectlyWriteSymbolValues() {
114        assertSame("\"\"", writer.write(new Symbol("")));
115        assertSame("\"10.01\"", writer.write(new Symbol("10.01")));
116        assertSame("\"10.01d\"", writer.write(new Symbol("10.01d")));
117        assertSame("\"null\"", writer.write(new Symbol("null")));
118        assertSame("\"abcdefghijklmnopqrstuvwxyz\"", writer.write(new Symbol("abcdefghijklmnopqrstuvwxyz")));
119    }
120
121    @Test
122    public void shouldCorrectlyWriteUuid() {
123        UUID id = UUID.randomUUID();
124        String expected = "{ \"$uuid\" : \"" + id + "\" }";
125        String actual = writer.write(id);
126        // print =true;
127        assertSame(expected, actual);
128    }
129
130    @Test
131    public void shouldCorrectlyWriteObjectId() {
132        ObjectId id = new ObjectId(300, 200, 9, 15);
133        String expected = "{ \"$oid\" : \"0000012c0000c8000900000f\" }";
134        String actual = writer.write(id);
135        assertSame(expected, actual);
136    }
137
138    @Test
139    public void shouldCorrectlyWriteDate() {
140        Date now = new Date();
141        String dateStr = Bson.getDateFormatter().format(now);
142        String expected = "{ \"$date\" : \"" + dateStr + "\" }";
143        String actual = writer.write(now);
144        assertSame(expected, actual);
145    }
146
147    @Test
148    public void shouldCorrectlyWriteTimestamp() {
149        Timestamp now = new Timestamp(new Date());
150        String expected = "{ \"$ts\" : " + now.getTime() + " , \"$inc\" : " + now.getInc() + " }";
151        String actual = writer.write(now);
152        assertSame(expected, actual);
153    }
154
155    @Test
156    public void shouldCorrectlyWriteMinKeyValue() {
157        assertSame("\"MinKey\"", writer.write(MinKey.getInstance()));
158    }
159
160    @Test
161    public void shouldCorrectlyWriteMaxKeyValue() {
162        assertSame("\"MaxKey\"", writer.write(MaxKey.getInstance()));
163    }
164
165    @Test
166    public void shouldCorrectlyWriteBinaryValue() {
167        byte[] data = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
168        @SuppressWarnings( "deprecation" )
169        Binary binary = new Binary(Bson.BinaryType.BINARY, data);
170        String expected = "{ \"$type\" : 2 , \"$base64\" : \"AAECAwQF\" }";
171        String actual = writer.write(binary);
172        // print =true;
173        assertSame(expected, actual);
174    }
175
176    @Test
177    public void shouldCorrectlyWritePattern() {
178        Pattern pattern = Pattern.compile("[CH]at\\s+in", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
179        String expected = "{ \"$regex\" : \"[CH]at\\\\s+in\" , \"$options\" : \"im\" }";
180        String actual = writer.write(pattern);
181        assertSame(expected, actual);
182    }
183
184    @Test
185    public void shouldCorrectlyWriteSimpleBsonObject() {
186        BasicDocument top = new BasicDocument();
187        top.put("firstName", "Jack");
188        top.put("lastName", "Riley");
189        top.put("age", 31);
190        String actual = writer.write(top);
191        String expected = "{ \"firstName\" : \"Jack\" , \"lastName\" : \"Riley\" , \"age\" : 31 }";
192        assertSame(expected, actual);
193    }
194
195    @Test
196    public void shouldCorrectlyWriteSimpleBsonObjectWithNullValue() {
197        BasicDocument top = new BasicDocument();
198        top.put("firstName", "Jack");
199        top.put("lastName", null);
200        top.put("age", 31);
201        String actual = writer.write(top);
202        String expected = "{ \"firstName\" : \"Jack\" , \"lastName\" : null , \"age\" : 31 }";
203        assertSame(expected, actual);
204    }
205
206    @Test
207    public void shouldCorrectlyWriteBsonObjectWithNestedObjectValue() {
208        BasicDocument address = new BasicDocument();
209        address.put("street", "100 Main St.");
210        address.put("city", "Springfield");
211        BasicDocument top = new BasicDocument();
212        top.put("firstName", "Jack");
213        top.put("lastName", "Riley");
214        top.put("address", address);
215        String actual = writer.write(top);
216        String expected = "{ \"firstName\" : \"Jack\" , \"lastName\" : \"Riley\" , \"address\" : { \"street\" : \"100 Main St.\" , \"city\" : \"Springfield\" } }";
217        assertSame(expected, actual);
218    }
219
220    @Test
221    public void shouldCorrectlyWriteListValue() {
222        testWritingList("[ ]");
223        testWritingList("[ \"value1\" ]", "value1");
224        testWritingList("[ \"value1\" , null , \"value3\" ]", "value1", null, "value3");
225        testWritingList("[ \"value1\" , \"value2\" , \"value3\" ]", "value1", "value2", "value3");
226        testWritingList("[ \"value1\" , \"value2\" , 4 ]", "value1", "value2", 4L);
227    }
228
229    protected void testWritingList( String expected,
230                                    Object... values ) {
231        List<Object> list = Arrays.asList(values);
232        String actual = writer.write(list);
233        assertSame(expected, actual);
234    }
235
236    @Test
237    public void shouldCorrectlyWriteCode() {
238        Code code = new Code("name");
239        String expected = "{ \"$code\" : \"name\" }";
240        String actual = writer.write(code);
241        assertSame(expected, actual);
242    }
243
244    @Test
245    public void shouldCorrectlyWriteCodeWithScope() {
246        BasicDocument scope = new BasicDocument();
247        scope.put("firstName", "Jack");
248        scope.put("lastName", "Riley");
249        CodeWithScope code = new CodeWithScope("name", scope);
250        String actual = writer.write(code);
251        String expected = "{ \"$code\" : \"name\" , \"$scope\" : { \"firstName\" : \"Jack\" , \"lastName\" : \"Riley\" } }";
252        assertSame(expected, actual);
253    }
254
255    protected void assertSame( String expected,
256                               String actual ) {
257        if (print) {
258            System.out.println("************************************************************");
259            System.out.println(actual);
260            System.out.println(expected);
261        }
262        assertThat(actual, is(expected));
263    }
264
265}