From 30a70c88861e4966f5023114c5d78640d16903bd Mon Sep 17 00:00:00 2001 From: katldewitt <80657210+katldewitt@users.noreply.github.com> Date: Sun, 24 Oct 2021 08:41:57 -0700 Subject: [PATCH 1/3] chore: initial unit tests --- .../java/org/json/junit/JSONPointerTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java index 89f1f6c..2701bfb 100644 --- a/src/test/java/org/json/junit/JSONPointerTest.java +++ b/src/test/java/org/json/junit/JSONPointerTest.java @@ -134,6 +134,14 @@ public class JSONPointerTest { public void quotationHandling() { assertEquals(6, query("/k\"l")); } + + /** + * KD Added + * */ + @Test + public void quotationEscaping() { + assertEquals(document.get("k\"l"), query("/k\\\"l")); + } @Test public void whitespaceKey() { @@ -389,4 +397,33 @@ public class JSONPointerTest { obj = jsonArray.optQuery(new JSONPointer("/a/b/c")); assertTrue("Expected null", obj == null); } + + /** + * KD added + * Coverage for JSONObject query(JSONPointer) + */ + @Test + public void queryFromJSONObjectUsingPointer2() { + String str = "{"+ + "\"string\\\\\\\\Key\":\"hello world!\","+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + Object obj = jsonObject.optQuery(new JSONPointer("/string\\\\\\\\Key")); + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); + } + /** + * KD added - understanding behavior + * Coverage for JSONObject query(JSONPointer) + */ + @Test + public void queryFromJSONObjectUsingPointer0() { + String str = "{"+ + "\"string\\\\Key\":\"hello world!\","+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + Object obj = jsonObject.optQuery(new JSONPointer("/string\\Key")); + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); + } } From 669316d29e78cb8012fa02e590db7efb38432fd5 Mon Sep 17 00:00:00 2001 From: katldewitt <80657210+katldewitt@users.noreply.github.com> Date: Thu, 28 Oct 2021 05:45:23 -0700 Subject: [PATCH 2/3] chore: write unit tests for behavior --- .../java/org/json/junit/JSONPointerTest.java | 60 +++++++++---------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java index 2701bfb..0058b0c 100644 --- a/src/test/java/org/json/junit/JSONPointerTest.java +++ b/src/test/java/org/json/junit/JSONPointerTest.java @@ -41,10 +41,11 @@ public class JSONPointerTest { private static final JSONObject document; private static final String EXPECTED_COMPLETE_DOCUMENT = "{\"\":0,\" \":7,\"g|h\":4,\"c%d\":2,\"k\\\"l\":6,\"a/b\":1,\"i\\\\j\":5," + - "\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," + + "\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," + "\"other~key\":{\"another/key\":[\"val\"]},\"key\":\"value\"},\"foo\":[\"bar\",\"baz\"],\"e^f\":3," + - "\"m~n\":8}"; + "\"m~n\":8,\"four\\\\\\\\slashes\":\"slash test!\"}"; + static { @SuppressWarnings("resource") InputStream resourceAsStream = JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json"); @@ -125,6 +126,17 @@ public class JSONPointerTest { assertEquals(5, query("/i\\j")); } + /** + * When creating a jsonObject we need to parse escaped characters "\\\\" + * --> it's the string representation of "\\", so when query'ing via the JSONPointer + * we DON'T escape them + * + */ + @Test + public void multipleBackslashHandling() { + assertEquals("slash test!", query("/four\\\\slashes")); + } + /** * We pass quotations as-is * @@ -134,15 +146,7 @@ public class JSONPointerTest { public void quotationHandling() { assertEquals(6, query("/k\"l")); } - - /** - * KD Added - * */ - @Test - public void quotationEscaping() { - assertEquals(document.get("k\"l"), query("/k\\\"l")); - } - + @Test public void whitespaceKey() { assertEquals(7, query("/ ")); @@ -399,31 +403,21 @@ public class JSONPointerTest { } /** - * KD added - * Coverage for JSONObject query(JSONPointer) - */ - @Test - public void queryFromJSONObjectUsingPointer2() { - String str = "{"+ - "\"string\\\\\\\\Key\":\"hello world!\","+ - "}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - Object obj = jsonObject.optQuery(new JSONPointer("/string\\\\\\\\Key")); - assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); - } - /** - * KD added - understanding behavior + * KD added - this should pass * Coverage for JSONObject query(JSONPointer) */ @Test public void queryFromJSONObjectUsingPointer0() { - String str = "{"+ - "\"string\\\\Key\":\"hello world!\","+ - "}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - Object obj = jsonObject.optQuery(new JSONPointer("/string\\Key")); - assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); + String str = "{"+ + "\"string\\\\\\\\Key\":\"hello world!\","+ + + "\"\\\\\":\"slash test\"," + + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + //Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held + // as "\\" which makes it impossible to get back where expected + Object obj = jsonObject.optQuery(new JSONPointer("/\\")); + assertEquals("slash test", obj); } } From 3ed9154f6316ed4b6105da2246e373f43a16f111 Mon Sep 17 00:00:00 2001 From: katldewitt <80657210+katldewitt@users.noreply.github.com> Date: Thu, 28 Oct 2021 05:59:22 -0700 Subject: [PATCH 3/3] fix: refactor tests context: the backslash unit tests cannot be in the complete document as uriFragmentNotationRoot() will fail due to backslash handling --- .../java/org/json/junit/JSONPointerTest.java | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java index 0058b0c..4ea7434 100644 --- a/src/test/java/org/json/junit/JSONPointerTest.java +++ b/src/test/java/org/json/junit/JSONPointerTest.java @@ -43,7 +43,7 @@ public class JSONPointerTest { private static final String EXPECTED_COMPLETE_DOCUMENT = "{\"\":0,\" \":7,\"g|h\":4,\"c%d\":2,\"k\\\"l\":6,\"a/b\":1,\"i\\\\j\":5," + "\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," + "\"other~key\":{\"another/key\":[\"val\"]},\"key\":\"value\"},\"foo\":[\"bar\",\"baz\"],\"e^f\":3," + - "\"m~n\":8,\"four\\\\\\\\slashes\":\"slash test!\"}"; + "\"m~n\":8}"; static { @@ -125,17 +125,6 @@ public class JSONPointerTest { public void backslashHandling() { assertEquals(5, query("/i\\j")); } - - /** - * When creating a jsonObject we need to parse escaped characters "\\\\" - * --> it's the string representation of "\\", so when query'ing via the JSONPointer - * we DON'T escape them - * - */ - @Test - public void multipleBackslashHandling() { - assertEquals("slash test!", query("/four\\\\slashes")); - } /** * We pass quotations as-is @@ -403,8 +392,10 @@ public class JSONPointerTest { } /** - * KD added - this should pass - * Coverage for JSONObject query(JSONPointer) + * When creating a jsonObject we need to parse escaped characters "\\\\" + * --> it's the string representation of "\\", so when query'ing via the JSONPointer + * we DON'T escape them + * */ @Test public void queryFromJSONObjectUsingPointer0() { @@ -416,8 +407,11 @@ public class JSONPointerTest { "}"; JSONObject jsonObject = new JSONObject(str); //Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held - // as "\\" which makes it impossible to get back where expected - Object obj = jsonObject.optQuery(new JSONPointer("/\\")); - assertEquals("slash test", obj); + // as "\\" which means when querying, we need to use "\\" + Object twoBackslahObj = jsonObject.optQuery(new JSONPointer("/\\")); + assertEquals("slash test", twoBackslahObj); + + Object fourBackslashObj = jsonObject.optQuery(new JSONPointer("/string\\\\Key")); + assertEquals("hello world!", fourBackslashObj); } }