Skip to content

Commit

Permalink
Test for new introduced JSONArray(int) constructor.
Browse files Browse the repository at this point in the history
Link: stleary/JSON-java#516

1. Checked with input parameter: 0
2. Checked with input parameter: positive number
3. Checked with input parameter: negative number

If input parameter is negative number JSONException is thrown:
    JSONArray initial capacity cannot be negative.
  • Loading branch information
viveksacademia4git committed May 16, 2020
1 parent f07ddd9 commit 21dc047
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;

Expand Down Expand Up @@ -1070,4 +1071,21 @@ public void toList() {
assertTrue("Removing an entry should succeed", list.remove(2) != null);
assertTrue("List should have 2 elements", list.size() == 2);
}

/**
* Attempt to create a JSONArray with an unclosed array.
* Expects an exception
*/
@Test
public void testJSONArrayInt() {
assertNotNull(new JSONArray(0));
assertNotNull(new JSONArray(10));
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());
}
}
}

0 comments on commit 21dc047

Please sign in to comment.