From 9c47ba299d5f20aee73721bb4d4d0a6c908cedc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Mon, 18 Apr 2016 21:49:14 +0200 Subject: [PATCH 1/8] initial test for JSONPointer class --- src/test/org/json/junit/JSONPointerTest.java | 78 ++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/test/org/json/junit/JSONPointerTest.java diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java new file mode 100644 index 0000000..221f035 --- /dev/null +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -0,0 +1,78 @@ +package org.json.junit; + +import static org.junit.Assert.assertSame; + +import org.json.JSONObject; +import org.json.JSONPointer; +import org.json.JSONPointerException; +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 Object query(String pointer) { + return new JSONPointer(pointer).queryFrom(document); + } + + @Test + public void emptyPointer() { + assertSame(document, query("")); + } + + @Test(expected = NullPointerException.class) + public void nullPointer() { + new JSONPointer(null); + } + + @Test + public void objectPropertyQuery() { + assertSame(document.get("foo"), query("/foo")); + } + + @Test + public void arrayIndexQuery() { + assertSame(document.getJSONArray("foo").get(0), query("/foo/0")); + } + + @Test(expected = JSONPointerException.class) + public void stringPropOfArrayFailure() { + query("/foo/bar"); + } + + @Test + public void queryByEmptyKey() { + assertSame(document.get(""), query("/")); + } + + @Test + public void slashEscaping() { + assertSame(document.get("a/b"), query("/a~1b")); + } + + @Test + public void tildeEscaping() { + assertSame(document.get("m~n"), query("/m~0n")); + } + + @Test + public void uriFragmentNotation() { + assertSame(document.get("foo"), query("#/foo")); + } + + @Test(expected = IllegalArgumentException.class) + public void syntaxError() { + new JSONPointer("key"); + } + +} 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 2/8] 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 From 66f740eb451066840cef1faf8e051a15c5d16dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 26 Apr 2016 23:02:26 +0200 Subject: [PATCH 3/8] rolling back unwanted gitignore change in previous commit --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index 2ede482..9e7b59c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,3 @@ build .classpath .project .settings/ -.gitignore -.gradle -src/main From f857dda5d84cf1baa809fa0d52c657266989a6e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 26 Apr 2016 23:03:01 +0200 Subject: [PATCH 4/8] removing some deprecated commented code --- src/test/org/json/junit/JSONPointerTest.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 463b66e..103c5a8 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -12,19 +12,6 @@ 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" - // + "}"); - static { document = new JSONObject(new JSONTokener( JSONPointerTest.class.getResourceAsStream("/org/json/junit/jsonpointer-testdoc.json"))); From e748c60eb10ba5605ca6c00f37df0d843f9592bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 26 Apr 2016 23:31:43 +0200 Subject: [PATCH 5/8] tests for improved failure handling --- src/test/org/json/junit/JSONPointerTest.java | 10 ++++++++++ src/test/org/json/junit/jsonpointer-testdoc.json | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 103c5a8..3818e82 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -94,4 +94,14 @@ public class JSONPointerTest { new JSONPointer("key"); } + @Test(expected = JSONPointerException.class) + public void arrayIndexFailure() { + query("/foo/2"); + } + + @Test(expected = JSONPointerException.class) + public void primitiveFailure() { + query("/obj/key/failure"); + } + } diff --git a/src/test/org/json/junit/jsonpointer-testdoc.json b/src/test/org/json/junit/jsonpointer-testdoc.json index 386bdb7..621ce93 100644 --- a/src/test/org/json/junit/jsonpointer-testdoc.json +++ b/src/test/org/json/junit/jsonpointer-testdoc.json @@ -12,5 +12,8 @@ "i\\j": 5, "k\"l": 6, " ": 7, - "m~n": 8 + "m~n": 8, + "obj" : { + "key" : "value" + } } \ No newline at end of file From 6edc0938033d736ab03c8e23ccaccda2c32bae7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 3 May 2016 23:20:17 +0200 Subject: [PATCH 6/8] adding unittests for JSPONPointer#toString(), toURIFragment() and its builder class --- src/test/org/json/junit/JSONPointerTest.java | 37 ++++++++++++++++++- .../org/json/junit/jsonpointer-testdoc.json | 7 +++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 3818e82..4299d8f 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -1,5 +1,6 @@ package org.json.junit; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import org.json.JSONObject; @@ -28,7 +29,7 @@ public class JSONPointerTest { @Test(expected = NullPointerException.class) public void nullPointer() { - new JSONPointer(null); + new JSONPointer((String) null); } @Test @@ -103,5 +104,39 @@ public class JSONPointerTest { public void primitiveFailure() { query("/obj/key/failure"); } + + @Test + public void builderTest() { + JSONPointer pointer = JSONPointer.builder() + .append("obj") + .append("other~key").append("another/key") + .append(0) + .build(); + assertEquals("val", pointer.queryFrom(document)); + } + + @Test + public void toStringEscaping() { + JSONPointer pointer = JSONPointer.builder() + .append("obj") + .append("other~key").append("another/key") + .append("\"") + .append(0) + .build(); + assertEquals("/obj/other~0key/another~1key/\\\"/0", pointer.toString()); + } + + @Test + public void emptyPointerToString() { + assertEquals("", new JSONPointer("").toString()); + } + + @Test + public void toURIFragment() { + assertEquals("#/c%25d", new JSONPointer("/c%d").toURIFragment()); + assertEquals("#/e%5Ef", new JSONPointer("/e^f").toURIFragment()); + assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment()); + assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment()); + } } diff --git a/src/test/org/json/junit/jsonpointer-testdoc.json b/src/test/org/json/junit/jsonpointer-testdoc.json index 621ce93..d58fe82 100644 --- a/src/test/org/json/junit/jsonpointer-testdoc.json +++ b/src/test/org/json/junit/jsonpointer-testdoc.json @@ -14,6 +14,11 @@ " ": 7, "m~n": 8, "obj" : { - "key" : "value" + "key" : "value", + "other~key" : { + "another/key" : [ + "val" + ] + } } } \ No newline at end of file From 2eed4be5fca99a8f2ced50b4827f3f1f6ab798b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 3 May 2016 23:42:26 +0200 Subject: [PATCH 7/8] one more test for null-check in Builder#append(String) --- src/test/org/json/junit/JSONPointerTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 4299d8f..f17ba6e 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -115,6 +115,11 @@ public class JSONPointerTest { assertEquals("val", pointer.queryFrom(document)); } + @Test(expected = NullPointerException.class) + public void nullToken() { + JSONPointer.builder().append(null); + } + @Test public void toStringEscaping() { JSONPointer pointer = JSONPointer.builder() From adb3118d31f84cd4d1b60828a95f64c0c9e282bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Thu, 5 May 2016 16:00:15 +0200 Subject: [PATCH 8/8] added test for checking if the JSONPointer is immutable --- src/test/org/json/junit/JSONPointerTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index f17ba6e..820023b 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -2,6 +2,7 @@ package org.json.junit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; import org.json.JSONObject; import org.json.JSONPointer; @@ -143,5 +144,16 @@ public class JSONPointerTest { assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment()); assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment()); } + + @Test + public void tokenListIsCopiedInConstructor() { + JSONPointer.Builder b = JSONPointer.builder().append("key1"); + JSONPointer jp1 = b.build(); + b.append("key2"); + JSONPointer jp2 = b.build(); + if(jp1.toString().equals(jp2.toString())) { + fail("Oops, my pointers are sharing a backing array"); + } + } }