1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.json.xml;
18  
19  import net.sf.json.JSONArray;
20  import net.sf.json.JSONNull;
21  import net.sf.json.JSONObject;
22  
23  import org.custommonkey.xmlunit.XMLTestCase;
24  
25  /**
26   * @author Andres Almiray <aalmiray@users.sourceforge.net>
27   */
28  public class TestXMLSerializer_writes extends XMLTestCase {
29     public static void main( String[] args ) {
30        junit.textui.TestRunner.run( TestXMLSerializer_writes.class );
31     }
32  
33     private XMLSerializer xmlSerializer;
34  
35     public TestXMLSerializer_writes( String testName ) {
36        super( testName );
37     }
38  
39     public void testWrite_null() throws Exception {
40        String expected = "<o null=\"true\"/>";
41        String xml = xmlSerializer.write( null );
42        assertXMLEqual( expected, xml );
43     }
44  
45     public void testWriteBooleanArray() throws Exception {
46        JSONArray jsonArray = JSONArray.fromObject( "[true,false]" );
47        String expected = "<a><e type=\"boolean\">true</e><e type=\"boolean\">false</e></a>";
48        String xml = xmlSerializer.write( jsonArray );
49        assertXMLEqual( expected, xml );
50     }
51  
52     public void testWriteEmptyObject() throws Exception {
53        JSONObject jsonObject = new JSONObject();
54        String expected = "<o/>";
55        String xml = xmlSerializer.write( jsonObject );
56        assertXMLEqual( expected, xml );
57     }
58  
59     public void testWriteFunctionArray() throws Exception {
60        JSONArray jsonArray = JSONArray.fromObject( "[function(a){ return a; }]" );
61        String expected = "<a><e type=\"function\" params=\"a\"><![CDATA[return a;]]></e></a>";
62        String xml = xmlSerializer.write( jsonArray );
63        assertXMLEqual( expected, xml );
64     }
65  
66     public void testWriteFunctionArray_noTypeHintsCompatibility() throws Exception {
67        JSONArray jsonArray = JSONArray.fromObject( "[function(a){ return a; }]" );
68        xmlSerializer.setTypeHintsCompatibility( false );
69        String expected = "<a><e json_type=\"function\" json_params=\"a\"><![CDATA[return a;]]></e></a>";
70        String xml = xmlSerializer.write( jsonArray );
71        assertXMLEqual( expected, xml );
72     }
73  
74     public void testWriteJSONArray_collapseProperties() throws Exception {
75        JSONObject jsonObject = new JSONObject();
76        jsonObject.element( "duplicated", "json1" );
77        jsonObject.accumulate( "duplicated", "json2" );
78        jsonObject.getJSONArray( "duplicated" )
79              .setExpandElements( true );
80        JSONArray jsonArray = new JSONArray().element( jsonObject );
81        String expected = "<a><e class=\"object\"><duplicated type=\"string\">json1</duplicated><duplicated type=\"string\">json2</duplicated></e></a>";
82        xmlSerializer.setExpandableProperties( new String[] { "duplicated" } );
83        String xml = xmlSerializer.write( jsonArray );
84        assertXMLEqual( expected, xml );
85     }
86  
87     public void testWriteJSONNull() throws Exception {
88        String expected = "<o null=\"true\"/>";
89        String xml = xmlSerializer.write( JSONNull.getInstance() );
90        assertXMLEqual( expected, xml );
91     }
92  
93     public void testWriteJSONNull_encoding() throws Exception {
94        String expected = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
95        String xml = xmlSerializer.write( JSONNull.getInstance(), "ISO-8859-1" );
96        assertTrue( xml.startsWith( expected ) );
97     }
98  
99     public void testWriteJSONObject_collapseProperties() throws Exception {
100       JSONObject jsonObject = new JSONObject();
101       jsonObject.element( "duplicated", "json1" );
102       jsonObject.accumulate( "duplicated", "json2" );
103       jsonObject.getJSONArray( "duplicated" )
104             .setExpandElements( true );
105       String expected = "<o><duplicated type=\"string\">json1</duplicated><duplicated type=\"string\">json2</duplicated></o>";
106       xmlSerializer.setExpandableProperties( new String[] { "duplicated" } );
107       String xml = xmlSerializer.write( jsonObject );
108       assertXMLEqual( expected, xml );
109    }
110 
111    public void testWriteMultiBooleanArray() throws Exception {
112       JSONArray jsonArray = JSONArray.fromObject( "[true,false,[true,false]]" );
113       String expected = "<a><e type=\"boolean\">true</e><e type=\"boolean\">false</e><e class=\"array\"><e type=\"boolean\">true</e><e type=\"boolean\">false</e></e></a>";
114       String xml = xmlSerializer.write( jsonArray );
115       assertXMLEqual( expected, xml );
116    }
117 
118    public void testWriteMultiNumberArray() throws Exception {
119       JSONArray jsonArray = JSONArray.fromObject( "[1.1,2,[3,4.4]]" );
120       String expected = "<a><e type=\"number\">1.1</e><e type=\"number\">2</e><e class=\"array\"><e type=\"number\">3</e><e type=\"number\">4.4</e></e></a>";
121       String xml = xmlSerializer.write( jsonArray );
122       assertXMLEqual( expected, xml );
123    }
124 
125    public void testWriteMultiStringArray() throws Exception {
126       JSONArray jsonArray = JSONArray.fromObject( "['1.1','2',['3','4.4']]" );
127       String expected = "<a><e type=\"string\">1.1</e><e type=\"string\">2</e><e class=\"array\"><e type=\"string\">3</e><e type=\"string\">4.4</e></e></a>";
128       String xml = xmlSerializer.write( jsonArray );
129       assertXMLEqual( expected, xml );
130    }
131 
132    public void testWriteNestedNullObject() throws Exception {
133       JSONObject jsonObject = JSONObject.fromObject( "{\"nested\":null}" );
134       String expected = "<o><nested class=\"object\" null=\"true\"/></o>";
135       String xml = xmlSerializer.write( jsonObject );
136       assertXMLEqual( expected, xml );
137    }
138 
139    public void testWriteNullObject() throws Exception {
140       JSONObject jsonObject = new JSONObject( true );
141       String expected = "<o null=\"true\"/>";
142       String xml = xmlSerializer.write( jsonObject );
143       assertXMLEqual( expected, xml );
144    }
145 
146    public void testWriteNullObject_noTypeHintsCompatibility() throws Exception {
147       JSONObject jsonObject = new JSONObject( true );
148       String expected = "<o json_null=\"true\"/>";
149       xmlSerializer.setTypeHintsCompatibility( false );
150       String xml = xmlSerializer.write( jsonObject );
151       assertXMLEqual( expected, xml );
152    }
153 
154    public void testWriteNullObjectArray() throws Exception {
155       JSONArray jsonArray = JSONArray.fromObject( "[null,null]" );
156       String expected = "<a><e class=\"object\" null=\"true\"/><e class=\"object\" null=\"true\"/></a>";
157       String xml = xmlSerializer.write( jsonArray );
158       assertXMLEqual( expected, xml );
159    }
160 
161    public void testWriteNullObjectArray_noTypeHintsCompatibility() throws Exception {
162       JSONArray jsonArray = JSONArray.fromObject( "[null,null]" );
163       String expected = "<a><e json_class=\"object\" json_null=\"true\"/><e json_class=\"object\" json_null=\"true\"/></a>";
164       xmlSerializer.setTypeHintsCompatibility( false );
165       String xml = xmlSerializer.write( jsonArray );
166       assertXMLEqual( expected, xml );
167    }
168 
169    public void testWriteNumberArray() throws Exception {
170       JSONArray jsonArray = JSONArray.fromObject( "[1.1,2]" );
171       String expected = "<a><e type=\"number\">1.1</e><e type=\"number\">2</e></a>";
172       String xml = xmlSerializer.write( jsonArray );
173       assertXMLEqual( expected, xml );
174    }
175 
176    public void testWriteObject() throws Exception {
177       JSONObject jsonObject = JSONObject.fromObject( "{\"name\":\"json\"}" );
178       String expected = "<o><name type=\"string\">json</name></o>";
179       String xml = xmlSerializer.write( jsonObject );
180       assertXMLEqual( expected, xml );
181    }
182 
183    public void testWriteObject_full_types() throws Exception {
184       JSONObject jsonObject = JSONObject.fromObject( "{\"string\":\"json\",\"int\":1,\"bool\":true,\"array\":[1.1,2],\"nested_null\":null,\"nested\":{\"name\":\"json\"},\"func\":function(a){ return a; }}" );
185       String expected = "<o><string type=\"string\">json</string>" + "<int type=\"number\">1</int>"
186             + "<bool type=\"boolean\">true</bool>"
187             + "<array class=\"array\"><e type=\"number\">1.1</e><e type=\"number\">2</e></array>"
188             + "<nested_null class=\"object\" null=\"true\"/>"
189             + "<nested class=\"object\"><name type=\"string\">json</name></nested>"
190             + "<func type=\"function\" params=\"a\"><![CDATA[return a;]]></func>" + "</o>";
191       String xml = xmlSerializer.write( jsonObject );
192       assertXMLEqual( expected, xml );
193    }
194 
195    public void testWriteObject_withAttributes() throws Exception {
196       JSONObject jsonObject = new JSONObject().element( "@name", "json" )
197             .element( "string", "json" );
198       String expected = "<o name=\"json\"><string type=\"string\">json</string></o>";
199       String xml = xmlSerializer.write( jsonObject );
200       assertXMLEqual( expected, xml );
201    }
202 
203    public void testWriteObject_withNamespacePrefix() throws Exception {
204       JSONObject jsonObject = JSONObject.fromObject( "{\"ns:name\":\"json\"}" );
205       String expected = "<o><ns:name type=\"string\">json</ns:name></o>";
206       xmlSerializer.setNamespaceLenient( true );
207       String xml = xmlSerializer.write( jsonObject );
208       assertTrue( xml.trim()
209             .endsWith( expected ) );
210    }
211 
212    public void testWriteObject_withNamespaces() throws Exception {
213       JSONObject jsonObject = JSONObject.fromObject( "{\"ns:name\":\"json\"}" );
214       String expected = "<o xmlns=\"http://json.org\" "
215             + "xmlns:ns=\"http://json.org/ns-schema\"><ns:name type=\"string\" >json</ns:name></o>";
216       xmlSerializer.setNamespace( null, "http://json.org" );
217       xmlSerializer.addNamespace( "ns", "http://json.org/ns-schema" );
218       String xml = xmlSerializer.write( jsonObject );
219       assertXMLEqual( expected, xml );
220    }
221 
222    public void testWriteObject_withNamespaces_element() throws Exception {
223       JSONObject jsonObject = JSONObject.fromObject( "{\"ns:name\":\"json\"}" );
224       String expected = "<o><ns:name xmlns=\"http://json.org\" "
225             + "xmlns:ns=\"http://json.org/ns-schema\" type=\"string\" >json</ns:name></o>";
226       xmlSerializer.setNamespace( null, "http://json.org", "ns:name" );
227       xmlSerializer.addNamespace( "ns", "http://json.org/ns-schema", "ns:name" );
228       String xml = xmlSerializer.write( jsonObject );
229       assertXMLEqual( expected, xml );
230    }
231 
232    public void testWriteObject_withText() throws Exception {
233       JSONObject jsonObject = new JSONObject().element( "#text", "json" )
234             .element( "string", "json" );
235       String expected = "<o>json<string type=\"string\">json</string></o>";
236       String xml = xmlSerializer.write( jsonObject );
237       assertXMLEqual( expected, xml );
238    }
239 
240    public void testWriteObject_withText_2() throws Exception {
241       JSONObject jsonObject = new JSONObject().element( "#text", "['json','json']" )
242             .element( "string", "json" );
243       String expected = "<o>jsonjson<string type=\"string\">json</string></o>";
244       String xml = xmlSerializer.write( jsonObject );
245       assertXMLEqual( expected, xml );
246    }
247 
248    public void testWriteObjectArray() throws Exception {
249       JSONArray jsonArray = JSONArray.fromObject( "[{\"name\":\"json\"}]" );
250       String expected = "<a><e class=\"object\"><name type=\"string\">json</name></e></a>";
251       String xml = xmlSerializer.write( jsonArray );
252       assertXMLEqual( expected, xml );
253    }
254 
255    public void testWriteStringArray() throws Exception {
256       JSONArray jsonArray = JSONArray.fromObject( "['1','2']" );
257       String expected = "<a><e type=\"string\">1</e><e type=\"string\">2</e></a>";
258       String xml = xmlSerializer.write( jsonArray );
259       assertXMLEqual( expected, xml );
260    }
261 
262    public void testWriteWithNamespace() throws Exception {
263       JSONObject jsonObject = new JSONObject().element( "@xmlns", "http://json.org/json/1.0" )
264             .element( "@xmlns:ns", "http://www.w3.org/2001/XMLSchema-instance" )
265             .element( "ns:string", "json" )
266             .element( "ns:number", "1" );
267       String expected = "<o xmlns=\"http://json.org/json/1.0\""
268             + " xmlns:ns=\"http://www.w3.org/2001/XMLSchema-instance\">"
269             + "<ns:number type=\"string\">1</ns:number><ns:string type=\"string\">json</ns:string></o>";
270       String xml = xmlSerializer.write( jsonObject );
271       assertXMLEqual( expected, xml );
272    }
273 
274    protected void setUp() throws Exception {
275       super.setUp();
276       xmlSerializer = new XMLSerializer();
277    }
278 }