Skip to content

Commit 51bcbeb

Browse files
committed
Merge pull request #48 from erosb/master
unittests for stleary/JSON-java#233
2 parents 8ed0362 + a1893eb commit 51bcbeb

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/test/org/json/junit/JSONArrayTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package org.json.junit;
22

3+
import static org.junit.Assert.assertNull;
34
import static org.junit.Assert.assertTrue;
45

5-
import java.util.*;
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
import java.util.Collections;
9+
import java.util.HashMap;
10+
import java.util.Iterator;
11+
import java.util.List;
12+
import java.util.Map;
613

714
import org.json.JSONArray;
815
import org.json.JSONException;
916
import org.json.JSONObject;
17+
import org.json.JSONPointerException;
1018
import org.junit.Test;
1119

12-
import com.jayway.jsonpath.*;
20+
import com.jayway.jsonpath.Configuration;
21+
import com.jayway.jsonpath.JsonPath;
1322

1423

1524
/**
@@ -686,4 +695,19 @@ public void iterator() {
686695
new Long(-1).equals(Long.parseLong((String) it.next())));
687696
assertTrue("should be at end of array", !it.hasNext());
688697
}
698+
699+
@Test(expected = JSONPointerException.class)
700+
public void queryWithNoResult() {
701+
new JSONArray().query("/a/b");
702+
}
703+
704+
@Test
705+
public void optQueryWithNoResult() {
706+
assertNull(new JSONArray().optQuery("/a/b"));
707+
}
708+
709+
@Test(expected = IllegalArgumentException.class)
710+
public void optQueryWithSyntaxError() {
711+
new JSONArray().optQuery("invalid");
712+
}
689713
}

src/test/org/json/junit/JSONObjectTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.json.junit;
22

33
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertNull;
45
import static org.junit.Assert.assertTrue;
56
import static org.mockito.Mockito.mock;
67
import static org.mockito.Mockito.when;
@@ -10,16 +11,25 @@
1011
import java.io.Writer;
1112
import java.math.BigDecimal;
1213
import java.math.BigInteger;
13-
import java.util.*;
14+
import java.util.ArrayList;
15+
import java.util.Arrays;
16+
import java.util.Collection;
17+
import java.util.Collections;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Locale;
21+
import java.util.Map;
1422

1523
import org.json.CDL;
1624
import org.json.JSONArray;
1725
import org.json.JSONException;
1826
import org.json.JSONObject;
27+
import org.json.JSONPointerException;
1928
import org.json.XML;
2029
import org.junit.Test;
2130

22-
import com.jayway.jsonpath.*;
31+
import com.jayway.jsonpath.Configuration;
32+
import com.jayway.jsonpath.JsonPath;
2333

2434
/**
2535
* JSONObject, along with JSONArray, are the central classes of the reference app.
@@ -1899,4 +1909,19 @@ public void jsonObjectNullOperations() {
18991909
String sNull = XML.toString(jsonObjectNull);
19001910
assertTrue("null should emit an empty string", "".equals(sNull));
19011911
}
1912+
1913+
@Test(expected = JSONPointerException.class)
1914+
public void queryWithNoResult() {
1915+
new JSONObject().query("/a/b");
1916+
}
1917+
1918+
@Test
1919+
public void optQueryWithNoResult() {
1920+
assertNull(new JSONObject().optQuery("/a/b"));
1921+
}
1922+
1923+
@Test(expected = IllegalArgumentException.class)
1924+
public void optQueryWithSyntaxError() {
1925+
new JSONObject().optQuery("invalid");
1926+
}
19021927
}

0 commit comments

Comments
 (0)