From 6211384f87e8aee878b2a59c9f9d1e9101d9cfd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 26 Apr 2016 23:01:18 +0200 Subject: [PATCH] tests for url fragment notation handling, moving test document to separate file --- .gitignore | 3 ++ src/test/org/json/junit/JSONPointerTest.java | 54 +++++++++++++++---- .../org/json/junit/jsonpointer-testdoc.json | 16 ++++++ 3 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 src/test/org/json/junit/jsonpointer-testdoc.json diff --git a/.gitignore b/.gitignore index 9e7b59c..2ede482 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ build .classpath .project .settings/ +.gitignore +.gradle +src/main diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 221f035..463b66e 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -5,21 +5,30 @@ import static org.junit.Assert.assertSame; import org.json.JSONObject; import org.json.JSONPointer; import org.json.JSONPointerException; +import org.json.JSONTokener; import org.junit.Test; public class JSONPointerTest { - private static final JSONObject document = new JSONObject("{" - + "\"foo\": [\"bar\", \"baz\"], " - + "\"\": 0," - + "\"a/b\": 1," - + "\"c%d\": 2," - + "\"e^f\": 3," - + "\"g|h\": 4," + "\"i\\\\j\": 5," - + "\"k\\\"l\": 6," - + "\" \": 7," - + "\"m~n\": 8" - + "}"); + private static final JSONObject document; + + // = new JSONObject("{" + // + "\"foo\": [\"bar\", \"baz\"], " + // + "\"\": 0," + // + "\"a/b\": 1," + // + "\"c%d\": 2," + // + "\"e^f\": 3," + // + "\"g|h\": 4," + // + "\"i\\\\j\": 5," + // + "\"k\\\\\\\"l\": 6," + // + "\" \": 7," + // + "\"m~n\": 8" + // + "}"); + + static { + document = new JSONObject(new JSONTokener( + JSONPointerTest.class.getResourceAsStream("/org/json/junit/jsonpointer-testdoc.json"))); + } private Object query(String pointer) { return new JSONPointer(pointer).queryFrom(document); @@ -65,11 +74,34 @@ public class JSONPointerTest { assertSame(document.get("m~n"), query("/m~0n")); } + @Test + public void backslashEscaping() { + assertSame(document.get("i\\j"), query("/i\\\\j")); + } + + @Test + public void quotationEscaping() { + assertSame(document.get("k\"l"), query("/k\\\\\\\"l")); + } + + @Test + public void whitespaceKey() { + assertSame(document.get(" "), query("/ ")); + } + @Test public void uriFragmentNotation() { assertSame(document.get("foo"), query("#/foo")); } + @Test + public void uriFragmentPercentHandling() { + assertSame(document.get("c%d"), query("#/c%25d")); + assertSame(document.get("e^f"), query("#/e%5Ef")); + assertSame(document.get("g|h"), query("#/g%7Ch")); + assertSame(document.get("m~n"), query("#/m~0n")); + } + @Test(expected = IllegalArgumentException.class) public void syntaxError() { new JSONPointer("key"); diff --git a/src/test/org/json/junit/jsonpointer-testdoc.json b/src/test/org/json/junit/jsonpointer-testdoc.json new file mode 100644 index 0000000..386bdb7 --- /dev/null +++ b/src/test/org/json/junit/jsonpointer-testdoc.json @@ -0,0 +1,16 @@ +{ + "foo": + [ + "bar", + "baz" + ], + "": 0, + "a/b": 1, + "c%d": 2, + "e^f": 3, + "g|h": 4, + "i\\j": 5, + "k\"l": 6, + " ": 7, + "m~n": 8 +} \ No newline at end of file