Skip to content

Commit a418d07

Browse files
authored
Merge pull request #55 from johnjaylward/verifyOptMissingKeys
Updates tests to include all opt methods and verify for missing keys.
2 parents 62524b5 + 8bae09f commit a418d07

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,10 @@ public void opt() {
373373
assertTrue("Array opt value out of range",
374374
null == jsonArray.opt(-1));
375375

376-
assertTrue("Array opt boolean",
376+
assertTrue("Array opt value out of range",
377+
null == jsonArray.opt(jsonArray.length()));
378+
379+
assertTrue("Array opt boolean",
377380
Boolean.TRUE == jsonArray.optBoolean(0));
378381
assertTrue("Array opt boolean default",
379382
Boolean.FALSE == jsonArray.optBoolean(-1, Boolean.FALSE));

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)