mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 16:00:51 -07:00
in progress
This commit is contained in:
parent
bc07b5196b
commit
ef7e0c7d08
2 changed files with 186 additions and 180 deletions
298
JSONMLTest.java
298
JSONMLTest.java
|
@ -12,30 +12,27 @@ import org.junit.Test;
|
||||||
public class JSONMLTest {
|
public class JSONMLTest {
|
||||||
|
|
||||||
@Test(expected=NullPointerException.class)
|
@Test(expected=NullPointerException.class)
|
||||||
public void shouldHandleNullXML() {
|
public void nullXMLException() {
|
||||||
|
|
||||||
String xmlStr = null;
|
String xmlStr = null;
|
||||||
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
JSONML.toJSONObject(xmlStr);
|
||||||
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=JSONException.class)
|
@Test(expected=JSONException.class)
|
||||||
public void shouldHandleEmptyXML() {
|
public void emptyXMLException() {
|
||||||
|
|
||||||
String xmlStr = "";
|
String xmlStr = "";
|
||||||
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
JSONML.toJSONObject(xmlStr);
|
||||||
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void shouldHandleNonXML() {
|
|
||||||
String xmlStr = "{ \"this is\": \"not xml\"}";
|
|
||||||
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
|
||||||
assertTrue("xml string should be empty", jsonObject.length() == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=JSONException.class)
|
@Test(expected=JSONException.class)
|
||||||
public void shouldHandleInvalidSlashInTag() {
|
public void nonXMLException() {
|
||||||
|
String xmlStr = "{ \"this is\": \"not xml\"}";
|
||||||
|
JSONML.toJSONObject(xmlStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=JSONException.class)
|
||||||
|
public void unvalidSlashInTagException() {
|
||||||
String xmlStr =
|
String xmlStr =
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||||
|
@ -49,7 +46,7 @@ public class JSONMLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=JSONException.class)
|
@Test(expected=JSONException.class)
|
||||||
public void shouldHandleInvalidBangInTag() {
|
public void invalidBangInTagException() {
|
||||||
String xmlStr =
|
String xmlStr =
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||||
|
@ -63,7 +60,7 @@ public class JSONMLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=JSONException.class)
|
@Test(expected=JSONException.class)
|
||||||
public void shouldHandleInvalidBangNoCloseInTag() {
|
public void invalidBangNoCloseInTagException() {
|
||||||
String xmlStr =
|
String xmlStr =
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||||
|
@ -77,7 +74,7 @@ public class JSONMLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=JSONException.class)
|
@Test(expected=JSONException.class)
|
||||||
public void shouldHandleNoCloseStartTag() {
|
public void noCloseStartTagException() {
|
||||||
String xmlStr =
|
String xmlStr =
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||||
|
@ -91,7 +88,7 @@ public class JSONMLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=JSONException.class)
|
@Test(expected=JSONException.class)
|
||||||
public void shouldHandleInvalidCDATABangInTag() {
|
public void invalidCDATABangInTagException() {
|
||||||
String xmlStr =
|
String xmlStr =
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||||
|
@ -105,99 +102,129 @@ public class JSONMLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=NullPointerException.class)
|
@Test(expected=NullPointerException.class)
|
||||||
public void shouldHandleNullJSONXML() {
|
public void nullJSONXMLException() {
|
||||||
JSONObject jsonObject= null;
|
JSONObject jsonObject= null;
|
||||||
JSONML.toString(jsonObject);
|
JSONML.toString(jsonObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected=JSONException.class)
|
||||||
public void shouldHandleEmptyJSONXML() {
|
public void emptyJSONXMLException() {
|
||||||
JSONObject jsonObject= new JSONObject();
|
JSONObject jsonObject= new JSONObject();
|
||||||
String xmlStr = JSONML.toString(jsonObject);
|
JSONML.toString(jsonObject);
|
||||||
assertTrue("xml string should be empty", xmlStr.length() == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHandleNoStartTag() {
|
public void noStartTag() {
|
||||||
String xmlStr =
|
String xmlStr =
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
"xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||||
" <address>\n"+
|
"<address>\n"+
|
||||||
" <name/>\n"+
|
"<name/>\n"+
|
||||||
" <nocontent/>>\n"+
|
"<nocontent/>>\n"+
|
||||||
" </address>\n"+
|
"</address>\n"+
|
||||||
"</addresses>";
|
"</addresses>";
|
||||||
String expectedStr =
|
String expectedStr =
|
||||||
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+
|
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||||
"content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
|
"\"childNodes\":[{"+
|
||||||
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
|
"\"childNodes\":"+
|
||||||
|
"[{\"tagName\":\"name\"},"+
|
||||||
|
"{\"tagName\":\"nocontent\"},"+
|
||||||
|
"\">\"],"+
|
||||||
|
"\"tagName\":\"address\"}],"+
|
||||||
|
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+
|
||||||
|
"\"tagName\":\"addresses\"}";
|
||||||
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHandleSimpleXML() {
|
public void simpleXML() {
|
||||||
String xmlStr =
|
String xmlStr =
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
"xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||||
" <address>\n"+
|
"<address>\n"+
|
||||||
" <name>Joe Tester</name>\n"+
|
"<name>Joe Tester</name>\n"+
|
||||||
" <street>[CDATA[Baker street 5]</street>\n"+
|
"<street>[CDATA[Baker street 5]</street>\n"+
|
||||||
" <NothingHere/>\n"+
|
"<NothingHere/>\n"+
|
||||||
" <TrueValue>true</TrueValue>\n"+
|
"<TrueValue>true</TrueValue>\n"+
|
||||||
" <FalseValue>false</FalseValue>\n"+
|
"<FalseValue>false</FalseValue>\n"+
|
||||||
" <NullValue>null</NullValue>\n"+
|
"<NullValue>null</NullValue>\n"+
|
||||||
" <PositiveValue>42</PositiveValue>\n"+
|
"<PositiveValue>42</PositiveValue>\n"+
|
||||||
" <NegativeValue>-23</NegativeValue>\n"+
|
"<NegativeValue>-23</NegativeValue>\n"+
|
||||||
" <DoubleValue>-23.45</DoubleValue>\n"+
|
"<DoubleValue>-23.45</DoubleValue>\n"+
|
||||||
" <Nan>-23x.45</Nan>\n"+
|
"<Nan>-23x.45</Nan>\n"+
|
||||||
" <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>\n"+
|
"<ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>\n"+
|
||||||
" </address>\n"+
|
"</address>\n"+
|
||||||
"</addresses>";
|
"</addresses>";
|
||||||
|
|
||||||
String expectedStr =
|
String expectedStr =
|
||||||
"{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
|
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||||
"\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+
|
"\"childNodes\":[{"+
|
||||||
"\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
|
"\"childNodes\":["+
|
||||||
"\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+
|
"{\"childNodes\":[\"Joe Tester\"],"+
|
||||||
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
|
"\"tagName\":\"name\"},"+
|
||||||
"},\"xsi:noNamespaceSchemaLocation\":"+
|
"{\"childNodes\":[\"[CDATA[Baker street 5]\"],"+
|
||||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
"\"tagName\":\"street\"},"+
|
||||||
"XMLSchema-instance\"}}";
|
"{\"tagName\":\"NothingHere\"},"+
|
||||||
|
"{\"childNodes\":[true],"+
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
"\"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\":[\"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);
|
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
||||||
|
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHandleCommentsInXML() {
|
public void commentsInXML() {
|
||||||
|
|
||||||
String xmlStr =
|
String xmlStr =
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||||
"<!-- this is a comment -->\n"+
|
"<!-- this is a comment -->\n"+
|
||||||
"<addresses>\n"+
|
"<addresses>\n"+
|
||||||
" <address>\n"+
|
"<address>\n"+
|
||||||
" <![CDATA[ this is -- <another> comment ]]>\n"+
|
"<![CDATA[ this is -- <another> comment ]]>\n"+
|
||||||
" <name>Joe Tester</name>\n"+
|
"<name>Joe Tester</name>\n"+
|
||||||
" <!-- this is a - multi line \n"+
|
"<!-- this is a - multi line \n"+
|
||||||
" comment -->\n"+
|
"comment -->\n"+
|
||||||
" <street>Baker street 5</street>\n"+
|
"<street>Baker street 5</street>\n"+
|
||||||
" </address>\n"+
|
"</address>\n"+
|
||||||
"</addresses>";
|
"</addresses>";
|
||||||
|
String expectedStr =
|
||||||
|
"{\"childNodes\":["+
|
||||||
|
"{\"childNodes\":["+
|
||||||
|
"\" 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 jsonObject = JSONML.toJSONObject(xmlStr);
|
||||||
String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+
|
|
||||||
"street 5\",\"name\":\"Joe Tester\",\"content\":\" this is -- "+
|
|
||||||
"<another> comment \"}}}";
|
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHandleToString() {
|
public void jsonObjectToString() {
|
||||||
String xmlStr =
|
String xmlStr =
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||||
|
@ -210,12 +237,18 @@ public class JSONMLTest {
|
||||||
"</addresses>";
|
"</addresses>";
|
||||||
|
|
||||||
String expectedStr =
|
String expectedStr =
|
||||||
"{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+
|
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
|
||||||
"\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+
|
"\"childNodes\":["+
|
||||||
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
|
"{\"childNodes\":["+
|
||||||
"},\"xsi:noNamespaceSchemaLocation\":"+
|
"{\"childNodes\":[\"[CDATA[Joe & T > e < s \\\" t ' er]]\"],"+
|
||||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
"\"tagName\":\"name\"},"+
|
||||||
"XMLSchema-instance\"}}";
|
"{\"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);
|
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
|
||||||
String xmlToStr = JSONML.toString(jsonObject);
|
String xmlToStr = JSONML.toString(jsonObject);
|
||||||
|
@ -226,72 +259,57 @@ public class JSONMLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHandleContentNoArraytoString() {
|
public void jsonArrayToString() {
|
||||||
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 = JSONML.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 = JSONML.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 = JSONML.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 =
|
String xmlStr =
|
||||||
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+
|
"<tag0>"+
|
||||||
"\"outer\":[[1], [2], [3]]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
|
"<tag1>"+
|
||||||
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
|
"<tag2>5</tag2>"+
|
||||||
JSONObject jsonObject = new JSONObject(xmlStr);
|
"<tag2>10</tag2>"+
|
||||||
String finalStr = JSONML.toString(jsonObject);
|
"<tag2>15</tag2>"+
|
||||||
JSONObject finalJsonObject = JSONML.toJSONObject(finalStr);
|
"</tag1>"+
|
||||||
String expectedStr = "<addresses><address><name/><nocontent/>"+
|
"<tag2>val2</tag2>"+
|
||||||
"<outer><array>1</array></outer><outer><array>2</array>"+
|
"<tag3>val3</tag3>"+
|
||||||
"</outer><outer><array>3</array></outer>"+
|
"<tag4>"+
|
||||||
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
|
"<tag5>-6</tag5>"+
|
||||||
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
|
"<tag5>true</tag5>"+
|
||||||
"ma-instance</xmlns:xsi></addresses>";
|
"</tag4>"+
|
||||||
JSONObject expectedJsonObject = JSONML.toJSONObject(expectedStr);
|
"<tag6>false</tag6>"+
|
||||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
"<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]"+
|
||||||
|
"]"+
|
||||||
|
"]";
|
||||||
|
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
|
||||||
|
String xmlToStr = JSONML.toString(jsonArray);
|
||||||
|
JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr);
|
||||||
|
JSONArray expectedJsonArray= new JSONArray(expectedStr);
|
||||||
|
Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray);
|
||||||
|
Util.compareActualVsExpectedJsonArrays(finalJsonArray,expectedJsonArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
56
Util.java
56
Util.java
|
@ -20,15 +20,9 @@ public class Util {
|
||||||
assertTrue("jsonArray lengths should be equal",
|
assertTrue("jsonArray lengths should be equal",
|
||||||
jsonArray.length() == expectedJsonArray.length());
|
jsonArray.length() == expectedJsonArray.length());
|
||||||
for (int i = 0; i < jsonArray.length(); ++i) {
|
for (int i = 0; i < jsonArray.length(); ++i) {
|
||||||
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
Object value = jsonArray.get(i);
|
||||||
JSONObject expectedJsonObject = expectedJsonArray.getJSONObject(i);
|
Object expectedValue = expectedJsonArray.get(i);
|
||||||
assertTrue("jsonObjects should have the same length",
|
compareActualVsExpectedObjects(value, expectedValue);
|
||||||
jsonObject.length() == expectedJsonObject.length());
|
|
||||||
Iterator<String> keys = jsonObject.keys();
|
|
||||||
while (keys.hasNext()) {
|
|
||||||
String key = keys.next();
|
|
||||||
compareJsonObjectEntries(jsonObject, expectedJsonObject, key);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,42 +38,36 @@ public class Util {
|
||||||
Iterator<String> keys = jsonObject.keys();
|
Iterator<String> keys = jsonObject.keys();
|
||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
String key = keys.next();
|
String key = keys.next();
|
||||||
compareJsonObjectEntries(jsonObject, expectedJsonObject, key);
|
Object value = jsonObject.get(key);
|
||||||
|
Object expectedValue = expectedJsonObject.get(key);
|
||||||
|
compareActualVsExpectedObjects(value, expectedValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare two jsonObject entries
|
* Compare two objects for equality. Might be JSONArray, JSONObject,
|
||||||
* @param jsonObject created by the code to be tested
|
* or something else.
|
||||||
* @param expectedJsonObject created specifically for comparing
|
* @param value created by the code to be tested
|
||||||
|
* @param expectedValue created specifically for comparing
|
||||||
* @param key key to the jsonObject entry to be compared
|
* @param key key to the jsonObject entry to be compared
|
||||||
*/
|
*/
|
||||||
private static void compareJsonObjectEntries(JSONObject jsonObject,
|
private static void compareActualVsExpectedObjects(Object value,
|
||||||
JSONObject expectedJsonObject, String key) {
|
Object expectedValue) {
|
||||||
Object value = jsonObject.get(key);
|
if (value instanceof JSONObject && expectedValue instanceof JSONObject) {
|
||||||
Object expectedValue = expectedJsonObject.get(key);
|
JSONObject jsonObject = (JSONObject)value;
|
||||||
if (value instanceof JSONObject) {
|
JSONObject expectedJsonObject = (JSONObject)expectedValue;
|
||||||
JSONObject childJsonObject = jsonObject.getJSONObject(key);
|
|
||||||
JSONObject expectedChildJsonObject =
|
|
||||||
expectedJsonObject.getJSONObject(key);
|
|
||||||
compareActualVsExpectedJsonObjects(
|
compareActualVsExpectedJsonObjects(
|
||||||
childJsonObject, expectedChildJsonObject);
|
jsonObject, expectedJsonObject);
|
||||||
} else if (value instanceof JSONArray) {
|
} else if (value instanceof JSONArray && expectedValue instanceof JSONArray) {
|
||||||
JSONArray childJsonArray = jsonObject.getJSONArray(key);
|
JSONArray jsonArray = (JSONArray)value;
|
||||||
JSONArray expectedChildJsonArray =
|
JSONArray expectedJsonArray = (JSONArray)expectedValue;
|
||||||
expectedJsonObject.getJSONArray(key);
|
|
||||||
compareActualVsExpectedJsonArrays(
|
compareActualVsExpectedJsonArrays(
|
||||||
childJsonArray, expectedChildJsonArray);
|
jsonArray, expectedJsonArray);
|
||||||
} else if (!(value instanceof String) && !(expectedValue instanceof String)) {
|
} else {
|
||||||
assertTrue("string values should be equal for actual: "+
|
assertTrue("string values should be equal for actual: "+
|
||||||
value.toString()+" expected: "+expectedValue.toString(),
|
value.toString()+" expected: "+expectedValue.toString(),
|
||||||
value.toString().equals(expectedValue.toString()));
|
value.toString().equals(expectedValue.toString()));
|
||||||
} else {
|
|
||||||
String testStr = "key: "+key+" val: "+value.toString();
|
|
||||||
String actualStr = expectedValue.toString();
|
|
||||||
assertTrue("string values should be equal for actual: "+
|
|
||||||
testStr+" expected: "+actualStr,
|
|
||||||
value.equals(expectedValue.toString()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue