Skip to content

Commit 6211384

Browse files
committed
tests for url fragment notation handling, moving test document to separate file
1 parent 9c47ba2 commit 6211384

3 files changed

Lines changed: 62 additions & 11 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ build
33
.classpath
44
.project
55
.settings/
6+
.gitignore
7+
.gradle
8+
src/main

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

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,30 @@
55
import org.json.JSONObject;
66
import org.json.JSONPointer;
77
import org.json.JSONPointerException;
8+
import org.json.JSONTokener;
89
import org.junit.Test;
910

1011
public class JSONPointerTest {
1112

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-
+ "}");
13+
private static final JSONObject document;
14+
15+
// = new JSONObject("{"
16+
// + "\"foo\": [\"bar\", \"baz\"], "
17+
// + "\"\": 0,"
18+
// + "\"a/b\": 1,"
19+
// + "\"c%d\": 2,"
20+
// + "\"e^f\": 3,"
21+
// + "\"g|h\": 4,"
22+
// + "\"i\\\\j\": 5,"
23+
// + "\"k\\\\\\\"l\": 6,"
24+
// + "\" \": 7,"
25+
// + "\"m~n\": 8"
26+
// + "}");
27+
28+
static {
29+
document = new JSONObject(new JSONTokener(
30+
JSONPointerTest.class.getResourceAsStream("/org/json/junit/jsonpointer-testdoc.json")));
31+
}
2332

2433
private Object query(String pointer) {
2534
return new JSONPointer(pointer).queryFrom(document);
@@ -65,11 +74,34 @@ public void tildeEscaping() {
6574
assertSame(document.get("m~n"), query("/m~0n"));
6675
}
6776

77+
@Test
78+
public void backslashEscaping() {
79+
assertSame(document.get("i\\j"), query("/i\\\\j"));
80+
}
81+
82+
@Test
83+
public void quotationEscaping() {
84+
assertSame(document.get("k\"l"), query("/k\\\\\\\"l"));
85+
}
86+
87+
@Test
88+
public void whitespaceKey() {
89+
assertSame(document.get(" "), query("/ "));
90+
}
91+
6892
@Test
6993
public void uriFragmentNotation() {
7094
assertSame(document.get("foo"), query("#/foo"));
7195
}
7296

97+
@Test
98+
public void uriFragmentPercentHandling() {
99+
assertSame(document.get("c%d"), query("#/c%25d"));
100+
assertSame(document.get("e^f"), query("#/e%5Ef"));
101+
assertSame(document.get("g|h"), query("#/g%7Ch"));
102+
assertSame(document.get("m~n"), query("#/m~0n"));
103+
}
104+
73105
@Test(expected = IllegalArgumentException.class)
74106
public void syntaxError() {
75107
new JSONPointer("key");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

0 commit comments

Comments
 (0)