Skip to content

Updates tests for better error handling changes #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/test/java/org/json/junit/EnumTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -325,6 +327,7 @@ public void enumAPI() {

JSONObject jsonObject = new JSONObject();
jsonObject.put("strKey", "value");
jsonObject.put("strKey2", "VAL1");
jsonObject.put("enumKey", myEnumField);
jsonObject.put("enumClassKey", myEnumClass);

Expand Down Expand Up @@ -360,11 +363,18 @@ public void enumAPI() {

// 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
Expand Down
124 changes: 121 additions & 3 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,8 @@ public void jsonObjectOptDefault() {
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);
Expand Down Expand Up @@ -2088,6 +2090,46 @@ public void jsonObjectOptCoercion() {
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
Expand All @@ -2099,13 +2141,13 @@ public void jsonObjectputNull() {
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);
}

/**
Expand Down Expand Up @@ -2190,6 +2232,70 @@ public void write() throws IOException {
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
Expand Down Expand Up @@ -2468,4 +2574,16 @@ public void toMap() {
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!");
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/json/junit/JSONStringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void testJSONStringExceptionValue() throws IOException {
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 {
Expand Down