package org.json.junit;
import static org.junit.Assert.*;
import org.json.*;
import org.junit.Test;
/**
* Tests for JSON-Java JSONML.java
*/
public class JSONMLTest {
@Test(expected=NullPointerException.class)
public void nullXMLException() {
String xmlStr = null;
JSONML.toJSONArray(xmlStr);
}
@Test(expected=JSONException.class)
public void emptyXMLException() {
String xmlStr = "";
JSONML.toJSONArray(xmlStr);
}
@Test(expected=JSONException.class)
public void nonXMLException() {
String xmlStr = "{ \"this is\": \"not xml\"}";
JSONML.toJSONArray(xmlStr);
}
@Test(expected=JSONException.class)
public void unvalidSlashInTagException() {
String xmlStr =
"\n"+
"\n"+
" \n"+
" \n"+
" abc street\n"+
" \n"+
"";
JSONML.toJSONArray(xmlStr);
}
@Test(expected=JSONException.class)
public void invalidBangInTagException() {
String xmlStr =
"\n"+
"\n"+
" \n"+
" \n"+
" \n"+
" \n"+
"";
JSONML.toJSONArray(xmlStr);
}
@Test(expected=JSONException.class)
public void invalidBangNoCloseInTagException() {
String xmlStr =
"\n"+
"\n"+
" \n"+
" \n"+
" \n"+
"";
JSONML.toJSONArray(xmlStr);
}
@Test(expected=JSONException.class)
public void noCloseStartTagException() {
String xmlStr =
"\n"+
"\n"+
" \n"+
" \n"+
" \n"+
"";
JSONML.toJSONArray(xmlStr);
}
@Test(expected=JSONException.class)
public void invalidCDATABangInTagException() {
String xmlStr =
"\n"+
"\n"+
" \n"+
" Joe Tester\n"+
" \n"+
" \n"+
"";
JSONML.toJSONArray(xmlStr);
}
@Test(expected=NullPointerException.class)
public void nullJSONXMLException() {
JSONArray jsonArray= null;
JSONML.toString(jsonArray);
}
@Test(expected=JSONException.class)
public void emptyJSONXMLException() {
JSONArray jsonArray = new JSONArray();
JSONML.toString(jsonArray);
}
@Test
public void complexTypeXML() {
String xmlStr =
"\n"+
"\n"+
"\n"+
"\n"+
">\n"+
"\n"+
"";
String expectedStr =
"[\"addresses\","+
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
"[\"address\","+
"[\"name\"],"+
"[\"nocontent\"],"+
"\">\""+
"]"+
"]";
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
JSONArray expectedJsonArray = new JSONArray(expectedStr);
Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray);
}
@Test
public void basicXMLAsObject() {
String xmlStr =
"\n"+
"\n"+
"\n"+
"Joe Tester\n"+
"[CDATA[Baker street 5]\n"+
"\n"+
"true\n"+
"false\n"+
"null\n"+
"42\n"+
"-23\n"+
"-23.45\n"+
"-23x.45\n"+
"\n"+
"1\n"+
"2\n"+
"3\n"+
"4.1\n"+
"5.2\n"+
"\n"+
"\n"+
"";
String expectedStr =
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
"\"childNodes\":[{"+
"\"childNodes\":["+
"{\"childNodes\":[\"Joe Tester\"],"+
"\"tagName\":\"name\"},"+
"{\"childNodes\":[\"[CDATA[Baker street 5]\"],"+
"\"tagName\":\"street\"},"+
"{\"tagName\":\"NothingHere\"},"+
"{\"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\":[1],"+
"\"tagName\":\"value\"},"+
"{\"childNodes\":[1],"+
"\"tagName\":\"value\"},"+
"{\"childNodes\":[1],"+
"\"tagName\":\"value\"},"+
"{\"childNodes\":[1],"+
"\"tagName\":\"value\"},"+
"],"
"\"tagName\":\"address\"}],"+
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+
"\"tagName\":\"addresses\"}";
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
public void basicXMLAsArray() {
String xmlStr =
"\n"+
"\n"+
"\n"+
"Joe Tester\n"+
"[CDATA[Baker street 5]\n"+
"\n"+
"true\n"+
"false\n"+
"null\n"+
"42\n"+
"-23\n"+
"-23.45\n"+
"-23x.45\n"+
"\n"+
"1\n"+
"2\n"+
"3\n"+
"4.1\n"+
"5.2\n"+
"\n"+
"\n"+
"";
String expectedStr =
"[\"addresses\","+
"{\"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);
}
@Test
public void commentsInXML() {
String xmlStr =
"\n"+
"\n"+
"\n"+
"\n"+
" comment ]]>\n"+
"Joe Tester\n"+
"\n"+
"Baker street 5\n"+
"\n"+
"";
String expectedStr =
"{\"childNodes\":["+
"{\"childNodes\":["+
"\" this is -- 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 =
"\n"+
"\n"+
" \n"+
" [CDATA[Joe & T > e < s " t ' er]]\n"+
" Baker street 5\n"+
" 1, 2, 3, 4.1, 5.2\n"+
" \n"+
"";
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 =
""+
""+
"5"+
"10"+
"15"+
""+
"val2"+
"val3"+
""+
"-6"+
"true"+
""+
"false"+
"null"+
""+
"10"+
"20"+
"33.33"+
"5220"+
""+
"";
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);
}
}