@@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal
2626
2727import static org .junit .Assert .assertEquals ;
2828import static org .junit .Assert .assertFalse ;
29+ import static org .junit .Assert .assertNotNull ;
2930import static org .junit .Assert .assertNull ;
3031import static org .junit .Assert .assertTrue ;
3132
@@ -1119,4 +1120,24 @@ public void toList() {
11191120 assertTrue ("Removing an entry should succeed" , list .remove (2 ) != null );
11201121 assertTrue ("List should have 2 elements" , list .size () == 2 );
11211122 }
1123+
1124+ /**
1125+ * Create a JSONArray with specified initial capacity.
1126+ * Expects an exception if the initial capacity is specified as a negative integer
1127+ */
1128+ @ Test
1129+ public void testJSONArrayInt () {
1130+ assertNotNull (new JSONArray (0 ));
1131+ assertNotNull (new JSONArray (5 ));
1132+ // Check Size -> Even though the capacity of the JSONArray can be specified using a positive
1133+ // integer but the length of JSONArray always reflects upon the items added into it.
1134+ assertEquals (0l , (long )new JSONArray (10 ).length ());
1135+ try {
1136+ assertNotNull ("Should throw an exception" , new JSONArray (-1 ));
1137+ } catch (JSONException e ) {
1138+ assertEquals ("Expected an exception message" ,
1139+ "JSONArray initial capacity cannot be negative." ,
1140+ e .getMessage ());
1141+ }
1142+ }
11221143}
0 commit comments