Skip to content

Commit 6edc093

Browse files
committed
adding unittests for JSPONPointer#toString(), toURIFragment() and its builder class
1 parent e748c60 commit 6edc093

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.json.junit;
22

3+
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertSame;
45

56
import org.json.JSONObject;
@@ -28,7 +29,7 @@ public void emptyPointer() {
2829

2930
@Test(expected = NullPointerException.class)
3031
public void nullPointer() {
31-
new JSONPointer(null);
32+
new JSONPointer((String) null);
3233
}
3334

3435
@Test
@@ -103,5 +104,39 @@ public void arrayIndexFailure() {
103104
public void primitiveFailure() {
104105
query("/obj/key/failure");
105106
}
107+
108+
@Test
109+
public void builderTest() {
110+
JSONPointer pointer = JSONPointer.builder()
111+
.append("obj")
112+
.append("other~key").append("another/key")
113+
.append(0)
114+
.build();
115+
assertEquals("val", pointer.queryFrom(document));
116+
}
117+
118+
@Test
119+
public void toStringEscaping() {
120+
JSONPointer pointer = JSONPointer.builder()
121+
.append("obj")
122+
.append("other~key").append("another/key")
123+
.append("\"")
124+
.append(0)
125+
.build();
126+
assertEquals("/obj/other~0key/another~1key/\\\"/0", pointer.toString());
127+
}
128+
129+
@Test
130+
public void emptyPointerToString() {
131+
assertEquals("", new JSONPointer("").toString());
132+
}
133+
134+
@Test
135+
public void toURIFragment() {
136+
assertEquals("#/c%25d", new JSONPointer("/c%d").toURIFragment());
137+
assertEquals("#/e%5Ef", new JSONPointer("/e^f").toURIFragment());
138+
assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment());
139+
assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment());
140+
}
106141

107142
}

src/test/org/json/junit/jsonpointer-testdoc.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
" ": 7,
1515
"m~n": 8,
1616
"obj" : {
17-
"key" : "value"
17+
"key" : "value",
18+
"other~key" : {
19+
"another/key" : [
20+
"val"
21+
]
22+
}
1823
}
1924
}

0 commit comments

Comments
 (0)