Skip to content

Commit 46044bf

Browse files
committed
Merge pull request #46 from erosb/master
Tests for JSON Pointers
2 parents a9ff159 + adb3118 commit 46044bf

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package org.json.junit;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertSame;
5+
import static org.junit.Assert.fail;
6+
7+
import org.json.JSONObject;
8+
import org.json.JSONPointer;
9+
import org.json.JSONPointerException;
10+
import org.json.JSONTokener;
11+
import org.junit.Test;
12+
13+
public class JSONPointerTest {
14+
15+
private static final JSONObject document;
16+
17+
static {
18+
document = new JSONObject(new JSONTokener(
19+
JSONPointerTest.class.getResourceAsStream("/org/json/junit/jsonpointer-testdoc.json")));
20+
}
21+
22+
private Object query(String pointer) {
23+
return new JSONPointer(pointer).queryFrom(document);
24+
}
25+
26+
@Test
27+
public void emptyPointer() {
28+
assertSame(document, query(""));
29+
}
30+
31+
@Test(expected = NullPointerException.class)
32+
public void nullPointer() {
33+
new JSONPointer((String) null);
34+
}
35+
36+
@Test
37+
public void objectPropertyQuery() {
38+
assertSame(document.get("foo"), query("/foo"));
39+
}
40+
41+
@Test
42+
public void arrayIndexQuery() {
43+
assertSame(document.getJSONArray("foo").get(0), query("/foo/0"));
44+
}
45+
46+
@Test(expected = JSONPointerException.class)
47+
public void stringPropOfArrayFailure() {
48+
query("/foo/bar");
49+
}
50+
51+
@Test
52+
public void queryByEmptyKey() {
53+
assertSame(document.get(""), query("/"));
54+
}
55+
56+
@Test
57+
public void slashEscaping() {
58+
assertSame(document.get("a/b"), query("/a~1b"));
59+
}
60+
61+
@Test
62+
public void tildeEscaping() {
63+
assertSame(document.get("m~n"), query("/m~0n"));
64+
}
65+
66+
@Test
67+
public void backslashEscaping() {
68+
assertSame(document.get("i\\j"), query("/i\\\\j"));
69+
}
70+
71+
@Test
72+
public void quotationEscaping() {
73+
assertSame(document.get("k\"l"), query("/k\\\\\\\"l"));
74+
}
75+
76+
@Test
77+
public void whitespaceKey() {
78+
assertSame(document.get(" "), query("/ "));
79+
}
80+
81+
@Test
82+
public void uriFragmentNotation() {
83+
assertSame(document.get("foo"), query("#/foo"));
84+
}
85+
86+
@Test
87+
public void uriFragmentPercentHandling() {
88+
assertSame(document.get("c%d"), query("#/c%25d"));
89+
assertSame(document.get("e^f"), query("#/e%5Ef"));
90+
assertSame(document.get("g|h"), query("#/g%7Ch"));
91+
assertSame(document.get("m~n"), query("#/m~0n"));
92+
}
93+
94+
@Test(expected = IllegalArgumentException.class)
95+
public void syntaxError() {
96+
new JSONPointer("key");
97+
}
98+
99+
@Test(expected = JSONPointerException.class)
100+
public void arrayIndexFailure() {
101+
query("/foo/2");
102+
}
103+
104+
@Test(expected = JSONPointerException.class)
105+
public void primitiveFailure() {
106+
query("/obj/key/failure");
107+
}
108+
109+
@Test
110+
public void builderTest() {
111+
JSONPointer pointer = JSONPointer.builder()
112+
.append("obj")
113+
.append("other~key").append("another/key")
114+
.append(0)
115+
.build();
116+
assertEquals("val", pointer.queryFrom(document));
117+
}
118+
119+
@Test(expected = NullPointerException.class)
120+
public void nullToken() {
121+
JSONPointer.builder().append(null);
122+
}
123+
124+
@Test
125+
public void toStringEscaping() {
126+
JSONPointer pointer = JSONPointer.builder()
127+
.append("obj")
128+
.append("other~key").append("another/key")
129+
.append("\"")
130+
.append(0)
131+
.build();
132+
assertEquals("/obj/other~0key/another~1key/\\\"/0", pointer.toString());
133+
}
134+
135+
@Test
136+
public void emptyPointerToString() {
137+
assertEquals("", new JSONPointer("").toString());
138+
}
139+
140+
@Test
141+
public void toURIFragment() {
142+
assertEquals("#/c%25d", new JSONPointer("/c%d").toURIFragment());
143+
assertEquals("#/e%5Ef", new JSONPointer("/e^f").toURIFragment());
144+
assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment());
145+
assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment());
146+
}
147+
148+
@Test
149+
public void tokenListIsCopiedInConstructor() {
150+
JSONPointer.Builder b = JSONPointer.builder().append("key1");
151+
JSONPointer jp1 = b.build();
152+
b.append("key2");
153+
JSONPointer jp2 = b.build();
154+
if(jp1.toString().equals(jp2.toString())) {
155+
fail("Oops, my pointers are sharing a backing array");
156+
}
157+
}
158+
159+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"foo":
3+
[
4+
"bar",
5+
"baz"
6+
],
7+
"": 0,
8+
"a/b": 1,
9+
"c%d": 2,
10+
"e^f": 3,
11+
"g|h": 4,
12+
"i\\j": 5,
13+
"k\"l": 6,
14+
" ": 7,
15+
"m~n": 8,
16+
"obj" : {
17+
"key" : "value",
18+
"other~key" : {
19+
"another/key" : [
20+
"val"
21+
]
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)