mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Replace util compare method with JsonPath
This commit is contained in:
parent
c6204a9f01
commit
a5390a0685
1 changed files with 105 additions and 100 deletions
|
@ -2,18 +2,15 @@ package org.json.junit;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.jayway.jsonpath.*;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for JSON-Java JSONArray.java
|
* Tests for JSON-Java JSONArray.java
|
||||||
|
@ -306,41 +303,41 @@ public class JSONArrayTest {
|
||||||
/**
|
/**
|
||||||
* Exercise JSONArray.join() by converting a JSONArray into a
|
* Exercise JSONArray.join() by converting a JSONArray into a
|
||||||
* comma-separated string. Since this is very nearly a JSON document,
|
* comma-separated string. Since this is very nearly a JSON document,
|
||||||
* array braces are added to the beginning and end, and it is reconverted
|
* array braces are added to the beginning and end prior to validation.
|
||||||
* back to a JSONArray for comparison.
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void join() {
|
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);
|
JSONArray jsonArray = new JSONArray(arrayStr);
|
||||||
String joinStr = jsonArray.join(",");
|
String joinStr = jsonArray.join(",");
|
||||||
JSONArray finalJsonArray = new JSONArray("["+joinStr+"]");
|
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
// validate JSON
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
/**
|
||||||
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
|
* Don't need to remake the JSONArray to perform the parsing
|
||||||
|
*/
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider()
|
||||||
|
.parse("["+joinStr+"]");
|
||||||
|
List<?> docList = JsonPath.read(doc, "$");
|
||||||
|
assertTrue("expected 13 items in top level object", docList.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]")));
|
||||||
|
docList = JsonPath.read(doc, "$[9]");
|
||||||
|
assertTrue("expected 1 array item", docList.size() == 1);
|
||||||
|
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[9][0]")));
|
||||||
|
Map<?,?> docMap = JsonPath.read(doc, "$[10]");
|
||||||
|
assertTrue("expected 4 object items", docMap.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 +416,7 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void put() {
|
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 jsonArray = new JSONArray();
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
|
||||||
|
|
||||||
// index 0
|
// index 0
|
||||||
jsonArray.put(true);
|
jsonArray.put(true);
|
||||||
|
@ -488,9 +459,36 @@ public class JSONArrayTest {
|
||||||
Collection<Object> collection = new ArrayList<Object>();
|
Collection<Object> collection = new ArrayList<Object>();
|
||||||
collection.add(1);
|
collection.add(1);
|
||||||
collection.add(2);
|
collection.add(2);
|
||||||
// 9
|
// 9
|
||||||
jsonArray.put(collection);
|
jsonArray.put(collection);
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
|
||||||
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider()
|
||||||
|
.parse(jsonArray.toString());
|
||||||
|
List<?> docList = JsonPath.read(doc, "$");
|
||||||
|
assertTrue("expected 10 items in top level object", docList.size() == 10);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
docList = JsonPath.read(doc, "$[2]");
|
||||||
|
assertTrue("expected 2 items in array", docList.size() == 2);
|
||||||
|
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]")));
|
||||||
|
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]")));
|
||||||
|
assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]")));
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]")));
|
||||||
|
assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]")));
|
||||||
|
assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]")));
|
||||||
|
Map<?,?> docMap = JsonPath.read(doc, "$[7]");
|
||||||
|
assertTrue("expected 3 items in object", docMap.size() == 3);
|
||||||
|
assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[7].key10")));
|
||||||
|
assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[7].key20")));
|
||||||
|
assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[7].key30")));
|
||||||
|
docMap = JsonPath.read(doc, "$[8]");
|
||||||
|
assertTrue("expected 1 item in object", docMap.size() == 1);
|
||||||
|
assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[8].k1")));
|
||||||
|
docList = JsonPath.read(doc, "$[9]");
|
||||||
|
assertTrue("expected 2 items in array", docList.size() == 2);
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -499,34 +497,7 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void putIndex() {
|
public void putIndex() {
|
||||||
String expectedStr =
|
|
||||||
"["+
|
|
||||||
"true,"+
|
|
||||||
"false,"+
|
|
||||||
"["+
|
|
||||||
"hello,"+
|
|
||||||
"world"+
|
|
||||||
"],"+
|
|
||||||
"2.5,"+
|
|
||||||
"1,"+
|
|
||||||
"45,"+
|
|
||||||
"\"objectPut\","+
|
|
||||||
"null,"+
|
|
||||||
"{"+
|
|
||||||
"\"key10\":\"val10\","+
|
|
||||||
"\"key20\":\"val20\","+
|
|
||||||
"\"key30\":\"val30\""+
|
|
||||||
"},"+
|
|
||||||
"["+
|
|
||||||
"1,"+
|
|
||||||
"2"+
|
|
||||||
"],"+
|
|
||||||
"{"+
|
|
||||||
"\"k1\":\"v1\""+
|
|
||||||
"},"+
|
|
||||||
"]";
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
|
||||||
|
|
||||||
// 1
|
// 1
|
||||||
jsonArray.put(1, false);
|
jsonArray.put(1, false);
|
||||||
|
@ -573,7 +544,35 @@ public class JSONArrayTest {
|
||||||
jsonArray.put(-1, "abc");
|
jsonArray.put(-1, "abc");
|
||||||
assertTrue("put index < 0 should have thrown exception", false);
|
assertTrue("put index < 0 should have thrown exception", false);
|
||||||
} catch(Exception ignored) {}
|
} catch(Exception ignored) {}
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
|
||||||
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider()
|
||||||
|
.parse(jsonArray.toString());
|
||||||
|
List<?> docList = JsonPath.read(doc, "$");
|
||||||
|
assertTrue("expected 11 items in top level object", docList.size() == 11);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
docList = JsonPath.read(doc, "$[2]");
|
||||||
|
assertTrue("expected 2 items in array", docList.size() == 2);
|
||||||
|
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]")));
|
||||||
|
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]")));
|
||||||
|
assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]")));
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]")));
|
||||||
|
assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]")));
|
||||||
|
assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]")));
|
||||||
|
assertTrue("expected null", null == JsonPath.read(doc, "$[7]"));
|
||||||
|
Map<?,?> docMap = JsonPath.read(doc, "$[8]");
|
||||||
|
assertTrue("expected 3 items in object", docMap.size() == 3);
|
||||||
|
assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[8].key10")));
|
||||||
|
assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[8].key20")));
|
||||||
|
assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[8].key30")));
|
||||||
|
docList = JsonPath.read(doc, "$[9]");
|
||||||
|
assertTrue("expected 2 items in array", docList.size() == 2);
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]")));
|
||||||
|
docMap = JsonPath.read(doc, "$[10]");
|
||||||
|
assertTrue("expected 1 item in object", docMap.size() == 1);
|
||||||
|
assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[10].k1")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -587,10 +586,9 @@ public class JSONArrayTest {
|
||||||
"1"+
|
"1"+
|
||||||
"]";
|
"]";
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(arrayStr);
|
||||||
JSONArray expectedJsonArray = new JSONArray();
|
|
||||||
jsonArray.remove(0);
|
jsonArray.remove(0);
|
||||||
assertTrue("array should be empty", null == jsonArray.remove(5));
|
assertTrue("array should be empty", null == jsonArray.remove(5));
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
assertTrue("jsonArray should be empty", jsonArray.length() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -648,15 +646,22 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void objectArrayVsIsArray() {
|
public void objectArrayVsIsArray() {
|
||||||
String expectedStr =
|
|
||||||
"["+
|
|
||||||
"1,2,3,4,5,6,7"+
|
|
||||||
"]";
|
|
||||||
int[] myInts = { 1, 2, 3, 4, 5, 6, 7 };
|
int[] myInts = { 1, 2, 3, 4, 5, 6, 7 };
|
||||||
Object myObject = myInts;
|
Object myObject = myInts;
|
||||||
JSONArray jsonArray = new JSONArray(myObject);
|
JSONArray jsonArray = new JSONArray(myObject);
|
||||||
JSONArray expectedJsonArray = new JSONArray(expectedStr);
|
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
// validate JSON
|
||||||
|
Object doc = Configuration.defaultConfiguration().jsonProvider()
|
||||||
|
.parse(jsonArray.toString());
|
||||||
|
List<?> docList = JsonPath.read(doc, "$");
|
||||||
|
assertTrue("expected 7 items in top level object", docList.size() == 7);
|
||||||
|
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]")));
|
||||||
|
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]")));
|
||||||
|
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]")));
|
||||||
|
assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$[3]")));
|
||||||
|
assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$[4]")));
|
||||||
|
assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$[5]")));
|
||||||
|
assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$[6]")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue