|
| 1 | +package org.everit.json.schema; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertSame; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.junit.Assert.fail; |
| 7 | + |
| 8 | +import java.io.InputStream; |
| 9 | + |
| 10 | +import org.json.JSONArray; |
| 11 | +import org.json.JSONObject; |
| 12 | +import org.json.JSONPointer; |
| 13 | +import org.json.JSONPointerException; |
| 14 | +import org.json.JSONTokener; |
| 15 | +import org.junit.Ignore; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +/** |
| 19 | + * This class is ported from https://raw.githubusercontent.com/stleary/JSON-Java-unit-test/ |
| 20 | + */ |
| 21 | +public class JSONPointerTest { |
| 22 | + |
| 23 | + private static final JSONObject document; |
| 24 | + |
| 25 | + static { |
| 26 | + @SuppressWarnings("resource") |
| 27 | + InputStream resourceAsStream = JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json"); |
| 28 | + if (resourceAsStream == null) { |
| 29 | + throw new ExceptionInInitializerError("Unable to locate test file. Please check your development environment configuration"); |
| 30 | + } |
| 31 | + document = new JSONObject(new JSONTokener(resourceAsStream)); |
| 32 | + } |
| 33 | + |
| 34 | + private Object query(String pointer) { |
| 35 | + return new JSONPointer(pointer).queryFrom(document); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void emptyPointer() { |
| 40 | + assertSame(document, query("")); |
| 41 | + } |
| 42 | + |
| 43 | + @Test(expected = NullPointerException.class) |
| 44 | + public void nullPointer() { |
| 45 | + new JSONPointer((String) null); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void objectPropertyQuery() { |
| 50 | + assertSame(document.get("foo"), query("/foo")); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void arrayIndexQuery() { |
| 55 | + assertSame(document.getJSONArray("foo").get(0), query("/foo/0")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test(expected = JSONPointerException.class) |
| 59 | + public void stringPropOfArrayFailure() { |
| 60 | + query("/foo/bar"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void queryByEmptyKey() { |
| 65 | + assertSame(document.get(""), query("/")); |
| 66 | + } |
| 67 | + |
| 68 | + @Test @Ignore |
| 69 | + public void queryByEmptyKeySubObject() { |
| 70 | + assertSame(document.getJSONObject("obj").getJSONObject(""), query("/obj/")); |
| 71 | + } |
| 72 | + |
| 73 | + @Test @Ignore |
| 74 | + public void queryByEmptyKeySubObjectSubOject() { |
| 75 | + assertSame( |
| 76 | + document.getJSONObject("obj").getJSONObject("").get(""), |
| 77 | + query("/obj//") |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void queryByEmptyKeySubObjectValue() { |
| 83 | + assertSame( |
| 84 | + document.getJSONObject("obj").getJSONObject("").get("subKey"), |
| 85 | + query("/obj//subKey") |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void slashEscaping() { |
| 91 | + assertSame(document.get("a/b"), query("/a~1b")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void tildeEscaping() { |
| 96 | + assertSame(document.get("m~n"), query("/m~0n")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void backslashEscaping() { |
| 101 | + assertSame(document.get("i\\j"), query("/i\\\\j")); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void quotationEscaping() { |
| 106 | + assertSame(document.get("k\"l"), query("/k\\\\\\\"l")); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void whitespaceKey() { |
| 111 | + assertSame(document.get(" "), query("/ ")); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void uriFragmentNotation() { |
| 116 | + assertSame(document.get("foo"), query("#/foo")); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void uriFragmentNotationRoot() { |
| 121 | + assertSame(document, query("#")); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void uriFragmentPercentHandling() { |
| 126 | + assertSame(document.get("c%d"), query("#/c%25d")); |
| 127 | + assertSame(document.get("e^f"), query("#/e%5Ef")); |
| 128 | + assertSame(document.get("g|h"), query("#/g%7Ch")); |
| 129 | + assertSame(document.get("m~n"), query("#/m~0n")); |
| 130 | + } |
| 131 | + |
| 132 | + @Test(expected = IllegalArgumentException.class) |
| 133 | + public void syntaxError() { |
| 134 | + new JSONPointer("key"); |
| 135 | + } |
| 136 | + |
| 137 | + @Test(expected = JSONPointerException.class) |
| 138 | + public void arrayIndexFailure() { |
| 139 | + query("/foo/2"); |
| 140 | + } |
| 141 | + |
| 142 | + @Test(expected = JSONPointerException.class) |
| 143 | + public void primitiveFailure() { |
| 144 | + query("/obj/key/failure"); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void builderTest() { |
| 149 | + JSONPointer pointer = JSONPointer.builder() |
| 150 | + .append("obj") |
| 151 | + .append("other~key").append("another/key") |
| 152 | + .append(0) |
| 153 | + .build(); |
| 154 | + assertEquals("val", pointer.queryFrom(document)); |
| 155 | + } |
| 156 | + |
| 157 | + @Test(expected = NullPointerException.class) |
| 158 | + public void nullToken() { |
| 159 | + JSONPointer.builder().append(null); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void toStringEscaping() { |
| 164 | + JSONPointer pointer = JSONPointer.builder() |
| 165 | + .append("obj") |
| 166 | + .append("other~key").append("another/key") |
| 167 | + .append("\"") |
| 168 | + .append(0) |
| 169 | + .build(); |
| 170 | + assertEquals("/obj/other~0key/another~1key/\\\"/0", pointer.toString()); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void emptyPointerToString() { |
| 175 | + assertEquals("", new JSONPointer("").toString()); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void toURIFragment() { |
| 180 | + assertEquals("#/c%25d", new JSONPointer("/c%d").toURIFragment()); |
| 181 | + assertEquals("#/e%5Ef", new JSONPointer("/e^f").toURIFragment()); |
| 182 | + assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment()); |
| 183 | + assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment()); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void tokenListIsCopiedInConstructor() { |
| 188 | + JSONPointer.Builder b = JSONPointer.builder().append("key1"); |
| 189 | + JSONPointer jp1 = b.build(); |
| 190 | + b.append("key2"); |
| 191 | + JSONPointer jp2 = b.build(); |
| 192 | + if (jp1.toString().equals(jp2.toString())) { |
| 193 | + fail("Oops, my pointers are sharing a backing array"); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Coverage for JSONObject query(String) |
| 199 | + */ |
| 200 | + @Test |
| 201 | + public void queryFromJSONObject() { |
| 202 | + String str = "{" + |
| 203 | + "\"stringKey\":\"hello world!\"," + |
| 204 | + "\"arrayKey\":[0,1,2]," + |
| 205 | + "\"objectKey\": {" + |
| 206 | + "\"a\":\"aVal\"," + |
| 207 | + "\"b\":\"bVal\"" + |
| 208 | + "}" + |
| 209 | + "}"; |
| 210 | + JSONObject jsonObject = new JSONObject(str); |
| 211 | + Object obj = jsonObject.query("/stringKey"); |
| 212 | + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); |
| 213 | + obj = jsonObject.query("/arrayKey/1"); |
| 214 | + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); |
| 215 | + obj = jsonObject.query("/objectKey/b"); |
| 216 | + assertTrue("Expected bVal", "bVal".equals(obj)); |
| 217 | + try { |
| 218 | + obj = jsonObject.query("/a/b/c"); |
| 219 | + assertTrue("Expected JSONPointerException", false); |
| 220 | + } catch (JSONPointerException e) { |
| 221 | + assertTrue("Expected bad key/value exception", |
| 222 | + "value [null] is not an array or object therefore its key b cannot be resolved". |
| 223 | + equals(e.getMessage())); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Coverage for JSONObject query(JSONPointer) |
| 229 | + */ |
| 230 | + @Test |
| 231 | + public void queryFromJSONObjectUsingPointer() { |
| 232 | + String str = "{" + |
| 233 | + "\"stringKey\":\"hello world!\"," + |
| 234 | + "\"arrayKey\":[0,1,2]," + |
| 235 | + "\"objectKey\": {" + |
| 236 | + "\"a\":\"aVal\"," + |
| 237 | + "\"b\":\"bVal\"" + |
| 238 | + "}" + |
| 239 | + "}"; |
| 240 | + JSONObject jsonObject = new JSONObject(str); |
| 241 | + Object obj = jsonObject.query(new JSONPointer("/stringKey")); |
| 242 | + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); |
| 243 | + obj = jsonObject.query(new JSONPointer("/arrayKey/1")); |
| 244 | + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); |
| 245 | + obj = jsonObject.query(new JSONPointer("/objectKey/b")); |
| 246 | + assertTrue("Expected bVal", "bVal".equals(obj)); |
| 247 | + try { |
| 248 | + obj = jsonObject.query(new JSONPointer("/a/b/c")); |
| 249 | + assertTrue("Expected JSONPointerException", false); |
| 250 | + } catch (JSONPointerException e) { |
| 251 | + assertTrue("Expected bad key/value exception", |
| 252 | + "value [null] is not an array or object therefore its key b cannot be resolved". |
| 253 | + equals(e.getMessage())); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Coverage for JSONObject optQuery(JSONPointer) |
| 259 | + */ |
| 260 | + @Test |
| 261 | + public void optQueryFromJSONObjectUsingPointer() { |
| 262 | + String str = "{" + |
| 263 | + "\"stringKey\":\"hello world!\"," + |
| 264 | + "\"arrayKey\":[0,1,2]," + |
| 265 | + "\"objectKey\": {" + |
| 266 | + "\"a\":\"aVal\"," + |
| 267 | + "\"b\":\"bVal\"" + |
| 268 | + "}" + |
| 269 | + "}"; |
| 270 | + JSONObject jsonObject = new JSONObject(str); |
| 271 | + Object obj = jsonObject.optQuery(new JSONPointer("/stringKey")); |
| 272 | + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); |
| 273 | + obj = jsonObject.optQuery(new JSONPointer("/arrayKey/1")); |
| 274 | + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); |
| 275 | + obj = jsonObject.optQuery(new JSONPointer("/objectKey/b")); |
| 276 | + assertTrue("Expected bVal", "bVal".equals(obj)); |
| 277 | + obj = jsonObject.optQuery(new JSONPointer("/a/b/c")); |
| 278 | + assertTrue("Expected null", obj == null); |
| 279 | + } |
| 280 | + |
| 281 | + /** |
| 282 | + * Coverage for JSONArray query(String) |
| 283 | + */ |
| 284 | + @Test |
| 285 | + public void queryFromJSONArray() { |
| 286 | + String str = "[" + |
| 287 | + "\"hello world!\"," + |
| 288 | + "[0,1,2]," + |
| 289 | + "{" + |
| 290 | + "\"a\":\"aVal\"," + |
| 291 | + "\"b\":\"bVal\"" + |
| 292 | + "}" + |
| 293 | + "]"; |
| 294 | + JSONArray jsonArray = new JSONArray(str); |
| 295 | + Object obj = jsonArray.query("/0"); |
| 296 | + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); |
| 297 | + obj = jsonArray.query("/1/1"); |
| 298 | + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); |
| 299 | + obj = jsonArray.query("/2/b"); |
| 300 | + assertTrue("Expected bVal", "bVal".equals(obj)); |
| 301 | + try { |
| 302 | + obj = jsonArray.query("/a/b/c"); |
| 303 | + assertTrue("Expected JSONPointerException", false); |
| 304 | + } catch (JSONPointerException e) { |
| 305 | + assertTrue("Expected bad index exception", |
| 306 | + "a is not an array index".equals(e.getMessage())); |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + /** |
| 311 | + * Coverage for JSONArray query(JSONPointer) |
| 312 | + */ |
| 313 | + @Test |
| 314 | + public void queryFromJSONArrayUsingPointer() { |
| 315 | + String str = "[" + |
| 316 | + "\"hello world!\"," + |
| 317 | + "[0,1,2]," + |
| 318 | + "{" + |
| 319 | + "\"a\":\"aVal\"," + |
| 320 | + "\"b\":\"bVal\"" + |
| 321 | + "}" + |
| 322 | + "]"; |
| 323 | + JSONArray jsonArray = new JSONArray(str); |
| 324 | + Object obj = jsonArray.query(new JSONPointer("/0")); |
| 325 | + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); |
| 326 | + obj = jsonArray.query(new JSONPointer("/1/1")); |
| 327 | + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); |
| 328 | + obj = jsonArray.query(new JSONPointer("/2/b")); |
| 329 | + assertTrue("Expected bVal", "bVal".equals(obj)); |
| 330 | + try { |
| 331 | + obj = jsonArray.query(new JSONPointer("/a/b/c")); |
| 332 | + assertTrue("Expected JSONPointerException", false); |
| 333 | + } catch (JSONPointerException e) { |
| 334 | + assertTrue("Expected bad index exception", |
| 335 | + "a is not an array index".equals(e.getMessage())); |
| 336 | + } |
| 337 | + } |
| 338 | + |
| 339 | + /** |
| 340 | + * Coverage for JSONArray optQuery(JSONPointer) |
| 341 | + */ |
| 342 | + @Test |
| 343 | + public void optQueryFromJSONArrayUsingPointer() { |
| 344 | + String str = "[" + |
| 345 | + "\"hello world!\"," + |
| 346 | + "[0,1,2]," + |
| 347 | + "{" + |
| 348 | + "\"a\":\"aVal\"," + |
| 349 | + "\"b\":\"bVal\"" + |
| 350 | + "}" + |
| 351 | + "]"; |
| 352 | + JSONArray jsonArray = new JSONArray(str); |
| 353 | + Object obj = jsonArray.optQuery(new JSONPointer("/0")); |
| 354 | + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); |
| 355 | + obj = jsonArray.optQuery(new JSONPointer("/1/1")); |
| 356 | + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); |
| 357 | + obj = jsonArray.optQuery(new JSONPointer("/2/b")); |
| 358 | + assertTrue("Expected bVal", "bVal".equals(obj)); |
| 359 | + obj = jsonArray.optQuery(new JSONPointer("/a/b/c")); |
| 360 | + assertTrue("Expected null", obj == null); |
| 361 | + } |
| 362 | +} |
0 commit comments