mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
XMLTest, in progress
This commit is contained in:
parent
8b9c3cbf47
commit
4fbe651e57
4 changed files with 160 additions and 83 deletions
40
CDLTest.java
40
CDLTest.java
|
@ -1,8 +1,5 @@
|
|||
package org.json.junit;
|
||||
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -127,7 +124,7 @@ public class CDLTest {
|
|||
JSONArray jsonArray = CDL.toJSONArray(lines);
|
||||
// This array is built from JSON parsing
|
||||
JSONArray expectedJsonArray = new JSONArray(expectedLines);
|
||||
compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -137,7 +134,7 @@ public class CDLTest {
|
|||
JSONArray nameJSONArray = new JSONArray(nameArrayStr);
|
||||
JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values);
|
||||
JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]");
|
||||
compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -151,38 +148,7 @@ public class CDLTest {
|
|||
String jsonStr = CDL.toString(jsonArray);
|
||||
JSONArray finalJsonArray = CDL.toJSONArray(jsonStr);
|
||||
JSONArray expectedJsonArray = new JSONArray(expectedLines);
|
||||
compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////// UTILITY METHODS /////////////////////////
|
||||
|
||||
/**
|
||||
* Compares two json arrays for equality
|
||||
* @param jsonArray created by the code to be tested
|
||||
* @param expectedJsonArray created specifically for compar
|
||||
*/
|
||||
private void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
|
||||
JSONArray expectedJsonArray) {
|
||||
assertTrue("jsonArray lengths should be equal",
|
||||
jsonArray.length() == expectedJsonArray.length());
|
||||
for (int i = 0; i < jsonArray.length(); ++i) {
|
||||
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
||||
JSONObject expectedJsonObject = expectedJsonArray.getJSONObject(i);
|
||||
assertTrue("jsonObjects should have the same length",
|
||||
jsonObject.length() == expectedJsonObject.length());
|
||||
Iterator<String> keys = jsonObject.keys();
|
||||
while (keys.hasNext()) {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -6,7 +6,8 @@ import org.junit.runners.Suite;
|
|||
@Suite.SuiteClasses({
|
||||
CDLTest.class,
|
||||
CookieTest.class,
|
||||
PropertyTest.class
|
||||
PropertyTest.class,
|
||||
XMLTest.class
|
||||
})
|
||||
public class JunitTestSuite {
|
||||
}
|
105
Util.java
105
Util.java
|
@ -2,55 +2,72 @@ package org.json.junit;
|
|||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.json.*;
|
||||
|
||||
public class Util {
|
||||
|
||||
/**
|
||||
* Utility method to check for a target string, then remove it from
|
||||
* the string to be searched.
|
||||
* @param jsonStr the string to be searched
|
||||
* @param expectedStr the target string to search for
|
||||
* @param assertStr the error message for the assert
|
||||
* @return new string with target substring removed
|
||||
*/
|
||||
public static String checkAndRemoveString(
|
||||
String jsonStr, String expectedStr, String assertStr) {
|
||||
int idx = jsonStr.indexOf(expectedStr);
|
||||
assertTrue(assertStr, idx != -1);
|
||||
String newStr = jsonStr.substring(0, idx)+
|
||||
jsonStr.substring(idx+expectedStr.length());
|
||||
return newStr;
|
||||
}
|
||||
|
||||
/////////////////////////// UTILITY METHODS /////////////////////////
|
||||
|
||||
/**
|
||||
* Utility method to strip out selected punctuation chars and confirm
|
||||
* that jsonStr is now empty
|
||||
* @param jsonStr the string to be verified
|
||||
* @param regexStr regex string of the chars to remove
|
||||
* @param assertStr the error message for the assert
|
||||
* Compares two json arrays for equality
|
||||
* @param jsonArray created by the code to be tested
|
||||
* @param expectedJsonArray created specifically for compar
|
||||
*/
|
||||
public static void verifyEmptyJsonStr(String jsonStr, String regexStr,
|
||||
String assertStr) {
|
||||
jsonStr = jsonStr.replaceAll(regexStr, "");
|
||||
assertTrue(assertStr, jsonStr.length() == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to check for a set of target strings,
|
||||
* then remove them from the string to be searched.
|
||||
* When completed, punctuation marks are stripped out and
|
||||
* the string to be searched is confirmed as empty
|
||||
* @param jsonStr the string to be searched
|
||||
* @param expectedStrArray the target strings to search for
|
||||
* @param regexStr regex string of the chars to remove
|
||||
* @param methodStr the method name
|
||||
*/
|
||||
public static void checkAndRemoveStrings(String jsonStr,
|
||||
String[] expectedStr, String regexStr, String methodStr) {
|
||||
for (int i = 0; i < expectedStr.length; ++i) {
|
||||
jsonStr = Util.checkAndRemoveString(jsonStr, expectedStr[i],
|
||||
methodStr+expectedStr+" should be included in string output");
|
||||
public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
|
||||
JSONArray expectedJsonArray) {
|
||||
assertTrue("jsonArray lengths should be equal",
|
||||
jsonArray.length() == expectedJsonArray.length());
|
||||
for (int i = 0; i < jsonArray.length(); ++i) {
|
||||
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
||||
JSONObject expectedJsonObject = expectedJsonArray.getJSONObject(i);
|
||||
assertTrue("jsonObjects should have the same length",
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void compareActualVsExpectedJsonObjects(
|
||||
JSONObject jsonObject, JSONObject expectedJsonObject) {
|
||||
assertTrue("jsonObjects should have the same length",
|
||||
jsonObject.length() == expectedJsonObject.length());
|
||||
Iterator<String> keys = jsonObject.keys();
|
||||
while (keys.hasNext()) {
|
||||
String key = keys.next();
|
||||
Object value = jsonObject.get(key);
|
||||
Object expectedValue = expectedJsonObject.get(key);
|
||||
if (value instanceof JSONObject) {
|
||||
JSONObject childJsonObject = jsonObject.getJSONObject(key);
|
||||
JSONObject expectedChildJsonObject =
|
||||
expectedJsonObject.getJSONObject(key);
|
||||
compareActualVsExpectedJsonObjects(
|
||||
childJsonObject, expectedChildJsonObject);
|
||||
} else if (value instanceof JSONArray) {
|
||||
JSONArray childJsonArray = jsonObject.getJSONArray(key);
|
||||
JSONArray expectedChildJsonArray =
|
||||
expectedJsonObject.getJSONArray(key);
|
||||
compareActualVsExpectedJsonArrays(
|
||||
childJsonArray, expectedChildJsonArray);
|
||||
} 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()));
|
||||
}
|
||||
}
|
||||
Util.verifyEmptyJsonStr(jsonStr, regexStr,
|
||||
methodStr+" jsonStr should be empty");
|
||||
}
|
||||
}
|
||||
|
|
93
XMLTest.java
93
XMLTest.java
|
@ -0,0 +1,93 @@
|
|||
package org.json.junit;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for JSON-Java XML.java
|
||||
*/
|
||||
public class XMLTest {
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void shouldHandleNullXML() {
|
||||
|
||||
String xmlStr = null;
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleEmptyXML() {
|
||||
|
||||
String xmlStr = "";
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
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 shouldHandleEmptyJSONXML() {
|
||||
JSONObject jsonObject= new JSONObject();
|
||||
String xmlStr = XML.toString(jsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleSimpleXML() {
|
||||
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>Baker street 5</street>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
|
||||
String expectedStr =
|
||||
"{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+
|
||||
"\"name\":\"Joe Tester\"},\"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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleToString() {
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addr&esses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
" <address>\n"+
|
||||
" <name>[CDATA[Joe Tester]]</name>\n"+
|
||||
" <street>Baker street 5</street>\n"+
|
||||
" </address>\n"+
|
||||
"</addr&esses>";
|
||||
|
||||
String expectedStr =
|
||||
"{\"addr&esses\":{\"address\":{\"street\":\"Baker street 5\","+
|
||||
"\"name\":\"[CDATA[Joe Tester]]\"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
||||
"XMLSchema-instance\"}}";
|
||||
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
String xmlToStr = XML.toString(jsonObject);
|
||||
JSONObject finalJsonObject = XML.toJSONObject(xmlToStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue