From a2c311527b1961ec8cefc20d8219bc72728bb7ec Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 9 Aug 2016 15:54:06 -0400 Subject: [PATCH 1/3] Updates tests to include all opt methods and verify for missing keys. --- .../java/org/json/junit/JSONArrayTest.java | 5 +- .../java/org/json/junit/JSONObjectTest.java | 48 +++++++++++++++++-- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index ef3a608..6e9fd6a 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -373,7 +373,10 @@ public void opt() { assertTrue("Array opt value out of range", null == jsonArray.opt(-1)); - assertTrue("Array opt boolean", + assertTrue("Array opt value out of range", + null == jsonArray.opt(jsonArray.length())); + + assertTrue("Array opt boolean", Boolean.TRUE == jsonArray.optBoolean(0)); assertTrue("Array opt boolean default", Boolean.FALSE == jsonArray.optBoolean(-1, Boolean.FALSE)); diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 08ec964..61043ae 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1714,20 +1714,28 @@ public void jsonObjectPutOnceNull() { } /** - * Exercise JSONObject opt(key, default) method + * Exercise JSONObject opt(key, default) method. */ @Test public void jsonObjectOptDefault() { - String str = "{\"myKey\": \"myval\"}"; + String str = "{\"myKey\": \"myval\", \"hiKey\": null}"; JSONObject jsonObject = new JSONObject(str); + assertTrue("optBigDecimal() should return default ", + BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); + assertTrue(" should return default ", + BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", - Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE)); - assertTrue("optInt() should return default int", - 42 == jsonObject.optInt("myKey", 42)); + true == jsonObject.optBoolean("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optEnum() should return default ", + MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); + assertTrue("optJSONArray() should return null ", + null==jsonObject.optJSONArray("myKey")); + assertTrue("optJSONObject() should return null ", + null==jsonObject.optJSONObject("myKey")); assertTrue("optLong() should return default long", 42 == jsonObject.optLong("myKey", 42)); assertTrue("optDouble() should return default double", @@ -1736,6 +1744,36 @@ public void jsonObjectOptDefault() { "hi".equals(jsonObject.optString("hiKey", "hi"))); } + /** + * Exercise JSONObject opt(key, default) method when the key doesn't exist. + */ + @Test + public void jsonObjectOptNoKey() { + + JSONObject jsonObject = new JSONObject(); + + assertTrue("optBigDecimal() should return default ", + BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); + assertTrue(" should return default ", + BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); + assertTrue("optBoolean() should return default boolean", + true == jsonObject.optBoolean("myKey", true)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optEnum() should return default ", + MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); + assertTrue("optJSONArray() should return null ", + null==jsonObject.optJSONArray("myKey")); + assertTrue("optJSONObject() should return null ", + null==jsonObject.optJSONObject("myKey")); + assertTrue("optLong() should return default long", + 42 == jsonObject.optLong("myKey", 42)); + assertTrue("optDouble() should return default double", + 42.3 == jsonObject.optDouble("myKey", 42.3)); + assertTrue("optString() should return default string", + "hi".equals(jsonObject.optString("hiKey", "hi"))); + } + /** * Verifies that the opt methods properly convert string values. */ From 80e36eb63c976079eda49cfc070fef5bdde89f0c Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 9 Aug 2016 15:59:27 -0400 Subject: [PATCH 2/3] Fixes error messages --- src/test/java/org/json/junit/JSONObjectTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 61043ae..935b687 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1722,15 +1722,15 @@ public void jsonObjectOptDefault() { String str = "{\"myKey\": \"myval\", \"hiKey\": null}"; JSONObject jsonObject = new JSONObject(str); - assertTrue("optBigDecimal() should return default ", + assertTrue("optBigDecimal() should return default BigDecimal", BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); - assertTrue(" should return default ", + assertTrue("optBigInteger() should return default BigInteger", BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", true == jsonObject.optBoolean("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optEnum() should return default ", + assertTrue("optEnum() should return default Enum", MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); assertTrue("optJSONArray() should return null ", null==jsonObject.optJSONArray("myKey")); @@ -1752,15 +1752,15 @@ public void jsonObjectOptNoKey() { JSONObject jsonObject = new JSONObject(); - assertTrue("optBigDecimal() should return default ", + assertTrue("optBigDecimal() should return default BigDecimal", BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); - assertTrue(" should return default ", + assertTrue("optBigInteger() should return default BigInteger", BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", true == jsonObject.optBoolean("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optEnum() should return default ", + assertTrue("optEnum() should return default Enum", MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); assertTrue("optJSONArray() should return null ", null==jsonObject.optJSONArray("myKey")); From 8bae09f81bda5d12f1ea2d625097cdf1a113d84a Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 9 Aug 2016 16:11:46 -0400 Subject: [PATCH 3/3] removes unnecessary comparison to true --- src/test/java/org/json/junit/JSONObjectTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 935b687..d55f5df 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1727,7 +1727,7 @@ public void jsonObjectOptDefault() { assertTrue("optBigInteger() should return default BigInteger", BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", - true == jsonObject.optBoolean("myKey", true)); + jsonObject.optBoolean("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); assertTrue("optEnum() should return default Enum", @@ -1757,7 +1757,7 @@ public void jsonObjectOptNoKey() { assertTrue("optBigInteger() should return default BigInteger", BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", - true == jsonObject.optBoolean("myKey", true)); + jsonObject.optBoolean("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); assertTrue("optEnum() should return default Enum",