Skip to content

Commit

Permalink
Merge pull request #68 from stleary/jsonpointer-query
Browse files Browse the repository at this point in the history
unit tests for query-by-JSONPointer
  • Loading branch information
stleary authored Mar 28, 2017
2 parents 82ff14e + d1a5f15 commit f6ab6d7
Showing 1 changed file with 109 additions and 2 deletions.
111 changes: 109 additions & 2 deletions src/test/java/org/json/junit/JSONPointerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void tokenListIsCopiedInConstructor() {
}

/**
* Coverage for JSONObject queryFrom()
* Coverage for JSONObject query(String)
*/
@Test
public void queryFromJSONObject() {
Expand Down Expand Up @@ -187,7 +187,61 @@ public void queryFromJSONObject() {
}

/**
* Coverage for JSONArray queryFrom()
* Coverage for JSONObject query(JSONPointer)
*/
@Test
public void queryFromJSONObjectUsingPointer() {
String str = "{"+
"\"stringKey\":\"hello world!\","+
"\"arrayKey\":[0,1,2],"+
"\"objectKey\": {"+
"\"a\":\"aVal\","+
"\"b\":\"bVal\""+
"}"+
"}";
JSONObject jsonObject = new JSONObject(str);
Object obj = jsonObject.query(new JSONPointer("/stringKey"));
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
obj = jsonObject.query(new JSONPointer("/arrayKey/1"));
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
obj = jsonObject.query(new JSONPointer("/objectKey/b"));
assertTrue("Expected bVal", "bVal".equals(obj));
try {
obj = jsonObject.query(new JSONPointer("/a/b/c"));
assertTrue("Expected JSONPointerException", false);
} catch (JSONPointerException e) {
assertTrue("Expected bad key/value exception",
"value [null] is not an array or object therefore its key b cannot be resolved".
equals(e.getMessage()));
}
}

/**
* Coverage for JSONObject optQuery(JSONPointer)
*/
@Test
public void optQueryFromJSONObjectUsingPointer() {
String str = "{"+
"\"stringKey\":\"hello world!\","+
"\"arrayKey\":[0,1,2],"+
"\"objectKey\": {"+
"\"a\":\"aVal\","+
"\"b\":\"bVal\""+
"}"+
"}";
JSONObject jsonObject = new JSONObject(str);
Object obj = jsonObject.optQuery(new JSONPointer("/stringKey"));
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
obj = jsonObject.optQuery(new JSONPointer("/arrayKey/1"));
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
obj = jsonObject.optQuery(new JSONPointer("/objectKey/b"));
assertTrue("Expected bVal", "bVal".equals(obj));
obj = jsonObject.optQuery(new JSONPointer("/a/b/c"));
assertTrue("Expected null", obj == null);
}

/**
* Coverage for JSONArray query(String)
*/
@Test
public void queryFromJSONArray() {
Expand All @@ -214,4 +268,57 @@ public void queryFromJSONArray() {
"a is not an array index".equals(e.getMessage()));
}
}

/**
* Coverage for JSONArray query(JSONPointer)
*/
@Test
public void queryFromJSONArrayUsingPointer() {
String str = "["+
"\"hello world!\","+
"[0,1,2],"+
"{"+
"\"a\":\"aVal\","+
"\"b\":\"bVal\""+
"}"+
"]";
JSONArray jsonArray = new JSONArray(str);
Object obj = jsonArray.query(new JSONPointer("/0"));
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
obj = jsonArray.query(new JSONPointer("/1/1"));
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
obj = jsonArray.query(new JSONPointer("/2/b"));
assertTrue("Expected bVal", "bVal".equals(obj));
try {
obj = jsonArray.query(new JSONPointer("/a/b/c"));
assertTrue("Expected JSONPointerException", false);
} catch (JSONPointerException e) {
assertTrue("Expected bad index exception",
"a is not an array index".equals(e.getMessage()));
}
}

/**
* Coverage for JSONArray optQuery(JSONPointer)
*/
@Test
public void optQueryFromJSONArrayUsingPointer() {
String str = "["+
"\"hello world!\","+
"[0,1,2],"+
"{"+
"\"a\":\"aVal\","+
"\"b\":\"bVal\""+
"}"+
"]";
JSONArray jsonArray = new JSONArray(str);
Object obj = jsonArray.optQuery(new JSONPointer("/0"));
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
obj = jsonArray.optQuery(new JSONPointer("/1/1"));
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
obj = jsonArray.optQuery(new JSONPointer("/2/b"));
assertTrue("Expected bVal", "bVal".equals(obj));
obj = jsonArray.optQuery(new JSONPointer("/a/b/c"));
assertTrue("Expected null", obj == null);
}
}

0 comments on commit f6ab6d7

Please sign in to comment.