From c8563ff93daee506201b075faf203f726319afd7 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 22 Sep 2016 12:38:30 -0400 Subject: [PATCH 01/15] new test case for XML changes --- src/test/java/org/json/junit/XMLTest.java | 31 ++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 2f3fea7..11c05d4 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -267,6 +267,35 @@ public void shouldHandleSimpleXML() { compareFileToJSONObject(xmlStr, expectedStr); } + /** + * Tests to verify that supported escapes in XML are converted to actual values. + */ + @Test + public void testXmlEscapeToJson(){ + String xmlStr = + "\n"+ + ""+ + "\""+ + "A €33"+ + "A €22€"+ + "some text ©"+ + "" " & ' < >"+ + ""; + String expectedStr = + "{\"root\":{" + + "\"rawQuote\":\"\\\"\"," + + "\"euro\":\"A €33\"," + + "\"euroX\":\"A €22€\"," + + "\"unknown\":\"some text ©\"," + + "\"known\":\"\\\" \\\" & ' < >\"" + + "}}"; + + compareStringToJSONObject(xmlStr, expectedStr); + compareReaderToJSONObject(xmlStr, expectedStr); + compareFileToJSONObject(xmlStr, expectedStr); + + } + /** * Valid XML with comments to JSONObject */ @@ -675,8 +704,8 @@ public void contentOperations() { * @param expectedStr the expected JSON string */ private void compareStringToJSONObject(String xmlStr, String expectedStr) { - JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject jsonObject = XML.toJSONObject(xmlStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } From 5027a283c1b9c1bc81663441f0a0b23de48115cc Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 22 Sep 2016 13:09:32 -0400 Subject: [PATCH 02/15] Adds test for escaping from a JSONObject to XML --- src/test/java/org/json/junit/XMLTest.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 11c05d4..800dc93 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1,6 +1,7 @@ package org.json.junit; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; @@ -293,7 +294,19 @@ public void testXmlEscapeToJson(){ compareStringToJSONObject(xmlStr, expectedStr); compareReaderToJSONObject(xmlStr, expectedStr); compareFileToJSONObject(xmlStr, expectedStr); - + } + + /** + * Tests that certain unicode characters are escaped. + */ + @Test + public void testJsonToXmlEscape(){ + JSONObject json = new JSONObject("{ \"amount\": \"10,00 €\", \"description\": \"Ação Válida\" }"); + String xml = XML.toString(json); + assertFalse("Escaping € failed. Found in XML output.", xml.contains("€")); + assertTrue("Escaping ç failed. Not found in XML output.", xml.contains("ç")); + assertTrue("Escaping ã failed. Not found in XML output.", xml.contains("ã")); + assertTrue("Escaping á failed. Not found in XML output.", xml.contains("á")); } /** From 2b87f334d0343774a77b1779011eaba250a2a0c9 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 22 Sep 2016 14:13:48 -0400 Subject: [PATCH 03/15] Update test cases to support ISO Control encoding changes. --- src/test/java/org/json/junit/XMLTest.java | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 800dc93..91ee420 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -297,16 +297,30 @@ public void testXmlEscapeToJson(){ } /** - * Tests that certain unicode characters are escaped. + * Tests that control characters are escaped. */ @Test public void testJsonToXmlEscape(){ - JSONObject json = new JSONObject("{ \"amount\": \"10,00 €\", \"description\": \"Ação Válida\" }"); + final String jsonSrc = "{\"amount\":\"10,00 €\"," + + "\"description\":\"Ação Válida\u0085\"," + + "\"xmlEntities\":\"\\\" ' & < >\"" + + "}"; + JSONObject json = new JSONObject(jsonSrc); String xml = XML.toString(json); - assertFalse("Escaping € failed. Found in XML output.", xml.contains("€")); + //test control character not existing + assertFalse("Escaping \u0085 failed. Found in XML output.", xml.contains("\u0085")); + assertTrue("Escaping \u0085 failed. Entity not found in XML output.", xml.contains("…")); + // test normal unicode existing + assertTrue("Escaping € failed. Not found in XML output.", xml.contains("€")); assertTrue("Escaping ç failed. Not found in XML output.", xml.contains("ç")); assertTrue("Escaping ã failed. Not found in XML output.", xml.contains("ã")); assertTrue("Escaping á failed. Not found in XML output.", xml.contains("á")); + // test XML Entities converted + assertTrue("Escaping \" failed. Not found in XML output.", xml.contains(""")); + assertTrue("Escaping ' failed. Not found in XML output.", xml.contains("'")); + assertTrue("Escaping & failed. Not found in XML output.", xml.contains("&")); + assertTrue("Escaping < failed. Not found in XML output.", xml.contains("<")); + assertTrue("Escaping > failed. Not found in XML output.", xml.contains(">")); } /** From f6a00e94c77e3b1ca6c74308696600502751efa4 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 22 Sep 2016 16:12:00 -0400 Subject: [PATCH 04/15] adds test for unicode that has surrogate pairs --- src/test/java/org/json/junit/XMLTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 91ee420..264fa44 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -281,6 +281,7 @@ public void testXmlEscapeToJson(){ "A €22€"+ "some text ©"+ "" " & ' < >"+ + "𝄢 𐅥" + ""; String expectedStr = "{\"root\":{" + @@ -288,7 +289,8 @@ public void testXmlEscapeToJson(){ "\"euro\":\"A €33\"," + "\"euroX\":\"A €22€\"," + "\"unknown\":\"some text ©\"," + - "\"known\":\"\\\" \\\" & ' < >\"" + + "\"known\":\"\\\" \\\" & ' < >\"," + + "\"high\":\"𝄢 𐅥\""+ "}}"; compareStringToJSONObject(xmlStr, expectedStr); From e7f7d348cd13e2e6d168e13398f6d7384294cc2a Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 10 Dec 2018 11:45:10 -0500 Subject: [PATCH 05/15] * updates tests to cover more cases of tokenizing * uncomments tests that should now work --- src/test/java/org/json/junit/CDLTest.java | 10 +- .../java/org/json/junit/JSONTokenerTest.java | 94 +++++++++++++++++++ src/test/java/org/json/junit/XMLTest.java | 32 +++---- 3 files changed, 115 insertions(+), 21 deletions(-) diff --git a/src/test/java/org/json/junit/CDLTest.java b/src/test/java/org/json/junit/CDLTest.java index b1f9561..721fd3c 100644 --- a/src/test/java/org/json/junit/CDLTest.java +++ b/src/test/java/org/json/junit/CDLTest.java @@ -30,7 +30,7 @@ public class CDLTest { ); /** - * CDL.toJSONArray() adds all values asstrings, with no filtering or + * CDL.toJSONArray() adds all values as strings, with no filtering or * conversions. For testing, this means that the expected JSONObject * values all must be quoted in the cases where the JSONObject parsing * might normally convert the value into a non-string. @@ -264,8 +264,8 @@ public void checkSpecialChars() { */ @Test public void textToJSONArray() { - JSONArray jsonArray = CDL.toJSONArray(lines); - JSONArray expectedJsonArray = new JSONArray(expectedLines); + JSONArray jsonArray = CDL.toJSONArray(this.lines); + JSONArray expectedJsonArray = new JSONArray(this.expectedLines); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } @@ -289,10 +289,10 @@ public void jsonArrayToJSONArray() { */ @Test public void textToJSONArrayAndBackToString() { - JSONArray jsonArray = CDL.toJSONArray(lines); + JSONArray jsonArray = CDL.toJSONArray(this.lines); String jsonStr = CDL.toString(jsonArray); JSONArray finalJsonArray = CDL.toJSONArray(jsonStr); - JSONArray expectedJsonArray = new JSONArray(expectedLines); + JSONArray expectedJsonArray = new JSONArray(this.expectedLines); Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java index dced89f..de1564d 100644 --- a/src/test/java/org/json/junit/JSONTokenerTest.java +++ b/src/test/java/org/json/junit/JSONTokenerTest.java @@ -12,7 +12,9 @@ import java.io.Reader; import java.io.StringReader; +import org.json.JSONArray; import org.json.JSONException; +import org.json.JSONObject; import org.json.JSONTokener; import org.junit.Test; @@ -64,7 +66,99 @@ public void verifyBackFailureDoubleBack() throws IOException { } } } + + @Test + public void testValid() { + checkValid("0",Number.class); + checkValid(" 0 ",Number.class); + checkValid("23",Number.class); + checkValid("23.5",Number.class); + checkValid(" 23.5 ",Number.class); + checkValid("null",null); + checkValid(" null ",null); + checkValid("true",Boolean.class); + checkValid(" true\n",Boolean.class); + checkValid("false",Boolean.class); + checkValid("\nfalse ",Boolean.class); + checkValid("{}",JSONObject.class); + checkValid(" {} ",JSONObject.class); + checkValid("{\"a\":1}",JSONObject.class); + checkValid(" {\"a\":1} ",JSONObject.class); + checkValid("[]",JSONArray.class); + checkValid(" [] ",JSONArray.class); + checkValid("[1,2]",JSONArray.class); + checkValid("\n\n[1,2]\n\n",JSONArray.class); + checkValid("1 2", String.class); + } + + @Test + public void testErrors() { + // Check that stream can detect that a value is found after + // the first one + checkError(" { \"a\":1 } 4 "); + checkError("null \"a\""); + checkError("{} true"); + } + + private Object checkValid(String testStr, Class aClass) { + Object result = nextValue(testStr); + // Check class of object returned + if( null == aClass ) { + if(JSONObject.NULL.equals(result)) { + // OK + } else { + throw new JSONException("Unexpected class: "+result.getClass().getSimpleName()); + } + } else { + if( null == result ) { + throw new JSONException("Unexpected null result"); + } else if(!aClass.isAssignableFrom(result.getClass()) ) { + throw new JSONException("Unexpected class: "+result.getClass().getSimpleName()); + } + } + + return result; + } + + private void checkError(String testStr) { + try { + nextValue(testStr); + + fail("Error should be triggered: (\""+testStr+"\")"); + } catch (JSONException e) { + // OK + } + } + + /** + * Verifies that JSONTokener can read a stream that contains a value. After + * the reading is done, check that the stream is left in the correct state + * by reading the characters after. All valid cases should reach end of stream. + * @param testStr + * @return + * @throws Exception + */ + private Object nextValue(String testStr) throws JSONException { + try(StringReader sr = new StringReader(testStr);){ + JSONTokener tokener = new JSONTokener(sr); + + Object result = tokener.nextValue(); + + if( result == null ) { + throw new JSONException("Unable to find value token in JSON stream: ("+tokener+"): "+testStr); + } + + char c = tokener.nextClean(); + if( 0 != c ) { + throw new JSONException("Unexpected character found at end of JSON stream: "+c+ " ("+tokener+"): "+testStr); + } + + return result; + } + + } + /** * Tests the failure of the skipTo method with a buffered reader. Preferably * we'd like this not to fail but at this time we don't have a good recovery. diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 1cff326..83a7cc2 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -6,6 +6,13 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -743,14 +750,10 @@ private void compareStringToJSONObject(String xmlStr, String expectedStr) { * @param expectedStr the expected JSON string */ private void compareReaderToJSONObject(String xmlStr, String expectedStr) { - /* - * Commenting out this method until the JSON-java code is updated - * to support XML.toJSONObject(reader) JSONObject expectedJsonObject = new JSONObject(expectedStr); Reader reader = new StringReader(xmlStr); JSONObject jsonObject = XML.toJSONObject(reader); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); - */ } /** @@ -764,22 +767,19 @@ private void compareReaderToJSONObject(String xmlStr, String expectedStr) { * @throws IOException */ private void compareFileToJSONObject(String xmlStr, String expectedStr) { - /* - * Commenting out this method until the JSON-java code is updated - * to support XML.toJSONObject(reader) try { JSONObject expectedJsonObject = new JSONObject(expectedStr); - File tempFile = testFolder.newFile("fileToJSONObject.xml"); - FileWriter fileWriter = new FileWriter(tempFile); - fileWriter.write(xmlStr); - fileWriter.close(); - Reader reader = new FileReader(tempFile); - JSONObject jsonObject = XML.toJSONObject(reader); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + File tempFile = this.testFolder.newFile("fileToJSONObject.xml"); + try(FileWriter fileWriter = new FileWriter(tempFile);){ + fileWriter.write(xmlStr); + } + try(Reader reader = new FileReader(tempFile);){ + JSONObject jsonObject = XML.toJSONObject(reader); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } } catch (IOException e) { - assertTrue("file writer error: " +e.getMessage(), false); + fail("file writer error: " +e.getMessage()); } - */ } /** From 614e8359b91eb997142d313a0469aa69b707394e Mon Sep 17 00:00:00 2001 From: meiskalt7 Date: Thu, 18 Apr 2019 21:42:57 +0700 Subject: [PATCH 06/15] add test for xsi:nil to null conversion --- src/test/java/org/json/junit/XMLTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 83a7cc2..195f706 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -17,6 +17,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.json.XML; +import org.json.XMLParserConfiguration; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -856,4 +857,15 @@ public void testUnescape() { } + /** + * test passes when xsi:nil="true" converting to null (JSON specification-like conversion) + */ + @Test + public void testToJsonWithNull() { + final String originalXml = ""; + final String expectedJsonString = "{\"root\":{\"id\":null}}"; + + final JSONObject json = XML.toJSONObject(originalXml,new XMLParserConfiguration(false, "content", true)); + assertEquals(expectedJsonString, json.toString()); + } } \ No newline at end of file From fa173fa51a61ba68c5c721c3170869f5ae93a927 Mon Sep 17 00:00:00 2001 From: meiskalt7 Date: Sun, 21 Apr 2019 00:53:39 +0700 Subject: [PATCH 07/15] add test for xsi:nil to null conversion disabled --- src/test/java/org/json/junit/XMLTest.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 195f706..b74daff 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -858,14 +858,26 @@ public void testUnescape() { } /** - * test passes when xsi:nil="true" converting to null (JSON specification-like conversion) + * test passes when xsi:nil="true" converting to null (JSON specification-like nil conversion enabled) */ @Test - public void testToJsonWithNull() { + public void testToJsonWithNullWhenNilConversionEnabled() { final String originalXml = ""; final String expectedJsonString = "{\"root\":{\"id\":null}}"; - final JSONObject json = XML.toJSONObject(originalXml,new XMLParserConfiguration(false, "content", true)); + final JSONObject json = XML.toJSONObject(originalXml, new XMLParserConfiguration(false, "content", true)); + assertEquals(expectedJsonString, json.toString()); + } + + /** + * test passes when xsi:nil="true" not converting to null (JSON specification-like nil conversion disabled) + */ + @Test + public void testToJsonWithNullWhenNilConversionDisabled() { + final String originalXml = ""; + final String expectedJsonString = "{\"root\":{\"id\":{\"xsi:nil\":true}}}"; + + final JSONObject json = XML.toJSONObject(originalXml, new XMLParserConfiguration()); assertEquals(expectedJsonString, json.toString()); } } \ No newline at end of file From 3e7a0b13d1c2ed0b18d5ba143ddaaf171c72932a Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 17 Sep 2019 10:36:48 -0400 Subject: [PATCH 08/15] new test case for issue 484 --- src/test/java/org/json/junit/JSONMLTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 84b33ba..26f4f90 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -803,4 +803,16 @@ public void testToJSONObject_reversibility() { // // assertEquals(expectedJsonString, actualJsonString); // } + + @Test (timeout = 6000) + public void testIssue484InfinteLoop() { + try { + JSONML.toJSONObject("??*^M??|?CglR^F??`??>?w??PIlr^E??D^X^]?$?-^R?o??O?*??{OD?^FY??`2a????NM?b^Tq?:O?>S$^K?J?^FB.gUK?m^H??zE??^??!v]?^A???^[^A??^U?c??????h???s???g^Z???`?q^Dbi??:^QZl?)?}1^??k?0??:$V?$?Ovs(}J??^V????2;^QgQ?^_^A?^D?^U?Tg?K?`?h%c?hmGA? Date: Tue, 17 Sep 2019 10:47:16 -0400 Subject: [PATCH 09/15] Test cases updates for standardized exception messages --- src/test/java/org/json/junit/JSONArrayTest.java | 8 ++++---- src/test/java/org/json/junit/JSONMLTest.java | 6 +++--- src/test/java/org/json/junit/JSONObjectTest.java | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 3b70446..5aef340 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -346,14 +346,14 @@ public void failedGetArrayValues() { assertTrue("expected getDouble to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a number.",e.getMessage()); + "JSONArray[4] is not a double.",e.getMessage()); } try { jsonArray.getInt(4); assertTrue("expected getInt to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a number.",e.getMessage()); + "JSONArray[4] is not a int.",e.getMessage()); } try { jsonArray.getJSONArray(4); @@ -374,14 +374,14 @@ public void failedGetArrayValues() { assertTrue("expected getLong to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a number.",e.getMessage()); + "JSONArray[4] is not a long.",e.getMessage()); } try { jsonArray.getString(5); assertTrue("expected getString to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[5] not a string.",e.getMessage()); + "JSONArray[5] is not a String.",e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 84b33ba..fe3cd87 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -133,9 +133,9 @@ public void emptyTagException() { JSONML.toString(jsonArray); assertTrue("Expecting an exception", false); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONArray[0] not a string.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONArray[0] is not a String.", + e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 8d32649..ac67980 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1041,7 +1041,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"trueKey\"] not a string.", + "JSONObject[\"trueKey\"] is not a string.", e.getMessage()); } try { @@ -1057,7 +1057,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.", + "JSONObject[\"stringKey\"] is not a double.", e.getMessage()); } try { @@ -1073,7 +1073,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.", + "JSONObject[\"stringKey\"] is not a float.", e.getMessage()); } try { @@ -1089,7 +1089,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.", + "JSONObject[\"stringKey\"] is not a int.", e.getMessage()); } try { @@ -1105,7 +1105,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.", + "JSONObject[\"stringKey\"] is not a long.", e.getMessage()); } try { @@ -2087,7 +2087,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[myKey] is not a JSONArray.", + "JSONObject[\"myKey\"] is not a JSONArray (null).", e.getMessage()); } try { From 67e59888a2764d5b36fa133473563d70da4136ac Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 17 Sep 2019 11:14:41 -0400 Subject: [PATCH 10/15] add second case for data in #484 --- src/test/java/org/json/junit/JSONMLTest.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 26f4f90..b7afe40 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -2,6 +2,8 @@ import static org.junit.Assert.*; +import java.util.Base64; + import org.json.*; import org.junit.Test; @@ -805,7 +807,7 @@ public void testToJSONObject_reversibility() { // } @Test (timeout = 6000) - public void testIssue484InfinteLoop() { + public void testIssue484InfinteLoop1() { try { JSONML.toJSONObject("??*^M??|?CglR^F??`??>?w??PIlr^E??D^X^]?$?-^R?o??O?*??{OD?^FY??`2a????NM?b^Tq?:O?>S$^K?J?^FB.gUK?m^H??zE??^??!v]?^A???^[^A??^U?c??????h???s???g^Z???`?q^Dbi??:^QZl?)?}1^??k?0??:$V?$?Ovs(}J??^V????2;^QgQ?^_^A?^D?^U?Tg?K?`?h%c?hmGA??w??PIlr??D?$?-?o??O?*??{OD?Y??`2a????NM?bq?:O?>S$ ?J?B.gUK?m\b??zE???!v]???????c??????h???s???g???`?qbi??:Zl?)?}1^??k?0??:$V?$?Ovs(}J??????2;gQ????Tg?K?`?h%c?hmGA? Date: Tue, 17 Sep 2019 11:15:25 -0400 Subject: [PATCH 11/15] remove unused import --- src/test/java/org/json/junit/JSONMLTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index b7afe40..3687fe5 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -2,8 +2,6 @@ import static org.junit.Assert.*; -import java.util.Base64; - import org.json.*; import org.junit.Test; From 16da56eb34b6036f145dea7e6bf638b6ad418b5e Mon Sep 17 00:00:00 2001 From: Alanscut Date: Sat, 28 Dec 2019 17:53:27 +0800 Subject: [PATCH 12/15] improve the confused assert message --- .../java/org/json/junit/JSONObjectTest.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index ac67980..438e55e 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -589,19 +589,19 @@ public void jsonObjectByBean2() { jsonObject.has("someString")); assertFalse("Normal field name (myDouble) processing did not work", jsonObject.has("myDouble")); - assertFalse("Normal field name (someFloat) found", + assertFalse("Normal field name (someFloat) processing did not work", jsonObject.has("someFloat")); - assertFalse("Ignored field found!", + assertFalse("Ignored field not found!", jsonObject.has("ignoredInt")); - assertTrue("Normal field name (someInt) processing did not work", + assertTrue("Normal field name (someInt) found", jsonObject.has("someInt")); - assertTrue("Normal field name (someLong) processing did not work", + assertTrue("Normal field name (someLong) found", jsonObject.has("someLong")); - assertTrue("Overridden String field name (myStringField) not found", + assertTrue("Overridden String field name (myStringField) found", jsonObject.has("myStringField")); - assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not found", + assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) found", jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); - assertTrue("Overridden String field name (InterfaceField) not found", + assertTrue("Overridden String field name (InterfaceField) found", jsonObject.has("InterfaceField")); } @@ -619,26 +619,29 @@ public void jsonObjectByBean3() { jsonObject.has("someInt")); assertFalse("Normal field name (myDouble) processing did not work", jsonObject.has("myDouble")); - assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) FOUND!", + assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not FOUND!", jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); - assertFalse("Normal field name (someFloat) found", + assertFalse("Normal field name (someFloat) found, but was overridden", jsonObject.has("someFloat")); - assertFalse("Ignored field found!", + assertFalse("Ignored field found! but was overridden", jsonObject.has("ignoredInt")); - assertFalse("Ignored field at the same level as forced name found", + assertFalse("Ignored field at the same level as forced name not found", jsonObject.has("ShouldBeIgnored")); - assertTrue("Overridden int field name (newIntFieldName) not found", + assertFalse("Normally ignored field (able) with explicit property name not found", + jsonObject.has("able")); + assertTrue("Overridden int field name (newIntFieldName) found", jsonObject.has("newIntFieldName")); - assertTrue("Normal field name (someLong) processing did not work", + assertTrue("Normal field name (someLong) found", jsonObject.has("someLong")); - assertTrue("Overridden String field name (myStringField) not found", + assertTrue("Overridden String field name (myStringField) found", jsonObject.has("myStringField")); - assertTrue(jsonObject.has("AMoreNormalName")); - assertTrue("Overridden String field name (InterfaceField) not found", + assertTrue("Overridden double field name (AMoreNormalName) found", + jsonObject.has("AMoreNormalName")); + assertTrue("Overridden String field name (InterfaceField) found", jsonObject.has("InterfaceField")); - assertTrue("Forced field not found!", + assertTrue("Forced field found!", jsonObject.has("forcedInt")); - assertTrue("Normally ignored field (getable) with explicit property name not found", + assertTrue("Overridden boolean field name (Getable) found", jsonObject.has("Getable")); } From 08719d4b3a6d73918631297030e8e03cc960de24 Mon Sep 17 00:00:00 2001 From: Alan Wang <948467222@qq.com> Date: Mon, 30 Dec 2019 09:51:08 +0800 Subject: [PATCH 13/15] Apply suggestions from code review Co-Authored-By: Sean Leary --- .../java/org/json/junit/JSONObjectTest.java | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 438e55e..b2f501e 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -593,15 +593,20 @@ public void jsonObjectByBean2() { jsonObject.has("someFloat")); assertFalse("Ignored field not found!", jsonObject.has("ignoredInt")); - assertTrue("Normal field name (someInt) found", + // getSomeInt() has no user-defined annotation + assertTrue("Normal field name (someInt) should have been found", jsonObject.has("someInt")); - assertTrue("Normal field name (someLong) found", + // the user-defined annotation does not replace any value, so someLong should be found + assertTrue("Normal field name (someLong) should have been found", jsonObject.has("someLong")); - assertTrue("Overridden String field name (myStringField) found", + // myStringField replaces someString property name via user-defined annotation + assertTrue("Overridden String field name (myStringField) should have been found", jsonObject.has("myStringField")); - assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) found", + // weird name replaces myDouble property name via user-defined annotation + assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) should have been found", jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); - assertTrue("Overridden String field name (InterfaceField) found", + // InterfaceField replaces someFloat property name via user-defined annotation + assertTrue("Overridden String field name (InterfaceField) should have been found", jsonObject.has("InterfaceField")); } @@ -619,29 +624,39 @@ public void jsonObjectByBean3() { jsonObject.has("someInt")); assertFalse("Normal field name (myDouble) processing did not work", jsonObject.has("myDouble")); - assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not FOUND!", + // myDouble was replaced by weird name, and then replaced again by AMoreNormalName via user-defined annotation + assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) should not be FOUND!", jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); assertFalse("Normal field name (someFloat) found, but was overridden", jsonObject.has("someFloat")); assertFalse("Ignored field found! but was overridden", jsonObject.has("ignoredInt")); - assertFalse("Ignored field at the same level as forced name not found", + // shouldNotBeJSON property name was first ignored, then replaced by ShouldBeIgnored via user-defined annotations + assertFalse("Ignored field at the same level as forced name should not have been found", jsonObject.has("ShouldBeIgnored")); - assertFalse("Normally ignored field (able) with explicit property name not found", + // able property name was replaced by Getable via user-defined annotation + assertFalse("Normally ignored field (able) with explicit property name should not have been found", jsonObject.has("able")); - assertTrue("Overridden int field name (newIntFieldName) found", + // property name someInt was replaced by newIntFieldName via user-defined annotation + assertTrue("Overridden int field name (newIntFieldName) should have been found", jsonObject.has("newIntFieldName")); - assertTrue("Normal field name (someLong) found", + // property name someLong was not replaced via user-defined annotation + assertTrue("Normal field name (someLong) should have been found", jsonObject.has("someLong")); - assertTrue("Overridden String field name (myStringField) found", + // property name someString was replaced by myStringField via user-defined annotation + assertTrue("Overridden String field name (myStringField) should have been found", jsonObject.has("myStringField")); - assertTrue("Overridden double field name (AMoreNormalName) found", + // property name myDouble was replaced by a weird name, followed by AMoreNormalName via user-defined annotations + assertTrue("Overridden double field name (AMoreNormalName) should have been found", jsonObject.has("AMoreNormalName")); - assertTrue("Overridden String field name (InterfaceField) found", + // property name someFloat was replaced by InterfaceField via user-defined annotation + assertTrue("Overridden String field name (InterfaceField) should have been found", jsonObject.has("InterfaceField")); - assertTrue("Forced field found!", + // property name ignoredInt was replaced by none, followed by forcedInt via user-defined annotations + assertTrue("Forced field should have been found!", jsonObject.has("forcedInt")); - assertTrue("Overridden boolean field name (Getable) found", + // property name able was replaced by Getable via user-defined annotation + assertTrue("Overridden boolean field name (Getable) should have been found", jsonObject.has("Getable")); } From cd24543e6decdf360adf6bd53ee06fc12a4294c7 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 23 May 2020 10:17:29 -0500 Subject: [PATCH 14/15] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index e6e61b5..f27ec95 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # JSON-Java-unit-test + +# This project is no longer the source of truth for JSON-Java unit tests and it not accepting pull requests. The unit tests have been ported to [https://github.com/stleary/JSON-java](https://github.com/stleary/JSON-java) so that code and tests reside together. If this move works out, then future unit test changes will be performed there. + Unit tests to validate the JSON-Java GitHub project code
https://github.com/stleary/JSON-java
From 37294c45c1d61c224b6cb6b2b74a46b14b23eb11 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Wed, 29 Jul 2020 00:35:01 -0500 Subject: [PATCH 15/15] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f27ec95..63d0fd7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # JSON-Java-unit-test -# This project is no longer the source of truth for JSON-Java unit tests and it not accepting pull requests. The unit tests have been ported to [https://github.com/stleary/JSON-java](https://github.com/stleary/JSON-java) so that code and tests reside together. If this move works out, then future unit test changes will be performed there. +# This project is no longer accepting pull requests. It is not the source of truth for JSON-Java unit tests. The unit tests have been ported to [https://github.com/stleary/JSON-java](https://github.com/stleary/JSON-java) so that code and tests reside together. Please submit all pull requests to the new location. Unit tests to validate the JSON-Java GitHub project code