/* * File: TestJSONML.java Author: JSON.org */ package org.json.tests; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONML; import org.json.JSONObject; import org.json.XML; import junit.framework.TestCase; /** * The Class TestJSONML. */ public class TestJSONML extends TestCase { /** The jsonobject. */ private JSONObject jsonobject; /** The jsonarray. */ private JSONArray jsonarray; /** The string. */ private String string; /** * Tests the toJsonArray method using open xml tag. */ public void testToJsonArray_OpenXmlTag() { 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 = JSONML.toJSONObject(string); assertEquals( "{\"cook_time\":\"3 hours\",\"name\":\"bread\",\"tagName\":\"recipe\",\"childNodes\":[{\"tagName\":\"title\",\"childNodes\":[\"Basic bread\"]},{\"amount\":8,\"unit\":\"dL\",\"tagName\":\"ingredient\",\"childNodes\":[\"Flour\"]},{\"amount\":10,\"unit\":\"grams\",\"tagName\":\"ingredient\",\"childNodes\":[\"Yeast\"]},{\"amount\":4,\"unit\":\"dL\",\"tagName\":\"ingredient\",\"state\":\"warm\",\"childNodes\":[\"Water\"]},{\"amount\":1,\"unit\":\"teaspoon\",\"tagName\":\"ingredient\",\"childNodes\":[\"Salt\"]},{\"tagName\":\"instructions\",\"childNodes\":[{\"tagName\":\"step\",\"childNodes\":[\"Mix all ingredients together.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Knead thoroughly.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Cover with a cloth, and leave for one hour in warm room.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Knead again.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Place in a bread baking tin.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Cover with a cloth, and leave for one hour in warm room.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Bake in the oven at 180(degrees)C for 30 minutes.\"]}]}],\"prep_time\":\"5 mins\"}", jsonobject.toString()); assertEquals( "Basic breadFlourYeastWaterSaltMix 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.", JSONML.toString(jsonobject)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toString method using xml recipe as json array. */ public void testToString_XmlRecipeAsJsonArray() { 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. "; jsonarray = JSONML.toJSONArray(string); assertEquals( "[\n \"recipe\",\n {\n \"cook_time\": \"3 hours\",\n \"name\": \"bread\",\n \"prep_time\": \"5 mins\"\n },\n [\n \"title\",\n \"Basic bread\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 8,\n \"unit\": \"dL\"\n },\n \"Flour\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 10,\n \"unit\": \"grams\"\n },\n \"Yeast\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 4,\n \"unit\": \"dL\",\n \"state\": \"warm\"\n },\n \"Water\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 1,\n \"unit\": \"teaspoon\"\n },\n \"Salt\"\n ],\n [\n \"instructions\",\n [\n \"step\",\n \"Mix all ingredients together.\"\n ],\n [\n \"step\",\n \"Knead thoroughly.\"\n ],\n [\n \"step\",\n \"Cover with a cloth, and leave for one hour in warm room.\"\n ],\n [\n \"step\",\n \"Knead again.\"\n ],\n [\n \"step\",\n \"Place in a bread baking tin.\"\n ],\n [\n \"step\",\n \"Cover with a cloth, and leave for one hour in warm room.\"\n ],\n [\n \"step\",\n \"Bake in the oven at 180(degrees)C for 30 minutes.\"\n ]\n ]\n]", jsonarray.toString(4)); assertEquals( "Basic breadFlourYeastWaterSaltMix 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.", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJSONObjectHtml method. */ public void testToJSONObjectHtml() { try { string = "

JSONML is a transformation between JSON and XML that preserves ordering of document features.

JSONML can work with JSON arrays or JSON objects.

Three
little
words

"; jsonobject = JSONML.toJSONObject(string); assertEquals( "{\n \"id\": \"demo\",\n \"tagName\": \"div\",\n \"class\": \"JSONML\",\n \"childNodes\": [\n {\n \"tagName\": \"p\",\n \"childNodes\": [\n \"JSONML is a transformation between\",\n {\n \"tagName\": \"b\",\n \"childNodes\": [\"JSON\"]\n },\n \"and\",\n {\n \"tagName\": \"b\",\n \"childNodes\": [\"XML\"]\n },\n \"that preserves ordering of document features.\"\n ]\n },\n {\n \"tagName\": \"p\",\n \"childNodes\": [\"JSONML can work with JSON arrays or JSON objects.\"]\n },\n {\n \"tagName\": \"p\",\n \"childNodes\": [\n \"Three\",\n {\"tagName\": \"br\"},\n \"little\",\n {\"tagName\": \"br\"},\n \"words\"\n ]\n }\n ]\n}", jsonobject.toString(4)); assertEquals( "

JSONML is a transformation betweenJSONandXMLthat preserves ordering of document features.

JSONML can work with JSON arrays or JSON objects.

Three
little
words

", JSONML.toString(jsonobject)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJSONArrayHtml method. */ public void testToJSONArrayHtml() { try { string = "

JSONML is a transformation between JSON and XML that preserves ordering of document features.

JSONML can work with JSON arrays or JSON objects.

Three
little
words

"; jsonarray = JSONML.toJSONArray(string); assertEquals( "[\n \"div\",\n {\n \"id\": \"demo\",\n \"class\": \"JSONML\"\n },\n [\n \"p\",\n \"JSONML is a transformation between\",\n [\n \"b\",\n \"JSON\"\n ],\n \"and\",\n [\n \"b\",\n \"XML\"\n ],\n \"that preserves ordering of document features.\"\n ],\n [\n \"p\",\n \"JSONML can work with JSON arrays or JSON objects.\"\n ],\n [\n \"p\",\n \"Three\",\n [\"br\"],\n \"little\",\n [\"br\"],\n \"words\"\n ]\n]", jsonarray.toString(4)); assertEquals( "

JSONML is a transformation betweenJSONandXMLthat preserves ordering of document features.

JSONML can work with JSON arrays or JSON objects.

Three
little
words

", JSONML.toString(jsonarray)); string = "{\"xmlns:soap\":\"http://www.w3.org/2003/05/soap-envelope\",\"tagName\":\"soap:Envelope\",\"childNodes\":[{\"tagName\":\"soap:Header\"},{\"tagName\":\"soap:Body\",\"childNodes\":[{\"tagName\":\"ws:listProducts\",\"childNodes\":[{\"tagName\":\"ws:delay\",\"childNodes\":[1]}]}]}],\"xmlns:ws\":\"http://warehouse.acme.com/ws\"}"; jsonobject = new JSONObject(string); assertEquals( "1", JSONML.toString(jsonobject)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJsonArray method using json information. */ public void testToJsonArray_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.!]]>"; jsonarray = JSONML.toJSONArray(string); assertEquals( "[\n \"xml\",\n {\n \"two\": \" \\\"2\\\" \",\n \"one\": 1\n },\n [\"five\"],\n \"First \\t\",\n [\"five\"],\n \"This is \\\"content\\\".\",\n [\n \"three\",\n 3\n ],\n \"JSON does not preserve the sequencing of elements and contents.\",\n [\n \"three\",\n \"III\"\n ],\n [\n \"three\",\n \"T H R E E\"\n ],\n [\"four\"],\n \"Content text is an implied structure in XML.\",\n [\n \"six\",\n {\"content\": 6}\n ],\n \"JSON does not have implied structure:\",\n [\n \"seven\",\n 7\n ],\n \"everything is explicit.\",\n \"CDATA blocks!\"\n]", jsonarray.toString(4)); assertEquals( "First \t<content>This is "content".JSON does not preserve the sequencing of elements and contents.IIIT H R E EContent text is an implied structure in XML.JSON does not have implied structure:everything is explicit.CDATA blocks<are><supported>!", JSONML.toString(jsonarray)); }catch(JSONException e) { fail(e.getMessage()); } } /** * Tests the toJsonArray method using spanish numbers. */ public void testToJsonArray_SpanishNumbers() { try { string = "unodostrestruequatrocinqoseis"; jsonarray = JSONML.toJSONArray(string); assertEquals( "[\n \"xml\",\n {\"do\": 0},\n \"uno\",\n [\n \"a\",\n {\n \"re\": 1,\n \"mi\": 2\n },\n \"dos\",\n [\n \"b\",\n {\"fa\": 3}\n ],\n \"tres\",\n [\n \"c\",\n true\n ],\n \"quatro\"\n ],\n \"cinqo\",\n [\n \"d\",\n \"seis\",\n [\"e\"]\n ]\n]", jsonarray.toString(4)); assertEquals( "unodostresquatrocinqoseis", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJsonArray method using music notes. */ public void testToJsonArray_MusicNotes() { try { string = "The content of b and The content of cdoremi"; jsonobject = XML.toJSONObject(string); assertEquals( "{\"a\":{\"f\":\"\",\"content\":\"and\",\"d\":[\"do\",\"re\",\"mi\"],\"ichi\":1,\"e\":\"\",\"b\":\"The content of b\",\"c\":{\"content\":\"The content of c\",\"san\":3},\"ni\":2}}", jsonobject.toString()); assertEquals( "anddoremi1The content of bThe content of c32", XML.toString(jsonobject)); jsonarray = JSONML.toJSONArray(string); assertEquals("[\n" + " \"a\",\n" + " {\n" + " \"ichi\": 1,\n" + " \"ni\": 2\n" + " },\n" + " [\n" + " \"b\",\n" + " \"The content of b\"\n" + " ],\n" + " \"and\",\n" + " [\n" + " \"c\",\n" + " {\"san\": 3},\n" + " \"The content of c\"\n" + " ],\n" + " [\n" + " \"d\",\n" + " \"do\"\n" + " ],\n" + " [\"e\"],\n" + " [\n" + " \"d\",\n" + " \"re\"\n" + " ],\n" + " [\"f\"],\n" + " [\n" + " \"d\",\n" + " \"mi\"\n" + " ]\n" + "]", jsonarray.toString(4)); assertEquals( "The content of bandThe content of cdoremi", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJsonArray method using table of contents. */ public void testToJsonArray_TableOfContents() { try { string = "Content of the first chapterContent of the second chapter Content of the first subchapter Content of the second subchapterThird Chapter"; jsonarray = JSONML.toJSONArray(string); assertEquals("[\n" + " \"book\",\n" + " [\n" + " \"chapter\",\n" + " \"Content of the first chapter\"\n" + " ],\n" + " [\n" + " \"chapter\",\n" + " \"Content of the second chapter\",\n" + " [\n" + " \"chapter\",\n" + " \"Content of the first subchapter\"\n" + " ],\n" + " [\n" + " \"chapter\",\n" + " \"Content of the second subchapter\"\n" + " ]\n" + " ],\n" + " [\n" + " \"chapter\",\n" + " \"Third Chapter\"\n" + " ]\n" + "]", jsonarray.toString(4)); assertEquals( "Content of the first chapterContent of the second chapterContent of the first subchapterContent of the second subchapterThird Chapter", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJsonObject method using message xml. */ public void testToJsonObject_MessageXml() { try { string = "111111111111111"; jsonobject = JSONML.toJSONObject(string); assertEquals( "{\"tagName\":\"Root\",\"childNodes\":[{\"tagName\":\"MsgType\",\"childNodes\":[{\"tagName\":\"BatchType\",\"childNodes\":[111111111111111],\"type\":\"string\"}],\"type\":\"node\"}]}", jsonobject.toString()); jsonarray = JSONML.toJSONArray(string); assertEquals( "[\"Root\",[\"MsgType\",{\"type\":\"node\"},[\"BatchType\",{\"type\":\"string\"},111111111111111]]]", jsonarray.toString()); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJsonArray method using table mapping. */ public void testToJsonArray_TableMapping() { try { string = " "; jsonarray = JSONML.toJSONArray(string); assertEquals( "[\n \"mapping\",\n [\"empty\"],\n [\n \"class\",\n {\"name\": \"Customer\"},\n [\n \"field\",\n {\n \"name\": \"ID\",\n \"type\": \"string\"\n },\n [\n \"bind-xml\",\n {\n \"node\": \"attribute\",\n \"name\": \"ID\"\n }\n ]\n ],\n [\n \"field\",\n {\n \"name\": \"FirstName\",\n \"type\": \"FirstName\"\n }\n ],\n [\n \"field\",\n {\n \"name\": \"MI\",\n \"type\": \"MI\"\n }\n ],\n [\n \"field\",\n {\n \"name\": \"LastName\",\n \"type\": \"LastName\"\n }\n ]\n ],\n [\n \"class\",\n {\"name\": \"FirstName\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ],\n [\n \"class\",\n {\"name\": \"MI\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ],\n [\n \"class\",\n {\"name\": \"LastName\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ]\n]", jsonarray.toString(4)); assertEquals( "", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the constructor method. */ public static void testConstructor() { JSONML jsonml = new JSONML(); assertEquals("JSONML", jsonml.getClass().getSimpleName()); } /** * Tests the toJSONArray method using empty closing tag. */ public void testToJSONArray_EmptyClosingTag() { try { jsonarray = JSONML.toJSONArray(""); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Expected a closing name instead of '>'.", e.getMessage()); } } /** * Tests the toJSONArray method using closing tag with question. */ public void testToJSONArray_ClosingTagWithQuestion() { try { jsonarray = JSONML.toJSONArray(""); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Misshaped close tag at 11 [character 12 line 1]", e.getMessage()); } } /** * Tests the toJSONArray method using meta tag with two dashes. */ public void testToJSONArray_MetaTagWithTwoDashes() { try { jsonarray = JSONML.toJSONArray(""); assertEquals( "[\"abc\",\">\"]", jsonarray.toString()); assertEquals( ">", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJSONArray method using meta tag with one dash. */ public void testToJSONArray_MetaTagWithOneDash() { try { jsonarray = JSONML.toJSONArray(""); assertEquals( "[\"abc\",\"abc-->\"]", jsonarray.toString()); assertEquals( "abc-->", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJSONArray method using meta tag with cdata. */ public void testToJSONArray_MetaTagWithCdata() { try { jsonarray = JSONML.toJSONArray("]]>"); assertEquals( "[\"abc\",\"<\\/abc>\"]", jsonarray.toString()); assertEquals( "<abc></abc>", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJSONArray method using meta tag with bad cdata. */ public void testToJSONArray_MetaTagWithBadCdata() { try { jsonarray = JSONML.toJSONArray("?]>"); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Unclosed CDATA at 35 [character 36 line 1]", e.getMessage()); } try { jsonarray = JSONML.toJSONArray("]?>"); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Unclosed CDATA at 35 [character 36 line 1]", e.getMessage()); } try { jsonarray = JSONML.toJSONArray("]]>"); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Expected 'CDATA[' at 12 [character 13 line 1]", e.getMessage()); } } /** * Tests the toJSONArray method using meta tag with cdata only. */ public void testToJSONArray_MetaTagWithCdataOnly() { try { jsonarray = JSONML.toJSONArray("]]>"); assertEquals( "[\"abc\"]", jsonarray.toString()); assertEquals( "", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJSONArray method using meta tag with broken cdata. */ public void testToJSONArray_MetaTagWithBrokenCdata() { try { jsonarray = JSONML.toJSONArray("]]>"); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Bad XML at 23 [character 24 line 1]", e.getMessage()); } try { jsonarray = JSONML.toJSONArray(""); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Expected 'CDATA[' at 9 [character 10 line 1]", e.getMessage()); } } /** * Tests the toJSONArray method using php tag. */ public void testToJSONArray_PhpTag() { try { jsonarray = JSONML.toJSONArray(""); assertEquals( "[\"abc\"]", jsonarray.toString()); assertEquals( "", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJSONArray method using misshaped tag. */ public void testToJSONArray_MisshapedTag() { try { jsonarray = JSONML.toJSONArray("<=abcde?>"); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Misshaped tag at 7 [character 8 line 1]", e.getMessage()); } } /** * Tests the toJSONObject method using reserved attribute tag name. */ public void testToJSONObject_ReservedAttributeTagName() { try { jsonobject = JSONML.toJSONObject("def"); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Reserved attribute. at 12 [character 13 line 1]", e.getMessage()); } } /** * Tests the toJSONObject method using reserved attribute child node. */ public void testToJSONObject_ReservedAttributeChildNode() { try { jsonobject = JSONML.toJSONObject("def"); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Reserved attribute. at 14 [character 15 line 1]", e.getMessage()); } } /** * Tests the toJSONObject method using no value attribute. */ public void testToJSONObject_NoValueAttribute() { try { jsonobject = JSONML.toJSONObject("def"); assertEquals( "{\"novalue\":\"\",\"tagName\":\"abc\",\"childNodes\":[\"def\"]}", jsonobject.toString()); assertEquals( "def", JSONML.toString(jsonobject)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJSONObject method using no value attribute with equals. */ public void testToJSONObject_NoValueAttributeWithEquals() { try { jsonobject = JSONML.toJSONObject("def"); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Missing value at 14 [character 15 line 1]", e.getMessage()); } } /** * Tests the toJSONObject method using empty tag. */ public void testToJSONObject_EmptyTag() { try { jsonobject = JSONML.toJSONObject(""); assertEquals( "{\"tagName\":\"abc\"}", jsonobject.toString()); assertEquals( "", JSONML.toString(jsonobject)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJSONArray method using empty tag. */ public void testToJSONArray_EmptyTag() { try { jsonarray = JSONML.toJSONArray(""); assertEquals( "[\"abc\"]", jsonarray.toString()); assertEquals( "", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toJSONObject method using broken empty tag. */ public void testToJSONObject_BrokenEmptyTag() { try { jsonobject = JSONML.toJSONObject(""); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Misshaped tag at 11 [character 12 line 1]", e.getMessage()); } } /** * Tests the toJSONObject method using misshaped tag. */ public void testToJSONObject_MisshapedTag() { try { jsonobject = JSONML.toJSONObject(""); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Bad XML at 6 [character 7 line 1]", e.getMessage()); } } /** * Tests the toJSONObject method using no name tag. */ public void testToJSONObject_NoNameTag() { try { jsonobject = JSONML.toJSONObject("<>"); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Misshaped tag at 2 [character 3 line 1]", e.getMessage()); } } /** * Tests the toJSONObject method using space. */ public void testToJSONObject_Space() { try { jsonobject = JSONML.toJSONObject(" "); fail("Should have thrown exception."); } catch (JSONException e) { assertEquals("Bad XML at 3 [character 4 line 1]", e.getMessage()); } } /** * Tests the toJSONObject method using space content. */ public void testToJSONObject_SpaceContent() { try { jsonobject = JSONML.toJSONObject(" "); assertEquals( "{\"tagName\":\"abc\"}", jsonobject.toString()); assertEquals( "", JSONML.toString(jsonobject)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toString method using json array of json objects. */ public void testToString_JsonArrayOfJsonObjects() { try { jsonarray = new JSONArray(); jsonarray.put("tagName"); jsonarray.put(new JSONObject().put("tagName", "myName")); jsonarray.put(new JSONObject().put("tagName", "otherName")); assertEquals( "", JSONML.toString(jsonarray)); } catch (JSONException e) { fail(e.getMessage()); } } /** * Tests the toString method using json object of json arrays. */ public void testToString_JsonObjectOfJsonArrays() { try { jsonobject = new JSONObject(); jsonobject.put("tagName", "MyName"); jsonobject.put("childNodes", new JSONArray().put("abc").put(new JSONArray().put("def"))); jsonobject.put("123", new JSONArray("[\"abc\"]")); assertEquals( "abc", JSONML.toString(jsonobject)); } catch (JSONException e) { fail(e.getMessage()); } } }