@@ -1714,20 +1714,28 @@ public void jsonObjectPutOnceNull() {
1714
1714
}
1715
1715
1716
1716
/**
1717
- * Exercise JSONObject opt(key, default) method
1717
+ * Exercise JSONObject opt(key, default) method.
1718
1718
*/
1719
1719
@ Test
1720
1720
public void jsonObjectOptDefault () {
1721
1721
1722
- String str = "{\" myKey\" : \" myval\" }" ;
1722
+ String str = "{\" myKey\" : \" myval\" , \" hiKey \" : null }" ;
1723
1723
JSONObject jsonObject = new JSONObject (str );
1724
1724
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 );
1725
1729
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 ));
1729
1731
assertTrue ("optInt() should return default int" ,
1730
1732
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" ));
1731
1739
assertTrue ("optLong() should return default long" ,
1732
1740
42 == jsonObject .optLong ("myKey" , 42 ));
1733
1741
assertTrue ("optDouble() should return default double" ,
@@ -1736,6 +1744,36 @@ public void jsonObjectOptDefault() {
1736
1744
"hi" .equals (jsonObject .optString ("hiKey" , "hi" )));
1737
1745
}
1738
1746
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
+
1739
1777
/**
1740
1778
* Verifies that the opt methods properly convert string values.
1741
1779
*/
0 commit comments