mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
coverage XMLTest 81.2% / XMLTokener 82.2%
This commit is contained in:
parent
a18e9f7a25
commit
89f359e4f8
2 changed files with 225 additions and 59 deletions
34
Util.java
34
Util.java
|
@ -9,12 +9,11 @@ import org.json.*;
|
|||
public class Util {
|
||||
|
||||
|
||||
/////////////////////////// UTILITY METHODS /////////////////////////
|
||||
|
||||
/**
|
||||
* Compares two json arrays for equality
|
||||
* @param jsonArray created by the code to be tested
|
||||
* @param expectedJsonArray created specifically for compar
|
||||
* @param expectedJsonArray created specifically for comparing
|
||||
*/
|
||||
public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
|
||||
JSONArray expectedJsonArray) {
|
||||
|
@ -27,19 +26,17 @@ public class Util {
|
|||
jsonObject.length() == expectedJsonObject.length());
|
||||
Iterator<String> keys = jsonObject.keys();
|
||||
while (keys.hasNext()) {
|
||||
// TODO: check for nonstring types
|
||||
String key = keys.next();
|
||||
Object value = jsonObject.get(key);
|
||||
String testStr = "row: "+i+" key: "+key+" val: "+value.toString();
|
||||
String actualStr = expectedJsonObject .get(key).toString();
|
||||
assertTrue("values should be equal for actual: "+testStr+
|
||||
" expected: "+actualStr,
|
||||
value.equals(expectedJsonArray.getJSONObject(i).
|
||||
get(key).toString()));
|
||||
compareJsonObjectEntries(jsonObject, expectedJsonObject, key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two json objects for equality
|
||||
* @param jsonObject created by the code to be tested
|
||||
* @param expectedJsonObject created specifically for comparing
|
||||
*/
|
||||
public static void compareActualVsExpectedJsonObjects(
|
||||
JSONObject jsonObject, JSONObject expectedJsonObject) {
|
||||
assertTrue("jsonObjects should have the same length",
|
||||
|
@ -47,6 +44,18 @@ public class Util {
|
|||
Iterator<String> keys = jsonObject.keys();
|
||||
while (keys.hasNext()) {
|
||||
String key = keys.next();
|
||||
compareJsonObjectEntries(jsonObject, expectedJsonObject, key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two jsonObject entries
|
||||
* @param jsonObject created by the code to be tested
|
||||
* @param expectedJsonObject created specifically for comparing
|
||||
* @param key key to the jsonObject entry to be compared
|
||||
*/
|
||||
private static void compareJsonObjectEntries(JSONObject jsonObject,
|
||||
JSONObject expectedJsonObject, String key) {
|
||||
Object value = jsonObject.get(key);
|
||||
Object expectedValue = expectedJsonObject.get(key);
|
||||
if (value instanceof JSONObject) {
|
||||
|
@ -61,6 +70,10 @@ public class Util {
|
|||
expectedJsonObject.getJSONArray(key);
|
||||
compareActualVsExpectedJsonArrays(
|
||||
childJsonArray, expectedChildJsonArray);
|
||||
} else if (!(value instanceof String) && !(expectedValue instanceof String)) {
|
||||
assertTrue("string values should be equal for actual: "+
|
||||
value.toString()+" expected: "+expectedValue.toString(),
|
||||
value.toString().equals(expectedValue.toString()));
|
||||
} else {
|
||||
String testStr = "key: "+key+" val: "+value.toString();
|
||||
String actualStr = expectedValue.toString();
|
||||
|
@ -69,5 +82,4 @@ public class Util {
|
|||
value.equals(expectedValue.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
210
XMLTest.java
210
XMLTest.java
|
@ -1,7 +1,5 @@
|
|||
package org.json.junit;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.json.*;
|
||||
|
@ -22,25 +20,19 @@ public class XMLTest {
|
|||
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
|
||||
}
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void shouldHandleNullJSONXML() {
|
||||
JSONObject jsonObject= null;
|
||||
String xmlStr = XML.toString(jsonObject);
|
||||
@Test
|
||||
public void shouldHandleEmptyXML() {
|
||||
|
||||
String xmlStr = "";
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
|
||||
}
|
||||
|
||||
@Test(expected=JSONException.class)
|
||||
public void shouldHandleInvalidBangInTag() {
|
||||
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>Joe Tester</name>\n"+
|
||||
" <!street>abc street</street>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
@Test
|
||||
public void shouldHandleNonXML() {
|
||||
String xmlStr = "{ \"this is\": \"not xml\"}";
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
assertTrue(jsonObject == null);
|
||||
assertTrue("xml string should be empty", jsonObject.length() == 0);
|
||||
}
|
||||
|
||||
@Test(expected=JSONException.class)
|
||||
|
@ -54,22 +46,96 @@ public class XMLTest {
|
|||
" <street>abc street</street>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
assertTrue(jsonObject == null);
|
||||
XML.toJSONObject(xmlStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleEmptyXML() {
|
||||
@Test(expected=JSONException.class)
|
||||
public void shouldHandleInvalidBangInTag() {
|
||||
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/>\n"+
|
||||
" <!>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
XML.toJSONObject(xmlStr);
|
||||
}
|
||||
|
||||
String xmlStr = "";
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
|
||||
@Test(expected=JSONException.class)
|
||||
public void shouldHandleInvalidBangNoCloseInTag() {
|
||||
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/>\n"+
|
||||
" <!\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
XML.toJSONObject(xmlStr);
|
||||
}
|
||||
|
||||
@Test(expected=JSONException.class)
|
||||
public void shouldHandleNoCloseStartTag() {
|
||||
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/>\n"+
|
||||
" <abc\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
XML.toJSONObject(xmlStr);
|
||||
}
|
||||
|
||||
@Test(expected=JSONException.class)
|
||||
public void shouldHandleInvalidCDATABangInTag() {
|
||||
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>Joe Tester</name>\n"+
|
||||
" <![[]>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
XML.toJSONObject(xmlStr);
|
||||
}
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void shouldHandleNullJSONXML() {
|
||||
JSONObject jsonObject= null;
|
||||
XML.toString(jsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleEmptyJSONXML() {
|
||||
JSONObject jsonObject= new JSONObject();
|
||||
String xmlStr = XML.toString(jsonObject);
|
||||
assertTrue("xml string should be empty", xmlStr.length() == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNoStartTag() {
|
||||
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/>\n"+
|
||||
" <nocontent/>>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
String expectedStr =
|
||||
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+
|
||||
"content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
|
||||
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -82,17 +148,28 @@ public class XMLTest {
|
|||
" <name>Joe Tester</name>\n"+
|
||||
" <street>[CDATA[Baker street 5]</street>\n"+
|
||||
" <NothingHere/>\n"+
|
||||
" <TrueValue>true</TrueValue>\n"+
|
||||
" <FalseValue>false</FalseValue>\n"+
|
||||
" <NullValue>null</NullValue>\n"+
|
||||
" <PositiveValue>42</PositiveValue>\n"+
|
||||
" <NegativeValue>-23</NegativeValue>\n"+
|
||||
" <DoubleValue>-23.45</DoubleValue>\n"+
|
||||
" <Nan>-23x.45</Nan>\n"+
|
||||
" <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
|
||||
String expectedStr =
|
||||
"{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
|
||||
"\"name\":\"Joe Tester\",\"NothingHere\":\"\"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||
"\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+
|
||||
"\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
|
||||
"\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+
|
||||
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
|
||||
"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
||||
"XMLSchema-instance\"}}";
|
||||
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
@ -113,6 +190,11 @@ public class XMLTest {
|
|||
" </address>\n"+
|
||||
"</addresses>";
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+
|
||||
"street 5\",\"name\":\"Joe Tester\",\"content\":\" this is -- "+
|
||||
"<another> comment \"}}}";
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -124,12 +206,15 @@ public class XMLTest {
|
|||
" <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 =
|
||||
"{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+
|
||||
"\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||
"\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+
|
||||
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
|
||||
"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
||||
"XMLSchema-instance\"}}";
|
||||
|
||||
|
@ -140,4 +225,73 @@ public class XMLTest {
|
|||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleContentNoArraytoString() {
|
||||
String expectedStr =
|
||||
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+
|
||||
"content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
|
||||
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
String finalStr = XML.toString(expectedJsonObject);
|
||||
String expectedFinalStr = "<addresses><address><name/><nocontent/>>"+
|
||||
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
|
||||
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
|
||||
"ma-instance</xmlns:xsi></addresses>";
|
||||
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
|
||||
finalStr+"]", expectedFinalStr.equals(finalStr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleContentArraytoString() {
|
||||
String expectedStr =
|
||||
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+
|
||||
"content\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
|
||||
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
String finalStr = XML.toString(expectedJsonObject);
|
||||
String expectedFinalStr = "<addresses><address><name/><nocontent/>"+
|
||||
"1\n2\n3"+
|
||||
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
|
||||
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
|
||||
"ma-instance</xmlns:xsi></addresses>";
|
||||
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
|
||||
finalStr+"]", expectedFinalStr.equals(finalStr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleArraytoString() {
|
||||
String expectedStr =
|
||||
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+
|
||||
"\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
|
||||
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
String finalStr = XML.toString(expectedJsonObject);
|
||||
String expectedFinalStr = "<addresses><address><name/><nocontent/>"+
|
||||
"<something>1</something><something>2</something><something>3</something>"+
|
||||
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
|
||||
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
|
||||
"ma-instance</xmlns:xsi></addresses>";
|
||||
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
|
||||
finalStr+"]", expectedFinalStr.equals(finalStr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNestedArraytoString() {
|
||||
String xmlStr =
|
||||
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+
|
||||
"\"outer\":[[1], [2], [3]]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
|
||||
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
|
||||
JSONObject jsonObject = new JSONObject(xmlStr);
|
||||
String finalStr = XML.toString(jsonObject);
|
||||
JSONObject finalJsonObject = XML.toJSONObject(finalStr);
|
||||
String expectedStr = "<addresses><address><name/><nocontent/>"+
|
||||
"<outer><array>1</array></outer><outer><array>2</array>"+
|
||||
"</outer><outer><array>3</array></outer>"+
|
||||
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
|
||||
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
|
||||
"ma-instance</xmlns:xsi></addresses>";
|
||||
JSONObject expectedJsonObject = XML.toJSONObject(expectedStr);
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue