package org.json.junit; /* Public Domain. */ import static org.junit.Assert.*; import org.json.*; import org.junit.Test; /** * Tests for org.json.JSONML.java * * Certain inputs are expected to result in exceptions. These tests are * executed first. JSONML provides an API to: * Convert an XML string into a JSONArray or a JSONObject. * Convert a JSONArray or JSONObject into an XML string. * Both fromstring and tostring operations operations should be symmetrical * within the limits of JSONML. * It should be possible to perform the following operations, which should * result in the original string being recovered, within the limits of the * underlying classes: * Convert a string -> JSONArray -> string -> JSONObject -> string * Convert a string -> JSONObject -> string -> JSONArray -> string * */ public class JSONMLTest { /** * Attempts to transform a null XML string to JSON. * Expects a NullPointerException */ @Test(expected=NullPointerException.class) public void nullXMLException() { String xmlStr = null; JSONML.toJSONArray(xmlStr); } /** * Attempts to transform an empty string to JSON. * Expects a JSONException */ @Test public void emptyXMLException() { String xmlStr = ""; try { JSONML.toJSONArray(xmlStr); fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", "Bad XML at 0 [character 1 line 1]", e.getMessage()); } } /** * Attempts to call JSONML.toString() with a null JSONArray. * Expects a NullPointerException. */ @Test(expected=NullPointerException.class) public void nullJSONXMLException() { /** * Tries to convert a null JSONArray to XML. */ JSONArray jsonArray= null; JSONML.toString(jsonArray); } /** * Attempts to call JSONML.toString() with a null JSONArray. * Expects a JSONException. */ @Test public void emptyJSONXMLException() { /** * Tries to convert an empty JSONArray to XML. */ JSONArray jsonArray = new JSONArray(); try { JSONML.toString(jsonArray); assertTrue("Expecting an exception", false); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONArray[0] not found.". equals(e.getMessage())); } } /** * Attempts to transform an non-XML string to JSON. * Expects a JSONException */ @Test public void nonXMLException() { /** * Attempts to transform a nonXML string to JSON */ String xmlStr = "{ \"this is\": \"not xml\"}"; try { JSONML.toJSONArray(xmlStr); fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", "Bad XML at 23 [character 24 line 1]", e.getMessage()); } } /** * Attempts to transform a JSON document with XML content that * does not follow JSONML conventions (element name is not first value * in a nested JSONArray) to a JSONArray then back to string. * Expects a JSONException */ @Test public void emptyTagException() { /** * jsonArrayStr is used to build a JSONArray which is then * turned into XML. For this transformation, all arrays represent * elements and the first array entry is the name of the element. * In this case, one of the arrays does not have a name */ String jsonArrayStr = "[\"addresses\","+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ // this array has no name "["+ "[\"name\"],"+ "[\"nocontent\"],"+ "\">\""+ "]"+ "]"; JSONArray jsonArray = new JSONArray(jsonArrayStr); try { JSONML.toString(jsonArray); assertTrue("Expecting an exception", false); } catch (JSONException e) { assertEquals("Expecting an exception message", "JSONArray[0] is not a String (class org.json.JSONArray).", e.getMessage()); } } /** * Attempts to transform a JSON document with XML content that * does not follow JSONML conventions (element tag has an embedded space) * to a JSONArray then back to string. Expects a JSONException */ @Test public void spaceInTagException() { /** * jsonArrayStr is used to build a JSONArray which is then * turned into XML. For this transformation, all arrays represent * elements and the first array entry is the name of the element. * In this case, one of the element names has an embedded space, * which is not allowed. */ String jsonArrayStr = "[\"addresses\","+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ // this array has an invalid name "[\"addr esses\","+ "[\"name\"],"+ "[\"nocontent\"],"+ "\">\""+ "]"+ "]"; JSONArray jsonArray = new JSONArray(jsonArrayStr); try { JSONML.toString(jsonArray); assertTrue("Expecting an exception", false); } catch (JSONException e) { assertTrue("Expecting an exception message", "'addr esses' contains a space character.". equals(e.getMessage())); } } /** * Attempts to transform a malformed XML document * (element tag has a frontslash) to a JSONArray.\ * Expects a JSONException */ @Test public void invalidSlashInTagException() { /** * xmlStr contains XML text which is transformed into a JSONArray. * In this case, the XML is invalid because the 'name' element * contains an invalid frontslash. */ String xmlStr = "\n"+ "\n"+ "
\n"+ " \n"+ " abc street\n"+ "
\n"+ "
"; try { JSONML.toJSONArray(xmlStr); fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", "Misshaped tag at 176 [character 14 line 4]", e.getMessage()); } } /** * Malformed XML text (invalid tagname) is transformed into a JSONArray. * Expects a JSONException. */ @Test public void invalidBangInTagException() { String xmlStr = "\n"+ "\n"+ "
\n"+ " \n"+ " \n"+ "
\n"+ "
"; try { JSONML.toJSONArray(xmlStr); fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", "Misshaped meta tag at 215 [character 12 line 7]", e.getMessage()); } } /** * Malformed XML text (invalid tagname, no close bracket) is transformed\ * into a JSONArray. Expects a JSONException. */ @Test public void invalidBangNoCloseInTagException() { /** * xmlStr contains XML text which is transformed into a JSONArray. * In this case, the XML is invalid because an element * starts with '!' and has no closing tag */ String xmlStr = "\n"+ "\n"+ "
\n"+ " \n"+ " \n"+ ""; try { JSONML.toJSONArray(xmlStr); fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", "Misshaped meta tag at 214 [character 12 line 7]", e.getMessage()); } } /** * Malformed XML text (tagname with no close bracket) is transformed\ * into a JSONArray. Expects a JSONException. */ @Test public void noCloseStartTagException() { /** * xmlStr contains XML text which is transformed into a JSONArray. * In this case, the XML is invalid because an element * has no closing '>'. */ String xmlStr = "\n"+ "\n"+ "
\n"+ " \n"+ " \n"+ ""; try { JSONML.toJSONArray(xmlStr); fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", "Misplaced '<' at 194 [character 5 line 6]", e.getMessage()); } } /** * Malformed XML text (endtag with no name) is transformed\ * into a JSONArray. Expects a JSONException. */ @Test public void noCloseEndTagException() { /** * xmlStr contains XML text which is transformed into a JSONArray. * In this case, the XML is invalid because an element * has no name after the closing tag '\n"+ "\n"+ "
\n"+ " \n"+ " \n"+ " \n"+ ""; try { JSONML.toJSONArray(xmlStr); assertTrue("Expecting an exception", false); } catch (JSONException e) { assertTrue("Expecting an exception message", "Expected a closing name instead of '>'.". equals(e.getMessage())); } } /** * Malformed XML text (endtag with no close bracket) is transformed\ * into a JSONArray. Expects a JSONException. */ @Test public void noCloseEndBraceException() { /** * xmlStr contains XML text which is transformed into a JSONArray. * In this case, the XML is invalid because an element * has '>' after the closing tag '\n"+ "\n"+ "
\n"+ " \n"+ " \n"+ " "; try { JSONML.toJSONArray(xmlStr); fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", "Misplaced '<' at 206 [character 1 line 7]", e.getMessage()); } } /** * Malformed XML text (incomplete CDATA string) is transformed\ * into a JSONArray. Expects a JSONException. */ @Test public void invalidCDATABangInTagException() { /** * xmlStr contains XML text which is transformed into a JSONArray. * In this case, the XML is invalid because an element * does not have a complete CDATA string. */ String xmlStr = "\n"+ "\n"+ "
\n"+ " Joe Tester\n"+ " \n"+ "
\n"+ "
"; try { JSONML.toJSONArray(xmlStr); fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", "Expected 'CDATA[' at 204 [character 11 line 5]", e.getMessage()); } } /** * Convert an XML document into a JSONArray, then use JSONML.toString() * to convert it into a string. This string is then converted back into * a JSONArray. Both JSONArrays are compared against a control to * confirm the contents. */ @Test public void toJSONArray() { /** * xmlStr contains XML text which is transformed into a JSONArray. * Each element becomes a JSONArray: * 1st entry = elementname * 2nd entry = attributes object (if present) * 3rd entry = content (if present) * 4th entry = child element JSONArrays (if present) * The result is compared against an expected JSONArray. * The transformed JSONArray is then transformed back into a string * which is used to create a final JSONArray, which is also compared * against the expected JSONArray. */ String xmlStr = "\n"+ "\n"+ "
\n"+ "myName\n"+ ">\n"+ "
\n"+ "
"; String expectedStr = "[\"addresses\","+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ "[\"address\","+ "{\"attr1\":\"attrValue1\",\"attr2\":\"attrValue2\",\"attr3\":\"attrValue3\"},"+ "[\"name\", {\"nameType\":\"mine\"},\"myName\"],"+ "[\"nocontent\"],"+ "\">\""+ "]"+ "]"; JSONArray jsonArray = JSONML.toJSONArray(xmlStr); JSONArray expectedJsonArray = new JSONArray(expectedStr); String xmlToStr = JSONML.toString(jsonArray); JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } /** * Convert an XML document into a JSONObject. Use JSONML.toString() to * convert it back into a string, and then re-convert it into a JSONObject. * Both JSONObjects are compared against a control JSONObject to confirm * the contents. *

* Next convert the XML document into a JSONArray. Use JSONML.toString() to * convert it back into a string, and then re-convert it into a JSONArray. * Both JSONArrays are compared against a control JSONArray to confirm * the contents. *

* This test gives a comprehensive example of how the JSONML * transformations work. */ @Test public void toJSONObjectToJSONArray() { /** * xmlStr contains XML text which is transformed into a JSONObject, * restored to XML, transformed into a JSONArray, and then restored * to XML again. Both JSONObject and JSONArray should contain the same * information and should produce the same XML, allowing for non-ordered * attributes. * * Transformation to JSONObject: * The elementName is stored as a string where key="tagName" * Attributes are simply stored as key/value pairs * If the element has either content or child elements, they are stored * in a jsonArray with key="childNodes". * * Transformation to JSONArray: * 1st entry = elementname * 2nd entry = attributes object (if present) * 3rd entry = content (if present) * 4th entry = child element JSONArrays (if present) */ String xmlStr = "\n"+ "\n"+ "

\n"+ "Joe Tester\n"+ "\n"+ "\n"+ "true\n"+ "false\n"+ "null\n"+ "42\n"+ "-23\n"+ "-23.45\n"+ "-23x.45\n"+ "\n"+ "1\n"+ "2\n"+ "abc\n"+ "3\n"+ "4.1\n"+ "5.2\n"+ "\n"+ "
\n"+ ""; String expectedJSONObjectStr = "{"+ "\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "\"childNodes\":["+ "{"+ "\"childNodes\":["+ "{"+ "\"childNodes\":[\"Joe Tester\"],"+ "\"nameType\":\"my name\","+ "\"tagName\":\"name\""+ "},"+ "{"+ "\"childNodes\":[\"Baker street 5\"],"+ "\"tagName\":\"street\""+ "},"+ "{"+ "\"tagName\":\"NothingHere\","+ "\"except\":\"an attribute\""+ "},"+ "{"+ "\"childNodes\":[true],"+ "\"tagName\":\"TrueValue\""+ "},"+ "{"+ "\"childNodes\":[false],"+ "\"tagName\":\"FalseValue\""+ "},"+ "{"+ "\"childNodes\":[null],"+ "\"tagName\":\"NullValue\""+ "},"+ "{"+ "\"childNodes\":[42],"+ "\"tagName\":\"PositiveValue\""+ "},"+ "{"+ "\"childNodes\":[-23],"+ "\"tagName\":\"NegativeValue\""+ "},"+ "{"+ "\"childNodes\":[-23.45],"+ "\"tagName\":\"DoubleValue\""+ "},"+ "{"+ "\"childNodes\":[\"-23x.45\"],"+ "\"tagName\":\"Nan\""+ "},"+ "{"+ "\"childNodes\":["+ "{"+ "\"childNodes\":[1],"+ "\"tagName\":\"value\""+ "},"+ "{"+ "\"childNodes\":[2],"+ "\"tagName\":\"value\""+ "},"+ "{"+ "\"childNodes\":["+ "{"+ "\"childNodes\":[\"abc\"],"+ "\"svAttr\":\"svValue\","+ "\"tagName\":\"subValue\""+ "}"+ "],"+ "\"tagName\":\"value\""+ "},"+ "{"+ "\"childNodes\":[3],"+ "\"tagName\":\"value\""+ "},"+ "{"+ "\"childNodes\":[4.1],"+ "\"tagName\":\"value\""+ "},"+ "{"+ "\"childNodes\":[5.2],"+ "\"tagName\":\"value\""+ "}"+ "],"+ "\"tagName\":\"ArrayOfNum\""+ "}"+ "],"+ "\"addrType\":\"my address\","+ "\"tagName\":\"address\""+ "}"+ "],"+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ "\"tagName\":\"addresses\""+ "}"; String expectedJSONArrayStr = "["+ "\"addresses\","+ "{"+ "\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\""+ "},"+ "["+ "\"address\","+ "{"+ "\"addrType\":\"my address\""+ "},"+ "["+ "\"name\","+ "{"+ "\"nameType\":\"my name\""+ "},"+ "\"Joe Tester\""+ "],"+ "[\"street\",\"Baker street 5\"],"+ "["+ "\"NothingHere\","+ "{\"except\":\"an attribute\"}"+ "],"+ "[\"TrueValue\",true],"+ "[\"FalseValue\",false],"+ "[\"NullValue\",null],"+ "[\"PositiveValue\",42],"+ "[\"NegativeValue\",-23],"+ "[\"DoubleValue\",-23.45],"+ "[\"Nan\",\"-23x.45\"],"+ "["+ "\"ArrayOfNum\","+ "[\"value\",1],"+ "[\"value\",2],"+ "[\"value\","+ "["+ "\"subValue\","+ "{\"svAttr\":\"svValue\"},"+ "\"abc\""+ "]"+ "],"+ "[\"value\",3],"+ "[\"value\",4.1],"+ "[\"value\",5.2]"+ "]"+ "]"+ "]"; // make a JSONObject and make sure it looks as expected JSONObject jsonObject = JSONML.toJSONObject(xmlStr); JSONObject expectedJsonObject = new JSONObject(expectedJSONObjectStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); // restore the XML, then make another JSONObject and make sure it // looks as expected String jsonObjectXmlToStr = JSONML.toString(jsonObject); JSONObject finalJsonObject = JSONML.toJSONObject(jsonObjectXmlToStr); Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); // create a JSON array from the original string and make sure it // looks as expected JSONArray jsonArray = JSONML.toJSONArray(xmlStr); JSONArray expectedJsonArray = new JSONArray(expectedJSONArrayStr); Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray); // restore the XML, then make another JSONArray and make sure it // looks as expected String jsonArrayXmlToStr = JSONML.toString(jsonArray); JSONArray finalJsonArray = JSONML.toJSONArray(jsonArrayXmlToStr); Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); // lastly, confirm the restored JSONObject XML and JSONArray XML look // reasonably similar JSONObject jsonObjectFromObject = JSONML.toJSONObject(jsonObjectXmlToStr); JSONObject jsonObjectFromArray = JSONML.toJSONObject(jsonArrayXmlToStr); Util.compareActualVsExpectedJsonObjects(jsonObjectFromObject, jsonObjectFromArray); } /** * Convert an XML document which contains embedded comments into * a JSONArray. Use JSONML.toString() to turn it into a string, then * reconvert it into a JSONArray. Compare both JSONArrays to a control * JSONArray to confirm the contents. *

* This test shows how XML comments are handled. */ @Test public void commentsInXML() { String xmlStr = "\n"+ "\n"+ "\n"+ "

\n"+ "\n"+ "Joe Tester\n"+ "\n"+ "Baker street 5\n"+ "
\n"+ ""; String expectedStr = "[\"addresses\","+ "[\"address\","+ "[\"name\",\"Joe Tester\"],"+ "[\"street\",\"Baker street 5\"]"+ "]"+ "]"; JSONArray jsonArray = JSONML.toJSONArray(xmlStr); JSONArray expectedJsonArray = new JSONArray(expectedStr); String xmlToStr = JSONML.toString(jsonArray); JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } /** * JSON string with lost leading zero and converted "True" to true. See test * result in comment below. */ @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]"; final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false); assertEquals(expectedJsonString, actualJsonOutput.toString()); } /** * JSON string cannot be reverted to original xml when type guessing is used. */ @Test public void testToJSONArray_reversibility() { final String originalXml = "011000True"; final String revertedXml = JSONML.toString(JSONML.toJSONArray(originalXml, false)); assertNotEquals(revertedXml, originalXml); } /** * Entity-looking text is decoded once, including when strings are kept. */ @Test public void shouldDecodeEntitiesOnceWhenKeepingStringsInJSONArray() { final String[][] cases = { {"&lt;", "<"}, {"&amp;", "&"}, {"&#65;", "A"}, {"&lt;", "<"}, {"<>&"'AB", "<>&\"'AB"} }; for (String[] testCase : cases) { final String xml = "

" + testCase[0] + "

"; final JSONArray json = JSONML.toJSONArray(xml, true); assertEquals(xml, testCase[1], json.getJSONObject(1).get("title")); assertEquals(xml, testCase[1], json.get(2)); assertEquals(xml, testCase[1], JSONML.toJSONArray(xml, false).get(2)); assertTrue(xml, json.similar(JSONML.toJSONArray(new XMLTokener(JSONML.toString(json)), JSONMLParserConfiguration.KEEP_STRINGS))); } } /** * Object form preserves the same decoded attribute and text values. */ @Test public void shouldDecodeEntitiesOnceWhenKeepingStringsInJSONObject() { final String xml = "

&lt;

"; final JSONObject json = JSONML.toJSONObject(xml, JSONMLParserConfiguration.ORIGINAL.withKeepStrings(true)); assertEquals("<", json.get("title")); assertEquals("<", json.getJSONArray("childNodes").get(0)); assertEquals("<", JSONML.toJSONObject(xml).getJSONArray("childNodes").get(0)); assertTrue(json.similar(JSONML.toJSONObject(new XMLTokener(JSONML.toString(json)), true))); } /** * Keeping strings disables type conversion without disabling entity decoding. */ @Test public void shouldPreserveValueTypesWhenKeepingStrings() { final String[] values = {"42", "true", "false", "null"}; final String[] content = {"42", "true", "false", "null"}; final Object[] typedValues = {Integer.valueOf(42), Boolean.TRUE, Boolean.FALSE, JSONObject.NULL}; for (int i = 0; i < values.length; i++) { final String xml = "

" + content[i] + "

"; final JSONArray array = JSONML.toJSONArray(new XMLTokener(xml), true); final JSONObject object = JSONML.toJSONObject(xml, true); assertEquals(values[i], array.getJSONObject(1).get("value")); assertEquals(values[i], array.get(2)); assertEquals(values[i], object.get("value")); assertEquals(values[i], object.getJSONArray("childNodes").get(0)); final JSONArray typedArray = JSONML.toJSONArray(xml); final JSONObject typedObject = JSONML.toJSONObject(xml); assertEquals(typedValues[i], typedArray.getJSONObject(1).get("value")); assertEquals(typedValues[i], typedArray.get(2)); assertEquals(typedValues[i], typedObject.get("value")); assertEquals(typedValues[i], typedObject.getJSONArray("childNodes").get(0)); } } /** * CDATA entity syntax remains literal content. */ @Test public void shouldPreserveCdataWhenKeepingStrings() { final String content = "< A & 42 true null"; final String xml = "

"; assertEquals(content, JSONML.toJSONArray(xml, true).get(1)); assertEquals(content, JSONML.toJSONObject(xml, true).getJSONArray("childNodes").get(0)); } /** * Escaped invalid character references are text, while raw references remain invalid. */ @Test public void shouldPreserveEscapedInvalidCharacterReferences() { final String[] references = {"#0", "#x110000"}; for (String reference : references) { final String expected = "&" + reference + ";"; final String xml = "

&" + reference + ";

"; assertEquals(expected, JSONML.toJSONArray(xml, true).get(1)); assertEquals(expected, JSONML.toJSONObject(xml, true).getJSONArray("childNodes").get(0)); assertEquals(expected, JSONML.toJSONArray(xml).get(1)); assertEquals(expected, JSONML.toJSONObject(xml).getJSONArray("childNodes").get(0)); final String invalidXml = "

" + expected + "

"; assertThrows(JSONException.class, () -> JSONML.toJSONArray(invalidXml, true)); assertThrows(JSONException.class, () -> JSONML.toJSONObject(invalidXml, true)); } } /** * JSON string cannot be reverted to original xml when type guessing is used. * When we force all the values as string, the original text comes back. */ @Test public void testToJSONArray_reversibility2() { final String originalXml = "011000True"; final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]"; final JSONArray json = JSONML.toJSONArray(originalXml,true); assertEquals(expectedJsonString, json.toString()); final String reverseXml = JSONML.toString(json); assertEquals(originalXml, reverseXml); } /** * JSON can be reverted to original xml. */ @Test public void testToJSONArray_reversibility3() { final String originalXml = "400402"; final JSONArray jsonArray = JSONML.toJSONArray(originalXml, false); final String revertedXml = JSONML.toString(jsonArray); assertEquals(revertedXml, originalXml); } /** * JSON string cannot be reverted to original xml. See test result in * comment below. */ @Test public void testToJSONObject_reversibility() { final String originalXml = "400402"; final JSONObject originalObject=JSONML.toJSONObject(originalXml,false); final String originalJson = originalObject.toString(); final String xml = JSONML.toString(originalObject); final JSONObject revertedObject = JSONML.toJSONObject(xml, false); final String newJson = revertedObject.toString(); assertTrue("JSON Objects are not similar", originalObject.similar(revertedObject)); assertTrue("JSON Strings are not similar", new JSONObject(originalJson).similar(new JSONObject(newJson))); } // these tests do not pass for the following reasons: // 1. Our XML parser does not handle generic HTML entities, only valid XML entities. Hence   // or other HTML specific entities would fail on reversability // 2. Our JSON implementation for storing the XML attributes uses the standard unordered map. // This means that can not be reversed reliably. // // /** // * Test texts taken from jsonml.org. Currently our implementation FAILS this conversion but shouldn't. // * Technically JsonML should be able to transform any valid xhtml document, but ours only supports // * standard XML entities, not HTML entities. // */ // @Test // public void testAttributeConversionReversabilityHTML() { // final String originalXml = "
#5D28D1Example text here
#AF44EF127310656
#AAD034 © 
"; // final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"\u00A0\",[\"span\",{ \"style\" : \"background-color:maroon\" },\"\u00A9\"],\"\u00A0\"]]]"; // final JSONArray json = JSONML.toJSONArray(originalXml,true); // final String actualJsonString = json.toString(); // // final String reverseXml = JSONML.toString(json); // assertNotEquals(originalXml, reverseXml); // // assertNotEquals(expectedJsonString, actualJsonString); // } // // /** // * Test texts taken from jsonml.org but modified to have XML entities only. // */ // @Test // public void testAttributeConversionReversabilityXML() { // final String originalXml = "
#5D28D1Example text here
#AF44EF127310656
#AAD034&><
"; // final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"&\",[\"span\",{ \"style\" : \"background-color:maroon\" },\">\"],\"<\"]]]"; // final JSONArray jsonML = JSONML.toJSONArray(originalXml,true); // final String actualJsonString = jsonML.toString(); // // final String reverseXml = JSONML.toString(jsonML); // // currently not equal because the hashing of the attribute objects makes the attribute // // order not happen the same way twice // assertEquals(originalXml, reverseXml); // // assertEquals(expectedJsonString, actualJsonString); // } @Test (timeout = 6000) public void testIssue484InfinteLoop1() { try { JSONML.toJSONObject("??*^M??|?CglR^F??`??>?w??PIlr^E??D^X^]?$?-^R?o??O?*??{OD?^FY??`2a????NM?b^Tq?:O?>S$^K?J?^FB.gUK?m^H??zE??^??!v]?^A???^[^A??^U?c??????h???s???g^Z???`?q^Dbi??:^QZl?)?}1^??k?0??:$V?$?Ovs(}J??^V????2;^QgQ?^_^A?^D?^U?Tg?K?`?h%c?hmGA??w??PIlr??D?$?-?o??O?*??{OD?Y??`2a????NM?bq?:O?>S$ ?J?B.gUK?m\b??zE???!v]???????c??????h???s???g???`?qbi??:Zl?)?}1^??k?0??:$V?$?Ovs(}J??????2;gQ????Tg?K?`?h%c?hmGA?"); final int maxNestingDepth = 42; try { JSONML.toJSONArray(wayTooLongMalformedXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth)); fail("Expecting a JSONException"); } catch (JSONException e) { assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">", e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth)); } } @Test public void testToJSONArrayMaxNestingDepthIsRespectedWithValidXML() { final String perfectlyFineXML = "\n" + " \n" + " sonoo\n" + " 56000\n" + " true\n" + " \n" + "\n"; final int maxNestingDepth = 1; try { JSONML.toJSONArray(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth)); fail("Expecting a JSONException"); } catch (JSONException e) { assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">", e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth)); } } @Test public void testToJSONArrayMaxNestingDepthWithValidFittingXML() { final String perfectlyFineXML = "\n" + " \n" + " sonoo\n" + " 56000\n" + " true\n" + " \n" + "\n"; final int maxNestingDepth = 3; try { JSONML.toJSONArray(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth)); } catch (JSONException e) { e.printStackTrace(); fail("XML document should be parsed as its maximum depth fits the maxNestingDepth " + "parameter of the JSONMLParserConfiguration used"); } } @Test public void testToJSONObjectMaxDefaultNestingDepthIsRespected() { final String wayTooLongMalformedXML = new String(new char[6000]).replace("\0", ""); try { JSONML.toJSONObject(wayTooLongMalformedXML, JSONMLParserConfiguration.ORIGINAL); fail("Expecting a JSONException"); } catch (JSONException e) { assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">", e.getMessage().startsWith("Maximum nesting depth of " + JSONMLParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH)); } } @Test public void testToJSONObjectUnlimitedNestingDepthIsPossible() { int actualDepth = JSONMLParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH +10; final String deeperThanDefaultMax = new String(new char[actualDepth]).replace("\0", "") + "value" + new String(new char[actualDepth]).replace("\0", ""); try { JSONML.toJSONObject(deeperThanDefaultMax, JSONMLParserConfiguration.ORIGINAL .withMaxNestingDepth(JSONMLParserConfiguration.UNDEFINED_MAXIMUM_NESTING_DEPTH)); } catch (JSONException e) { e.printStackTrace(); fail("XML document should be parsed beyond the default maximum depth if maxNestingDepth " + "parameter is set to -1 in JSONMLParserConfiguration"); } } @Test public void testToJSONObjectMaxNestingDepthOf42IsRespected() { final String wayTooLongMalformedXML = new String(new char[6000]).replace("\0", ""); final int maxNestingDepth = 42; try { JSONML.toJSONObject(wayTooLongMalformedXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth)); fail("Expecting a JSONException"); } catch (JSONException e) { assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">", e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth)); } } @Test public void testToJSONObjectMaxNestingDepthIsRespectedWithValidXML() { final String perfectlyFineXML = "\n" + " \n" + " sonoo\n" + " 56000\n" + " true\n" + " \n" + "\n"; final int maxNestingDepth = 1; try { JSONML.toJSONObject(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth)); fail("Expecting a JSONException"); } catch (JSONException e) { assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">", e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth)); } } @Test public void testToJSONObjectMaxNestingDepthWithValidFittingXML() { final String perfectlyFineXML = "\n" + " \n" + " sonoo\n" + " 56000\n" + " true\n" + " \n" + "\n"; final int maxNestingDepth = 3; try { JSONML.toJSONObject(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth)); } catch (JSONException e) { e.printStackTrace(); fail("XML document should be parsed as its maximum depth fits the maxNestingDepth " + "parameter of the JSONMLParserConfiguration used"); } } /** * Tests that malformed XML causing type mismatch throws JSONException. * Previously threw ClassCastException when parse() returned String instead of JSONArray. * Related to issue #1034 */ @Test(expected = JSONException.class) public void testMalformedXMLThrowsJSONExceptionNotClassCast() { // This malformed XML causes parse() to return wrong type byte[] data = {0x3c, 0x0a, 0x2f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x3e, 0x42}; String xmlStr = new String(data); JSONML.toJSONArray(xmlStr); } /** * Tests that type mismatch in toJSONObject throws JSONException. * Validates safe type casting in toJSONObject methods. */ @Test public void testToJSONObjectTypeMismatch() { // Create XML that would cause parse() to return wrong type String xmlStr = "<\n/\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff>B"; try { JSONML.toJSONObject(xmlStr); fail("Expected JSONException for type mismatch"); } catch (ClassCastException e) { fail("Should throw JSONException, not ClassCastException"); } catch (JSONException e) { // Expected - verify it's about type mismatch assertTrue("Exception message should mention type error", e.getMessage().contains("Expected") || e.getMessage().contains("got")); } } /** * Tests that valid XML still works correctly after the fix. * Ensures the type checking doesn't break normal operation. */ @Test public void testValidXMLStillWorks() { String xmlStr = "value"; try { JSONArray jsonArray = JSONML.toJSONArray(xmlStr); assertNotNull("JSONArray should not be null", jsonArray); assertEquals("root", jsonArray.getString(0)); } catch (Exception e) { fail("Valid XML should not throw exception: " + e.getMessage()); } } /** * Tests that valid XML to JSONObject still works correctly. */ @Test public void testValidXMLToJSONObjectStillWorks() { String xmlStr = "content"; try { JSONObject jsonObject = JSONML.toJSONObject(xmlStr); assertNotNull("JSONObject should not be null", jsonObject); assertEquals("root", jsonObject.getString("tagName")); } catch (Exception e) { fail("Valid XML should not throw exception: " + e.getMessage()); } } /** * Tests that the childNodes attribute is reserved in object-form parsing. */ @Test public void testToJSONObjectRejectsReservedChildNodesAttribute() { String xmlStr = "

text

"; for (boolean keepStrings : new boolean[] {false, true}) { JSONException exception = assertThrows(JSONException.class, () -> JSONML.toJSONObject(xmlStr, keepStrings)); assertTrue(exception.getMessage().startsWith("Reserved attribute.")); } } /** * Tests that the ordinary childNode attribute is preserved in object-form parsing. */ @Test public void testToJSONObjectPreservesChildNodeAttribute() { String xmlStr = "

text

"; for (boolean keepStrings : new boolean[] {false, true}) { JSONObject jsonObject = JSONML.toJSONObject(xmlStr, keepStrings); assertEquals("p", jsonObject.getString("tagName")); assertEquals("metadata", jsonObject.getString("childNode")); assertEquals("text", jsonObject.getJSONArray("childNodes").getString(0)); } } /** * Tests that the tagName attribute remains reserved in object-form parsing. */ @Test public void testToJSONObjectRejectsReservedTagNameAttribute() { String xmlStr = "

text

"; for (boolean keepStrings : new boolean[] {false, true}) { JSONException exception = assertThrows(JSONException.class, () -> JSONML.toJSONObject(xmlStr, keepStrings)); assertTrue(exception.getMessage().startsWith("Reserved attribute.")); } } /** * Tests that childNode and childNodes attributes are preserved in array-form parsing. */ @Test public void testToJSONArrayPreservesChildNodeAttributes() { String xmlStr = "

text

"; for (boolean keepStrings : new boolean[] {false, true}) { JSONArray jsonArray = JSONML.toJSONArray(xmlStr, keepStrings); assertEquals(3, jsonArray.length()); assertEquals("p", jsonArray.getString(0)); JSONObject attributes = jsonArray.getJSONObject(1); assertEquals("singular", attributes.getString("childNode")); assertEquals("plural", attributes.getString("childNodes")); assertEquals("text", jsonArray.getString(2)); } } }