Skip to content

Commit

Permalink
Merge pull request stleary#33 from stleary/Fix-todos-and-cleanup
Browse files Browse the repository at this point in the history
Fix some todos, clean up some tests, improve coverage
  • Loading branch information
stleary committed Dec 30, 2015
2 parents 0990f34 + fc318a7 commit 0dc886d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CookieListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ public void convertCookieListToString() {
"name5=myCookieValue5;"+
" name6=myCookieValue6;";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
// exercise CookieList.toString()
String cookieListString = CookieList.toString(jsonObject);
// have to convert it back for validation
jsonObject = CookieList.toJSONObject(cookieListString);

// validate JSON content
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
Expand Down
84 changes: 53 additions & 31 deletions JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,6 @@ public void jsonObjectByBean() {
when(myBean.isFalseKey()).thenReturn(false);
when(myBean.getStringReaderKey()).thenReturn(
new StringReader("") {
/**
* TODO: Need to understand why returning a string
* turns "this" into an empty JSONObject,
* but not overriding turns "this" into a string.
*/
@Override
public String toString(){
return "Whatever";
}
});

JSONObject jsonObject = new JSONObject(myBean);
Expand Down Expand Up @@ -349,11 +340,11 @@ public void jsonObjectByObjectAndNames() {
}

/**
* Exercise the JSONObject from resource bundle functionality
* Exercise the JSONObject from resource bundle functionality.
* The test resource bundle is uncomplicated, but provides adequate test coverage.
*/
@Test
public void jsonObjectByResourceBundle() {
// TODO: how to improve resource bundle testing?
JSONObject jsonObject = new
JSONObject("org.json.junit.StringsResourceBundle",
Locale.getDefault());
Expand All @@ -374,7 +365,6 @@ public void jsonObjectByResourceBundle() {
*/
@Test
public void jsonObjectAccumulate() {
// TODO: should include an unsupported object

JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("myArray", true);
Expand All @@ -383,6 +373,11 @@ public void jsonObjectAccumulate() {
jsonObject.accumulate("myArray", "h\be\tllo w\u1234orld!");
jsonObject.accumulate("myArray", 42);
jsonObject.accumulate("myArray", -23.45e7);
// include an unsupported object for coverage
try {
jsonObject.accumulate("myArray", Double.NaN);
assertTrue("Expected exception", false);
} catch (JSONException ignored) {}

// validate JSON
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
Expand All @@ -401,14 +396,18 @@ public void jsonObjectAccumulate() {
*/
@Test
public void jsonObjectAppend() {
// TODO: should include an unsupported object
JSONObject jsonObject = new JSONObject();
jsonObject.append("myArray", true);
jsonObject.append("myArray", false);
jsonObject.append("myArray", "hello world!");
jsonObject.append("myArray", "h\be\tllo w\u1234orld!");
jsonObject.append("myArray", 42);
jsonObject.append("myArray", -23.45e7);
// include an unsupported object for coverage
try {
jsonObject.append("myArray", Double.NaN);
assertTrue("Expected exception", false);
} catch (JSONException ignored) {}

// validate JSON
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
Expand Down Expand Up @@ -818,34 +817,49 @@ public void bigNumberOperations() {
/**
* JSONObject put(String, Object) method stores and serializes
* bigInt and bigDec correctly. Nothing needs to change.
* TODO: New methods
* get|optBigInteger|BigDecimal() should work like other supported
* objects. Uncomment the get/opt methods after JSONObject is updated.
*/
jsonObject = new JSONObject();
jsonObject.put("bigInt", bigInteger);
assertTrue("jsonObject.put() handles bigInt correctly",
jsonObject.get("bigInt").equals(bigInteger));
// assertTrue("jsonObject.getBigInteger() handles bigInt correctly",
// jsonObject.getBigInteger("bigInt").equals(bigInteger));
// assertTrue("jsonObject.optBigInteger() handles bigInt correctly",
// jsonObject.optBigInteger("bigInt", BigInteger.ONE).equals(bigInteger));
assertTrue("jsonObject.getBigInteger() handles bigInt correctly",
jsonObject.getBigInteger("bigInt").equals(bigInteger));
assertTrue("jsonObject.optBigInteger() handles bigInt correctly",
jsonObject.optBigInteger("bigInt", BigInteger.ONE).equals(bigInteger));
assertTrue("jsonObject serializes bigInt correctly",
jsonObject.toString().equals("{\"bigInt\":123456789012345678901234567890}"));
jsonObject = new JSONObject();
jsonObject.put("bigDec", bigDecimal);
assertTrue("jsonObject.put() handles bigDec correctly",
jsonObject.get("bigDec").equals(bigDecimal));
// assertTrue("jsonObject.getBigDecimal() handles bigDec correctly",
// jsonObject.getBigDecimal("bigDec").equals(bigDecimal));
// assertTrue("jsonObject.optBigDecimal() handles bigDec correctly",
// jsonObject.optBigDecimal("bigDec", BigDecimal.ONE).equals(bigDecimal));
assertTrue("jsonObject.getBigDecimal() handles bigDec correctly",
jsonObject.getBigDecimal("bigDec").equals(bigDecimal));
assertTrue("jsonObject.optBigDecimal() handles bigDec correctly",
jsonObject.optBigDecimal("bigDec", BigDecimal.ONE).equals(bigDecimal));
assertTrue("jsonObject serializes bigDec correctly",
jsonObject.toString().equals(
"{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}"));

JSONArray jsonArray = new JSONArray();

/**
* exercise some exceptions
*/
try {
jsonObject.getBigDecimal("bigInt");
assertTrue("expected an exeption", false);
} 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);
} catch (JSONException ignored) {}
jsonObject.put("stringKey", "abc");
try {
jsonObject.getBigDecimal("stringKey");
assertTrue("expected an exeption", false);
} catch (JSONException ignored) {}
obj = jsonObject.optBigInteger("bigDec", BigInteger.ONE);
assertTrue("expected BigInteger", obj.equals(BigInteger.ONE));

/**
* JSONObject.numberToString() works correctly, nothing to change.
Expand Down Expand Up @@ -904,7 +918,7 @@ public void bigNumberOperations() {
actualFromPutStr.equals(
"{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}"));
// bigInt,bigDec put
jsonArray = new JSONArray();
JSONArray jsonArray = new JSONArray();
jsonArray.put(bigInteger);
jsonArray.put(bigDecimal);
actualFromPutStr = jsonArray.toString();
Expand Down Expand Up @@ -1105,7 +1119,7 @@ public void jsonObjectIncrement() {
String str =
"{"+
"\"keyLong\":9999999991,"+
"\"keyDouble\":1.1,"+
"\"keyDouble\":1.1"+
"}";
JSONObject jsonObject = new JSONObject(str);
jsonObject.increment("keyInt");
Expand All @@ -1115,16 +1129,26 @@ public void jsonObjectIncrement() {
jsonObject.increment("keyInt");
jsonObject.increment("keyLong");
jsonObject.increment("keyDouble");
/**
* JSONObject constructor won't handle these types correctly, but
* adding them via put works.
*/
jsonObject.put("keyFloat", new Float(1.1));
jsonObject.put("keyBigInt", new BigInteger("123456789123456789123456789123456780"));
jsonObject.put("keyBigDec", new BigDecimal("123456789123456789123456789123456780.1"));
jsonObject.increment("keyFloat");
jsonObject.increment("keyFloat");
jsonObject.increment("keyBigInt");
jsonObject.increment("keyBigDec");

// validate JSON
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.keyInt")));
assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(JsonPath.read(doc, "$.keyLong")));
assertTrue("expected 3.1", Double.valueOf(3.1).equals(JsonPath.read(doc, "$.keyDouble")));
assertTrue("expected 123456789123456789123456789123456781", new BigInteger("123456789123456789123456789123456781").equals(JsonPath.read(doc, "$.keyBigInt")));
assertTrue("expected 123456789123456789123456789123456781.1", new BigDecimal("123456789123456789123456789123456781.1").equals(JsonPath.read(doc, "$.keyBigDec")));

/**
* Should work the same way on any platform! @see https://docs.oracle
Expand Down Expand Up @@ -1526,8 +1550,6 @@ public void wrapObject() {
assertTrue("expected val1", "val1".equals(JsonPath.read(doc, "$.key1")));
assertTrue("expected val2", "val2".equals(JsonPath.read(doc, "$.key2")));
assertTrue("expected val3", "val3".equals(JsonPath.read(doc, "$.key3")));

// TODO test wrap(package)
}

/**
Expand Down

0 comments on commit 0dc886d

Please sign in to comment.