diff --git a/CookieListTest.java b/CookieListTest.java
index 1d7ad8a..5b298e7 100644
--- a/CookieListTest.java
+++ b/CookieListTest.java
@@ -2,9 +2,13 @@ package org.json.junit;
import static org.junit.Assert.*;
+import java.util.*;
+
import org.json.*;
import org.junit.Test;
+import com.jayway.jsonpath.*;
+
/**
* HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265
*
@@ -60,7 +64,6 @@ public class CookieListTest {
@Test
public void emptyStringCookieList() {
String cookieStr = "";
- String expectedCookieStr = "";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
assertTrue(jsonObject.length() == 0);
}
@@ -71,10 +74,11 @@ public class CookieListTest {
@Test
public void simpleCookieList() {
String cookieStr = "SID=31d4d96e407aad42";
- String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
- JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
- Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("Expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
+ assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
}
/**
@@ -83,10 +87,11 @@ public class CookieListTest {
@Test
public void simpleCookieListWithDelimiter() {
String cookieStr = "SID=31d4d96e407aad42;";
- String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
- JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
- Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("Expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
+ assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
}
/**
@@ -102,18 +107,16 @@ public class CookieListTest {
" name4=myCookieValue4; "+
"name5=myCookieValue5;"+
" name6=myCookieValue6;";
- String expectedCookieStr =
- "{"+
- "\"name1\":\"myCookieValue1\","+
- "\"name2\":\"myCookieValue2\","+
- "\"name3\":\"myCookieValue3\","+
- "\"name4\":\"myCookieValue4\","+
- "\"name5\":\"myCookieValue5\","+
- "\"name6\":\"myCookieValue6\""+
- "}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
- JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
- Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("Expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
+ assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
+ assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
+ assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
+ assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
+ assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
+ assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
}
/**
@@ -139,21 +142,16 @@ public class CookieListTest {
" name4=myCookieValue4; "+
"name5=myCookieValue5;"+
" name6=myCookieValue6;";
- String expectedCookieStr =
- "{"+
- "\"name1\":\"myCookieValue1\","+
- "\"name2\":\"myCookieValue2\","+
- "\"name3\":\"myCookieValue3\","+
- "\"name4\":\"myCookieValue4\","+
- "\"name5\":\"myCookieValue5\","+
- "\"name6\":\"myCookieValue6\""+
- "}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
- JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
- String cookieToStr = CookieList.toString(jsonObject);
- JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr);
- Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
- Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("Expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
+ assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
+ assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
+ assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
+ assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
+ assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
+ assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
}
/**
@@ -169,22 +167,15 @@ public class CookieListTest {
" name4=my%25CookieValue4; "+
"name5=myCookieValue5;"+
" name6=myCookieValue6;";
- String expectedCookieStr =
- "{"+
- "\"name1\":\"myCookieValue1\","+
- "\"name2\":\"my Cookie Value 2\","+
- "\"name3\":\"my+Cookie&Value;3=\","+
- "\"name4\":\"my%CookieValue4\","+
- "\"name5\":\"myCookieValue5\","+
- "\"name6\":\"myCookieValue6\""+
- "}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
- JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
- String cookieToStr = CookieList.toString(jsonObject);
- JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr);
- Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
- Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("Expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
+ assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
+ assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(JsonPath.read(doc, "$.name2")));
+ assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3")));
+ assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(JsonPath.read(doc, "$.name4")));
+ assertTrue("expected my%CookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
+ assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
}
-
-
}
diff --git a/EnumTest.java b/EnumTest.java
index d24824b..a591dba 100644
--- a/EnumTest.java
+++ b/EnumTest.java
@@ -2,9 +2,13 @@ package org.json.junit;
import static org.junit.Assert.*;
+import java.util.*;
+
import org.json.*;
import org.junit.*;
+import com.jayway.jsonpath.*;
+
/**
* Enums are not explicitly supported in JSON-Java. But because enums act like
* classes, all required behavior is already be present in some form.
@@ -25,23 +29,32 @@ public class EnumTest {
assertTrue("simple enum has no getters", jsonObject.length() == 0);
// enum with a getters should create a non-empty object
- String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}";
MyEnumField myEnumField = MyEnumField.VAL2;
jsonObject = new JSONObject(myEnumField);
- JSONObject expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
+
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider()
+ .parse(jsonObject.toString());
+ assertTrue("expecting 2 items in top level object", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expecting val 2", "val 2".equals(JsonPath.read(doc, "$.value")));
+ assertTrue("expecting 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal")));
/**
* class which contains enum instances. Each enum should be stored
* in its own JSONObject
*/
- expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}";
MyEnumClass myEnumClass = new MyEnumClass();
myEnumClass.setMyEnum(MyEnum.VAL1);
myEnumClass.setMyEnumField(MyEnumField.VAL3);
jsonObject = new JSONObject(myEnumClass);
- expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expecting 2 items in top level object", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expecting 2 items in myEnumField object", ((Map,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
+ assertTrue("expecting 0 items in myEnum object", ((Map,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
+ assertTrue("expecting 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal")));
+ assertTrue("expecting val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value")));
}
/**
@@ -51,28 +64,30 @@ public class EnumTest {
@Test
public void jsonObjectFromEnumWithNames() {
String [] names;
- String expectedStr;
JSONObject jsonObject;
- JSONObject finalJsonObject;
- JSONObject expectedJsonObject;
- expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}";
MyEnum myEnum = MyEnum.VAL1;
names = JSONObject.getNames(myEnum);
- // The values will be MyEnmField fields, so need to convert back to string for comparison
+ // The values will be MyEnum fields
jsonObject = new JSONObject(myEnum, names);
- finalJsonObject = new JSONObject(jsonObject.toString());
- expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
- expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}";
+ // validate JSON object
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
+ assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
+ assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
+
MyEnumField myEnumField = MyEnumField.VAL3;
names = JSONObject.getNames(myEnumField);
- // The values will be MyEnmField fields, so need to convert back to string for comparison
+ // The values will be MyEnmField fields
jsonObject = new JSONObject(myEnumField, names);
- finalJsonObject = new JSONObject(jsonObject.toString());
- expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
+ assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
+ assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
+
}
/**
@@ -81,29 +96,33 @@ public class EnumTest {
*/
@Test
public void enumPut() {
- String expectedFinalStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL1\"}";
JSONObject jsonObject = new JSONObject();
MyEnum myEnum = MyEnum.VAL2;
jsonObject.put("myEnum", myEnum);
- assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonObject.get("myEnum")));
- assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonObject.opt("myEnum")));
MyEnumField myEnumField = MyEnumField.VAL1;
jsonObject.putOnce("myEnumField", myEnumField);
- assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonObject.get("myEnumField")));
- assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonObject.opt("myEnumField")));
- JSONObject finalJsonObject = new JSONObject(jsonObject.toString());
- JSONObject expectedFinalJsonObject = new JSONObject(expectedFinalStr);
- Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedFinalJsonObject);
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level objects", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnum")));
+ assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.myEnumField")));
+
JSONArray jsonArray = new JSONArray();
jsonArray.put(myEnum);
jsonArray.put(1, myEnumField);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 2 top level objects", ((List>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[0]")));
+ assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$[1]")));
+
+ /**
+ * Leaving these tests because they exercise get, opt, and remove
+ */
assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonArray.get(0)));
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.opt(1)));
- JSONArray expectedJsonArray = new JSONArray();
- expectedJsonArray.put(MyEnum.VAL2);
- expectedJsonArray.put(MyEnumField.VAL1);
- Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1)));
}
@@ -151,49 +170,66 @@ public class EnumTest {
MyEnumField myEnumField = MyEnumField.VAL2;
jsonObject = new JSONObject(myEnumField);
- expectedStr = "{\"value\":\"val 2\", \"intVal\":2}";
- JSONObject actualJsonObject = new JSONObject(jsonObject.toString());
- JSONObject expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject);
- expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}";
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected val 2", "val 2".equals(JsonPath.read(doc, "$.value")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal")));
+
MyEnumClass myEnumClass = new MyEnumClass();
myEnumClass.setMyEnum(MyEnum.VAL1);
myEnumClass.setMyEnumField(MyEnumField.VAL3);
jsonObject = new JSONObject(myEnumClass);
- actualJsonObject = new JSONObject(jsonObject.toString());
- expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject);
- expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}";
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected 2 myEnumField items", ((Map,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
+ assertTrue("expected 0 myEnum items", ((Map,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
+ assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal")));
+ assertTrue("expected val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value")));
+
String [] names = JSONObject.getNames(myEnum);
jsonObject = new JSONObject(myEnum, names);
- actualJsonObject = new JSONObject(jsonObject.toString());
- expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject);
- expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}";
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
+ assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
+ assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
+
names = JSONObject.getNames(myEnumField);
jsonObject = new JSONObject(myEnumField, names);
- actualJsonObject = new JSONObject(jsonObject.toString());
- expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
+ assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
+ assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}";
jsonObject = new JSONObject();
jsonObject.putOpt("myEnum", myEnum);
jsonObject.putOnce("myEnumField", myEnumField);
- actualJsonObject = new JSONObject(jsonObject.toString());
- expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject);
- expectedStr = "[\"VAL2\", \"VAL2\"]";
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnum")));
+ assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnumField")));
+
JSONArray jsonArray = new JSONArray();
jsonArray.put(myEnum);
jsonArray.put(1, myEnumField);
- JSONArray actualJsonArray = new JSONArray(jsonArray.toString());
- JSONArray expectedJsonArray = new JSONArray(expectedStr);
- Util.compareActualVsExpectedJsonArrays(actualJsonArray, expectedJsonArray);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 2 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[0]")));
+ assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[1]")));
}
/**
@@ -206,19 +242,28 @@ public class EnumTest {
JSONObject jsonObject = (JSONObject)JSONObject.wrap(myEnum);
assertTrue("simple enum has no getters", jsonObject.length() == 0);
- String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}";
MyEnumField myEnumField = MyEnumField.VAL2;
jsonObject = (JSONObject)JSONObject.wrap(myEnumField);
- JSONObject expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
- expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}";
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected val 2", "val 2".equals(JsonPath.read(doc, "$.value")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal")));
+
MyEnumClass myEnumClass = new MyEnumClass();
myEnumClass.setMyEnum(MyEnum.VAL1);
myEnumClass.setMyEnumField(MyEnumField.VAL3);
jsonObject = (JSONObject)JSONObject.wrap(myEnumClass);
- expectedJsonObject = new JSONObject(expectedStr);
- Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected 2 myEnumField items", ((Map,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
+ assertTrue("expected 0 myEnum items", ((Map,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
+ assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal")));
+ assertTrue("expected val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value")));
+
}
/**
diff --git a/JSONArrayTest.java b/JSONArrayTest.java
index 8327e18..455d680 100644
--- a/JSONArrayTest.java
+++ b/JSONArrayTest.java
@@ -2,18 +2,15 @@ package org.json.junit;
import static org.junit.Assert.assertTrue;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+import java.util.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
+import com.jayway.jsonpath.*;
+
/**
* Tests for JSON-Java JSONArray.java
@@ -306,41 +303,37 @@ public class JSONArrayTest {
/**
* Exercise JSONArray.join() by converting a JSONArray into a
* comma-separated string. Since this is very nearly a JSON document,
- * array braces are added to the beginning and end, and it is reconverted
- * back to a JSONArray for comparison.
+ * array braces are added to the beginning and end prior to validation.
*/
@Test
public void join() {
- String expectedStr =
- "["+
- "true,"+
- "false,"+
- "\"true\","+
- "\"false\","+
- "\"hello\","+
- "0.002345,"+
- "\"23.45\","+
- "42,"+
- "\"43\","+
- "["+
- "\"world\""+
- "],"+
- "{"+
- "\"key1\":\"value1\","+
- "\"key2\":\"value2\","+
- "\"key3\":\"value3\","+
- "\"key4\":\"value4\""+
- "},"+
- "0,"+
- "\"-1\""+
- "]";
-
JSONArray jsonArray = new JSONArray(arrayStr);
String joinStr = jsonArray.join(",");
- JSONArray finalJsonArray = new JSONArray("["+joinStr+"]");
- JSONArray expectedJsonArray = new JSONArray(expectedStr);
- Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
- Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
+
+ // validate JSON
+ /**
+ * Don't need to remake the JSONArray to perform the parsing
+ */
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse("["+joinStr+"]");
+ assertTrue("expected 13 items in top level object", ((List>)(JsonPath.read(doc, "$"))).size() == 13);
+ assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
+ assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
+ assertTrue("expected \"true\"", "true".equals(JsonPath.read(doc, "$[2]")));
+ assertTrue("expected \"false\"", "false".equals(JsonPath.read(doc, "$[3]")));
+ assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[4]")));
+ assertTrue("expected 0.002345", Double.valueOf(0.002345).equals(JsonPath.read(doc, "$[5]")));
+ assertTrue("expected \"23.45\"", "23.45".equals(JsonPath.read(doc, "$[6]")));
+ assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[7]")));
+ assertTrue("expected \"43\"", "43".equals(JsonPath.read(doc, "$[8]")));
+ assertTrue("expected 1 item in [9]", ((List>)(JsonPath.read(doc, "$[9]"))).size() == 1);
+ assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[9][0]")));
+ assertTrue("expected 4 items in [10]", ((Map,?>)(JsonPath.read(doc, "$[10]"))).size() == 4);
+ assertTrue("expected value1", "value1".equals(JsonPath.read(doc, "$[10].key1")));
+ assertTrue("expected value2", "value2".equals(JsonPath.read(doc, "$[10].key2")));
+ assertTrue("expected value3", "value3".equals(JsonPath.read(doc, "$[10].key3")));
+ assertTrue("expected value4", "value4".equals(JsonPath.read(doc, "$[10].key4")));
+ assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$[11]")));
+ assertTrue("expected \"-1\"", "-1".equals(JsonPath.read(doc, "$[12]")));
}
/**
@@ -419,33 +412,7 @@ public class JSONArrayTest {
*/
@Test
public void put() {
- String expectedStr =
- "["+
- "true,"+
- "false,"+
- "["+
- "hello,"+
- "world"+
- "],"+
- "2.5,"+
- "1,"+
- "45,"+
- "\"objectPut\","+
- "{"+
- "\"key10\":\"val10\","+
- "\"key20\":\"val20\","+
- "\"key30\":\"val30\""+
- "},"+
- "{"+
- "\"k1\":\"v1\""+
- "},"+
- "["+
- "1,"+
- "2"+
- "]"+
- "]";
JSONArray jsonArray = new JSONArray();
- JSONArray expectedJsonArray = new JSONArray(expectedStr);
// index 0
jsonArray.put(true);
@@ -488,9 +455,30 @@ public class JSONArrayTest {
Collection