From dfa37a298f019516224fe1b563a4fec66253e103 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 30 Oct 2017 08:09:42 -0400 Subject: [PATCH] Add more tests for unclosed arrays --- .../java/org/json/junit/JSONArrayTest.java | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 442fb0a..4a0249d 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -30,7 +30,7 @@ import com.jayway.jsonpath.JsonPath; * Tests for JSON-Java JSONArray.java */ public class JSONArrayTest { - String arrayStr = + private final String arrayStr = "["+ "true,"+ "false,"+ @@ -94,6 +94,36 @@ public class JSONArrayTest { e.getMessage()); } } + + /** + * Attempt to create a JSONArray with an unclosed array. + * Expects an exception + */ + @Test + public void unclosedArray2() { + try { + assertNull("Should throw an exception", new JSONArray("[\"test\"")); + } catch (JSONException e) { + assertEquals("Expected an exception message", + "Expected a ',' or ']' at 7 [character 8 line 1]", + e.getMessage()); + } + } + + /** + * Attempt to create a JSONArray with an unclosed array. + * Expects an exception + */ + @Test + public void unclosedArray3() { + try { + assertNull("Should throw an exception", new JSONArray("[\"test\",")); + } catch (JSONException e) { + assertEquals("Expected an exception message", + "Expected a ',' or ']' at 8 [character 9 line 1]", + e.getMessage()); + } + } /** * Attempt to create a JSONArray with a string as object that is @@ -372,7 +402,7 @@ public class JSONArrayTest { assertTrue("expected empty JSONArray length 0", new JSONArray().length() == 0); JSONArray jsonArray = new JSONArray(this.arrayStr); - assertTrue("expected JSONArray length 13", jsonArray.length() == 13); + assertTrue("expected JSONArray length 13. instead found "+jsonArray.length(), jsonArray.length() == 13); JSONArray nestedJsonArray = jsonArray.getJSONArray(9); assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1); }