diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 1417e6b..1f7a5c9 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -514,7 +515,7 @@ public class JSONObjectTest { // include an unsupported object for coverage try { jsonObject.accumulate("myArray", Double.NaN); - assertTrue("Expected exception", false); + fail("Expected exception"); } catch (JSONException ignored) {} // validate JSON @@ -545,7 +546,7 @@ public class JSONObjectTest { // include an unsupported object for coverage try { jsonObject.append("myArray", Double.NaN); - assertTrue("Expected exception", false); + fail("Expected exception"); } catch (JSONException ignored) {} // validate JSON @@ -595,6 +596,9 @@ public class JSONObjectTest { "\"longStrKey\":\"987654321098765432\","+ "\"doubleKey\":-23.45e7,"+ "\"doubleStrKey\":\"00001.000\","+ + "\"BigDecimalStrKey\":\"19007199254740993.35481234487103587486413587843213584\","+ + "\"negZeroKey\":-0.0,"+ + "\"negZeroStrKey\":\"-0.0\","+ "\"arrayKey\":[0,1,2],"+ "\"objectKey\":{\"myKey\":\"myVal\"}"+ "}"; @@ -611,10 +615,26 @@ public class JSONObjectTest { jsonObject.getDouble("doubleKey") == -23.45e7); assertTrue("doubleStrKey should be double", jsonObject.getDouble("doubleStrKey") == 1); + assertTrue("doubleKey can be float", + jsonObject.getFloat("doubleKey") == -23.45e7f); + assertTrue("doubleStrKey can be float", + jsonObject.getFloat("doubleStrKey") == 1f); assertTrue("opt doubleKey should be double", jsonObject.optDouble("doubleKey") == -23.45e7); assertTrue("opt doubleKey with Default should be double", jsonObject.optDouble("doubleStrKey", Double.NaN) == 1); + assertTrue("opt negZeroKey should be double", + Double.compare(jsonObject.optDouble("negZeroKey"), -0.0d) == 0); + assertTrue("opt negZeroStrKey with Default should be double", + Double.compare(jsonObject.optDouble("negZeroStrKey"), -0.0d) == 0); + assertTrue("optNumber negZeroKey should return Double", + jsonObject.optNumber("negZeroKey") instanceof Double); + assertTrue("optNumber negZeroStrKey should return Double", + jsonObject.optNumber("negZeroStrKey") instanceof Double); + assertTrue("optNumber negZeroKey should be -0.0", + Double.compare(jsonObject.optNumber("negZeroKey").doubleValue(), -0.0d) == 0); + assertTrue("optNumber negZeroStrKey should be -0.0", + Double.compare(jsonObject.optNumber("negZeroStrKey").doubleValue(), -0.0d) == 0); assertTrue("optFloat doubleKey should be float", jsonObject.optFloat("doubleKey") == -23.45e7f); assertTrue("optFloat doubleKey with Default should be float", @@ -641,12 +661,14 @@ public class JSONObjectTest { jsonObject.optNumber("longKey") instanceof Long); assertTrue("optNumber double should return Double", jsonObject.optNumber("doubleKey") instanceof Double); - assertTrue("optNumber Str int should return BigDecimal", - jsonObject.optNumber("intStrKey") instanceof BigDecimal); - assertTrue("optNumber Str long should return BigDecimal", - jsonObject.optNumber("longStrKey") instanceof BigDecimal); - assertTrue("optNumber Str double should return BigDecimal", - jsonObject.optNumber("doubleStrKey") instanceof BigDecimal); + assertTrue("optNumber Str int should return Integer", + jsonObject.optNumber("intStrKey") instanceof Integer); + assertTrue("optNumber Str long should return Long", + jsonObject.optNumber("longStrKey") instanceof Long); + assertTrue("optNumber Str double should return Double", + jsonObject.optNumber("doubleStrKey") instanceof Double); + assertTrue("optNumber BigDecimalStrKey should return BigDecimal", + jsonObject.optNumber("BigDecimalStrKey") instanceof BigDecimal); assertTrue("xKey should not exist", jsonObject.isNull("xKey")); assertTrue("stringKey should exist", @@ -804,14 +826,14 @@ public class JSONObjectTest { JSONObject jsonObject = new JSONObject(str); try { jsonObject.getBoolean("nonKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("expecting an exception message", "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); } try { jsonObject.getBoolean("stringKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"stringKey\"] is not a Boolean.". @@ -819,7 +841,7 @@ public class JSONObjectTest { } try { jsonObject.getString("nonKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"nonKey\"] not found.". @@ -827,7 +849,7 @@ public class JSONObjectTest { } try { jsonObject.getString("trueKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"trueKey\"] not a string.". @@ -835,7 +857,7 @@ public class JSONObjectTest { } try { jsonObject.getDouble("nonKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"nonKey\"] not found.". @@ -843,7 +865,23 @@ public class JSONObjectTest { } try { jsonObject.getDouble("stringKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.". + equals(e.getMessage())); + } + try { + jsonObject.getFloat("nonKey"); + fail("Expected an exception"); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } + try { + jsonObject.getFloat("stringKey"); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"stringKey\"] is not a number.". @@ -851,7 +889,7 @@ public class JSONObjectTest { } try { jsonObject.getInt("nonKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"nonKey\"] not found.". @@ -859,7 +897,7 @@ public class JSONObjectTest { } try { jsonObject.getInt("stringKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"stringKey\"] is not an int.". @@ -867,7 +905,7 @@ public class JSONObjectTest { } try { jsonObject.getLong("nonKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"nonKey\"] not found.". @@ -875,7 +913,7 @@ public class JSONObjectTest { } try { jsonObject.getLong("stringKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"stringKey\"] is not a long.". @@ -883,7 +921,7 @@ public class JSONObjectTest { } try { jsonObject.getJSONArray("nonKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"nonKey\"] not found.". @@ -891,7 +929,7 @@ public class JSONObjectTest { } try { jsonObject.getJSONArray("stringKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"stringKey\"] is not a JSONArray.". @@ -899,7 +937,7 @@ public class JSONObjectTest { } try { jsonObject.getJSONObject("nonKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"nonKey\"] not found.". @@ -907,7 +945,7 @@ public class JSONObjectTest { } try { jsonObject.getJSONObject("stringKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[\"stringKey\"] is not a JSONObject.". @@ -1004,18 +1042,18 @@ public class JSONObjectTest { */ try { jsonObject.getBigDecimal("bigInt"); - assertTrue("expected an exeption", false); + fail("expected an exeption"); } catch (JSONException ignored) {} obj = jsonObject.optBigDecimal("bigInt", BigDecimal.ONE); assertTrue("expected BigDecimal", obj.equals(BigDecimal.ONE)); try { jsonObject.getBigInteger("bigDec"); - assertTrue("expected an exeption", false); + fail("expected an exeption"); } catch (JSONException ignored) {} jsonObject.put("stringKey", "abc"); try { jsonObject.getBigDecimal("stringKey"); - assertTrue("expected an exeption", false); + fail("expected an exeption"); } catch (JSONException ignored) {} obj = jsonObject.optBigInteger("bigDec", BigInteger.ONE); assertTrue("expected BigInteger", obj instanceof BigInteger); @@ -1092,11 +1130,11 @@ public class JSONObjectTest { jsonArray.put(Boolean.TRUE); try { jsonArray.getBigInteger(2); - assertTrue("should not be able to get big int", false); + fail("should not be able to get big int"); } catch (Exception ignored) {} try { jsonArray.getBigDecimal(2); - assertTrue("should not be able to get big dec", false); + fail("should not be able to get big dec"); } catch (Exception ignored) {} assertTrue("optBigInt is default", jsonArray.optBigInteger(2, BigInteger.ONE).equals(BigInteger.ONE)); assertTrue("optBigDec is default", jsonArray.optBigDecimal(2, BigDecimal.ONE).equals(BigDecimal.ONE)); @@ -1851,7 +1889,7 @@ public class JSONObjectTest { String str = "{\"myKey\":true, \"myOtherKey\":false}"; JSONObject jsonObject = new JSONObject(str); jsonObject.append("myKey", "hello"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "JSONObject[myKey] is not a JSONArray.". @@ -1862,7 +1900,7 @@ public class JSONObjectTest { String str = "{\"myKey\":true, \"myOtherKey\":false}"; JSONObject jsonObject = new JSONObject(str); jsonObject.increment("myKey"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "Unable to increment [\"myKey\"].". @@ -1873,7 +1911,7 @@ public class JSONObjectTest { String str = "{\"myKey\":true, \"myOtherKey\":false}"; JSONObject jsonObject = new JSONObject(str); jsonObject.get(null); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "Null key.". @@ -1882,7 +1920,7 @@ public class JSONObjectTest { try { // invalid numberToString() JSONObject.numberToString((Number)null); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("Expecting an exception message", "Null pointer". @@ -1892,7 +1930,7 @@ public class JSONObjectTest { // null put key JSONObject jsonObject = new JSONObject("{}"); jsonObject.put(null, 0); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (NullPointerException ignored) { } try { @@ -1900,21 +1938,21 @@ public class JSONObjectTest { JSONObject jsonObject = new JSONObject("{}"); jsonObject.putOnce("hello", "world"); jsonObject.putOnce("hello", "world!"); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("", true); } try { // test validity of invalid double JSONObject.testValidity(Double.NaN); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("", true); } try { // test validity of invalid float JSONObject.testValidity(Float.NEGATIVE_INFINITY); - assertTrue("Expected an exception", false); + fail("Expected an exception"); } catch (JSONException e) { assertTrue("", true); } @@ -2294,7 +2332,7 @@ public class JSONObjectTest { // assertTrue("should convert null to empty string", "".equals(string)); try { value = jsonObjectNull.get("key"); - assertTrue("get() null should throw exception", false); + fail("get() null should throw exception"); } catch (Exception ignored) {} /**