From c2de22471172bcc1e3c328c0a171c0155fc98e06 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Fri, 8 Jul 2016 16:58:58 -0400 Subject: [PATCH] Verify opt method conversions for JSONArray and JSONObject --- src/test/java/org/json/junit/JSONArrayTest.java | 16 ++++++++++++++++ src/test/java/org/json/junit/JSONObjectTest.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 7643ee0..ef3a608 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -3,6 +3,8 @@ package org.json.junit; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.math.BigDecimal; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -414,6 +416,20 @@ public class JSONArrayTest { assertTrue("Array opt string default implicit", "".equals(jsonArray.optString(-1))); } + + /** + * Verifies that the opt methods properly convert string values. + */ + @Test + public void optStringConversion(){ + JSONArray ja = new JSONArray("[\"123\",\"true\",\"false\"]"); + assertTrue("unexpected optBoolean value",ja.optBoolean(1,false)==true); + assertTrue("unexpected optBoolean value",ja.optBoolean(2,true)==false); + assertTrue("unexpected optInt value",ja.optInt(0,0)==123); + assertTrue("unexpected optLong value",ja.optLong(0,0)==123); + assertTrue("unexpected optDouble value",ja.optDouble(0,0.0)==123.0); + assertTrue("unexpected optBigInteger value",ja.optBigInteger(0,BigInteger.ZERO).compareTo(new BigInteger("123"))==0); + assertTrue("unexpected optBigDecimal value",ja.optBigDecimal(0,BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); } /** * Exercise the JSONArray.put(value) method with various parameters diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index d550139..08ec964 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1735,6 +1735,22 @@ public class JSONObjectTest { assertTrue("optString() should return default string", "hi".equals(jsonObject.optString("hiKey", "hi"))); } + + /** + * Verifies that the opt methods properly convert string values. + */ + @Test + public void jsonObjectOptStringConversion() { + JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\"}"); + assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true); + assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false); + assertTrue("unexpected optInt value",jo.optInt("int",0)==123); + assertTrue("unexpected optLong value",jo.optLong("int",0)==123); + assertTrue("unexpected optDouble value",jo.optDouble("int",0.0)==123.0); + assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0); + assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); + + } /** * Confirm behavior when JSONObject put(key, null object) is called