From c5e4b91fa410687205205032a9b903824790f733 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 8 Jun 2017 02:25:59 -0400 Subject: [PATCH] Updates tests for better error handling changes --- src/test/java/org/json/junit/EnumTest.java | 14 +- .../java/org/json/junit/JSONObjectTest.java | 124 +++++++++++++++++- .../java/org/json/junit/JSONStringTest.java | 2 +- 3 files changed, 134 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/json/junit/EnumTest.java b/src/test/java/org/json/junit/EnumTest.java index 6b97107..53ac303 100644 --- a/src/test/java/org/json/junit/EnumTest.java +++ b/src/test/java/org/json/junit/EnumTest.java @@ -1,5 +1,7 @@ package org.json.junit; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.EnumSet; @@ -325,6 +327,7 @@ public class EnumTest { JSONObject jsonObject = new JSONObject(); jsonObject.put("strKey", "value"); + jsonObject.put("strKey2", "VAL1"); jsonObject.put("enumKey", myEnumField); jsonObject.put("enumClassKey", myEnumClass); @@ -360,11 +363,18 @@ public class EnumTest { // opt with default the wrong value actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey", null); - assertTrue("opt null", actualEnum == null); + assertNull("opt null", actualEnum); + + // opt with default the string value + actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey2", null); + assertEquals(MyEnumField.VAL1, actualEnum); // opt with default an index that does not exist actualEnum = jsonObject.optEnum(MyEnumField.class, "noKey", null); - assertTrue("opt null", actualEnum == null); + assertNull("opt null", actualEnum); + + assertNull("Expected Null when the enum class is null", + jsonObject.optEnum(null, "enumKey")); /** * Exercise the proposed enum API methods on JSONArray diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index b2d1362..ebd1cd1 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2010,6 +2010,8 @@ public class JSONObjectTest { public void jsonObjectOptNoKey() { JSONObject jsonObject = new JSONObject(); + + assertNull(jsonObject.opt(null)); assertTrue("optBigDecimal() should return default BigDecimal", BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); @@ -2088,6 +2090,46 @@ public class JSONObjectTest { assertEquals(19007199254740992l, (long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")); assertEquals(2147483647, (int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")); } + + /** + * Verifies that the optBigDecimal method properly converts values to BigDecimal and coerce them consistently. + */ + @Test + public void jsonObjectOptBigDecimal() { + JSONObject jo = new JSONObject().put("int", 123).put("long", 654L) + .put("float", 1.234f).put("double", 2.345d) + .put("bigInteger", new BigInteger("1234")) + .put("bigDecimal", new BigDecimal("1234.56789")) + .put("nullVal", JSONObject.NULL); + + assertEquals(new BigDecimal("123"),jo.optBigDecimal("int", null)); + assertEquals(new BigDecimal("654"),jo.optBigDecimal("long", null)); + assertEquals(new BigDecimal(1.234f),jo.optBigDecimal("float", null)); + assertEquals(new BigDecimal(2.345d),jo.optBigDecimal("double", null)); + assertEquals(new BigDecimal("1234"),jo.optBigDecimal("bigInteger", null)); + assertEquals(new BigDecimal("1234.56789"),jo.optBigDecimal("bigDecimal", null)); + assertNull(jo.optBigDecimal("nullVal", null)); + } + + /** + * Verifies that the optBigDecimal method properly converts values to BigDecimal and coerce them consistently. + */ + @Test + public void jsonObjectOptBigInteger() { + JSONObject jo = new JSONObject().put("int", 123).put("long", 654L) + .put("float", 1.234f).put("double", 2.345d) + .put("bigInteger", new BigInteger("1234")) + .put("bigDecimal", new BigDecimal("1234.56789")) + .put("nullVal", JSONObject.NULL); + + assertEquals(new BigInteger("123"),jo.optBigInteger("int", null)); + assertEquals(new BigInteger("654"),jo.optBigInteger("long", null)); + assertEquals(new BigInteger("1"),jo.optBigInteger("float", null)); + assertEquals(new BigInteger("2"),jo.optBigInteger("double", null)); + assertEquals(new BigInteger("1234"),jo.optBigInteger("bigInteger", null)); + assertEquals(new BigInteger("1234"),jo.optBigInteger("bigDecimal", null)); + assertNull(jo.optBigDecimal("nullVal", null)); + } /** * Confirm behavior when JSONObject put(key, null object) is called @@ -2099,13 +2141,13 @@ public class JSONObjectTest { String str = "{\"myKey\": \"myval\"}"; JSONObject jsonObjectRemove = new JSONObject(str); jsonObjectRemove.remove("myKey"); + assertEquals("jsonObject should be empty",0 ,jsonObjectRemove.length()); JSONObject jsonObjectPutNull = new JSONObject(str); jsonObjectPutNull.put("myKey", (Object) null); + assertEquals("jsonObject should be empty",0 ,jsonObjectPutNull.length()); + - // validate JSON - assertTrue("jsonObject should be empty", jsonObjectRemove.length() == 0 - && jsonObjectPutNull.length() == 0); } /** @@ -2190,6 +2232,70 @@ public class JSONObjectTest { stringWriter.close(); } } + + /** + * Confirms that exceptions thrown when writing values are wrapped properly. + */ + @Test + public void testJSONWriterException() throws IOException { + final JSONObject jsonObject = new JSONObject(); + + jsonObject.put("someKey",new BrokenToString()); + + // test single element JSONObject + try(StringWriter writer = new StringWriter();) { + jsonObject.write(writer).toString(); + fail("Expected an exception, got a String value"); + } catch (JSONException e) { + assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage()); + } catch(Exception e) { + fail("Expected JSONException"); + } + + //test multiElement + jsonObject.put("somethingElse", "a value"); + + try (StringWriter writer = new StringWriter()) { + jsonObject.write(writer).toString(); + fail("Expected an exception, got a String value"); + } catch (JSONException e) { + assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage()); + } catch(Exception e) { + fail("Expected JSONException"); + } + + // test a more complex object + try (StringWriter writer = new StringWriter()) { + new JSONObject() + .put("somethingElse", "a value") + .put("someKey", new JSONArray() + .put(new JSONObject().put("key1", new BrokenToString()))) + .write(writer).toString(); + fail("Expected an exception, got a String value"); + } catch (JSONException e) { + assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage()); + } catch(Exception e) { + fail("Expected JSONException"); + } + + // test a more slightly complex object + try (StringWriter writer = new StringWriter()) { + new JSONObject() + .put("somethingElse", "a value") + .put("someKey", new JSONArray() + .put(new JSONObject().put("key1", new BrokenToString())) + .put(12345) + ) + .write(writer).toString(); + fail("Expected an exception, got a String value"); + } catch (JSONException e) { + assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage()); + } catch(Exception e) { + fail("Expected JSONException"); + } + + } + /** * Exercise the JSONObject write() method @@ -2468,4 +2574,16 @@ public class JSONObjectTest { assertTrue("Map should have 2 elements", map.size() == 2); } + + /** + * test class for verifying write errors. + * @author John Aylward + * + */ + private static class BrokenToString { + @Override + public String toString() { + throw new IllegalStateException("Something went horribly wrong!"); + } + } } diff --git a/src/test/java/org/json/junit/JSONStringTest.java b/src/test/java/org/json/junit/JSONStringTest.java index 617b9e3..ec40dbb 100644 --- a/src/test/java/org/json/junit/JSONStringTest.java +++ b/src/test/java/org/json/junit/JSONStringTest.java @@ -242,7 +242,7 @@ public class JSONStringTest { jsonArray.write(writer).toString(); fail("Expected an exception, got a String value"); } catch (JSONException e) { - assertTrue("Exception message does not match", "the exception value".equals(e.getMessage())); + assertEquals("Unable to write JSONArray value at index: 0", e.getMessage()); } catch(Exception e) { fail("Expected JSONException"); } finally {