@@ -1714,20 +1714,28 @@ public void jsonObjectPutOnceNull() {
17141714 }
17151715
17161716 /**
1717- * Exercise JSONObject opt(key, default) method
1717+ * Exercise JSONObject opt(key, default) method.
17181718 */
17191719 @ Test
17201720 public void jsonObjectOptDefault () {
17211721
1722- String str = "{\" myKey\" : \" myval\" }" ;
1722+ String str = "{\" myKey\" : \" myval\" , \" hiKey \" : null }" ;
17231723 JSONObject jsonObject = new JSONObject (str );
17241724
1725+ assertTrue ("optBigDecimal() should return default BigDecimal" ,
1726+ BigDecimal .TEN .compareTo (jsonObject .optBigDecimal ("myKey" , BigDecimal .TEN ))==0 );
1727+ assertTrue ("optBigInteger() should return default BigInteger" ,
1728+ BigInteger .TEN .compareTo (jsonObject .optBigInteger ("myKey" ,BigInteger .TEN ))==0 );
17251729 assertTrue ("optBoolean() should return default boolean" ,
1726- Boolean .TRUE == jsonObject .optBoolean ("myKey" , Boolean .TRUE ));
1727- assertTrue ("optInt() should return default int" ,
1728- 42 == jsonObject .optInt ("myKey" , 42 ));
1730+ jsonObject .optBoolean ("myKey" , true ));
17291731 assertTrue ("optInt() should return default int" ,
17301732 42 == jsonObject .optInt ("myKey" , 42 ));
1733+ assertTrue ("optEnum() should return default Enum" ,
1734+ MyEnum .VAL1 .equals (jsonObject .optEnum (MyEnum .class , "myKey" , MyEnum .VAL1 )));
1735+ assertTrue ("optJSONArray() should return null " ,
1736+ null ==jsonObject .optJSONArray ("myKey" ));
1737+ assertTrue ("optJSONObject() should return null " ,
1738+ null ==jsonObject .optJSONObject ("myKey" ));
17311739 assertTrue ("optLong() should return default long" ,
17321740 42 == jsonObject .optLong ("myKey" , 42 ));
17331741 assertTrue ("optDouble() should return default double" ,
@@ -1736,6 +1744,36 @@ public void jsonObjectOptDefault() {
17361744 "hi" .equals (jsonObject .optString ("hiKey" , "hi" )));
17371745 }
17381746
1747+ /**
1748+ * Exercise JSONObject opt(key, default) method when the key doesn't exist.
1749+ */
1750+ @ Test
1751+ public void jsonObjectOptNoKey () {
1752+
1753+ JSONObject jsonObject = new JSONObject ();
1754+
1755+ assertTrue ("optBigDecimal() should return default BigDecimal" ,
1756+ BigDecimal .TEN .compareTo (jsonObject .optBigDecimal ("myKey" , BigDecimal .TEN ))==0 );
1757+ assertTrue ("optBigInteger() should return default BigInteger" ,
1758+ BigInteger .TEN .compareTo (jsonObject .optBigInteger ("myKey" ,BigInteger .TEN ))==0 );
1759+ assertTrue ("optBoolean() should return default boolean" ,
1760+ jsonObject .optBoolean ("myKey" , true ));
1761+ assertTrue ("optInt() should return default int" ,
1762+ 42 == jsonObject .optInt ("myKey" , 42 ));
1763+ assertTrue ("optEnum() should return default Enum" ,
1764+ MyEnum .VAL1 .equals (jsonObject .optEnum (MyEnum .class , "myKey" , MyEnum .VAL1 )));
1765+ assertTrue ("optJSONArray() should return null " ,
1766+ null ==jsonObject .optJSONArray ("myKey" ));
1767+ assertTrue ("optJSONObject() should return null " ,
1768+ null ==jsonObject .optJSONObject ("myKey" ));
1769+ assertTrue ("optLong() should return default long" ,
1770+ 42 == jsonObject .optLong ("myKey" , 42 ));
1771+ assertTrue ("optDouble() should return default double" ,
1772+ 42.3 == jsonObject .optDouble ("myKey" , 42.3 ));
1773+ assertTrue ("optString() should return default string" ,
1774+ "hi" .equals (jsonObject .optString ("hiKey" , "hi" )));
1775+ }
1776+
17391777 /**
17401778 * Verifies that the opt methods properly convert string values.
17411779 */
0 commit comments