/*
* File: TestXML.java Author: JSON.org
*/
package org.json.tests;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import junit.framework.TestCase;
/*
* Copyright (c) 2002 JSON.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* The Class TestXML.
*/
public class TestXML extends TestCase
{
/** The jsonobject. */
JSONObject jsonobject = new JSONObject();
/** The jsonarray. */
JSONArray jsonarray = new JSONArray();
/** The string. */
String string;
/**
* Tests the toJsonObject method using Simple xml.
*/
public static void testToJsonObject_SimpleXML()
{
try
{
String XMLString = "<![CDATA[--comment--]]>!123321";
JSONObject jo = new JSONObject();
JSONObject jo2 = new JSONObject();
jo2.put("tag2", "--comment--");
jo2.put("tag3", "!123321");
jo.put("tag1", jo2);
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
assertEquals("Unterminated string at 20 [character 21 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Bad name.
*/
public static void testToJsonObject_BadName()
{
try
{
String XMLString = "<!-abc>123!-abc>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Mismatched close tag ! at 13 [character 14 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Bad cdata.
*/
public static void testToJsonObject_BadCDATA()
{
try
{
String XMLString = "<![CDATA?[--comment--]]>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Expected 'CDATA[' at 14 [character 15 line 1]",
e.getMessage());
}
try
{
String XMLString = "<![CDATA[--comment--]>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Unclosed CDATA at 34 [character 35 line 1]",
e.getMessage());
}
try
{
String XMLString = "<![CDATA[--comment--]]?>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Unclosed CDATA at 36 [character 37 line 1]",
e.getMessage());
}
try
{
String XMLString = "<![CDAT[--comment--]]>";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Expected 'CDATA[' at 12 [character 13 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using null character.
*/
public static void testToJsonObject_NullCharacter()
{
try
{
String XMLString = "\0";
JSONObject jo = new JSONObject();
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toJsonObject method using Empty cdata.
*/
public static void testToJsonObject_EmptyCdata()
{
try
{
String XMLString = "<![CDATA[]]>";
JSONObject jo = new JSONObject();
jo.put("abc", "");
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
assertEquals("Unterminated string at 20 [character 21 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Bad meta.
*/
public static void testToJsonObject_BadMeta()
{
try
{
String XMLString = "<! abc";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Misshaped meta tag at 7 [character 8 line 1]",
e.getMessage());
}
try
{
String XMLString = "<!-123";
JSONObject jo = new JSONObject();
JSONObject jo2 = new JSONObject();
jo2.put("content", 123);
jo2.put("abc", 123);
jo.put("abc", jo2);
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
}
/**
* Tests the toJsonObject method using attributes with open string.
*/
public static void testToJsonObject_AttributesWithOpenString()
{
try
{
String XMLString = "123";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Unterminated string at 20 [character 21 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Attributes with ampersand.
*/
public static void testToJsonObject_AttributesWithAmpersand()
{
try
{
String XMLString = "123";
JSONObject jo = new JSONObject();
JSONObject jo2 = new JSONObject();
jo2.put("content", 123);
jo2.put("abc ", "");
jo.put("abc", jo2);
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
assertEquals("Unterminated string at 20 [character 21 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using Attributes missing value.
*/
public static void testToJsonObject_AttributesMissingValue()
{
try
{
String XMLString = "123";
XML.toJSONObject(XMLString);
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Missing value at 12 [character 13 line 1]",
e.getMessage());
}
}
/**
* Tests the toJsonObject method using empty tag.
*/
public static void testToJsonObject_EmptyTag()
{
try
{
String XMLString = "";
JSONObject jo = new JSONObject();
jo.put("abc", "");
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toJsonObject method using empty tag with attributes.
*/
public static void testToJsonObject_EmptyTagWithAttributes()
{
try
{
String XMLString = "";
JSONObject jo = new JSONObject();
JSONObject jo2 = new JSONObject();
jo2.put("def","jkk");
jo.put("abc", jo2);
assertEquals(jo.toString(), XML.toJSONObject(XMLString).toString());
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toJsonObject method using broken empty tag.
*/
public static void testToJsonObject_BrokenEmptyTag()
{
try
{
String XMLString = "";
XML.toJSONObject(XMLString).toString();
fail("Should have failed");
} catch (JSONException e)
{
assertEquals("Misshaped tag at 7 [character 8 line 1]",
e.getMessage());
}
}
/**
* Tests the toString method using jSON object.
*/
{
try
{
JSONObject jo = new JSONObject();
jo.put("abc", "123");
assertEquals("123", XML.toString(jo));
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toString method using empty json object.
*/
public static void testToString_EmptyJsonObject()
{
try
{
JSONObject jo = new JSONObject();
assertEquals("", XML.toString(jo));
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toString method using jSON object and name.
*/
public static void testToString_JsonObjectAndName()
{
try
{
JSONObject jo = new JSONObject();
jo.put("abc", "123");
assertEquals("123", XML.toString(jo, "my name"));
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toString method using empty json object and name.
*/
public static void testToString_EmptyJsonObjectAndName()
{
try
{
JSONObject jo = new JSONObject();
assertEquals("", XML.toString(jo, "my name"));
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toString method using empty json object and empty name.
*/
public static void testToString_EmptyJsonObjectAndEmptyName()
{
try
{
JSONObject jo = new JSONObject();
assertEquals("<>>", XML.toString(jo, ""));
} catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* Tests the toString method using json object with null string value.
*/
public static void testToString_JsonObjectWithNullStringValue()
{
try
{
JSONObject jo = new JSONObject();
jo.put("abc", "null");
assertEquals("null", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with json object null value.
*/
public static void testToString_JsonObjectWithJSONObjectNullValue()
{
try
{
JSONObject jo = new JSONObject();
jo.put("abc", JSONObject.NULL);
assertEquals("null", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with null key.
*/
public static void testToString_JsonObjectWithNullKey()
{
try
{
JSONObject jo = new JSONObject();
jo.put(null, "abc");
XML.toString(jo, "my name");
fail("Should have thrown Exception");
} catch (JSONException e)
{
assertEquals("Null key.", e.getMessage());
}
}
/**
* Tests the toString method using json object with integer.
*/
public static void testToString_JsonObjectWithInteger()
{
try
{
JSONObject jo = new JSONObject();
jo.put("abc", 45);
assertEquals("45", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with content key int value.
*/
public static void testToString_JsonObjectWithContentKeyIntValue()
{
try
{
JSONObject jo = new JSONObject();
jo.put("content", 45);
assertEquals("45", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with content key json array value.
*/
public static void testToString_JsonObjectWithContentKeyJsonArrayValue()
{
try
{
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
ja.put("123");
ja.put(72);
jo.put("content", ja);
assertEquals("123\n72", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with content key string value.
*/
public static void testToString_JsonObjectWithContentKeyStringValue()
{
try
{
JSONObject jo = new JSONObject();
jo.put("content", "42");
assertEquals("42", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with json array value.
*/
public static void testToString_JsonObjectWithJsonArrayValue()
{
try
{
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
ja.put("123");
ja.put(72);
jo.put("abc", ja);
assertEquals("12372", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json object with json array of json arrays value.
*/
public static void testToString_JsonObjectWithJsonArrayOfJsonArraysValue()
{
try
{
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
JSONArray ja2 = new JSONArray();
JSONArray ja3 = new JSONArray();
ja2.put("cat");
ja.put(ja2);
ja.put(ja3);
jo.put("abc", ja);
assertEquals("cat", XML.toString(jo, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using array.
*/
public static void testToString_Array()
{
try
{
String[] strings = {"abc", "123"};
assertEquals("abc123", XML.toString(strings, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using json array.
*/
public static void testToString_JsonArray()
{
try
{
JSONArray ja = new JSONArray();
ja.put("hi");
ja.put("bye");
assertEquals("hibye", XML.toString(ja, "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using empty string.
*/
public static void testToString_EmptyString()
{
try
{
assertEquals("", XML.toString("", "my name"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using string no name.
*/
public static void testToString_StringNoName()
{
try
{
assertEquals("\"123\"", XML.toString("123"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the escape method.
*/
public static void testEscape()
{
try
{
assertEquals("\"&<>"'\"", XML.toString("&<>\"'"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the noSpace method using empty string.
*/
public static void testNoSpace_EmptyString()
{
try
{
XML.noSpace("");
fail("Should have thrown exception");
} catch (JSONException e)
{
assertEquals("Empty string.", e.getMessage());
}
}
/**
* Tests the noSpace method using string with no spaces.
*/
public static void testNoSpace_StringWithNoSpaces()
{
try
{
XML.noSpace("123");
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the noSpace method using string with spaces.
*/
public static void testNoSpace_StringWithSpaces()
{
try
{
XML.noSpace("1 23");
fail("Should have thrown exception");
} catch (JSONException e)
{
assertEquals("'1 23' contains a space character.", e.getMessage());
}
}
/**
* Test.
*/
public static void testNoSpace()
{
try
{
XML.noSpace("1 23");
fail("Should have thrown exception");
} catch (JSONException e)
{
assertEquals("'1 23' contains a space character.", e.getMessage());
}
}
/**
* Tests the XML method.
*
* @throws Exception the exception
*/
public void testXML() throws Exception
{
jsonobject = XML
.toJSONObject("<![CDATA[This is a collection of test patterns and examples for json.]]> Ignore the stuff past the end. ");
assertEquals(
"{\"content\":\"This is a collection of test patterns and examples for json.\"}",
jsonobject.toString());
assertEquals(
"This is a collection of test patterns and examples for json.",
jsonobject.getString("content"));
string = "";
jsonobject = XML.toJSONObject(string);
assertEquals("{\"test\": {\n \"blank\": \"\",\n \"empty\": \"\"\n}}",
jsonobject.toString(2));
assertEquals("", XML.toString(jsonobject));
string = "";
jsonobject = XML.toJSONObject(string);
assertEquals(
"{\"subsonic-response\":{\"playlists\":{\"playlist\":[{\"id\":\"476c65652e6d3375\",\"int\":\"12345678901234567890123456789012345678901234567890213991133777039355058536718668104339937\"},{\"id\":\"50617274792e78737066\"}]}}}",
jsonobject.toString());
}
/**
* Tests the XML2 method.
*/
public void testXML2()
{
try {
jsonobject = XML
.toJSONObject(""
+ "\n\n"
+ ""
+ ""
+ "GOOGLEKEY '+search+'
0 10 true false latin1 latin1"
+ ""
+ "");
assertEquals(
"{\"SOAP-ENV:Envelope\": {\n \"SOAP-ENV:Body\": {\"ns1:doGoogleSearch\": {\n \"oe\": {\n \"content\": \"latin1\",\n \"xsi:type\": \"xsd:string\"\n },\n \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n \"lr\": {\"xsi:type\": \"xsd:string\"},\n \"start\": {\n \"content\": 0,\n \"xsi:type\": \"xsd:int\"\n },\n \"q\": {\n \"content\": \"'+search+'\",\n \"xsi:type\": \"xsd:string\"\n },\n \"ie\": {\n \"content\": \"latin1\",\n \"xsi:type\": \"xsd:string\"\n },\n \"safeSearch\": {\n \"content\": false,\n \"xsi:type\": \"xsd:boolean\"\n },\n \"xmlns:ns1\": \"urn:GoogleSearch\",\n \"restrict\": {\"xsi:type\": \"xsd:string\"},\n \"filter\": {\n \"content\": true,\n \"xsi:type\": \"xsd:boolean\"\n },\n \"maxResults\": {\n \"content\": 10,\n \"xsi:type\": \"xsd:int\"\n },\n \"key\": {\n \"content\": \"GOOGLEKEY\",\n \"xsi:type\": \"xsd:string\"\n }\n }},\n \"xmlns:xsd\": \"http://www.w3.org/1999/XMLSchema\",\n \"xmlns:xsi\": \"http://www.w3.org/1999/XMLSchema-instance\",\n \"xmlns:SOAP-ENV\": \"http://schemas.xmlsoap.org/soap/envelope/\"\n}}",
jsonobject.toString(2));
assertEquals(
"latin1xsd:stringhttp://schemas.xmlsoap.org/soap/encoding/xsd:string0xsd:int'+search+'xsd:string
latin1xsd:stringfalsexsd:booleanurn:GoogleSearchxsd:stringtruexsd:boolean10xsd:intGOOGLEKEYxsd:stringhttp://www.w3.org/1999/XMLSchemahttp://www.w3.org/1999/XMLSchema-instancehttp://schemas.xmlsoap.org/soap/envelope/",
XML.toString(jsonobject));
} catch (JSONException e) {
fail(e.toString());
}
}
/**
* Tests the constructor method.
*/
public static void testConstructor()
{
XML xml = new XML();
assertEquals("XML", xml.getClass().getSimpleName());
}
/**
* Tests the toJSONObject method using unclosed tag.
*/
public void testToJSONObject_UnclosedTag()
{
try {
jsonobject = XML.toJSONObject(" ");
fail("expecting JSONException here.");
} catch (JSONException jsone) {
assertEquals("Unclosed tag b at 11 [character 12 line 1]",
jsone.getMessage());
}
}
/**
* Tests the toJSONObject method using mismatched tags.
*/
public void testToJSONObject_MismatchedTags()
{
try {
jsonobject = XML.toJSONObject(" ");
fail("expecting JSONException here.");
} catch (JSONException jsone) {
assertEquals("Mismatched a and b at 6 [character 7 line 1]",
jsone.getMessage());
}
}
/**
* Tests the toJSONObject method using open tag.
*/
public void testToJSONObject_OpenTag()
{
try {
jsonobject = XML.toJSONObject("123456
",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toJSONObject method using xml recipe.
*/
public void testToJSONObject_XmlRecipe()
{
try
{
string = " Basic bread Flour Yeast Water Salt Mix all ingredients together. Knead thoroughly. Cover with a cloth, and leave for one hour in warm room. Knead again. Place in a bread baking tin. Cover with a cloth, and leave for one hour in warm room. Bake in the oven at 180(degrees)C for 30 minutes. ";
jsonobject = XML.toJSONObject(string);
assertEquals(
"{\"recipe\": {\n \"title\": \"Basic bread\",\n \"cook_time\": \"3 hours\",\n \"instructions\": {\"step\": [\n \"Mix all ingredients together.\",\n \"Knead thoroughly.\",\n \"Cover with a cloth, and leave for one hour in warm room.\",\n \"Knead again.\",\n \"Place in a bread baking tin.\",\n \"Cover with a cloth, and leave for one hour in warm room.\",\n \"Bake in the oven at 180(degrees)C for 30 minutes.\"\n ]},\n \"name\": \"bread\",\n \"ingredient\": [\n {\n \"content\": \"Flour\",\n \"amount\": 8,\n \"unit\": \"dL\"\n },\n {\n \"content\": \"Yeast\",\n \"amount\": 10,\n \"unit\": \"grams\"\n },\n {\n \"content\": \"Water\",\n \"amount\": 4,\n \"unit\": \"dL\",\n \"state\": \"warm\"\n },\n {\n \"content\": \"Salt\",\n \"amount\": 1,\n \"unit\": \"teaspoon\"\n }\n ],\n \"prep_time\": \"5 mins\"\n}}",
jsonobject.toString(4));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJSONObject method using personel record.
*/
public void testToJSONObject_PersonelRecord()
{
try
{
string = "\n Robert\n Smith\n \n 12345 Sixth Ave\n Anytown\n CA\n 98765-4321\n \n ";
jsonobject = XML.toJSONObject(string);
assertEquals(
"{\"person\": {\n \"lastName\": \"Smith\",\n \"address\": {\n \"postalCode\": \"98765-4321\",\n \"street\": \"12345 Sixth Ave\",\n \"state\": \"CA\",\n \"type\": \"home\",\n \"city\": \"Anytown\"\n },\n \"created\": \"2006-11-11T19:23\",\n \"firstName\": \"Robert\",\n \"modified\": \"2006-12-31T23:59\"\n}}",
jsonobject.toString(4));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using symbols.
*/
public void testToString_Symbols()
{
try
{
jsonobject = new JSONObject(
"{slashes: '///', closetag: '', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}");
assertEquals(
"{\n \"quotes\": [\n \"'\",\n \"\\\"\"\n ],\n \"slashes\": \"///\",\n \"ei\": {\"quotes\": \"\\\"'\"},\n \"eo\": {\n \"b\": \"don't\",\n \"a\": \"\\\"quoted\\\"\"\n },\n \"closetag\": \"<\\/script>\",\n \"backslash\": \"\\\\\"\n}",
jsonobject.toString(2));
assertEquals(
"'"///"'don't"quoted"</script>\\",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonObject method using json information.
*/
public void testToJsonObject_JsonInformation()
{
try
{
string = "First \u0009<content> This is \"content\". 3 JSON does not preserve the sequencing of elements and contents. III T H R E EContent text is an implied structure in XML. JSON does not have implied structure:7everything is explicit.<![CDATA[CDATA blocks!]]>";
jsonobject = XML.toJSONObject(string);
assertEquals(
"{\"xml\": {\n \"content\": [\n \"First \\t\",\n \"This is \\\"content\\\".\",\n \"JSON does not preserve the sequencing of elements and contents.\",\n \"Content text is an implied structure in XML.\",\n \"JSON does not have implied structure:\",\n \"everything is explicit.\",\n \"CDATA blocks!\"\n ],\n \"two\": \" \\\"2\\\" \",\n \"seven\": 7,\n \"five\": [\n \"\",\n \"\"\n ],\n \"one\": 1,\n \"three\": [\n 3,\n \"III\",\n \"T H R E E\"\n ],\n \"four\": \"\",\n \"six\": {\"content\": 6}\n}}",
jsonobject.toString(2));
assertEquals(
"First \t<content>\n"
+ "This is "content".\n"
+ "JSON does not preserve the sequencing of elements and contents.\n"
+ "Content text is an implied structure in XML.\n"
+ "JSON does not have implied structure:\n"
+ "everything is explicit.\n"
+ "CDATA blocks<are><supported>! "2" 713IIIT H R E E6",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using json array of int array.
*/
public void testToString_JsonArrayOfIntArray()
{
try
{
int ar[] =
{ 1, 2, 3 };
jsonarray = new JSONArray(ar);
assertEquals("[1,2,3]", jsonarray.toString());
assertEquals("123",
XML.toString(ar));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using table mapping.
*/
public void testToString_TableMapping()
{
try
{
string = " ";
jsonobject = XML.toJSONObject(string);
assertEquals(
"{\"mapping\": {\n \"empty\": \"\",\n \"class\": [\n {\n \"field\": [\n {\n \"bind-xml\": {\n \"node\": \"attribute\",\n \"name\": \"ID\"\n },\n \"name\": \"ID\",\n \"type\": \"string\"\n },\n {\n \"name\": \"FirstName\",\n \"type\": \"FirstName\"\n },\n {\n \"name\": \"MI\",\n \"type\": \"MI\"\n },\n {\n \"name\": \"LastName\",\n \"type\": \"LastName\"\n }\n ],\n \"name\": \"Customer\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"FirstName\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"MI\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"LastName\"\n }\n ]\n}}",
jsonobject.toString(2));
assertEquals(
"attributeIDIDstringFirstNameFirstNameMIMILastNameLastNameCustomertexttexttextFirstNametexttexttextMItexttexttextLastName",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using book info.
*/
public void testToString_BookInfo()
{
try
{
jsonobject = XML
.toJSONObject("Sample BookThis is chapter 1. It is not very long or interesting.This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.");
assertEquals(
"{\"Book\": {\n \"Chapter\": [\n {\n \"content\": \"This is chapter 1. It is not very long or interesting.\",\n \"id\": 1\n },\n {\n \"content\": \"This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.\",\n \"id\": 2\n }\n ],\n \"Author\": \"Anonymous\",\n \"Title\": \"Sample Book\"\n}}",
jsonobject.toString(2));
assertEquals(
"This is chapter 1. It is not very long or interesting.1This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.2AnonymousSample Book",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonObject method using table of contents.
*/
public void testToJsonObject_TableOfContents()
{
try
{
string = "Content of the first chapterContent of the second chapter Content of the first subchapter Content of the second subchapterThird Chapter";
jsonobject = XML.toJSONObject(string);
assertEquals(
"{\"book\": {\"chapter\": [\n \"Content of the first chapter\",\n {\n \"content\": \"Content of the second chapter\",\n \"chapter\": [\n \"Content of the first subchapter\",\n \"Content of the second subchapter\"\n ]\n },\n \"Third Chapter\"\n]}}",
jsonobject.toString(1));
assertEquals(
"Content of the first chapterContent of the second chapterContent of the first subchapterContent of the second subchapterThird Chapter",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonObject method using simple xml.
*/
public void testToJsonObject_SimpleXml()
{
try
{
string = "122333";
jsonobject = XML.toJSONObject(string);
assertEquals("{\"xml\": {\n" + " \"a\": [\n" + " \"\",\n"
+ " 1,\n" + " 22,\n" + " 333\n"
+ " ],\n" + " \"empty\": \"\"\n" + "}}",
jsonobject.toString(4));
assertEquals("122333",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonObject method using html escapes.
*/
public void testToJsonObject_HtmlEscapes()
{
try
{
jsonobject = XML
.toJSONObject("deluxe&"toot"&toot;Aeksbonusbonus2");
assertEquals(
"{\"test\": {\n \"w\": [\n \"bonus\",\n \"bonus2\"\n ],\n \"content\": \"deluxe\",\n \"intertag\": \"\",\n \"status\": \"ok\",\n \"blip\": {\n \"content\": \"&\\\"toot\\\"&toot;A\",\n \"sweet\": true\n },\n \"empty\": \"\",\n \"zero\": 0,\n \"x\": \"eks\"\n}}",
jsonobject.toString(2));
assertEquals(
"bonusbonus2deluxeok&"toot"&toot;Atrue0eks",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonObject method using phone book.
*/
public void testToJsonObject_PhoneBook()
{
try
{
jsonobject = XML
.toJSONObject("");
assertEquals(
"{\"bCard\": {\"bCard\": [\n {\n \"email\": \"[email protected]\",\n \"company\": \"MCI\",\n \"lastname\": \"Khare\",\n \"firstname\": \"Rohit\",\n \"homepage\": \"http://pest.w3.org/\"\n },\n {\n \"email\": \"[email protected]\",\n \"company\": \"Caltech Infospheres Project\",\n \"lastname\": \"Rifkin\",\n \"firstname\": \"Adam\",\n \"homepage\": \"http://www.cs.caltech.edu/~adam/\"\n }\n]}}",
jsonobject.toString(2));
assertEquals(
"[email protected]MCIKhareRohithttp://pest.w3.org/[email protected]Caltech Infospheres ProjectRifkinAdamhttp://www.cs.caltech.edu/~adam/",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonObject method using customer info.
*/
public void testToJsonObject_CustomerInfo()
{
try
{
jsonobject = XML
.toJSONObject(" Fred fbs0001 Scerbo B ");
assertEquals(
"{\"customer\": {\n \"lastName\": {\"text\": \"Scerbo\"},\n \"MI\": {\"text\": \"B\"},\n \"ID\": \"fbs0001\",\n \"firstName\": {\"text\": \"Fred\"}\n}}",
jsonobject.toString(2));
assertEquals(
"ScerboBfbs0001Fred",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toJsonObject method using library catalog.
*/
public void testToJsonObject_LibraryCatalog()
{
try
{
jsonobject = XML
.toJSONObject("<!ENTITY tp-address PUBLIC '-//ABC University::Special Collections Library//TEXT (titlepage: name and address)//EN' 'tpspcoll.sgm'>Repository Address - Special Collections Library
- ABC University
- Main Library, 40 Circle Drive
- Ourtown, Pennsylvania
- 17654 USA
");
assertEquals(
"{\"list\":{\"item\":[\"Special Collections Library\",\"ABC University\",\"Main Library, 40 Circle Drive\",\"Ourtown, Pennsylvania\",\"17654 USA\"],\"head\":\"Repository Address\",\"type\":\"simple\"}}",
jsonobject.toString());
assertEquals(
"- Special Collections Library
- ABC University
- Main Library, 40 Circle Drive
- Ourtown, Pennsylvania
- 17654 USA
Repository Addresssimple
",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using xml within xml.
*/
public void testToString_XmlWithinXml()
{
try{
jsonarray = new JSONArray(
" [\"\", next is an implied null , , ok,] ");
assertEquals("[\"\",\"next is an implied null\",null,\"ok\"]",
jsonarray.toString());
assertEquals(
"<escape>next is an implied nullnullok",
XML.toString(jsonarray));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using email.
*/
public void testToString_Email()
{
try{
jsonobject = new JSONObject(
"{Envelope: {Body: {\"ns1:doGoogleSearch\": {oe: \"latin1\", filter: true, q: \"'+search+'\", key: \"GOOGLEKEY\", maxResults: 10, \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\", start: 0, ie: \"latin1\", safeSearch:false, \"xmlns:ns1\": \"urn:GoogleSearch\"}}}}");
assertEquals(
"{\"Envelope\": {\"Body\": {\"ns1:doGoogleSearch\": {\n \"oe\": \"latin1\",\n \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n \"start\": 0,\n \"q\": \"'+search+'\",\n \"ie\": \"latin1\",\n \"safeSearch\": false,\n \"xmlns:ns1\": \"urn:GoogleSearch\",\n \"maxResults\": 10,\n \"key\": \"GOOGLEKEY\",\n \"filter\": true\n}}}}",
jsonobject.toString(2));
assertEquals(
"latin1http://schemas.xmlsoap.org/soap/encoding/0'+search+'
latin1falseurn:GoogleSearch10GOOGLEKEYtrue",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
/**
* Tests the toString method using http header.
*/
public void testToString_HttpHeader()
{
try{
jsonobject = new JSONObject(
"{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}");
assertEquals(
"{\n \"Request-URI\": \"/\",\n \"nix\": null,\n \"nux\": false,\n \"Method\": \"GET\",\n \"HTTP-Version\": \"HTTP/1.0\",\n \"null\": \"null\"\n}",
jsonobject.toString(2));
assertTrue(jsonobject.isNull("nix"));
assertTrue(jsonobject.has("nix"));
assertEquals(
"/nullfalseGETHTTP/1.0null",
XML.toString(jsonobject));
} catch (JSONException e)
{
fail(e.getMessage());
}
}
}