@@ -299,15 +299,6 @@ public void jsonObjectByBean() {
299299 when (myBean .isFalseKey ()).thenReturn (false );
300300 when (myBean .getStringReaderKey ()).thenReturn (
301301 new StringReader ("" ) {
302- /**
303- * TODO: Need to understand why returning a string
304- * turns "this" into an empty JSONObject,
305- * but not overriding turns "this" into a string.
306- */
307- @ Override
308- public String toString (){
309- return "Whatever" ;
310- }
311302 });
312303
313304 JSONObject jsonObject = new JSONObject (myBean );
@@ -349,11 +340,11 @@ public void jsonObjectByObjectAndNames() {
349340 }
350341
351342 /**
352- * Exercise the JSONObject from resource bundle functionality
343+ * Exercise the JSONObject from resource bundle functionality.
344+ * The test resource bundle is uncomplicated, but provides adequate test coverage.
353345 */
354346 @ Test
355347 public void jsonObjectByResourceBundle () {
356- // TODO: how to improve resource bundle testing?
357348 JSONObject jsonObject = new
358349 JSONObject ("org.json.junit.StringsResourceBundle" ,
359350 Locale .getDefault ());
@@ -374,7 +365,6 @@ public void jsonObjectByResourceBundle() {
374365 */
375366 @ Test
376367 public void jsonObjectAccumulate () {
377- // TODO: should include an unsupported object
378368
379369 JSONObject jsonObject = new JSONObject ();
380370 jsonObject .accumulate ("myArray" , true );
@@ -383,6 +373,11 @@ public void jsonObjectAccumulate() {
383373 jsonObject .accumulate ("myArray" , "h\b e\t llo w\u1234 orld!" );
384374 jsonObject .accumulate ("myArray" , 42 );
385375 jsonObject .accumulate ("myArray" , -23.45e7 );
376+ // include an unsupported object for coverage
377+ try {
378+ jsonObject .accumulate ("myArray" , Double .NaN );
379+ assertTrue ("Expected exception" , false );
380+ } catch (JSONException ignored ) {}
386381
387382 // validate JSON
388383 Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
@@ -401,14 +396,18 @@ public void jsonObjectAccumulate() {
401396 */
402397 @ Test
403398 public void jsonObjectAppend () {
404- // TODO: should include an unsupported object
405399 JSONObject jsonObject = new JSONObject ();
406400 jsonObject .append ("myArray" , true );
407401 jsonObject .append ("myArray" , false );
408402 jsonObject .append ("myArray" , "hello world!" );
409403 jsonObject .append ("myArray" , "h\b e\t llo w\u1234 orld!" );
410404 jsonObject .append ("myArray" , 42 );
411405 jsonObject .append ("myArray" , -23.45e7 );
406+ // include an unsupported object for coverage
407+ try {
408+ jsonObject .append ("myArray" , Double .NaN );
409+ assertTrue ("Expected exception" , false );
410+ } catch (JSONException ignored ) {}
412411
413412 // validate JSON
414413 Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
@@ -818,34 +817,49 @@ public void bigNumberOperations() {
818817 /**
819818 * JSONObject put(String, Object) method stores and serializes
820819 * bigInt and bigDec correctly. Nothing needs to change.
821- * TODO: New methods
822- * get|optBigInteger|BigDecimal() should work like other supported
823- * objects. Uncomment the get/opt methods after JSONObject is updated.
824820 */
825821 jsonObject = new JSONObject ();
826822 jsonObject .put ("bigInt" , bigInteger );
827823 assertTrue ("jsonObject.put() handles bigInt correctly" ,
828824 jsonObject .get ("bigInt" ).equals (bigInteger ));
829- // assertTrue("jsonObject.getBigInteger() handles bigInt correctly",
830- // jsonObject.getBigInteger("bigInt").equals(bigInteger));
831- // assertTrue("jsonObject.optBigInteger() handles bigInt correctly",
832- // jsonObject.optBigInteger("bigInt", BigInteger.ONE).equals(bigInteger));
825+ assertTrue ("jsonObject.getBigInteger() handles bigInt correctly" ,
826+ jsonObject .getBigInteger ("bigInt" ).equals (bigInteger ));
827+ assertTrue ("jsonObject.optBigInteger() handles bigInt correctly" ,
828+ jsonObject .optBigInteger ("bigInt" , BigInteger .ONE ).equals (bigInteger ));
833829 assertTrue ("jsonObject serializes bigInt correctly" ,
834830 jsonObject .toString ().equals ("{\" bigInt\" :123456789012345678901234567890}" ));
835831 jsonObject = new JSONObject ();
836832 jsonObject .put ("bigDec" , bigDecimal );
837833 assertTrue ("jsonObject.put() handles bigDec correctly" ,
838834 jsonObject .get ("bigDec" ).equals (bigDecimal ));
839- // assertTrue("jsonObject.getBigDecimal() handles bigDec correctly",
840- // jsonObject.getBigDecimal("bigDec").equals(bigDecimal));
841- // assertTrue("jsonObject.optBigDecimal() handles bigDec correctly",
842- // jsonObject.optBigDecimal("bigDec", BigDecimal.ONE).equals(bigDecimal));
835+ assertTrue ("jsonObject.getBigDecimal() handles bigDec correctly" ,
836+ jsonObject .getBigDecimal ("bigDec" ).equals (bigDecimal ));
837+ assertTrue ("jsonObject.optBigDecimal() handles bigDec correctly" ,
838+ jsonObject .optBigDecimal ("bigDec" , BigDecimal .ONE ).equals (bigDecimal ));
843839 assertTrue ("jsonObject serializes bigDec correctly" ,
844840 jsonObject .toString ().equals (
845841 "{\" bigDec\" :123456789012345678901234567890.12345678901234567890123456789}" ));
846842
847- JSONArray jsonArray = new JSONArray ();
848-
843+ /**
844+ * exercise some exceptions
845+ */
846+ try {
847+ jsonObject .getBigDecimal ("bigInt" );
848+ assertTrue ("expected an exeption" , false );
849+ } catch (JSONException ignored ) {}
850+ obj = jsonObject .optBigDecimal ("bigInt" , BigDecimal .ONE );
851+ assertTrue ("expected BigDecimal" , obj .equals (BigDecimal .ONE ));
852+ try {
853+ jsonObject .getBigInteger ("bigDec" );
854+ assertTrue ("expected an exeption" , false );
855+ } catch (JSONException ignored ) {}
856+ jsonObject .put ("stringKey" , "abc" );
857+ try {
858+ jsonObject .getBigDecimal ("stringKey" );
859+ assertTrue ("expected an exeption" , false );
860+ } catch (JSONException ignored ) {}
861+ obj = jsonObject .optBigInteger ("bigDec" , BigInteger .ONE );
862+ assertTrue ("expected BigInteger" , obj .equals (BigInteger .ONE ));
849863
850864 /**
851865 * JSONObject.numberToString() works correctly, nothing to change.
@@ -904,7 +918,7 @@ public void bigNumberOperations() {
904918 actualFromPutStr .equals (
905919 "{\" bigDec\" :123456789012345678901234567890.12345678901234567890123456789}" ));
906920 // bigInt,bigDec put
907- jsonArray = new JSONArray ();
921+ JSONArray jsonArray = new JSONArray ();
908922 jsonArray .put (bigInteger );
909923 jsonArray .put (bigDecimal );
910924 actualFromPutStr = jsonArray .toString ();
@@ -1105,7 +1119,7 @@ public void jsonObjectIncrement() {
11051119 String str =
11061120 "{" +
11071121 "\" keyLong\" :9999999991," +
1108- "\" keyDouble\" :1.1, " +
1122+ "\" keyDouble\" :1.1" +
11091123 "}" ;
11101124 JSONObject jsonObject = new JSONObject (str );
11111125 jsonObject .increment ("keyInt" );
@@ -1115,16 +1129,26 @@ public void jsonObjectIncrement() {
11151129 jsonObject .increment ("keyInt" );
11161130 jsonObject .increment ("keyLong" );
11171131 jsonObject .increment ("keyDouble" );
1132+ /**
1133+ * JSONObject constructor won't handle these types correctly, but
1134+ * adding them via put works.
1135+ */
11181136 jsonObject .put ("keyFloat" , new Float (1.1 ));
1137+ jsonObject .put ("keyBigInt" , new BigInteger ("123456789123456789123456789123456780" ));
1138+ jsonObject .put ("keyBigDec" , new BigDecimal ("123456789123456789123456789123456780.1" ));
11191139 jsonObject .increment ("keyFloat" );
11201140 jsonObject .increment ("keyFloat" );
1141+ jsonObject .increment ("keyBigInt" );
1142+ jsonObject .increment ("keyBigDec" );
11211143
11221144 // validate JSON
11231145 Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
1124- assertTrue ("expected 4 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 4 );
1146+ assertTrue ("expected 6 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 6 );
11251147 assertTrue ("expected 3" , Integer .valueOf (3 ).equals (JsonPath .read (doc , "$.keyInt" )));
11261148 assertTrue ("expected 9999999993" , Long .valueOf (9999999993L ).equals (JsonPath .read (doc , "$.keyLong" )));
11271149 assertTrue ("expected 3.1" , Double .valueOf (3.1 ).equals (JsonPath .read (doc , "$.keyDouble" )));
1150+ assertTrue ("expected 123456789123456789123456789123456781" , new BigInteger ("123456789123456789123456789123456781" ).equals (JsonPath .read (doc , "$.keyBigInt" )));
1151+ assertTrue ("expected 123456789123456789123456789123456781.1" , new BigDecimal ("123456789123456789123456789123456781.1" ).equals (JsonPath .read (doc , "$.keyBigDec" )));
11281152
11291153 /**
11301154 * Should work the same way on any platform! @see https://docs.oracle
@@ -1526,8 +1550,6 @@ public void wrapObject() {
15261550 assertTrue ("expected val1" , "val1" .equals (JsonPath .read (doc , "$.key1" )));
15271551 assertTrue ("expected val2" , "val2" .equals (JsonPath .read (doc , "$.key2" )));
15281552 assertTrue ("expected val3" , "val3" .equals (JsonPath .read (doc , "$.key3" )));
1529-
1530- // TODO test wrap(package)
15311553 }
15321554
15331555 /**
0 commit comments