mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
coverage 84.9%
This commit is contained in:
parent
9a6215c3be
commit
de13c7de86
1 changed files with 117 additions and 149 deletions
206
JSONMLTest.java
206
JSONMLTest.java
|
@ -31,6 +31,38 @@ public class JSONMLTest {
|
|||
JSONML.toJSONArray(xmlStr);
|
||||
}
|
||||
|
||||
@Test(expected=JSONException.class)
|
||||
public void emptyTagException() {
|
||||
String jsonArrayStr =
|
||||
"[\"addresses\","+
|
||||
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
|
||||
"["+
|
||||
"[\"name\"],"+
|
||||
"[\"nocontent\"],"+
|
||||
"\">\""+
|
||||
"]"+
|
||||
"]";
|
||||
JSONArray jsonArray = new JSONArray(jsonArrayStr);
|
||||
JSONML.toString(jsonArray);
|
||||
}
|
||||
|
||||
@Test(expected=JSONException.class)
|
||||
public void spaceInTagException() {
|
||||
String jsonArrayStr =
|
||||
"[\"addresses\","+
|
||||
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
|
||||
"[\"addr esses\","+
|
||||
"[\"name\"],"+
|
||||
"[\"nocontent\"],"+
|
||||
"\">\""+
|
||||
"]"+
|
||||
"]";
|
||||
JSONArray jsonArray = new JSONArray(jsonArrayStr);
|
||||
JSONML.toString(jsonArray);
|
||||
}
|
||||
|
||||
@Test(expected=JSONException.class)
|
||||
public void unvalidSlashInTagException() {
|
||||
String xmlStr =
|
||||
|
@ -136,7 +168,10 @@ public class JSONMLTest {
|
|||
"]";
|
||||
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
|
||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray);
|
||||
String xmlToStr = JSONML.toString(jsonArray);
|
||||
JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -168,8 +203,8 @@ public class JSONMLTest {
|
|||
|
||||
String expectedStr =
|
||||
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||
"\"childNodes\":[{"+
|
||||
"\"childNodes\":["+
|
||||
"{\"childNodes\":["+
|
||||
"{\"childNodes\":[\"Joe Tester\"],"+
|
||||
"\"tagName\":\"name\"},"+
|
||||
"{\"childNodes\":[\"[CDATA[Baker street 5]\"],"+
|
||||
|
@ -192,24 +227,30 @@ public class JSONMLTest {
|
|||
"{\"childNodes\":["+
|
||||
"{\"childNodes\":[1],"+
|
||||
"\"tagName\":\"value\"},"+
|
||||
"{\"childNodes\":[1],"+
|
||||
"{\"childNodes\":[2],"+
|
||||
"\"tagName\":\"value\"},"+
|
||||
"{\"childNodes\":[1],"+
|
||||
"{\"childNodes\":[3],"+
|
||||
"\"tagName\":\"value\"},"+
|
||||
"{\"childNodes\":[1],"+
|
||||
"{\"childNodes\":[4.1],"+
|
||||
"\"tagName\":\"value\"},"+
|
||||
"{\"childNodes\":[1],"+
|
||||
"\"tagName\":\"value\"},"+
|
||||
"],"
|
||||
|
||||
"\"tagName\":\"address\"}],"+
|
||||
"{\"childNodes\":[5.2],"+
|
||||
"\"tagName\":\"value\"}"+
|
||||
"],"+
|
||||
"\"tagName\":\"ArrayOfNum\"}"+
|
||||
"],"+
|
||||
"\"tagName\":\"address\"}"+
|
||||
"],"+
|
||||
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+
|
||||
"\"tagName\":\"addresses\"}";
|
||||
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
String xmlToStr = JSONML.toString(jsonObject);
|
||||
JSONObject finalJsonObject = JSONML.toJSONObject(xmlToStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basicXMLAsArray() {
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
|
@ -241,20 +282,33 @@ public class JSONMLTest {
|
|||
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
|
||||
"[\"address\","+
|
||||
"[\"name\", \"Joe Tester\"]"+
|
||||
"[\"street\", \"[CDATA[Baker street 5]\"]"+
|
||||
"[\"NothingHere\"]"+
|
||||
"[\"TrueValue\", true]"+
|
||||
"[\"FalseValue\", false]"+
|
||||
"[\"NullValue\", null]"+
|
||||
"[\"PositiveValue\", 42]"+
|
||||
"[\"NegativeValue\", -23]"+
|
||||
"[\"DoubleValue\", -23.45]"+
|
||||
"[\"Nan\", \"-23x.45\"]"+
|
||||
"[\"ArrayOfNum\", ]"+
|
||||
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
"[\"name\",\"Joe Tester\"],"+
|
||||
"[\"street\",\"[CDATA[Baker street 5]\"],"+
|
||||
"[\"NothingHere\"],"+
|
||||
"[\"TrueValue\",true],"+
|
||||
"[\"FalseValue\",false],"+
|
||||
"[\"NullValue\",null],"+
|
||||
"[\"PositiveValue\",42],"+
|
||||
"[\"NegativeValue\",-23],"+
|
||||
"[\"DoubleValue\",-23.45],"+
|
||||
"[\"Nan\",\"-23x.45\"],"+
|
||||
"[\"ArrayOfNum\","+
|
||||
"[\"value\",1],"+
|
||||
"[\"value\",2],"+
|
||||
"[\"value\",3],"+
|
||||
"[\"value\",4.1],"+
|
||||
"[\"value\",5.2]"+
|
||||
"]"+
|
||||
"]"+
|
||||
"]";
|
||||
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
|
||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
||||
String xmlToStr = JSONML.toString(jsonArray);
|
||||
JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
// TODO: this test fails because JSONML.toString() does not emit values
|
||||
// for true, false, null, and numbers
|
||||
// Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -273,107 +327,21 @@ public class JSONMLTest {
|
|||
"</address>\n"+
|
||||
"</addresses>";
|
||||
String expectedStr =
|
||||
"{\"childNodes\":["+
|
||||
"{\"childNodes\":["+
|
||||
"[\"addresses\","+
|
||||
"[\"address\","+
|
||||
"\" this is -- <another> comment \","+
|
||||
"{\"childNodes\":[\"Joe Tester\"],"+
|
||||
"\"tagName\":\"name\"},"+
|
||||
"{\"childNodes\":[\"Baker street 5\"],"+
|
||||
"\"tagName\":\"street\"}],"+
|
||||
"\"tagName\":\"address\"}],"+
|
||||
"\"tagName\":\"addresses\"}";
|
||||
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonObjectToString() {
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
" <address>\n"+
|
||||
" <name>[CDATA[Joe & T > e < s " t ' er]]</name>\n"+
|
||||
" <street>Baker street 5</street>\n"+
|
||||
" <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
|
||||
String expectedStr =
|
||||
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||
"\"childNodes\":["+
|
||||
"{\"childNodes\":["+
|
||||
"{\"childNodes\":[\"[CDATA[Joe & T > e < s \\\" t ' er]]\"],"+
|
||||
"\"tagName\":\"name\"},"+
|
||||
"{\"childNodes\":[\"Baker street 5\"],"+
|
||||
"\"tagName\":\"street\"},"+
|
||||
"{\"childNodes\":[\"1, 2, 3, 4.1, 5.2\"],"+
|
||||
"\"tagName\":\"ArrayOfNum\"}],"+
|
||||
"\"tagName\":\"address\"}],"+
|
||||
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+
|
||||
"\"tagName\":\"addresses\"}";
|
||||
|
||||
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
||||
String xmlToStr = JSONML.toString(jsonObject);
|
||||
JSONObject finalJsonObject = JSONML.toJSONObject(xmlToStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonArrayToString() {
|
||||
String xmlStr =
|
||||
"<tag0>"+
|
||||
"<tag1>"+
|
||||
"<tag2>5</tag2>"+
|
||||
"<tag2>10</tag2>"+
|
||||
"<tag2>15</tag2>"+
|
||||
"</tag1>"+
|
||||
"<tag2>val2</tag2>"+
|
||||
"<tag3>val3</tag3>"+
|
||||
"<tag4>"+
|
||||
"<tag5>-6</tag5>"+
|
||||
"<tag5>true</tag5>"+
|
||||
"</tag4>"+
|
||||
"<tag6>false</tag6>"+
|
||||
"<tag7>null</tag7>"+
|
||||
"<tag1>"+
|
||||
"<tag8>10</tag8>"+
|
||||
"<tag8>20</tag8>"+
|
||||
"<tag8>33.33</tag8>"+
|
||||
"<tag8>5220</tag8>"+
|
||||
"</tag1>"+
|
||||
"</tag0>";
|
||||
String expectedStr =
|
||||
"[\"tag0\","+
|
||||
"[\"tag1\","+
|
||||
"[\"tag2\",5],"+
|
||||
"[\"tag2\",10],"+
|
||||
"[\"tag2\",15]"+
|
||||
"],"+
|
||||
"[\"tag2\",\"val2\"],"+
|
||||
"[\"tag3\",\"val3\"],"+
|
||||
"[\"tag4\","+
|
||||
"[\"tag5\",-6],"+
|
||||
"[\"tag5\",true]"+
|
||||
"],"+
|
||||
"[\"tag6\",false],"+
|
||||
"[\"tag7\",null],"+
|
||||
"[\"tag1\","+
|
||||
"[\"tag8\",10],"+
|
||||
"[\"tag8\",20],"+
|
||||
"[\"tag8\",33.33],"+
|
||||
"[\"tag8\",5220]"+
|
||||
"[\"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);
|
||||
JSONArray expectedJsonArray= new JSONArray(expectedStr);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray);
|
||||
Util.compareActualVsExpectedJsonArrays(finalJsonArray,expectedJsonArray);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
// TODO this test fails because JSONML.toString() escapes the
|
||||
// <> chars in the comment.
|
||||
// Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue