From 48c872f66f4889754da36b209f1e8ade894744ab Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 28 Dec 2015 11:12:41 -0600 Subject: [PATCH] Replace util compare method with JsonPath --- JSONStringerTest.java | 108 ++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 57 deletions(-) diff --git a/JSONStringerTest.java b/JSONStringerTest.java index 7a35064..4e9a9db 100644 --- a/JSONStringerTest.java +++ b/JSONStringerTest.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.*; + /** * Tests for JSON-Java JSONStringer. @@ -175,16 +179,6 @@ public class JSONStringerTest { */ @Test public void simpleObjectString() { - String expectedStr = - "{"+ - "\"trueValue\":true,"+ - "\"falseValue\":false,"+ - "\"nullValue\":null,"+ - "\"stringValue\":\"hello world!\","+ - "\"complexStringValue\":\"h\be\tllo w\u1234orld!\","+ - "\"intValue\":42,"+ - "\"doubleValue\":-23.45e67"+ - "}"; JSONStringer jsonStringer = new JSONStringer(); jsonStringer.object(); jsonStringer.key("trueValue").value(true); @@ -197,8 +191,16 @@ public class JSONStringerTest { jsonStringer.endObject(); String str = jsonStringer.toString(); JSONObject jsonObject = new JSONObject(str); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 7 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 7); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseValue"))); + assertTrue("expected null", null == JsonPath.read(doc, "$.nullValue")); + assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.stringValue"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, "$.complexStringValue"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.intValue"))); + assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$.doubleValue"))); } /** @@ -207,15 +209,6 @@ public class JSONStringerTest { */ @Test public void simpleArrayString() { - String expectedStr = - "["+ - "true,"+ - "false,"+ - "null,"+ - "\"hello world!\","+ - "42,"+ - "-23.45e67"+ - "]"; JSONStringer jsonStringer = new JSONStringer(); jsonStringer.array(); jsonStringer.value(true); @@ -227,8 +220,15 @@ public class JSONStringerTest { jsonStringer.endArray(); String str = jsonStringer.toString(); JSONArray jsonArray = new JSONArray(str); - JSONArray expectedJsonArray = new JSONArray(expectedStr); - Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 6 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected null", null == JsonPath.read(doc, "$[2]")); + assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$[3]"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[4]"))); + assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$[5]"))); } /** @@ -237,38 +237,6 @@ public class JSONStringerTest { */ @Test public void complexObjectString() { - String expectedStr = - "{"+ - "\"trueValue\":true,"+ - "\"falseValue\":false,"+ - "\"nullValue\":null,"+ - "\"stringValue\":\"hello world!\","+ - "\"object2\":{"+ - "\"k1\":\"v1\","+ - "\"k2\":\"v2\","+ - "\"k3\":\"v3\","+ - "\"array1\":["+ - "1,"+ - "2,"+ - "{"+ - "\"k4\":\"v4\","+ - "\"k5\":\"v5\","+ - "\"k6\":\"v6\","+ - "\"array2\":["+ - "5,"+ - "6,"+ - "7,"+ - "8"+ - "]"+ - "},"+ - "3,"+ - "4"+ - "]"+ - "},"+ - "\"complexStringValue\":\"h\be\tllo w\u1234orld!\","+ - "\"intValue\":42,"+ - "\"doubleValue\":-23.45e67"+ - "}"; JSONStringer jsonStringer = new JSONStringer(); jsonStringer.object(); jsonStringer.key("trueValue").value(true); @@ -303,8 +271,34 @@ public class JSONStringerTest { jsonStringer.endObject(); String str = jsonStringer.toString(); JSONObject jsonObject = new JSONObject(str); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 8 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 8); + assertTrue("expected 4 object2 items", ((Map)(JsonPath.read(doc, "$.object2"))).size() == 4); + assertTrue("expected 5 array1 items", ((List)(JsonPath.read(doc, "$.object2.array1"))).size() == 5); + assertTrue("expected 4 array[2] items", ((Map)(JsonPath.read(doc, "$.object2.array1[2]"))).size() == 4); + assertTrue("expected 4 array1[2].array2 items", ((List)(JsonPath.read(doc, "$.object2.array1[2].array2"))).size() == 4); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseValue"))); + assertTrue("expected null", null == JsonPath.read(doc, "$.nullValue")); + assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.stringValue"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.intValue"))); + assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$.doubleValue"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, "$.complexStringValue"))); + assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$.object2.k1"))); + assertTrue("expected v2", "v2".equals(JsonPath.read(doc, "$.object2.k2"))); + assertTrue("expected v3", "v3".equals(JsonPath.read(doc, "$.object2.k3"))); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.object2.array1[0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.object2.array1[1]"))); + assertTrue("expected v4", "v4".equals(JsonPath.read(doc, "$.object2.array1[2].k4"))); + assertTrue("expected v5", "v5".equals(JsonPath.read(doc, "$.object2.array1[2].k5"))); + assertTrue("expected v6", "v6".equals(JsonPath.read(doc, "$.object2.array1[2].k6"))); + assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$.object2.array1[2].array2[0]"))); + assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$.object2.array1[2].array2[1]"))); + assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$.object2.array1[2].array2[2]"))); + assertTrue("expected 8", Integer.valueOf(8).equals(JsonPath.read(doc, "$.object2.array1[2].array2[3]"))); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.object2.array1[3]"))); + assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$.object2.array1[4]"))); } }