From 215321cd28008cfd5b2192efa16cf10f737206c3 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 18 Jul 2016 15:01:36 -0400 Subject: [PATCH] updates Test cases to support new JSONML and XML conversion options --- src/test/java/org/json/junit/JSONMLTest.java | 37 ++++++++++++++++ src/test/java/org/json/junit/XMLTest.java | 44 ++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 953a39d..1298591 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -698,4 +698,41 @@ public void commentsInXML() { 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. See test result in + * comment below. + */ + @Test + public void testToJSONArray_reversibility() { + final String originalXml = "011000True"; + final String revertedXml = JSONML.toString(JSONML.toJSONArray(originalXml, false)); + assertNotEquals(revertedXml, originalXml); + } + + /** + * test passes when using the new method toJsonML. + */ + @Test + public void testToJsonML() { + 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); + } + } diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index d35c8ac..2f3fea7 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1,12 +1,14 @@ package org.json.junit; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import java.io.IOException; import org.json.JSONArray; import org.json.JSONException; +import org.json.JSONML; import org.json.JSONObject; import org.json.XML; import org.junit.Rule; @@ -723,4 +725,46 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) { } */ } + + /** + * JSON string lost leading zero and converted "True" to true. + */ + @Test + public void testToJSONArray_jsonOutput() { + final String originalXml = "011000True"; + final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"; + final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false); + + assertEquals(expectedJsonString, actualJsonOutput.toString()); + } + + /** + * JSON string cannot be reverted to original xml. + */ + @Test + public void testToJSONArray_reversibility() { + final String originalXml = "011000True"; + final String revertedXml = XML.toString(XML.toJSONObject(originalXml, false)); + + assertNotEquals(revertedXml, originalXml); + } + + /** + * test passes when using the new method toJsonArray. + */ + @Test + public void testToJsonXML() { + final String originalXml = "011000True"; + final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}"; + + final JSONObject json = XML.toJSONObject(originalXml,true); + assertEquals(expectedJsonString, json.toString()); + + final String reverseXml = XML.toString(json); + // this reversal isn't exactly the same. use JSONML for an exact reversal + final String expectedReverseXml = "01011000True"; + + assertEquals(expectedReverseXml, reverseXml); + } + } \ No newline at end of file