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 junit.framework.TestCase;
20  import net.sf.json.Assertions;
21  import net.sf.json.JSON;
22  import net.sf.json.JSONObject;
23  import net.sf.json.JSONSerializer;
24  import net.sf.json.test.JSONAssert;
25  
26  /**
27   * @author Andres Almiray <aalmiray@users.sourceforge.net>
28   */
29  public class TestUserSubmitted extends TestCase {
30     public static void main( String[] args ) {
31        junit.textui.TestRunner.run( TestUserSubmitted.class );
32     }
33  
34     public TestUserSubmitted( String name ) {
35        super( name );
36     }
37  
38     public void testReadFromXMLToJSON_1735732() throws Exception {
39        // bug 1735732
40  
41        XMLSerializer xmlSerializer = new XMLSerializer();
42  
43        JSONObject actual = (JSONObject) xmlSerializer.readFromFile( "net/sf/json/xml/1735732.xml" );
44        JSONObject expected = new JSONObject().element( "@xmlns:ns2",
45              "http://schemas.foo.com/HelloWorld" )
46              .element( "item", new JSONObject().element( "age", "2 5" )
47                    .element( "name", "emp1" ) )
48              .accumulate( "item", new JSONObject().element( "age", "2" )
49                    .element( "name", "emp2" ) );
50        JSONAssert.assertEquals( expected, actual );
51     }
52  
53     public void testReadFromXMLToJSON_1739066() throws Exception {
54        // bug 1739066
55  
56        XMLSerializer xmlSerializer = new XMLSerializer();
57        xmlSerializer.setTrimSpaces( true );
58  
59        JSONObject actual = (JSONObject) xmlSerializer.readFromFile( "net/sf/json/xml/1739066.xml" );
60        JSONObject expected = new JSONObject().element( "Address", "http://localhost:0/te stToString" )
61              .element( "@xmlns", "http://www.w3.org/2005/08/addressing" )
62              .element(
63                    "Metadata",
64                    new JSONObject().element( "@xmlns:wsa-wsdl",
65                          "http://www.w3.org/2006/01/wsdl-instance" )
66                          .element( "@wsa-wsdl:wsdlLocation", "file:///b.wsdl" )
67                          .element(
68                                "ns3:InterfaceName",
69                                new JSONObject().element( "@xmlns:tns", "http://com.iona.cxf/GreetMe" )
70                                      .element( "@xmlns:ns3",
71                                            "http://www.w3.org/2005/02/addressing/wsdl" )
72                                      .element( "#text", "tns:GreetMePortType" ) )
73                          .element(
74                                "ns3:ServiceName",
75                                new JSONObject().element( "@EndpointName", "GreetMePort" )
76                                      .element( "@xmlns:tns", "http://com.iona.cxf/GreetMe" )
77                                      .element( "@xmlns:ns3",
78                                            "http://www.w3.org/2005/02/addressing/wsdl" )
79                                      .element( "#text", "tns:GreetMeService" ) ) );
80        JSONAssert.assertEquals( expected, actual );
81     }
82  
83     public void testIgnoreWhitespaceWhileReading() {
84        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
85              + "<ARCXML version=\"1.1\">\n"
86              + "<RESPONSE>\n"
87              + "<FEATURES>\n"
88              + "<FEATURE>\n"
89              + "<ENVELOPE minx=\"-178.216552734375\" miny=\"18.9254779815674\" maxx=\"179.775939941406\" maxy=\"71.3514404296875\"/>\n"
90              + "<FIELDS>\n"
91              + "<FIELD name=\"FIPS_CNTRY\" value=\"US\" />\n"
92              + "<FIELD name=\"GMI_CNTRY\" value=\"USA\" />\n"
93              + "<FIELD name=\"ISO_2DIGIT\" value=\"US\" />\n"
94              + "<FIELD name=\"ISO_3DIGIT\" value=\"USA\" />\n"
95              + "<FIELD name=\"CNTRY_NAME\" value=\"United States\" />\n"
96              + "<FIELD name=\"LONG_NAME\" value=\"United States\" />\n"
97              + "<FIELD name=\"SOVEREIGN\" value=\"United States\" />\n"
98              + "<FIELD name=\"POP_CNTRY\" value=\"258833000\" />\n"
99              + "<FIELD name=\"CURR_TYPE\" value=\"US Dollar\" />\n"
100             + "<FIELD name=\"CURR_CODE\" value=\"USD\" />\n"
101             + "<FIELD name=\"LANDLOCKED\" value=\"N\" />\n"
102             + "<FIELD name=\"SQKM\" value=\"9449365\" />\n"
103             + "<FIELD name=\"SQMI\" value=\"3648399.75\" />\n"
104             + "<FIELD name=\"COLORMAP\" value=\"5\" />\n"
105             + "<FIELD name=\"#SHAPE#\" value=\"[Geometry]\" />\n"
106             + "<FIELD name=\"#ID#\" value=\"234\" />\n"
107             + "</FIELDS>\n"
108             + "</FEATURE>\n"
109             + "<FEATURECOUNT count=\"1\" hasmore=\"false\" />\n"
110             + "<ENVELOPE minx=\"-178.216552734375\" miny=\"18.9254779815674\" maxx=\"179.775939941406\" maxy=\"71.3514404296875\"/>\n"
111             + "</FEATURES>\n" + "</RESPONSE>\n" + "</ARCXML>\n";
112 
113       XMLSerializer xmlSerializer = new XMLSerializer();
114       xmlSerializer.setSkipWhitespace( true );
115       JSON json1 = xmlSerializer.read( xml );
116 
117       xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
118             + "<ARCXML version=\"1.1\">"
119             + "<RESPONSE>"
120             + "<FEATURES>"
121             + "<FEATURE>"
122             + "<ENVELOPE minx=\"-178.216552734375\" miny=\"18.9254779815674\" maxx=\"179.775939941406\" maxy=\"71.3514404296875\"/>"
123             + "<FIELDS>"
124             + "<FIELD name=\"FIPS_CNTRY\" value=\"US\" />"
125             + "<FIELD name=\"GMI_CNTRY\" value=\"USA\" />"
126             + "<FIELD name=\"ISO_2DIGIT\" value=\"US\" />"
127             + "<FIELD name=\"ISO_3DIGIT\" value=\"USA\" />"
128             + "<FIELD name=\"CNTRY_NAME\" value=\"United States\" />"
129             + "<FIELD name=\"LONG_NAME\" value=\"United States\" />"
130             + "<FIELD name=\"SOVEREIGN\" value=\"United States\" />"
131             + "<FIELD name=\"POP_CNTRY\" value=\"258833000\" />"
132             + "<FIELD name=\"CURR_TYPE\" value=\"US Dollar\" />"
133             + "<FIELD name=\"CURR_CODE\" value=\"USD\" />"
134             + "<FIELD name=\"LANDLOCKED\" value=\"N\" />"
135             + "<FIELD name=\"SQKM\" value=\"9449365\" />"
136             + "<FIELD name=\"SQMI\" value=\"3648399.75\" />"
137             + "<FIELD name=\"COLORMAP\" value=\"5\" />"
138             + "<FIELD name=\"#SHAPE#\" value=\"[Geometry]\" />"
139             + "<FIELD name=\"#ID#\" value=\"234\" />"
140             + "</FIELDS>"
141             + "</FEATURE>"
142             + "<FEATURECOUNT count=\"1\" hasmore=\"false\" />"
143             + "<ENVELOPE minx=\"-178.216552734375\" miny=\"18.9254779815674\" maxx=\"179.775939941406\" maxy=\"71.3514404296875\"/>"
144             + "</FEATURES>" + "</RESPONSE>" + "</ARCXML>";
145       JSON json2 = xmlSerializer.read( xml );
146       Assertions.assertEquals( json2, json1 );
147    }
148 
149    public void testXMLRoundtrip() {
150       String json = "{\"entries\": [ { \"credits\": \"p1\", \"id\": 1, \"status\": true, \"text\": \"1\" }, { \"credits\": \"p2\", \"id\": 2, \"status\": true, \"text\": \"2\" } ]}";
151       JSONObject json1 = JSONObject.fromObject( json );
152       XMLSerializer xmlSerializer = new XMLSerializer();
153       String xml =  xmlSerializer.write(json1);
154       JSONObject json2 = (JSONObject) xmlSerializer.read( xml );
155       JSONAssert.assertEquals( json1, json2 );
156       assertTrue(json1.getJSONArray( "entries" ).getJSONObject( 0 ).get( "id" ) instanceof Integer );
157       assertTrue(json2.getJSONArray( "entries" ).getJSONObject( 0 ).get( "id" ) instanceof Integer );
158    }
159    
160    public void testXMLWithArraySingleElement() {
161       String testXML =
162                         "    <rate>" +
163                         "      <rateBreakdown>\n" +
164                         "        <rate>\n" +
165                         "          <date>\n" +
166                         "            <day>15</day>\n" +
167                         "            <month>1</month>\n" +
168                         "            <year>2007</year>\n" +
169                         "          </date>\n" +
170                         "          <amount>109.74</amount>\n" +
171                         "        </rate>\n" +
172                         "      </rateBreakdown>" +
173                         "      <totalAmount>219.48</totalAmount>\n" +
174                         "    </rate>";
175 
176       JSON expected = JSONSerializer.toJSON("{\"rate\":{\"rateBreakdown\":[{\"amount\":\"109.74\",\"date\":{\"month\":\"1\",\"day\":\"15\",\"year\":\"2007\"}}],\"totalAmount\":\"219.48\"}}");
177 
178       // rate.rateBreakdown.rate should be a single entry array.
179 
180       JSONObject actual = convertXML( testXML );
181       assertNotNull( actual );
182       Assertions.assertEquals( expected, actual );
183    }
184 
185    private JSONObject convertXML( String testXML ) {
186       XMLSerializer xmlSerializer = new XMLSerializer();
187       xmlSerializer.setSkipWhitespace( true );
188       xmlSerializer.setArrayName( "rate" );
189       xmlSerializer.setForceTopLevelObject( true );
190       JSON jsonElement = xmlSerializer.read( testXML );
191       return (JSONObject) jsonElement;
192    }
193 }