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 java.io.ByteArrayOutputStream; 019import java.io.IOException; 020import org.junit.Test; 021 022public class PrettyJsonWriterTest extends CompactJsonWriterTest { 023 024 @Override 025 @Test 026 public void shouldCorrectlyWriteSimpleBsonObject() { 027 writer = new PrettyJsonWriter(); 028 BasicDocument top = new BasicDocument(); 029 top.put("firstName", "Jack"); 030 top.put("lastName", "Riley"); 031 top.put("age", 31); 032 String actual = writer.write(top); 033 String expected = "{\n \"firstName\" : \"Jack\",\n \"lastName\" : \"Riley\",\n \"age\" : 31\n}"; 034 assertSame(expected, actual); 035 } 036 037 @Override 038 @Test 039 public void shouldCorrectlyWriteSimpleBsonObjectWithNullValue() { 040 writer = new PrettyJsonWriter(); 041 BasicDocument top = new BasicDocument(); 042 top.put("firstName", "Jack"); 043 top.put("lastName", null); 044 top.put("age", 31); 045 String actual = writer.write(top); 046 String expected = "{\n \"firstName\" : \"Jack\",\n \"lastName\" : null,\n \"age\" : 31\n}"; 047 assertSame(expected, actual); 048 } 049 050 @Override 051 @Test 052 public void shouldCorrectlyWriteBsonObjectWithNestedObjectValue() { 053 writer = new PrettyJsonWriter(); 054 BasicDocument address = new BasicDocument(); 055 address.put("street", "100 Main St."); 056 address.put("city", "Springfield"); 057 BasicDocument top = new BasicDocument(); 058 top.put("firstName", "Jack"); 059 top.put("lastName", "Riley"); 060 top.put("address", address); 061 String actual = writer.write(top); 062 String expected = "{\n \"firstName\" : \"Jack\",\n \"lastName\" : \"Riley\",\n \"address\" : {\n \"street\" : \"100 Main St.\",\n \"city\" : \"Springfield\"\n }\n}"; 063 assertSame(expected, actual); 064 } 065 066 @Test 067 public void shouldCorrectlyWriteDocumentToStream() throws IOException { 068 writer = new PrettyJsonWriter(); 069 BasicDocument address = new BasicDocument(); 070 address.put("street", "100 Main St."); 071 address.put("city", "Springfield"); 072 BasicDocument top = new BasicDocument(); 073 top.put("firstName", "Jack"); 074 top.put("lastName", "Riley"); 075 top.put("address", address); 076 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 077 writer.write(top, baos); 078 String actual = baos.toString(); 079 String expected = "{\n \"firstName\" : \"Jack\",\n \"lastName\" : \"Riley\",\n \"address\" : {\n \"street\" : \"100 Main St.\",\n \"city\" : \"Springfield\"\n }\n}"; 080 assertSame(expected, actual); 081 } 082 083 @Test 084 public void shouldCorrectlyWriteBsonObjectWithNestedArrayValue() { 085 writer = new PrettyJsonWriter(); 086 BasicArray emails = new BasicArray(); 087 emails.put("0", "jriley@example.com"); 088 emails.put("1", "jriley@foobar.com"); 089 BasicDocument top = new BasicDocument(); 090 top.put("firstName", "Jack"); 091 top.put("lastName", "Riley"); 092 top.put("emails", emails); 093 String actual = writer.write(top); 094 String expected = "{\n \"firstName\" : \"Jack\",\n \"lastName\" : \"Riley\",\n \"emails\" : [\n \"jriley@example.com\",\n \"jriley@foobar.com\"\n ]\n}"; 095 assertSame(expected, actual); 096 } 097 098 @Override 099 @Test 100 public void shouldCorrectlyWriteListValue() { 101 writer = new PrettyJsonWriter(); 102 testWritingList("[ ]"); 103 testWritingList("[\n \"value1\"\n]", "value1"); 104 testWritingList("[\n \"value1\",\n null,\n \"value3\"\n]", "value1", null, "value3"); 105 testWritingList("[\n \"value1\",\n \"value2\",\n \"value3\"\n]", "value1", "value2", "value3"); 106 testWritingList("[\n \"value1\",\n \"value2\",\n 4\n]", "value1", "value2", 4L); 107 } 108}