File tree Expand file tree Collapse file tree
src/test/java/org/json/junit Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1254,4 +1254,19 @@ public void testJSONArrayPutAll() {
12541254 assertEquals ("index " + i + " are equal" , a1 .get (i ), a2 .get (i ));
12551255 }
12561256 }
1257+
1258+ /**
1259+ * Tests if calling JSONArray clear() method actually makes the JSONArray empty
1260+ */
1261+ @ Test (expected = JSONException .class )
1262+ public void jsonArrayClearMethodTest () {
1263+ //Adds random stuff to the JSONArray
1264+ JSONArray jsonArray = new JSONArray ();
1265+ jsonArray .put (123 );
1266+ jsonArray .put ("456" );
1267+ jsonArray .put (new JSONArray ());
1268+ jsonArray .clear (); //Clears the JSONArray
1269+ assertTrue ("expected jsonArray.length() == 0" , jsonArray .length () == 0 ); //Check if its length is 0
1270+ jsonArray .getInt (0 ); //Should throws org.json.JSONException: JSONArray[0] not found
1271+ }
12571272}
Original file line number Diff line number Diff line change @@ -3215,4 +3215,19 @@ public void testIssue548ObjectWithEmptyJsonArray() {
32153215 assertNotNull ("'empty_json_array' should be an array" , jsonObject .getJSONArray ("empty_json_array" ));
32163216 assertEquals ("'empty_json_array' should have a length of 0" , 0 , jsonObject .getJSONArray ("empty_json_array" ).length ());
32173217 }
3218+
3219+ /**
3220+ * Tests if calling JSONObject clear() method actually makes the JSONObject empty
3221+ */
3222+ @ Test (expected = JSONException .class )
3223+ public void jsonObjectClearMethodTest () {
3224+ //Adds random stuff to the JSONObject
3225+ JSONObject jsonObject = new JSONObject ();
3226+ jsonObject .put ("key1" , 123 );
3227+ jsonObject .put ("key2" , "456" );
3228+ jsonObject .put ("key3" , new JSONObject ());
3229+ jsonObject .clear (); //Clears the JSONObject
3230+ assertTrue ("expected jsonObject.length() == 0" , jsonObject .length () == 0 ); //Check if its length is 0
3231+ jsonObject .getInt ("key1" ); //Should throws org.json.JSONException: JSONObject["asd"] not found
3232+ }
32183233}
You can’t perform that action at this time.
0 commit comments