Skip to content

Commit 9c47ba2

Browse files
committed
initial test for JSONPointer class
1 parent 8f16e06 commit 9c47ba2

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.json.junit;
2+
3+
import static org.junit.Assert.assertSame;
4+
5+
import org.json.JSONObject;
6+
import org.json.JSONPointer;
7+
import org.json.JSONPointerException;
8+
import org.junit.Test;
9+
10+
public class JSONPointerTest {
11+
12+
private static final JSONObject document = new JSONObject("{"
13+
+ "\"foo\": [\"bar\", \"baz\"], "
14+
+ "\"\": 0,"
15+
+ "\"a/b\": 1,"
16+
+ "\"c%d\": 2,"
17+
+ "\"e^f\": 3,"
18+
+ "\"g|h\": 4," + "\"i\\\\j\": 5,"
19+
+ "\"k\\\"l\": 6,"
20+
+ "\" \": 7,"
21+
+ "\"m~n\": 8"
22+
+ "}");
23+
24+
private Object query(String pointer) {
25+
return new JSONPointer(pointer).queryFrom(document);
26+
}
27+
28+
@Test
29+
public void emptyPointer() {
30+
assertSame(document, query(""));
31+
}
32+
33+
@Test(expected = NullPointerException.class)
34+
public void nullPointer() {
35+
new JSONPointer(null);
36+
}
37+
38+
@Test
39+
public void objectPropertyQuery() {
40+
assertSame(document.get("foo"), query("/foo"));
41+
}
42+
43+
@Test
44+
public void arrayIndexQuery() {
45+
assertSame(document.getJSONArray("foo").get(0), query("/foo/0"));
46+
}
47+
48+
@Test(expected = JSONPointerException.class)
49+
public void stringPropOfArrayFailure() {
50+
query("/foo/bar");
51+
}
52+
53+
@Test
54+
public void queryByEmptyKey() {
55+
assertSame(document.get(""), query("/"));
56+
}
57+
58+
@Test
59+
public void slashEscaping() {
60+
assertSame(document.get("a/b"), query("/a~1b"));
61+
}
62+
63+
@Test
64+
public void tildeEscaping() {
65+
assertSame(document.get("m~n"), query("/m~0n"));
66+
}
67+
68+
@Test
69+
public void uriFragmentNotation() {
70+
assertSame(document.get("foo"), query("#/foo"));
71+
}
72+
73+
@Test(expected = IllegalArgumentException.class)
74+
public void syntaxError() {
75+
new JSONPointer("key");
76+
}
77+
78+
}

0 commit comments

Comments
 (0)