Skip to content

Commit 441fec7

Browse files
authored
Merge pull request #73 from johnjaylward/BetterErrorHandling
Updates tests for better error handling changes
2 parents 93ca7b1 + c5e4b91 commit 441fec7

File tree

3 files changed

+134
-6
lines changed

3 files changed

+134
-6
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.json.junit;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
35
import static org.junit.Assert.assertTrue;
46

57
import java.util.EnumSet;
@@ -325,6 +327,7 @@ public void enumAPI() {
325327

326328
JSONObject jsonObject = new JSONObject();
327329
jsonObject.put("strKey", "value");
330+
jsonObject.put("strKey2", "VAL1");
328331
jsonObject.put("enumKey", myEnumField);
329332
jsonObject.put("enumClassKey", myEnumClass);
330333

@@ -360,11 +363,18 @@ public void enumAPI() {
360363

361364
// opt with default the wrong value
362365
actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey", null);
363-
assertTrue("opt null", actualEnum == null);
366+
assertNull("opt null", actualEnum);
367+
368+
// opt with default the string value
369+
actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey2", null);
370+
assertEquals(MyEnumField.VAL1, actualEnum);
364371

365372
// opt with default an index that does not exist
366373
actualEnum = jsonObject.optEnum(MyEnumField.class, "noKey", null);
367-
assertTrue("opt null", actualEnum == null);
374+
assertNull("opt null", actualEnum);
375+
376+
assertNull("Expected Null when the enum class is null",
377+
jsonObject.optEnum(null, "enumKey"));
368378

369379
/**
370380
* Exercise the proposed enum API methods on JSONArray

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

Lines changed: 121 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,8 @@ public void jsonObjectOptDefault() {
20102010
public void jsonObjectOptNoKey() {
20112011

20122012
JSONObject jsonObject = new JSONObject();
2013+
2014+
assertNull(jsonObject.opt(null));
20132015

20142016
assertTrue("optBigDecimal() should return default BigDecimal",
20152017
BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0);
@@ -2088,6 +2090,46 @@ public void jsonObjectOptCoercion() {
20882090
assertEquals(19007199254740992l, (long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"));
20892091
assertEquals(2147483647, (int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"));
20902092
}
2093+
2094+
/**
2095+
* Verifies that the optBigDecimal method properly converts values to BigDecimal and coerce them consistently.
2096+
*/
2097+
@Test
2098+
public void jsonObjectOptBigDecimal() {
2099+
JSONObject jo = new JSONObject().put("int", 123).put("long", 654L)
2100+
.put("float", 1.234f).put("double", 2.345d)
2101+
.put("bigInteger", new BigInteger("1234"))
2102+
.put("bigDecimal", new BigDecimal("1234.56789"))
2103+
.put("nullVal", JSONObject.NULL);
2104+
2105+
assertEquals(new BigDecimal("123"),jo.optBigDecimal("int", null));
2106+
assertEquals(new BigDecimal("654"),jo.optBigDecimal("long", null));
2107+
assertEquals(new BigDecimal(1.234f),jo.optBigDecimal("float", null));
2108+
assertEquals(new BigDecimal(2.345d),jo.optBigDecimal("double", null));
2109+
assertEquals(new BigDecimal("1234"),jo.optBigDecimal("bigInteger", null));
2110+
assertEquals(new BigDecimal("1234.56789"),jo.optBigDecimal("bigDecimal", null));
2111+
assertNull(jo.optBigDecimal("nullVal", null));
2112+
}
2113+
2114+
/**
2115+
* Verifies that the optBigDecimal method properly converts values to BigDecimal and coerce them consistently.
2116+
*/
2117+
@Test
2118+
public void jsonObjectOptBigInteger() {
2119+
JSONObject jo = new JSONObject().put("int", 123).put("long", 654L)
2120+
.put("float", 1.234f).put("double", 2.345d)
2121+
.put("bigInteger", new BigInteger("1234"))
2122+
.put("bigDecimal", new BigDecimal("1234.56789"))
2123+
.put("nullVal", JSONObject.NULL);
2124+
2125+
assertEquals(new BigInteger("123"),jo.optBigInteger("int", null));
2126+
assertEquals(new BigInteger("654"),jo.optBigInteger("long", null));
2127+
assertEquals(new BigInteger("1"),jo.optBigInteger("float", null));
2128+
assertEquals(new BigInteger("2"),jo.optBigInteger("double", null));
2129+
assertEquals(new BigInteger("1234"),jo.optBigInteger("bigInteger", null));
2130+
assertEquals(new BigInteger("1234"),jo.optBigInteger("bigDecimal", null));
2131+
assertNull(jo.optBigDecimal("nullVal", null));
2132+
}
20912133

20922134
/**
20932135
* Confirm behavior when JSONObject put(key, null object) is called
@@ -2099,13 +2141,13 @@ public void jsonObjectputNull() {
20992141
String str = "{\"myKey\": \"myval\"}";
21002142
JSONObject jsonObjectRemove = new JSONObject(str);
21012143
jsonObjectRemove.remove("myKey");
2144+
assertEquals("jsonObject should be empty",0 ,jsonObjectRemove.length());
21022145

21032146
JSONObject jsonObjectPutNull = new JSONObject(str);
21042147
jsonObjectPutNull.put("myKey", (Object) null);
2148+
assertEquals("jsonObject should be empty",0 ,jsonObjectPutNull.length());
2149+
21052150

2106-
// validate JSON
2107-
assertTrue("jsonObject should be empty", jsonObjectRemove.length() == 0
2108-
&& jsonObjectPutNull.length() == 0);
21092151
}
21102152

21112153
/**
@@ -2190,6 +2232,70 @@ public void write() throws IOException {
21902232
stringWriter.close();
21912233
}
21922234
}
2235+
2236+
/**
2237+
* Confirms that exceptions thrown when writing values are wrapped properly.
2238+
*/
2239+
@Test
2240+
public void testJSONWriterException() throws IOException {
2241+
final JSONObject jsonObject = new JSONObject();
2242+
2243+
jsonObject.put("someKey",new BrokenToString());
2244+
2245+
// test single element JSONObject
2246+
try(StringWriter writer = new StringWriter();) {
2247+
jsonObject.write(writer).toString();
2248+
fail("Expected an exception, got a String value");
2249+
} catch (JSONException e) {
2250+
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
2251+
} catch(Exception e) {
2252+
fail("Expected JSONException");
2253+
}
2254+
2255+
//test multiElement
2256+
jsonObject.put("somethingElse", "a value");
2257+
2258+
try (StringWriter writer = new StringWriter()) {
2259+
jsonObject.write(writer).toString();
2260+
fail("Expected an exception, got a String value");
2261+
} catch (JSONException e) {
2262+
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
2263+
} catch(Exception e) {
2264+
fail("Expected JSONException");
2265+
}
2266+
2267+
// test a more complex object
2268+
try (StringWriter writer = new StringWriter()) {
2269+
new JSONObject()
2270+
.put("somethingElse", "a value")
2271+
.put("someKey", new JSONArray()
2272+
.put(new JSONObject().put("key1", new BrokenToString())))
2273+
.write(writer).toString();
2274+
fail("Expected an exception, got a String value");
2275+
} catch (JSONException e) {
2276+
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
2277+
} catch(Exception e) {
2278+
fail("Expected JSONException");
2279+
}
2280+
2281+
// test a more slightly complex object
2282+
try (StringWriter writer = new StringWriter()) {
2283+
new JSONObject()
2284+
.put("somethingElse", "a value")
2285+
.put("someKey", new JSONArray()
2286+
.put(new JSONObject().put("key1", new BrokenToString()))
2287+
.put(12345)
2288+
)
2289+
.write(writer).toString();
2290+
fail("Expected an exception, got a String value");
2291+
} catch (JSONException e) {
2292+
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
2293+
} catch(Exception e) {
2294+
fail("Expected JSONException");
2295+
}
2296+
2297+
}
2298+
21932299

21942300
/**
21952301
* Exercise the JSONObject write() method
@@ -2468,4 +2574,16 @@ public void toMap() {
24682574
assertTrue("Map should have 2 elements", map.size() == 2);
24692575

24702576
}
2577+
2578+
/**
2579+
* test class for verifying write errors.
2580+
* @author John Aylward
2581+
*
2582+
*/
2583+
private static class BrokenToString {
2584+
@Override
2585+
public String toString() {
2586+
throw new IllegalStateException("Something went horribly wrong!");
2587+
}
2588+
}
24712589
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public void testJSONStringExceptionValue() throws IOException {
242242
jsonArray.write(writer).toString();
243243
fail("Expected an exception, got a String value");
244244
} catch (JSONException e) {
245-
assertTrue("Exception message does not match", "the exception value".equals(e.getMessage()));
245+
assertEquals("Unable to write JSONArray value at index: 0", e.getMessage());
246246
} catch(Exception e) {
247247
fail("Expected JSONException");
248248
} finally {

0 commit comments

Comments
 (0)