|
5 | 5 | import org.json.JSONObject; |
6 | 6 | import org.json.JSONPointer; |
7 | 7 | import org.json.JSONPointerException; |
| 8 | +import org.json.JSONTokener; |
8 | 9 | import org.junit.Test; |
9 | 10 |
|
10 | 11 | public class JSONPointerTest { |
11 | 12 |
|
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 | + } |
23 | 32 |
|
24 | 33 | private Object query(String pointer) { |
25 | 34 | return new JSONPointer(pointer).queryFrom(document); |
@@ -65,11 +74,34 @@ public void tildeEscaping() { |
65 | 74 | assertSame(document.get("m~n"), query("/m~0n")); |
66 | 75 | } |
67 | 76 |
|
| 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 | + |
68 | 92 | @Test |
69 | 93 | public void uriFragmentNotation() { |
70 | 94 | assertSame(document.get("foo"), query("#/foo")); |
71 | 95 | } |
72 | 96 |
|
| 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 | + |
73 | 105 | @Test(expected = IllegalArgumentException.class) |
74 | 106 | public void syntaxError() { |
75 | 107 | new JSONPointer("key"); |
|
0 commit comments