From d088cf014e42b75b92d932adac1645840548c76d Mon Sep 17 00:00:00 2001 From: vivek Date: Wed, 3 Jun 2020 11:46:48 +0200 Subject: [PATCH] Development for stleary#516 completed with rebased repository - Introduced JSONObject(int) constructor. - int > Initial capacity of the underlying data structure - Test for new introduced JSONArray(int) constructor. 1. Checked with input parameter: 0 2. Checked with input parameter: positive number 3. Checked with positive number input parameter and compared length 4. If input parameter is negative number JSONException is thrown: JSONArray initial capacity cannot be negative. --- src/main/java/org/json/JSONArray.java | 16 ++++++++++++++ .../java/org/json/junit/JSONArrayTest.java | 21 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index d5ad7f495..787e12435 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -205,6 +205,22 @@ public JSONArray(Object array) throws JSONException { } } + /** + * Construct a JSONArray with the specified initial capacity. + * + * @param initialCapacity + * the initial capacity of the JSONArray. + * @throws JSONException + * If the initial capacity is negative. + */ + public JSONArray(int initialCapacity) throws JSONException { + if (initialCapacity < 0) { + throw new JSONException( + "JSONArray initial capacity cannot be negative."); + } + this.myArrayList = new ArrayList(initialCapacity); + } + @Override public Iterator iterator() { return this.myArrayList.iterator(); diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index df4936538..eda3c06bd 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -1119,4 +1120,24 @@ public void toList() { assertTrue("Removing an entry should succeed", list.remove(2) != null); assertTrue("List should have 2 elements", list.size() == 2); } + + /** + * Create a JSONArray with specified initial capacity. + * Expects an exception if the initial capacity is specified as a negative integer + */ + @Test + public void testJSONArrayInt() { + assertNotNull(new JSONArray(0)); + assertNotNull(new JSONArray(5)); + // Check Size -> Even though the capacity of the JSONArray can be specified using a positive + // integer but the length of JSONArray always reflects upon the items added into it. + assertEquals(0l, (long)new JSONArray(10).length()); + try { + assertNotNull("Should throw an exception", new JSONArray(-1)); + } catch (JSONException e) { + assertEquals("Expected an exception message", + "JSONArray initial capacity cannot be negative.", + e.getMessage()); + } + } }