From 03bb6d08be4707e49a4c993526973563f527fc24 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 16 Mar 2015 12:00:24 -0500 Subject: [PATCH 001/315] Create README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 000000000..727f33a8b --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# JSON-Java-unit-test +Junit test harness to validate the JSON-Java GitHub project code. +See https://github.com/douglascrockford/JSON-java + From 7559b574dd6c0b9918a06a31335921d66a0b4330 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 16 Mar 2015 12:06:41 -0500 Subject: [PATCH 002/315] Add LICENSE file via addalicense.com --- LICENSE.txt | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 LICENSE.txt diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 000000000..2bb9ad240 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,176 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file From 03192b01624e2e65145e4fd9803d48b962c0a2fd Mon Sep 17 00:00:00 2001 From: stleary Date: Tue, 17 Mar 2015 00:38:08 -0500 Subject: [PATCH 003/315] adding this file --- CDLTest.java | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 CDLTest.java diff --git a/CDLTest.java b/CDLTest.java new file mode 100644 index 000000000..a12a91159 --- /dev/null +++ b/CDLTest.java @@ -0,0 +1,76 @@ +package org.json.junit; + +import static org.junit.Assert.*; + +import java.io.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.json.JSONObject; +import org.json.JSONArray; +import org.json.CDL; + +/** + * Tests for {@link CDL}. + * CDL provides an application level API, it is not actually used by the + * reference app. To test it, strings will be converted to JSON-Java classes + * and then converted back. But each row will be an unordered JSONObject, + * so can't use a simple string compare. + * @author JSON.org + * @version 2015-03-16 + * + */ +public class CDLTest { + + /** + * Compares a JSON array to the original string. The top row of the + * string contains the JSONObject keys and the remaining rows contain + * the values. The JSONObject rows are unordered and may differ between + * rows. + * @param jsonArray the JSONArray which was created from the string + * @param str the string which was used to create the JSONArray + * @return true if equal, otherwise false + */ + public boolean compareJSONArrayToString(JSONArray jsonArray, String str) { + boolean result = true; + int rows = jsonArray.length(); + StringReader sr = new StringReader(str); + BufferedReader reader = new BufferedReader(sr); + try { + String columnNames = reader.readLine(); + String[] keys = columnNames.split(","); + for (int i = 0; i < rows; ++i) { + String row = reader.readLine(); + String[] values = row.split(","); + JSONObject jsonObject = jsonArray.getJSONObject(i); + if (keys.length != jsonObject.length()) { + break; + } + int colIndex = 0; + for (String key: keys) { + + Object obj = jsonObject.get(key); + + } + } + } catch (IOException ignore) {} + return result; + } + + @Test + public void shouldConvertCDLToJSONArray() { + /** + * simple array where the first row of the string consists of the + * column names and there are 2 value rows + */ + String lines = new String( + "Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7\n" + + "val1, val2, val3, val4, val5, val6, val7\n" + + "1, 2, 3, 4, 5, 6, 7\n"); + JSONArray jsonArray = CDL.toJSONArray(lines); + assertTrue("CDL should convert string to JSONArray correctly", + compareJSONArrayToString(jsonArray, lines)); + } + +} \ No newline at end of file From 5b56b5707418f7003260a0cf83475414beb7cda1 Mon Sep 17 00:00:00 2001 From: stleary Date: Tue, 17 Mar 2015 21:47:53 -0500 Subject: [PATCH 004/315] updating cdltest --- CDLTest.java | 65 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index a12a91159..28320dd8f 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -3,14 +3,23 @@ import static org.junit.Assert.*; import java.io.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import org.json.JSONException; import org.json.JSONObject; import org.json.JSONArray; import org.json.CDL; + /** * Tests for {@link CDL}. * CDL provides an application level API, it is not actually used by the @@ -26,36 +35,59 @@ public class CDLTest { /** * Compares a JSON array to the original string. The top row of the * string contains the JSONObject keys and the remaining rows contain - * the values. The JSONObject rows are unordered and may differ between - * rows. + * the values. The JSONObject in each JSONArray row is expected to have + * an entry corresponding to each key/value pair in the string. + * Each JSONObject row is unordered in its own way. * @param jsonArray the JSONArray which was created from the string * @param str the string which was used to create the JSONArray - * @return true if equal, otherwise false + * @return null if equal, otherwise error description */ - public boolean compareJSONArrayToString(JSONArray jsonArray, String str) { - boolean result = true; + public String compareJSONArrayToString(JSONArray jsonArray, String str) { int rows = jsonArray.length(); StringReader sr = new StringReader(str); BufferedReader reader = new BufferedReader(sr); try { + // first line contains the keys to the JSONObject array entries String columnNames = reader.readLine(); String[] keys = columnNames.split(","); + /** + * Each line contains the values for the corresponding + * JSONObject array entry + */ for (int i = 0; i < rows; ++i) { String row = reader.readLine(); String[] values = row.split(","); + // need a value for every key to proceed + if (keys.length != values.length) { + return("row: " +i+ " key and value counts do not match"); + } JSONObject jsonObject = jsonArray.getJSONObject(i); + // need a key for every JSONObject entry to proceed if (keys.length != jsonObject.length()) { - break; + return("row: " +i+ " key and jsonObject counts do not match"); + } + /** + * convert string entries into a natural order map. Trim the + * keys and values for tokener compatibility + */ + Map strMap = new TreeMap(); + for (int j = 0; j < keys.length; ++j) { + strMap.put(keys[j].trim(), values[j].trim()); + } + // put the JSONObjet key/value pairs in natural key order + Iterator keyIt = jsonObject.keys(); + Map jsonObjectMap = new TreeMap(); + while (keyIt.hasNext()) { + String key = keyIt.next(); + jsonObjectMap.put(key, jsonObject.get(key).toString()); } - int colIndex = 0; - for (String key: keys) { - - Object obj = jsonObject.get(key); - + if (!strMap.equals(jsonObjectMap)) { + return("row: " +i+ "string does not match jsonObject"); } } - } catch (IOException ignore) {} - return result; + } catch (IOException ignore) { + } catch (JSONException ignore) {} + return null; } @Test @@ -69,8 +101,11 @@ public void shouldConvertCDLToJSONArray() { "val1, val2, val3, val4, val5, val6, val7\n" + "1, 2, 3, 4, 5, 6, 7\n"); JSONArray jsonArray = CDL.toJSONArray(lines); - assertTrue("CDL should convert string to JSONArray correctly", - compareJSONArrayToString(jsonArray, lines)); + String resultStr = compareJSONArrayToString(jsonArray, lines); + if (resultStr != null) { + assertTrue("CDL should convert string to JSONArray correctly: " + + resultStr, false); + } } } \ No newline at end of file From 6c5e25dcb95ff1ac7bfc04ecb664656bd6bd3048 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 18 Mar 2015 10:13:13 -0500 Subject: [PATCH 005/315] added embedded quotes and escapes --- CDLTest.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index 28320dd8f..fb5151487 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -59,20 +59,27 @@ public String compareJSONArrayToString(JSONArray jsonArray, String str) { String[] values = row.split(","); // need a value for every key to proceed if (keys.length != values.length) { + System.out.println("keys: " + Arrays.toString(keys)); + System.out.println("values: " + Arrays.toString(values)); return("row: " +i+ " key and value counts do not match"); } JSONObject jsonObject = jsonArray.getJSONObject(i); // need a key for every JSONObject entry to proceed if (keys.length != jsonObject.length()) { + System.out.println("keys: " + Arrays.toString(keys)); + System.out.println("jsonObject: " + jsonObject.toString()); return("row: " +i+ " key and jsonObject counts do not match"); } /** * convert string entries into a natural order map. Trim the - * keys and values for tokener compatibility + * keys and values for tokener compatibility. */ Map strMap = new TreeMap(); for (int j = 0; j < keys.length; ++j) { - strMap.put(keys[j].trim(), values[j].trim()); + values[j] = values[j].trim(); + // strip optional surrounding quotes + values[j] = values[j].replaceAll("^\"|\"$", ""); + strMap.put(keys[j].trim(), values[j]); } // put the JSONObjet key/value pairs in natural key order Iterator keyIt = jsonObject.keys(); @@ -82,6 +89,8 @@ public String compareJSONArrayToString(JSONArray jsonArray, String str) { jsonObjectMap.put(key, jsonObject.get(key).toString()); } if (!strMap.equals(jsonObjectMap)) { + System.out.println("strMap: " +strMap.toString()); + System.out.println("jsonObjectMap: " +jsonObjectMap.toString()); return("row: " +i+ "string does not match jsonObject"); } } @@ -99,7 +108,11 @@ public void shouldConvertCDLToJSONArray() { String lines = new String( "Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7\n" + "val1, val2, val3, val4, val5, val6, val7\n" + - "1, 2, 3, 4, 5, 6, 7\n"); + "1, 2, 3, 4, 5, 6, 7\n" + + "true, false, true, true, false, false, false\n" + + "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" + + "\"va\tl1\", \"val2\", \"val\\b3\", \"val4\\n\", \"va\\rl5\", val6, val7\n" + ); JSONArray jsonArray = CDL.toJSONArray(lines); String resultStr = compareJSONArrayToString(jsonArray, lines); if (resultStr != null) { From 5fc0c4e1df60ea5565733bbde53707bb2ac95522 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 18 Mar 2015 12:40:20 -0500 Subject: [PATCH 006/315] 72% coverage! --- CDLTest.java | 122 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 98 insertions(+), 24 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index fb5151487..f5ef9d0da 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -5,6 +5,7 @@ import java.io.*; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -32,6 +33,19 @@ */ public class CDLTest { + /** + * String of lines where the column names are in the first row, + * and all subsequent rows are values + */ + String lines = new String( + "Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7\n" + + "val1, val2, val3, val4, val5, val6, val7\n" + + "1, 2, 3, 4, 5, 6, 7\n" + + "true, false, true, true, false, false, false\n" + + "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" + + "\"va\tl1\", \"val2\", \"val\\b3\", \"val4\\n\", \"va\\rl5\", val6, val7\n" + ); + /** * Compares a JSON array to the original string. The top row of the * string contains the JSONObject keys and the remaining rows contain @@ -49,14 +63,16 @@ public String compareJSONArrayToString(JSONArray jsonArray, String str) { try { // first line contains the keys to the JSONObject array entries String columnNames = reader.readLine(); + columnNames = normalizeString(columnNames); String[] keys = columnNames.split(","); /** * Each line contains the values for the corresponding * JSONObject array entry */ for (int i = 0; i < rows; ++i) { - String row = reader.readLine(); - String[] values = row.split(","); + String line = reader.readLine(); + line = normalizeString(line); + String[] values = line.split(","); // need a value for every key to proceed if (keys.length != values.length) { System.out.println("keys: " + Arrays.toString(keys)); @@ -70,16 +86,10 @@ public String compareJSONArrayToString(JSONArray jsonArray, String str) { System.out.println("jsonObject: " + jsonObject.toString()); return("row: " +i+ " key and jsonObject counts do not match"); } - /** - * convert string entries into a natural order map. Trim the - * keys and values for tokener compatibility. - */ + // convert string entries into a natural order map. Map strMap = new TreeMap(); for (int j = 0; j < keys.length; ++j) { - values[j] = values[j].trim(); - // strip optional surrounding quotes - values[j] = values[j].replaceAll("^\"|\"$", ""); - strMap.put(keys[j].trim(), values[j]); + strMap.put(keys[j], values[j]); } // put the JSONObjet key/value pairs in natural key order Iterator keyIt = jsonObject.keys(); @@ -98,27 +108,91 @@ public String compareJSONArrayToString(JSONArray jsonArray, String str) { } catch (JSONException ignore) {} return null; } - + @Test public void shouldConvertCDLToJSONArray() { - /** - * simple array where the first row of the string consists of the - * column names and there are 2 value rows - */ - String lines = new String( - "Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7\n" + - "val1, val2, val3, val4, val5, val6, val7\n" + - "1, 2, 3, 4, 5, 6, 7\n" + - "true, false, true, true, false, false, false\n" + - "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" + - "\"va\tl1\", \"val2\", \"val\\b3\", \"val4\\n\", \"va\\rl5\", val6, val7\n" - ); JSONArray jsonArray = CDL.toJSONArray(lines); String resultStr = compareJSONArrayToString(jsonArray, lines); if (resultStr != null) { - assertTrue("CDL should convert string to JSONArray correctly: " + + assertTrue("CDL should convert string to JSONArray: " + resultStr, false); } } + @Test + public void shouldConvertJSONArrayToCDLString() { + final boolean normalize = true; + final boolean doNotNormalize = false; + JSONArray jsonArray = CDL.toJSONArray(lines); + String jsonStr = CDL.toString(jsonArray); + // normal sorted + List> sortedLines = sortColumnsInLines(lines, normalize); + // sorted, should already be normalized + List> sortedJsonStr = sortColumnsInLines(jsonStr, doNotNormalize); + boolean result = sortedLines.equals(sortedJsonStr); + if (!result) { + System.out.println("lines: " +sortedLines); + System.out.println("jsonStr: " +sortedJsonStr); + assertTrue("CDL should convert JSONArray back to original string: " + + lines.equals(jsonStr), false); + } + } + + /** + * Utility to trim and remove internal quotes from comma delimited strings. + * Need to do this because JSONObject does the same thing + * @param line the line to be normalized + * @return the normalized line + */ + private String normalizeString(String line) { + StringBuilder builder = new StringBuilder(); + boolean comma = false; + String[] values = line.split(","); + for (int i = 0; i < values.length; ++i) { + if (comma) { + builder.append(","); + } + comma = true; + values[i] = values[i].trim(); + // strip optional surrounding quotes + values[i] = values[i].replaceAll("^\"|\"$", ""); + builder.append(values[i]); + } + return builder.toString(); + } + + /** + * Utility to sort the columns in a (possibly) multi-lined string. + * The columns are column separated. Need to do this because + * JSONObects are not ordered + * @param string the string to be sorted + * @param normalize flag, true if line should be normalized + * @return a list of sorted lines, where each line is a list sorted + * in natural key order + */ + private List> sortColumnsInLines(String string, + boolean normalizeFlag) { + List> lineList = new ArrayList>(); + StringReader sr = new StringReader(string); + BufferedReader reader = new BufferedReader(sr); + try { + while (true) { + String line = reader.readLine(); + if (line == null) { + break; + } + if (normalizeFlag) { + line = normalizeString(line); + } + List columnList = new ArrayList(); + String[] values = line.split(","); + for (int i = 0; i < values.length; ++i) { + columnList.add(values[i]); + } + Collections.sort(columnList); + lineList.add(columnList); + } + } catch (IOException ignore) {} + return lineList; + } } \ No newline at end of file From a859e4f547588d214361776ce556bb41eacc5759 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 18 Mar 2015 18:16:19 -0500 Subject: [PATCH 007/315] few more tests --- CDLTest.java | 125 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 94 insertions(+), 31 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index f5ef9d0da..0395045f0 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -35,7 +35,7 @@ public class CDLTest { /** * String of lines where the column names are in the first row, - * and all subsequent rows are values + * and all subsequent rows are values. All keys and values should be legal. */ String lines = new String( "Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7\n" + @@ -46,6 +46,98 @@ public class CDLTest { "\"va\tl1\", \"val2\", \"val\\b3\", \"val4\\n\", \"va\\rl5\", val6, val7\n" ); + @Test(expected=NullPointerException.class) + public void shouldThrowExceptionOnNullString() { + String nullStr = null; + CDL.toJSONArray(nullStr); + } + + @Test + /** + * Note: This test reveals a bug in the method JavaDoc. It should + * mention it might return null, or it should return an empty JSONArray. + */ + public void shouldHandleOnlyColumnNames() { + String columnNameStr = "col1, col2, col3"; + JSONArray jsonArray = CDL.toJSONArray(columnNameStr); + assertTrue("CDL should return null when only 1 row is given", + jsonArray == null); + } + + @Test + /** + * Note: This test reveals a bug in the method JavaDoc. It should + * mention it might return null, or it should return an empty JSONArray. + */ + public void shouldHandleEmptyString() { + String emptyStr = ""; + JSONArray jsonArray = CDL.toJSONArray(emptyStr); + assertTrue("CDL should return null when the input string is empty", + jsonArray == null); + } + + @Test + public void toStringShouldCheckSpecialChars() { + /** + * This is pretty clumsy, there should be a better way + * to perform this test. Needs more debugging. The problem + * may be that these chars are sanitized out by CDL when constructing + * a JSONArray from a string. + */ + String singleStr = "\"Col 1\"\n1"; + JSONArray jsonArray = CDL.toJSONArray(singleStr); + JSONObject jsonObject = (JSONObject)(jsonArray.get(0)); + jsonObject.put("Col \r4", "V4"); + jsonObject.put("Col \0 a", "V5"); + boolean doNotNormalize = false; + List> expectedLines = + sortColumnsInLines("Col ,2\",Col 1,\"Col 4\",\"Col a\"\nV2,1,V4,V5,V3", + doNotNormalize); + List> jsonArrayLines = + sortColumnsInLines(CDL.toString(jsonArray), doNotNormalize); + System.out.println("expected: " +expectedLines); + System.out.println("jsonArray: " +jsonArrayLines); + } + + @Test + public void shouldConvertJSONArrayToCDLString() { + /** + * This is the first test of normal functionality. + * The string contains a typical variety of values + * that might be found in a real CDL. + */ + final boolean normalize = true; + final boolean doNotNormalize = false; + JSONArray jsonArray = CDL.toJSONArray(lines); + String jsonStr = CDL.toString(jsonArray); + // normal sorted + List> sortedLines = sortColumnsInLines(lines, normalize); + // sorted, should already be normalized + List> sortedJsonStr = sortColumnsInLines(jsonStr, doNotNormalize); + boolean result = sortedLines.equals(sortedJsonStr); + if (!result) { + System.out.println("lines: " +sortedLines); + System.out.println("jsonStr: " +sortedJsonStr); + assertTrue("CDL should convert JSONArray back to original string: " + + lines.equals(jsonStr), false); + } + } + + @Test + public void shouldConvertCDLToJSONArray() { + JSONArray jsonArray = CDL.toJSONArray(lines); + String resultStr = compareJSONArrayToString(jsonArray, lines); + if (resultStr != null) { + assertTrue("CDL should convert string to JSONArray: " + + resultStr, false); + } + } + + + /******************************************************************\ + * SUPPORT AND UTILITY + \******************************************************************/ + /** * Compares a JSON array to the original string. The top row of the * string contains the JSONObject keys and the remaining rows contain @@ -56,7 +148,7 @@ public class CDLTest { * @param str the string which was used to create the JSONArray * @return null if equal, otherwise error description */ - public String compareJSONArrayToString(JSONArray jsonArray, String str) { + private String compareJSONArrayToString(JSONArray jsonArray, String str) { int rows = jsonArray.length(); StringReader sr = new StringReader(str); BufferedReader reader = new BufferedReader(sr); @@ -109,35 +201,6 @@ public String compareJSONArrayToString(JSONArray jsonArray, String str) { return null; } - @Test - public void shouldConvertCDLToJSONArray() { - JSONArray jsonArray = CDL.toJSONArray(lines); - String resultStr = compareJSONArrayToString(jsonArray, lines); - if (resultStr != null) { - assertTrue("CDL should convert string to JSONArray: " + - resultStr, false); - } - } - - @Test - public void shouldConvertJSONArrayToCDLString() { - final boolean normalize = true; - final boolean doNotNormalize = false; - JSONArray jsonArray = CDL.toJSONArray(lines); - String jsonStr = CDL.toString(jsonArray); - // normal sorted - List> sortedLines = sortColumnsInLines(lines, normalize); - // sorted, should already be normalized - List> sortedJsonStr = sortColumnsInLines(jsonStr, doNotNormalize); - boolean result = sortedLines.equals(sortedJsonStr); - if (!result) { - System.out.println("lines: " +sortedLines); - System.out.println("jsonStr: " +sortedJsonStr); - assertTrue("CDL should convert JSONArray back to original string: " + - lines.equals(jsonStr), false); - } - } - /** * Utility to trim and remove internal quotes from comma delimited strings. * Need to do this because JSONObject does the same thing From 4a5809910bc7a2bbe5892f9336eae0a609af9912 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 18 Mar 2015 20:54:46 -0500 Subject: [PATCH 008/315] final, for now, 94.8% coverage --- CDLTest.java | 101 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 34 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index 0395045f0..d5ead3c4a 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -1,5 +1,4 @@ package org.json.junit; - import static org.junit.Assert.*; import java.io.*; @@ -9,12 +8,9 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.TreeMap; import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONArray; @@ -23,12 +19,12 @@ /** * Tests for {@link CDL}. - * CDL provides an application level API, it is not actually used by the + * CDL provides an application level API, but it is not used by the * reference app. To test it, strings will be converted to JSON-Java classes - * and then converted back. But each row will be an unordered JSONObject, - * so can't use a simple string compare. + * and then converted back. Since each row is an unordered JSONObject, + * can't use a simple string compare to check for equality. * @author JSON.org - * @version 2015-03-16 + * @version 2015-03-18 * */ public class CDLTest { @@ -43,7 +39,7 @@ public class CDLTest { "1, 2, 3, 4, 5, 6, 7\n" + "true, false, true, true, false, false, false\n" + "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" + - "\"va\tl1\", \"val2\", \"val\\b3\", \"val4\\n\", \"va\\rl5\", val6, val7\n" + "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n" ); @Test(expected=NullPointerException.class) @@ -79,28 +75,74 @@ public void shouldHandleEmptyString() { @Test public void toStringShouldCheckSpecialChars() { /** - * This is pretty clumsy, there should be a better way - * to perform this test. Needs more debugging. The problem - * may be that these chars are sanitized out by CDL when constructing - * a JSONArray from a string. + * Given a JSONArray that was not built by CDL, some chars may be + * found that would otherwise be filtered out by CDL. */ - String singleStr = "\"Col 1\"\n1"; - JSONArray jsonArray = CDL.toJSONArray(singleStr); - JSONObject jsonObject = (JSONObject)(jsonArray.get(0)); - jsonObject.put("Col \r4", "V4"); - jsonObject.put("Col \0 a", "V5"); - boolean doNotNormalize = false; + JSONArray jsonArray = new JSONArray(); + JSONObject jsonObject = new JSONObject(); + jsonArray.put(jsonObject); + // \r will be filtered from name + jsonObject.put("Col \r1", "V1"); + // \r will be filtered from value + jsonObject.put("Col 2", "V2\r"); + boolean normalize = true; List> expectedLines = - sortColumnsInLines("Col ,2\",Col 1,\"Col 4\",\"Col a\"\nV2,1,V4,V5,V3", - doNotNormalize); + sortColumnsInLines("Col 1, Col 2,\nV1, V2", normalize); List> jsonArrayLines = - sortColumnsInLines(CDL.toString(jsonArray), doNotNormalize); - System.out.println("expected: " +expectedLines); - System.out.println("jsonArray: " +jsonArrayLines); + sortColumnsInLines(CDL.toString(jsonArray), normalize); + if (!expectedLines.equals(jsonArrayLines)) { + System.out.println("expected: " +expectedLines); + System.out.println("jsonArray: " +jsonArrayLines); + assertTrue("Should filter out certain chars", + false); + } + } + + @Test(expected=JSONException.class) + public void shouldHandleUnbalancedQuoteInName() { + String badLine = "Col1, \"Col2\nVal1, Val2"; + CDL.toJSONArray(badLine); + } + + @Test(expected=JSONException.class) + public void shouldHandleUnbalancedQuoteInValue() { + String badLine = "Col1, Col2\n\"Val1, Val2"; + CDL.toJSONArray(badLine); + } + + @Test(expected=JSONException.class) + public void shouldHandleNullInName() { + String badLine = "C\0ol1, Col2\nVal1, Val2"; + CDL.toJSONArray(badLine); } @Test - public void shouldConvertJSONArrayToCDLString() { + public void shouldConvertCDLToJSONArray() { + JSONArray jsonArray = CDL.toJSONArray(lines); + String resultStr = compareJSONArrayToString(jsonArray, lines); + if (resultStr != null) { + assertTrue("CDL should convert string to JSONArray: " + + resultStr, false); + } + } + + @Test + public void shouldCreateJSONArrayUsingJSONArray() { + String names = "Col1, Col2"; + String nameArrayStr = "[" +names+ "]"; + String values = "V1, V2"; + JSONArray nameJSONArray = new JSONArray(nameArrayStr); + JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); + String combinedStr = names+ "\n" +values; + String resultStr = compareJSONArrayToString(jsonArray, combinedStr); + if (resultStr != null) { + assertTrue("CDL should convert JSONArray and string to JSONArray: " + + resultStr, false); + } + } + + @Test + public void shouldConvertCDLToJSONArrayAndBackToString() { /** * This is the first test of normal functionality. * The string contains a typical variety of values @@ -123,15 +165,6 @@ public void shouldConvertJSONArrayToCDLString() { } } - @Test - public void shouldConvertCDLToJSONArray() { - JSONArray jsonArray = CDL.toJSONArray(lines); - String resultStr = compareJSONArrayToString(jsonArray, lines); - if (resultStr != null) { - assertTrue("CDL should convert string to JSONArray: " + - resultStr, false); - } - } /******************************************************************\ From d613203eecf1e8707955df2cc6e3db5ba8d6c0ad Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 18 Mar 2015 21:51:08 -0500 Subject: [PATCH 009/315] Update README.md --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 727f33a8b..8fdb6ec00 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,10 @@ # JSON-Java-unit-test -Junit test harness to validate the JSON-Java GitHub project code. -See https://github.com/douglascrockford/JSON-java +Unit tests to validate the JSON-Java GitHub project code (https://github.com/douglascrockford/JSON-java). +Test harness: http://junit.org +Coverage: http://www.eclemma.org/ +Completed tests: +CDLTest.java +In progress: +CookieTest.java + From 3d3325aaca7cf7e450ce87e2b42e104a7319cc4c Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 18 Mar 2015 21:51:43 -0500 Subject: [PATCH 010/315] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 8fdb6ec00..bef669670 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,16 @@ # JSON-Java-unit-test Unit tests to validate the JSON-Java GitHub project code (https://github.com/douglascrockford/JSON-java). + Test harness: http://junit.org + Coverage: http://www.eclemma.org/ + Completed tests: + CDLTest.java + In progress: + CookieTest.java From df68a7b593a9fad6b5cc0dec27226b1c5deaa6cc Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 18 Mar 2015 22:39:54 -0500 Subject: [PATCH 011/315] test suite and cookie test --- CookieTest.java | 26 ++++++++++++++++++++++++++ JunitTestSuite.java | 11 +++++++++++ TestRunner.java | 15 +++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 CookieTest.java create mode 100644 JunitTestSuite.java create mode 100644 TestRunner.java diff --git a/CookieTest.java b/CookieTest.java new file mode 100644 index 000000000..f77cdf722 --- /dev/null +++ b/CookieTest.java @@ -0,0 +1,26 @@ +package org.json.junit; + +import java.io.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import static org.junit.Assert.*; +import org.junit.Test; + + +/** + * Tests for JSON-Java Cookie.java + */ +public class CookieTest { + + @Test + public void test() { + + } + +} diff --git a/JunitTestSuite.java b/JunitTestSuite.java new file mode 100644 index 000000000..20b69f9ce --- /dev/null +++ b/JunitTestSuite.java @@ -0,0 +1,11 @@ +package org.json.junit; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +@RunWith(Suite.class) +@Suite.SuiteClasses({ + CDLTest.class, + CookieTest.class +}) +public class JunitTestSuite { +} \ No newline at end of file diff --git a/TestRunner.java b/TestRunner.java new file mode 100644 index 000000000..8f9dd8956 --- /dev/null +++ b/TestRunner.java @@ -0,0 +1,15 @@ +package org.json.junit; + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +public class TestRunner { + public static void main(String[] args) { + Result result = JUnitCore.runClasses(JunitTestSuite.class); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(result.wasSuccessful()); + } +} \ No newline at end of file From b557180bcaf2a66eab5a4ae55db6893623f25ab5 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 19 Mar 2015 11:27:45 -0500 Subject: [PATCH 012/315] more cookie code, in progress --- CookieTest.java | 56 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/CookieTest.java b/CookieTest.java index f77cdf722..8f4cf149b 100644 --- a/CookieTest.java +++ b/CookieTest.java @@ -1,15 +1,11 @@ package org.json.junit; import java.io.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; +import java.util.*; import static org.junit.Assert.*; + +import org.json.*; import org.junit.Test; @@ -18,8 +14,52 @@ */ public class CookieTest { + String realWorldCookie = + "hpc=d=I.aZLE4l.8DeqRynle2fTnKxdAycw3CvCrzMNofhR9a5vYaU.XnHk6n3ZenMs6Xqq"+ + "3Mc5kMw.M1c.vR6zdxVMsfAQn75WNaFp8mY3UQgEw8lvIAbZvT_PiJofv7OMCbabUOe1Efd"+ + "i2M5.aVTX2bHB3EJPhNQNe0B5PL6mGbz7KYYyujkcn6hVS7U5d5OYv7L0GSAiKY-&v=2; y"+ + "wadp115488662=3370273056; AO=u=1&o=1; ywandp=10001806365479:1024785001;"+ + "10001576721379:3531995934; fpc=10001806365479:ZblWsSPj||;10001576721379"+ + ":ZY1jZhRq||; V=v=0.7&m=0&ccOptions={\"show\":false,\"lang\":\"en\",\"fo"+ + "ntSize\":24,\"fontName\":\"Helvetica Neue,Helvetica,Arial,_sans\",\"fon"+ + "tColor\":\"#ffffff\",\"fontOpacity\":1,\"fontEffect\":\"none\",\"bgColo"+ + "r\":\"#000000\",\"bgOpacity\":0.75}; yvap=193@yvap=193@cc=1@al=1@vl=0@r"+ + "vl=0@ac=1@rvl_NFL=0@session_NFL=0@lmsID=@rcc=0; YLS=v=1&p=1&n=1; ucs=tr"+ + "=1424831973913&sfcTs=1425971131&sfc=1; B=26tgei1adfl2v&b=4&d=j7.bbChrYH"+ + "1Ww.22z25N3S2YRsiX.e8VKSZpZdjeYXeN.w--&s=lr; F=a=MVvM8WsMvSxoU9K4FcyMxZ"+ + ".lwmw1yLWpNLOZbMVqjDB8d.bZm1C1JJVJFfCXcy3YfSZy47VAvKKSGZBmM1HQdIUWJA--&"+ + "b=PW8Y; YP=v=AwAAY&d=AEcAMEQCIHHEk.ugtA0iqWk_ctLMBWKG_gJfDzKX.tlKIIGBVH"+ + "cTAiBgmZUHV73V2i80FgqcVjQnvNTyor0rYBXsjhXBul2PzwA-; ypcdb=096e88ca6ff13"+ + "fee954ee414bb7b9362; Y=v=1&n=edbmi9njnt2h1&p=; CRZY={\"33935700511_2015"+ + "0317\":{\"expires\":1426808579870,\"data\":{\"nv\":1,\"bn\":1,\"collaps"+ + "ed\":0}},\"33726925511_20150318\":{\"expires\":1426859124988,\"data\":{"+ + "\"nv\":7,\"bn\":0,\"collapsed\":0}},\"33748770511_20150318\":{\"expires"+ + "\":1426911961098,\"data\":{\"nv\":2,\"bn\":0,\"collapsed\":0}}}; apeaf="+ + "td-applet-stream={\"tmpl\":\"items\",\"po\":{\"2409678.20150318\":{\"c"+ + "\":0,\"v\":2,\"ts\":1426719393315}}}"; + + @Test(expected=NullPointerException.class) + public void shouldHandleNullCookie() { + String cookieStr = null; + Cookie.toJSONObject(cookieStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleEmptyStringCookie() { + String cookieStr = ""; + Cookie.toJSONObject(cookieStr); + } + @Test - public void test() { + public void shouldHandleSimpleCookie() { + String cookieStr = "abc=def"; + JSONObject jsonObject = Cookie.toJSONObject(cookieStr); + Set keySet = jsonObject.keySet(); + assertTrue("Keyset should have exactly 2 keys", keySet.size() == 2); + assertTrue("name should have expected value", + jsonObject.getString("name").equals("abc")); + assertTrue("Value should have expected value", + jsonObject.getString("value").equals("def")); } From ab08db4ad666fa1cca7ed8e701a0ea7bff382fb8 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 19 Mar 2015 16:09:29 -0500 Subject: [PATCH 013/315] CookieTest 97.5% coverage --- CookieTest.java | 216 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 185 insertions(+), 31 deletions(-) diff --git a/CookieTest.java b/CookieTest.java index 8f4cf149b..d8453e943 100644 --- a/CookieTest.java +++ b/CookieTest.java @@ -1,6 +1,5 @@ package org.json.junit; -import java.io.*; import java.util.*; import static org.junit.Assert.*; @@ -11,33 +10,57 @@ /** * Tests for JSON-Java Cookie.java + * Paraphrased from: + * http://www.nczonline.net/blog/2009/05/05/http-cookies-explained/ + * + * A web server specifies a cookie to be stored by sending an HTTP header + * called Set-Cookie. The format of the Set-Cookie header is a string as + * follows (parts in square brackets are optional): + * Set-Cookie: value[; expires=date][; domain=domain][; path=path][; secure] + * Where value is usually, but not always, a key/value pair: name=value + * Separators between the optional segments (e.g. expires=date) consist of a + * semicolon followed by a space. + * + * Although cookies are typically url-encoded, they don't have to be. + * + * expires date example: + * Set-Cookie: name=Nicholas; expires=Sat, 02 May 2009 23:38:25 GMT + * + * domain option example: + * Set-Cookie: name=Nicholas; domain=nczonline.net + * + * Path option example: + * Set-Cookie: name=Nicholas; path=/blog + * + * Secure option example (it is just a flag): + * Set-Cookie: name=Nicholas; secure + * + * Subcookies. There is a hard limit of size (4k) that can't be finessed. + * But many browsers (not Chrome) have a max cookies per domain limit + * (usually 50). To get around this, subcookies are encoded in the initial + * name/value pair as follows: + * name=a=b&c=d&e=f&g=h */ public class CookieTest { - String realWorldCookie = - "hpc=d=I.aZLE4l.8DeqRynle2fTnKxdAycw3CvCrzMNofhR9a5vYaU.XnHk6n3ZenMs6Xqq"+ - "3Mc5kMw.M1c.vR6zdxVMsfAQn75WNaFp8mY3UQgEw8lvIAbZvT_PiJofv7OMCbabUOe1Efd"+ - "i2M5.aVTX2bHB3EJPhNQNe0B5PL6mGbz7KYYyujkcn6hVS7U5d5OYv7L0GSAiKY-&v=2; y"+ - "wadp115488662=3370273056; AO=u=1&o=1; ywandp=10001806365479:1024785001;"+ - "10001576721379:3531995934; fpc=10001806365479:ZblWsSPj||;10001576721379"+ - ":ZY1jZhRq||; V=v=0.7&m=0&ccOptions={\"show\":false,\"lang\":\"en\",\"fo"+ - "ntSize\":24,\"fontName\":\"Helvetica Neue,Helvetica,Arial,_sans\",\"fon"+ - "tColor\":\"#ffffff\",\"fontOpacity\":1,\"fontEffect\":\"none\",\"bgColo"+ - "r\":\"#000000\",\"bgOpacity\":0.75}; yvap=193@yvap=193@cc=1@al=1@vl=0@r"+ - "vl=0@ac=1@rvl_NFL=0@session_NFL=0@lmsID=@rcc=0; YLS=v=1&p=1&n=1; ucs=tr"+ - "=1424831973913&sfcTs=1425971131&sfc=1; B=26tgei1adfl2v&b=4&d=j7.bbChrYH"+ - "1Ww.22z25N3S2YRsiX.e8VKSZpZdjeYXeN.w--&s=lr; F=a=MVvM8WsMvSxoU9K4FcyMxZ"+ - ".lwmw1yLWpNLOZbMVqjDB8d.bZm1C1JJVJFfCXcy3YfSZy47VAvKKSGZBmM1HQdIUWJA--&"+ - "b=PW8Y; YP=v=AwAAY&d=AEcAMEQCIHHEk.ugtA0iqWk_ctLMBWKG_gJfDzKX.tlKIIGBVH"+ - "cTAiBgmZUHV73V2i80FgqcVjQnvNTyor0rYBXsjhXBul2PzwA-; ypcdb=096e88ca6ff13"+ - "fee954ee414bb7b9362; Y=v=1&n=edbmi9njnt2h1&p=; CRZY={\"33935700511_2015"+ - "0317\":{\"expires\":1426808579870,\"data\":{\"nv\":1,\"bn\":1,\"collaps"+ - "ed\":0}},\"33726925511_20150318\":{\"expires\":1426859124988,\"data\":{"+ - "\"nv\":7,\"bn\":0,\"collapsed\":0}},\"33748770511_20150318\":{\"expires"+ - "\":1426911961098,\"data\":{\"nv\":2,\"bn\":0,\"collapsed\":0}}}; apeaf="+ - "td-applet-stream={\"tmpl\":\"items\",\"po\":{\"2409678.20150318\":{\"c"+ - "\":0,\"v\":2,\"ts\":1426719393315}}}"; - + String simpleCookieStr = + "PH=deleted"+ + "; expires=Wed, 19-Mar-2014 17:53:53 GMT"+ + ";path=/"+ + "; domain=.yahoo.com"+ + ";secure"+ + ";not=included"; + + String encodedCookieStr = + "PH=contains+some+chars"+ + ";expires=Wed, 19-Mar-2014 17:53:53 GMT"+ + "; path=/"+ + ";domain=.yahoo.com?some+escape+chars"+ + "; secure"+ + "; CRZY=%7B%2233748770511_20150319%22%3A%7B%22expires%22%3A142696041"+ + "3419%2C%22data%22%3A%7B%22nv%22%3A3%2C%22bn%22%3A0%2C%22collapsed%2"+ + "2%3A0%7D%7D%7D"; + @Test(expected=NullPointerException.class) public void shouldHandleNullCookie() { String cookieStr = null; @@ -51,16 +74,147 @@ public void shouldHandleEmptyStringCookie() { } @Test - public void shouldHandleSimpleCookie() { - String cookieStr = "abc=def"; - JSONObject jsonObject = Cookie.toJSONObject(cookieStr); + public void shouldHandleNonEncodedCookie() { + JSONObject jsonObject = Cookie.toJSONObject(simpleCookieStr); Set keySet = jsonObject.keySet(); - assertTrue("Keyset should have exactly 2 keys", keySet.size() == 2); + assertTrue("Keyset should have exactly 7 keys", keySet.size() == 7); assertTrue("name should have expected value", - jsonObject.getString("name").equals("abc")); + "PH".equals(jsonObject.getString("name"))); assertTrue("Value should have expected value", - jsonObject.getString("value").equals("def")); + "deleted".equals(jsonObject.getString("value"))); + assertTrue("expires should have expected value", + "Wed, 19-Mar-2014 17:53:53 GMT".equals( + jsonObject.getString("expires"))); + assertTrue("domain should have expected value", + ".yahoo.com".equals( + jsonObject.getString("domain"))); + assertTrue("path should have expected value", + "/".equals( + jsonObject.getString("path"))); + assertTrue("not should have expected value", + "included".equals( + jsonObject.getString("not"))); + Boolean secureBool = jsonObject.getBoolean("secure"); + assertTrue("secure should be found in jsonObject", secureBool != null); + assertTrue("secure should have expected value", + secureBool.equals(true)); + } + + @Test + public void shouldConvertNonEncodedCookieToString() { + int idx; + String expectedStr; + JSONObject jsonObject = Cookie.toJSONObject(simpleCookieStr); + String cookieStr = Cookie.toString(jsonObject); + + // check for unordered expected output + expectedStr = "path=/"; + idx = cookieStr.indexOf(expectedStr); + assertTrue("path should be included in string output", idx != -1); + cookieStr = cookieStr.substring(0, idx)+ + cookieStr.substring(idx+expectedStr.length()); + + expectedStr = "expires=Wed, 19-Mar-2014 17:53:53 GMT"; + idx = cookieStr.indexOf(expectedStr); + assertTrue("expires should be included in string output", idx != -1); + cookieStr = cookieStr.substring(0, idx)+ + cookieStr.substring(idx+expectedStr.length()); + expectedStr = "domain=.yahoo.com"; + idx = cookieStr.indexOf(expectedStr); + assertTrue("domain should be included in string output", idx != -1); + cookieStr = cookieStr.substring(0, idx)+ + cookieStr.substring(idx+expectedStr.length()); + + expectedStr = "PH=deleted"; + idx = cookieStr.indexOf(expectedStr); + assertTrue("name/value should be included in string output", idx != -1); + cookieStr = cookieStr.substring(0, idx)+ + cookieStr.substring(idx+expectedStr.length()); + + expectedStr = "secure"; + idx = cookieStr.indexOf(expectedStr); + assertTrue("secure should be included in string output", idx != -1); + cookieStr = cookieStr.substring(0, idx)+ + cookieStr.substring(idx+expectedStr.length()); + + // after semicolons, nothing should be left + cookieStr = cookieStr.replaceAll(";", ""); + assertTrue("nothing else should remain in cookie toString()", + cookieStr.length() == 0); + } + + @Test + public void shouldHandleEncodedCookie() { + JSONObject jsonObject = Cookie.toJSONObject(encodedCookieStr); + Set keySet = jsonObject.keySet(); + // Note: the 7th key/value is not used by Cookie.java + assertTrue("Keyset should have exactly 7 keys", keySet.size() == 7); + assertTrue("name should have expected value", + "PH".equals(jsonObject.getString("name"))); + assertTrue("Value should have expected value", + "contains+some+chars".equals(jsonObject.getString("value"))); + assertTrue("expires should have expected value", + "Wed, 19-Mar-2014 17:53:53 GMT".equals( + jsonObject.getString("expires"))); + assertTrue("domain should have expected value", + ".yahoo.com?some escape chars".equals( + jsonObject.getString("domain"))); + assertTrue("path should have expected value", + "/".equals( + jsonObject.getString("path"))); + Boolean secureBool = jsonObject.getBoolean("secure"); + assertTrue("secure should be found in jsonObject", secureBool != null); + assertTrue("secure should have expected value", + secureBool.equals(true)); + String expectedStr = "{\"33748770511_20150319\":{\"expires\":14269604134"+ + "19,\"data\":{\"nv\":3,\"bn\":0,\"collapsed\":0}}}"; + assertTrue("CRZY should have expected value", + expectedStr.equals(jsonObject.getString("CRZY"))); } + @Test + public void shouldConvertEncodedCookieToString() { + int idx; + String expectedStr; + JSONObject jsonObject = Cookie.toJSONObject(encodedCookieStr); + String cookieStr = Cookie.toString(jsonObject); + + // check for unordered expected output + expectedStr = "path=/"; + idx = cookieStr.indexOf(expectedStr); + assertTrue("path should be included in string output", idx != -1); + cookieStr = cookieStr.substring(0, idx)+ + cookieStr.substring(idx+expectedStr.length()); + + expectedStr = "expires=Wed, 19-Mar-2014 17:53:53 GMT"; + idx = cookieStr.indexOf(expectedStr); + assertTrue("expires should be included in string output", idx != -1); + cookieStr = cookieStr.substring(0, idx)+ + cookieStr.substring(idx+expectedStr.length()); + + expectedStr = "domain=.yahoo.com?some escape chars"; + idx = cookieStr.indexOf(expectedStr); + assertTrue("domain should be included in string output", idx != -1); + cookieStr = cookieStr.substring(0, idx)+ + cookieStr.substring(idx+expectedStr.length()); + + expectedStr = "PH=contains%2bsome%2bchars"; + idx = cookieStr.indexOf(expectedStr); + assertTrue("name/value should be included in string output", idx != -1); + cookieStr = cookieStr.substring(0, idx)+ + cookieStr.substring(idx+expectedStr.length()); + + expectedStr = "secure"; + idx = cookieStr.indexOf(expectedStr); + assertTrue("secure should be included in string output", idx != -1); + cookieStr = cookieStr.substring(0, idx)+ + cookieStr.substring(idx+expectedStr.length()); + + // after semicolons, nothing should be left + cookieStr = cookieStr.replaceAll(";", ""); + assertTrue("nothing else should remain in cookie toString()", + cookieStr.length() == 0); + } + } From fac377fc134dc2b45d0d8d0b6c45fe310cc14161 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 19 Mar 2015 16:12:47 -0500 Subject: [PATCH 014/315] Update README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bef669670..0d3489c32 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,17 @@ Test harness: http://junit.org Coverage: http://www.eclemma.org/ +Run individual tests using eclemma or the entire test suite using TestRunner + Completed tests: CDLTest.java +CookieTest.java + In progress: -CookieTest.java +PropertyTest.java + From e899a2970d1fc5df19008f96537740c7e45cceb1 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 19 Mar 2015 17:30:44 -0500 Subject: [PATCH 015/315] PropertTest.java coverage 94.8% --- JunitTestSuite.java | 3 +- PropertyTest.java | 81 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 PropertyTest.java diff --git a/JunitTestSuite.java b/JunitTestSuite.java index 20b69f9ce..b9abad100 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -5,7 +5,8 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ CDLTest.class, - CookieTest.class + CookieTest.class, + PropertyTest.class }) public class JunitTestSuite { } \ No newline at end of file diff --git a/PropertyTest.java b/PropertyTest.java new file mode 100644 index 000000000..435448894 --- /dev/null +++ b/PropertyTest.java @@ -0,0 +1,81 @@ +package org.json.junit; + +import java.util.*; + +import static org.junit.Assert.*; + +import org.json.*; +import org.junit.Test; + + +/** + * Tests for JSON-Java Property.java + */ +public class PropertyTest { + + @Test + public void shouldHandleNullProperties() { + + Properties properties = null; + JSONObject jsonObject = Property.toJSONObject(properties); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + @Test + public void shouldHandleEmptyProperties() { + + Properties properties = new Properties(); + JSONObject jsonObject = Property.toJSONObject(properties); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + @Test + public void shouldHandleProperties() { + Properties properties = new Properties(); + + properties.put("Illinois", "Springfield"); + properties.put("Missouri", "Jefferson City"); + properties.put("Washington", "Olympia"); + properties.put("California", "Sacramento"); + properties.put("Indiana", "Indianapolis"); + + JSONObject jsonObject = Property.toJSONObject(properties); + + assertTrue("jsonObject should contain 5 items", jsonObject.length() == 5); + assertTrue("jsonObject should contain Illinois property", + "Springfield".equals(jsonObject.get("Illinois"))); + assertTrue("jsonObject should contain Missouri property", + "Jefferson City".equals(jsonObject.get("Missouri"))); + assertTrue("jsonObject should contain Washington property", + "Olympia".equals(jsonObject.get("Washington"))); + assertTrue("jsonObject should contain California property", + "Sacramento".equals(jsonObject.get("California"))); + assertTrue("jsonObject should contain Indiana property", + "Indianapolis".equals(jsonObject.get("Indiana"))); + } + + @Test + public void shouldHandleNullJSONProperty() { + JSONObject jsonObject= null; + Properties properties = Property.toProperties(jsonObject); + assertTrue("properties should be empty", + properties.size() == 0); + } + + @Test + public void shouldHandleJSONProperty() { + Properties properties = new Properties(); + + properties.put("Illinois", "Springfield"); + properties.put("Missouri", "Jefferson City"); + properties.put("Washington", "Olympia"); + properties.put("California", "Sacramento"); + properties.put("Indiana", "Indianapolis"); + + JSONObject jsonObject = Property.toJSONObject(properties); + Properties jsonProperties = Property.toProperties(jsonObject); + + assertTrue("property objects should match", + properties.equals(jsonProperties)); + } +} \ No newline at end of file From fd56452f6ba8941e786316f4d7229df6cf8ce7a5 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 19 Mar 2015 17:35:16 -0500 Subject: [PATCH 016/315] Update README.md --- README.md | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 0d3489c32..e750b7212 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,13 @@ # JSON-Java-unit-test -Unit tests to validate the JSON-Java GitHub project code (https://github.com/douglascrockford/JSON-java). - -Test harness: http://junit.org - -Coverage: http://www.eclemma.org/ - -Run individual tests using eclemma or the entire test suite using TestRunner - -Completed tests: - -CDLTest.java - -CookieTest.java - -In progress: - -PropertyTest.java - +Unit tests to validate the JSON-Java GitHub project code (https://github.com/douglascrockford/JSON-java).
+Test harness: http://junit.org
+Coverage: http://www.eclemma.org/
+Run individual tests using eclemma or the entire test suite using TestRunner
+Completed tests:
+CDLTest.java
+CookieTest.java
+PropertyTest.java
+In progress:
+XMLTest.java
From b5b4961a9dfd8b9077866dd54504697b5425d7c2 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 20 Mar 2015 07:34:03 -0500 Subject: [PATCH 017/315] cdltest refactor, in progress --- CDLTest.java | 70 ++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index d5ead3c4a..78e4fa2aa 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -41,6 +41,12 @@ public class CDLTest { "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" + "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n" ); + String jsonArrayLines = new String( + "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, "+ + "{Col 1:1, Col 2:2, Col 3:3, Col 4:4, Col 5:5, Col 6:6, Col 7:7}, "+ + "{Col 1:true, Col 2:false, Col 3:true, Col 4:true, Col 5:false, Col 6:false, Col 7:false}, "+ + "{Col 1:0.23, Col 2:57.42, Col 3:-234.879, Col 4:2.34e5, Col 5:2.34e5, Col 6:0.0, Col 7:9e-3}, "+ + "{Col 1:\"val1\", Col 2:\"val2\", Col 3:val3, Col 4\"val4\", Col 5:val5, Col 6:val6, Col 7:val7}]"); @Test(expected=NullPointerException.class) public void shouldThrowExceptionOnNullString() { @@ -48,6 +54,24 @@ public void shouldThrowExceptionOnNullString() { CDL.toJSONArray(nullStr); } + @Test(expected=JSONException.class) + public void shouldHandleUnbalancedQuoteInName() { + String badLine = "Col1, \"Col2\nVal1, Val2"; + CDL.toJSONArray(badLine); + } + + @Test(expected=JSONException.class) + public void shouldHandleUnbalancedQuoteInValue() { + String badLine = "Col1, Col2\n\"Val1, Val2"; + CDL.toJSONArray(badLine); + } + + @Test(expected=JSONException.class) + public void shouldHandleNullInName() { + String badLine = "C\0ol1, Col2\nVal1, Val2"; + CDL.toJSONArray(badLine); + } + @Test /** * Note: This test reveals a bug in the method JavaDoc. It should @@ -74,6 +98,7 @@ public void shouldHandleEmptyString() { @Test public void toStringShouldCheckSpecialChars() { + String method = "toStringShouldCheckSpecialChars():"; /** * Given a JSONArray that was not built by CDL, some chars may be * found that would otherwise be filtered out by CDL. @@ -81,49 +106,24 @@ public void toStringShouldCheckSpecialChars() { JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); jsonArray.put(jsonObject); - // \r will be filtered from name + // \r should be filtered from name jsonObject.put("Col \r1", "V1"); - // \r will be filtered from value + // \r should be filtered from value jsonObject.put("Col 2", "V2\r"); - boolean normalize = true; - List> expectedLines = - sortColumnsInLines("Col 1, Col 2,\nV1, V2", normalize); - List> jsonArrayLines = - sortColumnsInLines(CDL.toString(jsonArray), normalize); - if (!expectedLines.equals(jsonArrayLines)) { - System.out.println("expected: " +expectedLines); - System.out.println("jsonArray: " +jsonArrayLines); - assertTrue("Should filter out certain chars", - false); - } - } - - @Test(expected=JSONException.class) - public void shouldHandleUnbalancedQuoteInName() { - String badLine = "Col1, \"Col2\nVal1, Val2"; - CDL.toJSONArray(badLine); - } - - @Test(expected=JSONException.class) - public void shouldHandleUnbalancedQuoteInValue() { - String badLine = "Col1, Col2\n\"Val1, Val2"; - CDL.toJSONArray(badLine); - } + String jsonStr = CDL.toString(jsonArray); - @Test(expected=JSONException.class) - public void shouldHandleNullInName() { - String badLine = "C\0ol1, Col2\nVal1, Val2"; - CDL.toJSONArray(badLine); + String[] expectedStr = {"\"Col 1\"","Col 2","V1","\"V2\""}; + Util.checkAndRemoveStrings(jsonStr, expectedStr, "[,\n\"]", method); } @Test public void shouldConvertCDLToJSONArray() { + String method = "shouldConvertCDLToJSONArray(): "; JSONArray jsonArray = CDL.toJSONArray(lines); - String resultStr = compareJSONArrayToString(jsonArray, lines); - if (resultStr != null) { - assertTrue("CDL should convert string to JSONArray: " + - resultStr, false); - } + String jsonStr = CDL.toString(jsonArray); + JSONArray expectedJsonArray = new JSONArray(jsonArrayLines); + assertTrue("CDL should convert string to JSONArray", + jsonArray.equals(expectedJsonArray)); } @Test From 441a00afc92ec9655f512f40f9b05ce83c634267 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 20 Mar 2015 15:34:47 -0500 Subject: [PATCH 018/315] CDLTest still in progress --- CDLTest.java | 75 +++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 42 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index 78e4fa2aa..f79f2f039 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -41,6 +41,11 @@ public class CDLTest { "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" + "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n" ); + + /** + * If lines were to be converted to a JSONArray without the benefit of CDL, + * this would be the input string + */ String jsonArrayLines = new String( "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, "+ "{Col 1:1, Col 2:2, Col 3:3, Col 4:4, Col 5:5, Col 6:6, Col 7:7}, "+ @@ -55,19 +60,19 @@ public void shouldThrowExceptionOnNullString() { } @Test(expected=JSONException.class) - public void shouldHandleUnbalancedQuoteInName() { + public void shouldThrowExceptionOnUnbalancedQuoteInName() { String badLine = "Col1, \"Col2\nVal1, Val2"; CDL.toJSONArray(badLine); } @Test(expected=JSONException.class) - public void shouldHandleUnbalancedQuoteInValue() { + public void shouldThrowExceptionOnUnbalancedQuoteInValue() { String badLine = "Col1, Col2\n\"Val1, Val2"; CDL.toJSONArray(badLine); } @Test(expected=JSONException.class) - public void shouldHandleNullInName() { + public void shouldThrowExceptionOnNullInName() { String badLine = "C\0ol1, Col2\nVal1, Val2"; CDL.toJSONArray(badLine); } @@ -77,7 +82,7 @@ public void shouldHandleNullInName() { * Note: This test reveals a bug in the method JavaDoc. It should * mention it might return null, or it should return an empty JSONArray. */ - public void shouldHandleOnlyColumnNames() { + public void shouldReturnNullOnOnlyColumnNames() { String columnNameStr = "col1, col2, col3"; JSONArray jsonArray = CDL.toJSONArray(columnNameStr); assertTrue("CDL should return null when only 1 row is given", @@ -89,13 +94,33 @@ public void shouldHandleOnlyColumnNames() { * Note: This test reveals a bug in the method JavaDoc. It should * mention it might return null, or it should return an empty JSONArray. */ - public void shouldHandleEmptyString() { + public void shouldReturnNullOnEmptyString() { String emptyStr = ""; JSONArray jsonArray = CDL.toJSONArray(emptyStr); assertTrue("CDL should return null when the input string is empty", jsonArray == null); } + @Test + public void shouldConvertCDLToJSONArray() { + JSONArray jsonArray = CDL.toJSONArray(lines); + JSONArray expectedJsonArray = new JSONArray(jsonArrayLines); + assertTrue("CDL should convert string to JSONArray", + jsonArray.equals(expectedJsonArray)); + } + + @Test + public void shouldCreateJSONArrayUsingJSONArray() { + String names = "[Col1, Col2]"; + String values = "V1, V2"; + JSONArray nameJSONArray = new JSONArray(names); + JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); + + JSONArray expectedJsonArray = new JSONArray("[{Col1:V1},{Col2:V2}]"); + assertTrue("CDL should create JSONArray from a names JSONArray and a "+ + "string of values", jsonArray.equals(expectedJsonArray)); + } + @Test public void toStringShouldCheckSpecialChars() { String method = "toStringShouldCheckSpecialChars():"; @@ -112,33 +137,8 @@ public void toStringShouldCheckSpecialChars() { jsonObject.put("Col 2", "V2\r"); String jsonStr = CDL.toString(jsonArray); - String[] expectedStr = {"\"Col 1\"","Col 2","V1","\"V2\""}; - Util.checkAndRemoveStrings(jsonStr, expectedStr, "[,\n\"]", method); - } - - @Test - public void shouldConvertCDLToJSONArray() { - String method = "shouldConvertCDLToJSONArray(): "; - JSONArray jsonArray = CDL.toJSONArray(lines); - String jsonStr = CDL.toString(jsonArray); - JSONArray expectedJsonArray = new JSONArray(jsonArrayLines); - assertTrue("CDL should convert string to JSONArray", - jsonArray.equals(expectedJsonArray)); - } - - @Test - public void shouldCreateJSONArrayUsingJSONArray() { - String names = "Col1, Col2"; - String nameArrayStr = "[" +names+ "]"; - String values = "V1, V2"; - JSONArray nameJSONArray = new JSONArray(nameArrayStr); - JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); - String combinedStr = names+ "\n" +values; - String resultStr = compareJSONArrayToString(jsonArray, combinedStr); - if (resultStr != null) { - assertTrue("CDL should convert JSONArray and string to JSONArray: " + - resultStr, false); - } + String expectedStr = {"Col 1, Col 2","V1","\"V2\""}; + // Util.checkAndRemoveStrings(jsonStr, expectedStr, "[,\n\"]", method); } @Test @@ -152,16 +152,7 @@ public void shouldConvertCDLToJSONArrayAndBackToString() { final boolean doNotNormalize = false; JSONArray jsonArray = CDL.toJSONArray(lines); String jsonStr = CDL.toString(jsonArray); - // normal sorted - List> sortedLines = sortColumnsInLines(lines, normalize); - // sorted, should already be normalized - List> sortedJsonStr = sortColumnsInLines(jsonStr, doNotNormalize); - boolean result = sortedLines.equals(sortedJsonStr); - if (!result) { - System.out.println("lines: " +sortedLines); - System.out.println("jsonStr: " +sortedJsonStr); - assertTrue("CDL should convert JSONArray back to original string: " + - lines.equals(jsonStr), false); + assertTrue("CDL should convert string to JSONArray to string", } } From 68e01afd7ea94a66432637c864fda2c032d0c8d2 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 20 Mar 2015 18:01:27 -0500 Subject: [PATCH 019/315] first checkin --- Util.java | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Util.java diff --git a/Util.java b/Util.java new file mode 100644 index 000000000..89743c089 --- /dev/null +++ b/Util.java @@ -0,0 +1,56 @@ +package org.json.junit; + +import static org.junit.Assert.*; + +public class Util { + + /** + * Utility method to check for a target string, then remove it from + * the string to be searched. + * @param jsonStr the string to be searched + * @param expectedStr the target string to search for + * @param assertStr the error message for the assert + * @return new string with target substring removed + */ + public static String checkAndRemoveString( + String jsonStr, String expectedStr, String assertStr) { + int idx = jsonStr.indexOf(expectedStr); + assertTrue(assertStr, idx != -1); + String newStr = jsonStr.substring(0, idx)+ + jsonStr.substring(idx+expectedStr.length()); + return newStr; + } + + /** + * Utility method to strip out selected punctuation chars and confirm + * that jsonStr is now empty + * @param jsonStr the string to be verified + * @param regexStr regex string of the chars to remove + * @param assertStr the error message for the assert + */ + public static void verifyEmptyJsonStr(String jsonStr, String regexStr, + String assertStr) { + jsonStr = jsonStr.replaceAll(regexStr, ""); + assertTrue(assertStr, jsonStr.length() == 0); + } + + /** + * Utility method to check for a set of target strings, + * then remove them from the string to be searched. + * When completed, punctuation marks are stripped out and + * the string to be searched is confirmed as empty + * @param jsonStr the string to be searched + * @param expectedStrArray the target strings to search for + * @param regexStr regex string of the chars to remove + * @param methodStr the method name + */ + public static void checkAndRemoveStrings(String jsonStr, + String[] expectedStr, String regexStr, String methodStr) { + for (int i = 0; i < expectedStr.length; ++i) { + jsonStr = Util.checkAndRemoveString(jsonStr, expectedStr[i], + methodStr+expectedStr+" should be included in string output"); + } + Util.verifyEmptyJsonStr(jsonStr, regexStr, + methodStr+" jsonStr should be empty"); + } +} From dca3726bf17c3b80a9b56567dc30661d0a9fa25f Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:13:44 -0500 Subject: [PATCH 020/315] Update README.md --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e750b7212..bfe1619a5 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,32 @@ # JSON-Java-unit-test Unit tests to validate the JSON-Java GitHub project code (https://github.com/douglascrockford/JSON-java).
+ Test harness: http://junit.org
Coverage: http://www.eclemma.org/
-Run individual tests using eclemma or the entire test suite using TestRunner
+ +Eclipse is the recommended development environment. +Run individual tests or *JunitTestSuite* using ~~EclEmma Coverage~~, or execute the *TestRunner* application directly.
+ +Test filenames should consist of the name of the module being tested, with the suffix "Test". +For example, *Cookie.java* is tested by *CookieTest.java*. +When adding a new unit test, don't forget to update *JunitTestSuite.java*. + +The fundamental issues with JSON-Java testing are: +* *JSONObjects* are unordered, making simple string comparison ineffective. +* Comparisons via equals() is not currently supported. Neither JSONArray nor JSONObject overrride hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful. +* Access to the JSONArray and JSONObject internal containers for comparison is not currently available. +* JSONObject sometimes wraps entries in quotes, other times does not, complicating comparisons. + +When you start working on a test, add the empty file to the repository, so that others will know that test is taken. + +A unit test is considered complete when the coverage is >= 90% as measured by EclEmma. + +| Test file name | Coverage | Comments | +| ------------- | ------------- | +| CookieTest.java | 97.5% | Completed | +| PropertyTest.java | 94.8% | Completed | +| CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked | + Completed tests:
CDLTest.java
CookieTest.java
From 1bde00ed4c6b42add6702f82a06f80c8262f00d3 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:14:23 -0500 Subject: [PATCH 021/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bfe1619a5..ee7238650 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Test harness: http://junit.org
Coverage: http://www.eclemma.org/
Eclipse is the recommended development environment. -Run individual tests or *JunitTestSuite* using ~~EclEmma Coverage~~, or execute the *TestRunner* application directly.
+Run individual tests or *JunitTestSuite* using *EclEmma Coverage*, or execute the _TestRunner_ application directly.
Test filenames should consist of the name of the module being tested, with the suffix "Test". For example, *Cookie.java* is tested by *CookieTest.java*. From b0ce7f3bd94ae52c1efa5a797bf473201edd3ef6 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:15:46 -0500 Subject: [PATCH 022/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ee7238650..0e03195dc 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Test harness: http://junit.org
Coverage: http://www.eclemma.org/
Eclipse is the recommended development environment. -Run individual tests or *JunitTestSuite* using *EclEmma Coverage*, or execute the _TestRunner_ application directly.
+Run individual tests or JunitTestSuite using *EclEmma Coverage*, or execute the TestRunner application directly.
Test filenames should consist of the name of the module being tested, with the suffix "Test". For example, *Cookie.java* is tested by *CookieTest.java*. From 09b5562ad9b699db72d92400ec16e4f03f563012 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:18:56 -0500 Subject: [PATCH 023/315] Update README.md --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0e03195dc..1837be94c 100644 --- a/README.md +++ b/README.md @@ -8,24 +8,24 @@ Eclipse is the recommended development environment. Run individual tests or JunitTestSuite using *EclEmma Coverage*, or execute the TestRunner application directly.
Test filenames should consist of the name of the module being tested, with the suffix "Test". -For example, *Cookie.java* is tested by *CookieTest.java*. -When adding a new unit test, don't forget to update *JunitTestSuite.java*. +For example, Cookie.java is tested by CookieTest.java. +When adding a new unit test, don't forget to update JunitTestSuite.java. The fundamental issues with JSON-Java testing are: -* *JSONObjects* are unordered, making simple string comparison ineffective. -* Comparisons via equals() is not currently supported. Neither JSONArray nor JSONObject overrride hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful. -* Access to the JSONArray and JSONObject internal containers for comparison is not currently available. -* JSONObject sometimes wraps entries in quotes, other times does not, complicating comparisons. +* JSONObjects are unordered, making simple string comparison ineffective. +* Comparisons via equals() is not currently supported. Neither JSONArray nor JSONObject overrride hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful. +* Access to the JSONArray and JSONObject internal containers for comparison is not currently available. +* JSONObject sometimes wraps entries in quotes, other times does not, complicating comparisons. When you start working on a test, add the empty file to the repository, so that others will know that test is taken. A unit test is considered complete when the coverage is >= 90% as measured by EclEmma. -| Test file name | Coverage | Comments | +| Test file name | Coverage | Comments | ------------- | ------------- | -| CookieTest.java | 97.5% | Completed | -| PropertyTest.java | 94.8% | Completed | -| CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked | +| CookieTest.java | 97.5% | Completed +| PropertyTest.java | 94.8% | Completed +| CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked Completed tests:
CDLTest.java
From 42ad66b032cf27d1ae13878ac9f7473c0e20605f Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:19:32 -0500 Subject: [PATCH 024/315] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1837be94c..0dd42ba0e 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,10 @@ When you start working on a test, add the empty file to the repository, so that A unit test is considered complete when the coverage is >= 90% as measured by EclEmma. -| Test file name | Coverage | Comments +| Test file name | Coverage | Comments | | ------------- | ------------- | -| CookieTest.java | 97.5% | Completed -| PropertyTest.java | 94.8% | Completed +| CookieTest.java | 97.5% | Completed | +| PropertyTest.java | 94.8% | Completed | | CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked Completed tests:
From 2b0a9cc7c82c766348131530d6e9a4224b085951 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:19:58 -0500 Subject: [PATCH 025/315] Update README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0dd42ba0e..4c9de4a83 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,15 @@ When you start working on a test, add the empty file to the repository, so that A unit test is considered complete when the coverage is >= 90% as measured by EclEmma. | Test file name | Coverage | Comments | + | ------------- | ------------- | + | CookieTest.java | 97.5% | Completed | + | PropertyTest.java | 94.8% | Completed | -| CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked + +| CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked | + Completed tests:
CDLTest.java
From 8374382947a12c388be9979fa0502cc526849c2e Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:22:54 -0500 Subject: [PATCH 026/315] Update README.md --- README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4c9de4a83..d41135b7e 100644 --- a/README.md +++ b/README.md @@ -17,18 +17,14 @@ The fundamental issues with JSON-Java testing are: * Access to the JSONArray and JSONObject internal containers for comparison is not currently available. * JSONObject sometimes wraps entries in quotes, other times does not, complicating comparisons. -When you start working on a test, add the empty file to the repository, so that others will know that test is taken. +When you start working on a test, add the empty file to the repository and update the readme, so that others will know that test is taken. A unit test is considered complete when the coverage is >= 90% as measured by EclEmma. -| Test file name | Coverage | Comments | - -| ------------- | ------------- | - +| Test-file-name | Coverage | Comments | +| ------------- | ------------- | ---- | | CookieTest.java | 97.5% | Completed | - | PropertyTest.java | 94.8% | Completed | - | CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked | From e150039261f5dbba8b7ca1948457487033501192 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:23:49 -0500 Subject: [PATCH 027/315] Update README.md --- README.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d41135b7e..ecb61f6ee 100644 --- a/README.md +++ b/README.md @@ -21,18 +21,10 @@ When you start working on a test, add the empty file to the repository and updat A unit test is considered complete when the coverage is >= 90% as measured by EclEmma. -| Test-file-name | Coverage | Comments | +| Test file name | Coverage | Comments | | ------------- | ------------- | ---- | | CookieTest.java | 97.5% | Completed | | PropertyTest.java | 94.8% | Completed | | CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked | - - -Completed tests:
-CDLTest.java
-CookieTest.java
-PropertyTest.java
-In progress:
-XMLTest.java
- +| XMLTest.java | 0% | Just started - stleary | From f8c37b1e05cf07f4ec26ce2d6304f62c526a4472 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:27:38 -0500 Subject: [PATCH 028/315] Update README.md --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index ecb61f6ee..0e9ef5a6e 100644 --- a/README.md +++ b/README.md @@ -27,4 +27,18 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | PropertyTest.java | 94.8% | Completed | | CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked | | XMLTest.java | 0% | Just started - stleary | +| | | | +| CookieList.java | | | +| HTTP.java | | | +| HTTPTokener.java | | | +| JSONArray.java | | | +|JSONException.java | | | +| JSONML.java | | | +| JSONObject.java | | | +| JSONString.java | | | +| JSONStringer.java | | | +| JSONTokener.java | | | +| JSONWriter.java | | | +| XMLTokener.java| | | + From e4ebd8c44325805a0f272a7fd898b669fb04929b Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:32:47 -0500 Subject: [PATCH 029/315] Update README.md --- README.md | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0e9ef5a6e..f6140e530 100644 --- a/README.md +++ b/README.md @@ -23,22 +23,25 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | +| Total coverage | 26.5% | | | +| | | | | CookieTest.java | 97.5% | Completed | | PropertyTest.java | 94.8% | Completed | | CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked | | XMLTest.java | 0% | Just started - stleary | | | | | -| CookieList.java | | | -| HTTP.java | | | -| HTTPTokener.java | | | -| JSONArray.java | | | -|JSONException.java | | | -| JSONML.java | | | -| JSONObject.java | | | +| CookieList.java |0% | | +| HTTP.java | 0%| | +| HTTPTokener.java |0% | | +| JSONArray.java |25.3% | | +|JSONException.java | 26.7% | | +| JSONML.java | 0%| | +| JSONObject.java | 27.6% | | +| JSONObject.Null | 18.8% | | | | JSONString.java | | | -| JSONStringer.java | | | -| JSONTokener.java | | | -| JSONWriter.java | | | -| XMLTokener.java| | | +| JSONStringer.java | 0%| | +| JSONTokener.java |65.4% | | +| JSONWriter.java | 0% | | +| XMLTokener.java| 0%| | From e4ef254d8f08ae7e40220a043b1a9407797d3b5a Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:33:12 -0500 Subject: [PATCH 030/315] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index f6140e530..4bd1e9831 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,6 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | JSONML.java | 0%| | | JSONObject.java | 27.6% | | | JSONObject.Null | 18.8% | | | -| JSONString.java | | | | JSONStringer.java | 0%| | | JSONTokener.java |65.4% | | | JSONWriter.java | 0% | | From 25596c9578b78c2fa84bf6629878f7b803d50b34 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:34:04 -0500 Subject: [PATCH 031/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4bd1e9831..2993938fe 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ When adding a new unit test, don't forget to update JunitTestSuite.java. The fundamental issues with JSON-Java testing are: * JSONObjects are unordered, making simple string comparison ineffective. -* Comparisons via equals() is not currently supported. Neither JSONArray nor JSONObject overrride hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful. +* Comparisons via **equals()** is not currently supported. Neither JSONArray nor JSONObject overrride hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful. * Access to the JSONArray and JSONObject internal containers for comparison is not currently available. * JSONObject sometimes wraps entries in quotes, other times does not, complicating comparisons. From d0223c2d08a9e188f841e4dbce0c3e1a72b64c55 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:35:24 -0500 Subject: [PATCH 032/315] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2993938fe..c447ebaf6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # JSON-Java-unit-test + Unit tests to validate the JSON-Java GitHub project code (https://github.com/douglascrockford/JSON-java).
Test harness: http://junit.org
From 231c3de79ec9de45a80be147bb41a5e85bd972e8 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:41:25 -0500 Subject: [PATCH 033/315] Update README.md --- README.md | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c447ebaf6..9227a0e8e 100644 --- a/README.md +++ b/README.md @@ -24,24 +24,22 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 26.5% | | | +| Total coverage | 19% | | | | | | | -| CookieTest.java | 97.5% | Completed | -| PropertyTest.java | 94.8% | Completed | | CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked | -| XMLTest.java | 0% | Just started - stleary | -| | | | +| CookieTest.java | 97.5% | Completed | | CookieList.java |0% | | | HTTP.java | 0%| | | HTTPTokener.java |0% | | -| JSONArray.java |25.3% | | +| JSONArray.java |15.3% | | |JSONException.java | 26.7% | | | JSONML.java | 0%| | -| JSONObject.java | 27.6% | | -| JSONObject.Null | 18.8% | | | +| JSONObject.Null | 8.9% | | | | JSONStringer.java | 0%| | -| JSONTokener.java |65.4% | | +| JSONTokener.java |59.8% | | | JSONWriter.java | 0% | | +| PropertyTest.java | 94.8% | Completed | +| XMLTest.java | 0% | Just started - stleary | | XMLTokener.java| 0%| | From 30c31db095a2f5d4515ec971f7c736fe4c204dc4 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 21 Mar 2015 13:44:29 -0500 Subject: [PATCH 034/315] Reverting to https://github.com/stleary/JSON-Java-unit-test/commit/4a5809910bc7a2bbe5892f9336eae0a609af9912 until I have figured out the best way to refactor the code --- CDLTest.java | 125 +++++++++++++++++++++++++++------------------------ 1 file changed, 67 insertions(+), 58 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index f79f2f039..d5ead3c4a 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -42,47 +42,18 @@ public class CDLTest { "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n" ); - /** - * If lines were to be converted to a JSONArray without the benefit of CDL, - * this would be the input string - */ - String jsonArrayLines = new String( - "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, "+ - "{Col 1:1, Col 2:2, Col 3:3, Col 4:4, Col 5:5, Col 6:6, Col 7:7}, "+ - "{Col 1:true, Col 2:false, Col 3:true, Col 4:true, Col 5:false, Col 6:false, Col 7:false}, "+ - "{Col 1:0.23, Col 2:57.42, Col 3:-234.879, Col 4:2.34e5, Col 5:2.34e5, Col 6:0.0, Col 7:9e-3}, "+ - "{Col 1:\"val1\", Col 2:\"val2\", Col 3:val3, Col 4\"val4\", Col 5:val5, Col 6:val6, Col 7:val7}]"); - @Test(expected=NullPointerException.class) public void shouldThrowExceptionOnNullString() { String nullStr = null; CDL.toJSONArray(nullStr); } - @Test(expected=JSONException.class) - public void shouldThrowExceptionOnUnbalancedQuoteInName() { - String badLine = "Col1, \"Col2\nVal1, Val2"; - CDL.toJSONArray(badLine); - } - - @Test(expected=JSONException.class) - public void shouldThrowExceptionOnUnbalancedQuoteInValue() { - String badLine = "Col1, Col2\n\"Val1, Val2"; - CDL.toJSONArray(badLine); - } - - @Test(expected=JSONException.class) - public void shouldThrowExceptionOnNullInName() { - String badLine = "C\0ol1, Col2\nVal1, Val2"; - CDL.toJSONArray(badLine); - } - @Test /** * Note: This test reveals a bug in the method JavaDoc. It should * mention it might return null, or it should return an empty JSONArray. */ - public void shouldReturnNullOnOnlyColumnNames() { + public void shouldHandleOnlyColumnNames() { String columnNameStr = "col1, col2, col3"; JSONArray jsonArray = CDL.toJSONArray(columnNameStr); assertTrue("CDL should return null when only 1 row is given", @@ -94,36 +65,15 @@ public void shouldReturnNullOnOnlyColumnNames() { * Note: This test reveals a bug in the method JavaDoc. It should * mention it might return null, or it should return an empty JSONArray. */ - public void shouldReturnNullOnEmptyString() { + public void shouldHandleEmptyString() { String emptyStr = ""; JSONArray jsonArray = CDL.toJSONArray(emptyStr); assertTrue("CDL should return null when the input string is empty", jsonArray == null); } - @Test - public void shouldConvertCDLToJSONArray() { - JSONArray jsonArray = CDL.toJSONArray(lines); - JSONArray expectedJsonArray = new JSONArray(jsonArrayLines); - assertTrue("CDL should convert string to JSONArray", - jsonArray.equals(expectedJsonArray)); - } - - @Test - public void shouldCreateJSONArrayUsingJSONArray() { - String names = "[Col1, Col2]"; - String values = "V1, V2"; - JSONArray nameJSONArray = new JSONArray(names); - JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); - - JSONArray expectedJsonArray = new JSONArray("[{Col1:V1},{Col2:V2}]"); - assertTrue("CDL should create JSONArray from a names JSONArray and a "+ - "string of values", jsonArray.equals(expectedJsonArray)); - } - @Test public void toStringShouldCheckSpecialChars() { - String method = "toStringShouldCheckSpecialChars():"; /** * Given a JSONArray that was not built by CDL, some chars may be * found that would otherwise be filtered out by CDL. @@ -131,14 +81,64 @@ public void toStringShouldCheckSpecialChars() { JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); jsonArray.put(jsonObject); - // \r should be filtered from name + // \r will be filtered from name jsonObject.put("Col \r1", "V1"); - // \r should be filtered from value + // \r will be filtered from value jsonObject.put("Col 2", "V2\r"); - String jsonStr = CDL.toString(jsonArray); + boolean normalize = true; + List> expectedLines = + sortColumnsInLines("Col 1, Col 2,\nV1, V2", normalize); + List> jsonArrayLines = + sortColumnsInLines(CDL.toString(jsonArray), normalize); + if (!expectedLines.equals(jsonArrayLines)) { + System.out.println("expected: " +expectedLines); + System.out.println("jsonArray: " +jsonArrayLines); + assertTrue("Should filter out certain chars", + false); + } + } + + @Test(expected=JSONException.class) + public void shouldHandleUnbalancedQuoteInName() { + String badLine = "Col1, \"Col2\nVal1, Val2"; + CDL.toJSONArray(badLine); + } - String expectedStr = {"Col 1, Col 2","V1","\"V2\""}; - // Util.checkAndRemoveStrings(jsonStr, expectedStr, "[,\n\"]", method); + @Test(expected=JSONException.class) + public void shouldHandleUnbalancedQuoteInValue() { + String badLine = "Col1, Col2\n\"Val1, Val2"; + CDL.toJSONArray(badLine); + } + + @Test(expected=JSONException.class) + public void shouldHandleNullInName() { + String badLine = "C\0ol1, Col2\nVal1, Val2"; + CDL.toJSONArray(badLine); + } + + @Test + public void shouldConvertCDLToJSONArray() { + JSONArray jsonArray = CDL.toJSONArray(lines); + String resultStr = compareJSONArrayToString(jsonArray, lines); + if (resultStr != null) { + assertTrue("CDL should convert string to JSONArray: " + + resultStr, false); + } + } + + @Test + public void shouldCreateJSONArrayUsingJSONArray() { + String names = "Col1, Col2"; + String nameArrayStr = "[" +names+ "]"; + String values = "V1, V2"; + JSONArray nameJSONArray = new JSONArray(nameArrayStr); + JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); + String combinedStr = names+ "\n" +values; + String resultStr = compareJSONArrayToString(jsonArray, combinedStr); + if (resultStr != null) { + assertTrue("CDL should convert JSONArray and string to JSONArray: " + + resultStr, false); + } } @Test @@ -152,7 +152,16 @@ public void shouldConvertCDLToJSONArrayAndBackToString() { final boolean doNotNormalize = false; JSONArray jsonArray = CDL.toJSONArray(lines); String jsonStr = CDL.toString(jsonArray); - assertTrue("CDL should convert string to JSONArray to string", + // normal sorted + List> sortedLines = sortColumnsInLines(lines, normalize); + // sorted, should already be normalized + List> sortedJsonStr = sortColumnsInLines(jsonStr, doNotNormalize); + boolean result = sortedLines.equals(sortedJsonStr); + if (!result) { + System.out.println("lines: " +sortedLines); + System.out.println("jsonStr: " +sortedJsonStr); + assertTrue("CDL should convert JSONArray back to original string: " + + lines.equals(jsonStr), false); } } From 2876b27ec56603de6627c7ef6d555ba021579c71 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 22 Mar 2015 17:48:21 -0500 Subject: [PATCH 035/315] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9227a0e8e..1cef7e35c 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,9 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 19% | | | +| Total coverage | 20.8% | | | | | | | -| CDLTest.java | 94.8% | Relies too much on string tests, needs to be reworked | +| CDLTest.java | 94.8% | Completed | | CookieTest.java | 97.5% | Completed | | CookieList.java |0% | | | HTTP.java | 0%| | @@ -34,9 +34,9 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | JSONArray.java |15.3% | | |JSONException.java | 26.7% | | | JSONML.java | 0%| | -| JSONObject.Null | 8.9% | | | +| JSONObject.Null | 13.4% | | | | JSONStringer.java | 0%| | -| JSONTokener.java |59.8% | | +| JSONTokener.java |65.4% | | | JSONWriter.java | 0% | | | PropertyTest.java | 94.8% | Completed | | XMLTest.java | 0% | Just started - stleary | From 964cb540fb4af3905e60199fc7ec8329cec923bf Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 22 Mar 2015 17:59:43 -0500 Subject: [PATCH 036/315] completed 94.8% coverage --- CDLTest.java | 263 ++++++++++++++++----------------------------------- 1 file changed, 79 insertions(+), 184 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index d5ead3c4a..4ff9c3814 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -1,30 +1,24 @@ package org.json.junit; -import static org.junit.Assert.*; -import java.io.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; + import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; +import static org.junit.Assert.*; import org.junit.Test; + import org.json.JSONException; import org.json.JSONObject; import org.json.JSONArray; import org.json.CDL; - /** - * Tests for {@link CDL}. + * Tests for CDL.java. * CDL provides an application level API, but it is not used by the * reference app. To test it, strings will be converted to JSON-Java classes * and then converted back. Since each row is an unordered JSONObject, * can't use a simple string compare to check for equality. * @author JSON.org - * @version 2015-03-18 + * @version 2015-03-22 * */ public class CDLTest { @@ -42,6 +36,20 @@ public class CDLTest { "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n" ); + /** + * Something I did not expect is that CDL.toJSONArray() adds all values as + * strings, with no filtering or conversions. I suppose this makes it + * easier to emit it as CDL later. For testing, it means that the + * expected JSONObject values all must be quoted in the cases where the + * JSONObject parsing might normally convert the value into a non-string. + */ + String expectedLines = new String( + "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, "+ + "{Col 1:1, Col 2:2, Col 3:3, Col 4:4, Col 5:5, Col 6:6, Col 7:7}, "+ + "{Col 1:true, Col 2:false, Col 3:true, Col 4:true, Col 5:false, Col 6:false, Col 7:false}, "+ + "{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, "+ + "{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va\'l6, Col 7:val7}]"); + @Test(expected=NullPointerException.class) public void shouldThrowExceptionOnNullString() { String nullStr = null; @@ -72,32 +80,6 @@ public void shouldHandleEmptyString() { jsonArray == null); } - @Test - public void toStringShouldCheckSpecialChars() { - /** - * Given a JSONArray that was not built by CDL, some chars may be - * found that would otherwise be filtered out by CDL. - */ - JSONArray jsonArray = new JSONArray(); - JSONObject jsonObject = new JSONObject(); - jsonArray.put(jsonObject); - // \r will be filtered from name - jsonObject.put("Col \r1", "V1"); - // \r will be filtered from value - jsonObject.put("Col 2", "V2\r"); - boolean normalize = true; - List> expectedLines = - sortColumnsInLines("Col 1, Col 2,\nV1, V2", normalize); - List> jsonArrayLines = - sortColumnsInLines(CDL.toString(jsonArray), normalize); - if (!expectedLines.equals(jsonArrayLines)) { - System.out.println("expected: " +expectedLines); - System.out.println("jsonArray: " +jsonArrayLines); - assertTrue("Should filter out certain chars", - false); - } - } - @Test(expected=JSONException.class) public void shouldHandleUnbalancedQuoteInName() { String badLine = "Col1, \"Col2\nVal1, Val2"; @@ -116,29 +98,46 @@ public void shouldHandleNullInName() { CDL.toJSONArray(badLine); } + + @Test + public void toStringShouldCheckSpecialChars() { + /** + * Given a JSONArray that was not built by CDL, some chars may be + * found that would otherwise be filtered out by CDL. + */ + JSONArray jsonArray = new JSONArray(); + JSONObject jsonObject = new JSONObject(); + jsonArray.put(jsonObject); + // \r will be filtered from name + jsonObject.put("Col \r1", "V1"); + // \r will be filtered from value + jsonObject.put("Col 2", "V2\r"); + assertTrue("expected length should be 1",jsonArray.length() == 1); + String cdlStr = CDL.toString(jsonArray); + jsonObject = jsonArray.getJSONObject(0); + assertTrue(cdlStr.contains("\"Col 1\"")); + assertTrue(cdlStr.contains("Col 2")); + assertTrue(cdlStr.contains("V1")); + assertTrue(cdlStr.contains("\"V2\"")); + } + @Test public void shouldConvertCDLToJSONArray() { + // this array is built by CDL JSONArray jsonArray = CDL.toJSONArray(lines); - String resultStr = compareJSONArrayToString(jsonArray, lines); - if (resultStr != null) { - assertTrue("CDL should convert string to JSONArray: " + - resultStr, false); - } + // This array is built from JSON parsing + JSONArray expectedJsonArray = new JSONArray(expectedLines); + compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } @Test public void shouldCreateJSONArrayUsingJSONArray() { - String names = "Col1, Col2"; - String nameArrayStr = "[" +names+ "]"; + String nameArrayStr = "[Col1, Col2]"; String values = "V1, V2"; JSONArray nameJSONArray = new JSONArray(nameArrayStr); JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); - String combinedStr = names+ "\n" +values; - String resultStr = compareJSONArrayToString(jsonArray, combinedStr); - if (resultStr != null) { - assertTrue("CDL should convert JSONArray and string to JSONArray: " + - resultStr, false); - } + JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]"); + compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } @Test @@ -148,147 +147,43 @@ public void shouldConvertCDLToJSONArrayAndBackToString() { * The string contains a typical variety of values * that might be found in a real CDL. */ - final boolean normalize = true; - final boolean doNotNormalize = false; JSONArray jsonArray = CDL.toJSONArray(lines); String jsonStr = CDL.toString(jsonArray); - // normal sorted - List> sortedLines = sortColumnsInLines(lines, normalize); - // sorted, should already be normalized - List> sortedJsonStr = sortColumnsInLines(jsonStr, doNotNormalize); - boolean result = sortedLines.equals(sortedJsonStr); - if (!result) { - System.out.println("lines: " +sortedLines); - System.out.println("jsonStr: " +sortedJsonStr); - assertTrue("CDL should convert JSONArray back to original string: " + - lines.equals(jsonStr), false); - } + JSONArray finalJsonArray = CDL.toJSONArray(jsonStr); + JSONArray expectedJsonArray = new JSONArray(expectedLines); + compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } - - - - /******************************************************************\ - * SUPPORT AND UTILITY - \******************************************************************/ - - /** - * Compares a JSON array to the original string. The top row of the - * string contains the JSONObject keys and the remaining rows contain - * the values. The JSONObject in each JSONArray row is expected to have - * an entry corresponding to each key/value pair in the string. - * Each JSONObject row is unordered in its own way. - * @param jsonArray the JSONArray which was created from the string - * @param str the string which was used to create the JSONArray - * @return null if equal, otherwise error description - */ - private String compareJSONArrayToString(JSONArray jsonArray, String str) { - int rows = jsonArray.length(); - StringReader sr = new StringReader(str); - BufferedReader reader = new BufferedReader(sr); - try { - // first line contains the keys to the JSONObject array entries - String columnNames = reader.readLine(); - columnNames = normalizeString(columnNames); - String[] keys = columnNames.split(","); - /** - * Each line contains the values for the corresponding - * JSONObject array entry - */ - for (int i = 0; i < rows; ++i) { - String line = reader.readLine(); - line = normalizeString(line); - String[] values = line.split(","); - // need a value for every key to proceed - if (keys.length != values.length) { - System.out.println("keys: " + Arrays.toString(keys)); - System.out.println("values: " + Arrays.toString(values)); - return("row: " +i+ " key and value counts do not match"); - } - JSONObject jsonObject = jsonArray.getJSONObject(i); - // need a key for every JSONObject entry to proceed - if (keys.length != jsonObject.length()) { - System.out.println("keys: " + Arrays.toString(keys)); - System.out.println("jsonObject: " + jsonObject.toString()); - return("row: " +i+ " key and jsonObject counts do not match"); - } - // convert string entries into a natural order map. - Map strMap = new TreeMap(); - for (int j = 0; j < keys.length; ++j) { - strMap.put(keys[j], values[j]); - } - // put the JSONObjet key/value pairs in natural key order - Iterator keyIt = jsonObject.keys(); - Map jsonObjectMap = new TreeMap(); - while (keyIt.hasNext()) { - String key = keyIt.next(); - jsonObjectMap.put(key, jsonObject.get(key).toString()); - } - if (!strMap.equals(jsonObjectMap)) { - System.out.println("strMap: " +strMap.toString()); - System.out.println("jsonObjectMap: " +jsonObjectMap.toString()); - return("row: " +i+ "string does not match jsonObject"); - } - } - } catch (IOException ignore) { - } catch (JSONException ignore) {} - return null; - } - + + + /////////////////////////// UTILITY METHODS ///////////////////////// + /** - * Utility to trim and remove internal quotes from comma delimited strings. - * Need to do this because JSONObject does the same thing - * @param line the line to be normalized - * @return the normalized line + * Compares two json arrays for equality + * @param jsonArray created by the code to be tested + * @param expectedJsonArray created specifically for compar */ - private String normalizeString(String line) { - StringBuilder builder = new StringBuilder(); - boolean comma = false; - String[] values = line.split(","); - for (int i = 0; i < values.length; ++i) { - if (comma) { - builder.append(","); + private void compareActualVsExpectedJsonArrays(JSONArray jsonArray, + JSONArray expectedJsonArray) { + assertTrue("jsonArray lengths should be equal", + jsonArray.length() == expectedJsonArray.length()); + for (int i = 0; i < jsonArray.length(); ++i) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + JSONObject expectedJsonObject = expectedJsonArray.getJSONObject(i); + assertTrue("jsonObjects should have the same length", + jsonObject.length() == expectedJsonObject.length()); + Iterator keys = jsonObject.keys(); + while (keys.hasNext()) { + String key = keys.next(); + Object value = jsonObject.get(key); + String testStr = "row: "+i+" key: "+key+" val: "+value.toString(); + String actualStr = expectedJsonObject .get(key).toString(); + assertTrue("values should be equal for actual: "+testStr+ + " expected: "+actualStr, + value.equals(expectedJsonArray.getJSONObject(i). + get(key).toString())); } - comma = true; - values[i] = values[i].trim(); - // strip optional surrounding quotes - values[i] = values[i].replaceAll("^\"|\"$", ""); - builder.append(values[i]); } - return builder.toString(); } - /** - * Utility to sort the columns in a (possibly) multi-lined string. - * The columns are column separated. Need to do this because - * JSONObects are not ordered - * @param string the string to be sorted - * @param normalize flag, true if line should be normalized - * @return a list of sorted lines, where each line is a list sorted - * in natural key order - */ - private List> sortColumnsInLines(String string, - boolean normalizeFlag) { - List> lineList = new ArrayList>(); - StringReader sr = new StringReader(string); - BufferedReader reader = new BufferedReader(sr); - try { - while (true) { - String line = reader.readLine(); - if (line == null) { - break; - } - if (normalizeFlag) { - line = normalizeString(line); - } - List columnList = new ArrayList(); - String[] values = line.split(","); - for (int i = 0; i < values.length; ++i) { - columnList.add(values[i]); - } - Collections.sort(columnList); - lineList.add(columnList); - } - } catch (IOException ignore) {} - return lineList; - } + } \ No newline at end of file From 8b9c3cbf476cae2c2e295bcd4121a4b8e6fc26fe Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 22 Mar 2015 20:36:55 -0500 Subject: [PATCH 037/315] XMLTest, in progress --- XMLTest.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 XMLTest.java diff --git a/XMLTest.java b/XMLTest.java new file mode 100644 index 000000000..e69de29bb From 4fbe651e57570b7841c16624d9b8831a7703d027 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 22 Mar 2015 20:37:24 -0500 Subject: [PATCH 038/315] XMLTest, in progress --- CDLTest.java | 40 ++--------------- JunitTestSuite.java | 3 +- Util.java | 103 ++++++++++++++++++++++++++------------------ XMLTest.java | 93 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 81 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index 4ff9c3814..53284d0c6 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -1,8 +1,5 @@ package org.json.junit; - -import java.util.Iterator; - import static org.junit.Assert.*; import org.junit.Test; @@ -127,7 +124,7 @@ public void shouldConvertCDLToJSONArray() { JSONArray jsonArray = CDL.toJSONArray(lines); // This array is built from JSON parsing JSONArray expectedJsonArray = new JSONArray(expectedLines); - compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } @Test @@ -137,7 +134,7 @@ public void shouldCreateJSONArrayUsingJSONArray() { JSONArray nameJSONArray = new JSONArray(nameArrayStr); JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]"); - compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } @Test @@ -151,39 +148,8 @@ public void shouldConvertCDLToJSONArrayAndBackToString() { String jsonStr = CDL.toString(jsonArray); JSONArray finalJsonArray = CDL.toJSONArray(jsonStr); JSONArray expectedJsonArray = new JSONArray(expectedLines); - compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } - - /////////////////////////// UTILITY METHODS ///////////////////////// - - /** - * Compares two json arrays for equality - * @param jsonArray created by the code to be tested - * @param expectedJsonArray created specifically for compar - */ - private void compareActualVsExpectedJsonArrays(JSONArray jsonArray, - JSONArray expectedJsonArray) { - assertTrue("jsonArray lengths should be equal", - jsonArray.length() == expectedJsonArray.length()); - for (int i = 0; i < jsonArray.length(); ++i) { - JSONObject jsonObject = jsonArray.getJSONObject(i); - JSONObject expectedJsonObject = expectedJsonArray.getJSONObject(i); - assertTrue("jsonObjects should have the same length", - jsonObject.length() == expectedJsonObject.length()); - Iterator keys = jsonObject.keys(); - while (keys.hasNext()) { - String key = keys.next(); - Object value = jsonObject.get(key); - String testStr = "row: "+i+" key: "+key+" val: "+value.toString(); - String actualStr = expectedJsonObject .get(key).toString(); - assertTrue("values should be equal for actual: "+testStr+ - " expected: "+actualStr, - value.equals(expectedJsonArray.getJSONObject(i). - get(key).toString())); - } - } - } - } \ No newline at end of file diff --git a/JunitTestSuite.java b/JunitTestSuite.java index b9abad100..0d2b543e7 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -6,7 +6,8 @@ @Suite.SuiteClasses({ CDLTest.class, CookieTest.class, - PropertyTest.class + PropertyTest.class, + XMLTest.class }) public class JunitTestSuite { } \ No newline at end of file diff --git a/Util.java b/Util.java index 89743c089..13407af7b 100644 --- a/Util.java +++ b/Util.java @@ -2,55 +2,72 @@ import static org.junit.Assert.*; -public class Util { +import java.util.*; - /** - * Utility method to check for a target string, then remove it from - * the string to be searched. - * @param jsonStr the string to be searched - * @param expectedStr the target string to search for - * @param assertStr the error message for the assert - * @return new string with target substring removed - */ - public static String checkAndRemoveString( - String jsonStr, String expectedStr, String assertStr) { - int idx = jsonStr.indexOf(expectedStr); - assertTrue(assertStr, idx != -1); - String newStr = jsonStr.substring(0, idx)+ - jsonStr.substring(idx+expectedStr.length()); - return newStr; - } +import org.json.*; +public class Util { + + + /////////////////////////// UTILITY METHODS ///////////////////////// + /** - * Utility method to strip out selected punctuation chars and confirm - * that jsonStr is now empty - * @param jsonStr the string to be verified - * @param regexStr regex string of the chars to remove - * @param assertStr the error message for the assert + * Compares two json arrays for equality + * @param jsonArray created by the code to be tested + * @param expectedJsonArray created specifically for compar */ - public static void verifyEmptyJsonStr(String jsonStr, String regexStr, - String assertStr) { - jsonStr = jsonStr.replaceAll(regexStr, ""); - assertTrue(assertStr, jsonStr.length() == 0); + public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray, + JSONArray expectedJsonArray) { + assertTrue("jsonArray lengths should be equal", + jsonArray.length() == expectedJsonArray.length()); + for (int i = 0; i < jsonArray.length(); ++i) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + JSONObject expectedJsonObject = expectedJsonArray.getJSONObject(i); + assertTrue("jsonObjects should have the same length", + jsonObject.length() == expectedJsonObject.length()); + Iterator keys = jsonObject.keys(); + while (keys.hasNext()) { + // TODO: check for nonstring types + String key = keys.next(); + Object value = jsonObject.get(key); + String testStr = "row: "+i+" key: "+key+" val: "+value.toString(); + String actualStr = expectedJsonObject .get(key).toString(); + assertTrue("values should be equal for actual: "+testStr+ + " expected: "+actualStr, + value.equals(expectedJsonArray.getJSONObject(i). + get(key).toString())); + } + } } - /** - * Utility method to check for a set of target strings, - * then remove them from the string to be searched. - * When completed, punctuation marks are stripped out and - * the string to be searched is confirmed as empty - * @param jsonStr the string to be searched - * @param expectedStrArray the target strings to search for - * @param regexStr regex string of the chars to remove - * @param methodStr the method name - */ - public static void checkAndRemoveStrings(String jsonStr, - String[] expectedStr, String regexStr, String methodStr) { - for (int i = 0; i < expectedStr.length; ++i) { - jsonStr = Util.checkAndRemoveString(jsonStr, expectedStr[i], - methodStr+expectedStr+" should be included in string output"); + public static void compareActualVsExpectedJsonObjects( + JSONObject jsonObject, JSONObject expectedJsonObject) { + assertTrue("jsonObjects should have the same length", + jsonObject.length() == expectedJsonObject.length()); + Iterator keys = jsonObject.keys(); + while (keys.hasNext()) { + String key = keys.next(); + Object value = jsonObject.get(key); + Object expectedValue = expectedJsonObject.get(key); + if (value instanceof JSONObject) { + JSONObject childJsonObject = jsonObject.getJSONObject(key); + JSONObject expectedChildJsonObject = + expectedJsonObject.getJSONObject(key); + compareActualVsExpectedJsonObjects( + childJsonObject, expectedChildJsonObject); + } else if (value instanceof JSONArray) { + JSONArray childJsonArray = jsonObject.getJSONArray(key); + JSONArray expectedChildJsonArray = + expectedJsonObject.getJSONArray(key); + compareActualVsExpectedJsonArrays( + childJsonArray, expectedChildJsonArray); + } else { + String testStr = "key: "+key+" val: "+value.toString(); + String actualStr = expectedValue.toString(); + assertTrue("string values should be equal for actual: "+ + testStr+" expected: "+actualStr, + value.equals(expectedValue.toString())); + } } - Util.verifyEmptyJsonStr(jsonStr, regexStr, - methodStr+" jsonStr should be empty"); } } diff --git a/XMLTest.java b/XMLTest.java index e69de29bb..a6f1bf77f 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -0,0 +1,93 @@ +package org.json.junit; + +import java.util.*; + +import static org.junit.Assert.*; + +import org.json.*; +import org.junit.Test; + + +/** + * Tests for JSON-Java XML.java + */ +public class XMLTest { + + @Test(expected=NullPointerException.class) + public void shouldHandleNullXML() { + + String xmlStr = null; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + @Test + public void shouldHandleEmptyXML() { + + String xmlStr = ""; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + @Test(expected=NullPointerException.class) + public void shouldHandleNullJSONXML() { + JSONObject jsonObject= null; + String xmlStr = XML.toString(jsonObject); + } + + @Test + public void shouldHandleEmptyJSONXML() { + JSONObject jsonObject= new JSONObject(); + String xmlStr = XML.toString(jsonObject); + } + + @Test + public void shouldHandleSimpleXML() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " Baker street 5\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ + "\"name\":\"Joe Tester\"},\"xsi:noNamespaceSchemaLocation\":"+ + "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ + "XMLSchema-instance\"}}"; + + JSONObject expectedJsonObject = new JSONObject(expectedStr); + + JSONObject jsonObject = XML.toJSONObject(xmlStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void shouldHandleToString() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " [CDATA[Joe Tester]]\n"+ + " Baker street 5\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{\"addr&esses\":{\"address\":{\"street\":\"Baker street 5\","+ + "\"name\":\"[CDATA[Joe Tester]]\"},\"xsi:noNamespaceSchemaLocation\":"+ + "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ + "XMLSchema-instance\"}}"; + + JSONObject jsonObject = XML.toJSONObject(xmlStr); + String xmlToStr = XML.toString(jsonObject); + JSONObject finalJsonObject = XML.toJSONObject(xmlToStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } +} From 4c6da0e6f9bad5dcdec60df7e3aa176b8edae988 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 23 Mar 2015 19:23:22 -0500 Subject: [PATCH 039/315] In progress --- XMLTest.java | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/XMLTest.java b/XMLTest.java index a6f1bf77f..aca2e40e0 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -21,6 +21,24 @@ public void shouldHandleNullXML() { assertTrue("jsonObject should be empty", jsonObject.length() == 0); } + @Test + public void shouldHandleCommentsInXML() { + + String xmlStr = + "\n"+ + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " Joe Tester\n"+ + " \n"+ + " Baker street 5\n"+ + "
\n"+ + "
"; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + } + @Test public void shouldHandleEmptyXML() { @@ -69,17 +87,17 @@ public void shouldHandleSimpleXML() { public void shouldHandleToString() { String xmlStr = "\n"+ - "\n"+ "
\n"+ - " [CDATA[Joe Tester]]\n"+ + " [CDATA[Joe & T > e < s " t ' er]]\n"+ " Baker street 5\n"+ "
\n"+ - "
"; + ""; String expectedStr = - "{\"addr&esses\":{\"address\":{\"street\":\"Baker street 5\","+ - "\"name\":\"[CDATA[Joe Tester]]\"},\"xsi:noNamespaceSchemaLocation\":"+ + "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ + "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\"},\"xsi:noNamespaceSchemaLocation\":"+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ "XMLSchema-instance\"}}"; From 2df27fc6e76b71aa2ddbb6174b3f9ae03183a1dc Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 25 Mar 2015 08:27:29 -0500 Subject: [PATCH 040/315] in progress --- XMLTest.java | 64 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/XMLTest.java b/XMLTest.java index aca2e40e0..87d956eb3 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -10,6 +10,7 @@ /** * Tests for JSON-Java XML.java + * Note: noSpace() will be tested by JSONMLTest */ public class XMLTest { @@ -21,22 +22,25 @@ public void shouldHandleNullXML() { assertTrue("jsonObject should be empty", jsonObject.length() == 0); } - @Test - public void shouldHandleCommentsInXML() { + @Test(expected=NullPointerException.class) + public void shouldHandleNullJSONXML() { + JSONObject jsonObject= null; + String xmlStr = XML.toString(jsonObject); + } + @Test + public void shouldHandleInvalidCDATA() { String xmlStr = - "\n"+ - "\n"+ - "\n"+ - "
\n"+ - " \n"+ - " Joe Tester\n"+ - " \n"+ - " Baker street 5\n"+ - "
\n"+ - "
"; + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " ![Baker street 5\n"+ + "
\n"+ + "
"; JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertTrue(jsonObject == null); } @Test @@ -47,12 +51,6 @@ public void shouldHandleEmptyXML() { assertTrue("jsonObject should be empty", jsonObject.length() == 0); } - @Test(expected=NullPointerException.class) - public void shouldHandleNullJSONXML() { - JSONObject jsonObject= null; - String xmlStr = XML.toString(jsonObject); - } - @Test public void shouldHandleEmptyJSONXML() { JSONObject jsonObject= new JSONObject(); @@ -67,15 +65,15 @@ public void shouldHandleSimpleXML() { " xsi:noNamespaceSchemaLocation='test.xsd'>\n"+ "
\n"+ " Joe Tester\n"+ - " Baker street 5\n"+ + " [CDATA[Baker street 5]\n"+ "
\n"+ ""; String expectedStr = - "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ - "\"name\":\"Joe Tester\"},\"xsi:noNamespaceSchemaLocation\":"+ - "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ - "XMLSchema-instance\"}}"; + "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+ + "\"name\":\"Joe Tester\"},\"xsi:noNamespaceSchemaLocation\":"+ + "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ + "XMLSchema-instance\"}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); @@ -83,6 +81,24 @@ public void shouldHandleSimpleXML() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + @Test + public void shouldHandleCommentsInXML() { + + String xmlStr = + "\n"+ + "\n"+ + "\n"+ + "
\n"+ + " comment ]]>\n"+ + " Joe Tester\n"+ + " \n"+ + " Baker street 5\n"+ + "
\n"+ + "
"; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + } + @Test public void shouldHandleToString() { String xmlStr = From a18e9f7a259ba0bceba9bb4f8158b0313e58f1b2 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 25 Mar 2015 18:53:52 -0500 Subject: [PATCH 041/315] in progress --- XMLTest.java | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/XMLTest.java b/XMLTest.java index 87d956eb3..3f5771038 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -28,15 +28,30 @@ public void shouldHandleNullJSONXML() { String xmlStr = XML.toString(jsonObject); } - @Test - public void shouldHandleInvalidCDATA() { + @Test(expected=JSONException.class) + public void shouldHandleInvalidBangInTag() { String xmlStr = "\n"+ "\n"+ "
\n"+ " Joe Tester\n"+ - " ![Baker street 5\n"+ + " abc street\n"+ + "
\n"+ + "
"; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertTrue(jsonObject == null); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidSlashInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " abc street\n"+ "
\n"+ "
"; JSONObject jsonObject = XML.toJSONObject(xmlStr); @@ -66,12 +81,13 @@ public void shouldHandleSimpleXML() { "
\n"+ " Joe Tester\n"+ " [CDATA[Baker street 5]\n"+ + " \n"+ "
\n"+ ""; String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+ - "\"name\":\"Joe Tester\"},\"xsi:noNamespaceSchemaLocation\":"+ + "\"name\":\"Joe Tester\",\"NothingHere\":\"\"},\"xsi:noNamespaceSchemaLocation\":"+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ "XMLSchema-instance\"}}"; From 89f359e4f80ab4d62627fed635a1c2849a253025 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 26 Mar 2015 22:58:11 -0500 Subject: [PATCH 042/315] coverage XMLTest 81.2% / XMLTokener 82.2% --- Util.java | 74 +++++++++++-------- XMLTest.java | 200 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 220 insertions(+), 54 deletions(-) diff --git a/Util.java b/Util.java index 13407af7b..3a1cd099e 100644 --- a/Util.java +++ b/Util.java @@ -9,12 +9,11 @@ public class Util { - /////////////////////////// UTILITY METHODS ///////////////////////// /** * Compares two json arrays for equality * @param jsonArray created by the code to be tested - * @param expectedJsonArray created specifically for compar + * @param expectedJsonArray created specifically for comparing */ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray, JSONArray expectedJsonArray) { @@ -27,19 +26,17 @@ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray, jsonObject.length() == expectedJsonObject.length()); Iterator keys = jsonObject.keys(); while (keys.hasNext()) { - // TODO: check for nonstring types String key = keys.next(); - Object value = jsonObject.get(key); - String testStr = "row: "+i+" key: "+key+" val: "+value.toString(); - String actualStr = expectedJsonObject .get(key).toString(); - assertTrue("values should be equal for actual: "+testStr+ - " expected: "+actualStr, - value.equals(expectedJsonArray.getJSONObject(i). - get(key).toString())); + compareJsonObjectEntries(jsonObject, expectedJsonObject, key); } } } + /** + * Compares two json objects for equality + * @param jsonObject created by the code to be tested + * @param expectedJsonObject created specifically for comparing + */ public static void compareActualVsExpectedJsonObjects( JSONObject jsonObject, JSONObject expectedJsonObject) { assertTrue("jsonObjects should have the same length", @@ -47,27 +44,42 @@ public static void compareActualVsExpectedJsonObjects( Iterator keys = jsonObject.keys(); while (keys.hasNext()) { String key = keys.next(); - Object value = jsonObject.get(key); - Object expectedValue = expectedJsonObject.get(key); - if (value instanceof JSONObject) { - JSONObject childJsonObject = jsonObject.getJSONObject(key); - JSONObject expectedChildJsonObject = - expectedJsonObject.getJSONObject(key); - compareActualVsExpectedJsonObjects( - childJsonObject, expectedChildJsonObject); - } else if (value instanceof JSONArray) { - JSONArray childJsonArray = jsonObject.getJSONArray(key); - JSONArray expectedChildJsonArray = - expectedJsonObject.getJSONArray(key); - compareActualVsExpectedJsonArrays( - childJsonArray, expectedChildJsonArray); - } else { - String testStr = "key: "+key+" val: "+value.toString(); - String actualStr = expectedValue.toString(); - assertTrue("string values should be equal for actual: "+ - testStr+" expected: "+actualStr, - value.equals(expectedValue.toString())); - } + compareJsonObjectEntries(jsonObject, expectedJsonObject, key); + } + } + + /** + * Compare two jsonObject entries + * @param jsonObject created by the code to be tested + * @param expectedJsonObject created specifically for comparing + * @param key key to the jsonObject entry to be compared + */ + private static void compareJsonObjectEntries(JSONObject jsonObject, + JSONObject expectedJsonObject, String key) { + Object value = jsonObject.get(key); + Object expectedValue = expectedJsonObject.get(key); + if (value instanceof JSONObject) { + JSONObject childJsonObject = jsonObject.getJSONObject(key); + JSONObject expectedChildJsonObject = + expectedJsonObject.getJSONObject(key); + compareActualVsExpectedJsonObjects( + childJsonObject, expectedChildJsonObject); + } else if (value instanceof JSONArray) { + JSONArray childJsonArray = jsonObject.getJSONArray(key); + JSONArray expectedChildJsonArray = + expectedJsonObject.getJSONArray(key); + compareActualVsExpectedJsonArrays( + childJsonArray, expectedChildJsonArray); + } else if (!(value instanceof String) && !(expectedValue instanceof String)) { + assertTrue("string values should be equal for actual: "+ + value.toString()+" expected: "+expectedValue.toString(), + value.toString().equals(expectedValue.toString())); + } else { + String testStr = "key: "+key+" val: "+value.toString(); + String actualStr = expectedValue.toString(); + assertTrue("string values should be equal for actual: "+ + testStr+" expected: "+actualStr, + value.equals(expectedValue.toString())); } } } diff --git a/XMLTest.java b/XMLTest.java index 3f5771038..0c86eebc1 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -1,7 +1,5 @@ package org.json.junit; -import java.util.*; - import static org.junit.Assert.*; import org.json.*; @@ -22,10 +20,33 @@ public void shouldHandleNullXML() { assertTrue("jsonObject should be empty", jsonObject.length() == 0); } - @Test(expected=NullPointerException.class) - public void shouldHandleNullJSONXML() { - JSONObject jsonObject= null; - String xmlStr = XML.toString(jsonObject); + @Test + public void shouldHandleEmptyXML() { + + String xmlStr = ""; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + @Test + public void shouldHandleNonXML() { + String xmlStr = "{ \"this is\": \"not xml\"}"; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertTrue("xml string should be empty", jsonObject.length() == 0); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidSlashInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " abc street\n"+ + "
\n"+ + "
"; + XML.toJSONObject(xmlStr); } @Test(expected=JSONException.class) @@ -35,41 +56,86 @@ public void shouldHandleInvalidBangInTag() { "\n"+ "
\n"+ - " Joe Tester\n"+ - " abc street\n"+ + " \n"+ + " \n"+ "
\n"+ "
"; - JSONObject jsonObject = XML.toJSONObject(xmlStr); - assertTrue(jsonObject == null); + XML.toJSONObject(xmlStr); } @Test(expected=JSONException.class) - public void shouldHandleInvalidSlashInTag() { + public void shouldHandleInvalidBangNoCloseInTag() { String xmlStr = "\n"+ "\n"+ "
\n"+ - " \n"+ - " abc street\n"+ + " \n"+ + " \n"+ ""; - JSONObject jsonObject = XML.toJSONObject(xmlStr); - assertTrue(jsonObject == null); + XML.toJSONObject(xmlStr); } - @Test - public void shouldHandleEmptyXML() { + @Test(expected=JSONException.class) + public void shouldHandleNoCloseStartTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + ""; + XML.toJSONObject(xmlStr); + } - String xmlStr = ""; - JSONObject jsonObject = XML.toJSONObject(xmlStr); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + @Test(expected=JSONException.class) + public void shouldHandleInvalidCDATABangInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " \n"+ + "
\n"+ + "
"; + XML.toJSONObject(xmlStr); + } + + @Test(expected=NullPointerException.class) + public void shouldHandleNullJSONXML() { + JSONObject jsonObject= null; + XML.toString(jsonObject); } @Test public void shouldHandleEmptyJSONXML() { JSONObject jsonObject= new JSONObject(); String xmlStr = XML.toString(jsonObject); + assertTrue("xml string should be empty", xmlStr.length() == 0); + } + + @Test + public void shouldHandleNoStartTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " >\n"+ + "
\n"+ + "
"; + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test @@ -82,17 +148,28 @@ public void shouldHandleSimpleXML() { " Joe Tester\n"+ " [CDATA[Baker street 5]\n"+ " \n"+ + " true\n"+ + " false\n"+ + " null\n"+ + " 42\n"+ + " -23\n"+ + " -23.45\n"+ + " -23x.45\n"+ + " 1, 2, 3, 4.1, 5.2\n"+ "
\n"+ "
"; String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+ - "\"name\":\"Joe Tester\",\"NothingHere\":\"\"},\"xsi:noNamespaceSchemaLocation\":"+ + "\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+ + "\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+ + "\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+ + "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ + "},\"xsi:noNamespaceSchemaLocation\":"+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ "XMLSchema-instance\"}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); - JSONObject jsonObject = XML.toJSONObject(xmlStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @@ -113,6 +190,11 @@ public void shouldHandleCommentsInXML() { "
\n"+ "
"; JSONObject jsonObject = XML.toJSONObject(xmlStr); + String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+ + "street 5\",\"name\":\"Joe Tester\",\"content\":\" this is -- "+ + " comment \"}}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test @@ -124,12 +206,15 @@ public void shouldHandleToString() { "
\n"+ " [CDATA[Joe & T > e < s " t ' er]]\n"+ " Baker street 5\n"+ + " 1, 2, 3, 4.1, 5.2\n"+ "
\n"+ ""; String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ - "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\"},\"xsi:noNamespaceSchemaLocation\":"+ + "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+ + "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ + "},\"xsi:noNamespaceSchemaLocation\":"+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ "XMLSchema-instance\"}}"; @@ -140,4 +225,73 @@ public void shouldHandleToString() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } + + @Test + public void shouldHandleContentNoArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + String finalStr = XML.toString(expectedJsonObject); + String expectedFinalStr = "
>"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + @Test + public void shouldHandleContentArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "content\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + String finalStr = XML.toString(expectedJsonObject); + String expectedFinalStr = "
"+ + "1\n2\n3"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + @Test + public void shouldHandleArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ + "\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + String finalStr = XML.toString(expectedJsonObject); + String expectedFinalStr = "
"+ + "123"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + @Test + public void shouldHandleNestedArraytoString() { + String xmlStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ + "\"outer\":[[1], [2], [3]]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject jsonObject = new JSONObject(xmlStr); + String finalStr = XML.toString(jsonObject); + JSONObject finalJsonObject = XML.toJSONObject(finalStr); + String expectedStr = "
"+ + "12"+ + "3"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + JSONObject expectedJsonObject = XML.toJSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } } From 210bb41ba12ade3a29089a630b7c8ec824aac41c Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Thu, 26 Mar 2015 23:05:07 -0500 Subject: [PATCH 043/315] Update README.md --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1cef7e35c..8f8348075 100644 --- a/README.md +++ b/README.md @@ -26,20 +26,20 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | ------------- | ------------- | ---- | | Total coverage | 20.8% | | | | | | | -| CDLTest.java | 94.8% | Completed | -| CookieTest.java | 97.5% | Completed | +| CDL.java | 94.8% | Completed | +| Cookie.java | 97.5% | Completed | | CookieList.java |0% | | | HTTP.java | 0%| | | HTTPTokener.java |0% | | | JSONArray.java |15.3% | | |JSONException.java | 26.7% | | | JSONML.java | 0%| | -| JSONObject.Null | 13.4% | | | +| JSONObject.Null | 17.6% | | | | JSONStringer.java | 0%| | -| JSONTokener.java |65.4% | | +| JSONTokener.java |68.5% | | | JSONWriter.java | 0% | | -| PropertyTest.java | 94.8% | Completed | -| XMLTest.java | 0% | Just started - stleary | -| XMLTokener.java| 0%| | +| Property.java | 94.8% | Completed | +| XML.java | 81.2% | Completed | +| XMLTokener.java| 82.2%| Completed | From 1ff945de69e981511981bf0646ad770fc66be0a7 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Thu, 26 Mar 2015 23:05:59 -0500 Subject: [PATCH 044/315] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8f8348075..8555eb605 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,11 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | CDL.java | 94.8% | Completed | | Cookie.java | 97.5% | Completed | | CookieList.java |0% | | -| HTTP.java | 0%| | -| HTTPTokener.java |0% | | +| HTTP.java | 0%| | +| HTTPTokener.java |0% | | | JSONArray.java |15.3% | | -|JSONException.java | 26.7% | | -| JSONML.java | 0%| | +| JSONException.java | 26.7% | | +| JSONML.java | 0%| In progress| | JSONObject.Null | 17.6% | | | | JSONStringer.java | 0%| | | JSONTokener.java |68.5% | | From bc07b5196b5a90cd0bfdbf62f669683ef5b96790 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 27 Mar 2015 17:52:41 -0500 Subject: [PATCH 045/315] starting jsonml test --- JSONMLTest.java | 297 ++++++++++++++++++++++++++++++++++++++++++++ JunitTestSuite.java | 3 +- 2 files changed, 299 insertions(+), 1 deletion(-) create mode 100644 JSONMLTest.java diff --git a/JSONMLTest.java b/JSONMLTest.java new file mode 100644 index 000000000..ca7c2d116 --- /dev/null +++ b/JSONMLTest.java @@ -0,0 +1,297 @@ +package org.json.junit; + +import static org.junit.Assert.*; + +import org.json.*; +import org.junit.Test; + + +/** + * Tests for JSON-Java JSONML.java + */ +public class JSONMLTest { + + @Test(expected=NullPointerException.class) + public void shouldHandleNullXML() { + + String xmlStr = null; + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + @Test(expected=JSONException.class) + public void shouldHandleEmptyXML() { + + String xmlStr = ""; + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + @Test + public void shouldHandleNonXML() { + String xmlStr = "{ \"this is\": \"not xml\"}"; + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + assertTrue("xml string should be empty", jsonObject.length() == 0); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidSlashInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " abc street\n"+ + "
\n"+ + "
"; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidBangInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + "
\n"+ + "
"; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidBangNoCloseInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + ""; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleNoCloseStartTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + ""; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidCDATABangInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " \n"+ + "
\n"+ + "
"; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=NullPointerException.class) + public void shouldHandleNullJSONXML() { + JSONObject jsonObject= null; + JSONML.toString(jsonObject); + } + + @Test + public void shouldHandleEmptyJSONXML() { + JSONObject jsonObject= new JSONObject(); + String xmlStr = JSONML.toString(jsonObject); + assertTrue("xml string should be empty", xmlStr.length() == 0); + } + + @Test + public void shouldHandleNoStartTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " >\n"+ + "
\n"+ + "
"; + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void shouldHandleSimpleXML() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " [CDATA[Baker street 5]\n"+ + " \n"+ + " true\n"+ + " false\n"+ + " null\n"+ + " 42\n"+ + " -23\n"+ + " -23.45\n"+ + " -23x.45\n"+ + " 1, 2, 3, 4.1, 5.2\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+ + "\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+ + "\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+ + "\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+ + "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ + "},\"xsi:noNamespaceSchemaLocation\":"+ + "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ + "XMLSchema-instance\"}}"; + + JSONObject expectedJsonObject = new JSONObject(expectedStr); + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void shouldHandleCommentsInXML() { + + String xmlStr = + "\n"+ + "\n"+ + "\n"+ + "
\n"+ + " comment ]]>\n"+ + " Joe Tester\n"+ + " \n"+ + " Baker street 5\n"+ + "
\n"+ + "
"; + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+ + "street 5\",\"name\":\"Joe Tester\",\"content\":\" this is -- "+ + " comment \"}}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void shouldHandleToString() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " [CDATA[Joe & T > e < s " t ' er]]\n"+ + " Baker street 5\n"+ + " 1, 2, 3, 4.1, 5.2\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ + "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+ + "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ + "},\"xsi:noNamespaceSchemaLocation\":"+ + "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ + "XMLSchema-instance\"}}"; + + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + String xmlToStr = JSONML.toString(jsonObject); + JSONObject finalJsonObject = JSONML.toJSONObject(xmlToStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } + + @Test + public void shouldHandleContentNoArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + String finalStr = JSONML.toString(expectedJsonObject); + String expectedFinalStr = "
>"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + @Test + public void shouldHandleContentArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "content\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + String finalStr = JSONML.toString(expectedJsonObject); + String expectedFinalStr = "
"+ + "1\n2\n3"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + @Test + public void shouldHandleArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ + "\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + String finalStr = JSONML.toString(expectedJsonObject); + String expectedFinalStr = "
"+ + "123"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + @Test + public void shouldHandleNestedArraytoString() { + String xmlStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ + "\"outer\":[[1], [2], [3]]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject jsonObject = new JSONObject(xmlStr); + String finalStr = JSONML.toString(jsonObject); + JSONObject finalJsonObject = JSONML.toJSONObject(finalStr); + String expectedStr = "
"+ + "12"+ + "3"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + JSONObject expectedJsonObject = JSONML.toJSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } + +} diff --git a/JunitTestSuite.java b/JunitTestSuite.java index 0d2b543e7..8c680a0d9 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -7,7 +7,8 @@ CDLTest.class, CookieTest.class, PropertyTest.class, - XMLTest.class + XMLTest.class, + JSONMLTest.class }) public class JunitTestSuite { } \ No newline at end of file From ef7e0c7d08662739cddf9ef9ad977043855f5969 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 30 Mar 2015 08:14:34 -0500 Subject: [PATCH 046/315] in progress --- JSONMLTest.java | 302 +++++++++++++++++++++++++----------------------- Util.java | 58 ++++------ 2 files changed, 183 insertions(+), 177 deletions(-) diff --git a/JSONMLTest.java b/JSONMLTest.java index ca7c2d116..327158474 100644 --- a/JSONMLTest.java +++ b/JSONMLTest.java @@ -12,30 +12,27 @@ public class JSONMLTest { @Test(expected=NullPointerException.class) - public void shouldHandleNullXML() { + public void nullXMLException() { String xmlStr = null; - JSONObject jsonObject = JSONML.toJSONObject(xmlStr); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + JSONML.toJSONObject(xmlStr); } @Test(expected=JSONException.class) - public void shouldHandleEmptyXML() { + public void emptyXMLException() { String xmlStr = ""; - JSONObject jsonObject = JSONML.toJSONObject(xmlStr); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + JSONML.toJSONObject(xmlStr); } - @Test - public void shouldHandleNonXML() { + @Test(expected=JSONException.class) + public void nonXMLException() { String xmlStr = "{ \"this is\": \"not xml\"}"; - JSONObject jsonObject = JSONML.toJSONObject(xmlStr); - assertTrue("xml string should be empty", jsonObject.length() == 0); + JSONML.toJSONObject(xmlStr); } @Test(expected=JSONException.class) - public void shouldHandleInvalidSlashInTag() { + public void unvalidSlashInTagException() { String xmlStr = "\n"+ "\n"+ "\n"+ "\n"+ "\n"+ "\n"+ "\n"+ - "
\n"+ - " \n"+ - " >\n"+ - "
\n"+ + "xsi:noNamespaceSchemaLocation='test.xsd'>\n"+ + "
\n"+ + "\n"+ + ">\n"+ + "
\n"+ "
"; String expectedStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ - "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"childNodes\":[{"+ + "\"childNodes\":"+ + "[{\"tagName\":\"name\"},"+ + "{\"tagName\":\"nocontent\"},"+ + "\">\"],"+ + "\"tagName\":\"address\"}],"+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ + "\"tagName\":\"addresses\"}"; JSONObject jsonObject = JSONML.toJSONObject(xmlStr); JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test - public void shouldHandleSimpleXML() { + public void simpleXML() { String xmlStr = "\n"+ "\n"+ - "
\n"+ - " Joe Tester\n"+ - " [CDATA[Baker street 5]\n"+ - " \n"+ - " true\n"+ - " false\n"+ - " null\n"+ - " 42\n"+ - " -23\n"+ - " -23.45\n"+ - " -23x.45\n"+ - " 1, 2, 3, 4.1, 5.2\n"+ - "
\n"+ + "xsi:noNamespaceSchemaLocation='test.xsd'>\n"+ + "
\n"+ + "Joe Tester\n"+ + "[CDATA[Baker street 5]\n"+ + "\n"+ + "true\n"+ + "false\n"+ + "null\n"+ + "42\n"+ + "-23\n"+ + "-23.45\n"+ + "-23x.45\n"+ + "1, 2, 3, 4.1, 5.2\n"+ + "
\n"+ "
"; String expectedStr = - "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+ - "\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+ - "\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+ - "\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+ - "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ - "},\"xsi:noNamespaceSchemaLocation\":"+ - "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ - "XMLSchema-instance\"}}"; - - JSONObject expectedJsonObject = new JSONObject(expectedStr); + "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"childNodes\":[{"+ + "\"childNodes\":["+ + "{\"childNodes\":[\"Joe Tester\"],"+ + "\"tagName\":\"name\"},"+ + "{\"childNodes\":[\"[CDATA[Baker street 5]\"],"+ + "\"tagName\":\"street\"},"+ + "{\"tagName\":\"NothingHere\"},"+ + "{\"childNodes\":[true],"+ + "\"tagName\":\"TrueValue\"},"+ + "{\"childNodes\":[false],"+ + "\"tagName\":\"FalseValue\"},"+ + "{\"childNodes\":[null],"+ + "\"tagName\":\"NullValue\"},"+ + "{\"childNodes\":[42],"+ + "\"tagName\":\"PositiveValue\"},"+ + "{\"childNodes\":[-23],"+ + "\"tagName\":\"NegativeValue\"},"+ + "{\"childNodes\":[-23.45],"+ + "\"tagName\":\"DoubleValue\"},"+ + "{\"childNodes\":[\"-23x.45\"],"+ + "\"tagName\":\"Nan\"},"+ + "{\"childNodes\":[\"1, 2, 3, 4.1, 5.2\"],"+ + "\"tagName\":\"ArrayOfNum\"}],"+ + "\"tagName\":\"address\"}],"+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ + "\"tagName\":\"addresses\"}"; JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test - public void shouldHandleCommentsInXML() { + public void commentsInXML() { String xmlStr = - "\n"+ - "\n"+ - "\n"+ - "
\n"+ - " comment ]]>\n"+ - " Joe Tester\n"+ - " \n"+ - " Baker street 5\n"+ - "
\n"+ - "
"; + "\n"+ + "\n"+ + "\n"+ + "
\n"+ + " comment ]]>\n"+ + "Joe Tester\n"+ + "\n"+ + "Baker street 5\n"+ + "
\n"+ + "
"; + String expectedStr = + "{\"childNodes\":["+ + "{\"childNodes\":["+ + "\" this is -- comment \","+ + "{\"childNodes\":[\"Joe Tester\"],"+ + "\"tagName\":\"name\"},"+ + "{\"childNodes\":[\"Baker street 5\"],"+ + "\"tagName\":\"street\"}],"+ + "\"tagName\":\"address\"}],"+ + "\"tagName\":\"addresses\"}"; JSONObject jsonObject = JSONML.toJSONObject(xmlStr); - String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+ - "street 5\",\"name\":\"Joe Tester\",\"content\":\" this is -- "+ - " comment \"}}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test - public void shouldHandleToString() { + public void jsonObjectToString() { String xmlStr = "\n"+ ""; String expectedStr = - "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ - "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+ - "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ - "},\"xsi:noNamespaceSchemaLocation\":"+ - "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ - "XMLSchema-instance\"}}"; + "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"childNodes\":["+ + "{\"childNodes\":["+ + "{\"childNodes\":[\"[CDATA[Joe & T > e < s \\\" t ' er]]\"],"+ + "\"tagName\":\"name\"},"+ + "{\"childNodes\":[\"Baker street 5\"],"+ + "\"tagName\":\"street\"},"+ + "{\"childNodes\":[\"1, 2, 3, 4.1, 5.2\"],"+ + "\"tagName\":\"ArrayOfNum\"}],"+ + "\"tagName\":\"address\"}],"+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ + "\"tagName\":\"addresses\"}"; JSONObject jsonObject = JSONML.toJSONObject(xmlStr); String xmlToStr = JSONML.toString(jsonObject); @@ -226,72 +259,57 @@ public void shouldHandleToString() { } @Test - public void shouldHandleContentNoArraytoString() { - String expectedStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ - "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; - JSONObject expectedJsonObject = new JSONObject(expectedStr); - String finalStr = JSONML.toString(expectedJsonObject); - String expectedFinalStr = "
>"+ - "
test.xsdhttp://www.w3.org/2001/XMLSche"+ - "ma-instance
"; - assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ - finalStr+"]", expectedFinalStr.equals(finalStr)); - } - - @Test - public void shouldHandleContentArraytoString() { - String expectedStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ - "content\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; - JSONObject expectedJsonObject = new JSONObject(expectedStr); - String finalStr = JSONML.toString(expectedJsonObject); - String expectedFinalStr = "
"+ - "1\n2\n3"+ - "
test.xsdhttp://www.w3.org/2001/XMLSche"+ - "ma-instance
"; - assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ - finalStr+"]", expectedFinalStr.equals(finalStr)); - } - - @Test - public void shouldHandleArraytoString() { - String expectedStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ - "\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; - JSONObject expectedJsonObject = new JSONObject(expectedStr); - String finalStr = JSONML.toString(expectedJsonObject); - String expectedFinalStr = "
"+ - "123"+ - "
test.xsdhttp://www.w3.org/2001/XMLSche"+ - "ma-instance
"; - assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ - finalStr+"]", expectedFinalStr.equals(finalStr)); - } - - @Test - public void shouldHandleNestedArraytoString() { - String xmlStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ - "\"outer\":[[1], [2], [3]]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; - JSONObject jsonObject = new JSONObject(xmlStr); - String finalStr = JSONML.toString(jsonObject); - JSONObject finalJsonObject = JSONML.toJSONObject(finalStr); - String expectedStr = "
"+ - "12"+ - "3"+ - "
test.xsdhttp://www.w3.org/2001/XMLSche"+ - "ma-instance
"; - JSONObject expectedJsonObject = JSONML.toJSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + public void jsonArrayToString() { + String xmlStr = + ""+ + ""+ + "5"+ + "10"+ + "15"+ + ""+ + "val2"+ + "val3"+ + ""+ + "-6"+ + "true"+ + ""+ + "false"+ + "null"+ + ""+ + "10"+ + "20"+ + "33.33"+ + "5220"+ + ""+ + ""; + String expectedStr = + "[\"tag0\","+ + "[\"tag1\","+ + "[\"tag2\",5],"+ + "[\"tag2\",10],"+ + "[\"tag2\",15]"+ + "],"+ + "[\"tag2\",\"val2\"],"+ + "[\"tag3\",\"val3\"],"+ + "[\"tag4\","+ + "[\"tag5\",-6],"+ + "[\"tag5\",true]"+ + "],"+ + "[\"tag6\",false],"+ + "[\"tag7\",null],"+ + "[\"tag1\","+ + "[\"tag8\",10],"+ + "[\"tag8\",20],"+ + "[\"tag8\",33.33],"+ + "[\"tag8\",5220]"+ + "]"+ + "]"; + JSONArray jsonArray = JSONML.toJSONArray(xmlStr); + String xmlToStr = JSONML.toString(jsonArray); + JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr); + JSONArray expectedJsonArray= new JSONArray(expectedStr); + Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(finalJsonArray,expectedJsonArray); } } diff --git a/Util.java b/Util.java index 3a1cd099e..d494d13ba 100644 --- a/Util.java +++ b/Util.java @@ -20,15 +20,9 @@ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray, assertTrue("jsonArray lengths should be equal", jsonArray.length() == expectedJsonArray.length()); for (int i = 0; i < jsonArray.length(); ++i) { - JSONObject jsonObject = jsonArray.getJSONObject(i); - JSONObject expectedJsonObject = expectedJsonArray.getJSONObject(i); - assertTrue("jsonObjects should have the same length", - jsonObject.length() == expectedJsonObject.length()); - Iterator keys = jsonObject.keys(); - while (keys.hasNext()) { - String key = keys.next(); - compareJsonObjectEntries(jsonObject, expectedJsonObject, key); - } + Object value = jsonArray.get(i); + Object expectedValue = expectedJsonArray.get(i); + compareActualVsExpectedObjects(value, expectedValue); } } @@ -44,42 +38,36 @@ public static void compareActualVsExpectedJsonObjects( Iterator keys = jsonObject.keys(); while (keys.hasNext()) { String key = keys.next(); - compareJsonObjectEntries(jsonObject, expectedJsonObject, key); + Object value = jsonObject.get(key); + Object expectedValue = expectedJsonObject.get(key); + compareActualVsExpectedObjects(value, expectedValue); } } /** - * Compare two jsonObject entries - * @param jsonObject created by the code to be tested - * @param expectedJsonObject created specifically for comparing + * Compare two objects for equality. Might be JSONArray, JSONObject, + * or something else. + * @param value created by the code to be tested + * @param expectedValue created specifically for comparing * @param key key to the jsonObject entry to be compared */ - private static void compareJsonObjectEntries(JSONObject jsonObject, - JSONObject expectedJsonObject, String key) { - Object value = jsonObject.get(key); - Object expectedValue = expectedJsonObject.get(key); - if (value instanceof JSONObject) { - JSONObject childJsonObject = jsonObject.getJSONObject(key); - JSONObject expectedChildJsonObject = - expectedJsonObject.getJSONObject(key); + private static void compareActualVsExpectedObjects(Object value, + Object expectedValue) { + if (value instanceof JSONObject && expectedValue instanceof JSONObject) { + JSONObject jsonObject = (JSONObject)value; + JSONObject expectedJsonObject = (JSONObject)expectedValue; compareActualVsExpectedJsonObjects( - childJsonObject, expectedChildJsonObject); - } else if (value instanceof JSONArray) { - JSONArray childJsonArray = jsonObject.getJSONArray(key); - JSONArray expectedChildJsonArray = - expectedJsonObject.getJSONArray(key); + jsonObject, expectedJsonObject); + } else if (value instanceof JSONArray && expectedValue instanceof JSONArray) { + JSONArray jsonArray = (JSONArray)value; + JSONArray expectedJsonArray = (JSONArray)expectedValue; compareActualVsExpectedJsonArrays( - childJsonArray, expectedChildJsonArray); - } else if (!(value instanceof String) && !(expectedValue instanceof String)) { - assertTrue("string values should be equal for actual: "+ - value.toString()+" expected: "+expectedValue.toString(), - value.toString().equals(expectedValue.toString())); + jsonArray, expectedJsonArray); } else { - String testStr = "key: "+key+" val: "+value.toString(); - String actualStr = expectedValue.toString(); assertTrue("string values should be equal for actual: "+ - testStr+" expected: "+actualStr, - value.equals(expectedValue.toString())); + value.toString()+" expected: "+expectedValue.toString(), + value.toString().equals(expectedValue.toString())); } } + } From 9a6215c3bef6f47103d1d757a124dad0ddc0e4ce Mon Sep 17 00:00:00 2001 From: stleary Date: Tue, 31 Mar 2015 22:53:52 -0500 Subject: [PATCH 047/315] in progress --- JSONMLTest.java | 122 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 93 insertions(+), 29 deletions(-) diff --git a/JSONMLTest.java b/JSONMLTest.java index 327158474..3629fd4b2 100644 --- a/JSONMLTest.java +++ b/JSONMLTest.java @@ -15,20 +15,20 @@ public class JSONMLTest { public void nullXMLException() { String xmlStr = null; - JSONML.toJSONObject(xmlStr); + JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) public void emptyXMLException() { String xmlStr = ""; - JSONML.toJSONObject(xmlStr); + JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) public void nonXMLException() { String xmlStr = "{ \"this is\": \"not xml\"}"; - JSONML.toJSONObject(xmlStr); + JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) @@ -42,7 +42,7 @@ public void unvalidSlashInTagException() { " abc street\n"+ "
\n"+ "
"; - JSONML.toJSONObject(xmlStr); + JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) @@ -56,7 +56,7 @@ public void invalidBangInTagException() { " \n"+ "
\n"+ "
"; - JSONML.toJSONObject(xmlStr); + JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) @@ -70,7 +70,7 @@ public void invalidBangNoCloseInTagException() { " \n"+ ""; - JSONML.toJSONObject(xmlStr); + JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) @@ -84,7 +84,7 @@ public void noCloseStartTagException() { " \n"+ ""; - JSONML.toJSONObject(xmlStr); + JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) @@ -98,23 +98,23 @@ public void invalidCDATABangInTagException() { " \n"+ " \n"+ ""; - JSONML.toJSONObject(xmlStr); + JSONML.toJSONArray(xmlStr); } @Test(expected=NullPointerException.class) public void nullJSONXMLException() { - JSONObject jsonObject= null; - JSONML.toString(jsonObject); + JSONArray jsonArray= null; + JSONML.toString(jsonArray); } @Test(expected=JSONException.class) public void emptyJSONXMLException() { - JSONObject jsonObject= new JSONObject(); - JSONML.toString(jsonObject); + JSONArray jsonArray = new JSONArray(); + JSONML.toString(jsonArray); } @Test - public void noStartTag() { + public void complexTypeXML() { String xmlStr = "\n"+ "\n"+ ""; String expectedStr = - "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ - "\"childNodes\":[{"+ - "\"childNodes\":"+ - "[{\"tagName\":\"name\"},"+ - "{\"tagName\":\"nocontent\"},"+ - "\">\"],"+ - "\"tagName\":\"address\"}],"+ - "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ - "\"tagName\":\"addresses\"}"; - JSONObject jsonObject = JSONML.toJSONObject(xmlStr); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + "[\"addresses\","+ + "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ + "[\"address\","+ + "[\"name\"],"+ + "[\"nocontent\"],"+ + "\">\""+ + "]"+ + "]"; + JSONArray jsonArray = JSONML.toJSONArray(xmlStr); + JSONArray expectedJsonArray = new JSONArray(expectedStr); + Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray); } @Test - public void simpleXML() { + public void basicXMLAsObject() { String xmlStr = "\n"+ "-23\n"+ "-23.45\n"+ "-23x.45\n"+ - "1, 2, 3, 4.1, 5.2\n"+ + "\n"+ + "1\n"+ + "2\n"+ + "3\n"+ + "4.1\n"+ + "5.2\n"+ + "\n"+ "\n"+ ""; @@ -183,8 +189,19 @@ public void simpleXML() { "\"tagName\":\"DoubleValue\"},"+ "{\"childNodes\":[\"-23x.45\"],"+ "\"tagName\":\"Nan\"},"+ - "{\"childNodes\":[\"1, 2, 3, 4.1, 5.2\"],"+ - "\"tagName\":\"ArrayOfNum\"}],"+ + "{\"childNodes\":["+ + "{\"childNodes\":[1],"+ + "\"tagName\":\"value\"},"+ + "{\"childNodes\":[1],"+ + "\"tagName\":\"value\"},"+ + "{\"childNodes\":[1],"+ + "\"tagName\":\"value\"},"+ + "{\"childNodes\":[1],"+ + "\"tagName\":\"value\"},"+ + "{\"childNodes\":[1],"+ + "\"tagName\":\"value\"},"+ + "]," + "\"tagName\":\"address\"}],"+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ "\"tagName\":\"addresses\"}"; @@ -193,6 +210,53 @@ public void simpleXML() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + public void basicXMLAsArray() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + "Joe Tester\n"+ + "[CDATA[Baker street 5]\n"+ + "\n"+ + "true\n"+ + "false\n"+ + "null\n"+ + "42\n"+ + "-23\n"+ + "-23.45\n"+ + "-23x.45\n"+ + "\n"+ + "1\n"+ + "2\n"+ + "3\n"+ + "4.1\n"+ + "5.2\n"+ + "\n"+ + "
\n"+ + "
"; + + String expectedStr = + "[\"addresses\","+ + "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ + "[\"address\","+ + "[\"name\", \"Joe Tester\"]"+ + "[\"street\", \"[CDATA[Baker street 5]\"]"+ + "[\"NothingHere\"]"+ + "[\"TrueValue\", true]"+ + "[\"FalseValue\", false]"+ + "[\"NullValue\", null]"+ + "[\"PositiveValue\", 42]"+ + "[\"NegativeValue\", -23]"+ + "[\"DoubleValue\", -23.45]"+ + "[\"Nan\", \"-23x.45\"]"+ + "[\"ArrayOfNum\", ]"+ + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + @Test public void commentsInXML() { From de13c7de86b3dd604246bac4e3c3b53310e28c90 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 1 Apr 2015 01:12:22 -0500 Subject: [PATCH 048/315] coverage 84.9% --- JSONMLTest.java | 254 +++++++++++++++++++++--------------------------- 1 file changed, 111 insertions(+), 143 deletions(-) diff --git a/JSONMLTest.java b/JSONMLTest.java index 3629fd4b2..04f49d77d 100644 --- a/JSONMLTest.java +++ b/JSONMLTest.java @@ -31,6 +31,38 @@ public void nonXMLException() { JSONML.toJSONArray(xmlStr); } + @Test(expected=JSONException.class) + public void emptyTagException() { + String jsonArrayStr = + "[\"addresses\","+ + "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ + "["+ + "[\"name\"],"+ + "[\"nocontent\"],"+ + "\">\""+ + "]"+ + "]"; + JSONArray jsonArray = new JSONArray(jsonArrayStr); + JSONML.toString(jsonArray); + } + + @Test(expected=JSONException.class) + public void spaceInTagException() { + String jsonArrayStr = + "[\"addresses\","+ + "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ + "[\"addr esses\","+ + "[\"name\"],"+ + "[\"nocontent\"],"+ + "\">\""+ + "]"+ + "]"; + JSONArray jsonArray = new JSONArray(jsonArrayStr); + JSONML.toString(jsonArray); + } + @Test(expected=JSONException.class) public void unvalidSlashInTagException() { String xmlStr = @@ -136,7 +168,10 @@ public void complexTypeXML() { "]"; JSONArray jsonArray = JSONML.toJSONArray(xmlStr); JSONArray expectedJsonArray = new JSONArray(expectedStr); - Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray); + String xmlToStr = JSONML.toString(jsonArray); + JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } @Test @@ -166,50 +201,56 @@ public void basicXMLAsObject() { "\n"+ ""; - String expectedStr = + String expectedStr = "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ - "\"childNodes\":[{"+ "\"childNodes\":["+ - "{\"childNodes\":[\"Joe Tester\"],"+ - "\"tagName\":\"name\"},"+ - "{\"childNodes\":[\"[CDATA[Baker street 5]\"],"+ - "\"tagName\":\"street\"},"+ - "{\"tagName\":\"NothingHere\"},"+ - "{\"childNodes\":[true],"+ + "{\"childNodes\":["+ + "{\"childNodes\":[\"Joe Tester\"],"+ + "\"tagName\":\"name\"},"+ + "{\"childNodes\":[\"[CDATA[Baker street 5]\"],"+ + "\"tagName\":\"street\"},"+ + "{\"tagName\":\"NothingHere\"},"+ + "{\"childNodes\":[true],"+ "\"tagName\":\"TrueValue\"},"+ - "{\"childNodes\":[false],"+ + "{\"childNodes\":[false],"+ "\"tagName\":\"FalseValue\"},"+ - "{\"childNodes\":[null],"+ + "{\"childNodes\":[null],"+ "\"tagName\":\"NullValue\"},"+ - "{\"childNodes\":[42],"+ + "{\"childNodes\":[42],"+ "\"tagName\":\"PositiveValue\"},"+ - "{\"childNodes\":[-23],"+ + "{\"childNodes\":[-23],"+ "\"tagName\":\"NegativeValue\"},"+ - "{\"childNodes\":[-23.45],"+ + "{\"childNodes\":[-23.45],"+ "\"tagName\":\"DoubleValue\"},"+ - "{\"childNodes\":[\"-23x.45\"],"+ + "{\"childNodes\":[\"-23x.45\"],"+ "\"tagName\":\"Nan\"},"+ - "{\"childNodes\":["+ - "{\"childNodes\":[1],"+ - "\"tagName\":\"value\"},"+ - "{\"childNodes\":[1],"+ - "\"tagName\":\"value\"},"+ - "{\"childNodes\":[1],"+ - "\"tagName\":\"value\"},"+ - "{\"childNodes\":[1],"+ - "\"tagName\":\"value\"},"+ - "{\"childNodes\":[1],"+ - "\"tagName\":\"value\"},"+ - "]," - - "\"tagName\":\"address\"}],"+ - "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ - "\"tagName\":\"addresses\"}"; + "{\"childNodes\":["+ + "{\"childNodes\":[1],"+ + "\"tagName\":\"value\"},"+ + "{\"childNodes\":[2],"+ + "\"tagName\":\"value\"},"+ + "{\"childNodes\":[3],"+ + "\"tagName\":\"value\"},"+ + "{\"childNodes\":[4.1],"+ + "\"tagName\":\"value\"},"+ + "{\"childNodes\":[5.2],"+ + "\"tagName\":\"value\"}"+ + "],"+ + "\"tagName\":\"ArrayOfNum\"}"+ + "],"+ + "\"tagName\":\"address\"}"+ + "],"+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ + "\"tagName\":\"addresses\"}"; JSONObject jsonObject = JSONML.toJSONObject(xmlStr); JSONObject expectedJsonObject = new JSONObject(expectedStr); + String xmlToStr = JSONML.toString(jsonObject); + JSONObject finalJsonObject = JSONML.toJSONObject(xmlToStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); } + @Test public void basicXMLAsArray() { String xmlStr = "\n"+ @@ -236,25 +277,38 @@ public void basicXMLAsArray() { "\n"+ ""; - String expectedStr = + String expectedStr = "[\"addresses\","+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ - "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ "[\"address\","+ - "[\"name\", \"Joe Tester\"]"+ - "[\"street\", \"[CDATA[Baker street 5]\"]"+ - "[\"NothingHere\"]"+ - "[\"TrueValue\", true]"+ - "[\"FalseValue\", false]"+ - "[\"NullValue\", null]"+ - "[\"PositiveValue\", 42]"+ - "[\"NegativeValue\", -23]"+ - "[\"DoubleValue\", -23.45]"+ - "[\"Nan\", \"-23x.45\"]"+ - "[\"ArrayOfNum\", ]"+ - JSONObject jsonObject = JSONML.toJSONObject(xmlStr); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + "[\"name\",\"Joe Tester\"],"+ + "[\"street\",\"[CDATA[Baker street 5]\"],"+ + "[\"NothingHere\"],"+ + "[\"TrueValue\",true],"+ + "[\"FalseValue\",false],"+ + "[\"NullValue\",null],"+ + "[\"PositiveValue\",42],"+ + "[\"NegativeValue\",-23],"+ + "[\"DoubleValue\",-23.45],"+ + "[\"Nan\",\"-23x.45\"],"+ + "[\"ArrayOfNum\","+ + "[\"value\",1],"+ + "[\"value\",2],"+ + "[\"value\",3],"+ + "[\"value\",4.1],"+ + "[\"value\",5.2]"+ + "]"+ + "]"+ + "]"; + JSONArray jsonArray = JSONML.toJSONArray(xmlStr); + JSONArray expectedJsonArray = new JSONArray(expectedStr); + String xmlToStr = JSONML.toString(jsonArray); + JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + // TODO: this test fails because JSONML.toString() does not emit values + // for true, false, null, and numbers + // Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } @Test @@ -272,108 +326,22 @@ public void commentsInXML() { "Baker street 5\n"+ "\n"+ ""; - String expectedStr = - "{\"childNodes\":["+ - "{\"childNodes\":["+ - "\" this is -- comment \","+ - "{\"childNodes\":[\"Joe Tester\"],"+ - "\"tagName\":\"name\"},"+ - "{\"childNodes\":[\"Baker street 5\"],"+ - "\"tagName\":\"street\"}],"+ - "\"tagName\":\"address\"}],"+ - "\"tagName\":\"addresses\"}"; - JSONObject jsonObject = JSONML.toJSONObject(xmlStr); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); - } - - @Test - public void jsonObjectToString() { - String xmlStr = - "\n"+ - "\n"+ - "
\n"+ - " [CDATA[Joe & T > e < s " t ' er]]\n"+ - " Baker street 5\n"+ - " 1, 2, 3, 4.1, 5.2\n"+ - "
\n"+ - "
"; - - String expectedStr = - "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ - "\"childNodes\":["+ - "{\"childNodes\":["+ - "{\"childNodes\":[\"[CDATA[Joe & T > e < s \\\" t ' er]]\"],"+ - "\"tagName\":\"name\"},"+ - "{\"childNodes\":[\"Baker street 5\"],"+ - "\"tagName\":\"street\"},"+ - "{\"childNodes\":[\"1, 2, 3, 4.1, 5.2\"],"+ - "\"tagName\":\"ArrayOfNum\"}],"+ - "\"tagName\":\"address\"}],"+ - "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ - "\"tagName\":\"addresses\"}"; - - JSONObject jsonObject = JSONML.toJSONObject(xmlStr); - String xmlToStr = JSONML.toString(jsonObject); - JSONObject finalJsonObject = JSONML.toJSONObject(xmlToStr); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); - Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); - } - - @Test - public void jsonArrayToString() { - String xmlStr = - ""+ - ""+ - "5"+ - "10"+ - "15"+ - ""+ - "val2"+ - "val3"+ - ""+ - "-6"+ - "true"+ - ""+ - "false"+ - "null"+ - ""+ - "10"+ - "20"+ - "33.33"+ - "5220"+ - ""+ - ""; String expectedStr = - "[\"tag0\","+ - "[\"tag1\","+ - "[\"tag2\",5],"+ - "[\"tag2\",10],"+ - "[\"tag2\",15]"+ - "],"+ - "[\"tag2\",\"val2\"],"+ - "[\"tag3\",\"val3\"],"+ - "[\"tag4\","+ - "[\"tag5\",-6],"+ - "[\"tag5\",true]"+ - "],"+ - "[\"tag6\",false],"+ - "[\"tag7\",null],"+ - "[\"tag1\","+ - "[\"tag8\",10],"+ - "[\"tag8\",20],"+ - "[\"tag8\",33.33],"+ - "[\"tag8\",5220]"+ + "[\"addresses\","+ + "[\"address\","+ + "\" this is -- comment \","+ + "[\"name\",\"Joe Tester\"],"+ + "[\"street\",\"Baker street 5\"]"+ "]"+ "]"; JSONArray jsonArray = JSONML.toJSONArray(xmlStr); + JSONArray expectedJsonArray = new JSONArray(expectedStr); String xmlToStr = JSONML.toString(jsonArray); JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr); - JSONArray expectedJsonArray= new JSONArray(expectedStr); - Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray); - Util.compareActualVsExpectedJsonArrays(finalJsonArray,expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + // TODO this test fails because JSONML.toString() escapes the + // <> chars in the comment. + // Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } } From 45d7503e249c7cc304583ae99b22245cf06663f8 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Wed, 1 Apr 2015 01:23:04 -0500 Subject: [PATCH 049/315] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8555eb605..8769cc427 100644 --- a/README.md +++ b/README.md @@ -24,22 +24,22 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 20.8% | | | +| Total coverage | 47.1% | | | | | | | | CDL.java | 94.8% | Completed | | Cookie.java | 97.5% | Completed | | CookieList.java |0% | | | HTTP.java | 0%| | | HTTPTokener.java |0% | | -| JSONArray.java |15.3% | | +| JSONArray.java |19.5% | | | JSONException.java | 26.7% | | -| JSONML.java | 0%| In progress| -| JSONObject.Null | 17.6% | | | +| JSONML.java | 84.9%| completed | +| JSONObject.Null | 75.0% | | | | JSONStringer.java | 0%| | | JSONTokener.java |68.5% | | | JSONWriter.java | 0% | | | Property.java | 94.8% | Completed | -| XML.java | 81.2% | Completed | -| XMLTokener.java| 82.2%| Completed | +| XML.java | 85.1% | Completed | +| XMLTokener.java| 82.7%| Completed | From e80ded6ebe6c74fe6bf9ec2ab7c0435edd6d92e8 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 2 Apr 2015 21:31:15 -0500 Subject: [PATCH 050/315] fixed comment test 83.2% coverage --- JSONMLTest.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/JSONMLTest.java b/JSONMLTest.java index 04f49d77d..729c68f4e 100644 --- a/JSONMLTest.java +++ b/JSONMLTest.java @@ -319,7 +319,7 @@ public void commentsInXML() { "\n"+ "\n"+ "
\n"+ - " comment ]]>\n"+ + "\n"+ "Joe Tester\n"+ "\n"+ @@ -329,7 +329,6 @@ public void commentsInXML() { String expectedStr = "[\"addresses\","+ "[\"address\","+ - "\" this is -- comment \","+ "[\"name\",\"Joe Tester\"],"+ "[\"street\",\"Baker street 5\"]"+ "]"+ @@ -339,9 +338,7 @@ public void commentsInXML() { String xmlToStr = JSONML.toString(jsonArray); JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); - // TODO this test fails because JSONML.toString() escapes the - // <> chars in the comment. - // Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } } From 5ee4a3fc12fad158c8782ef4288935b18954ba1a Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 2 Apr 2015 21:44:35 -0500 Subject: [PATCH 051/315] fixed comment test 83.2% coverage --- JSONMLTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/JSONMLTest.java b/JSONMLTest.java index 729c68f4e..b2a4e0633 100644 --- a/JSONMLTest.java +++ b/JSONMLTest.java @@ -5,7 +5,6 @@ import org.json.*; import org.junit.Test; - /** * Tests for JSON-Java JSONML.java */ From ac8b1b098ada45301813a6ba71346c8f6de4b012 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 3 Apr 2015 11:29:16 -0500 Subject: [PATCH 052/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8769cc427..7e5f59454 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | | | | | CDL.java | 94.8% | Completed | | Cookie.java | 97.5% | Completed | -| CookieList.java |0% | | +| CookieList.java |0% | In progress | | HTTP.java | 0%| | | HTTPTokener.java |0% | | | JSONArray.java |19.5% | | From 3406acd0aa2bc6a5e355b5b5da4ce2109b042dcc Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 3 Apr 2015 14:35:59 -0500 Subject: [PATCH 053/315] 97.5% coverage --- CookieTest.java | 268 ++++++++++++++++-------------------------------- 1 file changed, 86 insertions(+), 182 deletions(-) diff --git a/CookieTest.java b/CookieTest.java index d8453e943..d5bc239fb 100644 --- a/CookieTest.java +++ b/CookieTest.java @@ -10,211 +10,115 @@ /** * Tests for JSON-Java Cookie.java - * Paraphrased from: - * http://www.nczonline.net/blog/2009/05/05/http-cookies-explained/ - * - * A web server specifies a cookie to be stored by sending an HTTP header - * called Set-Cookie. The format of the Set-Cookie header is a string as - * follows (parts in square brackets are optional): - * Set-Cookie: value[; expires=date][; domain=domain][; path=path][; secure] - * Where value is usually, but not always, a key/value pair: name=value - * Separators between the optional segments (e.g. expires=date) consist of a - * semicolon followed by a space. - * - * Although cookies are typically url-encoded, they don't have to be. - * - * expires date example: - * Set-Cookie: name=Nicholas; expires=Sat, 02 May 2009 23:38:25 GMT - * - * domain option example: - * Set-Cookie: name=Nicholas; domain=nczonline.net - * - * Path option example: - * Set-Cookie: name=Nicholas; path=/blog - * - * Secure option example (it is just a flag): - * Set-Cookie: name=Nicholas; secure - * - * Subcookies. There is a hard limit of size (4k) that can't be finessed. - * But many browsers (not Chrome) have a max cookies per domain limit - * (usually 50). To get around this, subcookies are encoded in the initial - * name/value pair as follows: - * name=a=b&c=d&e=f&g=h + * See RFC6265 + * At its most basic, a cookie is a name=value pair. + * A JSON-Java encoded cookie escapes '+', '%', '=', ';' with %hh values. */ public class CookieTest { - String simpleCookieStr = - "PH=deleted"+ - "; expires=Wed, 19-Mar-2014 17:53:53 GMT"+ - ";path=/"+ - "; domain=.yahoo.com"+ - ";secure"+ - ";not=included"; - - String encodedCookieStr = - "PH=contains+some+chars"+ - ";expires=Wed, 19-Mar-2014 17:53:53 GMT"+ - "; path=/"+ - ";domain=.yahoo.com?some+escape+chars"+ - "; secure"+ - "; CRZY=%7B%2233748770511_20150319%22%3A%7B%22expires%22%3A142696041"+ - "3419%2C%22data%22%3A%7B%22nv%22%3A3%2C%22bn%22%3A0%2C%22collapsed%2"+ - "2%3A0%7D%7D%7D"; - @Test(expected=NullPointerException.class) - public void shouldHandleNullCookie() { + public void nullCookieException() { String cookieStr = null; Cookie.toJSONObject(cookieStr); } @Test(expected=JSONException.class) - public void shouldHandleEmptyStringCookie() { + public void malFormedCookieException() { + String cookieStr = "thisCookieHasNoEqualsChar"; + Cookie.toJSONObject(cookieStr); + } + + @Test(expected=JSONException.class) + public void emptyStringCookieException() { + /** + * Cookie throws an exception, but CookieList does not + */ String cookieStr = ""; Cookie.toJSONObject(cookieStr); } @Test - public void shouldHandleNonEncodedCookie() { - JSONObject jsonObject = Cookie.toJSONObject(simpleCookieStr); - Set keySet = jsonObject.keySet(); - assertTrue("Keyset should have exactly 7 keys", keySet.size() == 7); - assertTrue("name should have expected value", - "PH".equals(jsonObject.getString("name"))); - assertTrue("Value should have expected value", - "deleted".equals(jsonObject.getString("value"))); - assertTrue("expires should have expected value", - "Wed, 19-Mar-2014 17:53:53 GMT".equals( - jsonObject.getString("expires"))); - assertTrue("domain should have expected value", - ".yahoo.com".equals( - jsonObject.getString("domain"))); - assertTrue("path should have expected value", - "/".equals( - jsonObject.getString("path"))); - assertTrue("not should have expected value", - "included".equals( - jsonObject.getString("not"))); - Boolean secureBool = jsonObject.getBoolean("secure"); - assertTrue("secure should be found in jsonObject", secureBool != null); - assertTrue("secure should have expected value", - secureBool.equals(true)); + public void simpleCookie() { + String cookieStr = "SID=31d4d96e407aad42"; + String expectedCookieStr = "{\"name\":\"SID\",\"value\":\"31d4d96e407aad42\"}"; + JSONObject jsonObject = Cookie.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test - public void shouldConvertNonEncodedCookieToString() { - int idx; - String expectedStr; - JSONObject jsonObject = Cookie.toJSONObject(simpleCookieStr); - String cookieStr = Cookie.toString(jsonObject); - - // check for unordered expected output - expectedStr = "path=/"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("path should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "expires=Wed, 19-Mar-2014 17:53:53 GMT"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("expires should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "domain=.yahoo.com"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("domain should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "PH=deleted"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("name/value should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "secure"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("secure should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - // after semicolons, nothing should be left - cookieStr = cookieStr.replaceAll(";", ""); - assertTrue("nothing else should remain in cookie toString()", - cookieStr.length() == 0); + public void multiPartCookie() { + String cookieStr = + "PH=deleted; "+ + " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ + "path=/; "+ + " domain=.yahoo.com;"+ + "secure"; + String expectedCookieStr = + "{\"path\":\"/\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"name\":\"PH\","+ + "\"secure\":true,"+ + "\"value\":\"deleted\"}"; + JSONObject jsonObject = Cookie.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test - public void shouldHandleEncodedCookie() { - JSONObject jsonObject = Cookie.toJSONObject(encodedCookieStr); - Set keySet = jsonObject.keySet(); - // Note: the 7th key/value is not used by Cookie.java - assertTrue("Keyset should have exactly 7 keys", keySet.size() == 7); - assertTrue("name should have expected value", - "PH".equals(jsonObject.getString("name"))); - assertTrue("Value should have expected value", - "contains+some+chars".equals(jsonObject.getString("value"))); - assertTrue("expires should have expected value", - "Wed, 19-Mar-2014 17:53:53 GMT".equals( - jsonObject.getString("expires"))); - assertTrue("domain should have expected value", - ".yahoo.com?some escape chars".equals( - jsonObject.getString("domain"))); - assertTrue("path should have expected value", - "/".equals( - jsonObject.getString("path"))); - Boolean secureBool = jsonObject.getBoolean("secure"); - assertTrue("secure should be found in jsonObject", secureBool != null); - assertTrue("secure should have expected value", - secureBool.equals(true)); - String expectedStr = "{\"33748770511_20150319\":{\"expires\":14269604134"+ - "19,\"data\":{\"nv\":3,\"bn\":0,\"collapsed\":0}}}"; - assertTrue("CRZY should have expected value", - expectedStr.equals(jsonObject.getString("CRZY"))); + public void convertCookieToString() { + String cookieStr = + "PH=deleted; "+ + " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ + "path=/; "+ + " domain=.yahoo.com;"+ + "thisWont=beIncluded;"+ + "secure"; + String expectedCookieStr = + "{\"path\":\"/\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"name\":\"PH\","+ + "\"secure\":true,"+ + "\"value\":\"deleted\"}"; + /** + * ToString() will omit the non-standard segment, + * but it will still be stored in the JSONObject + */ + String expectedDirectCompareCookieStr = + expectedCookieStr.replaceAll("\\{", "\\{\"thisWont\":\"beIncluded\","); + JSONObject jsonObject = Cookie.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + JSONObject expectedDirectCompareJsonObject = + new JSONObject(expectedDirectCompareCookieStr); + String cookieToStr = Cookie.toString(jsonObject); + JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedDirectCompareJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } @Test - public void shouldConvertEncodedCookieToString() { - int idx; - String expectedStr; - JSONObject jsonObject = Cookie.toJSONObject(encodedCookieStr); - String cookieStr = Cookie.toString(jsonObject); - - // check for unordered expected output - expectedStr = "path=/"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("path should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "expires=Wed, 19-Mar-2014 17:53:53 GMT"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("expires should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "domain=.yahoo.com?some escape chars"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("domain should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "PH=contains%2bsome%2bchars"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("name/value should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "secure"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("secure should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - // after semicolons, nothing should be left - cookieStr = cookieStr.replaceAll(";", ""); - assertTrue("nothing else should remain in cookie toString()", - cookieStr.length() == 0); + public void convertEncodedCookieToString() { + String cookieStr = + "PH=deleted; "+ + " expires=Wed,+19-Mar-2014+17:53:53+GMT;"+ + "path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+ + " domain=.yahoo.com;"+ + "secure"; + String expectedCookieStr = + "{\"path\":\"/+this/is&/a/spec;segment=\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"name\":\"PH\","+ + "\"secure\":true,"+ + "\"value\":\"deleted\"}"; + JSONObject jsonObject = Cookie.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + String cookieToStr = Cookie.toString(jsonObject); + JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } } From 519c21c8b06b603b3129928d70ada198fe09df95 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 3 Apr 2015 14:36:18 -0500 Subject: [PATCH 054/315] 96.5% coverage --- CookieListTest.java | 113 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 CookieListTest.java diff --git a/CookieListTest.java b/CookieListTest.java new file mode 100644 index 000000000..c8e4c8a20 --- /dev/null +++ b/CookieListTest.java @@ -0,0 +1,113 @@ +package org.json.junit; + +import java.util.*; + +import static org.junit.Assert.*; + +import org.json.*; +import org.junit.Test; + + +/** + * Tests for JSON-Java CookieList.java + * The main differences between Cookie and CookieList appears to be that + * CookieList does not treat the initial name/value pair different than + * the other segments, and does not handle "secure". + * Therefore the tests will be similar, but not identical. + */ +public class CookieListTest { + @Test(expected=NullPointerException.class) + public void nullCookieListException() { + String cookieStr = null; + CookieList.toJSONObject(cookieStr); + } + + @Test(expected=JSONException.class) + public void malFormedCookieListException() { + String cookieStr = "thisCookieHasNoEqualsChar"; + CookieList.toJSONObject(cookieStr); + } + + @Test(expected=JSONException.class) + public void emptyStringCookieList() { + /** + * Cookie throws an exception, but CookieList does not + */ + String cookieStr = ""; + String expectedCookieStr = ""; + JSONObject jsonObject = CookieList.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void simpleCookieList() { + String cookieStr = "SID=31d4d96e407aad42"; + String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}"; + JSONObject jsonObject = CookieList.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void multiPartCookieList() { + String cookieStr = + "PH=deleted; "+ + " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ + "path=/; "+ + " domain=.yahoo.com;"; + String expectedCookieStr = + "{\"path\":\"/\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"name\":\"PH\","+ + "\"value\":\"deleted\"}"; + JSONObject jsonObject = CookieList.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void convertCookieListToString() { + String cookieStr = + "PH=deleted; "+ + " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ + "path=/; "+ + " domain=.yahoo.com;"+ + "thisWont=beIncluded;"; + String expectedCookieStr = + "{\"path\":\"/\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"thisWont\":\"beIncluded\","+ + "\"PH\":\"deleted\"}"; + JSONObject jsonObject = CookieList.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + String cookieToStr = CookieList.toString(jsonObject); + JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } + + @Test + public void convertEncodedCookieListToString() { + String cookieStr = + "PH=deleted; "+ + " expires=Wed,+19-Mar-2014+17:53:53+GMT;"+ + "path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+ + " domain=.yahoo.com;"; + String expectedCookieStr = + "{\"path\":\"/+this/is&/a/spec;segment=\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"PH\":\"deleted\"}"; + JSONObject jsonObject = CookieList.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + String cookieToStr = CookieList.toString(jsonObject); + JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } + + +} From 970e7a45a9bdb409d77753e77103a3597ce7cf25 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 3 Apr 2015 14:36:40 -0500 Subject: [PATCH 055/315] added cookielisttest --- JunitTestSuite.java | 1 + 1 file changed, 1 insertion(+) diff --git a/JunitTestSuite.java b/JunitTestSuite.java index 8c680a0d9..e76a7c3a5 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -6,6 +6,7 @@ @Suite.SuiteClasses({ CDLTest.class, CookieTest.class, + CookieListTest.class, PropertyTest.class, XMLTest.class, JSONMLTest.class From 83ac581f3d9bee43adb2e8accd3d43452e41491e Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 3 Apr 2015 14:46:45 -0500 Subject: [PATCH 056/315] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7e5f59454..00bbaad7c 100644 --- a/README.md +++ b/README.md @@ -24,19 +24,19 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 47.1% | | | +| Total coverage | 48.4% | | | | | | | | CDL.java | 94.8% | Completed | | Cookie.java | 97.5% | Completed | -| CookieList.java |0% | In progress | -| HTTP.java | 0%| | -| HTTPTokener.java |0% | | +| CookieList.java |96.5% | Completed | +| HTTP.java | 0%| In progress | +| HTTPTokener.java |0% |In progress | | JSONArray.java |19.5% | | | JSONException.java | 26.7% | | -| JSONML.java | 84.9%| completed | +| JSONML.java | 83.2%| completed | | JSONObject.Null | 75.0% | | | | JSONStringer.java | 0%| | -| JSONTokener.java |68.5% | | +| JSONTokener.java |70.3% | | | JSONWriter.java | 0% | | | Property.java | 94.8% | Completed | | XML.java | 85.1% | Completed | From f7b51414b8c013e12f08f53e45d0dcad3c369a35 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 3 Apr 2015 14:46:51 -0500 Subject: [PATCH 057/315] 96.5% coverage --- CookieListTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CookieListTest.java b/CookieListTest.java index c8e4c8a20..d8b4a5b10 100644 --- a/CookieListTest.java +++ b/CookieListTest.java @@ -60,8 +60,7 @@ public void multiPartCookieList() { "{\"path\":\"/\","+ "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ "\"domain\":\".yahoo.com\","+ - "\"name\":\"PH\","+ - "\"value\":\"deleted\"}"; + "\"PH\":\"deleted\"}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); From 400bbd7fbf17c66c572ab175d9c04eab28d73ddc Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 3 Apr 2015 19:12:15 -0500 Subject: [PATCH 058/315] 98.7% coverage --- HTTPTest.java | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 HTTPTest.java diff --git a/HTTPTest.java b/HTTPTest.java new file mode 100644 index 000000000..04b9dafae --- /dev/null +++ b/HTTPTest.java @@ -0,0 +1,149 @@ +package org.json.junit; + +import java.util.*; + +import static org.junit.Assert.*; + +import org.json.*; +import org.junit.Test; + + +/** + * Tests for JSON-Java HTTP.java + * See RFC7230 + */ +public class HTTPTest { + + @Test(expected=NullPointerException.class) + public void nullHTTPException() { + String httpStr = null; + HTTP.toJSONObject(httpStr); + } + + @Test(expected=JSONException.class) + public void notEnoughHTTPException() { + String httpStr = "{}"; + JSONObject jsonObject = new JSONObject(httpStr); + HTTP.toString(jsonObject); + } + + @Test + public void emptyStringHTTPException() { + String httpStr = ""; + String expectedHTTPStr = "{\"Request-URI\":\"\",\"Method\":\"\",\"HTTP-Version\":\"\"}"; + JSONObject jsonObject = HTTP.toJSONObject(httpStr); + JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void simpleHTTPRequest() { + String httpStr = "GET /hello.txt HTTP/1.1"; + String expectedHTTPStr = + "{\"Request-URI\":\"/hello.txt\",\"Method\":\"GET\",\"HTTP-Version\":\"HTTP/1.1\"}"; + JSONObject jsonObject = HTTP.toJSONObject(httpStr); + JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void simpleHTTPResponse() { + String httpStr = "HTTP/1.1 200 OK"; + String expectedHTTPStr = + "{\"HTTP-Version\":\"HTTP/1.1\",\"Status-Code\":\"200\",\"Reason-Phrase\":\"OK\"}"; + JSONObject jsonObject = HTTP.toJSONObject(httpStr); + JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void extendedHTTPRequest() { + String httpStr = + "POST /enlighten/calais.asmx HTTP/1.1\n"+ + "Host: api.opencalais.com\n"+ + "Content-Type: text/xml; charset=utf-8\n"+ + "Content-Length: 100\n"+ + "SOAPAction: \"http://clearforest.com/Enlighten\""; + String expectedHTTPStr = + "{"+ + "\"Request-URI\":\"/enlighten/calais.asmx\","+ + "\"Host\":\"api.opencalais.com\","+ + "\"Method\":\"POST\","+ + "\"HTTP-Version\":\"HTTP/1.1\","+ + "\"Content-Length\":\"100\","+ + "\"Content-Type\":\"text/xml; charset=utf-8\"}"; + JSONObject jsonObject = HTTP.toJSONObject(httpStr); + JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); + // not too easy for JSONObject to parse a string with embedded quotes + expectedJsonObject.put("SOAPAction","\"http://clearforest.com/Enlighten\""); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void extendedHTTPResponse() { + String httpStr = + "HTTP/1.1 200 OK\n"+ + "Content-Type: text/xml; charset=utf-8\n"+ + "Content-Length: 100\n"; + String expectedHTTPStr = + "{\"HTTP-Version\":\"HTTP/1.1\","+ + "\"Status-Code\":\"200\","+ + "\"Content-Length\":\"100\","+ + "\"Reason-Phrase\":\"OK\","+ + "\"Content-Type\":\"text/xml; charset=utf-8\"}"; + JSONObject jsonObject = HTTP.toJSONObject(httpStr); + JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void convertHTTPRequestToString() { + String httpStr = + "POST /enlighten/calais.asmx HTTP/1.1\n"+ + "Host: api.opencalais.com\n"+ + "Content-Type: text/xml; charset=utf-8\n"+ + "Content-Length: 100"; + String expectedHTTPStr = + "{"+ + "\"Request-URI\":\"/enlighten/calais.asmx\","+ + "\"Host\":\"api.opencalais.com\","+ + "\"Method\":\"POST\","+ + "\"HTTP-Version\":\"HTTP/1.1\","+ + "\"Content-Length\":\"100\","+ + "\"Content-Type\":\"text/xml; charset=utf-8\"}"; + JSONObject jsonObject = HTTP.toJSONObject(httpStr); + JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); + String httpToStr = HTTP.toString(jsonObject); + // JSONObject objects to crlfs and any trailing chars + // httpToStr = httpToStr.replaceAll("(\r\n\r\n)", ""); + httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", ""); + httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n"); + JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } + + @Test + public void convertHTTPResponseToString() { + String httpStr = + "HTTP/1.1 200 OK\n"+ + "Content-Type: text/xml; charset=utf-8\n"+ + "Content-Length: 100\n"; + String expectedHTTPStr = + "{\"HTTP-Version\":\"HTTP/1.1\","+ + "\"Status-Code\":\"200\","+ + "\"Content-Length\":\"100\","+ + "\"Reason-Phrase\":\"OK\","+ + "\"Content-Type\":\"text/xml; charset=utf-8\"}"; + JSONObject jsonObject = HTTP.toJSONObject(httpStr); + JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); + String httpToStr = HTTP.toString(jsonObject); + // JSONObject objects to crlfs and any trailing chars + // httpToStr = httpToStr.replaceAll("(\r\n\r\n)", ""); + httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", ""); + httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n"); + JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } +} From 76cb83643d5db55311ebc8fe7ab29c318f0f2954 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 3 Apr 2015 19:12:41 -0500 Subject: [PATCH 059/315] add http test --- JunitTestSuite.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/JunitTestSuite.java b/JunitTestSuite.java index e76a7c3a5..b965fb878 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -9,7 +9,8 @@ CookieListTest.class, PropertyTest.class, XMLTest.class, - JSONMLTest.class + JSONMLTest.class, + HTTPTest.class }) public class JunitTestSuite { } \ No newline at end of file From 702a91827114bab63894079a0bc294bf41d2e8c4 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 3 Apr 2015 19:16:45 -0500 Subject: [PATCH 060/315] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 00bbaad7c..4d0a6c750 100644 --- a/README.md +++ b/README.md @@ -24,18 +24,18 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 48.4% | | | +| Total coverage | 52.6% | | | | | | | | CDL.java | 94.8% | Completed | | Cookie.java | 97.5% | Completed | | CookieList.java |96.5% | Completed | -| HTTP.java | 0%| In progress | -| HTTPTokener.java |0% |In progress | +| HTTP.java | 98.7%| Completed | +| HTTPTokener.java |93.2% |Completed | | JSONArray.java |19.5% | | | JSONException.java | 26.7% | | | JSONML.java | 83.2%| completed | | JSONObject.Null | 75.0% | | | -| JSONStringer.java | 0%| | +| JSONStringer.java | 0%| In progress | | JSONTokener.java |70.3% | | | JSONWriter.java | 0% | | | Property.java | 94.8% | Completed | From 03d1f0af727993f85a3c65eb572e8776456564e3 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 3 Apr 2015 19:18:13 -0500 Subject: [PATCH 061/315] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4d0a6c750..41996e99b 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | JSONArray.java |19.5% | | | JSONException.java | 26.7% | | | JSONML.java | 83.2%| completed | +| JSONObject | 19.0% | | | | JSONObject.Null | 75.0% | | | | JSONStringer.java | 0%| In progress | | JSONTokener.java |70.3% | | From 2db11cd4db8139e9a1ee2fd766ab0609377a6ef7 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 4 Apr 2015 14:00:57 -0500 Subject: [PATCH 062/315] add JSONStringerTest --- JunitTestSuite.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/JunitTestSuite.java b/JunitTestSuite.java index b965fb878..2519e8166 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -10,7 +10,8 @@ PropertyTest.class, XMLTest.class, JSONMLTest.class, - HTTPTest.class + HTTPTest.class, + JSONStringerTest.class }) public class JunitTestSuite { } \ No newline at end of file From b2e0a77ae06573f53d383743f7c39a6718362fb8 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 4 Apr 2015 14:01:29 -0500 Subject: [PATCH 063/315] 98.7% coverage --- HTTPTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/HTTPTest.java b/HTTPTest.java index 04b9dafae..52e166b57 100644 --- a/HTTPTest.java +++ b/HTTPTest.java @@ -1,9 +1,5 @@ package org.json.junit; -import java.util.*; - -import static org.junit.Assert.*; - import org.json.*; import org.junit.Test; From 8168e6f52a52843cf7b4547b090dc21dc9abefd8 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 4 Apr 2015 14:01:52 -0500 Subject: [PATCH 064/315] 93.8% coverage --- JSONStringerTest.java | 224 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 JSONStringerTest.java diff --git a/JSONStringerTest.java b/JSONStringerTest.java new file mode 100644 index 000000000..d76bfb105 --- /dev/null +++ b/JSONStringerTest.java @@ -0,0 +1,224 @@ +package org.json.junit; + +import org.json.*; +import org.junit.Test; + + +/** + * Tests for JSON-Java JSONStringer.java + */ +public class JSONStringerTest { + + @Test(expected=JSONException.class) + public void nullKeyException() { + JSONStringer jsonStringer = new JSONStringer(); + jsonStringer.object(); + jsonStringer.key(null); + } + + @Test(expected=JSONException.class) + public void outOfSequenceException() { + JSONStringer jsonStringer = new JSONStringer(); + jsonStringer.key("hi"); + } + + @Test(expected=JSONException.class) + public void missplacedArrayException() { + JSONStringer jsonStringer = new JSONStringer(); + jsonStringer.object().endObject(); + jsonStringer.array(); + } + + @Test(expected=JSONException.class) + public void missplacedEndArrayException() { + JSONStringer jsonStringer = new JSONStringer(); + jsonStringer.object(); + jsonStringer.endArray(); + } + + @Test(expected=JSONException.class) + public void missplacedEndObjectException() { + JSONStringer jsonStringer = new JSONStringer(); + jsonStringer.array(); + jsonStringer.endObject(); + } + + @Test(expected=JSONException.class) + public void missplacedObjectException() { + JSONStringer jsonStringer = new JSONStringer(); + jsonStringer.object().endObject(); + jsonStringer.object(); + } + + @Test(expected=JSONException.class) + public void exceedNestDepthException() { + new JSONStringer().object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(); + } + + @Test + public void simpleObjectString() { + String expectedStr = + "{"+ + "\"trueValue\":true,"+ + "\"falseValue\":false,"+ + "\"nullValue\":null,"+ + "\"stringValue\":\"hello world!\","+ + "\"complexStringValue\":\"h\be\tllo w\u1234orld!\","+ + "\"intValue\":42,"+ + "\"doubleValue\":-23.45e67"+ + "}"; + JSONStringer jsonStringer = new JSONStringer(); + jsonStringer.object(); + jsonStringer.key("trueValue").value(true); + jsonStringer.key("falseValue").value(false); + jsonStringer.key("nullValue").value(null); + jsonStringer.key("stringValue").value("hello world!"); + jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!"); + jsonStringer.key("intValue").value(42); + jsonStringer.key("doubleValue").value(-23.45e67); + jsonStringer.endObject(); + String str = jsonStringer.toString(); + JSONObject jsonObject = new JSONObject(str); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void simpleArrayString() { + String expectedStr = + "["+ + "true,"+ + "false,"+ + "null,"+ + "\"hello world!\","+ + "42,"+ + "-23.45e67"+ + "]"; + JSONStringer jsonStringer = new JSONStringer(); + jsonStringer.array(); + jsonStringer.value(true); + jsonStringer.value(false); + jsonStringer.value(null); + jsonStringer.value("hello world!"); + jsonStringer.value(42); + jsonStringer.value(-23.45e67); + jsonStringer.endArray(); + String str = jsonStringer.toString(); + JSONArray jsonArray = new JSONArray(str); + JSONArray expectedJsonArray = new JSONArray(expectedStr); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + } + + @Test + public void complexObjectString() { + String expectedStr = + "{"+ + "\"trueValue\":true,"+ + "\"falseValue\":false,"+ + "\"nullValue\":null,"+ + "\"stringValue\":\"hello world!\","+ + "\"object2\":{"+ + "\"k1\":\"v1\","+ + "\"k2\":\"v2\","+ + "\"k3\":\"v3\","+ + "\"array1\":["+ + "1,"+ + "2,"+ + "{"+ + "\"k4\":\"v4\","+ + "\"k5\":\"v5\","+ + "\"k6\":\"v6\","+ + "\"array2\":["+ + "5,"+ + "6,"+ + "7,"+ + "8"+ + "]"+ + "},"+ + "3,"+ + "4"+ + "]"+ + "},"+ + "\"complexStringValue\":\"h\be\tllo w\u1234orld!\","+ + "\"intValue\":42,"+ + "\"doubleValue\":-23.45e67"+ + "}"; + JSONStringer jsonStringer = new JSONStringer(); + jsonStringer.object(); + jsonStringer.key("trueValue").value(true); + jsonStringer.key("falseValue").value(false); + jsonStringer.key("nullValue").value(null); + jsonStringer.key("stringValue").value("hello world!"); + jsonStringer.key("object2").object(); + jsonStringer.key("k1").value("v1"); + jsonStringer.key("k2").value("v2"); + jsonStringer.key("k3").value("v3"); + jsonStringer.key("array1").array(); + jsonStringer.value(1); + jsonStringer.value(2); + jsonStringer.object(); + jsonStringer.key("k4").value("v4"); + jsonStringer.key("k5").value("v5"); + jsonStringer.key("k6").value("v6"); + jsonStringer.key("array2").array(); + jsonStringer.value(5); + jsonStringer.value(6); + jsonStringer.value(7); + jsonStringer.value(8); + jsonStringer.endArray(); + jsonStringer.endObject(); + jsonStringer.value(3); + jsonStringer.value(4); + jsonStringer.endArray(); + jsonStringer.endObject(); + jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!"); + jsonStringer.key("intValue").value(42); + jsonStringer.key("doubleValue").value(-23.45e67); + jsonStringer.endObject(); + String str = jsonStringer.toString(); + JSONObject jsonObject = new JSONObject(str); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + +} From 2219b5919b86906c80e96d9b06099c8ccdb717ea Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 4 Apr 2015 14:05:06 -0500 Subject: [PATCH 065/315] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 41996e99b..da0c4ee13 100644 --- a/README.md +++ b/README.md @@ -24,21 +24,21 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 52.6% | | | +| Total coverage | 58.4% | | | | | | | | CDL.java | 94.8% | Completed | | Cookie.java | 97.5% | Completed | | CookieList.java |96.5% | Completed | | HTTP.java | 98.7%| Completed | | HTTPTokener.java |93.2% |Completed | -| JSONArray.java |19.5% | | +| JSONArray.java |18.3% | | | JSONException.java | 26.7% | | | JSONML.java | 83.2%| completed | -| JSONObject | 19.0% | | | +| JSONObject | 24.9% | | in progress | | JSONObject.Null | 75.0% | | | -| JSONStringer.java | 0%| In progress | -| JSONTokener.java |70.3% | | -| JSONWriter.java | 0% | | +| JSONStringer.java | 93.8%| Completed | +| JSONTokener.java | 72.1% | | +| JSONWriter.java | 88.9% | Completed | | Property.java | 94.8% | Completed | | XML.java | 85.1% | Completed | | XMLTokener.java| 82.7%| Completed | From 22d5fd3aed9cc452d305bed5cd4154025b2195e3 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 6 Apr 2015 19:01:54 -0500 Subject: [PATCH 066/315] in progress --- JSONObjectTest.java | 161 +++++++++++++++++++++++++++++++++++++ JunitTestSuite.java | 3 +- MyBean.java | 37 +++++++++ StringsResourceBundle.java | 15 ++++ 4 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 JSONObjectTest.java create mode 100644 MyBean.java create mode 100644 StringsResourceBundle.java diff --git a/JSONObjectTest.java b/JSONObjectTest.java new file mode 100644 index 000000000..301f149fc --- /dev/null +++ b/JSONObjectTest.java @@ -0,0 +1,161 @@ +package org.json.junit; + +import java.util.*; + +import org.json.*; +import org.junit.*; + + +public class JSONObjectTest { + + + @Test + public void jsonObjectByNames() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"nullKey\":null,"+ + "\"stringKey\":\"hello world!\","+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e67"+ + "}"; + String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"}; + String expectedStr = + "{"+ + "\"falseKey\":false,"+ + "\"nullKey\":null,"+ + "\"stringKey\":\"hello world!\","+ + "\"doubleKey\":-23.45e67"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + JSONObject copyJsonObject = new JSONObject(jsonObject, keys); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(copyJsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByMap() { + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e67"+ + "}"; + Map jsonMap = new HashMap(); + jsonMap.put("trueKey", new Boolean(true)); + jsonMap.put("falseKey", new Boolean(false)); + jsonMap.put("stringKey", "hello world!"); + jsonMap.put("complexStringKey", "h\be\tllo w\u1234orld!"); + jsonMap.put("intKey", new Long(42)); + jsonMap.put("doubleKey", new Double(-23.45e67)); + + JSONObject jsonObject = new JSONObject(jsonMap); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByBean() { + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e7"+ + "}"; + MyBean myBean = new MyBean(); + JSONObject jsonObject = new JSONObject(myBean); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByBeanAndNames() { + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"doubleKey\":-23.45e7"+ + "}"; + String[] keys = {"trueKey", "complexStringKey", "doubleKey"}; + MyBean myBean = new MyBean(); + JSONObject jsonObject = new JSONObject(myBean, keys); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByResourceBundle() { + String expectedStr = + "{"+ + "\"greetings\": {"+ + "\"hello\":\"Hello, \","+ + "\"world\":\"World!\""+ + "},"+ + "\"farewells\": {"+ + "\"later\":\"Later, \","+ + "\"gator\":\"Alligator!\""+ + "}"+ + "}"; + JSONObject jsonObject = new + JSONObject("org.json.junit.StringsResourceBundle", + Locale.getDefault()); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectAccumulate() { + String expectedStr = + "{"+ + "\"myArray\": ["+ + "true,"+ + "false,"+ + "\"hello world!\","+ + "\"h\be\tllo w\u1234orld!\","+ + "42,"+ + "-23.45e7"+ + "]"+ + "}"; + JSONObject jsonObject = new JSONObject(); + jsonObject.accumulate("myArray", true); + jsonObject.accumulate("myArray", false); + jsonObject.accumulate("myArray", "hello world!"); + jsonObject.accumulate("myArray", "h\be\tllo w\u1234orld!"); + jsonObject.accumulate("myArray", 42); + jsonObject.accumulate("myArray", -23.45e7); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectAppend() { + String expectedStr = + "{"+ + "\"myArray\": ["+ + "true,"+ + "false,"+ + "\"hello world!\","+ + "\"h\be\tllo w\u1234orld!\","+ + "42,"+ + "-23.45e7"+ + "]"+ + "}"; + 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); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } +} diff --git a/JunitTestSuite.java b/JunitTestSuite.java index 2519e8166..63bde2fda 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -11,7 +11,8 @@ XMLTest.class, JSONMLTest.class, HTTPTest.class, - JSONStringerTest.class + JSONStringerTest.class, + JSONObjectTest.class }) public class JunitTestSuite { } \ No newline at end of file diff --git a/MyBean.java b/MyBean.java new file mode 100644 index 000000000..6477fdef1 --- /dev/null +++ b/MyBean.java @@ -0,0 +1,37 @@ +package org.json.junit; + +public class MyBean { + public int intKey; + public double doubleKey; + public String stringKey; + public String complexStringKey; + public boolean trueKey; + public boolean falseKey; + + public MyBean() { + intKey = 42; + doubleKey = -23.45e7; + stringKey = "hello world!"; + complexStringKey = "h\be\tllo w\u1234orld!"; + trueKey = true; + falseKey = false; + } + public int getIntKey() { + return intKey; + } + public double getDoubleKey() { + return doubleKey; + } + public String getStringKey() { + return stringKey; + } + public String getComplexStringKey() { + return complexStringKey; + } + public boolean isTrueKey() { + return trueKey; + } + public boolean isFalseKey() { + return falseKey; + } +} diff --git a/StringsResourceBundle.java b/StringsResourceBundle.java new file mode 100644 index 000000000..b1b9df4dd --- /dev/null +++ b/StringsResourceBundle.java @@ -0,0 +1,15 @@ +package org.json.junit; + +import java.util.*; + +public class StringsResourceBundle extends ListResourceBundle { + public Object[][] getContents() { + return contents; + } + static final Object[][] contents = { + {"greetings.hello", "Hello, "}, + {"greetings.world", "World!"}, + {"farewells.later", "Later, "}, + {"farewells.gator", "Alligator!"} + }; +} \ No newline at end of file From a9bce1d6b28a18ddbba35c660933914cde310f7f Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 8 Apr 2015 22:23:39 -0500 Subject: [PATCH 067/315] in progress --- JSONObjectTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 301f149fc..3fb20fce4 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -1,5 +1,7 @@ package org.json.junit; +import static org.junit.Assert.*; + import java.util.*; import org.json.*; @@ -158,4 +160,17 @@ public void jsonObjectAppend() { JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + + @Test + public void jsonObjectValuesToString() { + String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", null }; + Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, + new Double(1/0) }; + for (int i = 0; i < expectedStrs.length; ++i) { + String actualStr = JSONObject.doubleToString(doubles[i]); + assertTrue("double value expected ["+expectedStrs[i]+ + "] found ["+actualStr+ "]", + expectedStrs[i].equals(actualStr)); + } + } } From bef37079dcf3842185977c21f492da6cd54ce957 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 9 Apr 2015 18:02:45 -0500 Subject: [PATCH 068/315] in progress --- JSONObjectTest.java | 70 +++++++++++++++++++++++++++++++++++++++++---- Util.java | 13 +++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 3fb20fce4..aa434eadc 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -162,15 +162,75 @@ public void jsonObjectAppend() { } @Test - public void jsonObjectValuesToString() { - String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", null }; - Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, - new Double(1/0) }; + public void jsonObjectDoubleToString() { + String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68" }; + Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67 }; for (int i = 0; i < expectedStrs.length; ++i) { String actualStr = JSONObject.doubleToString(doubles[i]); - assertTrue("double value expected ["+expectedStrs[i]+ + assertTrue("value expected ["+expectedStrs[i]+ "] found ["+actualStr+ "]", expectedStrs[i].equals(actualStr)); } } + + @Test + public void jsonObjectValues() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"trueStrKey\":\"true\","+ + "\"falseStrKey\":\"false\","+ + "\"stringKey\":\"hello world!\","+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"intStrKey\":\"43\","+ + "\"longKey\":1234567890123456789,"+ + "\"longStrKey\":\"987654321098765432\","+ + "\"doubleKey\":-23.45e7,"+ + "\"doubleStrKey\":\"00001.000\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{\"myKey\":\"myVal\"}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); + assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey")); + assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey")); + assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey")); + assertTrue("doubleKey should be double", + jsonObject.getDouble("doubleKey") == -23.45e7); + assertTrue("doubleStrKey should be double", + jsonObject.getDouble("doubleStrKey") == 1); + assertTrue("intKey should be int", + jsonObject.getInt("intKey") == 42); + assertTrue("intStrKey should be int", + jsonObject.getInt("intStrKey") == 43); + assertTrue("longKey should be long", + jsonObject.getLong("longKey") == 1234567890123456789L); + assertTrue("longStrKey should be long", + jsonObject.getLong("longStrKey") == 987654321098765432L); + JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); + assertTrue("arrayKey should be JSONArray", + jsonArray.getInt(0) == 0 && + jsonArray.getInt(1) == 1 && + jsonArray.getInt(2) == 2); + JSONObject jsonObjectInner = jsonObject.getJSONObject("objectKey"); + assertTrue("objectKey should be JSONObject", + jsonObjectInner.get("myKey").equals("myVal")); + } + + @Test + public void jsonObjectNames() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "}"; + String [] expectedNames = {"trueKey", "falseKey", "stringKey"}; + JSONObject jsonObject = new JSONObject(str); + String [] names = JSONObject.getNames(jsonObject); + Util.compareActualVsExpectedStringArrays(names, expectedNames); + } + } diff --git a/Util.java b/Util.java index d494d13ba..71f6c0d75 100644 --- a/Util.java +++ b/Util.java @@ -70,4 +70,17 @@ private static void compareActualVsExpectedObjects(Object value, } } + public static void compareActualVsExpectedStringArrays(String[] names, + String [] expectedNames) { + assertTrue("Array lengths should be equal", + names.length == expectedNames.length); + List lNames = new ArrayList(Arrays.asList(names)); + for (int i = 0; i < expectedNames.length; ++i) { + String expectedName = expectedNames[i]; + assertTrue("expected to find "+expectedName, + lNames.contains(expectedName)); + lNames.remove(expectedName); + } + } + } From 2784c614d4bd6ee8c5810417c3de39d39ddda8da Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 10 Apr 2015 08:21:09 -0500 Subject: [PATCH 069/315] ip --- JSONObjectTest.java | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index aa434eadc..a46dc8b5d 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -182,7 +182,6 @@ public void jsonObjectValues() { "\"trueStrKey\":\"true\","+ "\"falseStrKey\":\"false\","+ "\"stringKey\":\"hello world!\","+ - "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ "\"intKey\":42,"+ "\"intStrKey\":\"43\","+ "\"longKey\":1234567890123456789,"+ @@ -233,4 +232,42 @@ public void jsonObjectNames() { Util.compareActualVsExpectedStringArrays(names, expectedNames); } + @Test + public void objectNames() { + MyBean myBean = new MyBean(); + String [] expectedNames = {"intKey", "doubleKey", "stringKey", + "complexStringKey", "trueKey", "falseKey"}; + String [] names = JSONObject.getNames(myBean); + Util.compareActualVsExpectedStringArrays(names, expectedNames); + } + + @Test + public void jsonObjectIncrement() { + String str = + "{"+ + "\"keyLong\":1L,"+ + "\"keyDouble\":1.1,"+ + "\"keyFloat\":1.1F,"+ + "}"; + String expectedStr = + "{"+ + "\"keyInt\":3,"+ + "\"keyLong\":3,"+ + "\"keyDouble\":3.1,"+ + "\"keyFloat\":3.1"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.increment("keyInt"); + jsonObject.increment("keyInt"); + jsonObject.increment("keyLong"); + jsonObject.increment("keyDouble"); + jsonObject.increment("keyFloat"); + jsonObject.increment("keyInt"); + jsonObject.increment("keyLong"); + jsonObject.increment("keyDouble"); + jsonObject.increment("keyFloat"); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + } From dcaf5fa23a43504a0ae2894dd4b3f43bcee48928 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 10 Apr 2015 19:42:34 -0500 Subject: [PATCH 070/315] ip --- JSONObjectTest.java | 70 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index a46dc8b5d..860bbcf7f 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -193,8 +193,10 @@ public void jsonObjectValues() { "}"; JSONObject jsonObject = new JSONObject(str); assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); + assertTrue("opt trueKey should be true", jsonObject.optBoolean("trueKey")); assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey")); assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey")); + assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey")); assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey")); assertTrue("doubleKey should be double", jsonObject.getDouble("doubleKey") == -23.45e7); @@ -208,6 +210,12 @@ public void jsonObjectValues() { jsonObject.getLong("longKey") == 1234567890123456789L); assertTrue("longStrKey should be long", jsonObject.getLong("longStrKey") == 987654321098765432L); + assertTrue("xKey should not exist", + jsonObject.isNull("xKey")); + assertTrue("stringKey should exist", + jsonObject.has("stringKey")); + assertTrue("stringKey should string", + jsonObject.getString("stringKey").equals("hello world!")); JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); assertTrue("arrayKey should be JSONArray", jsonArray.getInt(0) == 0 && @@ -232,6 +240,32 @@ public void jsonObjectNames() { Util.compareActualVsExpectedStringArrays(names, expectedNames); } + @Test + public void jsonObjectNamesToJsonAray() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "}"; + String [] expectedNames = {"trueKey", "falseKey", "stringKey" }; + + JSONObject jsonObject = new JSONObject(str); + JSONArray jsonArray = jsonObject.names(); + /** + * Cannot really compare to an expected JSONArray because the ordering + * of the JSONObject keys is not fixed, and JSONArray comparisons + * presume fixed. Since this test is limited to key strings, a + * string comparison will have to suffice. + */ + String namesStr = jsonArray.toString(); + // remove square brackets, commas, and spaces + namesStr = namesStr.replaceAll("[\\]|\\[|\"]", ""); + String [] names = namesStr.split(","); + + Util.compareActualVsExpectedStringArrays(names, expectedNames); + } + @Test public void objectNames() { MyBean myBean = new MyBean(); @@ -245,29 +279,53 @@ public void objectNames() { public void jsonObjectIncrement() { String str = "{"+ - "\"keyLong\":1L,"+ + "\"keyLong\":9999999991,"+ "\"keyDouble\":1.1,"+ - "\"keyFloat\":1.1F,"+ "}"; String expectedStr = "{"+ "\"keyInt\":3,"+ - "\"keyLong\":3,"+ + "\"keyLong\":9999999993,"+ "\"keyDouble\":3.1,"+ - "\"keyFloat\":3.1"+ "}"; JSONObject jsonObject = new JSONObject(str); jsonObject.increment("keyInt"); jsonObject.increment("keyInt"); jsonObject.increment("keyLong"); jsonObject.increment("keyDouble"); - jsonObject.increment("keyFloat"); jsonObject.increment("keyInt"); jsonObject.increment("keyLong"); jsonObject.increment("keyDouble"); - jsonObject.increment("keyFloat"); JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + @Test + public void jsonObjectNamesToArray() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "}"; + String [] expectedNames = {"trueKey", "falseKey", "stringKey"}; + JSONObject jsonObject = new JSONObject(str); + String [] names = JSONObject.getNames(jsonObject); + Util.compareActualVsExpectedStringArrays(names, expectedNames); + } + + @Test + public void jsonObjectNumberToString() { + String str; + Double dVal; + Integer iVal = 1; + str = JSONObject.numberToString(iVal); + assertTrue("expected "+iVal+" actual "+str, iVal.toString().equals(str)); + dVal = 12.34; + str = JSONObject.numberToString(dVal); + assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + dVal = 12.34e27; + str = JSONObject.numberToString(dVal); + assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + } } From c4d9a9c5f9f8c88b1f0a4b123c7f2bbf81cd0ca9 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 12 Apr 2015 19:29:32 -0500 Subject: [PATCH 071/315] 90.8% coverage --- JSONObjectTest.java | 554 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 552 insertions(+), 2 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 860bbcf7f..db4e153d0 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -2,11 +2,19 @@ import static org.junit.Assert.*; +import java.io.*; import java.util.*; import org.json.*; import org.junit.*; +class MyJsonString implements JSONString { + + @Override + public String toJSONString() { + return "my string"; + } +} public class JSONObjectTest { @@ -198,36 +206,154 @@ public void jsonObjectValues() { assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey")); assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey")); assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey")); + assertTrue("stringKey should be string", + jsonObject.getString("stringKey").equals("hello world!")); assertTrue("doubleKey should be double", jsonObject.getDouble("doubleKey") == -23.45e7); assertTrue("doubleStrKey should be double", jsonObject.getDouble("doubleStrKey") == 1); + 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("intKey should be int", + jsonObject.optInt("intKey") == 42); + assertTrue("opt intKey should be int", + jsonObject.optInt("intKey", 0) == 42); + assertTrue("opt intKey with default should be int", jsonObject.getInt("intKey") == 42); assertTrue("intStrKey should be int", jsonObject.getInt("intStrKey") == 43); assertTrue("longKey should be long", jsonObject.getLong("longKey") == 1234567890123456789L); + assertTrue("opt longKey should be long", + jsonObject.optLong("longKey") == 1234567890123456789L); + assertTrue("opt longKey with default should be long", + jsonObject.optLong("longKey", 0) == 1234567890123456789L); assertTrue("longStrKey should be long", jsonObject.getLong("longStrKey") == 987654321098765432L); assertTrue("xKey should not exist", jsonObject.isNull("xKey")); assertTrue("stringKey should exist", jsonObject.has("stringKey")); - assertTrue("stringKey should string", - jsonObject.getString("stringKey").equals("hello world!")); + assertTrue("opt stringKey should string", + jsonObject.optString("stringKey").equals("hello world!")); + assertTrue("opt stringKey with default should string", + jsonObject.optString("stringKey", "not found").equals("hello world!")); JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); assertTrue("arrayKey should be JSONArray", jsonArray.getInt(0) == 0 && jsonArray.getInt(1) == 1 && jsonArray.getInt(2) == 2); + jsonArray = jsonObject.optJSONArray("arrayKey"); + assertTrue("opt arrayKey should be JSONArray", + jsonArray.getInt(0) == 0 && + jsonArray.getInt(1) == 1 && + jsonArray.getInt(2) == 2); JSONObject jsonObjectInner = jsonObject.getJSONObject("objectKey"); assertTrue("objectKey should be JSONObject", jsonObjectInner.get("myKey").equals("myVal")); } + @Test + public void jsonObjectNonAndWrongValues() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"trueStrKey\":\"true\","+ + "\"falseStrKey\":\"false\","+ + "\"stringKey\":\"hello world!\","+ + "\"intKey\":42,"+ + "\"intStrKey\":\"43\","+ + "\"longKey\":1234567890123456789,"+ + "\"longStrKey\":\"987654321098765432\","+ + "\"doubleKey\":-23.45e7,"+ + "\"doubleStrKey\":\"00001.000\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{\"myKey\":\"myVal\"}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + int tryCount = 0; + int exceptionCount = 0; + try { + ++tryCount; + jsonObject.getBoolean("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getBoolean("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getString("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getString("trueKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getDouble("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getDouble("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getInt("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getInt("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getLong("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getLong("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getJSONArray("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getJSONArray("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getJSONObject("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getJSONObject("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("all get calls should have failed", + exceptionCount == tryCount); + } + @Test public void jsonObjectNames() { + + // getNames() from null JSONObject + assertTrue("null names from null Object", + null == JSONObject.getNames((Object)null)); + + // getNames() from object with no fields + assertTrue("null names from Object with no fields", + null == JSONObject.getNames(new MyJsonString())); + + // getNames() from empty JSONObject + String emptyStr = "{}"; + JSONObject emptyJsonObject = new JSONObject(emptyStr); + assertTrue("empty JSONObject should have null names", + null == JSONObject.getNames(emptyJsonObject)); + + // getNames() from JSONObject String str = "{"+ "\"trueKey\":true,"+ @@ -327,5 +453,429 @@ public void jsonObjectNumberToString() { dVal = 12.34e27; str = JSONObject.numberToString(dVal); assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + // trailing .0 is truncated, so it doesn't quite match toString() + dVal = 5000000.0000000; + str = JSONObject.numberToString(dVal); + assertTrue("expected 5000000 actual "+str, str.equals("5000000")); + } + + @Test + public void jsonObjectPut() { + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + String expectedStrAfterRemoval = + "{"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(); + jsonObject.put("trueKey", true); + jsonObject.put("falseKey", false); + Integer [] intArray = { 0, 1, 2 }; + jsonObject.put("arrayKey", Arrays.asList(intArray)); + Map myMap = new HashMap(); + myMap.put("myKey1", "myVal1"); + myMap.put("myKey2", "myVal2"); + myMap.put("myKey3", "myVal3"); + myMap.put("myKey4", "myVal4"); + jsonObject.put("objectKey", myMap); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + assertTrue("equal jsonObjects should be similar", + jsonObject.similar(expectedJsonObject)); + + jsonObject.remove("trueKey"); + JSONObject expectedJsonObjectAfterRemoval = + new JSONObject(expectedStrAfterRemoval); + Util.compareActualVsExpectedJsonObjects(jsonObject, + expectedJsonObjectAfterRemoval); + assertTrue("unequal jsonObjects should not be similar", + !jsonObject.similar(expectedJsonObject)); + assertTrue("unequal Objects should not be similar", + !jsonObject.similar(new JSONArray())); + + String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}"; + String bCompareValueStr = "{\"a\":\"notAval\",\"b\":true}"; + JSONObject aCompareValueJsonObject = new JSONObject(aCompareValueStr); + JSONObject bCompareValueJsonObject = new JSONObject(bCompareValueStr); + assertTrue("different values should not be similar", + !aCompareValueJsonObject.similar(bCompareValueJsonObject)); + + String aCompareObjectStr = "{\"a\":\"aval\",\"b\":{}}"; + String bCompareObjectStr = "{\"a\":\"aval\",\"b\":true}"; + JSONObject aCompareObjectJsonObject = new JSONObject(aCompareObjectStr); + JSONObject bCompareObjectJsonObject = new JSONObject(bCompareObjectStr); + assertTrue("different nested JSONObjects should not be similar", + !aCompareObjectJsonObject.similar(bCompareObjectJsonObject)); + + String aCompareArrayStr = "{\"a\":\"aval\",\"b\":[]}"; + String bCompareArrayStr = "{\"a\":\"aval\",\"b\":true}"; + JSONObject aCompareArrayJsonObject = new JSONObject(aCompareArrayStr); + JSONObject bCompareArrayJsonObject = new JSONObject(bCompareArrayStr); + assertTrue("different nested JSONArrays should not be similar", + !aCompareArrayJsonObject.similar(bCompareArrayJsonObject)); + } + + @Test + public void jsonObjectToString() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + String toStr = jsonObject.toString(); + JSONObject expectedJsonObject = new JSONObject(toStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void valueToString() { + + assertTrue("null valueToString() incorrect", + "null".equals(JSONObject.valueToString(null))); + MyJsonString jsonString = new MyJsonString(); + assertTrue("jsonstring valueToString() incorrect", + "my string".equals(JSONObject.valueToString(jsonString))); + assertTrue("boolean valueToString() incorrect", + "true".equals(JSONObject.valueToString(Boolean.TRUE))); + assertTrue("non-numeric double", + "null".equals(JSONObject.doubleToString(Double.POSITIVE_INFINITY))); + String jsonObjectStr = + "{"+ + "\"key1\":\"val1\","+ + "\"key2\":\"val2\","+ + "\"key3\":\"val3\""+ + "}"; + JSONObject jsonObject = new JSONObject(jsonObjectStr); + assertTrue("jsonObject valueToString() incorrect", + JSONObject.valueToString(jsonObject).equals(jsonObject.toString())); + String jsonArrayStr = + "[1,2,3]"; + JSONArray jsonArray = new JSONArray(jsonArrayStr); + assertTrue("jsonArra valueToString() incorrect", + JSONObject.valueToString(jsonArray).equals(jsonArray.toString())); + Map map = new HashMap(); + map.put("key1", "val1"); + map.put("key2", "val2"); + map.put("key3", "val3"); + assertTrue("map valueToString() incorrect", + jsonObject.toString().equals(JSONObject.valueToString(map))); + Collection collection = new ArrayList(); + collection.add(new Integer(1)); + collection.add(new Integer(2)); + collection.add(new Integer(3)); + assertTrue("collection valueToString() expected: "+ + jsonArray.toString()+ " actual: "+ + JSONObject.valueToString(collection), + jsonArray.toString().equals(JSONObject.valueToString(collection))); + Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + assertTrue("array valueToString() incorrect", + jsonArray.toString().equals(JSONObject.valueToString(array))); + } + + @Test + public void wrapObject() { + // wrap(null) returns NULL + assertTrue("null wrap() incorrect", + JSONObject.NULL == JSONObject.wrap(null)); + + // wrap(Integer) returns Integer + Integer in = new Integer(1); + assertTrue("Integer wrap() incorrect", + in == JSONObject.wrap(in)); + + // wrap JSONObject returns JSONObject + String jsonObjectStr = + "{"+ + "\"key1\":\"val1\","+ + "\"key2\":\"val2\","+ + "\"key3\":\"val3\""+ + "}"; + JSONObject jsonObject = new JSONObject(jsonObjectStr); + assertTrue("JSONObject wrap() incorrect", + jsonObject == JSONObject.wrap(jsonObject)); + + // wrap collection returns JSONArray + Collection collection = new ArrayList(); + collection.add(new Integer(1)); + collection.add(new Integer(2)); + collection.add(new Integer(3)); + JSONArray jsonArray = (JSONArray)(JSONObject.wrap(collection)); + String expectedCollectionJsonArrayStr = + "[1,2,3]"; + JSONArray expectedCollectionJsonArray = + new JSONArray(expectedCollectionJsonArrayStr); + Util.compareActualVsExpectedJsonArrays(jsonArray, + expectedCollectionJsonArray); + + // wrap Array returns JSONArray + Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); + JSONArray expectedIntegerArrayJsonArray = new JSONArray("[1,2,3]"); + Util.compareActualVsExpectedJsonArrays(integerArrayJsonArray, + expectedIntegerArrayJsonArray); + + // wrap map returns JSONObject + Map map = new HashMap(); + map.put("key1", "val1"); + map.put("key2", "val2"); + map.put("key3", "val3"); + JSONObject mapJsonObject = (JSONObject)(JSONObject.wrap(map)); + Util.compareActualVsExpectedJsonObjects(jsonObject, mapJsonObject); + + // TODO test wrap(package) + } + + @Test + public void jsonObjectEquals() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + String diffStr = + "{"+ + "\"arrayKey\":[0]"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + JSONObject otherJsonObject = new JSONObject(str); + JSONObject unequalJsonObject = new JSONObject(diffStr); + assertTrue("equal JSONObjects should be equal", + jsonObject.equals(otherJsonObject)); + assertTrue("unequal JSONObjects should not be equal", + !jsonObject.equals(unequalJsonObject)); + + // make sure hashcode returns some interesting value + assertTrue("equal JSONObjects should have equal hashCode", + jsonObject.hashCode() == otherJsonObject.hashCode()); + assertTrue("unequal JSONObjects should have unequal hashCode", + jsonObject.hashCode() != unequalJsonObject.hashCode()); + } + + @Test + public void jsonObjectParsingErrors() { + int tryCount = 0; + int exceptionCount = 0; + try { + // does not start with '{' + ++tryCount; + String str = "abc"; + new JSONObject(str); + } catch (JSONException ignore) {++exceptionCount; } + try { + // does not end with '}' + ++tryCount; + String str = "{"; + new JSONObject(str); + } catch (JSONException ignore) {++exceptionCount; } + try { + // key with no ':' + ++tryCount; + String str = "{\"myKey\" = true}"; + new JSONObject(str); + } catch (JSONException ignore) {++exceptionCount; } + try { + // entries with no ',' separator + ++tryCount; + String str = "{\"myKey\":true \"myOtherKey\":false}"; + new JSONObject(str); + } catch (JSONException ignore) {++exceptionCount; } + try { + // append to wrong key + ++tryCount; + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.append("myKey", "hello"); + } catch (JSONException ignore) {++exceptionCount; } + try { + // increment wrong key + ++tryCount; + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.increment("myKey"); + } catch (JSONException ignore) {++exceptionCount; } + try { + // invalid key + ++tryCount; + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.get(null); + } catch (JSONException ignore) {++exceptionCount; } + try { + // invalid numberToString() + ++tryCount; + JSONObject.numberToString((Number)null); + } catch (JSONException ignore) {++exceptionCount; } + try { + // null put key + ++tryCount; + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0); + } catch (NullPointerException ignore) {++exceptionCount; } + try { + // multiple putOnce key + ++tryCount; + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.putOnce("hello", "world"); + jsonObject.putOnce("hello", "world!"); + } catch (JSONException ignore) {++exceptionCount; } + try { + // test validity of invalid double + ++tryCount; + JSONObject.testValidity(Double.NaN); + } catch (JSONException ignore) {++exceptionCount; } + try { + // test validity of invalid float + ++tryCount; + JSONObject.testValidity(Float.NEGATIVE_INFINITY); + } catch (JSONException ignore) {++exceptionCount; } + + assertTrue("all tries should have failed", + exceptionCount == tryCount); + } + + @Test + public void jsonObjectOptDefault() { + + String str = "{\"myKey\": \"myval\"}"; + JSONObject jsonObject = new JSONObject(str); + + assertTrue("optBoolean() should return default boolean", + Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optLong() should return default long", + 42 == jsonObject.optLong("myKey", 42)); + assertTrue("optDouble() should return default double", + 42.3 == jsonObject.optDouble("myKey", 42.3)); + assertTrue("optString() should return default string", + "hi".equals(jsonObject.optString("hiKey", "hi"))); + } + + @Test + public void jsonObjectputNull() { + + // put null should remove the item. + String str = "{\"myKey\": \"myval\"}"; + JSONObject jsonObjectRemove = new JSONObject(str); + JSONObject jsonObjectPutNull = new JSONObject(str); + jsonObjectRemove.remove("myKey"); + jsonObjectPutNull.put("myKey", (Object)null); + Util.compareActualVsExpectedJsonObjects(jsonObjectRemove, jsonObjectPutNull); + assertTrue("jsonObject should be empty", + jsonObjectRemove.length() == 0 && + jsonObjectPutNull.length() == 0); + } + + @Test + public void jsonObjectQuote() { + String str; + str = ""; + String quotedStr; + quotedStr = JSONObject.quote(str); + assertTrue("quote() expected escaped quotes, found "+quotedStr, + "\"\"".equals(quotedStr)); + str = "\"\""; + quotedStr = JSONObject.quote(str); + assertTrue("quote() expected escaped quotes, found "+quotedStr, + "\"\\\"\\\"\"".equals(quotedStr)); + str = " Date: Sun, 12 Apr 2015 19:35:34 -0500 Subject: [PATCH 072/315] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index da0c4ee13..195f080d1 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,11 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | CookieList.java |96.5% | Completed | | HTTP.java | 98.7%| Completed | | HTTPTokener.java |93.2% |Completed | -| JSONArray.java |18.3% | | +| JSONArray.java |42.3% | In progress | | JSONException.java | 26.7% | | -| JSONML.java | 83.2%| completed | -| JSONObject | 24.9% | | in progress | -| JSONObject.Null | 75.0% | | | +| JSONML.java | 83.2%| Completed | +| JSONObject | 90.8% | Completed | +| JSONObject.Null | 87.5% | | | | JSONStringer.java | 93.8%| Completed | | JSONTokener.java | 72.1% | | | JSONWriter.java | 88.9% | Completed | From a85786952201351a0522892f2c02b49b142e345f Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 12 Apr 2015 19:36:01 -0500 Subject: [PATCH 073/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 195f080d1..53f11a53e 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 58.4% | | | +| Total coverage | 81.6% | | | | | | | | CDL.java | 94.8% | Completed | | Cookie.java | 97.5% | Completed | From 6a2c974581fca0d889fc5b9799ea7d9e7c2395c5 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 12 Apr 2015 23:34:23 -0500 Subject: [PATCH 074/315] 90.9% coverage --- JSONObjectTest.java | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index db4e153d0..824a6744d 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -650,39 +650,6 @@ public void wrapObject() { // TODO test wrap(package) } - @Test - public void jsonObjectEquals() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{"+ - "\"myKey1\":\"myVal1\","+ - "\"myKey2\":\"myVal2\","+ - "\"myKey3\":\"myVal3\","+ - "\"myKey4\":\"myVal4\""+ - "}"+ - "}"; - String diffStr = - "{"+ - "\"arrayKey\":[0]"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - JSONObject otherJsonObject = new JSONObject(str); - JSONObject unequalJsonObject = new JSONObject(diffStr); - assertTrue("equal JSONObjects should be equal", - jsonObject.equals(otherJsonObject)); - assertTrue("unequal JSONObjects should not be equal", - !jsonObject.equals(unequalJsonObject)); - - // make sure hashcode returns some interesting value - assertTrue("equal JSONObjects should have equal hashCode", - jsonObject.hashCode() == otherJsonObject.hashCode()); - assertTrue("unequal JSONObjects should have unequal hashCode", - jsonObject.hashCode() != unequalJsonObject.hashCode()); - } - @Test public void jsonObjectParsingErrors() { int tryCount = 0; @@ -869,13 +836,8 @@ public void write() { public void equals() { String str = "{\"key\":\"value\"}"; JSONObject aJsonObject = new JSONObject(str); - JSONObject bJsonObject = new JSONObject(str); assertTrue("Same JSONObject should be equal to itself", aJsonObject.equals(aJsonObject)); - assertTrue("JSONObjects with equal content should be equal", - aJsonObject.equals(bJsonObject)); - assertTrue("JSONObjects should not be equal to non J", - !aJsonObject.equals(new String())); } } From 76c30539ccb0583f81548a837b6ca6ab8e7120fd Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 13 Apr 2015 21:39:26 -0500 Subject: [PATCH 075/315] 95.9% coverage --- JSONArrayTest.java | 455 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 455 insertions(+) create mode 100644 JSONArrayTest.java diff --git a/JSONArrayTest.java b/JSONArrayTest.java new file mode 100644 index 000000000..2799a043b --- /dev/null +++ b/JSONArrayTest.java @@ -0,0 +1,455 @@ +package org.json.junit; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.json.*; +import org.junit.Test; + + +/** + * Tests for JSON-Java JSONArray.java + */ +public class JSONArrayTest { + String arrayStr = + "["+ + "true,"+ + "false,"+ + "\"true\","+ + "\"false\","+ + "\"hello\","+ + "23.45e-4,"+ + "\"23.45\","+ + "42,"+ + "\"43\","+ + "["+ + "\"world\""+ + "],"+ + "{"+ + "\"key1\":\"value1\","+ + "\"key2\":\"value2\","+ + "\"key3\":\"value3\","+ + "\"key4\":\"value4\""+ + "},"+ + "0,"+ + "\"-1\""+ + "]"; + + @Test(expected=NullPointerException.class) + public void nullException() { + String str = null; + new JSONArray(str); + } + + @Test(expected=JSONException.class) + public void emptStr() { + String str = ""; + new JSONArray(str); + } + + @Test(expected=JSONException.class) + public void badObject() { + String str = "abc"; + new JSONArray((Object)str); + } + + @Test + public void getArrayValues() { + JSONArray jsonArray = new JSONArray(arrayStr); + assertTrue("Array true", + true == jsonArray.getBoolean(0)); + assertTrue("Array false", + false == jsonArray.getBoolean(1)); + assertTrue("Array string true", + true == jsonArray.getBoolean(2)); + assertTrue("Array string false", + false == jsonArray.getBoolean(3)); + + assertTrue("Array double", + new Double(23.45e-4).equals(jsonArray.getDouble(5))); + assertTrue("Array string double", + new Double(23.45).equals(jsonArray.getDouble(6))); + + assertTrue("Array value int", + new Integer(42).equals(jsonArray.getInt(7))); + assertTrue("Array value string int", + new Integer(43).equals(jsonArray.getInt(8))); + + JSONArray nestedJsonArray = jsonArray.getJSONArray(9); + assertTrue("Array value JSONArray", nestedJsonArray != null); + + JSONObject nestedJsonObject = jsonArray.getJSONObject(10); + assertTrue("Array value JSONObject", nestedJsonObject != null); + + assertTrue("Array value long", + new Long(0).equals(jsonArray.getLong(11))); + assertTrue("Array value string long", + new Long(-1).equals(jsonArray.getLong(12))); + + assertTrue("Array value string", + "hello".equals(jsonArray.getString(4))); + + assertTrue("Array value null", jsonArray.isNull(-1)); + } + + @Test + public void failedGetArrayValues() { + int tryCount = 0; + int exceptionCount = 0; + JSONArray jsonArray = new JSONArray(arrayStr); + try { + tryCount++; + jsonArray.getBoolean(4); + assertTrue("expected getBoolean to fail", false); + } catch (JSONException ignored) { exceptionCount++; } + try { + tryCount++; + jsonArray.get(-1); + assertTrue("expected get to fail", false); + } catch (JSONException ignored) { exceptionCount++; } + try { + tryCount++; + jsonArray.getDouble(4); + assertTrue("expected getDouble to fail", false); + } catch (JSONException ignored) { exceptionCount++; } + try { + tryCount++; + jsonArray.getInt(4); + assertTrue("expected getInt to fail", false); + } catch (JSONException ignored) { exceptionCount++; } + try { + tryCount++; + jsonArray.getJSONArray(4); + assertTrue("expected getJSONArray to fail", false); + } catch (JSONException ignored) { exceptionCount++; } + try { + tryCount++; + jsonArray.getJSONObject(4); + assertTrue("expected getJSONObject to fail", false); + } catch (JSONException ignored) { exceptionCount++; } + try { + tryCount++; + jsonArray.getLong(4); + assertTrue("expected getLong to fail", false); + } catch (JSONException ignored) { exceptionCount++; } + try { + tryCount++; + jsonArray.getString(5); + assertTrue("expected getString to fail", false); + } catch (JSONException ignored) { exceptionCount++; } + assertTrue("tryCount should match exceptionCount", + tryCount == exceptionCount); + } + + @Test + public void join() { + String expectedStr = + "["+ + "true,"+ + "false,"+ + "\"true\","+ + "\"false\","+ + "\"hello\","+ + "0.002345,"+ + "\"23.45\","+ + "42,"+ + "\"43\","+ + "["+ + "\"world\""+ + "],"+ + "{"+ + "\"key1\":\"value1\","+ + "\"key2\":\"value2\","+ + "\"key3\":\"value3\","+ + "\"key4\":\"value4\""+ + "},"+ + "0,"+ + "\"-1\""+ + "]"; + + JSONArray jsonArray = new JSONArray(arrayStr); + String joinStr = jsonArray.join(","); + JSONArray finalJsonArray = new JSONArray("["+joinStr+"]"); + JSONArray expectedJsonArray = new JSONArray(expectedStr); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); + } + + @Test + public void length() { + assertTrue("expected empty JSONArray length 0", + new JSONArray().length() == 0); + JSONArray jsonArray = new JSONArray(arrayStr); + assertTrue("expected JSONArray length 13", jsonArray.length() == 13); + JSONArray nestedJsonArray = jsonArray.getJSONArray(9); + assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1); + } + + @Test + public void opt() { + JSONArray jsonArray = new JSONArray(arrayStr); + assertTrue("Array opt value true", + Boolean.TRUE == jsonArray.opt(0)); + assertTrue("Array opt value out of range", + null == jsonArray.opt(-1)); + + assertTrue("Array opt boolean", + Boolean.TRUE == jsonArray.optBoolean(0)); + assertTrue("Array opt boolean default", + Boolean.FALSE == jsonArray.optBoolean(-1, Boolean.FALSE)); + assertTrue("Array opt boolean implicit default", + Boolean.FALSE == jsonArray.optBoolean(-1)); + + assertTrue("Array opt double", + new Double(23.45e-4).equals(jsonArray.optDouble(5))); + assertTrue("Array opt double default", + new Double(1).equals(jsonArray.optDouble(0, 1))); + assertTrue("Array opt double default implicit", + new Double(jsonArray.optDouble(99)).isNaN()); + + assertTrue("Array opt int", + new Integer(42).equals(jsonArray.optInt(7))); + assertTrue("Array opt int default", + new Integer(-1).equals(jsonArray.optInt(0, -1))); + assertTrue("Array opt int default implicit", + 0 == jsonArray.optInt(0)); + + JSONArray nestedJsonArray = jsonArray.optJSONArray(9); + assertTrue("Array opt JSONArray", nestedJsonArray != null); + assertTrue("Array opt JSONArray default", + null == jsonArray.optJSONArray(99)); + + JSONObject nestedJsonObject = jsonArray.optJSONObject(10); + assertTrue("Array opt JSONObject", nestedJsonObject != null); + assertTrue("Array opt JSONObject default", + null == jsonArray.optJSONObject(99)); + + assertTrue("Array opt long", + 0 == jsonArray.optLong(11)); + assertTrue("Array opt long default", + -2 == jsonArray.optLong(-1, -2)); + assertTrue("Array opt long default implicit", + 0 == jsonArray.optLong(-1)); + + assertTrue("Array opt string", + "hello".equals(jsonArray.optString(4))); + assertTrue("Array opt string default implicit", + "".equals(jsonArray.optString(-1))); + } + + @Test + public void put() { + String expectedStr = + "["+ + "true,"+ + "false,"+ + "["+ + "hello,"+ + "world"+ + "],"+ + "2.5,"+ + "1,"+ + "45,"+ + "\"objectPut\","+ + "{"+ + "\"key10\":\"val10\","+ + "\"key20\":\"val20\","+ + "\"key30\":\"val30\""+ + "},"+ + "{"+ + "\"k1\":\"v1\""+ + "},"+ + "["+ + "1,"+ + "2"+ + "]"+ + "]"; + JSONArray jsonArray = new JSONArray(); + JSONArray expectedJsonArray = new JSONArray(expectedStr); + + // index 0 + jsonArray.put(true); + // 1 + jsonArray.put(false); + + String jsonArrayStr = + "["+ + "hello,"+ + "world"+ + "]"; + // 2 + jsonArray.put(new JSONArray(jsonArrayStr)); + + // 3 + jsonArray.put(2.5); + // 4 + jsonArray.put(1); + // 5 + jsonArray.put(45L); + + // 6 + jsonArray.put("objectPut"); + + String jsonObjectStr = + "{"+ + "\"key10\":\"val10\","+ + "\"key20\":\"val20\","+ + "\"key30\":\"val30\""+ + "}"; + JSONObject jsonObject = new JSONObject(jsonObjectStr); + // 7 + jsonArray.put(jsonObject); + + Map map = new HashMap(); + map.put("k1", "v1"); + // 8 + jsonArray.put(map); + + Collection collection = new ArrayList(); + collection.add(1); + collection.add(2); + // 9 + jsonArray.put(collection); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + } + + @Test + public void putIndex() { + String expectedStr = + "["+ + "true,"+ + "false,"+ + "["+ + "hello,"+ + "world"+ + "],"+ + "2.5,"+ + "1,"+ + "45,"+ + "\"objectPut\","+ + "null,"+ + "{"+ + "\"key10\":\"val10\","+ + "\"key20\":\"val20\","+ + "\"key30\":\"val30\""+ + "},"+ + "["+ + "1,"+ + "2"+ + "],"+ + "{"+ + "\"k1\":\"v1\""+ + "},"+ + "]"; + JSONArray jsonArray = new JSONArray(); + JSONArray expectedJsonArray = new JSONArray(expectedStr); + + // 1 + jsonArray.put(1, false); + // index 0 + jsonArray.put(0, true); + + String jsonArrayStr = + "["+ + "hello,"+ + "world"+ + "]"; + // 2 + jsonArray.put(2, new JSONArray(jsonArrayStr)); + + // 5 + jsonArray.put(5, 45L); + // 4 + jsonArray.put(4, 1); + // 3 + jsonArray.put(3, 2.5); + + // 6 + jsonArray.put(6, "objectPut"); + + // 7 will be null + + String jsonObjectStr = + "{"+ + "\"key10\":\"val10\","+ + "\"key20\":\"val20\","+ + "\"key30\":\"val30\""+ + "}"; + JSONObject jsonObject = new JSONObject(jsonObjectStr); + jsonArray.put(8, jsonObject); + Collection collection = new ArrayList(); + collection.add(1); + collection.add(2); + jsonArray.put(9,collection); + + Map map = new HashMap(); + map.put("k1", "v1"); + jsonArray.put(10, map); + try { + jsonArray.put(-1, "abc"); + assertTrue("put index < 0 should have thrown exception", false); + } catch(Exception ignored) {} + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + } + + @Test + public void remove() { + String arrayStr = + "["+ + "1"+ + "]"; + JSONArray jsonArray = new JSONArray(arrayStr); + JSONArray expectedJsonArray = new JSONArray(); + jsonArray.remove(0); + assertTrue("array should be empty", null == jsonArray.remove(5)); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + } + + @Test + public void notSimilar() { + String arrayStr = + "["+ + "1"+ + "]"; + JSONArray jsonArray = new JSONArray(arrayStr); + JSONArray otherJsonArray = new JSONArray(); + assertTrue("arrays lengths differ", !jsonArray.similar(otherJsonArray)); + + JSONObject jsonObject = new JSONObject("{\"k1\":\"v1\"}"); + JSONObject otherJsonObject = new JSONObject(); + jsonArray = new JSONArray(); + jsonArray.put(jsonObject); + otherJsonArray = new JSONArray(); + otherJsonArray.put(otherJsonObject); + assertTrue("arrays JSONObjects differ", !jsonArray.similar(otherJsonArray)); + + JSONArray nestedJsonArray = new JSONArray("[1, 2]"); + JSONArray otherNestedJsonArray = new JSONArray(); + jsonArray = new JSONArray(); + jsonArray.put(nestedJsonArray); + otherJsonArray = new JSONArray(); + otherJsonArray.put(otherNestedJsonArray); + assertTrue("arrays nested JSONArrays differ", + !jsonArray.similar(otherJsonArray)); + + jsonArray = new JSONArray(); + jsonArray.put("hello"); + otherJsonArray = new JSONArray(); + otherJsonArray.put("world"); + assertTrue("arrays values differ", + !jsonArray.similar(otherJsonArray)); + } + + @Test + public void toJSONObject() { + JSONArray names = new JSONArray(); + JSONArray jsonArray = new JSONArray(); + assertTrue("toJSONObject should return null", + null == jsonArray.toJSONObject(names)); + + } +} From 51e8a2d1e57cf5626b83403813f1c7e3cc028a46 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 13 Apr 2015 21:45:31 -0500 Subject: [PATCH 076/315] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 53f11a53e..d6439c8e7 100644 --- a/README.md +++ b/README.md @@ -24,17 +24,17 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 81.6% | | | +| Total coverage | 88.7% | | | | | | | | CDL.java | 94.8% | Completed | | Cookie.java | 97.5% | Completed | | CookieList.java |96.5% | Completed | | HTTP.java | 98.7%| Completed | | HTTPTokener.java |93.2% |Completed | -| JSONArray.java |42.3% | In progress | +| JSONArray.java |95.9% | In progress | | JSONException.java | 26.7% | | | JSONML.java | 83.2%| Completed | -| JSONObject | 90.8% | Completed | +| JSONObject | 90.9% | Completed | | JSONObject.Null | 87.5% | | | | JSONStringer.java | 93.8%| Completed | | JSONTokener.java | 72.1% | | From 9bffd5b3ca341b9dc0d97a71960f1fe854351ee6 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 13 Apr 2015 22:25:13 -0500 Subject: [PATCH 077/315] Update README.md --- README.md | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index d6439c8e7..a8f37a08e 100644 --- a/README.md +++ b/README.md @@ -26,21 +26,29 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | ------------- | ------------- | ---- | | Total coverage | 88.7% | | | | | | | -| CDL.java | 94.8% | Completed | -| Cookie.java | 97.5% | Completed | -| CookieList.java |96.5% | Completed | -| HTTP.java | 98.7%| Completed | -| HTTPTokener.java |93.2% |Completed | -| JSONArray.java |95.9% | In progress | -| JSONException.java | 26.7% | | -| JSONML.java | 83.2%| Completed | -| JSONObject | 90.9% | Completed | -| JSONObject.Null | 87.5% | | | -| JSONStringer.java | 93.8%| Completed | -| JSONTokener.java | 72.1% | | -| JSONWriter.java | 88.9% | Completed | -| Property.java | 94.8% | Completed | -| XML.java | 85.1% | Completed | -| XMLTokener.java| 82.7%| Completed | +| CDLTest.java | 94.8% | Completed | +| CookieTest.java | 97.5% | Completed | +| CookieListTest.java |96.5% | Completed | +| HTTPTest.java | 98.7%| Completed | +| HTTPTokene.java |93.2% |(no test file) | +| JSONArrayTest.java |95.9% | In progress | +| JSONException.java | 26.7% | (no test file) | +| JSONMLTest.java | 83.2%| Completed | +| JSONObjectTest | 90.9% | Completed | +| JSONObject.Null | 87.5% | (no test file) | +| JSONString.java | | (no lines to test) | +| JSONStringerTest.java | 93.8%| Completed | +| JSONTokener.java | 72.1% | (no test file) | +| JSONWriter.java | 88.9% | (no test file) | +| PropertyTest.java | 94.8% | Completed | +| XMLTest.java | 85.1% | Completed | +| XMLTokener.java| 82.7%| (no test file) | + +|| Files used in test || +| JunitTestSuite.java | +| MyBean.java | +| StringsResourceBundle.java | +|TestRUnner.java | +| Util.java | From 67fbfa12eaf7569e9157b731d7d717fd889b67c6 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 13 Apr 2015 22:27:48 -0500 Subject: [PATCH 078/315] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a8f37a08e..768288bfa 100644 --- a/README.md +++ b/README.md @@ -31,14 +31,14 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | CookieListTest.java |96.5% | Completed | | HTTPTest.java | 98.7%| Completed | | HTTPTokene.java |93.2% |(no test file) | -| JSONArrayTest.java |95.9% | In progress | +| JSONArrayTest.java |95.9% | Completed | | JSONException.java | 26.7% | (no test file) | | JSONMLTest.java | 83.2%| Completed | | JSONObjectTest | 90.9% | Completed | | JSONObject.Null | 87.5% | (no test file) | | JSONString.java | | (no lines to test) | | JSONStringerTest.java | 93.8%| Completed | -| JSONTokener.java | 72.1% | (no test file) | +| JSONTokenerTest.java | 72.1% | In progress | | JSONWriter.java | 88.9% | (no test file) | | PropertyTest.java | 94.8% | Completed | | XMLTest.java | 85.1% | Completed | From 98cdaf6a3963cb272d413a7b98c3f3eba9a691e2 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 13 Apr 2015 22:28:57 -0500 Subject: [PATCH 079/315] Update README.md --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 768288bfa..f5b12f4e0 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,12 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | XMLTest.java | 85.1% | Completed | | XMLTokener.java| 82.7%| (no test file) | -|| Files used in test || -| JunitTestSuite.java | -| MyBean.java | -| StringsResourceBundle.java | -|TestRUnner.java | -| Util.java | +|| Files used in test || || +| ------------- | | +| JunitTestSuite.java | | +| MyBean.java | | +| StringsResourceBundle.java | | +|TestRUnner.java | | +| Util.java | | From e5c01e4ff8ab95d0592e0526b8293b46aff3f1c5 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 13 Apr 2015 22:29:27 -0500 Subject: [PATCH 080/315] Update README.md --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f5b12f4e0..588481ee4 100644 --- a/README.md +++ b/README.md @@ -44,12 +44,12 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | XMLTest.java | 85.1% | Completed | | XMLTokener.java| 82.7%| (no test file) | -|| Files used in test || || -| ------------- | | -| JunitTestSuite.java | | -| MyBean.java | | -| StringsResourceBundle.java | | -|TestRUnner.java | | -| Util.java | | +| Files used in test | +| ------------- | +| JunitTestSuite.java | +| MyBean.java | +| StringsResourceBundle.java | +|TestRUnner.java | +| Util.java | From d75a96ae59c723fbac857443b750b32cb8856006 Mon Sep 17 00:00:00 2001 From: stleary Date: Tue, 14 Apr 2015 01:12:08 -0500 Subject: [PATCH 081/315] 95.9% coverage --- JSONArrayTest.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/JSONArrayTest.java b/JSONArrayTest.java index 2799a043b..78b194eb7 100644 --- a/JSONArrayTest.java +++ b/JSONArrayTest.java @@ -450,6 +450,18 @@ public void toJSONObject() { JSONArray jsonArray = new JSONArray(); assertTrue("toJSONObject should return null", null == jsonArray.toJSONObject(names)); - + } + + @Test + public void objectArrayVsIsArray() { + String expectedStr = + "["+ + "1,2,3,4,5,6,7"+ + "]"; + int[] myInts = { 1, 2, 3, 4, 5, 6, 7 }; + Object myObject = myInts; + JSONArray jsonArray = new JSONArray(myObject); + JSONArray expectedJsonArray = new JSONArray(expectedStr); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } } From 0409c9bfb225a11c7d1ddc75dcc9cb9f5d58d68b Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 14 Apr 2015 10:59:19 -0500 Subject: [PATCH 082/315] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 588481ee4..848a02f65 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # JSON-Java-unit-test -Unit tests to validate the JSON-Java GitHub project code (https://github.com/douglascrockford/JSON-java).
+Unit tests to validate the JSON-Java GitHub project code
+https://github.com/douglascrockford/JSON-java
+ +*These tests are a work in progress. Help improving the tests is welcome* +More coverage is needed, but more important is to improve the quality of the tests. Test harness: http://junit.org
Coverage: http://www.eclemma.org/
From 9e78cfc48df7058e1497160ea290fe17c9ccdada Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 14 Apr 2015 11:00:42 -0500 Subject: [PATCH 083/315] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 848a02f65..8bfa5e821 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ Unit tests to validate the JSON-Java GitHub project code
https://github.com/douglascrockford/JSON-java
-*These tests are a work in progress. Help improving the tests is welcome* -More coverage is needed, but more important is to improve the quality of the tests. +*These tests are a work in progress. Help from interested developers is welcome.*
+More coverage is needed, but more important is to improve the quality of the tests.
Test harness: http://junit.org
Coverage: http://www.eclemma.org/
From d9e04ec5b64a15af0b133056aca4a3e67df8ac2e Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 14 Apr 2015 11:01:29 -0500 Subject: [PATCH 084/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8bfa5e821..bd17a5f4a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Unit tests to validate the JSON-Java GitHub project code
https://github.com/douglascrockford/JSON-java
*These tests are a work in progress. Help from interested developers is welcome.*
-More coverage is needed, but more important is to improve the quality of the tests.
+More coverage is needed, but more importantly, improvements to test quality is needed.
Test harness: http://junit.org
Coverage: http://www.eclemma.org/
From 9df929963f83c209a36a64c776a679371855cc67 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 19 Apr 2015 17:10:47 -0500 Subject: [PATCH 085/315] added a trivial XML string compare method - still needs work --- Util.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Util.java b/Util.java index 71f6c0d75..53ec402a5 100644 --- a/Util.java +++ b/Util.java @@ -83,4 +83,13 @@ public static void compareActualVsExpectedStringArrays(String[] names, } } + public static void compareXML(String aXmlStr, String bXmlStr) { + // TODO For simple tests this may be adequate, but it won't work for + // elements with multiple attributes and possibly other cases as well. + // Should use XMLUnit or similar. + assertTrue("expected equal XML strings \naXmlStr: "+ + aXmlStr+ "\nbXmlStr: " +bXmlStr, aXmlStr.equals(bXmlStr)); + + } + } From f0d175c5b2005b9061287f843af03c7d24e2000c Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 19 Apr 2015 17:11:05 -0500 Subject: [PATCH 086/315] Added JSONArrayTest --- JunitTestSuite.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/JunitTestSuite.java b/JunitTestSuite.java index 63bde2fda..a91426da3 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -12,7 +12,8 @@ JSONMLTest.class, HTTPTest.class, JSONStringerTest.class, - JSONObjectTest.class + JSONObjectTest.class, + JSONArrayTest.class }) public class JunitTestSuite { } \ No newline at end of file From 35a4fefd2ebef1f14b1b251bf5d5332ba9ddc8ca Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 19 Apr 2015 17:11:51 -0500 Subject: [PATCH 087/315] test JSONML.toString(JSONArray) --- JSONMLTest.java | 406 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 301 insertions(+), 105 deletions(-) diff --git a/JSONMLTest.java b/JSONMLTest.java index b2a4e0633..fa982e137 100644 --- a/JSONMLTest.java +++ b/JSONMLTest.java @@ -6,36 +6,63 @@ import org.junit.Test; /** - * Tests for JSON-Java JSONML.java + * Tests for org.json.JSONML.java + * + * Certain inputs are expected to result in exceptions. These tests are + * executed first. JSONML provides an API to: + * Convert an XML string into a JSONArray or a JSONObject. + * Convert a JSONArray or JSONObject into an XML string. + * Both fromstring and tostring operations operations should be symmetrical + * within the limits of JSONML. + * It should be possible to perform the following operations, which should + * result in the original string being recovered, within the limits of the + * underlying classes: + * Convert a string -> JSONArray -> string -> JSONObject -> string + * Convert a string -> JSONObject -> string -> JSONArray -> string + * */ public class JSONMLTest { @Test(expected=NullPointerException.class) public void nullXMLException() { - + /** + * Attempts to transform a null XML string to JSON + */ String xmlStr = null; JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) public void emptyXMLException() { - + /** + * Attempts to transform an empty XML string to JSON + */ String xmlStr = ""; JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) public void nonXMLException() { + /** + * Attempts to transform a nonXML string to JSON + */ String xmlStr = "{ \"this is\": \"not xml\"}"; JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) public void emptyTagException() { + /** + * jsonArrayStr is used to build a JSONArray which is then + * turned into XML. For this transformation, all arrays represent + * elements and the first array entry is the name of the element. + * In this case, one of the arrays does not have a name + */ String jsonArrayStr = "[\"addresses\","+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ + // this array has no name "["+ "[\"name\"],"+ "[\"nocontent\"],"+ @@ -48,10 +75,18 @@ public void emptyTagException() { @Test(expected=JSONException.class) public void spaceInTagException() { + /** + * jsonArrayStr is used to build a JSONArray which is then + * turned into XML. For this transformation, all arrays represent + * elements and the first array entry is the name of the element. + * In this case, one of the element names has an embedded space, + * which is not allowed. + */ String jsonArrayStr = "[\"addresses\","+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ + // this array has an invalid name "[\"addr esses\","+ "[\"name\"],"+ "[\"nocontent\"],"+ @@ -63,7 +98,12 @@ public void spaceInTagException() { } @Test(expected=JSONException.class) - public void unvalidSlashInTagException() { + public void invalidSlashInTagException() { + /** + * xmlStr contains XML text which is transformed into a JSONArray. + * In this case, the XML is invalid because the 'name' element + * contains an invalid frontslash. + */ String xmlStr = "\n"+ "\n"+ " \n"+ " abc street\n"+ - " \n"+ + " \n"+ ""; JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) public void invalidBangInTagException() { + /** + * xmlStr contains XML text which is transformed into a JSONArray. + * In this case, the XML is invalid because an element + * has the invalid name '!'. + */ String xmlStr = "\n"+ "\n"+ " \n"+ " \n"+ - " \n"+ + " \n"+ ""; JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) public void invalidBangNoCloseInTagException() { + /** + * xmlStr contains XML text which is transformed into a JSONArray. + * In this case, the XML is invalid because an element + * starts with '!' and has no closing tag + */ String xmlStr = "\n"+ "\n"+ " \n"+ " \n"+ + " \n"+ ""; JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) public void noCloseStartTagException() { + /** + * xmlStr contains XML text which is transformed into a JSONArray. + * In this case, the XML is invalid because an element + * has no closing '>'. + */ String xmlStr = "\n"+ "\n"+ " \n"+ " \n"+ + " \n"+ + ""; + JSONML.toJSONArray(xmlStr); + } + + @Test(expected=JSONException.class) + public void noCloseEndTagException() { + /** + * xmlStr contains XML text which is transformed into a JSONArray. + * In this case, the XML is invalid because an element + * has no name after the closing tag '\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + " \n"+ + ""; + JSONML.toJSONArray(xmlStr); + } + + @Test(expected=JSONException.class) + public void noCloseEndBraceException() { + /** + * xmlStr contains XML text which is transformed into a JSONArray. + * In this case, the XML is invalid because an element + * has '>' after the closing tag '\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + " "; JSONML.toJSONArray(xmlStr); } @Test(expected=JSONException.class) public void invalidCDATABangInTagException() { + /** + * xmlStr contains XML text which is transformed into a JSONArray. + * In this case, the XML is invalid because an element + * does not have a complete CDATA string. + */ String xmlStr = "\n"+ "\n"+ "\n"+ - "
\n"+ - "\n"+ + "
\n"+ + "myName\n"+ ">\n"+ "
\n"+ ""; @@ -160,7 +276,8 @@ public void complexTypeXML() { "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ "[\"address\","+ - "[\"name\"],"+ + "{\"attr1\":\"attrValue1\",\"attr2\":\"attrValue2\",\"attr3\":\"attrValue3\"},"+ + "[\"name\", {\"nameType\":\"mine\"},\"myName\"],"+ "[\"nocontent\"],"+ "\">\""+ "]"+ @@ -174,15 +291,34 @@ public void complexTypeXML() { } @Test - public void basicXMLAsObject() { + public void toJSONObjectToJSONArray() { + /** + * xmlStr contains XML text which is transformed into a JSONObject, + * restored to XML, transformed into a JSONArray, and then restored + * to XML again. Both JSONObject and JSONArray should contain the same + * information and should produce the same XML, allowing for non-ordered + * attributes. + * + * Transformation to JSONObject: + * The elementName is stored as a string where key="tagName" + * Attributes are simply stored as key/value pairs + * If the element has either content or child elements, they are stored + * in a jsonArray with key="childNodes". + * + * Transformation to JSONArray: + * 1st entry = elementname + * 2nd entry = attributes object (if present) + * 3rd entry = content (if present) + * 4th entry = child element JSONArrays (if present) + */ String xmlStr = "\n"+ "\n"+ - "
\n"+ - "Joe Tester\n"+ - "[CDATA[Baker street 5]\n"+ - "\n"+ + "
\n"+ + "Joe Tester\n"+ + "\n"+ + "\n"+ "true\n"+ "false\n"+ "null\n"+ @@ -193,6 +329,7 @@ public void basicXMLAsObject() { "\n"+ "1\n"+ "2\n"+ + "abc\n"+ "3\n"+ "4.1\n"+ "5.2\n"+ @@ -200,90 +337,121 @@ public void basicXMLAsObject() { "
\n"+ ""; - String expectedStr = - "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + String expectedJSONObjectStr = + "{"+ + "\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "\"childNodes\":["+ - "{\"childNodes\":["+ - "{\"childNodes\":[\"Joe Tester\"],"+ - "\"tagName\":\"name\"},"+ - "{\"childNodes\":[\"[CDATA[Baker street 5]\"],"+ - "\"tagName\":\"street\"},"+ - "{\"tagName\":\"NothingHere\"},"+ - "{\"childNodes\":[true],"+ - "\"tagName\":\"TrueValue\"},"+ - "{\"childNodes\":[false],"+ - "\"tagName\":\"FalseValue\"},"+ - "{\"childNodes\":[null],"+ - "\"tagName\":\"NullValue\"},"+ - "{\"childNodes\":[42],"+ - "\"tagName\":\"PositiveValue\"},"+ - "{\"childNodes\":[-23],"+ - "\"tagName\":\"NegativeValue\"},"+ - "{\"childNodes\":[-23.45],"+ - "\"tagName\":\"DoubleValue\"},"+ - "{\"childNodes\":[\"-23x.45\"],"+ - "\"tagName\":\"Nan\"},"+ - "{\"childNodes\":["+ - "{\"childNodes\":[1],"+ - "\"tagName\":\"value\"},"+ - "{\"childNodes\":[2],"+ - "\"tagName\":\"value\"},"+ - "{\"childNodes\":[3],"+ - "\"tagName\":\"value\"},"+ - "{\"childNodes\":[4.1],"+ - "\"tagName\":\"value\"},"+ - "{\"childNodes\":[5.2],"+ - "\"tagName\":\"value\"}"+ - "],"+ - "\"tagName\":\"ArrayOfNum\"}"+ - "],"+ - "\"tagName\":\"address\"}"+ + "{"+ + "\"childNodes\":["+ + "{"+ + "\"childNodes\":[\"Joe Tester\"],"+ + "\"nameType\":\"my name\","+ + "\"tagName\":\"name\""+ + "},"+ + "{"+ + "\"childNodes\":[\"Baker street 5\"],"+ + "\"tagName\":\"street\""+ + "},"+ + "{"+ + "\"tagName\":\"NothingHere\","+ + "\"except\":\"an attribute\""+ + "},"+ + "{"+ + "\"childNodes\":[true],"+ + "\"tagName\":\"TrueValue\""+ + "},"+ + "{"+ + "\"childNodes\":[false],"+ + "\"tagName\":\"FalseValue\""+ + "},"+ + "{"+ + "\"childNodes\":[null],"+ + "\"tagName\":\"NullValue\""+ + "},"+ + "{"+ + "\"childNodes\":[42],"+ + "\"tagName\":\"PositiveValue\""+ + "},"+ + "{"+ + "\"childNodes\":[-23],"+ + "\"tagName\":\"NegativeValue\""+ + "},"+ + "{"+ + "\"childNodes\":[-23.45],"+ + "\"tagName\":\"DoubleValue\""+ + "},"+ + "{"+ + "\"childNodes\":[\"-23x.45\"],"+ + "\"tagName\":\"Nan\""+ + "},"+ + "{"+ + "\"childNodes\":["+ + "{"+ + "\"childNodes\":[1],"+ + "\"tagName\":\"value\""+ + "},"+ + "{"+ + "\"childNodes\":[2],"+ + "\"tagName\":\"value\""+ + "},"+ + "{"+ + "\"childNodes\":["+ + "{"+ + "\"childNodes\":[\"abc\"],"+ + "\"svAttr\":\"svValue\","+ + "\"tagName\":\"subValue\""+ + "}"+ + "],"+ + "\"tagName\":\"value\""+ + "},"+ + "{"+ + "\"childNodes\":[3],"+ + "\"tagName\":\"value\""+ + "},"+ + "{"+ + "\"childNodes\":[4.1],"+ + "\"tagName\":\"value\""+ + "},"+ + "{"+ + "\"childNodes\":[5.2],"+ + "\"tagName\":\"value\""+ + "}"+ + "],"+ + "\"tagName\":\"ArrayOfNum\""+ + "}"+ + "],"+ + "\"addrType\":\"my address\","+ + "\"tagName\":\"address\""+ + "}"+ "],"+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ - "\"tagName\":\"addresses\"}"; - JSONObject jsonObject = JSONML.toJSONObject(xmlStr); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - String xmlToStr = JSONML.toString(jsonObject); - JSONObject finalJsonObject = JSONML.toJSONObject(xmlToStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); - Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); - } + "\"tagName\":\"addresses\""+ + "}"; - @Test - public void basicXMLAsArray() { - String xmlStr = - "\n"+ - "\n"+ - "
\n"+ - "Joe Tester\n"+ - "[CDATA[Baker street 5]\n"+ - "\n"+ - "true\n"+ - "false\n"+ - "null\n"+ - "42\n"+ - "-23\n"+ - "-23.45\n"+ - "-23x.45\n"+ - "\n"+ - "1\n"+ - "2\n"+ - "3\n"+ - "4.1\n"+ - "5.2\n"+ - "\n"+ - "
\n"+ - "
"; - - String expectedStr = - "[\"addresses\","+ - "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ - "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ - "[\"address\","+ - "[\"name\",\"Joe Tester\"],"+ - "[\"street\",\"[CDATA[Baker street 5]\"],"+ - "[\"NothingHere\"],"+ + String expectedJSONArrayStr = + "["+ + "\"addresses\","+ + "{"+ + "\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\""+ + "},"+ + "["+ + "\"address\","+ + "{"+ + "\"addrType\":\"my address\""+ + "},"+ + "["+ + "\"name\","+ + "{"+ + "\"nameType\":\"my name\""+ + "},"+ + "\"Joe Tester\""+ + "],"+ + "[\"street\",\"Baker street 5\"],"+ + "["+ + "\"NothingHere\","+ + "{\"except\":\"an attribute\"}"+ + "],"+ "[\"TrueValue\",true],"+ "[\"FalseValue\",false],"+ "[\"NullValue\",null],"+ @@ -291,25 +459,53 @@ public void basicXMLAsArray() { "[\"NegativeValue\",-23],"+ "[\"DoubleValue\",-23.45],"+ "[\"Nan\",\"-23x.45\"],"+ - "[\"ArrayOfNum\","+ + "["+ + "\"ArrayOfNum\","+ "[\"value\",1],"+ "[\"value\",2],"+ + "[\"value\","+ + "["+ + "\"subValue\","+ + "{\"svAttr\":\"svValue\"},"+ + "\"abc\""+ + "],"+ + "],"+ "[\"value\",3],"+ "[\"value\",4.1],"+ "[\"value\",5.2]"+ "]"+ "]"+ "]"; + + // make a JSONObject and make sure it looks as expected + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + JSONObject expectedJsonObject = new JSONObject(expectedJSONObjectStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + + // restore the XML, then make another JSONObject and make sure it + // looks as expected + String jsonObjectXmlToStr = JSONML.toString(jsonObject); + JSONObject finalJsonObject = JSONML.toJSONObject(jsonObjectXmlToStr); + Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); + + // create a JSON array from the original string and make sure it + // looks as expected JSONArray jsonArray = JSONML.toJSONArray(xmlStr); - JSONArray expectedJsonArray = new JSONArray(expectedStr); - String xmlToStr = JSONML.toString(jsonArray); - JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr); - Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); - // TODO: this test fails because JSONML.toString() does not emit values - // for true, false, null, and numbers - // Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); + JSONArray expectedJsonArray = new JSONArray(expectedJSONArrayStr); + Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray); + + // restore the XML, then make another JSONArray and make sure it + // looks as expected + String jsonArrayXmlToStr = JSONML.toString(jsonArray); + JSONArray finalJsonArray = JSONML.toJSONArray(jsonArrayXmlToStr); + Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); + + // lastly, confirm the restored JSONObject XML and JSONArray XML look + // reasonably similar + Util.compareXML(jsonObjectXmlToStr, jsonArrayXmlToStr); } + @Test public void commentsInXML() { From 5acbee2719293d6ae3bd75bbb8f53d6f2180ee68 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 23 Apr 2015 21:41:46 -0500 Subject: [PATCH 088/315] 98% coverage --- CDLTest.java | 155 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 103 insertions(+), 52 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index 53284d0c6..d218b10e4 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -12,11 +12,7 @@ * Tests for CDL.java. * CDL provides an application level API, but it is not used by the * reference app. To test it, strings will be converted to JSON-Java classes - * and then converted back. Since each row is an unordered JSONObject, - * can't use a simple string compare to check for equality. - * @author JSON.org - * @version 2015-03-22 - * + * and then converted back. */ public class CDLTest { @@ -25,79 +21,130 @@ public class CDLTest { * and all subsequent rows are values. All keys and values should be legal. */ String lines = new String( - "Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7\n" + + "Col 1, Col 2, \tCol 3, Col 4, Col 5, Col 6, Col 7\n" + "val1, val2, val3, val4, val5, val6, val7\n" + - "1, 2, 3, 4, 5, 6, 7\n" + + "1, 2, 3, 4\t, 5, 6, 7\n" + "true, false, true, true, false, false, false\n" + "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" + "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n" ); /** - * Something I did not expect is that CDL.toJSONArray() adds all values as - * strings, with no filtering or conversions. I suppose this makes it - * easier to emit it as CDL later. For testing, it means that the - * expected JSONObject values all must be quoted in the cases where the - * JSONObject parsing might normally convert the value into a non-string. + * CDL.toJSONArray() adds all values asstrings, with no filtering or + * conversions. For testing, this means that the expected JSONObject + * values all must be quoted in the cases where the JSONObject parsing + * might normally convert the value into a non-string. */ String expectedLines = new String( "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, "+ - "{Col 1:1, Col 2:2, Col 3:3, Col 4:4, Col 5:5, Col 6:6, Col 7:7}, "+ - "{Col 1:true, Col 2:false, Col 3:true, Col 4:true, Col 5:false, Col 6:false, Col 7:false}, "+ + "{Col 1:\"1\", Col 2:\"2\", Col 3:\"3\", Col 4:\"4\", Col 5:\"5\", Col 6:\"6\", Col 7:\"7\"}, "+ + "{Col 1:\"true\", Col 2:\"false\", Col 3:\"true\", Col 4:\"true\", Col 5:\"false\", Col 6:\"false\", Col 7:\"false\"}, "+ "{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, "+ "{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va\'l6, Col 7:val7}]"); @Test(expected=NullPointerException.class) - public void shouldThrowExceptionOnNullString() { + public void exceptionOnNullString() { + /** + * Attempts to create a JSONArray from a null string + */ String nullStr = null; CDL.toJSONArray(nullStr); } - @Test - /** - * Note: This test reveals a bug in the method JavaDoc. It should - * mention it might return null, or it should return an empty JSONArray. - */ - public void shouldHandleOnlyColumnNames() { - String columnNameStr = "col1, col2, col3"; - JSONArray jsonArray = CDL.toJSONArray(columnNameStr); - assertTrue("CDL should return null when only 1 row is given", - jsonArray == null); - } - - @Test - /** - * Note: This test reveals a bug in the method JavaDoc. It should - * mention it might return null, or it should return an empty JSONArray. - */ - public void shouldHandleEmptyString() { - String emptyStr = ""; - JSONArray jsonArray = CDL.toJSONArray(emptyStr); - assertTrue("CDL should return null when the input string is empty", - jsonArray == null); - } - @Test(expected=JSONException.class) - public void shouldHandleUnbalancedQuoteInName() { + public void unbalancedQuoteInName() { + /** + * Attempts to create a JSONArray from a string with unbalanced quotes + * in column title line + */ String badLine = "Col1, \"Col2\nVal1, Val2"; CDL.toJSONArray(badLine); } @Test(expected=JSONException.class) - public void shouldHandleUnbalancedQuoteInValue() { + public void unbalancedQuoteInValue() { + /** + * Attempts to create a JSONArray from a string with unbalanced quotes + * in value line + */ String badLine = "Col1, Col2\n\"Val1, Val2"; CDL.toJSONArray(badLine); } @Test(expected=JSONException.class) - public void shouldHandleNullInName() { + public void nullInName() { + /** + * Attempts to create a JSONArray from a string with null char + * in column title line + */ String badLine = "C\0ol1, Col2\nVal1, Val2"; CDL.toJSONArray(badLine); } - + @Test(expected=NullPointerException.class) + public void nullJSONArrayToString() { + /** + * call toString with a null array + */ + CDL.toString((JSONArray)null); + } + @Test - public void toStringShouldCheckSpecialChars() { + public void emptyString() { + /** + * Create a JSONArray from an empty string + */ + String emptyStr = ""; + JSONArray jsonArray = CDL.toJSONArray(emptyStr); + assertTrue("CDL should return null when the input string is empty", + jsonArray == null); + } + + @Test + public void onlyColumnNames() { + /** + * Create a JSONArray with only 1 row + */ + String columnNameStr = "col1, col2, col3"; + JSONArray jsonArray = CDL.toJSONArray(columnNameStr); + assertTrue("CDL should return null when only 1 row is given", + jsonArray == null); + } + + @Test + public void emptyLinesToJSONArray() { + /** + * Create a JSONArray from string containing only whitespace and commas + */ + String str = " , , , \n , , , "; + JSONArray jsonArray = CDL.toJSONArray(str); + assertTrue("JSONArray should be null for no content", + jsonArray == null); + } + + @Test + public void emptyJSONArrayToString() { + /** + * call toString with a null array + */ + JSONArray jsonArray = new JSONArray(); + String str = CDL.toString(jsonArray); + assertTrue("CDL should return null for toString(null)", + str == null); + } + + @Test + public void nullJSONArraysToString() { + /** + * call toString with a null arrays for names and values + */ + String str = CDL.toString(null, null); + assertTrue("CDL should return null for toString(null)", + str == null); + } + + @Test + public void checkSpecialChars() { /** * Given a JSONArray that was not built by CDL, some chars may be * found that would otherwise be filtered out by CDL. @@ -119,16 +166,21 @@ public void toStringShouldCheckSpecialChars() { } @Test - public void shouldConvertCDLToJSONArray() { - // this array is built by CDL + public void textToJSONArray() { + /** + * Create a JSONArray from a string of lines + */ JSONArray jsonArray = CDL.toJSONArray(lines); - // This array is built from JSON parsing JSONArray expectedJsonArray = new JSONArray(expectedLines); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } @Test - public void shouldCreateJSONArrayUsingJSONArray() { + public void jsonArrayToJSONArray() { + /** + * Create a JSONArray from a JSONArray of titles and a + * string of value lines + */ String nameArrayStr = "[Col1, Col2]"; String values = "V1, V2"; JSONArray nameJSONArray = new JSONArray(nameArrayStr); @@ -138,11 +190,10 @@ public void shouldCreateJSONArrayUsingJSONArray() { } @Test - public void shouldConvertCDLToJSONArrayAndBackToString() { + public void textToJSONArrayAndBackToString() { /** - * This is the first test of normal functionality. - * The string contains a typical variety of values - * that might be found in a real CDL. + * Create a JSONArray from a string of lines, + * then convert to string and then back to JSONArray */ JSONArray jsonArray = CDL.toJSONArray(lines); String jsonStr = CDL.toString(jsonArray); From 30c86811c037ffa9adec44ed6015d442752348c8 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 23 Apr 2015 21:42:04 -0500 Subject: [PATCH 089/315] improved object comparison --- Util.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Util.java b/Util.java index 71f6c0d75..635b7a625 100644 --- a/Util.java +++ b/Util.java @@ -64,6 +64,20 @@ private static void compareActualVsExpectedObjects(Object value, compareActualVsExpectedJsonArrays( jsonArray, expectedJsonArray); } else { + /** + * Certain helper classes (e.g. XML) may create Long instead of + * Integer for small int values. As long as both are Numbers, + * just compare the toString() values. + */ + if (!(value instanceof Number && expectedValue instanceof Number)) { + assertTrue("object types should be equal for actual: "+ + value.toString()+" ("+ + value.getClass().toString()+") expected: "+ + expectedValue.toString()+" ("+ + expectedValue.getClass().toString()+")", + value.getClass().toString().equals( + expectedValue.getClass().toString())); + } assertTrue("string values should be equal for actual: "+ value.toString()+" expected: "+expectedValue.toString(), value.toString().equals(expectedValue.toString())); @@ -83,4 +97,13 @@ public static void compareActualVsExpectedStringArrays(String[] names, } } + public static void compareXML(String aXmlStr, String bXmlStr) { + // TODO For simple tests this may be adequate, but it won't work for + // elements with multiple attributes and possibly other cases as well. + // Should use XMLUnit or similar. + assertTrue("expected equal XML strings \naXmlStr: "+ + aXmlStr+ "\nbXmlStr: " +bXmlStr, aXmlStr.equals(bXmlStr)); + + } + } From 890fd4a39726bebb16d8d6f17a65b58c275f7b70 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Thu, 23 Apr 2015 21:46:13 -0500 Subject: [PATCH 090/315] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bd17a5f4a..76ac4c2b0 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,9 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 88.7% | | | +| Total coverage | 88.6% | | | | | | | -| CDLTest.java | 94.8% | Completed | +| CDLTest.java | 98% | Completed | | CookieTest.java | 97.5% | Completed | | CookieListTest.java |96.5% | Completed | | HTTPTest.java | 98.7%| Completed | From 9bca907208631de2e13d009a82aa6126d02d2e7a Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 23 Apr 2015 21:47:15 -0500 Subject: [PATCH 091/315] added JSONArrayTest --- JunitTestSuite.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/JunitTestSuite.java b/JunitTestSuite.java index 63bde2fda..a91426da3 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -12,7 +12,8 @@ JSONMLTest.class, HTTPTest.class, JSONStringerTest.class, - JSONObjectTest.class + JSONObjectTest.class, + JSONArrayTest.class }) public class JunitTestSuite { } \ No newline at end of file From 15b5306f48104874639965cd9bf7d93dc2719965 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Thu, 23 Apr 2015 22:01:33 -0500 Subject: [PATCH 092/315] Update README.md --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 76ac4c2b0..43bbfb1d8 100644 --- a/README.md +++ b/README.md @@ -30,22 +30,22 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | ------------- | ------------- | ---- | | Total coverage | 88.6% | | | | | | | -| CDLTest.java | 98% | Completed | -| CookieTest.java | 97.5% | Completed | -| CookieListTest.java |96.5% | Completed | -| HTTPTest.java | 98.7%| Completed | -| HTTPTokene.java |93.2% |(no test file) | -| JSONArrayTest.java |95.9% | Completed | +| CDLTest.java | 98% | basic coverage completed. Reasonable testing completed. | +| CookieTest.java | 97.5% | basic coverage completed | +| CookieListTest.java |96.5% | basic coverage completed | +| HTTPTest.java | 98.7%| basic coverage completed | +| HTTPTokener.java |93.2% |(no test file) | +| JSONArrayTest.java |95.9% | basic coverage completed | | JSONException.java | 26.7% | (no test file) | -| JSONMLTest.java | 83.2%| Completed | -| JSONObjectTest | 90.9% | Completed | +| JSONMLTest.java | 83.2%| working on basic coverage | +| JSONObjectTest | 90.9% | basic coverage completed | | JSONObject.Null | 87.5% | (no test file) | | JSONString.java | | (no lines to test) | -| JSONStringerTest.java | 93.8%| Completed | +| JSONStringerTest.java | 93.8%| basic coverage completed | | JSONTokenerTest.java | 72.1% | In progress | | JSONWriter.java | 88.9% | (no test file) | -| PropertyTest.java | 94.8% | Completed | -| XMLTest.java | 85.1% | Completed | +| PropertyTest.java | 94.8% | basic coverage completed | +| XMLTest.java | 85.1% | working on basic coverage | | XMLTokener.java| 82.7%| (no test file) | | Files used in test | @@ -53,7 +53,7 @@ A unit test is considered complete when the coverage is >= 90% as measured by Ec | JunitTestSuite.java | | MyBean.java | | StringsResourceBundle.java | -|TestRUnner.java | +|TestRunner.java | | Util.java | From 102d273be3d5a9b53974eccd0d0f5b68b6a18844 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 24 Apr 2015 00:25:53 -0500 Subject: [PATCH 093/315] Update README.md --- README.md | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 43bbfb1d8..2fde1d2a0 100644 --- a/README.md +++ b/README.md @@ -24,29 +24,34 @@ The fundamental issues with JSON-Java testing are: When you start working on a test, add the empty file to the repository and update the readme, so that others will know that test is taken. -A unit test is considered complete when the coverage is >= 90% as measured by EclEmma. +A unit test has the following stages: +|No test| +|In progress| +|Coverage > 90%| +|Reasonable test cases| +|Checked against previous unit tests| | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | | Total coverage | 88.6% | | | | | | | -| CDLTest.java | 98% | basic coverage completed. Reasonable testing completed. | -| CookieTest.java | 97.5% | basic coverage completed | -| CookieListTest.java |96.5% | basic coverage completed | -| HTTPTest.java | 98.7%| basic coverage completed | -| HTTPTokener.java |93.2% |(no test file) | -| JSONArrayTest.java |95.9% | basic coverage completed | -| JSONException.java | 26.7% | (no test file) | -| JSONMLTest.java | 83.2%| working on basic coverage | -| JSONObjectTest | 90.9% | basic coverage completed | -| JSONObject.Null | 87.5% | (no test file) | -| JSONString.java | | (no lines to test) | -| JSONStringerTest.java | 93.8%| basic coverage completed | +| CDLTest.java | 98% | Reasonable test cases. | +| CookieTest.java | 97.5% | Coverage > 90% | +| CookieListTest.java |96.5% | Coverage > 90% | +| HTTPTest.java | 98.7%| Coverage > 90% | +| HTTPTokener.java |93.2% | No test | +| JSONArrayTest.java |95.9% | Coverage > 90% | +| JSONException.java | 26.7% | No test | +| JSONMLTest.java | 83.2%| In progress | +| JSONObjectTest | 90.9% | Coverage > 90% | +| JSONObject.Null | 87.5% | No test | +| JSONString.java | | No test | +| JSONStringerTest.java | 93.8%| Coverage > 90% | | JSONTokenerTest.java | 72.1% | In progress | -| JSONWriter.java | 88.9% | (no test file) | -| PropertyTest.java | 94.8% | basic coverage completed | -| XMLTest.java | 85.1% | working on basic coverage | -| XMLTokener.java| 82.7%| (no test file) | +| JSONWriter.java | 88.9% | No test | +| PropertyTest.java | 94.8% | Coverage > 90% | +| XMLTest.java | 85.1% | In progress | +| XMLTokener.java| 82.7%| No test | | Files used in test | | ------------- | From 3112e320895c1c2da2fbd75b2a2484bf89aa593d Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 24 Apr 2015 00:26:26 -0500 Subject: [PATCH 094/315] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2fde1d2a0..9b934ac29 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ The fundamental issues with JSON-Java testing are: When you start working on a test, add the empty file to the repository and update the readme, so that others will know that test is taken. A unit test has the following stages: + |No test| |In progress| |Coverage > 90%| From 571b1a79bb8c0f69b78bac591a01ac2c2b347654 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 24 Apr 2015 00:27:34 -0500 Subject: [PATCH 095/315] Update README.md --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9b934ac29..2c01eb3e2 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,13 @@ When you start working on a test, add the empty file to the repository and updat A unit test has the following stages: -|No test| -|In progress| -|Coverage > 90%| -|Reasonable test cases| -|Checked against previous unit tests| +| Test phase | +|----| +| No test | +| In progress | +| Coverage > 90% | +| Reasonable test cases | +| Checked against previous unit tests | | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | From d2e19cc78ccfa657ba60c857956685fe4f8348c6 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 24 Apr 2015 16:40:42 -0500 Subject: [PATCH 096/315] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2c01eb3e2..b22295e4e 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ A unit test has the following stages: | Coverage > 90% | | Reasonable test cases | | Checked against previous unit tests | +| Completed | + | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | From da2e548f56087cb29a4d86777ab2dfb71a257b6a Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 26 Apr 2015 17:46:52 -0500 Subject: [PATCH 097/315] 98.9% coverage --- CookieTest.java | 161 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 118 insertions(+), 43 deletions(-) diff --git a/CookieTest.java b/CookieTest.java index d5bc239fb..c96bd7228 100644 --- a/CookieTest.java +++ b/CookieTest.java @@ -1,6 +1,6 @@ package org.json.junit; -import java.util.*; + import static org.junit.Assert.*; @@ -9,29 +9,52 @@ /** - * Tests for JSON-Java Cookie.java - * See RFC6265 - * At its most basic, a cookie is a name=value pair. + * HTTP cookie specification: RFC6265 + * + * At its most basic, a cookie is a name=value pair. The value may be subdivided + * into other cookies, but that is not tested here. The cookie may also include + * certain named attributes, delimited by semicolons. + * + * The Cookie.toString() method emits certain attributes if present: expires, + * domain, path, secure. All but secure are name-value pairs. Other attributes + * are not included in the toString() output. + * * A JSON-Java encoded cookie escapes '+', '%', '=', ';' with %hh values. */ public class CookieTest { @Test(expected=NullPointerException.class) public void nullCookieException() { + /** + * Attempts to create a JSONObject from a null string + */ String cookieStr = null; Cookie.toJSONObject(cookieStr); } @Test(expected=JSONException.class) - public void malFormedCookieException() { + public void malFormedNameValueException() { + /** + * Attempts to create a JSONObject from a malformed cookie string + */ String cookieStr = "thisCookieHasNoEqualsChar"; Cookie.toJSONObject(cookieStr); } + @Test(expected=JSONException.class) + public void malFormedAttributeException() { + /** + * Attempts to create a JSONObject from a malformed cookie string + */ + String cookieStr = "this=Cookie;myAttribute"; + Cookie.toJSONObject(cookieStr); + } + @Test(expected=JSONException.class) public void emptyStringCookieException() { /** - * Cookie throws an exception, but CookieList does not + * Attempts to create a JSONObject from an empty cookie string + * Note: Cookie throws an exception, but CookieList does not */ String cookieStr = ""; Cookie.toJSONObject(cookieStr); @@ -39,6 +62,9 @@ public void emptyStringCookieException() { @Test public void simpleCookie() { + /** + * The simplest cookie is a name/value pair with no delimiter + */ String cookieStr = "SID=31d4d96e407aad42"; String expectedCookieStr = "{\"name\":\"SID\",\"value\":\"31d4d96e407aad42\"}"; JSONObject jsonObject = Cookie.toJSONObject(cookieStr); @@ -48,6 +74,11 @@ public void simpleCookie() { @Test public void multiPartCookie() { + /** + * Store a cookie with all of the supported attributes in a + * JSONObject. The secure attribute, which has no value, is treated + * as a boolean. + */ String cookieStr = "PH=deleted; "+ " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ @@ -55,12 +86,14 @@ public void multiPartCookie() { " domain=.yahoo.com;"+ "secure"; String expectedCookieStr = - "{\"path\":\"/\","+ - "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ - "\"domain\":\".yahoo.com\","+ - "\"name\":\"PH\","+ - "\"secure\":true,"+ - "\"value\":\"deleted\"}"; + "{"+ + "\"name\":\"PH\","+ + "\"value\":\"deleted\","+ + "\"path\":\"/\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"secure\":true"+ + "}"; JSONObject jsonObject = Cookie.toJSONObject(cookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); @@ -68,51 +101,64 @@ public void multiPartCookie() { @Test public void convertCookieToString() { + /** + * ToString() will omit the non-standard "thiswont=beIncluded" + * attribute, but the attribute is still stored in the JSONObject. + * This test confirms both behaviors. + */ String cookieStr = - "PH=deleted; "+ - " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ - "path=/; "+ - " domain=.yahoo.com;"+ - "thisWont=beIncluded;"+ - "secure"; - String expectedCookieStr = - "{\"path\":\"/\","+ - "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ - "\"domain\":\".yahoo.com\","+ - "\"name\":\"PH\","+ - "\"secure\":true,"+ - "\"value\":\"deleted\"}"; - /** - * ToString() will omit the non-standard segment, - * but it will still be stored in the JSONObject - */ - String expectedDirectCompareCookieStr = - expectedCookieStr.replaceAll("\\{", "\\{\"thisWont\":\"beIncluded\","); + "PH=deleted; "+ + " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ + "path=/; "+ + " domain=.yahoo.com;"+ + "thisWont=beIncluded;"+ + "secure"; + String expectedCookieStr = + "{\"path\":\"/\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"name\":\"PH\","+ + "\"secure\":true,"+ + "\"value\":\"deleted\"}"; + // Add the nonstandard attribute to the expected cookie string + String expectedDirectCompareCookieStr = + expectedCookieStr.replaceAll("\\{", "\\{\"thisWont\":\"beIncluded\","); + // convert all strings into JSONObjects JSONObject jsonObject = Cookie.toJSONObject(cookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); JSONObject expectedDirectCompareJsonObject = new JSONObject(expectedDirectCompareCookieStr); + // emit the string String cookieToStr = Cookie.toString(jsonObject); + // create a final JSONObject from the string JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr); + // JSONObject should contain the nonstandard string Util.compareActualVsExpectedJsonObjects(jsonObject,expectedDirectCompareJsonObject); + // JSONObject -> string -> JSONObject should not contain the nonstandard string Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } @Test public void convertEncodedCookieToString() { + /** + * A string may be URL-encoded when converting to JSONObject. + * If found, '+' is converted to ' ', and %hh hex strings are converted + * to their ascii char equivalents. This test confirms the decoding + * behavior. + */ String cookieStr = - "PH=deleted; "+ - " expires=Wed,+19-Mar-2014+17:53:53+GMT;"+ - "path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+ - " domain=.yahoo.com;"+ - "secure"; - String expectedCookieStr = - "{\"path\":\"/+this/is&/a/spec;segment=\","+ - "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ - "\"domain\":\".yahoo.com\","+ - "\"name\":\"PH\","+ - "\"secure\":true,"+ - "\"value\":\"deleted\"}"; + "PH=deleted; "+ + " expires=Wed,+19-Mar-2014+17:53:53+GMT;"+ + "path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+ + " domain=.yahoo.com;"+ + "secure"; + String expectedCookieStr = + "{\"path\":\"/+this/is&/a/spec;segment=\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"name\":\"PH\","+ + "\"secure\":true,"+ + "\"value\":\"deleted\"}"; JSONObject jsonObject = Cookie.toJSONObject(cookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); String cookieToStr = Cookie.toString(jsonObject); @@ -121,4 +167,33 @@ public void convertEncodedCookieToString() { Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } + @Test + public void escapeString() { + /** + * A public API method performs a URL encoding for selected chars + * in a string. Control chars, '+', '%', '=', ';' are all encoded + * as %hh hex strings. The string is also trimmed. + * This test confirms that behavior. + */ + String str = " +%\r\n\t\b%=;;; "; + String expectedStr = "%2b%25%0d%0a%09%08%25%3d%3b%3b%3b"; + String actualStr = Cookie.escape(str); + assertTrue("expect escape() to encode correctly. Actual: " +actualStr+ + " expected: " +expectedStr, expectedStr.equals(actualStr)); + } + + @Test + public void unescapeString() { + /** + * A public API method performs URL decoding for strings. + * '+' is converted to space and %hh hex strings are converted to + * their ascii equivalent values. The string is not trimmed. + * This test confirms that behavior. + */ + String str = " +%2b%25%0d%0a%09%08%25%3d%3b%3b%3b+ "; + String expectedStr = " +%\r\n\t\b%=;;; "; + String actualStr = Cookie.unescape(str); + assertTrue("expect unescape() to decode correctly. Actual: " +actualStr+ + " expected: " +expectedStr, expectedStr.equals(actualStr)); + } } From ee06176ab2be22d2e09228d0a6ba5e534f7e726b Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 26 Apr 2015 17:49:04 -0500 Subject: [PATCH 098/315] Update README.md --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b22295e4e..73c3259f3 100644 --- a/README.md +++ b/README.md @@ -40,22 +40,22 @@ A unit test has the following stages: | ------------- | ------------- | ---- | | Total coverage | 88.6% | | | | | | | -| CDLTest.java | 98% | Reasonable test cases. | -| CookieTest.java | 97.5% | Coverage > 90% | -| CookieListTest.java |96.5% | Coverage > 90% | -| HTTPTest.java | 98.7%| Coverage > 90% | +| CDL.java | 98% | Reasonable test cases. | +| Cookie.java | 98.9% | Reasonable test cases. | +| CookieList.java |96.5% | Coverage > 90% | +| HTTP.java | 98.7%| Coverage > 90% | | HTTPTokener.java |93.2% | No test | -| JSONArrayTest.java |95.9% | Coverage > 90% | +| JSONArray.java |95.9% | Coverage > 90% | | JSONException.java | 26.7% | No test | -| JSONMLTest.java | 83.2%| In progress | -| JSONObjectTest | 90.9% | Coverage > 90% | +| JSONML.java | 83.2%| In progress | +| JSONObject | 90.9% | Coverage > 90% | | JSONObject.Null | 87.5% | No test | | JSONString.java | | No test | -| JSONStringerTest.java | 93.8%| Coverage > 90% | -| JSONTokenerTest.java | 72.1% | In progress | +| JSONStringer.java | 93.8%| Coverage > 90% | +| JSONTokener.java | 72.1% | In progress | | JSONWriter.java | 88.9% | No test | -| PropertyTest.java | 94.8% | Coverage > 90% | -| XMLTest.java | 85.1% | In progress | +| Property.java | 94.8% | Coverage > 90% | +| XML.java | 85.1% | In progress | | XMLTokener.java| 82.7%| No test | | Files used in test | From f115d6a3b8b797091584171ecab6eafaf3eeaf1c Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 26 Apr 2015 17:53:39 -0500 Subject: [PATCH 099/315] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 73c3259f3..49d912ade 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,14 @@ When you start working on a test, add the empty file to the repository and updat A unit test has the following stages: -| Test phase | -|----| -| No test | -| In progress | -| Coverage > 90% | -| Reasonable test cases | -| Checked against previous unit tests | -| Completed | +| Test phase |Description | +|----|----| +| No test | No test specifically for this class has been written, or the class contains no executable code. | +| In progress | Unit tests have been started for this class. | +| Coverage > 90% | Initial goal of 90% coverage has been reached. Test quality may be questionable | +| Reasonable test cases | 90% coverage. Functionality and behavior has been confirmed | +| Checked against previous unit tests | Historical unit tests have been checked in case something important was missed | +| Completed | The unit test is completed | | Test file name | Coverage | Comments | From 65ae3e663fcf9c2d7d93b492594f2eef0ab89f6b Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 27 Apr 2015 10:04:41 -0500 Subject: [PATCH 100/315] Improving test case quality --- CookieListTest.java | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/CookieListTest.java b/CookieListTest.java index d8b4a5b10..9edc9adc0 100644 --- a/CookieListTest.java +++ b/CookieListTest.java @@ -7,23 +7,37 @@ import org.json.*; import org.junit.Test; - /** - * Tests for JSON-Java CookieList.java - * The main differences between Cookie and CookieList appears to be that - * CookieList does not treat the initial name/value pair different than - * the other segments, and does not handle "secure". - * Therefore the tests will be similar, but not identical. + * HTTP cookie specification: RFC6265 + * + * A cookie list is a JSONObject whose members are cookie name/value pairs. + * Entries are unescaped while being added, and escaped in the toString() + * method. Unescaping means to convert %hh hex strings to the ascii equivalent + * and converting '+' to ' '. Escaping converts '+', '%', '=', ';', + * and ascii control chars to %hh hex strings. + * + * CookieList should not be considered as just a list of Cookie objects: + * - CookieList stores a cookie name/value pair as a single entry; Cookie stores + * it as 2 entries. + * - CookieList expects multiple name/value pairs as input; Cookie allows the + * 'secure' name with no associated value */ public class CookieListTest { + @Test(expected=NullPointerException.class) public void nullCookieListException() { + /** + * Attempts to create a CookieList from a null string + */ String cookieStr = null; CookieList.toJSONObject(cookieStr); } @Test(expected=JSONException.class) public void malFormedCookieListException() { + /** + * Attempts to create a CookieList from a malformed string + */ String cookieStr = "thisCookieHasNoEqualsChar"; CookieList.toJSONObject(cookieStr); } @@ -31,6 +45,7 @@ public void malFormedCookieListException() { @Test(expected=JSONException.class) public void emptyStringCookieList() { /** + * Creates a CookieList from an empty string. * Cookie throws an exception, but CookieList does not */ String cookieStr = ""; @@ -42,6 +57,9 @@ public void emptyStringCookieList() { @Test public void simpleCookieList() { + /** + * The simplest cookie is a name/value pair with no delimiter + */ String cookieStr = "SID=31d4d96e407aad42"; String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); From 912350ec7526c848f022cb4fb1d48e262fe3ad7f Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 27 Apr 2015 14:56:01 -0500 Subject: [PATCH 101/315] 96.5% coverage --- CookieListTest.java | 108 +++++++++++++++++++++++++++++--------------- 1 file changed, 72 insertions(+), 36 deletions(-) diff --git a/CookieListTest.java b/CookieListTest.java index 9edc9adc0..0eaef7162 100644 --- a/CookieListTest.java +++ b/CookieListTest.java @@ -1,7 +1,5 @@ package org.json.junit; -import java.util.*; - import static org.junit.Assert.*; import org.json.*; @@ -10,17 +8,19 @@ /** * HTTP cookie specification: RFC6265 * - * A cookie list is a JSONObject whose members are cookie name/value pairs. - * Entries are unescaped while being added, and escaped in the toString() - * method. Unescaping means to convert %hh hex strings to the ascii equivalent - * and converting '+' to ' '. Escaping converts '+', '%', '=', ';', - * and ascii control chars to %hh hex strings. + * A cookie list is a JSONObject whose members are presumed to be cookie + * name/value pairs. Entries are unescaped while being added, and escaped in + * the toString() output. + * Unescaping means to convert %hh hex strings to the ascii equivalent + * and converting '+' to ' '. + * Escaping converts '+', '%', '=', ';' and ascii control chars to %hh hex strings. * * CookieList should not be considered as just a list of Cookie objects: * - CookieList stores a cookie name/value pair as a single entry; Cookie stores - * it as 2 entries. - * - CookieList expects multiple name/value pairs as input; Cookie allows the + * it as 2 entries (key="name" and key="value"). + * - CookieList requires multiple name/value pairs as input; Cookie allows the * 'secure' name with no associated value + * - CookieList has no special handling for attribute name/value pairs. */ public class CookieListTest { @@ -67,37 +67,67 @@ public void simpleCookieList() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + @Test + public void simpleCookieListWithDelimiter() { + /** + * The simplest cookie is a name/value pair with a delimiter + */ + String cookieStr = "SID=31d4d96e407aad42;"; + String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}"; + JSONObject jsonObject = CookieList.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + @Test public void multiPartCookieList() { String cookieStr = - "PH=deleted; "+ - " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ - "path=/; "+ - " domain=.yahoo.com;"; + "name1=myCookieValue1; "+ + " name2=myCookieValue2;"+ + "name3=myCookieValue3;"+ + " name4=myCookieValue4; "+ + "name5=myCookieValue5;"+ + " name6=myCookieValue6;"; String expectedCookieStr = - "{\"path\":\"/\","+ - "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ - "\"domain\":\".yahoo.com\","+ - "\"PH\":\"deleted\"}"; + "{"+ + "\"name1\":\"myCookieValue1\","+ + "\"name2\":\"myCookieValue2\","+ + "\"name3\":\"myCookieValue3\","+ + "\"name4\":\"myCookieValue4\","+ + "\"name5\":\"myCookieValue5\","+ + "\"name6\":\"myCookieValue6\""+ + "}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + @Test + public void convertCookieListWithNullValueToString() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("key", JSONObject.NULL); + String cookieToStr = CookieList.toString(jsonObject); + assertTrue("toString() should be empty", "".equals(cookieToStr)); + } + @Test public void convertCookieListToString() { String cookieStr = - "PH=deleted; "+ - " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ - "path=/; "+ - " domain=.yahoo.com;"+ - "thisWont=beIncluded;"; + "name1=myCookieValue1; "+ + " name2=myCookieValue2;"+ + "name3=myCookieValue3;"+ + " name4=myCookieValue4; "+ + "name5=myCookieValue5;"+ + " name6=myCookieValue6;"; String expectedCookieStr = - "{\"path\":\"/\","+ - "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ - "\"domain\":\".yahoo.com\","+ - "\"thisWont\":\"beIncluded\","+ - "\"PH\":\"deleted\"}"; + "{"+ + "\"name1\":\"myCookieValue1\","+ + "\"name2\":\"myCookieValue2\","+ + "\"name3\":\"myCookieValue3\","+ + "\"name4\":\"myCookieValue4\","+ + "\"name5\":\"myCookieValue5\","+ + "\"name6\":\"myCookieValue6\""+ + "}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); String cookieToStr = CookieList.toString(jsonObject); @@ -106,18 +136,24 @@ public void convertCookieListToString() { Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } - @Test + @Test public void convertEncodedCookieListToString() { String cookieStr = - "PH=deleted; "+ - " expires=Wed,+19-Mar-2014+17:53:53+GMT;"+ - "path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+ - " domain=.yahoo.com;"; + "name1=myCookieValue1; "+ + " name2=my+Cookie+Value+2;"+ + "name3=my%2BCookie%26Value%3B3%3D;"+ + " name4=my%25CookieValue4; "+ + "name5=myCookieValue5;"+ + " name6=myCookieValue6;"; String expectedCookieStr = - "{\"path\":\"/+this/is&/a/spec;segment=\","+ - "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ - "\"domain\":\".yahoo.com\","+ - "\"PH\":\"deleted\"}"; + "{"+ + "\"name1\":\"myCookieValue1\","+ + "\"name2\":\"my Cookie Value 2\","+ + "\"name3\":\"my+Cookie&Value;3=\","+ + "\"name4\":\"my%CookieValue4\","+ + "\"name5\":\"myCookieValue5\","+ + "\"name6\":\"myCookieValue6\""+ + "}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); String cookieToStr = CookieList.toString(jsonObject); From 31614fe8264b0c0ef5825c0ecc44b2c9660cbe01 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 27 Apr 2015 15:01:27 -0500 Subject: [PATCH 102/315] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 49d912ade..f92e029cc 100644 --- a/README.md +++ b/README.md @@ -38,11 +38,11 @@ A unit test has the following stages: | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 88.6% | | | +| Total coverage | 88.9% | | | | | | | | CDL.java | 98% | Reasonable test cases. | | Cookie.java | 98.9% | Reasonable test cases. | -| CookieList.java |96.5% | Coverage > 90% | +| CookieList.java |96.5% | Reasonable test cases. | | HTTP.java | 98.7%| Coverage > 90% | | HTTPTokener.java |93.2% | No test | | JSONArray.java |95.9% | Coverage > 90% | From 6195bd248b50d8691f8b2e7c80a88464ad2f4b88 Mon Sep 17 00:00:00 2001 From: stleary Date: Tue, 5 May 2015 21:53:51 -0500 Subject: [PATCH 103/315] added unit tests for suppress warning coverage --- JSONObjectTest.java | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 824a6744d..86c6b1db5 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -552,6 +552,62 @@ public void jsonObjectToString() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + @Test + public void jsonObjectToStringSuppressWarningOnCastToMap() { + JSONObject jsonObject = new JSONObject(); + Map map = new HashMap(); + map.put("abc", "def"); + jsonObject.put("key", map); + String toStr = jsonObject.toString(); + JSONObject expectedJsonObject = new JSONObject(toStr); + assertTrue("keys should be equal", + jsonObject.keySet().iterator().next().equals( + expectedJsonObject.keySet().iterator().next())); + /** + * Can't do a Util compare because although they look the same + * in the debugger, one is a map and the other is a JSONObject. + */ + // TODO: fix warnings + map = (Map)jsonObject.get("key"); + JSONObject mapJsonObject = expectedJsonObject.getJSONObject("key"); + assertTrue("value size should be equal", + map.size() == mapJsonObject.length() && map.size() == 1); + assertTrue("keys should be equal for key: "+map.keySet().iterator().next(), + mapJsonObject.keys().next().equals(map.keySet().iterator().next())); + assertTrue("values should be equal for key: "+map.keySet().iterator().next(), + mapJsonObject.get(mapJsonObject.keys().next()).toString().equals( + map.get(map.keySet().iterator().next()))); + } + + @Test + public void jsonObjectToStringSuppressWarningOnCastToCollection() { + JSONObject jsonObject = new JSONObject(); + Collection collection = new ArrayList(); + collection.add("abc"); + // ArrayList will be added as an object + jsonObject.put("key", collection); + String toStr = jsonObject.toString(); + // [abc] will be added as a JSONArray + JSONObject expectedJsonObject = new JSONObject(toStr); + /** + * Can't do a Util compare because although they look the same + * in the debugger, one is a collection and the other is a JSONArray. + */ + assertTrue("keys should be equal", + jsonObject.keySet().iterator().next().equals( + expectedJsonObject.keySet().iterator().next())); + // TODO: fix warnings + collection = (Collection)jsonObject.get("key"); + JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); + assertTrue("value size should be equal", + collection.size() == jsonArray.length()); + Iterator it = collection.iterator(); + for (int i = 0; i < collection.size(); ++i) { + assertTrue("items should be equal for index: "+i, + jsonArray.get(i).toString().equals(it.next().toString())); + } + } + @Test public void valueToString() { From fbd07da05a71d6d8b710c3554a68ae6ebb2b0aa9 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Wed, 6 May 2015 14:37:52 -0500 Subject: [PATCH 104/315] Update README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f92e029cc..d91f7c26b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,13 @@ The fundamental issues with JSON-Java testing are: * JSONObjects are unordered, making simple string comparison ineffective. * Comparisons via **equals()** is not currently supported. Neither JSONArray nor JSONObject overrride hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful. * Access to the JSONArray and JSONObject internal containers for comparison is not currently available. -* JSONObject sometimes wraps entries in quotes, other times does not, complicating comparisons. + +General issues with unit testing are: +* Just writing tests to make coverage goals tends to result in poor tests. +* Unit tests are a form of documentation - how a given method actually works is demonstrated by the test. So for a code reviewer or future developer looking at code a good test helps explain how a function is supposed to work according to the original author. This can be difficult if you are not the original developer. +* It is difficult to evaluate unit tests in a vacuum. You also need to see the code being tested to understand if a test is good. +* Without unit tests it is hard to feel confident about the quality of the code, especially when fixing bugs or refactoring. Good tests prevents regressions and keeps the intent of the code correct. +* If you have unit test results along with pull requests, the reviewer has an easier time understanding your code and determining if the it works as intended. When you start working on a test, add the empty file to the repository and update the readme, so that others will know that test is taken. From 95b8cd5b0393922c64fd0ff30f3c1a93568f46cf Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 6 May 2015 22:16:08 -0500 Subject: [PATCH 105/315] test improvements, in progress --- JSONObjectTest.java | 1894 +++++++++++++++++++++++-------------------- 1 file changed, 995 insertions(+), 899 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 86c6b1db5..1aa19d798 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -1,899 +1,995 @@ -package org.json.junit; - -import static org.junit.Assert.*; - -import java.io.*; -import java.util.*; - -import org.json.*; -import org.junit.*; - -class MyJsonString implements JSONString { - - @Override - public String toJSONString() { - return "my string"; - } -} - -public class JSONObjectTest { - - - @Test - public void jsonObjectByNames() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"nullKey\":null,"+ - "\"stringKey\":\"hello world!\","+ - "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ - "\"intKey\":42,"+ - "\"doubleKey\":-23.45e67"+ - "}"; - String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"}; - String expectedStr = - "{"+ - "\"falseKey\":false,"+ - "\"nullKey\":null,"+ - "\"stringKey\":\"hello world!\","+ - "\"doubleKey\":-23.45e67"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - JSONObject copyJsonObject = new JSONObject(jsonObject, keys); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(copyJsonObject, expectedJsonObject); - } - - @Test - public void jsonObjectByMap() { - String expectedStr = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ - "\"intKey\":42,"+ - "\"doubleKey\":-23.45e67"+ - "}"; - Map jsonMap = new HashMap(); - jsonMap.put("trueKey", new Boolean(true)); - jsonMap.put("falseKey", new Boolean(false)); - jsonMap.put("stringKey", "hello world!"); - jsonMap.put("complexStringKey", "h\be\tllo w\u1234orld!"); - jsonMap.put("intKey", new Long(42)); - jsonMap.put("doubleKey", new Double(-23.45e67)); - - JSONObject jsonObject = new JSONObject(jsonMap); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - } - - @Test - public void jsonObjectByBean() { - String expectedStr = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ - "\"intKey\":42,"+ - "\"doubleKey\":-23.45e7"+ - "}"; - MyBean myBean = new MyBean(); - JSONObject jsonObject = new JSONObject(myBean); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - } - - @Test - public void jsonObjectByBeanAndNames() { - String expectedStr = - "{"+ - "\"trueKey\":true,"+ - "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ - "\"doubleKey\":-23.45e7"+ - "}"; - String[] keys = {"trueKey", "complexStringKey", "doubleKey"}; - MyBean myBean = new MyBean(); - JSONObject jsonObject = new JSONObject(myBean, keys); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - } - - @Test - public void jsonObjectByResourceBundle() { - String expectedStr = - "{"+ - "\"greetings\": {"+ - "\"hello\":\"Hello, \","+ - "\"world\":\"World!\""+ - "},"+ - "\"farewells\": {"+ - "\"later\":\"Later, \","+ - "\"gator\":\"Alligator!\""+ - "}"+ - "}"; - JSONObject jsonObject = new - JSONObject("org.json.junit.StringsResourceBundle", - Locale.getDefault()); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - } - - @Test - public void jsonObjectAccumulate() { - String expectedStr = - "{"+ - "\"myArray\": ["+ - "true,"+ - "false,"+ - "\"hello world!\","+ - "\"h\be\tllo w\u1234orld!\","+ - "42,"+ - "-23.45e7"+ - "]"+ - "}"; - JSONObject jsonObject = new JSONObject(); - jsonObject.accumulate("myArray", true); - jsonObject.accumulate("myArray", false); - jsonObject.accumulate("myArray", "hello world!"); - jsonObject.accumulate("myArray", "h\be\tllo w\u1234orld!"); - jsonObject.accumulate("myArray", 42); - jsonObject.accumulate("myArray", -23.45e7); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - } - - @Test - public void jsonObjectAppend() { - String expectedStr = - "{"+ - "\"myArray\": ["+ - "true,"+ - "false,"+ - "\"hello world!\","+ - "\"h\be\tllo w\u1234orld!\","+ - "42,"+ - "-23.45e7"+ - "]"+ - "}"; - 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); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - } - - @Test - public void jsonObjectDoubleToString() { - String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68" }; - Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67 }; - for (int i = 0; i < expectedStrs.length; ++i) { - String actualStr = JSONObject.doubleToString(doubles[i]); - assertTrue("value expected ["+expectedStrs[i]+ - "] found ["+actualStr+ "]", - expectedStrs[i].equals(actualStr)); - } - } - - @Test - public void jsonObjectValues() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"trueStrKey\":\"true\","+ - "\"falseStrKey\":\"false\","+ - "\"stringKey\":\"hello world!\","+ - "\"intKey\":42,"+ - "\"intStrKey\":\"43\","+ - "\"longKey\":1234567890123456789,"+ - "\"longStrKey\":\"987654321098765432\","+ - "\"doubleKey\":-23.45e7,"+ - "\"doubleStrKey\":\"00001.000\","+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{\"myKey\":\"myVal\"}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); - assertTrue("opt trueKey should be true", jsonObject.optBoolean("trueKey")); - assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey")); - assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey")); - assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey")); - assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey")); - assertTrue("stringKey should be string", - jsonObject.getString("stringKey").equals("hello world!")); - assertTrue("doubleKey should be double", - jsonObject.getDouble("doubleKey") == -23.45e7); - assertTrue("doubleStrKey should be double", - jsonObject.getDouble("doubleStrKey") == 1); - 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("intKey should be int", - jsonObject.optInt("intKey") == 42); - assertTrue("opt intKey should be int", - jsonObject.optInt("intKey", 0) == 42); - assertTrue("opt intKey with default should be int", - jsonObject.getInt("intKey") == 42); - assertTrue("intStrKey should be int", - jsonObject.getInt("intStrKey") == 43); - assertTrue("longKey should be long", - jsonObject.getLong("longKey") == 1234567890123456789L); - assertTrue("opt longKey should be long", - jsonObject.optLong("longKey") == 1234567890123456789L); - assertTrue("opt longKey with default should be long", - jsonObject.optLong("longKey", 0) == 1234567890123456789L); - assertTrue("longStrKey should be long", - jsonObject.getLong("longStrKey") == 987654321098765432L); - assertTrue("xKey should not exist", - jsonObject.isNull("xKey")); - assertTrue("stringKey should exist", - jsonObject.has("stringKey")); - assertTrue("opt stringKey should string", - jsonObject.optString("stringKey").equals("hello world!")); - assertTrue("opt stringKey with default should string", - jsonObject.optString("stringKey", "not found").equals("hello world!")); - JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); - assertTrue("arrayKey should be JSONArray", - jsonArray.getInt(0) == 0 && - jsonArray.getInt(1) == 1 && - jsonArray.getInt(2) == 2); - jsonArray = jsonObject.optJSONArray("arrayKey"); - assertTrue("opt arrayKey should be JSONArray", - jsonArray.getInt(0) == 0 && - jsonArray.getInt(1) == 1 && - jsonArray.getInt(2) == 2); - JSONObject jsonObjectInner = jsonObject.getJSONObject("objectKey"); - assertTrue("objectKey should be JSONObject", - jsonObjectInner.get("myKey").equals("myVal")); - } - - @Test - public void jsonObjectNonAndWrongValues() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"trueStrKey\":\"true\","+ - "\"falseStrKey\":\"false\","+ - "\"stringKey\":\"hello world!\","+ - "\"intKey\":42,"+ - "\"intStrKey\":\"43\","+ - "\"longKey\":1234567890123456789,"+ - "\"longStrKey\":\"987654321098765432\","+ - "\"doubleKey\":-23.45e7,"+ - "\"doubleStrKey\":\"00001.000\","+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{\"myKey\":\"myVal\"}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - int tryCount = 0; - int exceptionCount = 0; - try { - ++tryCount; - jsonObject.getBoolean("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getBoolean("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getString("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getString("trueKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getDouble("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getDouble("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getInt("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getInt("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getLong("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getLong("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getJSONArray("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getJSONArray("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getJSONObject("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } - try { - ++tryCount; - jsonObject.getJSONObject("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } - assertTrue("all get calls should have failed", - exceptionCount == tryCount); - } - - @Test - public void jsonObjectNames() { - - // getNames() from null JSONObject - assertTrue("null names from null Object", - null == JSONObject.getNames((Object)null)); - - // getNames() from object with no fields - assertTrue("null names from Object with no fields", - null == JSONObject.getNames(new MyJsonString())); - - // getNames() from empty JSONObject - String emptyStr = "{}"; - JSONObject emptyJsonObject = new JSONObject(emptyStr); - assertTrue("empty JSONObject should have null names", - null == JSONObject.getNames(emptyJsonObject)); - - // getNames() from JSONObject - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "}"; - String [] expectedNames = {"trueKey", "falseKey", "stringKey"}; - JSONObject jsonObject = new JSONObject(str); - String [] names = JSONObject.getNames(jsonObject); - Util.compareActualVsExpectedStringArrays(names, expectedNames); - } - - @Test - public void jsonObjectNamesToJsonAray() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "}"; - String [] expectedNames = {"trueKey", "falseKey", "stringKey" }; - - JSONObject jsonObject = new JSONObject(str); - JSONArray jsonArray = jsonObject.names(); - /** - * Cannot really compare to an expected JSONArray because the ordering - * of the JSONObject keys is not fixed, and JSONArray comparisons - * presume fixed. Since this test is limited to key strings, a - * string comparison will have to suffice. - */ - String namesStr = jsonArray.toString(); - // remove square brackets, commas, and spaces - namesStr = namesStr.replaceAll("[\\]|\\[|\"]", ""); - String [] names = namesStr.split(","); - - Util.compareActualVsExpectedStringArrays(names, expectedNames); - } - - @Test - public void objectNames() { - MyBean myBean = new MyBean(); - String [] expectedNames = {"intKey", "doubleKey", "stringKey", - "complexStringKey", "trueKey", "falseKey"}; - String [] names = JSONObject.getNames(myBean); - Util.compareActualVsExpectedStringArrays(names, expectedNames); - } - - @Test - public void jsonObjectIncrement() { - String str = - "{"+ - "\"keyLong\":9999999991,"+ - "\"keyDouble\":1.1,"+ - "}"; - String expectedStr = - "{"+ - "\"keyInt\":3,"+ - "\"keyLong\":9999999993,"+ - "\"keyDouble\":3.1,"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.increment("keyInt"); - jsonObject.increment("keyInt"); - jsonObject.increment("keyLong"); - jsonObject.increment("keyDouble"); - jsonObject.increment("keyInt"); - jsonObject.increment("keyLong"); - jsonObject.increment("keyDouble"); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - } - - @Test - public void jsonObjectNamesToArray() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "}"; - String [] expectedNames = {"trueKey", "falseKey", "stringKey"}; - JSONObject jsonObject = new JSONObject(str); - String [] names = JSONObject.getNames(jsonObject); - Util.compareActualVsExpectedStringArrays(names, expectedNames); - } - - @Test - public void jsonObjectNumberToString() { - String str; - Double dVal; - Integer iVal = 1; - str = JSONObject.numberToString(iVal); - assertTrue("expected "+iVal+" actual "+str, iVal.toString().equals(str)); - dVal = 12.34; - str = JSONObject.numberToString(dVal); - assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); - dVal = 12.34e27; - str = JSONObject.numberToString(dVal); - assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); - // trailing .0 is truncated, so it doesn't quite match toString() - dVal = 5000000.0000000; - str = JSONObject.numberToString(dVal); - assertTrue("expected 5000000 actual "+str, str.equals("5000000")); - } - - @Test - public void jsonObjectPut() { - String expectedStr = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{"+ - "\"myKey1\":\"myVal1\","+ - "\"myKey2\":\"myVal2\","+ - "\"myKey3\":\"myVal3\","+ - "\"myKey4\":\"myVal4\""+ - "}"+ - "}"; - String expectedStrAfterRemoval = - "{"+ - "\"falseKey\":false,"+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{"+ - "\"myKey1\":\"myVal1\","+ - "\"myKey2\":\"myVal2\","+ - "\"myKey3\":\"myVal3\","+ - "\"myKey4\":\"myVal4\""+ - "}"+ - "}"; - JSONObject jsonObject = new JSONObject(); - jsonObject.put("trueKey", true); - jsonObject.put("falseKey", false); - Integer [] intArray = { 0, 1, 2 }; - jsonObject.put("arrayKey", Arrays.asList(intArray)); - Map myMap = new HashMap(); - myMap.put("myKey1", "myVal1"); - myMap.put("myKey2", "myVal2"); - myMap.put("myKey3", "myVal3"); - myMap.put("myKey4", "myVal4"); - jsonObject.put("objectKey", myMap); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - assertTrue("equal jsonObjects should be similar", - jsonObject.similar(expectedJsonObject)); - - jsonObject.remove("trueKey"); - JSONObject expectedJsonObjectAfterRemoval = - new JSONObject(expectedStrAfterRemoval); - Util.compareActualVsExpectedJsonObjects(jsonObject, - expectedJsonObjectAfterRemoval); - assertTrue("unequal jsonObjects should not be similar", - !jsonObject.similar(expectedJsonObject)); - assertTrue("unequal Objects should not be similar", - !jsonObject.similar(new JSONArray())); - - String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}"; - String bCompareValueStr = "{\"a\":\"notAval\",\"b\":true}"; - JSONObject aCompareValueJsonObject = new JSONObject(aCompareValueStr); - JSONObject bCompareValueJsonObject = new JSONObject(bCompareValueStr); - assertTrue("different values should not be similar", - !aCompareValueJsonObject.similar(bCompareValueJsonObject)); - - String aCompareObjectStr = "{\"a\":\"aval\",\"b\":{}}"; - String bCompareObjectStr = "{\"a\":\"aval\",\"b\":true}"; - JSONObject aCompareObjectJsonObject = new JSONObject(aCompareObjectStr); - JSONObject bCompareObjectJsonObject = new JSONObject(bCompareObjectStr); - assertTrue("different nested JSONObjects should not be similar", - !aCompareObjectJsonObject.similar(bCompareObjectJsonObject)); - - String aCompareArrayStr = "{\"a\":\"aval\",\"b\":[]}"; - String bCompareArrayStr = "{\"a\":\"aval\",\"b\":true}"; - JSONObject aCompareArrayJsonObject = new JSONObject(aCompareArrayStr); - JSONObject bCompareArrayJsonObject = new JSONObject(bCompareArrayStr); - assertTrue("different nested JSONArrays should not be similar", - !aCompareArrayJsonObject.similar(bCompareArrayJsonObject)); - } - - @Test - public void jsonObjectToString() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{"+ - "\"myKey1\":\"myVal1\","+ - "\"myKey2\":\"myVal2\","+ - "\"myKey3\":\"myVal3\","+ - "\"myKey4\":\"myVal4\""+ - "}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - String toStr = jsonObject.toString(); - JSONObject expectedJsonObject = new JSONObject(toStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - } - - @Test - public void jsonObjectToStringSuppressWarningOnCastToMap() { - JSONObject jsonObject = new JSONObject(); - Map map = new HashMap(); - map.put("abc", "def"); - jsonObject.put("key", map); - String toStr = jsonObject.toString(); - JSONObject expectedJsonObject = new JSONObject(toStr); - assertTrue("keys should be equal", - jsonObject.keySet().iterator().next().equals( - expectedJsonObject.keySet().iterator().next())); - /** - * Can't do a Util compare because although they look the same - * in the debugger, one is a map and the other is a JSONObject. - */ - // TODO: fix warnings - map = (Map)jsonObject.get("key"); - JSONObject mapJsonObject = expectedJsonObject.getJSONObject("key"); - assertTrue("value size should be equal", - map.size() == mapJsonObject.length() && map.size() == 1); - assertTrue("keys should be equal for key: "+map.keySet().iterator().next(), - mapJsonObject.keys().next().equals(map.keySet().iterator().next())); - assertTrue("values should be equal for key: "+map.keySet().iterator().next(), - mapJsonObject.get(mapJsonObject.keys().next()).toString().equals( - map.get(map.keySet().iterator().next()))); - } - - @Test - public void jsonObjectToStringSuppressWarningOnCastToCollection() { - JSONObject jsonObject = new JSONObject(); - Collection collection = new ArrayList(); - collection.add("abc"); - // ArrayList will be added as an object - jsonObject.put("key", collection); - String toStr = jsonObject.toString(); - // [abc] will be added as a JSONArray - JSONObject expectedJsonObject = new JSONObject(toStr); - /** - * Can't do a Util compare because although they look the same - * in the debugger, one is a collection and the other is a JSONArray. - */ - assertTrue("keys should be equal", - jsonObject.keySet().iterator().next().equals( - expectedJsonObject.keySet().iterator().next())); - // TODO: fix warnings - collection = (Collection)jsonObject.get("key"); - JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); - assertTrue("value size should be equal", - collection.size() == jsonArray.length()); - Iterator it = collection.iterator(); - for (int i = 0; i < collection.size(); ++i) { - assertTrue("items should be equal for index: "+i, - jsonArray.get(i).toString().equals(it.next().toString())); - } - } - - @Test - public void valueToString() { - - assertTrue("null valueToString() incorrect", - "null".equals(JSONObject.valueToString(null))); - MyJsonString jsonString = new MyJsonString(); - assertTrue("jsonstring valueToString() incorrect", - "my string".equals(JSONObject.valueToString(jsonString))); - assertTrue("boolean valueToString() incorrect", - "true".equals(JSONObject.valueToString(Boolean.TRUE))); - assertTrue("non-numeric double", - "null".equals(JSONObject.doubleToString(Double.POSITIVE_INFINITY))); - String jsonObjectStr = - "{"+ - "\"key1\":\"val1\","+ - "\"key2\":\"val2\","+ - "\"key3\":\"val3\""+ - "}"; - JSONObject jsonObject = new JSONObject(jsonObjectStr); - assertTrue("jsonObject valueToString() incorrect", - JSONObject.valueToString(jsonObject).equals(jsonObject.toString())); - String jsonArrayStr = - "[1,2,3]"; - JSONArray jsonArray = new JSONArray(jsonArrayStr); - assertTrue("jsonArra valueToString() incorrect", - JSONObject.valueToString(jsonArray).equals(jsonArray.toString())); - Map map = new HashMap(); - map.put("key1", "val1"); - map.put("key2", "val2"); - map.put("key3", "val3"); - assertTrue("map valueToString() incorrect", - jsonObject.toString().equals(JSONObject.valueToString(map))); - Collection collection = new ArrayList(); - collection.add(new Integer(1)); - collection.add(new Integer(2)); - collection.add(new Integer(3)); - assertTrue("collection valueToString() expected: "+ - jsonArray.toString()+ " actual: "+ - JSONObject.valueToString(collection), - jsonArray.toString().equals(JSONObject.valueToString(collection))); - Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; - assertTrue("array valueToString() incorrect", - jsonArray.toString().equals(JSONObject.valueToString(array))); - } - - @Test - public void wrapObject() { - // wrap(null) returns NULL - assertTrue("null wrap() incorrect", - JSONObject.NULL == JSONObject.wrap(null)); - - // wrap(Integer) returns Integer - Integer in = new Integer(1); - assertTrue("Integer wrap() incorrect", - in == JSONObject.wrap(in)); - - // wrap JSONObject returns JSONObject - String jsonObjectStr = - "{"+ - "\"key1\":\"val1\","+ - "\"key2\":\"val2\","+ - "\"key3\":\"val3\""+ - "}"; - JSONObject jsonObject = new JSONObject(jsonObjectStr); - assertTrue("JSONObject wrap() incorrect", - jsonObject == JSONObject.wrap(jsonObject)); - - // wrap collection returns JSONArray - Collection collection = new ArrayList(); - collection.add(new Integer(1)); - collection.add(new Integer(2)); - collection.add(new Integer(3)); - JSONArray jsonArray = (JSONArray)(JSONObject.wrap(collection)); - String expectedCollectionJsonArrayStr = - "[1,2,3]"; - JSONArray expectedCollectionJsonArray = - new JSONArray(expectedCollectionJsonArrayStr); - Util.compareActualVsExpectedJsonArrays(jsonArray, - expectedCollectionJsonArray); - - // wrap Array returns JSONArray - Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; - JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); - JSONArray expectedIntegerArrayJsonArray = new JSONArray("[1,2,3]"); - Util.compareActualVsExpectedJsonArrays(integerArrayJsonArray, - expectedIntegerArrayJsonArray); - - // wrap map returns JSONObject - Map map = new HashMap(); - map.put("key1", "val1"); - map.put("key2", "val2"); - map.put("key3", "val3"); - JSONObject mapJsonObject = (JSONObject)(JSONObject.wrap(map)); - Util.compareActualVsExpectedJsonObjects(jsonObject, mapJsonObject); - - // TODO test wrap(package) - } - - @Test - public void jsonObjectParsingErrors() { - int tryCount = 0; - int exceptionCount = 0; - try { - // does not start with '{' - ++tryCount; - String str = "abc"; - new JSONObject(str); - } catch (JSONException ignore) {++exceptionCount; } - try { - // does not end with '}' - ++tryCount; - String str = "{"; - new JSONObject(str); - } catch (JSONException ignore) {++exceptionCount; } - try { - // key with no ':' - ++tryCount; - String str = "{\"myKey\" = true}"; - new JSONObject(str); - } catch (JSONException ignore) {++exceptionCount; } - try { - // entries with no ',' separator - ++tryCount; - String str = "{\"myKey\":true \"myOtherKey\":false}"; - new JSONObject(str); - } catch (JSONException ignore) {++exceptionCount; } - try { - // append to wrong key - ++tryCount; - String str = "{\"myKey\":true, \"myOtherKey\":false}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.append("myKey", "hello"); - } catch (JSONException ignore) {++exceptionCount; } - try { - // increment wrong key - ++tryCount; - String str = "{\"myKey\":true, \"myOtherKey\":false}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.increment("myKey"); - } catch (JSONException ignore) {++exceptionCount; } - try { - // invalid key - ++tryCount; - String str = "{\"myKey\":true, \"myOtherKey\":false}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.get(null); - } catch (JSONException ignore) {++exceptionCount; } - try { - // invalid numberToString() - ++tryCount; - JSONObject.numberToString((Number)null); - } catch (JSONException ignore) {++exceptionCount; } - try { - // null put key - ++tryCount; - JSONObject jsonObject = new JSONObject("{}"); - jsonObject.put(null, 0); - } catch (NullPointerException ignore) {++exceptionCount; } - try { - // multiple putOnce key - ++tryCount; - JSONObject jsonObject = new JSONObject("{}"); - jsonObject.putOnce("hello", "world"); - jsonObject.putOnce("hello", "world!"); - } catch (JSONException ignore) {++exceptionCount; } - try { - // test validity of invalid double - ++tryCount; - JSONObject.testValidity(Double.NaN); - } catch (JSONException ignore) {++exceptionCount; } - try { - // test validity of invalid float - ++tryCount; - JSONObject.testValidity(Float.NEGATIVE_INFINITY); - } catch (JSONException ignore) {++exceptionCount; } - - assertTrue("all tries should have failed", - exceptionCount == tryCount); - } - - @Test - public void jsonObjectOptDefault() { - - String str = "{\"myKey\": \"myval\"}"; - JSONObject jsonObject = new JSONObject(str); - - assertTrue("optBoolean() should return default boolean", - Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE)); - assertTrue("optInt() should return default int", - 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optInt() should return default int", - 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optLong() should return default long", - 42 == jsonObject.optLong("myKey", 42)); - assertTrue("optDouble() should return default double", - 42.3 == jsonObject.optDouble("myKey", 42.3)); - assertTrue("optString() should return default string", - "hi".equals(jsonObject.optString("hiKey", "hi"))); - } - - @Test - public void jsonObjectputNull() { - - // put null should remove the item. - String str = "{\"myKey\": \"myval\"}"; - JSONObject jsonObjectRemove = new JSONObject(str); - JSONObject jsonObjectPutNull = new JSONObject(str); - jsonObjectRemove.remove("myKey"); - jsonObjectPutNull.put("myKey", (Object)null); - Util.compareActualVsExpectedJsonObjects(jsonObjectRemove, jsonObjectPutNull); - assertTrue("jsonObject should be empty", - jsonObjectRemove.length() == 0 && - jsonObjectPutNull.length() == 0); - } - - @Test - public void jsonObjectQuote() { - String str; - str = ""; - String quotedStr; - quotedStr = JSONObject.quote(str); - assertTrue("quote() expected escaped quotes, found "+quotedStr, - "\"\"".equals(quotedStr)); - str = "\"\""; - quotedStr = JSONObject.quote(str); - assertTrue("quote() expected escaped quotes, found "+quotedStr, - "\"\\\"\\\"\"".equals(quotedStr)); - str = ". + * In this test the map is null. + */ + Map map = null; + JSONObject jsonObject = new JSONObject(map); + JSONObject expectedJsonObject = new JSONObject(); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByMap() { + /** + * JSONObjects can be built from a Map. + * In this test all of the map entries are valid JSON types. + * TODO: test with map values that are not valid JSON types + */ + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e67"+ + "}"; + Map jsonMap = new HashMap(); + jsonMap.put("trueKey", new Boolean(true)); + jsonMap.put("falseKey", new Boolean(false)); + jsonMap.put("stringKey", "hello world!"); + jsonMap.put("complexStringKey", "h\be\tllo w\u1234orld!"); + jsonMap.put("intKey", new Long(42)); + jsonMap.put("doubleKey", new Double(-23.45e67)); + + JSONObject jsonObject = new JSONObject(jsonMap); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByMapWithNullValue() { + /** + * JSONObjects can be built from a Map. + * In this test one of the map values is null + */ + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e67"+ + "}"; + Map jsonMap = new HashMap(); + jsonMap.put("trueKey", new Boolean(true)); + jsonMap.put("falseKey", new Boolean(false)); + jsonMap.put("stringKey", "hello world!"); + jsonMap.put("nullKey", null); + jsonMap.put("complexStringKey", "h\be\tllo w\u1234orld!"); + jsonMap.put("intKey", new Long(42)); + jsonMap.put("doubleKey", new Double(-23.45e67)); + + JSONObject jsonObject = new JSONObject(jsonMap); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test(expected=NullPointerException.class) + public void jsonObjectByNullBean() { + /** + * JSONObject built from a bean, but only using a null value. + * Nothing good is expected to happen. + */ + MyBean myBean = null; + new JSONObject(myBean); + } + + @Test + public void jsonObjectByBean() { + /** + * JSONObject built from a bean. In this case all of the + * bean properties are valid JSON types + * TODO: test with bean fields that are not valid JSON types + */ + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e7"+ + "}"; + MyBean myBean = new MyBean(); + JSONObject jsonObject = new JSONObject(myBean); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByObjectAndNames() { + /** + * A bean is also an object. But in order to test the JSONObject + * ctor that takes an object and a list of names, + * this particular bean needs some public + * data members, which have been added to the class. + */ + String expectedStr = + "{"+ + "\"publicStr\":\"abc\","+ + "\"publicInt\":42"+ + "}"; + String[] keys = {"publicStr", "publicInt"}; + + MyBean myBean = new MyBean(); + JSONObject jsonObject = new JSONObject(myBean, keys); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + // this is where I left off + + @Test + public void jsonObjectByResourceBundle() { + String expectedStr = + "{"+ + "\"greetings\": {"+ + "\"hello\":\"Hello, \","+ + "\"world\":\"World!\""+ + "},"+ + "\"farewells\": {"+ + "\"later\":\"Later, \","+ + "\"gator\":\"Alligator!\""+ + "}"+ + "}"; + JSONObject jsonObject = new + JSONObject("org.json.junit.StringsResourceBundle", + Locale.getDefault()); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectAccumulate() { + String expectedStr = + "{"+ + "\"myArray\": ["+ + "true,"+ + "false,"+ + "\"hello world!\","+ + "\"h\be\tllo w\u1234orld!\","+ + "42,"+ + "-23.45e7"+ + "]"+ + "}"; + JSONObject jsonObject = new JSONObject(); + jsonObject.accumulate("myArray", true); + jsonObject.accumulate("myArray", false); + jsonObject.accumulate("myArray", "hello world!"); + jsonObject.accumulate("myArray", "h\be\tllo w\u1234orld!"); + jsonObject.accumulate("myArray", 42); + jsonObject.accumulate("myArray", -23.45e7); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectAppend() { + String expectedStr = + "{"+ + "\"myArray\": ["+ + "true,"+ + "false,"+ + "\"hello world!\","+ + "\"h\be\tllo w\u1234orld!\","+ + "42,"+ + "-23.45e7"+ + "]"+ + "}"; + 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); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectDoubleToString() { + String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68" }; + Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67 }; + for (int i = 0; i < expectedStrs.length; ++i) { + String actualStr = JSONObject.doubleToString(doubles[i]); + assertTrue("value expected ["+expectedStrs[i]+ + "] found ["+actualStr+ "]", + expectedStrs[i].equals(actualStr)); + } + } + + @Test + public void jsonObjectValues() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"trueStrKey\":\"true\","+ + "\"falseStrKey\":\"false\","+ + "\"stringKey\":\"hello world!\","+ + "\"intKey\":42,"+ + "\"intStrKey\":\"43\","+ + "\"longKey\":1234567890123456789,"+ + "\"longStrKey\":\"987654321098765432\","+ + "\"doubleKey\":-23.45e7,"+ + "\"doubleStrKey\":\"00001.000\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{\"myKey\":\"myVal\"}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); + assertTrue("opt trueKey should be true", jsonObject.optBoolean("trueKey")); + assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey")); + assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey")); + assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey")); + assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey")); + assertTrue("stringKey should be string", + jsonObject.getString("stringKey").equals("hello world!")); + assertTrue("doubleKey should be double", + jsonObject.getDouble("doubleKey") == -23.45e7); + assertTrue("doubleStrKey should be double", + jsonObject.getDouble("doubleStrKey") == 1); + 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("intKey should be int", + jsonObject.optInt("intKey") == 42); + assertTrue("opt intKey should be int", + jsonObject.optInt("intKey", 0) == 42); + assertTrue("opt intKey with default should be int", + jsonObject.getInt("intKey") == 42); + assertTrue("intStrKey should be int", + jsonObject.getInt("intStrKey") == 43); + assertTrue("longKey should be long", + jsonObject.getLong("longKey") == 1234567890123456789L); + assertTrue("opt longKey should be long", + jsonObject.optLong("longKey") == 1234567890123456789L); + assertTrue("opt longKey with default should be long", + jsonObject.optLong("longKey", 0) == 1234567890123456789L); + assertTrue("longStrKey should be long", + jsonObject.getLong("longStrKey") == 987654321098765432L); + assertTrue("xKey should not exist", + jsonObject.isNull("xKey")); + assertTrue("stringKey should exist", + jsonObject.has("stringKey")); + assertTrue("opt stringKey should string", + jsonObject.optString("stringKey").equals("hello world!")); + assertTrue("opt stringKey with default should string", + jsonObject.optString("stringKey", "not found").equals("hello world!")); + JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); + assertTrue("arrayKey should be JSONArray", + jsonArray.getInt(0) == 0 && + jsonArray.getInt(1) == 1 && + jsonArray.getInt(2) == 2); + jsonArray = jsonObject.optJSONArray("arrayKey"); + assertTrue("opt arrayKey should be JSONArray", + jsonArray.getInt(0) == 0 && + jsonArray.getInt(1) == 1 && + jsonArray.getInt(2) == 2); + JSONObject jsonObjectInner = jsonObject.getJSONObject("objectKey"); + assertTrue("objectKey should be JSONObject", + jsonObjectInner.get("myKey").equals("myVal")); + } + + @Test + public void jsonObjectNonAndWrongValues() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"trueStrKey\":\"true\","+ + "\"falseStrKey\":\"false\","+ + "\"stringKey\":\"hello world!\","+ + "\"intKey\":42,"+ + "\"intStrKey\":\"43\","+ + "\"longKey\":1234567890123456789,"+ + "\"longStrKey\":\"987654321098765432\","+ + "\"doubleKey\":-23.45e7,"+ + "\"doubleStrKey\":\"00001.000\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{\"myKey\":\"myVal\"}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + int tryCount = 0; + int exceptionCount = 0; + try { + ++tryCount; + jsonObject.getBoolean("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getBoolean("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getString("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getString("trueKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getDouble("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getDouble("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getInt("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getInt("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getLong("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getLong("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getJSONArray("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getJSONArray("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getJSONObject("nonKey"); + } catch (JSONException ignore) { ++exceptionCount; } + try { + ++tryCount; + jsonObject.getJSONObject("stringKey"); + } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("all get calls should have failed", + exceptionCount == tryCount); + } + + @Test + public void jsonObjectNames() { + + // getNames() from null JSONObject + assertTrue("null names from null Object", + null == JSONObject.getNames((Object)null)); + + // getNames() from object with no fields + assertTrue("null names from Object with no fields", + null == JSONObject.getNames(new MyJsonString())); + + // getNames() from empty JSONObject + String emptyStr = "{}"; + JSONObject emptyJsonObject = new JSONObject(emptyStr); + assertTrue("empty JSONObject should have null names", + null == JSONObject.getNames(emptyJsonObject)); + + // getNames() from JSONObject + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "}"; + String [] expectedNames = {"trueKey", "falseKey", "stringKey"}; + JSONObject jsonObject = new JSONObject(str); + String [] names = JSONObject.getNames(jsonObject); + Util.compareActualVsExpectedStringArrays(names, expectedNames); + } + + @Test + public void jsonObjectNamesToJsonAray() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "}"; + String [] expectedNames = {"trueKey", "falseKey", "stringKey" }; + + JSONObject jsonObject = new JSONObject(str); + JSONArray jsonArray = jsonObject.names(); + /** + * Cannot really compare to an expected JSONArray because the ordering + * of the JSONObject keys is not fixed, and JSONArray comparisons + * presume fixed. Since this test is limited to key strings, a + * string comparison will have to suffice. + */ + String namesStr = jsonArray.toString(); + // remove square brackets, commas, and spaces + namesStr = namesStr.replaceAll("[\\]|\\[|\"]", ""); + String [] names = namesStr.split(","); + + Util.compareActualVsExpectedStringArrays(names, expectedNames); + } + + @Test + public void objectNames() { + /** + * A bean is also an object. But in order to test the static + * method getNames(), this particular bean needs some public + * data members, which have been added to the class. + */ + MyBean myBean = new MyBean(); + String [] expectedNames = {"publicStr", "publicInt"}; + String [] names = JSONObject.getNames(myBean); + Util.compareActualVsExpectedStringArrays(names, expectedNames); + } + + @Test + public void jsonObjectIncrement() { + String str = + "{"+ + "\"keyLong\":9999999991,"+ + "\"keyDouble\":1.1,"+ + "}"; + String expectedStr = + "{"+ + "\"keyInt\":3,"+ + "\"keyLong\":9999999993,"+ + "\"keyDouble\":3.1,"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.increment("keyInt"); + jsonObject.increment("keyInt"); + jsonObject.increment("keyLong"); + jsonObject.increment("keyDouble"); + jsonObject.increment("keyInt"); + jsonObject.increment("keyLong"); + jsonObject.increment("keyDouble"); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectNamesToArray() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "}"; + String [] expectedNames = {"trueKey", "falseKey", "stringKey"}; + JSONObject jsonObject = new JSONObject(str); + String [] names = JSONObject.getNames(jsonObject); + Util.compareActualVsExpectedStringArrays(names, expectedNames); + } + + @Test + public void jsonObjectNumberToString() { + String str; + Double dVal; + Integer iVal = 1; + str = JSONObject.numberToString(iVal); + assertTrue("expected "+iVal+" actual "+str, iVal.toString().equals(str)); + dVal = 12.34; + str = JSONObject.numberToString(dVal); + assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + dVal = 12.34e27; + str = JSONObject.numberToString(dVal); + assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + // trailing .0 is truncated, so it doesn't quite match toString() + dVal = 5000000.0000000; + str = JSONObject.numberToString(dVal); + assertTrue("expected 5000000 actual "+str, str.equals("5000000")); + } + + @Test + public void jsonObjectPut() { + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + String expectedStrAfterRemoval = + "{"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(); + jsonObject.put("trueKey", true); + jsonObject.put("falseKey", false); + Integer [] intArray = { 0, 1, 2 }; + jsonObject.put("arrayKey", Arrays.asList(intArray)); + Map myMap = new HashMap(); + myMap.put("myKey1", "myVal1"); + myMap.put("myKey2", "myVal2"); + myMap.put("myKey3", "myVal3"); + myMap.put("myKey4", "myVal4"); + jsonObject.put("objectKey", myMap); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + assertTrue("equal jsonObjects should be similar", + jsonObject.similar(expectedJsonObject)); + + jsonObject.remove("trueKey"); + JSONObject expectedJsonObjectAfterRemoval = + new JSONObject(expectedStrAfterRemoval); + Util.compareActualVsExpectedJsonObjects(jsonObject, + expectedJsonObjectAfterRemoval); + assertTrue("unequal jsonObjects should not be similar", + !jsonObject.similar(expectedJsonObject)); + assertTrue("unequal Objects should not be similar", + !jsonObject.similar(new JSONArray())); + + String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}"; + String bCompareValueStr = "{\"a\":\"notAval\",\"b\":true}"; + JSONObject aCompareValueJsonObject = new JSONObject(aCompareValueStr); + JSONObject bCompareValueJsonObject = new JSONObject(bCompareValueStr); + assertTrue("different values should not be similar", + !aCompareValueJsonObject.similar(bCompareValueJsonObject)); + + String aCompareObjectStr = "{\"a\":\"aval\",\"b\":{}}"; + String bCompareObjectStr = "{\"a\":\"aval\",\"b\":true}"; + JSONObject aCompareObjectJsonObject = new JSONObject(aCompareObjectStr); + JSONObject bCompareObjectJsonObject = new JSONObject(bCompareObjectStr); + assertTrue("different nested JSONObjects should not be similar", + !aCompareObjectJsonObject.similar(bCompareObjectJsonObject)); + + String aCompareArrayStr = "{\"a\":\"aval\",\"b\":[]}"; + String bCompareArrayStr = "{\"a\":\"aval\",\"b\":true}"; + JSONObject aCompareArrayJsonObject = new JSONObject(aCompareArrayStr); + JSONObject bCompareArrayJsonObject = new JSONObject(bCompareArrayStr); + assertTrue("different nested JSONArrays should not be similar", + !aCompareArrayJsonObject.similar(bCompareArrayJsonObject)); + } + + @Test + public void jsonObjectToString() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + String toStr = jsonObject.toString(); + JSONObject expectedJsonObject = new JSONObject(toStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectToStringSuppressWarningOnCastToMap() { + JSONObject jsonObject = new JSONObject(); + Map map = new HashMap(); + map.put("abc", "def"); + jsonObject.put("key", map); + String toStr = jsonObject.toString(); + JSONObject expectedJsonObject = new JSONObject(toStr); + assertTrue("keys should be equal", + jsonObject.keySet().iterator().next().equals( + expectedJsonObject.keySet().iterator().next())); + /** + * Can't do a Util compare because although they look the same + * in the debugger, one is a map and the other is a JSONObject. + */ + // TODO: fix warnings + map = (Map)jsonObject.get("key"); + JSONObject mapJsonObject = expectedJsonObject.getJSONObject("key"); + assertTrue("value size should be equal", + map.size() == mapJsonObject.length() && map.size() == 1); + assertTrue("keys should be equal for key: "+map.keySet().iterator().next(), + mapJsonObject.keys().next().equals(map.keySet().iterator().next())); + assertTrue("values should be equal for key: "+map.keySet().iterator().next(), + mapJsonObject.get(mapJsonObject.keys().next()).toString().equals( + map.get(map.keySet().iterator().next()))); + } + + @Test + public void jsonObjectToStringSuppressWarningOnCastToCollection() { + JSONObject jsonObject = new JSONObject(); + Collection collection = new ArrayList(); + collection.add("abc"); + // ArrayList will be added as an object + jsonObject.put("key", collection); + String toStr = jsonObject.toString(); + // [abc] will be added as a JSONArray + JSONObject expectedJsonObject = new JSONObject(toStr); + /** + * Can't do a Util compare because although they look the same + * in the debugger, one is a collection and the other is a JSONArray. + */ + assertTrue("keys should be equal", + jsonObject.keySet().iterator().next().equals( + expectedJsonObject.keySet().iterator().next())); + // TODO: fix warnings + collection = (Collection)jsonObject.get("key"); + JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); + assertTrue("value size should be equal", + collection.size() == jsonArray.length()); + Iterator it = collection.iterator(); + for (int i = 0; i < collection.size(); ++i) { + assertTrue("items should be equal for index: "+i, + jsonArray.get(i).toString().equals(it.next().toString())); + } + } + + @Test + public void valueToString() { + + assertTrue("null valueToString() incorrect", + "null".equals(JSONObject.valueToString(null))); + MyJsonString jsonString = new MyJsonString(); + assertTrue("jsonstring valueToString() incorrect", + "my string".equals(JSONObject.valueToString(jsonString))); + assertTrue("boolean valueToString() incorrect", + "true".equals(JSONObject.valueToString(Boolean.TRUE))); + assertTrue("non-numeric double", + "null".equals(JSONObject.doubleToString(Double.POSITIVE_INFINITY))); + String jsonObjectStr = + "{"+ + "\"key1\":\"val1\","+ + "\"key2\":\"val2\","+ + "\"key3\":\"val3\""+ + "}"; + JSONObject jsonObject = new JSONObject(jsonObjectStr); + assertTrue("jsonObject valueToString() incorrect", + JSONObject.valueToString(jsonObject).equals(jsonObject.toString())); + String jsonArrayStr = + "[1,2,3]"; + JSONArray jsonArray = new JSONArray(jsonArrayStr); + assertTrue("jsonArra valueToString() incorrect", + JSONObject.valueToString(jsonArray).equals(jsonArray.toString())); + Map map = new HashMap(); + map.put("key1", "val1"); + map.put("key2", "val2"); + map.put("key3", "val3"); + assertTrue("map valueToString() incorrect", + jsonObject.toString().equals(JSONObject.valueToString(map))); + Collection collection = new ArrayList(); + collection.add(new Integer(1)); + collection.add(new Integer(2)); + collection.add(new Integer(3)); + assertTrue("collection valueToString() expected: "+ + jsonArray.toString()+ " actual: "+ + JSONObject.valueToString(collection), + jsonArray.toString().equals(JSONObject.valueToString(collection))); + Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + assertTrue("array valueToString() incorrect", + jsonArray.toString().equals(JSONObject.valueToString(array))); + } + + @Test + public void wrapObject() { + // wrap(null) returns NULL + assertTrue("null wrap() incorrect", + JSONObject.NULL == JSONObject.wrap(null)); + + // wrap(Integer) returns Integer + Integer in = new Integer(1); + assertTrue("Integer wrap() incorrect", + in == JSONObject.wrap(in)); + + // wrap JSONObject returns JSONObject + String jsonObjectStr = + "{"+ + "\"key1\":\"val1\","+ + "\"key2\":\"val2\","+ + "\"key3\":\"val3\""+ + "}"; + JSONObject jsonObject = new JSONObject(jsonObjectStr); + assertTrue("JSONObject wrap() incorrect", + jsonObject == JSONObject.wrap(jsonObject)); + + // wrap collection returns JSONArray + Collection collection = new ArrayList(); + collection.add(new Integer(1)); + collection.add(new Integer(2)); + collection.add(new Integer(3)); + JSONArray jsonArray = (JSONArray)(JSONObject.wrap(collection)); + String expectedCollectionJsonArrayStr = + "[1,2,3]"; + JSONArray expectedCollectionJsonArray = + new JSONArray(expectedCollectionJsonArrayStr); + Util.compareActualVsExpectedJsonArrays(jsonArray, + expectedCollectionJsonArray); + + // wrap Array returns JSONArray + Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); + JSONArray expectedIntegerArrayJsonArray = new JSONArray("[1,2,3]"); + Util.compareActualVsExpectedJsonArrays(integerArrayJsonArray, + expectedIntegerArrayJsonArray); + + // wrap map returns JSONObject + Map map = new HashMap(); + map.put("key1", "val1"); + map.put("key2", "val2"); + map.put("key3", "val3"); + JSONObject mapJsonObject = (JSONObject)(JSONObject.wrap(map)); + Util.compareActualVsExpectedJsonObjects(jsonObject, mapJsonObject); + + // TODO test wrap(package) + } + + @Test + public void jsonObjectParsingErrors() { + int tryCount = 0; + int exceptionCount = 0; + try { + // does not start with '{' + ++tryCount; + String str = "abc"; + new JSONObject(str); + } catch (JSONException ignore) {++exceptionCount; } + try { + // does not end with '}' + ++tryCount; + String str = "{"; + new JSONObject(str); + } catch (JSONException ignore) {++exceptionCount; } + try { + // key with no ':' + ++tryCount; + String str = "{\"myKey\" = true}"; + new JSONObject(str); + } catch (JSONException ignore) {++exceptionCount; } + try { + // entries with no ',' separator + ++tryCount; + String str = "{\"myKey\":true \"myOtherKey\":false}"; + new JSONObject(str); + } catch (JSONException ignore) {++exceptionCount; } + try { + // append to wrong key + ++tryCount; + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.append("myKey", "hello"); + } catch (JSONException ignore) {++exceptionCount; } + try { + // increment wrong key + ++tryCount; + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.increment("myKey"); + } catch (JSONException ignore) {++exceptionCount; } + try { + // invalid key + ++tryCount; + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.get(null); + } catch (JSONException ignore) {++exceptionCount; } + try { + // invalid numberToString() + ++tryCount; + JSONObject.numberToString((Number)null); + } catch (JSONException ignore) {++exceptionCount; } + try { + // null put key + ++tryCount; + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0); + } catch (NullPointerException ignore) {++exceptionCount; } + try { + // multiple putOnce key + ++tryCount; + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.putOnce("hello", "world"); + jsonObject.putOnce("hello", "world!"); + } catch (JSONException ignore) {++exceptionCount; } + try { + // test validity of invalid double + ++tryCount; + JSONObject.testValidity(Double.NaN); + } catch (JSONException ignore) {++exceptionCount; } + try { + // test validity of invalid float + ++tryCount; + JSONObject.testValidity(Float.NEGATIVE_INFINITY); + } catch (JSONException ignore) {++exceptionCount; } + + assertTrue("all tries should have failed", + exceptionCount == tryCount); + } + + @Test + public void jsonObjectOptDefault() { + + String str = "{\"myKey\": \"myval\"}"; + JSONObject jsonObject = new JSONObject(str); + + assertTrue("optBoolean() should return default boolean", + Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optLong() should return default long", + 42 == jsonObject.optLong("myKey", 42)); + assertTrue("optDouble() should return default double", + 42.3 == jsonObject.optDouble("myKey", 42.3)); + assertTrue("optString() should return default string", + "hi".equals(jsonObject.optString("hiKey", "hi"))); + } + + @Test + public void jsonObjectputNull() { + + // put null should remove the item. + String str = "{\"myKey\": \"myval\"}"; + JSONObject jsonObjectRemove = new JSONObject(str); + JSONObject jsonObjectPutNull = new JSONObject(str); + jsonObjectRemove.remove("myKey"); + jsonObjectPutNull.put("myKey", (Object)null); + Util.compareActualVsExpectedJsonObjects(jsonObjectRemove, jsonObjectPutNull); + assertTrue("jsonObject should be empty", + jsonObjectRemove.length() == 0 && + jsonObjectPutNull.length() == 0); + } + + @Test + public void jsonObjectQuote() { + String str; + str = ""; + String quotedStr; + quotedStr = JSONObject.quote(str); + assertTrue("quote() expected escaped quotes, found "+quotedStr, + "\"\"".equals(quotedStr)); + str = "\"\""; + quotedStr = JSONObject.quote(str); + assertTrue("quote() expected escaped quotes, found "+quotedStr, + "\"\\\"\\\"\"".equals(quotedStr)); + str = " Date: Thu, 7 May 2015 23:04:26 -0500 Subject: [PATCH 106/315] More trickery from the bean --- MyBean.java | 57 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/MyBean.java b/MyBean.java index 6477fdef1..1b9e6f023 100644 --- a/MyBean.java +++ b/MyBean.java @@ -1,13 +1,26 @@ package org.json.junit; -public class MyBean { - public int intKey; - public double doubleKey; - public String stringKey; - public String complexStringKey; - public boolean trueKey; - public boolean falseKey; +import java.io.*; +// bean must be serializable +public class MyBean implements Serializable { + // bean properties should be private + private static final long serialVersionUID = 1L; + private int intKey; + private double doubleKey; + private String stringKey; + private String complexStringKey; + private boolean trueKey; + private boolean falseKey; + + /** + * Throw in a few public properties in order to test building + * from an Object. + */ + public String publicStr; + public int publicInt; + + // bean needs a default ctor public MyBean() { intKey = 42; doubleKey = -23.45e7; @@ -15,7 +28,12 @@ public MyBean() { complexStringKey = "h\be\tllo w\u1234orld!"; trueKey = true; falseKey = false; + + publicStr = "abc"; + publicInt = 42; } + + // need getters, but don't need setters public int getIntKey() { return intKey; } @@ -34,4 +52,29 @@ public boolean isTrueKey() { public boolean isFalseKey() { return falseKey; } + + /** + * Just a random invalid JSON getter, not even backed up by a property + */ + public StringReader getStringReaderKey() { + return (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"; + } + }); + } + // bean hashcode is recommended + public int hashCode() { + return super.hashCode(); + } + // bean equals is recommended + public boolean equals(Object obj) { + return super.equals(obj); + } } From fcb8048038a31bc9683d777d36c0ef3c114a44fd Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 7 May 2015 23:04:44 -0500 Subject: [PATCH 107/315] deepened the testing a little bit, slow going --- JSONObjectTest.java | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 1aa19d798..6ef9b0269 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -85,7 +85,6 @@ public void jsonObjectByMap() { /** * JSONObjects can be built from a Map. * In this test all of the map entries are valid JSON types. - * TODO: test with map values that are not valid JSON types */ String expectedStr = "{"+ @@ -109,6 +108,28 @@ public void jsonObjectByMap() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + @Test + public void jsonObjectByMapWithUnsupportedValues() { + /** + * JSONObjects can be built from a Map. + * In this test the map entries are not valid JSON types. + * The actual conversion is kind of interesting. + */ + String expectedStr = + "{"+ + "\"key1\":{},"+ + "\"key2\":\"java.lang.Exception\""+ + "}"; + Map jsonMap = new HashMap(); + // Just insert some random objects + jsonMap.put("key1", new CDL()); + jsonMap.put("key2", new Exception()); + + JSONObject jsonObject = new JSONObject(jsonMap); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + @Test public void jsonObjectByMapWithNullValue() { /** @@ -151,9 +172,8 @@ public void jsonObjectByNullBean() { @Test public void jsonObjectByBean() { /** - * JSONObject built from a bean. In this case all of the - * bean properties are valid JSON types - * TODO: test with bean fields that are not valid JSON types + * JSONObject built from a bean. In this case all but one of the + * bean getters return valid JSON types */ String expectedStr = "{"+ @@ -162,7 +182,8 @@ public void jsonObjectByBean() { "\"stringKey\":\"hello world!\","+ "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ "\"intKey\":42,"+ - "\"doubleKey\":-23.45e7"+ + "\"doubleKey\":-23.45e7,"+ + "\"stringReaderKey\":{}"+ "}"; MyBean myBean = new MyBean(); JSONObject jsonObject = new JSONObject(myBean); From fb36918d854d5561d90bb8728ef1698bc545f42f Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 8 May 2015 23:30:41 -0500 Subject: [PATCH 108/315] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d91f7c26b..64c00142f 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,10 @@ https://github.com/douglascrockford/JSON-java
*These tests are a work in progress. Help from interested developers is welcome.*
More coverage is needed, but more importantly, improvements to test quality is needed.
+You will need the following libraries for testing: Test harness: http://junit.org
Coverage: http://www.eclemma.org/
+Mockery: https://github.com/mockito/mockito Eclipse is the recommended development environment. Run individual tests or JunitTestSuite using *EclEmma Coverage*, or execute the TestRunner application directly.
From 0a995318e7faad8835d11ad73f47004bcbff3d64 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 9 May 2015 15:24:21 -0500 Subject: [PATCH 109/315] Delete MyBean.java --- MyBean.java | 80 ----------------------------------------------------- 1 file changed, 80 deletions(-) delete mode 100644 MyBean.java diff --git a/MyBean.java b/MyBean.java deleted file mode 100644 index 1b9e6f023..000000000 --- a/MyBean.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.json.junit; - -import java.io.*; - -// bean must be serializable -public class MyBean implements Serializable { - // bean properties should be private - private static final long serialVersionUID = 1L; - private int intKey; - private double doubleKey; - private String stringKey; - private String complexStringKey; - private boolean trueKey; - private boolean falseKey; - - /** - * Throw in a few public properties in order to test building - * from an Object. - */ - public String publicStr; - public int publicInt; - - // bean needs a default ctor - public MyBean() { - intKey = 42; - doubleKey = -23.45e7; - stringKey = "hello world!"; - complexStringKey = "h\be\tllo w\u1234orld!"; - trueKey = true; - falseKey = false; - - publicStr = "abc"; - publicInt = 42; - } - - // need getters, but don't need setters - public int getIntKey() { - return intKey; - } - public double getDoubleKey() { - return doubleKey; - } - public String getStringKey() { - return stringKey; - } - public String getComplexStringKey() { - return complexStringKey; - } - public boolean isTrueKey() { - return trueKey; - } - public boolean isFalseKey() { - return falseKey; - } - - /** - * Just a random invalid JSON getter, not even backed up by a property - */ - public StringReader getStringReaderKey() { - return (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"; - } - }); - } - // bean hashcode is recommended - public int hashCode() { - return super.hashCode(); - } - // bean equals is recommended - public boolean equals(Object obj) { - return super.equals(obj); - } -} From f2ef541c2d62441d1b90febedf15209ec6053bc4 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 9 May 2015 15:25:06 -0500 Subject: [PATCH 110/315] still in progress, 94% coverage --- JSONObjectTest.java | 119 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 98 insertions(+), 21 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 6ef9b0269..1903543ee 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -1,6 +1,7 @@ package org.json.junit; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; import java.io.*; import java.util.*; @@ -8,13 +9,26 @@ import org.json.*; import org.junit.*; +/** + * These classes will be used for testing + */ class MyJsonString implements JSONString { @Override public String toJSONString() { return "my string"; } -} +}; + +interface MyBean { + public Integer getIntKey(); + public Double getDoubleKey(); + public String getStringKey(); + public String getEscapeStringKey(); + public Boolean isTrueKey(); + public Boolean isFalseKey(); + public StringReader getStringReaderKey(); +}; /** * JSONObject, along with JSONArray, are the central classes of the reference app. @@ -22,6 +36,13 @@ public String toJSONString() { * impossible without it. */ public class JSONObjectTest { + /** + * Need a class with some public data members for testing, so + * JSONObjectTest is chosen. + */ + public Integer publicInt = 42; + public String publicString = "abc"; + @Test public void emptyJsonObject() { @@ -45,7 +66,7 @@ public void jsonObjectByNames() { "\"falseKey\":false,"+ "\"nullKey\":null,"+ "\"stringKey\":\"hello world!\","+ - "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ "\"intKey\":42,"+ "\"doubleKey\":-23.45e67"+ "}"; @@ -91,7 +112,7 @@ public void jsonObjectByMap() { "\"trueKey\":true,"+ "\"falseKey\":false,"+ "\"stringKey\":\"hello world!\","+ - "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ "\"intKey\":42,"+ "\"doubleKey\":-23.45e67"+ "}"; @@ -99,7 +120,7 @@ public void jsonObjectByMap() { jsonMap.put("trueKey", new Boolean(true)); jsonMap.put("falseKey", new Boolean(false)); jsonMap.put("stringKey", "hello world!"); - jsonMap.put("complexStringKey", "h\be\tllo w\u1234orld!"); + jsonMap.put("escapeStringKey", "h\be\tllo w\u1234orld!"); jsonMap.put("intKey", new Long(42)); jsonMap.put("doubleKey", new Double(-23.45e67)); @@ -141,7 +162,7 @@ public void jsonObjectByMapWithNullValue() { "\"trueKey\":true,"+ "\"falseKey\":false,"+ "\"stringKey\":\"hello world!\","+ - "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ "\"intKey\":42,"+ "\"doubleKey\":-23.45e67"+ "}"; @@ -150,7 +171,7 @@ public void jsonObjectByMapWithNullValue() { jsonMap.put("falseKey", new Boolean(false)); jsonMap.put("stringKey", "hello world!"); jsonMap.put("nullKey", null); - jsonMap.put("complexStringKey", "h\be\tllo w\u1234orld!"); + jsonMap.put("escapeStringKey", "h\be\tllo w\u1234orld!"); jsonMap.put("intKey", new Long(42)); jsonMap.put("doubleKey", new Double(-23.45e67)); @@ -180,12 +201,37 @@ public void jsonObjectByBean() { "\"trueKey\":true,"+ "\"falseKey\":false,"+ "\"stringKey\":\"hello world!\","+ - "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ "\"intKey\":42,"+ "\"doubleKey\":-23.45e7,"+ - "\"stringReaderKey\":{}"+ + "\"stringReaderKey\":{},"+ + "\"callbacks\":[{\"handler\":{}},{}]"+ // sorry, mockito artifact "}"; - MyBean myBean = new MyBean(); + + /** + * Default access classes have to be mocked since JSONObject, which is + * not in the same package, cannot call MyBean methods by reflection. + */ + MyBean myBean = mock(MyBean.class); + when(myBean.getDoubleKey()).thenReturn(-23.45e7); + when(myBean.getIntKey()).thenReturn(42); + when(myBean.getStringKey()).thenReturn("hello world!"); + when(myBean.getEscapeStringKey()).thenReturn("h\be\tllo w\u1234orld!"); + when(myBean.isTrueKey()).thenReturn(true); + 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); JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); @@ -201,21 +247,20 @@ public void jsonObjectByObjectAndNames() { */ String expectedStr = "{"+ - "\"publicStr\":\"abc\","+ + "\"publicString\":\"abc\","+ "\"publicInt\":42"+ "}"; - String[] keys = {"publicStr", "publicInt"}; - - MyBean myBean = new MyBean(); - JSONObject jsonObject = new JSONObject(myBean, keys); + String[] keys = {"publicString", "publicInt"}; + // just need a class that has public data members + JSONObjectTest jsonObjectTest = new JSONObjectTest(); + JSONObject jsonObject = new JSONObject(jsonObjectTest, keys); JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } - // this is where I left off - @Test public void jsonObjectByResourceBundle() { + // TODO: how to improve resource bundle testing? String expectedStr = "{"+ "\"greetings\": {"+ @@ -236,6 +281,7 @@ public void jsonObjectByResourceBundle() { @Test public void jsonObjectAccumulate() { + // TODO: should include an unsupported object String expectedStr = "{"+ "\"myArray\": ["+ @@ -260,6 +306,7 @@ public void jsonObjectAccumulate() { @Test public void jsonObjectAppend() { + // TODO: should include an unsupported object String expectedStr = "{"+ "\"myArray\": ["+ @@ -284,8 +331,9 @@ public void jsonObjectAppend() { @Test public void jsonObjectDoubleToString() { - String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68" }; - Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67 }; + String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" }; + Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, + Double.NaN, Double.NEGATIVE_INFINITY }; for (int i = 0; i < expectedStrs.length; ++i) { String actualStr = JSONObject.doubleToString(doubles[i]); assertTrue("value expected ["+expectedStrs[i]+ @@ -368,6 +416,8 @@ public void jsonObjectValues() { jsonObjectInner.get("myKey").equals("myVal")); } + // improving unit tests left off here + @Test public void jsonObjectNonAndWrongValues() { String str = @@ -479,6 +529,13 @@ public void jsonObjectNames() { Util.compareActualVsExpectedStringArrays(names, expectedNames); } + @Test + public void emptyJsonObjectNamesToJsonAray() { + JSONObject jsonObject = new JSONObject(); + JSONArray jsonArray = jsonObject.names(); + assertTrue("jsonArray should be null", jsonArray == null); + } + @Test public void jsonObjectNamesToJsonAray() { String str = @@ -512,9 +569,9 @@ public void objectNames() { * method getNames(), this particular bean needs some public * data members, which have been added to the class. */ - MyBean myBean = new MyBean(); - String [] expectedNames = {"publicStr", "publicInt"}; - String [] names = JSONObject.getNames(myBean); + JSONObjectTest jsonObjectTest = new JSONObjectTest(); + String [] expectedNames = {"publicString", "publicInt"}; + String [] names = JSONObject.getNames(jsonObjectTest); Util.compareActualVsExpectedStringArrays(names, expectedNames); } @@ -530,6 +587,8 @@ public void jsonObjectIncrement() { "\"keyInt\":3,"+ "\"keyLong\":9999999993,"+ "\"keyDouble\":3.1,"+ + // TODO: not sure if this will work on other platforms + "\"keyFloat\":3.0999999046325684,"+ "}"; JSONObject jsonObject = new JSONObject(str); jsonObject.increment("keyInt"); @@ -539,10 +598,20 @@ public void jsonObjectIncrement() { jsonObject.increment("keyInt"); jsonObject.increment("keyLong"); jsonObject.increment("keyDouble"); + jsonObject.put("keyFloat", new Float(1.1)); + jsonObject.increment("keyFloat"); + jsonObject.increment("keyFloat"); JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + @Test + public void emptyJsonObjectNamesToArray() { + JSONObject jsonObject = new JSONObject(); + String [] names = JSONObject.getNames(jsonObject); + assertTrue("names should be null", names == null); + } + @Test public void jsonObjectNamesToArray() { String str = @@ -905,6 +974,13 @@ public void jsonObjectParsingErrors() { exceptionCount == tryCount); } + @Test + public void jsonObjectPutOnceNull() { + JSONObject jsonObject = new JSONObject(); + jsonObject.putOnce(null, null); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + @Test public void jsonObjectOptDefault() { @@ -1014,3 +1090,4 @@ public void equals() { } } + From a9dd8e7b1d8547c3ba46e13d75443c2920c486e8 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 9 May 2015 15:26:15 -0500 Subject: [PATCH 111/315] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 64c00142f..18c20d065 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,6 @@ A unit test has the following stages: | Files used in test | | ------------- | | JunitTestSuite.java | -| MyBean.java | | StringsResourceBundle.java | |TestRunner.java | | Util.java | From 327c0e177eb0d593d49063a656c840f25d8c4693 Mon Sep 17 00:00:00 2001 From: dieter Date: Fri, 22 May 2015 12:47:28 +0200 Subject: [PATCH 112/315] Playing numbers --- JSONObjectTest.java | 94 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 1903543ee..ed3f374b6 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -2,10 +2,10 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; - import java.io.*; +import java.math.BigDecimal; +import java.math.BigInteger; import java.util.*; - import org.json.*; import org.junit.*; @@ -418,6 +418,91 @@ public void jsonObjectValues() { // improving unit tests left off here + @Test + public void stringToValueNumbersTest() { + // Check if library handles large or high precision numbers correctly + assertTrue( "0.2 should be a Double!", + JSONObject.stringToValue( "0.2" ) instanceof Double ); + assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!", + JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double ); + assertTrue( "299792.457999999984 should be a BigDecimal!", + JSONObject.stringToValue( "299792.457999999984" ) instanceof BigDecimal ); + assertTrue( "1 should be an Integer!", + JSONObject.stringToValue( "1" ) instanceof Integer ); + assertTrue( "Integer.MAX_VALUE should still be an Integer!", + JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer ); + assertTrue( "Large integers should be a Long!", + JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long ); + assertTrue( "Long.MAX_VALUE should still be an Integer!", + JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long ); + assertTrue( "Really large integers should be a BigInteger!", + JSONObject.stringToValue( + new BigInteger( new Long( Long.MAX_VALUE ).toString() ) + .add( BigInteger.ONE ).toString() ) instanceof BigInteger ); + } + + @Test + public void jsonValidNumberValuesNeitherLongNorIEE754Compatible() { + // Valid JSON Numbers, probably should return BigDecimal or BigInteger objects + String str = + "{"+ + "\"someNumber\":299792.457999999984,"+ + "\"largeNumber\":12345678901234567890,"+ + "\"preciseNumber\":0.2000000000000000111,"+ + "\"largeExponent\":-23.45e2327"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + assertFalse( "someNumber certainly was not 299792.458!", + jsonObject.get( "someNumber" ).equals( new Double( "299792.458" ) ) ); + assertTrue( "someNumber must be a number", + jsonObject.get( "someNumber" ) instanceof Number ); + assertTrue( "largeNumber must be a number", + jsonObject.get( "largeNumber" ) instanceof Number ); + assertTrue( "preciseNumber must be a number", + jsonObject.get( "preciseNumber" ) instanceof Number ); + assertTrue( "largeExponent must be a number", + jsonObject.get( "largeExponent" ) instanceof Number ); + } + + @Test + public void jsonInvalidNumberValues() { + // Number-notations supported by Java and invalid as JSON + String str = + "{"+ + "\"hexNumber\":-0x123,"+ + "\"tooManyZeros\":00,"+ + "\"negativeInfinite\":-Infinity,"+ + "\"negativeNaN\":-NaN,"+ + "\"negativeFraction\":-.01,"+ + "\"tooManyZerosFraction\":00.001,"+ + "\"negativeHexFloat\":-0x1.fffp1,"+ + "\"hexFloat\":0x1.0P-1074,"+ + "\"floatIdentifier\":0.1f,"+ + "\"doubleIdentifier\":0.1d,"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + assertFalse( "hexNumber must not be a number (should throw exception!?)", + jsonObject.get( "hexNumber" ) instanceof Number ); + assertFalse( "tooManyZeros must not be a number (should throw exception!?)", + jsonObject.get( "tooManyZeros" ) instanceof Number ); + assertFalse( "negativeInfinite must not be a number (should throw exception!?)", + jsonObject.get( "negativeInfinite" ) instanceof Number ); + assertFalse( "negativeNaN must not be a number (should throw exception!?)", + jsonObject.get( "negativeNaN" ) instanceof Number ); + assertFalse( "tooManyZerosFraction must not be a number (should throw exception!?)", + jsonObject.get( "tooManyZerosFraction" ) instanceof Number ); + assertFalse( "negativeFraction must not be a number (should throw exception!?)", + jsonObject.get( "negativeFraction" ) instanceof Number ); + assertFalse( "negativeHexFloat must not be a number (should throw exception!?)", + jsonObject.get( "negativeHexFloat" ) instanceof Number ); + assertFalse( "hexFloat must not be a number (should throw exception!?)", + jsonObject.get( "hexFloat" ) instanceof Number ); + assertFalse( "floatIdentifier must not be a number (should throw exception!?)", + jsonObject.get( "floatIdentifier" ) instanceof Number ); + assertFalse( "doubleIdentifier must not be a number (should throw exception!?)", + jsonObject.get( "doubleIdentifier" ) instanceof Number ); + } + @Test public void jsonObjectNonAndWrongValues() { String str = @@ -850,6 +935,11 @@ public void wrapObject() { assertTrue("Integer wrap() incorrect", in == JSONObject.wrap(in)); + // wrap(BigDecimal) returns BigDecimal + BigDecimal bd = BigDecimal.ONE; + assertTrue("BigDecimal wrap() incorrect", + bd == JSONObject.wrap(bd)); + // wrap JSONObject returns JSONObject String jsonObjectStr = "{"+ From 60e84bff922ab6022a3cfbd9e5ddb0c420efe6b0 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 24 May 2015 23:36:48 -0500 Subject: [PATCH 113/315] fix so numeric behavior is documented but tests succeed --- JSONObjectTest.java | 107 +++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 41 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index ed3f374b6..de36c01f4 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -419,14 +419,19 @@ public void jsonObjectValues() { // improving unit tests left off here @Test + public void stringToValueNumbersTest() { // Check if library handles large or high precision numbers correctly assertTrue( "0.2 should be a Double!", JSONObject.stringToValue( "0.2" ) instanceof Double ); assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!", JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double ); - assertTrue( "299792.457999999984 should be a BigDecimal!", - JSONObject.stringToValue( "299792.457999999984" ) instanceof BigDecimal ); + /** + * This test documents a need for BigDecimal conversion. + */ + Object obj = JSONObject.stringToValue( "299792.457999999984" ); + assertTrue( "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!", + obj.equals(new Double(299792.458)) ); assertTrue( "1 should be an Integer!", JSONObject.stringToValue( "1" ) instanceof Integer ); assertTrue( "Integer.MAX_VALUE should still be an Integer!", @@ -435,35 +440,45 @@ public void stringToValueNumbersTest() { JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long ); assertTrue( "Long.MAX_VALUE should still be an Integer!", JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long ); - assertTrue( "Really large integers should be a BigInteger!", - JSONObject.stringToValue( - new BigInteger( new Long( Long.MAX_VALUE ).toString() ) - .add( BigInteger.ONE ).toString() ) instanceof BigInteger ); + + String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString(); + assertTrue( "Really large integers currently evaluate to string", + JSONObject.stringToValue(str).equals("9223372036854775808")); } @Test - public void jsonValidNumberValuesNeitherLongNorIEE754Compatible() { + /** + * This test documents numeric values which could be numerically + * handled as BigDecimal or BigInteger. It helps determine what outputs + * will change if those types are supported. + */ + public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() { // Valid JSON Numbers, probably should return BigDecimal or BigInteger objects String str = "{"+ - "\"someNumber\":299792.457999999984,"+ + "\"numberWithDecimals\":299792.457999999984,"+ "\"largeNumber\":12345678901234567890,"+ "\"preciseNumber\":0.2000000000000000111,"+ "\"largeExponent\":-23.45e2327"+ "}"; JSONObject jsonObject = new JSONObject(str); - assertFalse( "someNumber certainly was not 299792.458!", - jsonObject.get( "someNumber" ).equals( new Double( "299792.458" ) ) ); - assertTrue( "someNumber must be a number", - jsonObject.get( "someNumber" ) instanceof Number ); - assertTrue( "largeNumber must be a number", - jsonObject.get( "largeNumber" ) instanceof Number ); - assertTrue( "preciseNumber must be a number", - jsonObject.get( "preciseNumber" ) instanceof Number ); - assertTrue( "largeExponent must be a number", - jsonObject.get( "largeExponent" ) instanceof Number ); + // Comes back as a double, but loses precision + assertTrue( "numberWithDecimals currently evaluates to double 299792.458", + jsonObject.get( "numberWithDecimals" ).equals( new Double( "299792.458" ) ) ); + Object obj = jsonObject.get( "largeNumber" ); + assertTrue("largeNumber currently evaluates to string", + "12345678901234567890".equals(obj)); + // comes back as a double but loses precision + assertTrue( "preciseNumber currently evaluates to double 0.2", + jsonObject.get( "preciseNumber" ).equals(new Double(0.2))); + obj = jsonObject.get( "largeExponent" ); + assertTrue("largeExponent should currently evaluates as a string", + "-23.45e2327".equals(obj)); } + /** + * This test documents how JSON-Java handles invalid numeric input. + */ @Test public void jsonInvalidNumberValues() { // Number-notations supported by Java and invalid as JSON @@ -481,26 +496,32 @@ public void jsonInvalidNumberValues() { "\"doubleIdentifier\":0.1d,"+ "}"; JSONObject jsonObject = new JSONObject(str); + Object obj; + obj = jsonObject.get( "hexNumber" ); assertFalse( "hexNumber must not be a number (should throw exception!?)", - jsonObject.get( "hexNumber" ) instanceof Number ); - assertFalse( "tooManyZeros must not be a number (should throw exception!?)", - jsonObject.get( "tooManyZeros" ) instanceof Number ); - assertFalse( "negativeInfinite must not be a number (should throw exception!?)", - jsonObject.get( "negativeInfinite" ) instanceof Number ); - assertFalse( "negativeNaN must not be a number (should throw exception!?)", - jsonObject.get( "negativeNaN" ) instanceof Number ); - assertFalse( "tooManyZerosFraction must not be a number (should throw exception!?)", - jsonObject.get( "tooManyZerosFraction" ) instanceof Number ); - assertFalse( "negativeFraction must not be a number (should throw exception!?)", - jsonObject.get( "negativeFraction" ) instanceof Number ); - assertFalse( "negativeHexFloat must not be a number (should throw exception!?)", - jsonObject.get( "negativeHexFloat" ) instanceof Number ); - assertFalse( "hexFloat must not be a number (should throw exception!?)", - jsonObject.get( "hexFloat" ) instanceof Number ); - assertFalse( "floatIdentifier must not be a number (should throw exception!?)", - jsonObject.get( "floatIdentifier" ) instanceof Number ); - assertFalse( "doubleIdentifier must not be a number (should throw exception!?)", - jsonObject.get( "doubleIdentifier" ) instanceof Number ); + obj instanceof Number ); + assertTrue("hexNumber currently evaluates to string", + obj.equals("-0x123")); + assertTrue( "tooManyZeros currently evaluates to string", + jsonObject.get( "tooManyZeros" ).equals("00")); + obj = jsonObject.get("negativeInfinite"); + assertTrue( "negativeInfinite currently evaluates to string", + obj.equals("-Infinity")); + obj = jsonObject.get("negativeNaN"); + assertTrue( "negativeNaN currently evaluates to string", + obj.equals("-NaN")); + assertTrue( "negativeFraction currently evaluates to double -0.01", + jsonObject.get( "negativeFraction" ).equals(new Double(-0.01))); + assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", + jsonObject.get( "tooManyZerosFraction" ).equals(new Double(0.001))); + assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", + jsonObject.get( "negativeHexFloat" ).equals(new Double(-3.99951171875))); + assertTrue("hexFloat currently evaluates to double 4.9E-324", + jsonObject.get("hexFloat").equals(new Double(4.9E-324))); + assertTrue("floatIdentifier currently evaluates to double 0.1", + jsonObject.get("floatIdentifier").equals(new Double(0.1))); + assertTrue("doubleIdentifier currently evaluates to double 0.1", + jsonObject.get("doubleIdentifier").equals(new Double(0.1))); } @Test @@ -935,10 +956,14 @@ public void wrapObject() { assertTrue("Integer wrap() incorrect", in == JSONObject.wrap(in)); - // wrap(BigDecimal) returns BigDecimal - BigDecimal bd = BigDecimal.ONE; - assertTrue("BigDecimal wrap() incorrect", - bd == JSONObject.wrap(bd)); + /** + * This test is to document the preferred behavior if BigDecimal is + * supported. At the present time, bd returns as a string, since it + * is recognized as being a Java package class. + */ + Object bdWrap = JSONObject.wrap(BigDecimal.ONE); + assertTrue("BigDecimal.ONE currently evaluates to string", + bdWrap.equals("1")); // wrap JSONObject returns JSONObject String jsonObjectStr = From 88756c04908d5137bf570e2c399587e222169f92 Mon Sep 17 00:00:00 2001 From: dieter Date: Wed, 27 May 2015 15:51:30 +0200 Subject: [PATCH 114/315] Hidden typecast for Float objects in JSONobject.increment(String key) --- JSONObjectTest.java | 60 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index de36c01f4..c02ebd1df 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -493,7 +493,7 @@ public void jsonInvalidNumberValues() { "\"negativeHexFloat\":-0x1.fffp1,"+ "\"hexFloat\":0x1.0P-1074,"+ "\"floatIdentifier\":0.1f,"+ - "\"doubleIdentifier\":0.1d,"+ + "\"doubleIdentifier\":0.1d"+ "}"; JSONObject jsonObject = new JSONObject(str); Object obj; @@ -694,6 +694,17 @@ public void jsonObjectIncrement() { "\"keyLong\":9999999993,"+ "\"keyDouble\":3.1,"+ // TODO: not sure if this will work on other platforms + + // Should work the same way on any platform, this the effect of a float to double conversion and happens because + // java type-casts float to double. A 32 bit float is type-casted to 64 bit double by simply appending zero-bits to the + // mantissa (and extended the signed exponent by 3 bits.) + + // Like 1/3 cannot be represented as base10 number because it is periodically, 1/5 cannot be represented as base2 number + // since it is periodically in base2 (take a look at http://www.h-schmidt.net/FloatConverter/) + // The same happens to 3.1, that decimal number (base10 representation) is periodic in base2 representation, therefore + // appending zero-bits is inaccurate only repeating the periodically repeating bits (0110) would be a proper conversion. + // However one cannot detect from a 32 bit IEE754 representation which bits would "repeat infinitely", since the missing + // bits would not fit into the 32 bit float, i.e. the information needed is not there! ;) "\"keyFloat\":3.0999999046325684,"+ "}"; JSONObject jsonObject = new JSONObject(str); @@ -709,6 +720,53 @@ public void jsonObjectIncrement() { jsonObject.increment("keyFloat"); JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + /* + float f = 3.1f; + double df = (double) f; + double d = 3.1d; + System.out.println(Integer.toBinaryString(Float.floatToRawIntBits(f))); + System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(df))); + System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(d))); + + - Float: + seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm + 1000000010001100110011001100110 + - Double + seeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm + 10000000 10001100110011001100110 + 100000000001000110011001100110011000000000000000000000000000000 + 100000000001000110011001100110011001100110011001100110011001101 + */ + assertTrue( "Correctly converting float to double via base10 (string) representation!", new Double( 3.1d ).equals( new Double( new Float( 3.1f ).toString() ) ) ); + + // Pinpointing the not so obvious "buggy" conversion from float to double in JSONObject + JSONObject jo = new JSONObject(); + jo.put( "bug", 3.1f ); // will call put( String key, double value ) with implicit and "buggy" type-cast from float to double + assertFalse( "The java-compiler did add some zero bits for you (probably unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) ); + + JSONObject inc = new JSONObject(); + inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) + assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float ); + inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double! + // this.put(key, (Float) value + 1); + // 1. The (Object)value will be typecasted to (Float)value since it is an instanceof Float actually nothing is done. + // 2. Float instance will be autoboxed into float because the + operator will work on primitives not Objects! + // 3. A float+float operation will be performed and results into a float primitive. + // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method + // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa + assertTrue( "JSONObject increment unexpected behaviour, Float will not stay Float!", jo.get( "bug" ) instanceof Float ); + // correct implementation (with change of behaviour) would be: + // this.put(key, new Float((Float) value + 1)); + // Probably it would be better to deprecate the method and remove some day, while convenient processing the "payload" is not + // really in the the scope of a JSON-library (IMHO.) + + // Some more examples of well documented unexpeced "numbercrunching" ;) + assertTrue("Stumbled over explicitly type-casting float as double!", (double)0.2f == 0.2d ); + assertTrue("Stumbled over comparing float with double any implicit type-cast!", 0.2f == 0.2d ); + Double d1 = new Double( 1.1f ); + Double d2 = new Double( "1.1f" ); + assertTrue( "Stumbled again over implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) ); } @Test From fa79826f0c429cdfc3d060697c6790b7d4b31ee6 Mon Sep 17 00:00:00 2001 From: dieter Date: Wed, 27 May 2015 16:33:42 +0200 Subject: [PATCH 115/315] Better show what has to be expected and what goes wrong --- JSONObjectTest.java | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index c02ebd1df..06f96e5b4 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -695,16 +695,19 @@ public void jsonObjectIncrement() { "\"keyDouble\":3.1,"+ // TODO: not sure if this will work on other platforms - // Should work the same way on any platform, this the effect of a float to double conversion and happens because - // java type-casts float to double. A 32 bit float is type-casted to 64 bit double by simply appending zero-bits to the - // mantissa (and extended the signed exponent by 3 bits.) - - // Like 1/3 cannot be represented as base10 number because it is periodically, 1/5 cannot be represented as base2 number - // since it is periodically in base2 (take a look at http://www.h-schmidt.net/FloatConverter/) + // Should work the same way on any platform! @see https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 + // This is the effect of a float to double conversion and is inherent to the shortcomings of the IEEE 754 format, when + // converting 32-bit into double-precision 64-bit. + // Java type-casts float to double. A 32 bit float is type-casted to 64 bit double by simply appending zero-bits to the + // mantissa (and extended the signed exponent by 3 bits.) and there is no way to obtain more information than it is + // stored in the 32-bits float. + + // Like 1/3 cannot be represented as base10 number because it is periodically, 1/5 (for example) cannot be represented + // as base2 number since it is periodically in base2 (take a look at http://www.h-schmidt.net/FloatConverter/) // The same happens to 3.1, that decimal number (base10 representation) is periodic in base2 representation, therefore - // appending zero-bits is inaccurate only repeating the periodically repeating bits (0110) would be a proper conversion. + // appending zero-bits is inaccurate. Only repeating the periodically occuring bits (0110) would be a proper conversion. // However one cannot detect from a 32 bit IEE754 representation which bits would "repeat infinitely", since the missing - // bits would not fit into the 32 bit float, i.e. the information needed is not there! ;) + // bits would not fit into the 32 bit float, i.e. the information needed simply is not there! "\"keyFloat\":3.0999999046325684,"+ "}"; JSONObject jsonObject = new JSONObject(str); @@ -738,12 +741,19 @@ public void jsonObjectIncrement() { 100000000001000110011001100110011000000000000000000000000000000 100000000001000110011001100110011001100110011001100110011001101 */ + // Examples of well documented but probably unexpected behavior in java / with 32-bit float to 64-bit float conversion. + assertFalse("Document unexpected behaviour with explicit type-casting float as double!", (double)0.2f == 0.2d ); + assertFalse("Document unexpected behaviour with implicit type-cast!", 0.2f == 0.2d ); + Double d1 = new Double( 1.1f ); + Double d2 = new Double( "1.1f" ); + assertFalse( "Document implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) ); + assertTrue( "Correctly converting float to double via base10 (string) representation!", new Double( 3.1d ).equals( new Double( new Float( 3.1f ).toString() ) ) ); // Pinpointing the not so obvious "buggy" conversion from float to double in JSONObject JSONObject jo = new JSONObject(); jo.put( "bug", 3.1f ); // will call put( String key, double value ) with implicit and "buggy" type-cast from float to double - assertFalse( "The java-compiler did add some zero bits for you (probably unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) ); + assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) ); JSONObject inc = new JSONObject(); inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) @@ -755,18 +765,12 @@ public void jsonObjectIncrement() { // 3. A float+float operation will be performed and results into a float primitive. // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa - assertTrue( "JSONObject increment unexpected behaviour, Float will not stay Float!", jo.get( "bug" ) instanceof Float ); - // correct implementation (with change of behaviour) would be: + assertTrue( "JSONObject increment unexpected behavior, Float will not stay Float!", jo.get( "bug" ) instanceof Float ); + // correct implementation (with change of behavior) would be: // this.put(key, new Float((Float) value + 1)); // Probably it would be better to deprecate the method and remove some day, while convenient processing the "payload" is not // really in the the scope of a JSON-library (IMHO.) - // Some more examples of well documented unexpeced "numbercrunching" ;) - assertTrue("Stumbled over explicitly type-casting float as double!", (double)0.2f == 0.2d ); - assertTrue("Stumbled over comparing float with double any implicit type-cast!", 0.2f == 0.2d ); - Double d1 = new Double( 1.1f ); - Double d2 = new Double( "1.1f" ); - assertTrue( "Stumbled again over implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) ); } @Test From 32ea7e0ba3f66fc542f0cbd66588cc3f4f2542df Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 28 May 2015 20:43:47 -0500 Subject: [PATCH 116/315] tests should succeed --- JSONObjectTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 06f96e5b4..e044c8cbf 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -765,7 +765,7 @@ public void jsonObjectIncrement() { // 3. A float+float operation will be performed and results into a float primitive. // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa - assertTrue( "JSONObject increment unexpected behavior, Float will not stay Float!", jo.get( "bug" ) instanceof Float ); + assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Double ); // correct implementation (with change of behavior) would be: // this.put(key, new Float((Float) value + 1)); // Probably it would be better to deprecate the method and remove some day, while convenient processing the "payload" is not From 6b03f1bbe745a8574ca8ca3d71b02e05c3b5a151 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 3 Jun 2015 22:50:08 -0500 Subject: [PATCH 117/315] support enum testing --- EnumTest.java | 80 +++++++++++++++++++++++++++++++++++++++++++++ JunitTestSuite.java | 3 +- MyEnum.java | 7 ++++ MyEnumClass.java | 22 +++++++++++++ MyEnumField.java | 20 ++++++++++++ 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 EnumTest.java create mode 100644 MyEnum.java create mode 100644 MyEnumClass.java create mode 100644 MyEnumField.java diff --git a/EnumTest.java b/EnumTest.java new file mode 100644 index 000000000..6b2a4bc40 --- /dev/null +++ b/EnumTest.java @@ -0,0 +1,80 @@ +package org.json.junit; + +import static org.junit.Assert.*; + +import org.json.*; +import org.junit.*; + +/** + * Documents how enum is handled by JSON-Java. + */ +public class EnumTest { + + @Test + public void simpleEnum() { + /** + * Nothing happens when a simple enum is parsed to JSONObject + */ + MyEnum myEnum = MyEnum.VAL2; + JSONObject jsonObject = new JSONObject(myEnum); + assertTrue("simple enum is not processed by JSONObject", jsonObject.length() == 0); + /** + * Nothing good happens when a simple enum is parsed to JSONArray + */ + try { + new JSONArray(myEnum); + } catch (JSONException e) { + assertTrue("JSONArray throws exception when passed enum", true); + } + } + + @Test + public void enumWithField() { + /** + * enum with a getters is handled like a bean + */ + String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}"; + MyEnumField myEnum = MyEnumField.VAL2; + JSONObject jsonObject = new JSONObject(myEnum); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void enumInClass() { + /** + * class which contains enum instances. + * The enum values in MyEnum are lost. + * The string values in MyEnumFild are extracted and wrapped. + */ + String expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; + MyEnumClass myEnumClass = new MyEnumClass(); + myEnumClass.setMyEnum(MyEnum.VAL1); + myEnumClass.setMyEnumField(MyEnumField.VAL3); + JSONObject jsonObject = new JSONObject(myEnumClass); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void enumValueToString() { + String expectedStr1 = "\"VAL1\""; + String expectedStr2 = "\"VAL1\""; + String expectedStr3 = "\"org.json.junit.MyEnumClass@"; + MyEnum myEnum = MyEnum.VAL1; + MyEnumField myEnumField = MyEnumField.VAL1; + MyEnumClass myEnumClass = new MyEnumClass(); + myEnumClass.setMyEnum(MyEnum.VAL1); + myEnumClass.setMyEnumField(MyEnumField.VAL1); + + String str1 = JSONObject.valueToString(myEnum); + assertTrue("actual myEnum: "+str1+" expected: "+expectedStr1, + str1.equals(expectedStr1)); + String str2 = JSONObject.valueToString(myEnumField); + assertTrue("actual myEnumField: "+str2+" expected: "+expectedStr2, + str2.equals(expectedStr2)); + String str3 = JSONObject.valueToString(myEnumClass); + assertTrue("actual myEnumClass: "+str3+" expected: "+expectedStr3, + str3.startsWith(expectedStr3)); + } +} diff --git a/JunitTestSuite.java b/JunitTestSuite.java index a91426da3..ceff6f143 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -13,7 +13,8 @@ HTTPTest.class, JSONStringerTest.class, JSONObjectTest.class, - JSONArrayTest.class + JSONArrayTest.class, + EnumTest.class }) public class JunitTestSuite { } \ No newline at end of file diff --git a/MyEnum.java b/MyEnum.java new file mode 100644 index 000000000..d2fe08d8e --- /dev/null +++ b/MyEnum.java @@ -0,0 +1,7 @@ +package org.json.junit; + +public enum MyEnum { + VAL1, + VAL2, + VAL3; +} diff --git a/MyEnumClass.java b/MyEnumClass.java new file mode 100644 index 000000000..8e71663ec --- /dev/null +++ b/MyEnumClass.java @@ -0,0 +1,22 @@ +package org.json.junit; + +/** + * this is simply a class that contains some enum instances + */ +public class MyEnumClass { + private MyEnum myEnum; + private MyEnumField myEnumField; + + public MyEnum getMyEnum() { + return myEnum; + } + public void setMyEnum(MyEnum myEnum) { + this.myEnum = myEnum; + } + public MyEnumField getMyEnumField() { + return myEnumField; + } + public void setMyEnumField(MyEnumField myEnumField) { + this.myEnumField = myEnumField; + } +} diff --git a/MyEnumField.java b/MyEnumField.java new file mode 100644 index 000000000..7efec54fc --- /dev/null +++ b/MyEnumField.java @@ -0,0 +1,20 @@ +package org.json.junit; + +public enum MyEnumField { + VAL1(1, "val 1"), + VAL2(2, "val 2"), + VAL3(3, "val 3"); + + private String value; + private Integer intVal; + private MyEnumField(Integer intVal, String value) { + this.value = value; + this.intVal = intVal; + } + public String getValue() { + return value; + } + public Integer getIntVal() { + return intVal; + } +} From 67a0c734b6137aa26f2b8cc63e019e895b2822f4 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Wed, 3 Jun 2015 22:54:12 -0500 Subject: [PATCH 118/315] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 18c20d065..fa0792a09 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ A unit test has the following stages: | CDL.java | 98% | Reasonable test cases. | | Cookie.java | 98.9% | Reasonable test cases. | | CookieList.java |96.5% | Reasonable test cases. | +| EnumTest.java | n/a | Just documenting how enums are handled. | | HTTP.java | 98.7%| Coverage > 90% | | HTTPTokener.java |93.2% | No test | | JSONArray.java |95.9% | Coverage > 90% | From f6bdc908d818cabdba0c52d1598d64341e673d2c Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Wed, 3 Jun 2015 22:55:35 -0500 Subject: [PATCH 119/315] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fa0792a09..bd36d8549 100644 --- a/README.md +++ b/README.md @@ -69,9 +69,12 @@ A unit test has the following stages: | Files used in test | | ------------- | +| MyEnum.java | +| MyEnumClass.java | +| MyEnumField.java | | JunitTestSuite.java | | StringsResourceBundle.java | -|TestRunner.java | +| TestRunner.java | | Util.java | From 969e2d4fd52a503d8f4a7fe81b8758c13a9180e4 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 4 Jun 2015 22:29:55 -0500 Subject: [PATCH 120/315] test iterable --- JSONArrayTest.java | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/JSONArrayTest.java b/JSONArrayTest.java index 78b194eb7..b033a60f1 100644 --- a/JSONArrayTest.java +++ b/JSONArrayTest.java @@ -2,10 +2,7 @@ import static org.junit.Assert.*; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import org.json.*; import org.junit.Test; @@ -464,4 +461,42 @@ public void objectArrayVsIsArray() { JSONArray expectedJsonArray = new JSONArray(expectedStr); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } + + @Test + public void iterator() { + JSONArray jsonArray = new JSONArray(arrayStr); + Iterator it = jsonArray.iterator(); + assertTrue("Array true", + Boolean.TRUE.equals(it.next())); + assertTrue("Array false", + Boolean.FALSE.equals(it.next())); + assertTrue("Array string true", + "true".equals(it.next())); + assertTrue("Array string false", + "false".equals(it.next())); + assertTrue("Array string", + "hello".equals(it.next())); + + assertTrue("Array double", + new Double(23.45e-4).equals(it.next())); + assertTrue("Array string double", + new Double(23.45).equals(Double.parseDouble((String)it.next()))); + + assertTrue("Array value int", + new Integer(42).equals(it.next())); + assertTrue("Array value string int", + new Integer(43).equals(Integer.parseInt((String)it.next()))); + + JSONArray nestedJsonArray = (JSONArray)it.next(); + assertTrue("Array value JSONArray", nestedJsonArray != null); + + JSONObject nestedJsonObject = (JSONObject)it.next(); + assertTrue("Array value JSONObject", nestedJsonObject != null); + + assertTrue("Array value long", + new Long(0).equals(((Number) it.next()).longValue())); + assertTrue("Array value string long", + new Long(-1).equals(Long.parseLong((String) it.next()))); + assertTrue("should be at end of array", !it.hasNext()); + } } From 9cf532828d11a00bf60e082cade9e12d2e573cd9 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 7 Jun 2015 22:22:14 -0500 Subject: [PATCH 121/315] confirm current behavior --- EnumTest.java | 207 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 174 insertions(+), 33 deletions(-) diff --git a/EnumTest.java b/EnumTest.java index 6b2a4bc40..14605b19c 100644 --- a/EnumTest.java +++ b/EnumTest.java @@ -6,66 +6,117 @@ import org.junit.*; /** - * Documents how enum is handled by JSON-Java. + * Enums are not explicitly supported in JSON-Java. But because enums act like + * classes, all required behavior is already be present in some form. + * These tests explore how enum serialization works with JSON-Java. */ public class EnumTest { - @Test - public void simpleEnum() { + public void jsonObjectFromEnum() { /** - * Nothing happens when a simple enum is parsed to JSONObject + * To serialize an enum by its getters, use the JSONObject Object constructor. + * The JSONObject ctor handles enum like any other bean. A JSONobject + * is created whose entries are the getter name/value pairs. */ + + // If there are no getters then the object is empty. MyEnum myEnum = MyEnum.VAL2; JSONObject jsonObject = new JSONObject(myEnum); - assertTrue("simple enum is not processed by JSONObject", jsonObject.length() == 0); - /** - * Nothing good happens when a simple enum is parsed to JSONArray - */ - try { - new JSONArray(myEnum); - } catch (JSONException e) { - assertTrue("JSONArray throws exception when passed enum", true); - } - } + assertTrue("simple enum has no getters", jsonObject.length() == 0); - @Test - public void enumWithField() { - /** - * enum with a getters is handled like a bean - */ + // enum with a getters should create a non-empty object String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}"; - MyEnumField myEnum = MyEnumField.VAL2; - JSONObject jsonObject = new JSONObject(myEnum); + MyEnumField myEnumField = MyEnumField.VAL2; + jsonObject = new JSONObject(myEnumField); JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - } - @Test - public void enumInClass() { /** - * class which contains enum instances. - * The enum values in MyEnum are lost. - * The string values in MyEnumFild are extracted and wrapped. + * class which contains enum instances. Each enum should be stored + * in its own JSONObject */ - String expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; + expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; MyEnumClass myEnumClass = new MyEnumClass(); myEnumClass.setMyEnum(MyEnum.VAL1); myEnumClass.setMyEnumField(MyEnumField.VAL3); - JSONObject jsonObject = new JSONObject(myEnumClass); - JSONObject expectedJsonObject = new JSONObject(expectedStr); + jsonObject = new JSONObject(myEnumClass); + expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + @Test + public void jsonObjectFromEnumWithNames() { + /** + * To serialize an enum by its set of allowed values, use getNames() + * and the the JSONObject Object with names constructor. + */ + String [] names; + String expectedStr; + JSONObject jsonObject; + JSONObject finalJsonObject; + JSONObject expectedJsonObject; + + expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; + MyEnum myEnum = MyEnum.VAL1; + names = JSONObject.getNames(myEnum); + // The values will be MyEnmField fields, so need to convert back to string for comparison + jsonObject = new JSONObject(myEnum, names); + finalJsonObject = new JSONObject(jsonObject.toString()); + expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); + + expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; + MyEnumField myEnumField = MyEnumField.VAL3; + names = JSONObject.getNames(myEnumField); + // The values will be MyEnmField fields, so need to convert back to string for comparison + jsonObject = new JSONObject(myEnumField, names); + finalJsonObject = new JSONObject(jsonObject.toString()); + expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); + } + @Test + public void enumPut() { + /** + * To serialize by assigned value, use the put() methods. The value + * will be stored as a enum type. + */ + String expectedFinalStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL1\"}"; + JSONObject jsonObject = new JSONObject(); + MyEnum myEnum = MyEnum.VAL2; + jsonObject.put("myEnum", myEnum); + assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonObject.get("myEnum"))); + assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonObject.opt("myEnum"))); + MyEnumField myEnumField = MyEnumField.VAL1; + jsonObject.putOnce("myEnumField", myEnumField); + assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonObject.get("myEnumField"))); + assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonObject.opt("myEnumField"))); + JSONObject finalJsonObject = new JSONObject(jsonObject.toString()); + JSONObject expectedFinalJsonObject = new JSONObject(expectedFinalStr); + Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedFinalJsonObject); + + JSONArray jsonArray = new JSONArray(); + jsonArray.put(myEnum); + jsonArray.put(1, myEnumField); + assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonArray.get(0))); + assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.opt(1))); + JSONArray expectedJsonArray = new JSONArray(); + expectedJsonArray.put(MyEnum.VAL2); + expectedJsonArray.put(MyEnumField.VAL1); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1))); + } + @Test public void enumValueToString() { + /** + * The default action of valueToString() is to call object.toString(). + * For enums, this means the assigned value will be returned as a string. + */ String expectedStr1 = "\"VAL1\""; String expectedStr2 = "\"VAL1\""; - String expectedStr3 = "\"org.json.junit.MyEnumClass@"; MyEnum myEnum = MyEnum.VAL1; MyEnumField myEnumField = MyEnumField.VAL1; MyEnumClass myEnumClass = new MyEnumClass(); - myEnumClass.setMyEnum(MyEnum.VAL1); - myEnumClass.setMyEnumField(MyEnumField.VAL1); String str1 = JSONObject.valueToString(myEnum); assertTrue("actual myEnum: "+str1+" expected: "+expectedStr1, @@ -73,8 +124,98 @@ public void enumValueToString() { String str2 = JSONObject.valueToString(myEnumField); assertTrue("actual myEnumField: "+str2+" expected: "+expectedStr2, str2.equals(expectedStr2)); + + /** + * However, an enum within another class will not be rendered + * unless that class overrides default toString() + */ + String expectedStr3 = "\"org.json.junit.MyEnumClass@"; + myEnumClass.setMyEnum(MyEnum.VAL1); + myEnumClass.setMyEnumField(MyEnumField.VAL1); String str3 = JSONObject.valueToString(myEnumClass); assertTrue("actual myEnumClass: "+str3+" expected: "+expectedStr3, str3.startsWith(expectedStr3)); } + + @Test + public void enumToString() { + /** + * In whatever form the enum was added to the JSONObject or JSONArray, + * json[Object|Array].toString should serialize it in a reasonable way. + */ + MyEnum myEnum = MyEnum.VAL2; + JSONObject jsonObject = new JSONObject(myEnum); + String expectedStr = "{}"; + assertTrue("myEnum toString() should be empty", expectedStr.equals(jsonObject.toString())); + + MyEnumField myEnumField = MyEnumField.VAL2; + jsonObject = new JSONObject(myEnumField); + expectedStr = "{\"value\":\"val 2\", \"intVal\":2}"; + JSONObject actualJsonObject = new JSONObject(jsonObject.toString()); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); + + expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; + MyEnumClass myEnumClass = new MyEnumClass(); + myEnumClass.setMyEnum(MyEnum.VAL1); + myEnumClass.setMyEnumField(MyEnumField.VAL3); + jsonObject = new JSONObject(myEnumClass); + actualJsonObject = new JSONObject(jsonObject.toString()); + expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); + + expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; + String [] names = JSONObject.getNames(myEnum); + jsonObject = new JSONObject(myEnum, names); + actualJsonObject = new JSONObject(jsonObject.toString()); + expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); + + expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; + names = JSONObject.getNames(myEnumField); + jsonObject = new JSONObject(myEnumField, names); + actualJsonObject = new JSONObject(jsonObject.toString()); + expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); + + expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}"; + jsonObject = new JSONObject(); + jsonObject.putOpt("myEnum", myEnum); + jsonObject.putOnce("myEnumField", myEnumField); + actualJsonObject = new JSONObject(jsonObject.toString()); + expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); + + expectedStr = "[\"VAL2\", \"VAL2\"]"; + JSONArray jsonArray = new JSONArray(); + jsonArray.put(myEnum); + jsonArray.put(1, myEnumField); + JSONArray actualJsonArray = new JSONArray(jsonArray.toString()); + JSONArray expectedJsonArray = new JSONArray(expectedStr); + Util.compareActualVsExpectedJsonArrays(actualJsonArray, expectedJsonArray); + } + + public void wrap() { + /** + * Wrap should handle enums exactly the same way as the JSONObject(Object) + * constructor. + */ + MyEnum myEnum = MyEnum.VAL2; + JSONObject jsonObject = (JSONObject)JSONObject.wrap(myEnum); + assertTrue("simple enum has no getters", jsonObject.length() == 0); + + String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}"; + MyEnumField myEnumField = MyEnumField.VAL2; + jsonObject = (JSONObject)JSONObject.wrap(myEnumField); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; + MyEnumClass myEnumClass = new MyEnumClass(); + myEnumClass.setMyEnum(MyEnum.VAL1); + myEnumClass.setMyEnumField(MyEnumField.VAL3); + jsonObject = (JSONObject)JSONObject.wrap(myEnumClass); + expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } } From 56aa2f86079738f0945abb5728d0954a2b9904bc Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 7 Jun 2015 22:22:42 -0500 Subject: [PATCH 122/315] ongoing unit test improvement --- JSONObjectTest.java | 93 ++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 48 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index e044c8cbf..827049e78 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -2,10 +2,12 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; + import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; + import org.json.*; import org.junit.*; @@ -416,10 +418,7 @@ public void jsonObjectValues() { jsonObjectInner.get("myKey").equals("myVal")); } - // improving unit tests left off here - @Test - public void stringToValueNumbersTest() { // Check if library handles large or high precision numbers correctly assertTrue( "0.2 should be a Double!", @@ -446,12 +445,12 @@ public void stringToValueNumbersTest() { JSONObject.stringToValue(str).equals("9223372036854775808")); } - @Test /** * This test documents numeric values which could be numerically * handled as BigDecimal or BigInteger. It helps determine what outputs * will change if those types are supported. */ + @Test public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() { // Valid JSON Numbers, probably should return BigDecimal or BigInteger objects String str = @@ -605,8 +604,16 @@ public void jsonObjectNonAndWrongValues() { exceptionCount == tryCount); } + /** + * The purpose for the static method getNames() methods are not clear. + * This method is not called from within JSON-Java. Most likely + * uses are to prep names arrays for: + * JSONObject(JSONObject jo, String[] names) + * JSONObject(Object object, String names[]), + */ @Test public void jsonObjectNames() { + JSONObject jsonObject; // getNames() from null JSONObject assertTrue("null names from null Object", @@ -616,11 +623,17 @@ public void jsonObjectNames() { assertTrue("null names from Object with no fields", null == JSONObject.getNames(new MyJsonString())); + // getNames from new JSONOjbect + jsonObject = new JSONObject(); + String [] names = JSONObject.getNames(jsonObject); + assertTrue("names should be null", names == null); + + // getNames() from empty JSONObject String emptyStr = "{}"; - JSONObject emptyJsonObject = new JSONObject(emptyStr); + jsonObject = new JSONObject(emptyStr); assertTrue("empty JSONObject should have null names", - null == JSONObject.getNames(emptyJsonObject)); + null == JSONObject.getNames(jsonObject)); // getNames() from JSONObject String str = @@ -630,9 +643,28 @@ public void jsonObjectNames() { "\"stringKey\":\"hello world!\","+ "}"; String [] expectedNames = {"trueKey", "falseKey", "stringKey"}; - JSONObject jsonObject = new JSONObject(str); - String [] names = JSONObject.getNames(jsonObject); + jsonObject = new JSONObject(str); + names = JSONObject.getNames(jsonObject); Util.compareActualVsExpectedStringArrays(names, expectedNames); + + /** + * getNames() from an enum with properties has an interesting result. + * It returns the enum values, not the selected enum properties + */ + MyEnumField myEnumField = MyEnumField.VAL1; + String[] enumExpectedNames = {"VAL1", "VAL2", "VAL3"}; + names = JSONObject.getNames(myEnumField); + Util.compareActualVsExpectedStringArrays(names, enumExpectedNames); + + /** + * A bean is also an object. But in order to test the static + * method getNames(), this particular bean needs some public + * data members, which have been added to the class. + */ + JSONObjectTest jsonObjectTest = new JSONObjectTest(); + String [] jsonObjectTestExpectedNames = {"publicString", "publicInt"}; + names = JSONObject.getNames(jsonObjectTest); + Util.compareActualVsExpectedStringArrays(names, jsonObjectTestExpectedNames); } @Test @@ -668,19 +700,6 @@ public void jsonObjectNamesToJsonAray() { Util.compareActualVsExpectedStringArrays(names, expectedNames); } - @Test - public void objectNames() { - /** - * A bean is also an object. But in order to test the static - * method getNames(), this particular bean needs some public - * data members, which have been added to the class. - */ - JSONObjectTest jsonObjectTest = new JSONObjectTest(); - String [] expectedNames = {"publicString", "publicInt"}; - String [] names = JSONObject.getNames(jsonObjectTest); - Util.compareActualVsExpectedStringArrays(names, expectedNames); - } - @Test public void jsonObjectIncrement() { String str = @@ -693,8 +712,6 @@ public void jsonObjectIncrement() { "\"keyInt\":3,"+ "\"keyLong\":9999999993,"+ "\"keyDouble\":3.1,"+ - // TODO: not sure if this will work on other platforms - // Should work the same way on any platform! @see https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 // This is the effect of a float to double conversion and is inherent to the shortcomings of the IEEE 754 format, when // converting 32-bit into double-precision 64-bit. @@ -773,26 +790,6 @@ public void jsonObjectIncrement() { } - @Test - public void emptyJsonObjectNamesToArray() { - JSONObject jsonObject = new JSONObject(); - String [] names = JSONObject.getNames(jsonObject); - assertTrue("names should be null", names == null); - } - - @Test - public void jsonObjectNamesToArray() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "}"; - String [] expectedNames = {"trueKey", "falseKey", "stringKey"}; - JSONObject jsonObject = new JSONObject(str); - String [] names = JSONObject.getNames(jsonObject); - Util.compareActualVsExpectedStringArrays(names, expectedNames); - } @Test public void jsonObjectNumberToString() { @@ -907,6 +904,7 @@ public void jsonObjectToString() { } @Test + @SuppressWarnings("unchecked") public void jsonObjectToStringSuppressWarningOnCastToMap() { JSONObject jsonObject = new JSONObject(); Map map = new HashMap(); @@ -921,8 +919,7 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { * Can't do a Util compare because although they look the same * in the debugger, one is a map and the other is a JSONObject. */ - // TODO: fix warnings - map = (Map)jsonObject.get("key"); + map = (Map)jsonObject.get("key"); JSONObject mapJsonObject = expectedJsonObject.getJSONObject("key"); assertTrue("value size should be equal", map.size() == mapJsonObject.length() && map.size() == 1); @@ -934,6 +931,7 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { } @Test + @SuppressWarnings("unchecked") public void jsonObjectToStringSuppressWarningOnCastToCollection() { JSONObject jsonObject = new JSONObject(); Collection collection = new ArrayList(); @@ -950,12 +948,11 @@ public void jsonObjectToStringSuppressWarningOnCastToCollection() { assertTrue("keys should be equal", jsonObject.keySet().iterator().next().equals( expectedJsonObject.keySet().iterator().next())); - // TODO: fix warnings - collection = (Collection)jsonObject.get("key"); + collection = (Collection)jsonObject.get("key"); JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); assertTrue("value size should be equal", collection.size() == jsonArray.length()); - Iterator it = collection.iterator(); + Iterator it = collection.iterator(); for (int i = 0; i < collection.size(); ++i) { assertTrue("items should be equal for index: "+i, jsonArray.get(i).toString().equals(it.next().toString())); From 44f98e6a132a2148545b330e8cd3abd4658d182c Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 13 Jun 2015 13:30:06 -0500 Subject: [PATCH 123/315] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index bd36d8549..80e73effd 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,16 @@ More coverage is needed, but more importantly, improvements to test quality is n You will need the following libraries for testing: Test harness: http://junit.org
Coverage: http://www.eclemma.org/
+JsonPath: https://github.com/jayway/JsonPath Mockery: https://github.com/mockito/mockito +Include these libraries in your project: +JSON-Java.jar (make this jar of the files to be tested yourself) +hamcrest-core-1.3.jar (for Junit) +junit-4.12.jar +mockito-all-1.9.5.jar + + Eclipse is the recommended development environment. Run individual tests or JunitTestSuite using *EclEmma Coverage*, or execute the TestRunner application directly.
From cb7b602f35aadca7a9fef910997784eda464a9a7 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 13 Jun 2015 14:53:43 -0500 Subject: [PATCH 124/315] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 80e73effd..b683c1376 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ More coverage is needed, but more importantly, improvements to test quality is n You will need the following libraries for testing: Test harness: http://junit.org
Coverage: http://www.eclemma.org/
-JsonPath: https://github.com/jayway/JsonPath Mockery: https://github.com/mockito/mockito Include these libraries in your project: From 0640856462f25623487435dac728d42ca12a09d6 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 17 Jun 2015 02:59:43 -0500 Subject: [PATCH 125/315] unexpected double behavior --- JSONObjectTest.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 827049e78..f7672a2e1 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -604,6 +604,33 @@ public void jsonObjectNonAndWrongValues() { exceptionCount == tryCount); } + @Test + public void unexpectedDoubleToIntConversion() { + /** + * This test documents an unexpected numeric behavior. + * A double that ends with .0 is parsed, serialized, then + * parsed again. On the second parse, it has become an int. + */ + String key30 = "key30"; + String key31 = "key31"; + JSONObject jsonObject = new JSONObject(); + jsonObject.put(key30, new Double(3.0)); + jsonObject.put(key31, new Double(3.1)); + + assertTrue("3.0 should remain a double", + jsonObject.getDouble(key30) == 3); + assertTrue("3.1 should remain a double", + jsonObject.getDouble(key31) == 3.1); + + // turns 3.0 into 3. + String serializedString = jsonObject.toString(); + JSONObject deserialized = new JSONObject(serializedString); + assertTrue("3.0 is now an int", deserialized.get(key30) instanceof Integer); + assertTrue("3.0 can still be interpreted as a double", + deserialized.getDouble(key30) == 3.0); + assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1); + } + /** * The purpose for the static method getNames() methods are not clear. * This method is not called from within JSON-Java. Most likely From c5173e7cc3a7b6b8d736563e10f4fc94df6ce7d7 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 17 Jun 2015 20:09:31 -0500 Subject: [PATCH 126/315] ip --- JSONObjectTest.java | 84 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index f7672a2e1..e79117907 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -631,6 +631,90 @@ public void unexpectedDoubleToIntConversion() { assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1); } + @Test + public void bigNumberOperations() { + /** + * JSONObject tries to parse BigInteger as a bean, but it only has + * one getter, getLowestBitSet(). The value is lost and an unhelpful + * value is stored. This should be fixed. + */ + BigInteger bigInteger = new BigInteger("123456789012345678901234567890"); + JSONObject jsonObject = new JSONObject(bigInteger); + Object obj = jsonObject.get("lowestSetBit"); + assertTrue("JSONObject only has 1 value", jsonObject.length() == 1); + assertTrue("JSONObject parses BigInteger as the Integer lowestBitSet", + obj instanceof Integer); + assertTrue("this bigInteger lowestBitSet happens to be 1", + obj.equals(1)); + + /** + * JSONObject put(String, Object) method stores and serializesbigInt and bigDec + * correctly. Nothing needs to change. + */ + BigDecimal bigDecimal = new BigDecimal( + "123456789012345678901234567890.12345678901234567890123456789"); + jsonObject = new JSONObject(bigDecimal); + assertTrue("large bigDecimal is not stored", jsonObject.length() == 0); + + /** + * JSONObject put(String, Object) method stores and serializes + * bigInt and bigDec correctly. Nothing needs to change. + */ + jsonObject = new JSONObject(); + jsonObject.put("bigInt", bigInteger); + assertTrue("jsonObject.put() handles bigInt correctly", + jsonObject.get("bigInt").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 serializes bigDec correctly", + jsonObject.toString().equals( + "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + + /** + * There is no way to get bigInt or bigDec by type. + * This should be fixed. E.G. jsonObject.getBigInteger(key); + */ + + /** + * JSONObject.numberToString() works correctly, nothing to change. + */ + String str = JSONObject.numberToString(bigInteger); + assertTrue("numberToString() handles bigInteger correctly", + str.equals("123456789012345678901234567890")); + str = JSONObject.numberToString(bigDecimal); + assertTrue("numberToString() handles bigDecimal correctly", + str.equals("123456789012345678901234567890.12345678901234567890123456789")); + + /** + * JSONObject.stringToValue() turns bigInt into an accurate string, + * and rounds bigDec. This incorrect, but users may have come to + * expect this behavior. Change would be marginally better, but + * might inconvenience users. + */ + obj = JSONObject.stringToValue(bigInteger.toString()); + assertTrue("stringToValue() turns bigInteger string into string", + obj instanceof String); + obj = JSONObject.stringToValue(bigDecimal.toString()); + assertTrue("stringToValue() changes bigDecimal string", + !obj.toString().equals(bigDecimal.toString())); + + /** + * JSONObject.wrap() performs the advertised behavior, + * which is to turn Java classes into strings. + * Probably not a bug + */ + obj = JSONObject.wrap(bigInteger); + assertTrue("wrap() turns bigInt into a string", + obj.equals(bigInteger.toString())); + obj = JSONObject.wrap(bigDecimal); + assertTrue("wrap() turns bigDec into a string", + obj.equals(bigDecimal.toString())); + } + /** * The purpose for the static method getNames() methods are not clear. * This method is not called from within JSON-Java. Most likely From 8c1a0c47b7000bebdf8362f3d817d8b93aba84e8 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 17 Jun 2015 20:18:51 -0500 Subject: [PATCH 127/315] fixed test comment --- JSONObjectTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index e79117907..5b98aae06 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -648,8 +648,9 @@ public void bigNumberOperations() { obj.equals(1)); /** - * JSONObject put(String, Object) method stores and serializesbigInt and bigDec - * correctly. Nothing needs to change. + * JSONObject tries to parse BigDecimal as a bean, but it has + * no getters, The value is lost and no value is stored. + * This should be fixed. */ BigDecimal bigDecimal = new BigDecimal( "123456789012345678901234567890.12345678901234567890123456789"); From 6c48db010f06c63b13c99d92a2932e9379155047 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 20 Jun 2015 23:35:48 -0500 Subject: [PATCH 128/315] bigInt and bigDec behavior --- JSONObjectTest.java | 108 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 17 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 5b98aae06..ccef775ed 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -20,7 +20,7 @@ class MyJsonString implements JSONString { public String toJSONString() { return "my string"; } -}; +} interface MyBean { public Integer getIntKey(); @@ -32,6 +32,11 @@ interface MyBean { public StringReader getStringReaderKey(); }; +interface MyBigNumberBean { + public BigInteger getBigInteger(); + public BigDecimal getBigDecimal(); +} + /** * JSONObject, along with JSONArray, are the central classes of the reference app. * All of the other classes interact with it and JSON functionality would be @@ -659,26 +664,32 @@ public void bigNumberOperations() { /** * JSONObject put(String, Object) method stores and serializes - * bigInt and bigDec correctly. Nothing needs to change. + * 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 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 serializes bigDec correctly", jsonObject.toString().equals( - "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); - - /** - * There is no way to get bigInt or bigDec by type. - * This should be fixed. E.G. jsonObject.getBigInteger(key); - */ + "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); /** * JSONObject.numberToString() works correctly, nothing to change. @@ -704,16 +715,79 @@ public void bigNumberOperations() { !obj.toString().equals(bigDecimal.toString())); /** - * JSONObject.wrap() performs the advertised behavior, - * which is to turn Java classes into strings. - * Probably not a bug + * wrap() vs put() big number behavior changes serialization + * This is a bug. + * TODO: Updated expected wrap() test results after JSONObject is updated. */ + // bigInt map ctor + Map map = new HashMap(); + map.put("bigInt", bigInteger); + jsonObject = new JSONObject(map); + String actualFromMapStr = jsonObject.toString(); + assertTrue("bigInt in map (or array or bean) is a string", + actualFromMapStr.equals( + "{\"bigInt\":\"123456789012345678901234567890\"}")); + // bigInt put + jsonObject = new JSONObject(); + jsonObject.put("bigInt", bigInteger); + String actualFromPutStr = jsonObject.toString(); + assertTrue("bigInt from put is a number", + actualFromPutStr.equals( + "{\"bigInt\":123456789012345678901234567890}")); + // bigDec map ctor + map = new HashMap(); + map.put("bigDec", bigDecimal); + jsonObject = new JSONObject(map); + actualFromMapStr = jsonObject.toString(); + assertTrue("bigDec in map (or array or bean) is a string", + actualFromMapStr.equals( + "{\"bigDec\":\"123456789012345678901234567890.12345678901234567890123456789\"}")); + // bigDec put + jsonObject = new JSONObject(); + jsonObject.put("bigDec", bigDecimal); + actualFromPutStr = jsonObject.toString(); + assertTrue("bigDec from put is a number", + actualFromPutStr.equals( + "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + // bigInt,bigDec put + JSONArray jsonArray = new JSONArray(); + jsonArray.put(bigInteger); + jsonArray.put(bigDecimal); + actualFromPutStr = jsonArray.toString(); + assertTrue("bigInt, bigDec from put is a number", + actualFromPutStr.equals( + "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); + // bigInt,bigDec list ctor + List list = new ArrayList(); + list.add(bigInteger); + list.add(bigDecimal); + jsonArray = new JSONArray(list); + String actualFromListStr = jsonArray.toString(); + assertTrue("bigInt, bigDec in list is a string", + actualFromListStr.equals( + "[\"123456789012345678901234567890\",\"123456789012345678901234567890.12345678901234567890123456789\"]")); + // bigInt bean ctor + MyBigNumberBean myBigNumberBean = mock(MyBigNumberBean.class); + when(myBigNumberBean.getBigInteger()).thenReturn(new BigInteger("123456789012345678901234567890")); + jsonObject = new JSONObject(myBigNumberBean); + String actualFromBeanStr = jsonObject.toString(); + // can't do a full string compare because mockery adds an extra key/value + assertTrue("bigInt from bean ctor is a string", + actualFromBeanStr.contains("\"123456789012345678901234567890\"")); + // bigDec bean ctor + myBigNumberBean = mock(MyBigNumberBean.class); + when(myBigNumberBean.getBigDecimal()).thenReturn(new BigDecimal("123456789012345678901234567890.12345678901234567890123456789")); + jsonObject = new JSONObject(myBigNumberBean); + actualFromBeanStr = jsonObject.toString(); + // can't do a full string compare because mockery adds an extra key/value + assertTrue("bigDec from bean ctor is a string", + actualFromBeanStr.contains("\"123456789012345678901234567890.12345678901234567890123456789\"")); + // bigInt,bigDec wrap() obj = JSONObject.wrap(bigInteger); - assertTrue("wrap() turns bigInt into a string", - obj.equals(bigInteger.toString())); + assertTrue("wrap() returns string",obj.equals(bigInteger.toString())); obj = JSONObject.wrap(bigDecimal); - assertTrue("wrap() turns bigDec into a string", - obj.equals(bigDecimal.toString())); + assertTrue("wrap() returns string",obj.equals(bigDecimal.toString())); + } /** @@ -1133,8 +1207,8 @@ public void wrapObject() { * is recognized as being a Java package class. */ Object bdWrap = JSONObject.wrap(BigDecimal.ONE); - assertTrue("BigDecimal.ONE currently evaluates to string", - bdWrap.equals("1")); + assertTrue("BigDecimal.ONE evaluates to string", + bdWrap.equals(BigDecimal.ONE.toString())); // wrap JSONObject returns JSONObject String jsonObjectStr = From 355e8323377c1a9104fc8e122573d57f617dbe2e Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 6 Jul 2015 22:31:48 -0500 Subject: [PATCH 129/315] latest --- JSONObjectTest.java | 55 +++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index ccef775ed..a66131299 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -637,6 +637,10 @@ public void unexpectedDoubleToIntConversion() { } @Test + /** + * Important behaviors of big numbers. Includes both JSONObject + * and JSONArray tests + */ public void bigNumberOperations() { /** * JSONObject tries to parse BigInteger as a bean, but it only has @@ -691,6 +695,9 @@ public void bigNumberOperations() { jsonObject.toString().equals( "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + JSONArray jsonArray = new JSONArray(); + + /** * JSONObject.numberToString() works correctly, nothing to change. */ @@ -715,9 +722,7 @@ public void bigNumberOperations() { !obj.toString().equals(bigDecimal.toString())); /** - * wrap() vs put() big number behavior changes serialization - * This is a bug. - * TODO: Updated expected wrap() test results after JSONObject is updated. + * wrap() vs put() big number behavior is now the same. */ // bigInt map ctor Map map = new HashMap(); @@ -726,7 +731,7 @@ public void bigNumberOperations() { String actualFromMapStr = jsonObject.toString(); assertTrue("bigInt in map (or array or bean) is a string", actualFromMapStr.equals( - "{\"bigInt\":\"123456789012345678901234567890\"}")); + "{\"bigInt\":123456789012345678901234567890}")); // bigInt put jsonObject = new JSONObject(); jsonObject.put("bigInt", bigInteger); @@ -739,9 +744,9 @@ public void bigNumberOperations() { map.put("bigDec", bigDecimal); jsonObject = new JSONObject(map); actualFromMapStr = jsonObject.toString(); - assertTrue("bigDec in map (or array or bean) is a string", + assertTrue("bigDec in map (or array or bean) is a bigDec", actualFromMapStr.equals( - "{\"bigDec\":\"123456789012345678901234567890.12345678901234567890123456789\"}")); + "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); // bigDec put jsonObject = new JSONObject(); jsonObject.put("bigDec", bigDecimal); @@ -750,43 +755,59 @@ public void bigNumberOperations() { actualFromPutStr.equals( "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); // bigInt,bigDec put - JSONArray jsonArray = new JSONArray(); + jsonArray = new JSONArray(); jsonArray.put(bigInteger); jsonArray.put(bigDecimal); actualFromPutStr = jsonArray.toString(); assertTrue("bigInt, bigDec from put is a number", actualFromPutStr.equals( "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); + assertTrue("getBigInt is bigInt", jsonArray.getBigInteger(0).equals(bigInteger)); + assertTrue("getBigDec is bigDec", jsonArray.getBigDecimal(1).equals(bigDecimal)); + assertTrue("optBigInt is bigInt", jsonArray.optBigInteger(0, BigInteger.ONE).equals(bigInteger)); + assertTrue("optBigDec is bigDec", jsonArray.optBigDecimal(1, BigDecimal.ONE).equals(bigDecimal)); + jsonArray.put(Boolean.TRUE); + try { + jsonArray.getBigInteger(2); + assertTrue("should not be able to get big int", false); + } catch (Exception ignored) {} + try { + jsonArray.getBigDecimal(2); + assertTrue("should not be able to get big dec", false); + } 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)); + // bigInt,bigDec list ctor List list = new ArrayList(); list.add(bigInteger); list.add(bigDecimal); jsonArray = new JSONArray(list); String actualFromListStr = jsonArray.toString(); - assertTrue("bigInt, bigDec in list is a string", + assertTrue("bigInt, bigDec in list is a bigInt, bigDec", actualFromListStr.equals( - "[\"123456789012345678901234567890\",\"123456789012345678901234567890.12345678901234567890123456789\"]")); + "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); // bigInt bean ctor MyBigNumberBean myBigNumberBean = mock(MyBigNumberBean.class); when(myBigNumberBean.getBigInteger()).thenReturn(new BigInteger("123456789012345678901234567890")); jsonObject = new JSONObject(myBigNumberBean); String actualFromBeanStr = jsonObject.toString(); // can't do a full string compare because mockery adds an extra key/value - assertTrue("bigInt from bean ctor is a string", - actualFromBeanStr.contains("\"123456789012345678901234567890\"")); + assertTrue("bigInt from bean ctor is a bigInt", + actualFromBeanStr.contains("123456789012345678901234567890")); // bigDec bean ctor myBigNumberBean = mock(MyBigNumberBean.class); when(myBigNumberBean.getBigDecimal()).thenReturn(new BigDecimal("123456789012345678901234567890.12345678901234567890123456789")); jsonObject = new JSONObject(myBigNumberBean); actualFromBeanStr = jsonObject.toString(); // can't do a full string compare because mockery adds an extra key/value - assertTrue("bigDec from bean ctor is a string", - actualFromBeanStr.contains("\"123456789012345678901234567890.12345678901234567890123456789\"")); + assertTrue("bigDec from bean ctor is a bigDec", + actualFromBeanStr.contains("123456789012345678901234567890.12345678901234567890123456789")); // bigInt,bigDec wrap() obj = JSONObject.wrap(bigInteger); - assertTrue("wrap() returns string",obj.equals(bigInteger.toString())); + assertTrue("wrap() returns big num",obj.equals(bigInteger)); obj = JSONObject.wrap(bigDecimal); - assertTrue("wrap() returns string",obj.equals(bigDecimal.toString())); + assertTrue("wrap() returns string",obj.equals(bigDecimal)); } @@ -1207,8 +1228,8 @@ public void wrapObject() { * is recognized as being a Java package class. */ Object bdWrap = JSONObject.wrap(BigDecimal.ONE); - assertTrue("BigDecimal.ONE evaluates to string", - bdWrap.equals(BigDecimal.ONE.toString())); + assertTrue("BigDecimal.ONE evaluates to ONE", + bdWrap.equals(BigDecimal.ONE)); // wrap JSONObject returns JSONObject String jsonObjectStr = From e056fc0881d4fcc7dca2aaeb12a3b029998ae7de Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 6 Jul 2015 22:33:52 -0500 Subject: [PATCH 130/315] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b683c1376..f05db8187 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # JSON-Java-unit-test +*Tests are broken until bigint/bigdec changes are committed in JsonJava*
+ Unit tests to validate the JSON-Java GitHub project code
https://github.com/douglascrockford/JSON-java
From 3de0a0a70e96ac3ba0c82cd19553db0515eda57f Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 7 Jul 2015 23:13:59 -0500 Subject: [PATCH 131/315] Update README.md --- README.md | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f05db8187..fda6f2ecc 100644 --- a/README.md +++ b/README.md @@ -8,26 +8,24 @@ https://github.com/douglascrockford/JSON-java
*These tests are a work in progress. Help from interested developers is welcome.*
More coverage is needed, but more importantly, improvements to test quality is needed.
+Eclipse is the recommended development environment. +Run individual tests or JunitTestSuite using *EclEmma Coverage*, or execute the TestRunner application directly.
+ You will need the following libraries for testing: -Test harness: http://junit.org
-Coverage: http://www.eclemma.org/
+Test harness: http://junit.org
+* hamcrest-core-1.3.jar (for Junit) +* junit-4.12.jar Mockery: https://github.com/mockito/mockito - -Include these libraries in your project: +* mockito-all-1.9.5.jar +Coverage: http://www.eclemma.org/ (just install the latest in Eclipse)
JSON-Java.jar (make this jar of the files to be tested yourself) -hamcrest-core-1.3.jar (for Junit) -junit-4.12.jar -mockito-all-1.9.5.jar - - -Eclipse is the recommended development environment. -Run individual tests or JunitTestSuite using *EclEmma Coverage*, or execute the TestRunner application directly.
+*Conventions* Test filenames should consist of the name of the module being tested, with the suffix "Test". For example, Cookie.java is tested by CookieTest.java. When adding a new unit test, don't forget to update JunitTestSuite.java. -The fundamental issues with JSON-Java testing are: +*The fundamental issues with JSON-Java testing are:* * JSONObjects are unordered, making simple string comparison ineffective. * Comparisons via **equals()** is not currently supported. Neither JSONArray nor JSONObject overrride hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful. * Access to the JSONArray and JSONObject internal containers for comparison is not currently available. From 99927c55162beff1869f377e8ffb10dbf965303a Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 7 Jul 2015 23:15:06 -0500 Subject: [PATCH 132/315] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fda6f2ecc..b7be9be89 100644 --- a/README.md +++ b/README.md @@ -8,19 +8,19 @@ https://github.com/douglascrockford/JSON-java
*These tests are a work in progress. Help from interested developers is welcome.*
More coverage is needed, but more importantly, improvements to test quality is needed.
-Eclipse is the recommended development environment. +Eclipse is the recommended development environment.
Run individual tests or JunitTestSuite using *EclEmma Coverage*, or execute the TestRunner application directly.
-You will need the following libraries for testing: +You will need the following libraries for testing:
Test harness: http://junit.org
* hamcrest-core-1.3.jar (for Junit) * junit-4.12.jar -Mockery: https://github.com/mockito/mockito +Mockery: https://github.com/mockito/mockito
* mockito-all-1.9.5.jar Coverage: http://www.eclemma.org/ (just install the latest in Eclipse)
-JSON-Java.jar (make this jar of the files to be tested yourself) +JSON-Java.jar (make this jar of the files to be tested yourself)
-*Conventions* +*Conventions*
Test filenames should consist of the name of the module being tested, with the suffix "Test". For example, Cookie.java is tested by CookieTest.java. When adding a new unit test, don't forget to update JunitTestSuite.java. From 6dd85ad5b6bc04031d8fe8c42402c27402fe5a5b Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 7 Jul 2015 23:16:18 -0500 Subject: [PATCH 133/315] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b7be9be89..9c27a4f94 100644 --- a/README.md +++ b/README.md @@ -20,17 +20,17 @@ Mockery: https://github.com/mockito/mockito
Coverage: http://www.eclemma.org/ (just install the latest in Eclipse)
JSON-Java.jar (make this jar of the files to be tested yourself)
-*Conventions*
+Conventions
Test filenames should consist of the name of the module being tested, with the suffix "Test". For example, Cookie.java is tested by CookieTest.java. When adding a new unit test, don't forget to update JunitTestSuite.java. -*The fundamental issues with JSON-Java testing are:* +The fundamental issues with JSON-Java testing are:
* JSONObjects are unordered, making simple string comparison ineffective. * Comparisons via **equals()** is not currently supported. Neither JSONArray nor JSONObject overrride hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful. * Access to the JSONArray and JSONObject internal containers for comparison is not currently available. -General issues with unit testing are: +General issues with unit testing are:
* Just writing tests to make coverage goals tends to result in poor tests. * Unit tests are a form of documentation - how a given method actually works is demonstrated by the test. So for a code reviewer or future developer looking at code a good test helps explain how a function is supposed to work according to the original author. This can be difficult if you are not the original developer. * It is difficult to evaluate unit tests in a vacuum. You also need to see the code being tested to understand if a test is good. From 7ed1f78f5f6347ac0102d858eba9eb22c9530bf4 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 7 Jul 2015 23:18:31 -0500 Subject: [PATCH 134/315] Update README.md --- README.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9c27a4f94..758ce72ce 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,18 @@ https://github.com/douglascrockford/JSON-java
More coverage is needed, but more importantly, improvements to test quality is needed.
Eclipse is the recommended development environment.
-Run individual tests or JunitTestSuite using *EclEmma Coverage*, or execute the TestRunner application directly.
+Run individual tests or JunitTestSuite using EclEmma Coverage, or execute the TestRunner application directly.
-You will need the following libraries for testing:
+**You will need the following libraries for testing:**
Test harness: http://junit.org
-* hamcrest-core-1.3.jar (for Junit) -* junit-4.12.jar +* hamcrest-core-1.3.jar (for Junit)
+* junit-4.12.jar
+ Mockery: https://github.com/mockito/mockito
-* mockito-all-1.9.5.jar +* mockito-all-1.9.5.jar
+ Coverage: http://www.eclemma.org/ (just install the latest in Eclipse)
+ JSON-Java.jar (make this jar of the files to be tested yourself)
Conventions
From 27b22b472471f4b95863ec947fc8d6dcf7564729 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 7 Jul 2015 23:19:37 -0500 Subject: [PATCH 135/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 758ce72ce..706d80e8d 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ https://github.com/douglascrockford/JSON-java
More coverage is needed, but more importantly, improvements to test quality is needed.
Eclipse is the recommended development environment.
-Run individual tests or JunitTestSuite using EclEmma Coverage, or execute the TestRunner application directly.
+Run individual tests or JunitTestSuite using EclEmma Coverage, or execute the **TestRunner** application directly.
**You will need the following libraries for testing:**
Test harness: http://junit.org
From ab143af1463383ffffa6699850dd60623f37e4ad Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 7 Jul 2015 23:20:55 -0500 Subject: [PATCH 136/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 706d80e8d..bd833414d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # JSON-Java-unit-test -*Tests are broken until bigint/bigdec changes are committed in JsonJava*
+**Tests are broken until bigint/bigdec changes are committed in JsonJava** [ETA for go-nogo decision is 9 Jul 2015]
Unit tests to validate the JSON-Java GitHub project code
https://github.com/douglascrockford/JSON-java
From 6cca29202034a9c9fa518b6ac9feffffae00c6e2 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 17 Jul 2015 21:55:50 -0500 Subject: [PATCH 137/315] investigate XML.toString() behavior with null JSONObject values --- JSONObjectTest.java | 83 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index a66131299..85576784b 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -1469,6 +1469,89 @@ public void equals() { assertTrue("Same JSONObject should be equal to itself", aJsonObject.equals(aJsonObject)); } + + @Test + public void jsonObjectNullOperations() { + /** + * The Javadoc for JSONObject.NULL states: + * "JSONObject.NULL is equivalent to the value that JavaScript calls null, + * whilst Java's null is equivalent to the value that JavaScript calls + * undefined." + * + * Standard ECMA-262 6th Edition / June 2015 (included to help explain the javadoc): + * undefined value: primitive value used when a variable has not been assigned a value + * Undefined type: type whose sole value is the undefined value + * null value: primitive value that represents the intentional absence of any object value + * Null type: type whose sole value is the null value + * Java SE8 language spec (included to help explain the javadoc): + * The Kinds of Types and Values ... + * There is also a special null type, the type of the expression null, which has no name. + * Because the null type has no name, it is impossible to declare a variable of the null + * type or to cast to the null type. The null reference is the only possible value of an + * expression of null type. The null reference can always be assigned or cast to any reference type. + * In practice, the programmer can ignore the null type and just pretend that null is merely + * a special literal that can be of any reference type. + * Extensible Markup Language (XML) 1.0 Fifth Edition / 26 November 2008 + * No mention of null + * ECMA-404 1st Edition / October 2013: + * JSON Text ... + * These are three literal name tokens: ... + * null + * + * There seems to be no best practice, it's all about what we want the code to do. + * In the code we see that JSONObject.NULL is tranformed into null + */ + + // add JSONObject.NULL then convert to string in the manner of XML.toString() + JSONObject jsonObjectJONull = new JSONObject(); + Object obj = JSONObject.NULL; + jsonObjectJONull.put("key", obj); + Object value = jsonObjectJONull.opt("key"); + assertTrue("opt() JSONObject.NULL should find JSONObject.NULL", obj.equals(value)); + value = jsonObjectJONull.get("key"); + assertTrue("get() JSONObject.NULL should find JSONObject.NULL", obj.equals(value)); + if (value == null) { + value = ""; + } + String string = value instanceof String ? (String)value : null; + assertTrue("XML toString() should convert JSONObject.NULL to null", string == null); + + // now try it with null + JSONObject jsonObjectNull = new JSONObject(); + obj = null; + jsonObjectNull.put("key", obj); + value = jsonObjectNull.opt("key"); + assertTrue("opt() null should find null", value == null);; + if (value == null) { + value = ""; + } + string = value instanceof String ? (String)value : null; + assertTrue("should convert null to empty string", "".equals(string)); + try { + value = jsonObjectNull.get("key"); + assertTrue("get() null should throw exception", false); + } catch (Exception ignored) {} + + /** + * XML.toString() then goes on to do something with the value + * if the key val is "content", then value.toString() will be + * called. This will evaluate to "null" for JSONObject.NULL, + * and the empty string for null. + * But if the key is anything else, then JSONObject.NULL will be emitted as null + * and null will be emitted as "" + */ + String sJONull = XML.toString(jsonObjectJONull); + assertTrue("JSONObject.NULL should emit a null value", "null".equals(sJONull)); + String sNull = XML.toString(jsonObjectNull); + assertTrue("null should emit an empty string", "".equals(sNull)); + } + + /** + * + */ + private void nullOperations(Object value) { + } + } From 0056b1af94e4e3a128280d70d8c185377c601e96 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 17 Jul 2015 21:58:42 -0500 Subject: [PATCH 138/315] investigate XML.toString() behavior with null JSONObject values --- JSONObjectTest.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 85576784b..69b6f0cae 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -1545,13 +1545,6 @@ public void jsonObjectNullOperations() { String sNull = XML.toString(jsonObjectNull); assertTrue("null should emit an empty string", "".equals(sNull)); } - - /** - * - */ - private void nullOperations(Object value) { - } - } From b06182cb734eb3acbaedc0af2e2421adfe3559d9 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 17 Jul 2015 22:01:52 -0500 Subject: [PATCH 139/315] investigate XML.toString() behavior with null JSONObject values --- JSONObjectTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 69b6f0cae..7efd2d864 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -1498,8 +1498,7 @@ public void jsonObjectNullOperations() { * These are three literal name tokens: ... * null * - * There seems to be no best practice, it's all about what we want the code to do. - * In the code we see that JSONObject.NULL is tranformed into null + * There seems to be no best practice to follow, it's all about what we want the code to do. */ // add JSONObject.NULL then convert to string in the manner of XML.toString() From 16fa69c0f60d13c371aa0fd42d4bd493b503ff61 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 17 Jul 2015 22:04:01 -0500 Subject: [PATCH 140/315] investigate XML.toString() behavior with null JSONObject values --- JSONObjectTest.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 7efd2d864..b818a9e8d 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -1498,7 +1498,8 @@ public void jsonObjectNullOperations() { * These are three literal name tokens: ... * null * - * There seems to be no best practice to follow, it's all about what we want the code to do. + * There seems to be no best practice to follow, it's all about what we + * want the code to do. */ // add JSONObject.NULL then convert to string in the manner of XML.toString() @@ -1506,14 +1507,17 @@ public void jsonObjectNullOperations() { Object obj = JSONObject.NULL; jsonObjectJONull.put("key", obj); Object value = jsonObjectJONull.opt("key"); - assertTrue("opt() JSONObject.NULL should find JSONObject.NULL", obj.equals(value)); + assertTrue("opt() JSONObject.NULL should find JSONObject.NULL", + obj.equals(value)); value = jsonObjectJONull.get("key"); - assertTrue("get() JSONObject.NULL should find JSONObject.NULL", obj.equals(value)); + assertTrue("get() JSONObject.NULL should find JSONObject.NULL", + obj.equals(value)); if (value == null) { value = ""; } String string = value instanceof String ? (String)value : null; - assertTrue("XML toString() should convert JSONObject.NULL to null", string == null); + assertTrue("XML toString() should convert JSONObject.NULL to null", + string == null); // now try it with null JSONObject jsonObjectNull = new JSONObject(); @@ -1536,11 +1540,12 @@ public void jsonObjectNullOperations() { * if the key val is "content", then value.toString() will be * called. This will evaluate to "null" for JSONObject.NULL, * and the empty string for null. - * But if the key is anything else, then JSONObject.NULL will be emitted as null - * and null will be emitted as "" + * But if the key is anything else, then JSONObject.NULL will be emitted + * as null and null will be emitted as "" */ String sJONull = XML.toString(jsonObjectJONull); - assertTrue("JSONObject.NULL should emit a null value", "null".equals(sJONull)); + assertTrue("JSONObject.NULL should emit a null value", + "null".equals(sJONull)); String sNull = XML.toString(jsonObjectNull); assertTrue("null should emit an empty string", "".equals(sNull)); } From 5eadebb797c761d58ebf73d337566cd3745995e8 Mon Sep 17 00:00:00 2001 From: JaXt0r Date: Sun, 19 Jul 2015 14:24:06 +0200 Subject: [PATCH 141/315] Showing issue of illegal node names with possible underscore-replacement. (Will currently assterted to an Exception). --- XMLTest.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/XMLTest.java b/XMLTest.java index 0c86eebc1..1c04c1b37 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -294,4 +294,25 @@ public void shouldHandleNestedArraytoString() { JSONObject expectedJsonObject = XML.toJSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } + + + /** + * Illegal node-names must be converted to legal XML-node-names. + * The given example shows 2 nodes which are valid for JSON, but not for XML. + * Therefore illegal arguments should be converted to e.g. an underscore (_). + * + */ + @Test + public void shouldHandleIllegalJSONNodeNames() + { + JSONObject inputJSON = new JSONObject(); + inputJSON.append("123IllegalNode", "someValue1"); + inputJSON.append("Illegal@node", "someValue2"); + + String result = XML.toString(inputJSON); + + String expected = "<___IllegalNode>someValue1someValue3"; + + assertEquals(expected, result); + } } From 2c026eb5f8fdf10285cb4505d95b6dce0d99143d Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 19 Jul 2015 08:56:20 -0500 Subject: [PATCH 142/315] Fixed test shouldHandleIllegalJSONNodeNames --- XMLTest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/XMLTest.java b/XMLTest.java index 1c04c1b37..ac1750837 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -311,7 +311,13 @@ public void shouldHandleIllegalJSONNodeNames() String result = XML.toString(inputJSON); - String expected = "<___IllegalNode>someValue1someValue3"; + /** + * This is invalid XML. Names should not begin with digits or contain + * certain values, including '@'. One possible solution is to replace + * illegal chars with '_', in which case the expected output would be: + * <___IllegalNode>someValue1someValue2 + */ + String expected = "<123IllegalNode>someValue1someValue2"; assertEquals(expected, result); } From f48b6439f682f9941a3bf0516a3ece472bf2f696 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 19 Jul 2015 09:02:27 -0500 Subject: [PATCH 143/315] manual merge of pull request #18 --- XMLTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/XMLTest.java b/XMLTest.java index ac1750837..8a564072d 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -321,4 +321,20 @@ public void shouldHandleIllegalJSONNodeNames() assertEquals(expected, result); } + + @Test + public void shouldHandleNullNodeValue() + { + JSONObject inputJSON = new JSONObject(); + inputJSON.put("nullValue", JSONObject.NULL); + // This is a possible preferred result + String expectedXML = ""; + String actualXML = "null"; + /** + * This is the current behavior. JSONObject.NULL is emitted as + * the string, "null". + */ + String resultXML = XML.toString(inputJSON); + assertEquals(actualXML, resultXML); + } } From b9c6f335ee832e1db56a3db4a84657323b82fc2f Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 19 Jul 2015 09:04:10 -0500 Subject: [PATCH 144/315] fixed comment location --- XMLTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/XMLTest.java b/XMLTest.java index 8a564072d..6dc38420e 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -329,11 +329,11 @@ public void shouldHandleNullNodeValue() inputJSON.put("nullValue", JSONObject.NULL); // This is a possible preferred result String expectedXML = ""; - String actualXML = "null"; /** * This is the current behavior. JSONObject.NULL is emitted as * the string, "null". */ + String actualXML = "null"; String resultXML = XML.toString(inputJSON); assertEquals(actualXML, resultXML); } From b39c3df766875d58abcfeb27c49489e4c051e081 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 20 Jul 2015 23:14:15 -0500 Subject: [PATCH 145/315] document behavior of content keyword --- XMLTest.java | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/XMLTest.java b/XMLTest.java index 6dc38420e..1ba88b189 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -328,7 +328,7 @@ public void shouldHandleNullNodeValue() JSONObject inputJSON = new JSONObject(); inputJSON.put("nullValue", JSONObject.NULL); // This is a possible preferred result - String expectedXML = ""; + // String expectedXML = ""; /** * This is the current behavior. JSONObject.NULL is emitted as * the string, "null". @@ -337,4 +337,89 @@ public void shouldHandleNullNodeValue() String resultXML = XML.toString(inputJSON); assertEquals(actualXML, resultXML); } -} + + @Test + public void contentOperations() { + /** + * Make sure we understand exactly how the "content" keyword works + */ + + /** + * When a standalone 0) then return]]>"; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertTrue("1. 3 items", 3 == jsonObject.length()); + assertTrue("1. empty tag1", "".equals(jsonObject.get("tag1"))); + assertTrue("1. empty tag2", "".equals(jsonObject.get("tag2"))); + assertTrue("1. content found", "if (a < b && a > 0) then return".equals(jsonObject.get("content"))); + + // multiple consecutive standalone cdatas are accumulated into an array + xmlStr = " 0) then return]]>"; + jsonObject = XML.toJSONObject(xmlStr); + assertTrue("2. 3 items", 3 == jsonObject.length()); + assertTrue("2. empty tag1", "".equals(jsonObject.get("tag1"))); + assertTrue("2. empty tag2", "".equals(jsonObject.get("tag2"))); + assertTrue("2. content array found", jsonObject.get("content") instanceof JSONArray); + JSONArray jsonArray = jsonObject.getJSONArray("content"); + assertTrue("2. array size", jsonArray.length() == 2); + assertTrue("2. content array entry 1", "if (a < b && a > 0) then return".equals(jsonArray.get(0))); + assertTrue("2. content array entry 2", "here is another cdata".equals(jsonArray.get(1))); + + /** + * text content is accumulated in a "content" inside a local JSONObject. + * If there is only one instance, it is saved in the context (a different JSONObject + * from the calling code. and the content element is discarded. + */ + xmlStr = "value 1"; + jsonObject = XML.toJSONObject(xmlStr); + assertTrue("3. 2 items", 1 == jsonObject.length()); + assertTrue("3. value tag1", "value 1".equals(jsonObject.get("tag1"))); + + /** + * array-style text content (multiple tags with the same name) is + * accumulated in a local JSONObject with key="content" and value=JSONArray, + * saved in the context, and then the local JSONObject is discarded. + */ + xmlStr = "value 12true"; + jsonObject = XML.toJSONObject(xmlStr); + assertTrue("4. 1 item", 1 == jsonObject.length()); + assertTrue("4. content array found", jsonObject.get("tag1") instanceof JSONArray); + jsonArray = jsonObject.getJSONArray("tag1"); + assertTrue("4. array size", jsonArray.length() == 3); + assertTrue("4. content array entry 1", "value 1".equals(jsonArray.get(0))); + assertTrue("4. content array entry 2", jsonArray.getInt(1) == 2); + assertTrue("4. content array entry 2", jsonArray.getBoolean(2) == true); + + /** + * Complex content is accumulated in a "content" field. For example, an element + * may contain a mix of child elements and text. Each text segment is + * accumulated to content. + */ + xmlStr = "val1val2"; + jsonObject = XML.toJSONObject(xmlStr); + assertTrue("5. 1 item", 1 == jsonObject.length()); + assertTrue("5. jsonObject found", jsonObject.get("tag1") instanceof JSONObject); + jsonObject = jsonObject.getJSONObject("tag1"); + assertTrue("5. 2 contained items", 2 == jsonObject.length()); + assertTrue("5. contained tag", "".equals(jsonObject.get("tag2"))); + assertTrue("5. contained content jsonArray found", jsonObject.get("content") instanceof JSONArray); + jsonArray = jsonObject.getJSONArray("content"); + assertTrue("5. array size", jsonArray.length() == 2); + assertTrue("5. content array entry 1", "val1".equals(jsonArray.get(0))); + assertTrue("5. content array entry 2", "val2".equals(jsonArray.get(1))); + + /** + * If there is only 1 complex text content, then it is accumulated in a + * "content" field as a string. + */ + xmlStr = "val1"; + jsonObject = XML.toJSONObject(xmlStr); + assertTrue("6. 1 item", 1 == jsonObject.length()); + assertTrue("6. jsonObject found", jsonObject.get("tag1") instanceof JSONObject); + jsonObject = jsonObject.getJSONObject("tag1"); + assertTrue("6. contained content found", "val1".equals(jsonObject.get("content"))); + assertTrue("6. contained tag2", "".equals(jsonObject.get("tag2"))); + } +} \ No newline at end of file From 0361cc58d696ce6cb7d94c0824c56945c1283e3e Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 20 Jul 2015 23:31:52 -0500 Subject: [PATCH 146/315] one more test - and a bug? --- XMLTest.java | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/XMLTest.java b/XMLTest.java index 1ba88b189..ab959ab28 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -364,8 +364,8 @@ public void contentOperations() { assertTrue("2. content array found", jsonObject.get("content") instanceof JSONArray); JSONArray jsonArray = jsonObject.getJSONArray("content"); assertTrue("2. array size", jsonArray.length() == 2); - assertTrue("2. content array entry 1", "if (a < b && a > 0) then return".equals(jsonArray.get(0))); - assertTrue("2. content array entry 2", "here is another cdata".equals(jsonArray.get(1))); + assertTrue("2. content array entry 0", "if (a < b && a > 0) then return".equals(jsonArray.get(0))); + assertTrue("2. content array entry 1", "here is another cdata".equals(jsonArray.get(1))); /** * text content is accumulated in a "content" inside a local JSONObject. @@ -388,8 +388,8 @@ public void contentOperations() { assertTrue("4. content array found", jsonObject.get("tag1") instanceof JSONArray); jsonArray = jsonObject.getJSONArray("tag1"); assertTrue("4. array size", jsonArray.length() == 3); - assertTrue("4. content array entry 1", "value 1".equals(jsonArray.get(0))); - assertTrue("4. content array entry 2", jsonArray.getInt(1) == 2); + assertTrue("4. content array entry 0", "value 1".equals(jsonArray.get(0))); + assertTrue("4. content array entry 1", jsonArray.getInt(1) == 2); assertTrue("4. content array entry 2", jsonArray.getBoolean(2) == true); /** @@ -407,8 +407,8 @@ public void contentOperations() { assertTrue("5. contained content jsonArray found", jsonObject.get("content") instanceof JSONArray); jsonArray = jsonObject.getJSONArray("content"); assertTrue("5. array size", jsonArray.length() == 2); - assertTrue("5. content array entry 1", "val1".equals(jsonArray.get(0))); - assertTrue("5. content array entry 2", "val2".equals(jsonArray.get(1))); + assertTrue("5. content array entry 0", "val1".equals(jsonArray.get(0))); + assertTrue("5. content array entry 1", "val2".equals(jsonArray.get(1))); /** * If there is only 1 complex text content, then it is accumulated in a @@ -421,5 +421,22 @@ public void contentOperations() { jsonObject = jsonObject.getJSONObject("tag1"); assertTrue("6. contained content found", "val1".equals(jsonObject.get("content"))); assertTrue("6. contained tag2", "".equals(jsonObject.get("tag2"))); - } + + /** + * In this corner case, the content sibling happens to have key=content + * We end up with an array within an array, and no content element. + * This is probably a bug. + */ + xmlStr = "val1"; + jsonObject = XML.toJSONObject(xmlStr); + assertTrue("7. 1 item", 1 == jsonObject.length()); + assertTrue("7. jsonArray found", jsonObject.get("tag1") instanceof JSONArray); + jsonArray = jsonObject.getJSONArray("tag1"); + assertTrue("array size 1", jsonArray.length() == 1); + assertTrue("7. contained array found", jsonArray.get(0) instanceof JSONArray); + jsonArray = jsonArray.getJSONArray(0); + assertTrue("7. inner array size 2", jsonArray.length() == 2); + assertTrue("7. inner array item 0", "val1".equals(jsonArray.get(0))); + assertTrue("7. inner array item 1", "".equals(jsonArray.get(1))); +} } \ No newline at end of file From 752f66746b6fa921af49e85b692a115fb6d6344b Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 21 Jul 2015 09:12:16 -0500 Subject: [PATCH 147/315] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index bd833414d..75d7861dd 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # JSON-Java-unit-test -**Tests are broken until bigint/bigdec changes are committed in JsonJava** [ETA for go-nogo decision is 9 Jul 2015]
- Unit tests to validate the JSON-Java GitHub project code
https://github.com/douglascrockford/JSON-java
From 4e38ed01e57c3061890c6d1ed1a5c9b38bf79d8d Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 22 Jul 2015 19:47:12 -0500 Subject: [PATCH 148/315] tests for enum-support --- EnumTest.java | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/EnumTest.java b/EnumTest.java index 14605b19c..e7bb0cda2 100644 --- a/EnumTest.java +++ b/EnumTest.java @@ -195,6 +195,7 @@ public void enumToString() { Util.compareActualVsExpectedJsonArrays(actualJsonArray, expectedJsonArray); } + @Test public void wrap() { /** * Wrap should handle enums exactly the same way as the JSONObject(Object) @@ -218,4 +219,54 @@ public void wrap() { expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + + @Test + public void enumAPI() { + /** + * Exercise the proposed enum API methods + */ + MyEnumClass myEnumClass = new MyEnumClass(); + myEnumClass.setMyEnum(MyEnum.VAL1); + MyEnumField myEnumField = MyEnumField.VAL2; + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("strKey", "value"); + jsonObject.put("enumKey", myEnumField); + jsonObject.put("enumClassKey", myEnumClass); + + // get a plain old enum + MyEnumField actualEnum = jsonObject.getEnum(MyEnumField.class, "enumKey"); + assertTrue("get myEnumField", actualEnum == MyEnumField.VAL2); + + // try to get the wrong value + try { + actualEnum = jsonObject.getEnum(MyEnumField.class, "strKey"); + assertTrue("should throw an exception for wrong key", false); + } catch (Exception ignored) {} + + // get a class that contains an enum + MyEnumClass actualEnumClass = (MyEnumClass)jsonObject.get("enumClassKey"); + assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1); + + // opt a plain old enum + actualEnum = jsonObject.optEnum(MyEnumField.class, "enumKey"); + assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2); + + // opt the wrong value + actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey"); + assertTrue("opt null", actualEnum == null); + + // opt a class that contains an enum + actualEnumClass = (MyEnumClass)jsonObject.opt("enumClassKey"); + assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1); + + // opt with default a plain old enum + actualEnum = jsonObject.optEnum(MyEnumField.class, "enumKey", null); + assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2); + + // opt with default the wrong value + actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey", null); + assertTrue("opt null", actualEnum == null); + + } } From 8ac8c34e9f75f5737d55b5d98668f16c0ff0fcdc Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 22 Jul 2015 20:12:10 -0500 Subject: [PATCH 149/315] tests for enum-support --- EnumTest.java | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/EnumTest.java b/EnumTest.java index e7bb0cda2..469ba9d6c 100644 --- a/EnumTest.java +++ b/EnumTest.java @@ -222,13 +222,13 @@ public void wrap() { @Test public void enumAPI() { - /** - * Exercise the proposed enum API methods - */ MyEnumClass myEnumClass = new MyEnumClass(); myEnumClass.setMyEnum(MyEnum.VAL1); MyEnumField myEnumField = MyEnumField.VAL2; + /** + * Exercise the proposed enum API methods on JSONObject + */ JSONObject jsonObject = new JSONObject(); jsonObject.put("strKey", "value"); jsonObject.put("enumKey", myEnumField); @@ -268,5 +268,46 @@ public void enumAPI() { actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey", null); assertTrue("opt null", actualEnum == null); + /** + * Exercise the proposed enum API methods on JSONArray + */ + JSONArray jsonArray = new JSONArray(); + jsonArray.put("value"); + jsonArray.put(myEnumField); + jsonArray.put(myEnumClass); + + // get a plain old enum + actualEnum = jsonArray.getEnum(MyEnumField.class, 1); + assertTrue("get myEnumField", actualEnum == MyEnumField.VAL2); + + // try to get the wrong value + try { + actualEnum = jsonArray.getEnum(MyEnumField.class, 0); + assertTrue("should throw an exception for wrong index", false); + } catch (Exception ignored) {} + + // get a class that contains an enum + actualEnumClass = (MyEnumClass)jsonArray.get(2); + assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1); + + // opt a plain old enum + actualEnum = jsonArray.optEnum(MyEnumField.class, 1); + assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2); + + // opt the wrong value + actualEnum = jsonArray.optEnum(MyEnumField.class, 0); + assertTrue("opt null", actualEnum == null); + + // opt a class that contains an enum + actualEnumClass = (MyEnumClass)jsonArray.opt(2); + assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1); + + // opt with default a plain old enum + actualEnum = jsonArray.optEnum(MyEnumField.class, 1, null); + assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2); + + // opt with default the wrong value + actualEnum = jsonArray.optEnum(MyEnumField.class, 0, null); + assertTrue("opt null", actualEnum == null); } } From 1f4e8368631660b368c0c353ed038fffd027cb66 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 22 Jul 2015 20:24:45 -0500 Subject: [PATCH 150/315] few more enum support tests --- EnumTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/EnumTest.java b/EnumTest.java index 469ba9d6c..6b7cc6b10 100644 --- a/EnumTest.java +++ b/EnumTest.java @@ -268,6 +268,10 @@ public void enumAPI() { actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey", null); assertTrue("opt null", actualEnum == null); + // opt with default an index that does not exist + actualEnum = jsonObject.optEnum(MyEnumField.class, "noKey", null); + assertTrue("opt null", actualEnum == null); + /** * Exercise the proposed enum API methods on JSONArray */ @@ -309,5 +313,10 @@ public void enumAPI() { // opt with default the wrong value actualEnum = jsonArray.optEnum(MyEnumField.class, 0, null); assertTrue("opt null", actualEnum == null); + + // opt with default an index that does not exist + actualEnum = jsonArray.optEnum(MyEnumField.class, 3, null); + assertTrue("opt null", actualEnum == null); + } } From f69466f4c215b58bafddac537f7064cf90310679 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 22 Jul 2015 21:21:23 -0500 Subject: [PATCH 151/315] recreate original documented issue --- XMLTest.java | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/XMLTest.java b/XMLTest.java index ab959ab28..58ddee594 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -438,5 +438,47 @@ public void contentOperations() { assertTrue("7. inner array size 2", jsonArray.length() == 2); assertTrue("7. inner array item 0", "val1".equals(jsonArray.get(0))); assertTrue("7. inner array item 1", "".equals(jsonArray.get(1))); -} + + /** + * Confirm behavior of original issue + */ + String jsonStr = + "{"+ + "\"Profile\": {"+ + "\"list\": {"+ + "\"history\": {"+ + "\"entries\": ["+ + "{"+ + "\"deviceId\": \"id\","+ + "\"content\": {"+ + "\"material\": ["+ + "{"+ + "\"stuff\": false"+ + "}"+ + "]"+ + "}"+ + "}"+ + "]"+ + "}"+ + "}"+ + "}"+ + "}"; + jsonObject = new JSONObject(jsonStr); + xmlStr = XML.toString(jsonObject); + /** + * This is the created XML. Looks like content was mistaken for + * complex (child node + text) XML. + * + * + * + * + * id + * {"material":[{"stuff":false}]} + * + * + * + * + */ + assertTrue("nothing to test here, see comment on created XML, above", true); + } } \ No newline at end of file From ee0a53d4948503fcbbd206013db9fd4919fc9c6b Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 8 Aug 2015 08:13:22 -0500 Subject: [PATCH 152/315] Verify exception messages. Move method comments so JavaDoc will pick them up. --- CDLTest.java | 132 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 78 insertions(+), 54 deletions(-) diff --git a/CDLTest.java b/CDLTest.java index d218b10e4..340977b4d 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -42,113 +42,137 @@ public class CDLTest { "{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, "+ "{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va\'l6, Col 7:val7}]"); + /** + * Attempts to create a JSONArray from a null string. + * Expect a NullPointerException. + */ @Test(expected=NullPointerException.class) public void exceptionOnNullString() { - /** - * Attempts to create a JSONArray from a null string - */ String nullStr = null; CDL.toJSONArray(nullStr); } - @Test(expected=JSONException.class) + /** + * Attempts to create a JSONArray from a string with unbalanced quotes + * in column title line. Expects a JSONException. + */ + @Test public void unbalancedQuoteInName() { - /** - * Attempts to create a JSONArray from a string with unbalanced quotes - * in column title line - */ String badLine = "Col1, \"Col2\nVal1, Val2"; - CDL.toJSONArray(badLine); + try { + CDL.toJSONArray(badLine); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Missing close quote '\"'. at 12 [character 0 line 2]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Attempts to create a JSONArray from a string with unbalanced quotes + * in value line. Expects a JSONException. + */ + @Test public void unbalancedQuoteInValue() { - /** - * Attempts to create a JSONArray from a string with unbalanced quotes - * in value line - */ String badLine = "Col1, Col2\n\"Val1, Val2"; - CDL.toJSONArray(badLine); + try { + CDL.toJSONArray(badLine); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Missing close quote '\"'. at 23 [character 12 line 3]". + equals(e.getMessage())); + + } } - @Test(expected=JSONException.class) + /** + * Attempts to create a JSONArray from a string with null char + * in column title line. Expects a JSONException. + */ + @Test public void nullInName() { - /** - * Attempts to create a JSONArray from a string with null char - * in column title line - */ String badLine = "C\0ol1, Col2\nVal1, Val2"; - CDL.toJSONArray(badLine); + try { + CDL.toJSONArray(badLine); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Bad character 'o' (111). at 3 [character 4 line 1]". + equals(e.getMessage())); + + } } + /** + * call toString with a null array + */ @Test(expected=NullPointerException.class) public void nullJSONArrayToString() { - /** - * call toString with a null array - */ CDL.toString((JSONArray)null); } + /** + * Create a JSONArray from an empty string + */ @Test public void emptyString() { - /** - * Create a JSONArray from an empty string - */ String emptyStr = ""; JSONArray jsonArray = CDL.toJSONArray(emptyStr); assertTrue("CDL should return null when the input string is empty", jsonArray == null); } + /** + * Create a JSONArray with only 1 row + */ @Test public void onlyColumnNames() { - /** - * Create a JSONArray with only 1 row - */ String columnNameStr = "col1, col2, col3"; JSONArray jsonArray = CDL.toJSONArray(columnNameStr); assertTrue("CDL should return null when only 1 row is given", jsonArray == null); } + /** + * Create a JSONArray from string containing only whitespace and commas + */ @Test public void emptyLinesToJSONArray() { - /** - * Create a JSONArray from string containing only whitespace and commas - */ String str = " , , , \n , , , "; JSONArray jsonArray = CDL.toJSONArray(str); assertTrue("JSONArray should be null for no content", jsonArray == null); } + /** + * call toString with a null array + */ @Test public void emptyJSONArrayToString() { - /** - * call toString with a null array - */ JSONArray jsonArray = new JSONArray(); String str = CDL.toString(jsonArray); assertTrue("CDL should return null for toString(null)", str == null); } + /** + * call toString with a null arrays for names and values + */ @Test public void nullJSONArraysToString() { - /** - * call toString with a null arrays for names and values - */ String str = CDL.toString(null, null); assertTrue("CDL should return null for toString(null)", str == null); } + /** + * Given a JSONArray that was not built by CDL, some chars may be + * found that would otherwise be filtered out by CDL. + */ @Test public void checkSpecialChars() { - /** - * Given a JSONArray that was not built by CDL, some chars may be - * found that would otherwise be filtered out by CDL. - */ JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); jsonArray.put(jsonObject); @@ -165,22 +189,22 @@ public void checkSpecialChars() { assertTrue(cdlStr.contains("\"V2\"")); } + /** + * Create a JSONArray from a string of lines + */ @Test public void textToJSONArray() { - /** - * Create a JSONArray from a string of lines - */ JSONArray jsonArray = CDL.toJSONArray(lines); JSONArray expectedJsonArray = new JSONArray(expectedLines); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } + /** + * Create a JSONArray from a JSONArray of titles and a + * string of value lines + */ @Test public void jsonArrayToJSONArray() { - /** - * Create a JSONArray from a JSONArray of titles and a - * string of value lines - */ String nameArrayStr = "[Col1, Col2]"; String values = "V1, V2"; JSONArray nameJSONArray = new JSONArray(nameArrayStr); @@ -189,12 +213,12 @@ public void jsonArrayToJSONArray() { Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } + /** + * Create a JSONArray from a string of lines, + * then convert to string and then back to JSONArray + */ @Test public void textToJSONArrayAndBackToString() { - /** - * Create a JSONArray from a string of lines, - * then convert to string and then back to JSONArray - */ JSONArray jsonArray = CDL.toJSONArray(lines); String jsonStr = CDL.toString(jsonArray); JSONArray finalJsonArray = CDL.toJSONArray(jsonStr); From a0108f3e8e9e2d3bf1bac7dabdd6226b34c718d1 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 8 Aug 2015 09:32:15 -0500 Subject: [PATCH 153/315] Verify exception messages. Move method comments so JavaDoc will pick them up. --- CookieListTest.java | 80 +++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 28 deletions(-) diff --git a/CookieListTest.java b/CookieListTest.java index 0eaef7162..1d7ad8a5b 100644 --- a/CookieListTest.java +++ b/CookieListTest.java @@ -6,60 +6,70 @@ import org.junit.Test; /** - * HTTP cookie specification: RFC6265 - * + * HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265 + *

* A cookie list is a JSONObject whose members are presumed to be cookie * name/value pairs. Entries are unescaped while being added, and escaped in * the toString() output. * Unescaping means to convert %hh hex strings to the ascii equivalent * and converting '+' to ' '. * Escaping converts '+', '%', '=', ';' and ascii control chars to %hh hex strings. - * - * CookieList should not be considered as just a list of Cookie objects: + *

+ * CookieList should not be considered as just a list of Cookie objects:
* - CookieList stores a cookie name/value pair as a single entry; Cookie stores - * it as 2 entries (key="name" and key="value"). + * it as 2 entries (key="name" and key="value").
* - CookieList requires multiple name/value pairs as input; Cookie allows the - * 'secure' name with no associated value - * - CookieList has no special handling for attribute name/value pairs. + * 'secure' name with no associated value
+ * - CookieList has no special handling for attribute name/value pairs.
*/ public class CookieListTest { + /** + * Attempts to create a CookieList from a null string. + * Expects a NullPointerException. + */ @Test(expected=NullPointerException.class) public void nullCookieListException() { - /** - * Attempts to create a CookieList from a null string - */ String cookieStr = null; CookieList.toJSONObject(cookieStr); } - @Test(expected=JSONException.class) + /** + * Attempts to create a CookieList from a malformed string. + * Expects a JSONException. + */ + @Test public void malFormedCookieListException() { - /** - * Attempts to create a CookieList from a malformed string - */ String cookieStr = "thisCookieHasNoEqualsChar"; - CookieList.toJSONObject(cookieStr); + try { + CookieList.toJSONObject(cookieStr); + assertTrue("should throw an exception", false); + } catch (JSONException e) { + /** + * Not sure of the missing char, but full string compare fails + */ + assertTrue("Expecting an exception message", + e.getMessage().startsWith("Expected '=' and instead saw '") && + e.getMessage().endsWith("' at 27 [character 28 line 1]")); + } } - @Test(expected=JSONException.class) + /** + * Creates a CookieList from an empty string. + */ + @Test public void emptyStringCookieList() { - /** - * Creates a CookieList from an empty string. - * Cookie throws an exception, but CookieList does not - */ String cookieStr = ""; String expectedCookieStr = ""; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + assertTrue(jsonObject.length() == 0); } + /** + * CookieList with the simplest cookie - a name/value pair with no delimiter. + */ @Test public void simpleCookieList() { - /** - * The simplest cookie is a name/value pair with no delimiter - */ String cookieStr = "SID=31d4d96e407aad42"; String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); @@ -67,11 +77,11 @@ public void simpleCookieList() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * CookieList with a single a cookie which has a name/value pair and delimiter. + */ @Test public void simpleCookieListWithDelimiter() { - /** - * The simplest cookie is a name/value pair with a delimiter - */ String cookieStr = "SID=31d4d96e407aad42;"; String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); @@ -79,6 +89,10 @@ public void simpleCookieListWithDelimiter() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * CookieList with multiple cookies consisting of name/value pairs + * with delimiters. + */ @Test public void multiPartCookieList() { String cookieStr = @@ -102,6 +116,9 @@ public void multiPartCookieList() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * CookieList from a JSONObject with valid key and null value + */ @Test public void convertCookieListWithNullValueToString() { JSONObject jsonObject = new JSONObject(); @@ -110,6 +127,9 @@ public void convertCookieListWithNullValueToString() { assertTrue("toString() should be empty", "".equals(cookieToStr)); } + /** + * CookieList with multiple entries converted to a JSON document. + */ @Test public void convertCookieListToString() { String cookieStr = @@ -136,6 +156,10 @@ public void convertCookieListToString() { Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } + /** + * CookieList with multiple entries and some '+' chars and URL-encoded + * values converted to a JSON document. + */ @Test public void convertEncodedCookieListToString() { String cookieStr = From f66cc8d5c4fea0deb96fe06c4eca6bc61673af35 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 8 Aug 2015 10:22:17 -0500 Subject: [PATCH 154/315] Verify exception messages. Move method comments so JavaDoc will pick them up. --- CookieTest.java | 133 +++++++++++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 53 deletions(-) diff --git a/CookieTest.java b/CookieTest.java index c96bd7228..9104b60e1 100644 --- a/CookieTest.java +++ b/CookieTest.java @@ -10,61 +10,88 @@ /** * HTTP cookie specification: RFC6265 - * + *

* At its most basic, a cookie is a name=value pair. The value may be subdivided * into other cookies, but that is not tested here. The cookie may also include * certain named attributes, delimited by semicolons. - * + *

* The Cookie.toString() method emits certain attributes if present: expires, * domain, path, secure. All but secure are name-value pairs. Other attributes * are not included in the toString() output. - * + *

* A JSON-Java encoded cookie escapes '+', '%', '=', ';' with %hh values. */ public class CookieTest { + /** + * Attempts to create a JSONObject from a null string. + * Expects a NullPointerException. + */ @Test(expected=NullPointerException.class) public void nullCookieException() { - /** - * Attempts to create a JSONObject from a null string - */ String cookieStr = null; Cookie.toJSONObject(cookieStr); } - @Test(expected=JSONException.class) + /** + * Attempts to create a JSONObject from a cookie string with + * no '=' char. + * Expects a JSONException. + */ + @Test public void malFormedNameValueException() { - /** - * Attempts to create a JSONObject from a malformed cookie string - */ String cookieStr = "thisCookieHasNoEqualsChar"; - Cookie.toJSONObject(cookieStr); + try { + Cookie.toJSONObject(cookieStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + e.getMessage().startsWith("Expected '=' and instead saw '") + && e.getMessage().endsWith("' at 27 [character 28 line 1]")); + } } - @Test(expected=JSONException.class) + /** + * Attempts to create a JSONObject from a cookie string + * with embedded ';' char. + * Expects a JSONException. + */ + @Test public void malFormedAttributeException() { - /** - * Attempts to create a JSONObject from a malformed cookie string - */ String cookieStr = "this=Cookie;myAttribute"; - Cookie.toJSONObject(cookieStr); + try { + Cookie.toJSONObject(cookieStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Missing '=' in cookie parameter. at 25 [character 26 line 1]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Attempts to create a JSONObject from an empty cookie string.
+ * Note: Cookie throws an exception, but CookieList does not.
+ * Expects a JSONException + */ + @Test public void emptyStringCookieException() { - /** - * Attempts to create a JSONObject from an empty cookie string - * Note: Cookie throws an exception, but CookieList does not - */ String cookieStr = ""; - Cookie.toJSONObject(cookieStr); + try { + Cookie.toJSONObject(cookieStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + e.getMessage().startsWith("Expected '=' and instead saw '") && + e.getMessage().endsWith("' at 2 [character 3 line 1]")); + } } + /** + * Cookie from a simple name/value pair with no delimiter + */ @Test public void simpleCookie() { - /** - * The simplest cookie is a name/value pair with no delimiter - */ String cookieStr = "SID=31d4d96e407aad42"; String expectedCookieStr = "{\"name\":\"SID\",\"value\":\"31d4d96e407aad42\"}"; JSONObject jsonObject = Cookie.toJSONObject(cookieStr); @@ -72,13 +99,13 @@ public void simpleCookie() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * Store a cookie with all of the supported attributes in a + * JSONObject. The secure attribute, which has no value, is treated + * as a boolean. + */ @Test public void multiPartCookie() { - /** - * Store a cookie with all of the supported attributes in a - * JSONObject. The secure attribute, which has no value, is treated - * as a boolean. - */ String cookieStr = "PH=deleted; "+ " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ @@ -99,13 +126,13 @@ public void multiPartCookie() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * Cookie.toString() will omit the non-standard "thiswont=beIncluded" + * attribute, but the attribute is still stored in the JSONObject. + * This test confirms both behaviors. + */ @Test public void convertCookieToString() { - /** - * ToString() will omit the non-standard "thiswont=beIncluded" - * attribute, but the attribute is still stored in the JSONObject. - * This test confirms both behaviors. - */ String cookieStr = "PH=deleted; "+ " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ @@ -138,14 +165,14 @@ public void convertCookieToString() { Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } + /** + * A string may be URL-encoded when converting to JSONObject. + * If found, '+' is converted to ' ', and %hh hex strings are converted + * to their ascii char equivalents. This test confirms the decoding + * behavior. + */ @Test public void convertEncodedCookieToString() { - /** - * A string may be URL-encoded when converting to JSONObject. - * If found, '+' is converted to ' ', and %hh hex strings are converted - * to their ascii char equivalents. This test confirms the decoding - * behavior. - */ String cookieStr = "PH=deleted; "+ " expires=Wed,+19-Mar-2014+17:53:53+GMT;"+ @@ -167,14 +194,14 @@ public void convertEncodedCookieToString() { Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } + /** + * A public API method performs a URL encoding for selected chars + * in a string. Control chars, '+', '%', '=', ';' are all encoded + * as %hh hex strings. The string is also trimmed. + * This test confirms that behavior. + */ @Test public void escapeString() { - /** - * A public API method performs a URL encoding for selected chars - * in a string. Control chars, '+', '%', '=', ';' are all encoded - * as %hh hex strings. The string is also trimmed. - * This test confirms that behavior. - */ String str = " +%\r\n\t\b%=;;; "; String expectedStr = "%2b%25%0d%0a%09%08%25%3d%3b%3b%3b"; String actualStr = Cookie.escape(str); @@ -182,14 +209,14 @@ public void escapeString() { " expected: " +expectedStr, expectedStr.equals(actualStr)); } + /** + * A public API method performs URL decoding for strings. + * '+' is converted to space and %hh hex strings are converted to + * their ascii equivalent values. The string is not trimmed. + * This test confirms that behavior. + */ @Test public void unescapeString() { - /** - * A public API method performs URL decoding for strings. - * '+' is converted to space and %hh hex strings are converted to - * their ascii equivalent values. The string is not trimmed. - * This test confirms that behavior. - */ String str = " +%2b%25%0d%0a%09%08%25%3d%3b%3b%3b+ "; String expectedStr = " +%\r\n\t\b%=;;; "; String actualStr = Cookie.unescape(str); From 9ce62b954047eafda5d8eccdc1576c2102ae0593 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 8 Aug 2015 10:30:20 -0500 Subject: [PATCH 155/315] Move method comments so JavaDoc will pick them up. --- EnumTest.java | 68 +++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/EnumTest.java b/EnumTest.java index 6b7cc6b10..d24824bce 100644 --- a/EnumTest.java +++ b/EnumTest.java @@ -11,14 +11,14 @@ * These tests explore how enum serialization works with JSON-Java. */ public class EnumTest { + + /** + * To serialize an enum by its getters, use the JSONObject Object constructor. + * The JSONObject ctor handles enum like any other bean. A JSONobject + * is created whose entries are the getter name/value pairs. + */ @Test public void jsonObjectFromEnum() { - /** - * To serialize an enum by its getters, use the JSONObject Object constructor. - * The JSONObject ctor handles enum like any other bean. A JSONobject - * is created whose entries are the getter name/value pairs. - */ - // If there are no getters then the object is empty. MyEnum myEnum = MyEnum.VAL2; JSONObject jsonObject = new JSONObject(myEnum); @@ -44,12 +44,12 @@ public void jsonObjectFromEnum() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * To serialize an enum by its set of allowed values, use getNames() + * and the the JSONObject Object with names constructor. + */ @Test public void jsonObjectFromEnumWithNames() { - /** - * To serialize an enum by its set of allowed values, use getNames() - * and the the JSONObject Object with names constructor. - */ String [] names; String expectedStr; JSONObject jsonObject; @@ -74,12 +74,13 @@ public void jsonObjectFromEnumWithNames() { expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); } + + /** + * To serialize by assigned value, use the put() methods. The value + * will be stored as a enum type. + */ @Test public void enumPut() { - /** - * To serialize by assigned value, use the put() methods. The value - * will be stored as a enum type. - */ String expectedFinalStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL1\"}"; JSONObject jsonObject = new JSONObject(); MyEnum myEnum = MyEnum.VAL2; @@ -106,12 +107,12 @@ public void enumPut() { assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1))); } + /** + * The default action of valueToString() is to call object.toString(). + * For enums, this means the assigned value will be returned as a string. + */ @Test public void enumValueToString() { - /** - * The default action of valueToString() is to call object.toString(). - * For enums, this means the assigned value will be returned as a string. - */ String expectedStr1 = "\"VAL1\""; String expectedStr2 = "\"VAL1\""; MyEnum myEnum = MyEnum.VAL1; @@ -137,12 +138,12 @@ public void enumValueToString() { str3.startsWith(expectedStr3)); } + /** + * In whatever form the enum was added to the JSONObject or JSONArray, + * json[Object|Array].toString should serialize it in a reasonable way. + */ @Test public void enumToString() { - /** - * In whatever form the enum was added to the JSONObject or JSONArray, - * json[Object|Array].toString should serialize it in a reasonable way. - */ MyEnum myEnum = MyEnum.VAL2; JSONObject jsonObject = new JSONObject(myEnum); String expectedStr = "{}"; @@ -195,12 +196,12 @@ public void enumToString() { Util.compareActualVsExpectedJsonArrays(actualJsonArray, expectedJsonArray); } + /** + * Wrap should handle enums exactly the same way as the JSONObject(Object) + * constructor. + */ @Test public void wrap() { - /** - * Wrap should handle enums exactly the same way as the JSONObject(Object) - * constructor. - */ MyEnum myEnum = MyEnum.VAL2; JSONObject jsonObject = (JSONObject)JSONObject.wrap(myEnum); assertTrue("simple enum has no getters", jsonObject.length() == 0); @@ -220,15 +221,24 @@ public void wrap() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * It was determined that some API methods should be added to + * support enums:
+ * JSONObject.getEnum(class, key)
+ * JSONObject.optEnum(class, key)
+ * JSONObject.optEnum(class, key, default)
+ * JSONArray.getEnum(class, index)
+ * JSONArray.optEnum(class, index)
+ * JSONArray.optEnum(class, index, default)
+ *

+ * Exercise these enum API methods on JSONObject and JSONArray + */ @Test public void enumAPI() { MyEnumClass myEnumClass = new MyEnumClass(); myEnumClass.setMyEnum(MyEnum.VAL1); MyEnumField myEnumField = MyEnumField.VAL2; - /** - * Exercise the proposed enum API methods on JSONObject - */ JSONObject jsonObject = new JSONObject(); jsonObject.put("strKey", "value"); jsonObject.put("enumKey", myEnumField); From 6f5bcb32e58064a8ef6fc81c15eb346c337024b9 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 8 Aug 2015 12:10:18 -0500 Subject: [PATCH 156/315] Unit tests for JSON-Java HTTP.java. See RFC7230 --- HTTPTest.java | 71 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/HTTPTest.java b/HTTPTest.java index 52e166b57..1bc04632f 100644 --- a/HTTPTest.java +++ b/HTTPTest.java @@ -1,30 +1,50 @@ package org.json.junit; +import static org.junit.Assert.*; + import org.json.*; import org.junit.Test; /** - * Tests for JSON-Java HTTP.java - * See RFC7230 + * Unit tests for JSON-Java HTTP.java. See RFC7230 */ public class HTTPTest { + /** + * Attempt to call HTTP.toJSONObject() with a null string + * Expects a NUllPointerException. + */ @Test(expected=NullPointerException.class) public void nullHTTPException() { String httpStr = null; HTTP.toJSONObject(httpStr); } - @Test(expected=JSONException.class) + /** + * Attempt to call HTTP.toJSONObject() with a string containing + * an empty object. Expects a JSONException. + */ + @Test public void notEnoughHTTPException() { String httpStr = "{}"; JSONObject jsonObject = new JSONObject(httpStr); - HTTP.toString(jsonObject); + try { + HTTP.toString(jsonObject); + assertTrue("Expected to throw exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Not enough material for an HTTP header.".equals(e.getMessage())); + } } + /** + * Calling HTTP.toJSONObject() with an empty string will result in a + * populated JSONObject with keys but no values for Request-URI, Method, + * and HTTP-Version. + */ @Test - public void emptyStringHTTPException() { + public void emptyStringHTTPRequest() { String httpStr = ""; String expectedHTTPStr = "{\"Request-URI\":\"\",\"Method\":\"\",\"HTTP-Version\":\"\"}"; JSONObject jsonObject = HTTP.toJSONObject(httpStr); @@ -32,6 +52,10 @@ public void emptyStringHTTPException() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * Call HTTP.toJSONObject() with a Request-URI, Method, + * and HTTP-Version. + */ @Test public void simpleHTTPRequest() { String httpStr = "GET /hello.txt HTTP/1.1"; @@ -42,6 +66,10 @@ public void simpleHTTPRequest() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * Call HTTP.toJSONObject() with a response string containing a + * HTTP-Version, Status-Code, and Reason. + */ @Test public void simpleHTTPResponse() { String httpStr = "HTTP/1.1 200 OK"; @@ -52,6 +80,10 @@ public void simpleHTTPResponse() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * Call HTTP.toJSONObject() with a full request string including + * request headers. + */ @Test public void extendedHTTPRequest() { String httpStr = @@ -70,11 +102,18 @@ public void extendedHTTPRequest() { "\"Content-Type\":\"text/xml; charset=utf-8\"}"; JSONObject jsonObject = HTTP.toJSONObject(httpStr); JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); - // not too easy for JSONObject to parse a string with embedded quotes + /** + * Not too easy for JSONObject to parse a string with embedded quotes. + * For the sake of the test, add it here. + */ expectedJsonObject.put("SOAPAction","\"http://clearforest.com/Enlighten\""); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * Call HTTP.toJSONObject() with a full response string including + * response headers. + */ @Test public void extendedHTTPResponse() { String httpStr = @@ -92,6 +131,10 @@ public void extendedHTTPResponse() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * Call HTTP.toJSONObject() with a full POST request string including + * response headers, then convert it back into an HTTP string. + */ @Test public void convertHTTPRequestToString() { String httpStr = @@ -110,8 +153,10 @@ public void convertHTTPRequestToString() { JSONObject jsonObject = HTTP.toJSONObject(httpStr); JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); String httpToStr = HTTP.toString(jsonObject); - // JSONObject objects to crlfs and any trailing chars - // httpToStr = httpToStr.replaceAll("(\r\n\r\n)", ""); + /** + * JSONObject objects to crlfs and any trailing chars. + * For the sake of the test, simplify the resulting string + */ httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", ""); httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n"); JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr); @@ -119,6 +164,10 @@ public void convertHTTPRequestToString() { Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } + /** + * Call HTTP.toJSONObject() with a full response string including + * response headers, then convert it back into an HTTP string. + */ @Test public void convertHTTPResponseToString() { String httpStr = @@ -134,8 +183,10 @@ public void convertHTTPResponseToString() { JSONObject jsonObject = HTTP.toJSONObject(httpStr); JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); String httpToStr = HTTP.toString(jsonObject); - // JSONObject objects to crlfs and any trailing chars - // httpToStr = httpToStr.replaceAll("(\r\n\r\n)", ""); + /** + * JSONObject objects to crlfs and any trailing chars. + * For the sake of the test, simplify the resulting string + */ httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", ""); httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n"); JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr); From 41bfdad91fe4136f54080716b408493daf1262b0 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 8 Aug 2015 12:10:52 -0500 Subject: [PATCH 157/315] Move method comments so JavaDoc will pick them up. --- HTTPTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HTTPTest.java b/HTTPTest.java index 1bc04632f..2716c3ce5 100644 --- a/HTTPTest.java +++ b/HTTPTest.java @@ -7,7 +7,7 @@ /** - * Unit tests for JSON-Java HTTP.java. See RFC7230 + * Unit tests for JSON-Java HTTP.java. See RFC7230. */ public class HTTPTest { From 4a3bbedc3237c98beda74f33fd66c4a3797ad5f4 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 8 Aug 2015 14:09:52 -0500 Subject: [PATCH 158/315] Verify exception messages. Move method comments so JavaDoc will pick them up. --- JSONArrayTest.java | 151 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 119 insertions(+), 32 deletions(-) diff --git a/JSONArrayTest.java b/JSONArrayTest.java index b033a60f1..4c2145696 100644 --- a/JSONArrayTest.java +++ b/JSONArrayTest.java @@ -36,27 +36,59 @@ public class JSONArrayTest { "\"-1\""+ "]"; + /** + * Attempt to create a JSONArray with a null string. + * Expects a NullPointerException. + */ @Test(expected=NullPointerException.class) public void nullException() { String str = null; new JSONArray(str); } - @Test(expected=JSONException.class) + /** + * Attempt to create a JSONArray with an empty string. + * Expects a JSONException. + */ + @Test public void emptStr() { String str = ""; - new JSONArray(str); + try { + new JSONArray(str); + assertTrue("Should throw an exception", false); + } catch (JSONException e) { + assertTrue("Expected an exception message", + "A JSONArray text must start with '[' at 1 [character 2 line 1]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Attempt to create a JSONArray with a string as object that is + * not a JSON array doc. + * Expects a JSONException. + */ + @Test public void badObject() { String str = "abc"; - new JSONArray((Object)str); + try { + new JSONArray((Object)str); + assertTrue("Should throw an exception", false); + } catch (JSONException e) { + assertTrue("Expected an exception message", + "JSONArray initial value should be a string or collection or array.". + equals(e.getMessage())); + } } + /** + * Create a JSONArray doc with a variety of different elements. + * Confirm that the values can be accessed via the get[type]() API methods + */ @Test public void getArrayValues() { JSONArray jsonArray = new JSONArray(arrayStr); + // booleans assertTrue("Array true", true == jsonArray.getBoolean(0)); assertTrue("Array false", @@ -65,83 +97,105 @@ public void getArrayValues() { true == jsonArray.getBoolean(2)); assertTrue("Array string false", false == jsonArray.getBoolean(3)); - + // strings + assertTrue("Array value string", + "hello".equals(jsonArray.getString(4))); + // doubles assertTrue("Array double", new Double(23.45e-4).equals(jsonArray.getDouble(5))); assertTrue("Array string double", new Double(23.45).equals(jsonArray.getDouble(6))); - + // ints assertTrue("Array value int", new Integer(42).equals(jsonArray.getInt(7))); assertTrue("Array value string int", new Integer(43).equals(jsonArray.getInt(8))); - + // nested objects JSONArray nestedJsonArray = jsonArray.getJSONArray(9); assertTrue("Array value JSONArray", nestedJsonArray != null); - JSONObject nestedJsonObject = jsonArray.getJSONObject(10); assertTrue("Array value JSONObject", nestedJsonObject != null); - + // longs assertTrue("Array value long", new Long(0).equals(jsonArray.getLong(11))); assertTrue("Array value string long", new Long(-1).equals(jsonArray.getLong(12))); - assertTrue("Array value string", - "hello".equals(jsonArray.getString(4))); - assertTrue("Array value null", jsonArray.isNull(-1)); } + /** + * Create a JSONArray doc with a variety of different elements. + * Confirm that attempting to get the wrong types via the get[type]() + * API methods result in JSONExceptions + */ @Test public void failedGetArrayValues() { - int tryCount = 0; - int exceptionCount = 0; JSONArray jsonArray = new JSONArray(arrayStr); try { - tryCount++; jsonArray.getBoolean(4); assertTrue("expected getBoolean to fail", false); - } catch (JSONException ignored) { exceptionCount++; } + } catch (JSONException e) { + assertTrue("Expected an exception message", + "JSONArray[4] is not a boolean.".equals(e.getMessage())); + } try { - tryCount++; jsonArray.get(-1); assertTrue("expected get to fail", false); - } catch (JSONException ignored) { exceptionCount++; } + } catch (JSONException e) { + assertTrue("Expected an exception message", + "JSONArray[-1] not found.".equals(e.getMessage())); + } try { - tryCount++; jsonArray.getDouble(4); assertTrue("expected getDouble to fail", false); - } catch (JSONException ignored) { exceptionCount++; } + } catch (JSONException e) { + assertTrue("Expected an exception message", + "JSONArray[4] is not a number.".equals(e.getMessage())); + } try { - tryCount++; jsonArray.getInt(4); assertTrue("expected getInt to fail", false); - } catch (JSONException ignored) { exceptionCount++; } + } catch (JSONException e) { + assertTrue("Expected an exception message", + "JSONArray[4] is not a number.".equals(e.getMessage())); + } try { - tryCount++; jsonArray.getJSONArray(4); assertTrue("expected getJSONArray to fail", false); - } catch (JSONException ignored) { exceptionCount++; } + } catch (JSONException e) { + assertTrue("Expected an exception message", + "JSONArray[4] is not a JSONArray.".equals(e.getMessage())); + } try { - tryCount++; jsonArray.getJSONObject(4); assertTrue("expected getJSONObject to fail", false); - } catch (JSONException ignored) { exceptionCount++; } + } catch (JSONException e) { + assertTrue("Expected an exception message", + "JSONArray[4] is not a JSONObject.".equals(e.getMessage())); + } try { - tryCount++; jsonArray.getLong(4); assertTrue("expected getLong to fail", false); - } catch (JSONException ignored) { exceptionCount++; } + } catch (JSONException e) { + assertTrue("Expected an exception message", + "JSONArray[4] is not a number.".equals(e.getMessage())); + } try { - tryCount++; jsonArray.getString(5); assertTrue("expected getString to fail", false); - } catch (JSONException ignored) { exceptionCount++; } - assertTrue("tryCount should match exceptionCount", - tryCount == exceptionCount); + } catch (JSONException e) { + assertTrue("Expected an exception message", + "JSONArray[5] not a string.".equals(e.getMessage())); + } } + /** + * Exercise JSONArray.join() by converting a JSONArray into a + * comma-separated string. Since this is very nearly a JSON document, + * array braces are added to the beginning and end, and it is reconverted + * back to a JSONArray for comparison. + */ @Test public void join() { String expectedStr = @@ -176,6 +230,9 @@ public void join() { Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } + /** + * Confirm the JSONArray.length() method + */ @Test public void length() { assertTrue("expected empty JSONArray length 0", @@ -186,6 +243,11 @@ public void length() { assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1); } + /** + * Create a JSONArray doc with a variety of different elements. + * Confirm that the values can be accessed via the opt[type](index) + * and opt[type](index, default) API methods. + */ @Test public void opt() { JSONArray jsonArray = new JSONArray(arrayStr); @@ -238,6 +300,10 @@ public void opt() { "".equals(jsonArray.optString(-1))); } + /** + * Exercise the JSONArray.put(value) method with various parameters + * and confirm the resulting JSONArray. + */ @Test public void put() { String expectedStr = @@ -314,6 +380,10 @@ public void put() { Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } + /** + * Exercise the JSONArray.put(index, value) method with various parameters + * and confirm the resulting JSONArray. + */ @Test public void putIndex() { String expectedStr = @@ -393,6 +463,10 @@ public void putIndex() { Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } + /** + * Exercise the JSONArray.remove(index) method + * and confirm the resulting JSONArray. + */ @Test public void remove() { String arrayStr = @@ -406,6 +480,10 @@ public void remove() { Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } + /** + * Exercise the JSONArray.similar() method with various parameters + * and confirm the results when not similar. + */ @Test public void notSimilar() { String arrayStr = @@ -441,6 +519,9 @@ public void notSimilar() { !jsonArray.similar(otherJsonArray)); } + /** + * Convert an empty JSONArray to JSONObject + */ @Test public void toJSONObject() { JSONArray names = new JSONArray(); @@ -449,6 +530,9 @@ public void toJSONObject() { null == jsonArray.toJSONObject(names)); } + /** + * Confirm the creation of a JSONArray from an array of ints + */ @Test public void objectArrayVsIsArray() { String expectedStr = @@ -462,6 +546,9 @@ public void objectArrayVsIsArray() { Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } + /** + * Exercise the JSONArray iterator. + */ @Test public void iterator() { JSONArray jsonArray = new JSONArray(arrayStr); From 8e48caeb3d056749f373aa2cfd6aaf3bea70691c Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 8 Aug 2015 15:12:20 -0500 Subject: [PATCH 159/315] Verify exception messages. Move method comments so JavaDoc will pick them up. --- JSONMLTest.java | 262 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 211 insertions(+), 51 deletions(-) diff --git a/JSONMLTest.java b/JSONMLTest.java index fa982e137..a16f0ea53 100644 --- a/JSONMLTest.java +++ b/JSONMLTest.java @@ -23,34 +23,93 @@ */ public class JSONMLTest { + /** + * Attempts to transform a null XML string to JSON. + * Expects a NullPointerException + */ @Test(expected=NullPointerException.class) public void nullXMLException() { - /** - * Attempts to transform a null XML string to JSON - */ String xmlStr = null; JSONML.toJSONArray(xmlStr); } - @Test(expected=JSONException.class) + /** + * Attempts to transform an empty string to JSON. + * Expects a JSONException + */ + @Test public void emptyXMLException() { + String xmlStr = ""; + try { + JSONML.toJSONArray(xmlStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Bad XML at 1 [character 2 line 1]". + equals(e.getMessage())); + } + } + + /** + * Attempts to call JSONML.toString() with a null JSONArray. + * Expects a NullPointerException. + */ + @Test(expected=NullPointerException.class) + public void nullJSONXMLException() { /** - * Attempts to transform an empty XML string to JSON + * Tries to convert a null JSONArray to XML. */ - String xmlStr = ""; - JSONML.toJSONArray(xmlStr); + JSONArray jsonArray= null; + JSONML.toString(jsonArray); + } + + /** + * Attempts to call JSONML.toString() with a null JSONArray. + * Expects a JSONException. + */ + @Test + public void emptyJSONXMLException() { + /** + * Tries to convert an empty JSONArray to XML. + */ + JSONArray jsonArray = new JSONArray(); + try { + JSONML.toString(jsonArray); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONArray[0] not found.". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Attempts to transform an non-XML string to JSON. + * Expects a JSONException + */ + @Test public void nonXMLException() { /** * Attempts to transform a nonXML string to JSON */ String xmlStr = "{ \"this is\": \"not xml\"}"; - JSONML.toJSONArray(xmlStr); + try { + JSONML.toJSONArray(xmlStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Bad XML at 25 [character 26 line 1]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Attempts to transform a JSON document with XML content that + * does not follow JSONML conventions (element name is not first value + * in a nested JSONArray) to a JSONArray then back to string. + * Expects a JSONException + */ + @Test public void emptyTagException() { /** * jsonArrayStr is used to build a JSONArray which is then @@ -70,10 +129,22 @@ public void emptyTagException() { "]"+ "]"; JSONArray jsonArray = new JSONArray(jsonArrayStr); - JSONML.toString(jsonArray); + try { + JSONML.toString(jsonArray); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONArray[0] not a string.". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Attempts to transform a JSON document with XML content that + * does not follow JSONML conventions (element tag has an embedded space) + * to a JSONArray then back to string. Expects a JSONException + */ + @Test public void spaceInTagException() { /** * jsonArrayStr is used to build a JSONArray which is then @@ -94,10 +165,22 @@ public void spaceInTagException() { "]"+ "]"; JSONArray jsonArray = new JSONArray(jsonArrayStr); - JSONML.toString(jsonArray); + try { + JSONML.toString(jsonArray); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "'addr esses' contains a space character.". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Attempts to transform a malformed XML document + * (element tag has a frontslash) to a JSONArray.\ + * Expects a JSONException + */ + @Test public void invalidSlashInTagException() { /** * xmlStr contains XML text which is transformed into a JSONArray. @@ -113,16 +196,22 @@ public void invalidSlashInTagException() { " abc street\n"+ " \n"+ ""; - JSONML.toJSONArray(xmlStr); + try { + JSONML.toJSONArray(xmlStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Misshaped tag at 176 [character 14 line 7]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Malformed XML text (invalid tagname) is transformed into a JSONArray. + * Expects a JSONException. + */ + @Test public void invalidBangInTagException() { - /** - * xmlStr contains XML text which is transformed into a JSONArray. - * In this case, the XML is invalid because an element - * has the invalid name '!'. - */ String xmlStr = "\n"+ "\n"+ " \n"+ ""; - JSONML.toJSONArray(xmlStr); + try { + JSONML.toJSONArray(xmlStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Misshaped meta tag at 216 [character 13 line 11]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Malformed XML text (invalid tagname, no close bracket) is transformed\ + * into a JSONArray. Expects a JSONException. + */ + @Test public void invalidBangNoCloseInTagException() { /** * xmlStr contains XML text which is transformed into a JSONArray. @@ -151,10 +251,21 @@ public void invalidBangNoCloseInTagException() { " \n"+ ""; - JSONML.toJSONArray(xmlStr); + try { + JSONML.toJSONArray(xmlStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Misshaped meta tag at 215 [character 13 line 11]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Malformed XML text (tagname with no close bracket) is transformed\ + * into a JSONArray. Expects a JSONException. + */ + @Test public void noCloseStartTagException() { /** * xmlStr contains XML text which is transformed into a JSONArray. @@ -170,10 +281,21 @@ public void noCloseStartTagException() { " \n"+ ""; - JSONML.toJSONArray(xmlStr); + try { + JSONML.toJSONArray(xmlStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Misplaced '<' at 194 [character 5 line 10]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Malformed XML text (endtag with no name) is transformed\ + * into a JSONArray. Expects a JSONException. + */ + @Test public void noCloseEndTagException() { /** * xmlStr contains XML text which is transformed into a JSONArray. @@ -189,10 +311,21 @@ public void noCloseEndTagException() { " \n"+ " \n"+ ""; - JSONML.toJSONArray(xmlStr); + try { + JSONML.toJSONArray(xmlStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Expected a closing name instead of '>'.". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Malformed XML text (endtag with no close bracket) is transformed\ + * into a JSONArray. Expects a JSONException. + */ + @Test public void noCloseEndBraceException() { /** * xmlStr contains XML text which is transformed into a JSONArray. @@ -208,10 +341,21 @@ public void noCloseEndBraceException() { " \n"+ " "; - JSONML.toJSONArray(xmlStr); + try { + JSONML.toJSONArray(xmlStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Misplaced '<' at 206 [character 1 line 12]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Malformed XML text (incomplete CDATA string) is transformed\ + * into a JSONArray. Expects a JSONException. + */ + @Test public void invalidCDATABangInTagException() { /** * xmlStr contains XML text which is transformed into a JSONArray. @@ -227,27 +371,22 @@ public void invalidCDATABangInTagException() { " \n"+ " \n"+ ""; - JSONML.toJSONArray(xmlStr); - } - - @Test(expected=NullPointerException.class) - public void nullJSONXMLException() { - /** - * Tries to convert a null JSONArray to XML. - */ - JSONArray jsonArray= null; - JSONML.toString(jsonArray); - } - - @Test(expected=JSONException.class) - public void emptyJSONXMLException() { - /** - * Tries to convert an empty JSONArray to XML. - */ - JSONArray jsonArray = new JSONArray(); - JSONML.toString(jsonArray); + try { + JSONML.toJSONArray(xmlStr); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Expected 'CDATA[' at 204 [character 11 line 9]". + equals(e.getMessage())); + } } + /** + * Convert an XML document into a JSONArray, then use JSONML.toString() + * to convert it into a string. This string is then converted back into + * a JSONArray. Both JSONArrays are compared against a control to + * confirm the contents. + */ @Test public void toJSONArray() { /** @@ -290,6 +429,20 @@ public void toJSONArray() { Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } + /** + * Convert an XML document into a JSONObject. Use JSONML.toString() to + * convert it back into a string, and then re-convert it into a JSONObject. + * Both JSONObjects are compared against a control JSONObject to confirm + * the contents. + *

+ * Next convert the XML document into a JSONArray. Use JSONML.toString() to + * convert it back into a string, and then re-convert it into a JSONArray. + * Both JSONArrays are compared against a control JSONArray to confirm + * the contents. + *

+ * This test gives a comprehensive example of how the JSONML + * transformations work. + */ @Test public void toJSONObjectToJSONArray() { /** @@ -505,7 +658,14 @@ public void toJSONObjectToJSONArray() { Util.compareXML(jsonObjectXmlToStr, jsonArrayXmlToStr); } - + /** + * Convert an XML document which contains embedded comments into + * a JSONArray. Use JSONML.toString() to turn it into a string, then + * reconvert it into a JSONArray. Compare both JSONArrays to a control + * JSONArray to confirm the contents. + *

+ * This test shows how XML comments are handled. + */ @Test public void commentsInXML() { From 58d72fe20f2759679f931c2ae03340a15d171606 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 9 Aug 2015 18:19:32 -0500 Subject: [PATCH 160/315] Verify exception messages. Move method comments so JavaDoc will pick them up. --- JSONObjectTest.java | 512 +++++++++++++++++++++++++++++------------- JSONStringerTest.java | 196 +++++++++++----- 2 files changed, 499 insertions(+), 209 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index b818a9e8d..240bbeba7 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -12,7 +12,7 @@ import org.junit.*; /** - * These classes will be used for testing + * Used in testing when a JSONString is needed */ class MyJsonString implements JSONString { @@ -22,6 +22,9 @@ public String toJSONString() { } } +/** + * Used in testing when Bean behavior is needed + */ interface MyBean { public Integer getIntKey(); public Double getDoubleKey(); @@ -32,6 +35,9 @@ interface MyBean { public StringReader getStringReaderKey(); }; +/** + * Used in testing when a Bean containing big numbers is needed + */ interface MyBigNumberBean { public BigInteger getBigInteger(); public BigDecimal getBigDecimal(); @@ -39,34 +45,45 @@ interface MyBigNumberBean { /** * JSONObject, along with JSONArray, are the central classes of the reference app. - * All of the other classes interact with it and JSON functionality would be - * impossible without it. + * All of the other classes interact with them, and JSON functionality would + * otherwise be impossible. */ public class JSONObjectTest { /** * Need a class with some public data members for testing, so - * JSONObjectTest is chosen. + * JSONObjectTest itself will be used for this purpose. + * TODO: Why not use MyBigNumberBean or MyBean? */ public Integer publicInt = 42; public String publicString = "abc"; + /** + * JSONObject built from a bean, but only using a null value. + * Nothing good is expected to happen. + * Expects NullPointerException + */ + @Test(expected=NullPointerException.class) + public void jsonObjectByNullBean() { + MyBean myBean = null; + new JSONObject(myBean); + } + /** + * A JSONObject can be created with no content + */ @Test public void emptyJsonObject() { - /** - * A JSONObject can be created with no content - */ JSONObject jsonObject = new JSONObject(); assertTrue("jsonObject should be empty", jsonObject.length() == 0); } + /** + * A JSONObject can be created from another JSONObject plus a list of names. + * In this test, some of the starting JSONObject keys are not in the + * names list. + */ @Test public void jsonObjectByNames() { - /** - * A JSONObject can be created from another JSONObject plus a list of names. - * In this test, some of the starting JSONObject keys are not in the - * names list. - */ String str = "{"+ "\"trueKey\":true,"+ @@ -92,28 +109,25 @@ public void jsonObjectByNames() { } /** + * JSONObjects can be built from a Map. + * In this test the map is null. * the JSONObject(JsonTokener) ctor is not tested directly since it already * has full coverage from other tests. */ - @Test public void jsonObjectByNullMap() { - /** - * JSONObjects can be built from a Map. - * In this test the map is null. - */ Map map = null; JSONObject jsonObject = new JSONObject(map); JSONObject expectedJsonObject = new JSONObject(); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * JSONObjects can be built from a Map. + * In this test all of the map entries are valid JSON types. + */ @Test public void jsonObjectByMap() { - /** - * JSONObjects can be built from a Map. - * In this test all of the map entries are valid JSON types. - */ String expectedStr = "{"+ "\"trueKey\":true,"+ @@ -136,13 +150,13 @@ public void jsonObjectByMap() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * JSONObjects can be built from a Map. + * In this test the map entries are not valid JSON types. + * The actual conversion is kind of interesting. + */ @Test public void jsonObjectByMapWithUnsupportedValues() { - /** - * JSONObjects can be built from a Map. - * In this test the map entries are not valid JSON types. - * The actual conversion is kind of interesting. - */ String expectedStr = "{"+ "\"key1\":{},"+ @@ -158,12 +172,12 @@ public void jsonObjectByMapWithUnsupportedValues() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * JSONObjects can be built from a Map. + * In this test one of the map values is null + */ @Test public void jsonObjectByMapWithNullValue() { - /** - * JSONObjects can be built from a Map. - * In this test one of the map values is null - */ String expectedStr = "{"+ "\"trueKey\":true,"+ @@ -187,22 +201,12 @@ public void jsonObjectByMapWithNullValue() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } - @Test(expected=NullPointerException.class) - public void jsonObjectByNullBean() { - /** - * JSONObject built from a bean, but only using a null value. - * Nothing good is expected to happen. - */ - MyBean myBean = null; - new JSONObject(myBean); - } - + /** + * JSONObject built from a bean. In this case all but one of the + * bean getters return valid JSON types + */ @Test public void jsonObjectByBean() { - /** - * JSONObject built from a bean. In this case all but one of the - * bean getters return valid JSON types - */ String expectedStr = "{"+ "\"trueKey\":true,"+ @@ -244,14 +248,14 @@ public String toString(){ Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * A bean is also an object. But in order to test the JSONObject + * ctor that takes an object and a list of names, + * this particular bean needs some public + * data members, which have been added to the class. + */ @Test public void jsonObjectByObjectAndNames() { - /** - * A bean is also an object. But in order to test the JSONObject - * ctor that takes an object and a list of names, - * this particular bean needs some public - * data members, which have been added to the class. - */ String expectedStr = "{"+ "\"publicString\":\"abc\","+ @@ -265,6 +269,9 @@ public void jsonObjectByObjectAndNames() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * Exercise the JSONObject from resource bundle functionality + */ @Test public void jsonObjectByResourceBundle() { // TODO: how to improve resource bundle testing? @@ -286,6 +293,9 @@ public void jsonObjectByResourceBundle() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * Exercise the JSONObject.accumulate() method + */ @Test public void jsonObjectAccumulate() { // TODO: should include an unsupported object @@ -311,6 +321,9 @@ public void jsonObjectAccumulate() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * Exercise the JSONObject append() functionality + */ @Test public void jsonObjectAppend() { // TODO: should include an unsupported object @@ -336,6 +349,9 @@ public void jsonObjectAppend() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * Exercise the JSONObject doubleToString() method + */ @Test public void jsonObjectDoubleToString() { String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" }; @@ -349,6 +365,9 @@ public void jsonObjectDoubleToString() { } } + /** + * Exercise some JSONObject get[type] and opt[type] methods + */ @Test public void jsonObjectValues() { String str = @@ -423,9 +442,12 @@ public void jsonObjectValues() { jsonObjectInner.get("myKey").equals("myVal")); } + /** + * Check whether JSONObject handles large or high precision numbers correctly + */ @Test public void stringToValueNumbersTest() { - // Check if library handles large or high precision numbers correctly + assertTrue( "0.2 should be a Double!", JSONObject.stringToValue( "0.2" ) instanceof Double ); assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!", @@ -528,6 +550,9 @@ public void jsonInvalidNumberValues() { jsonObject.get("doubleIdentifier").equals(new Double(0.1))); } + /** + * Tests how JSONObject get[type] handles incorrect types + */ @Test public void jsonObjectNonAndWrongValues() { String str = @@ -547,75 +572,126 @@ public void jsonObjectNonAndWrongValues() { "\"objectKey\":{\"myKey\":\"myVal\"}"+ "}"; JSONObject jsonObject = new JSONObject(str); - int tryCount = 0; - int exceptionCount = 0; try { - ++tryCount; jsonObject.getBoolean("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("expecting an exception message", + "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getBoolean("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a Boolean.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getString("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getString("trueKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"trueKey\"] not a string.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getDouble("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getDouble("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getInt("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getInt("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not an int.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getLong("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getLong("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a long.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getJSONArray("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getJSONArray("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONArray.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getJSONObject("nonKey"); - } catch (JSONException ignore) { ++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } try { - ++tryCount; jsonObject.getJSONObject("stringKey"); - } catch (JSONException ignore) { ++exceptionCount; } - assertTrue("all get calls should have failed", - exceptionCount == tryCount); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONObject.". + equals(e.getMessage())); + } } + /** + * This test documents an unexpected numeric behavior. + * A double that ends with .0 is parsed, serialized, then + * parsed again. On the second parse, it has become an int. + */ @Test public void unexpectedDoubleToIntConversion() { - /** - * This test documents an unexpected numeric behavior. - * A double that ends with .0 is parsed, serialized, then - * parsed again. On the second parse, it has become an int. - */ String key30 = "key30"; String key31 = "key31"; JSONObject jsonObject = new JSONObject(); @@ -636,11 +712,11 @@ public void unexpectedDoubleToIntConversion() { assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1); } - @Test /** - * Important behaviors of big numbers. Includes both JSONObject + * Document behaviors of big numbers. Includes both JSONObject * and JSONArray tests */ + @Test public void bigNumberOperations() { /** * JSONObject tries to parse BigInteger as a bean, but it only has @@ -874,6 +950,10 @@ public void jsonObjectNames() { Util.compareActualVsExpectedStringArrays(names, jsonObjectTestExpectedNames); } + /** + * Populate a JSONArray from an empty JSONObject names() method. + * It should be empty. + */ @Test public void emptyJsonObjectNamesToJsonAray() { JSONObject jsonObject = new JSONObject(); @@ -881,6 +961,10 @@ public void emptyJsonObjectNamesToJsonAray() { assertTrue("jsonArray should be null", jsonArray == null); } + /** + * Populate a JSONArray from a JSONObject names() method. + * Confirm that it contains the expected names. + */ @Test public void jsonObjectNamesToJsonAray() { String str = @@ -907,6 +991,9 @@ public void jsonObjectNamesToJsonAray() { Util.compareActualVsExpectedStringArrays(names, expectedNames); } + /** + * Exercise the JSONObject increment() method. + */ @Test public void jsonObjectIncrement() { String str = @@ -919,19 +1006,31 @@ public void jsonObjectIncrement() { "\"keyInt\":3,"+ "\"keyLong\":9999999993,"+ "\"keyDouble\":3.1,"+ - // Should work the same way on any platform! @see https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 - // This is the effect of a float to double conversion and is inherent to the shortcomings of the IEEE 754 format, when - // converting 32-bit into double-precision 64-bit. - // Java type-casts float to double. A 32 bit float is type-casted to 64 bit double by simply appending zero-bits to the - // mantissa (and extended the signed exponent by 3 bits.) and there is no way to obtain more information than it is - // stored in the 32-bits float. - - // Like 1/3 cannot be represented as base10 number because it is periodically, 1/5 (for example) cannot be represented - // as base2 number since it is periodically in base2 (take a look at http://www.h-schmidt.net/FloatConverter/) - // The same happens to 3.1, that decimal number (base10 representation) is periodic in base2 representation, therefore - // appending zero-bits is inaccurate. Only repeating the periodically occuring bits (0110) would be a proper conversion. - // However one cannot detect from a 32 bit IEE754 representation which bits would "repeat infinitely", since the missing - // bits would not fit into the 32 bit float, i.e. the information needed simply is not there! + /** + * Should work the same way on any platform! @see + * https://docs.oracle + * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is + * the effect of a float to double conversion and is inherent to + * the shortcomings of the IEEE 754 format, when converting + * 32-bit into double-precision 64-bit. Java type-casts float to + * double. A 32 bit float is type-casted to 64 bit double by + * simply appending zero-bits to the mantissa (and extended the + * signed exponent by 3 bits.) and there is no way to obtain + * more information than it is stored in the 32-bits float. + * + * Like 1/3 cannot be represented as base10 number because it is + * periodically, 1/5 (for example) cannot be represented as + * base2 number since it is periodically in base2 (take a look + * at http://www.h-schmidt.net/FloatConverter/) The same happens + * to 3.1, that decimal number (base10 representation) is + * periodic in base2 representation, therefore appending + * zero-bits is inaccurate. Only repeating the periodically + * occuring bits (0110) would be a proper conversion. However + * one cannot detect from a 32 bit IEE754 representation which + * bits would "repeat infinitely", since the missing bits would + * not fit into the 32 bit float, i.e. the information needed + * simply is not there! + */ "\"keyFloat\":3.0999999046325684,"+ "}"; JSONObject jsonObject = new JSONObject(str); @@ -948,24 +1047,28 @@ public void jsonObjectIncrement() { JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - /* - float f = 3.1f; - double df = (double) f; - double d = 3.1d; - System.out.println(Integer.toBinaryString(Float.floatToRawIntBits(f))); - System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(df))); - System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(d))); - - - Float: - seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm - 1000000010001100110011001100110 - - Double - seeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm - 10000000 10001100110011001100110 - 100000000001000110011001100110011000000000000000000000000000000 - 100000000001000110011001100110011001100110011001100110011001101 - */ - // Examples of well documented but probably unexpected behavior in java / with 32-bit float to 64-bit float conversion. + /** + * float f = 3.1f; + * double df = (double) f; + * double d = 3.1d; + * System.out.println(Integer.toBinaryString(Float.floatToRawIntBits(f))); + * System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(df))); + * System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(d))); + * + * - Float: + * seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm + * 1000000010001100110011001100110 + * - Double + * seeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm + * 10000000 10001100110011001100110 + * 100000000001000110011001100110011000000000000000000000000000000 + * 100000000001000110011001100110011001100110011001100110011001101 + */ + + /** + * Examples of well documented but probably unexpected behavior in + * java / with 32-bit float to 64-bit float conversion. + */ assertFalse("Document unexpected behaviour with explicit type-casting float as double!", (double)0.2f == 0.2d ); assertFalse("Document unexpected behaviour with implicit type-cast!", 0.2f == 0.2d ); Double d1 = new Double( 1.1f ); @@ -997,7 +1100,9 @@ public void jsonObjectIncrement() { } - + /** + * Exercise JSONObject numberToString() method + */ @Test public void jsonObjectNumberToString() { String str; @@ -1017,6 +1122,9 @@ public void jsonObjectNumberToString() { assertTrue("expected 5000000 actual "+str, str.equals("5000000")); } + /** + * Exercise JSONObject put() and similar() methods + */ @Test public void jsonObjectPut() { String expectedStr = @@ -1090,6 +1198,9 @@ public void jsonObjectPut() { !aCompareArrayJsonObject.similar(bCompareArrayJsonObject)); } + /** + * Exercise JSONObject toString() method + */ @Test public void jsonObjectToString() { String str = @@ -1110,6 +1221,13 @@ public void jsonObjectToString() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * Explores how JSONObject handles maps. Insert a string/string map + * as a value in a JSONObject. It will remain a map. Convert the + * JSONObject to string, then create a new JSONObject from the string. + * In the new JSONObject, the value will be stored as a nested JSONObject. + * Confirm that map and nested JSONObject have the same contents. + */ @Test @SuppressWarnings("unchecked") public void jsonObjectToStringSuppressWarningOnCastToMap() { @@ -1124,7 +1242,8 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { expectedJsonObject.keySet().iterator().next())); /** * Can't do a Util compare because although they look the same - * in the debugger, one is a map and the other is a JSONObject. + * in the debugger, one is a map and the other is a JSONObject. + * TODO: write a util method for such comparisons */ map = (Map)jsonObject.get("key"); JSONObject mapJsonObject = expectedJsonObject.getJSONObject("key"); @@ -1137,6 +1256,13 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { map.get(map.keySet().iterator().next()))); } + /** + * Explores how JSONObject handles collections. Insert a string collection + * as a value in a JSONObject. It will remain a collection. Convert the + * JSONObject to string, then create a new JSONObject from the string. + * In the new JSONObject, the value will be stored as a nested JSONArray. + * Confirm that collection and nested JSONArray have the same contents. + */ @Test @SuppressWarnings("unchecked") public void jsonObjectToStringSuppressWarningOnCastToCollection() { @@ -1166,6 +1292,9 @@ public void jsonObjectToStringSuppressWarningOnCastToCollection() { } } + /** + * Exercises the JSONObject.valueToString() method for various types + */ @Test public void valueToString() { @@ -1211,6 +1340,12 @@ public void valueToString() { jsonArray.toString().equals(JSONObject.valueToString(array))); } + /** + * Exercise the JSONObject wrap() method. Sometimes wrap() will change + * the object being wrapped, other times not. The purpose of wrap() is + * to ensure the value is packaged in a way that is compatible with how + * a JSONObject value or JSONArray value is supposed to be stored. + */ @Test public void wrapObject() { // wrap(null) returns NULL @@ -1224,8 +1359,9 @@ public void wrapObject() { /** * This test is to document the preferred behavior if BigDecimal is - * supported. At the present time, bd returns as a string, since it - * is recognized as being a Java package class. + * supported. Previously bd returned as a string, since it + * is recognized as being a Java package class. Now with explicit + * support for big numbers, it remains a BigDecimal */ Object bdWrap = JSONObject.wrap(BigDecimal.ONE); assertTrue("BigDecimal.ONE evaluates to ONE", @@ -1273,88 +1409,128 @@ public void wrapObject() { // TODO test wrap(package) } + /** + * Explore how JSONObject handles parsing errors. + */ @Test public void jsonObjectParsingErrors() { - int tryCount = 0; - int exceptionCount = 0; try { // does not start with '{' - ++tryCount; String str = "abc"; new JSONObject(str); - } catch (JSONException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "A JSONObject text must begin with '{' at 1 [character 2 line 1]". + equals(e.getMessage())); + } try { // does not end with '}' - ++tryCount; String str = "{"; new JSONObject(str); - } catch (JSONException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "A JSONObject text must end with '}' at 2 [character 3 line 1]". + equals(e.getMessage())); + } try { // key with no ':' - ++tryCount; String str = "{\"myKey\" = true}"; new JSONObject(str); - } catch (JSONException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Expected a ':' after a key at 10 [character 11 line 1]". + equals(e.getMessage())); + } try { // entries with no ',' separator - ++tryCount; String str = "{\"myKey\":true \"myOtherKey\":false}"; new JSONObject(str); - } catch (JSONException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Expected a ',' or '}' at 15 [character 16 line 1]". + equals(e.getMessage())); + } try { // append to wrong key - ++tryCount; String str = "{\"myKey\":true, \"myOtherKey\":false}"; JSONObject jsonObject = new JSONObject(str); jsonObject.append("myKey", "hello"); - } catch (JSONException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[myKey] is not a JSONArray.". + equals(e.getMessage())); + } try { // increment wrong key - ++tryCount; String str = "{\"myKey\":true, \"myOtherKey\":false}"; JSONObject jsonObject = new JSONObject(str); jsonObject.increment("myKey"); - } catch (JSONException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Unable to increment [\"myKey\"].". + equals(e.getMessage())); + } try { // invalid key - ++tryCount; String str = "{\"myKey\":true, \"myOtherKey\":false}"; JSONObject jsonObject = new JSONObject(str); jsonObject.get(null); - } catch (JSONException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Null key.". + equals(e.getMessage())); + } try { // invalid numberToString() - ++tryCount; JSONObject.numberToString((Number)null); - } catch (JSONException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Null pointer". + equals(e.getMessage())); + } try { // null put key - ++tryCount; JSONObject jsonObject = new JSONObject("{}"); jsonObject.put(null, 0); - } catch (NullPointerException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (NullPointerException ignored) { + } try { // multiple putOnce key - ++tryCount; JSONObject jsonObject = new JSONObject("{}"); jsonObject.putOnce("hello", "world"); jsonObject.putOnce("hello", "world!"); - } catch (JSONException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("", true); + } try { // test validity of invalid double - ++tryCount; JSONObject.testValidity(Double.NaN); - } catch (JSONException ignore) {++exceptionCount; } + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("", true); + } try { // test validity of invalid float - ++tryCount; JSONObject.testValidity(Float.NEGATIVE_INFINITY); - } catch (JSONException ignore) {++exceptionCount; } - - assertTrue("all tries should have failed", - exceptionCount == tryCount); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("", true); + } } + /** + * Confirm behavior when putOnce() is called with null parameters + */ @Test public void jsonObjectPutOnceNull() { JSONObject jsonObject = new JSONObject(); @@ -1362,6 +1538,9 @@ public void jsonObjectPutOnceNull() { assertTrue("jsonObject should be empty", jsonObject.length() == 0); } + /** + * Exercise JSONObject opt(key, default) method + */ @Test public void jsonObjectOptDefault() { @@ -1382,6 +1561,9 @@ public void jsonObjectOptDefault() { "hi".equals(jsonObject.optString("hiKey", "hi"))); } + /** + * Confirm behavior when JSONObject put(key, null object) is called + */ @Test public void jsonObjectputNull() { @@ -1397,6 +1579,11 @@ public void jsonObjectputNull() { jsonObjectPutNull.length() == 0); } + /** + * Exercise JSONObject quote() method + * This purpose of quote() is to ensure that for strings with embedded + * quotes, the quotes are properly escaped. + */ @Test public void jsonObjectQuote() { String str; @@ -1435,6 +1622,10 @@ public void jsonObjectQuote() { "\"\u1234\\u0088\"".equals(quotedStr)); } + /** + * Confirm behavior when JSONObject stringToValue() is called for an + * empty string + */ @Test public void stringToValue() { String str = ""; @@ -1443,12 +1634,18 @@ public void stringToValue() { "".equals(valueStr)); } + /** + * Confirm behavior when toJSONArray is called with a null value + */ @Test public void toJSONArray() { assertTrue("toJSONArray() with null names should be null", null == new JSONObject().toJSONArray(null)); } + /** + * Exercise the JSONObject write() method + */ @Test public void write() { String str = "{\"key\":\"value\"}"; @@ -1462,6 +1659,9 @@ public void write() { expectedStr.equals(actualStr)); } + /** + * Exercise the JSONObject equals() method + */ @Test public void equals() { String str = "{\"key\":\"value\"}"; @@ -1470,6 +1670,10 @@ public void equals() { aJsonObject.equals(aJsonObject)); } + /** + * JSON null is not the same as Java null. This test examines the differences + * in how they are handled by JSON-java. + */ @Test public void jsonObjectNullOperations() { /** diff --git a/JSONStringerTest.java b/JSONStringerTest.java index d76bfb105..7a3506443 100644 --- a/JSONStringerTest.java +++ b/JSONStringerTest.java @@ -1,100 +1,178 @@ package org.json.junit; +import static org.junit.Assert.*; + import org.json.*; import org.junit.Test; /** - * Tests for JSON-Java JSONStringer.java + * Tests for JSON-Java JSONStringer. + * TODO: Could use a lot more testing. For example, cascade-style productions. */ public class JSONStringerTest { - @Test(expected=JSONException.class) + /** + * Object with a null key. + * Expects a JSONException. + */ + @Test public void nullKeyException() { JSONStringer jsonStringer = new JSONStringer(); jsonStringer.object(); - jsonStringer.key(null); + try { + jsonStringer.key(null); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expected an exception message", + "Null key.". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Add a key with no object. + * Expects a JSONException. + */ + @Test public void outOfSequenceException() { JSONStringer jsonStringer = new JSONStringer(); - jsonStringer.key("hi"); + try { + jsonStringer.key("hi"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expected an exception message", + "Misplaced key.". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Missplace an array. + * Expects a JSONException + */ + @Test public void missplacedArrayException() { JSONStringer jsonStringer = new JSONStringer(); jsonStringer.object().endObject(); - jsonStringer.array(); + try { + jsonStringer.array(); + } catch (JSONException e) { + assertTrue("Expected an exception message", + "Misplaced array.". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Missplace an endErray. + * Expects a JSONException + */ + @Test public void missplacedEndArrayException() { JSONStringer jsonStringer = new JSONStringer(); jsonStringer.object(); - jsonStringer.endArray(); + try { + jsonStringer.endArray(); + } catch (JSONException e) { + assertTrue("Expected an exception message", + "Misplaced endArray.". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Missplace an endObject. + * Expects a JSONException + */ + @Test public void missplacedEndObjectException() { JSONStringer jsonStringer = new JSONStringer(); jsonStringer.array(); - jsonStringer.endObject(); + try { + jsonStringer.endObject(); + } catch (JSONException e) { + assertTrue("Expected an exception message", + "Misplaced endObject.". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Missplace an object. + * Expects a JSONException. + */ + @Test public void missplacedObjectException() { JSONStringer jsonStringer = new JSONStringer(); jsonStringer.object().endObject(); - jsonStringer.object(); + try { + jsonStringer.object(); + } catch (JSONException e) { + assertTrue("Expected an exception message", + "Misplaced object.". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Exceeds implementation max nesting depth. + * Expects a JSONException + */ + @Test public void exceedNestDepthException() { - new JSONStringer().object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). - key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(); + try { + new JSONStringer().object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(); + } catch (JSONException e) { + assertTrue("Expected an exception message", + "". + equals(e.getMessage())); + } } + /** + * Build a JSON doc using JSONString API calls, + * then convert to JSONObject + */ @Test public void simpleObjectString() { String expectedStr = @@ -123,6 +201,10 @@ public void simpleObjectString() { Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + /** + * Build a JSON doc using JSONString API calls, + * then convert to JSONArray + */ @Test public void simpleArrayString() { String expectedStr = @@ -149,6 +231,10 @@ public void simpleArrayString() { Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } + /** + * Build a nested JSON doc using JSONString API calls, + * then convert to JSONObject + */ @Test public void complexObjectString() { String expectedStr = From 1081ae092bf5dc37d402c8f3c9e3a56cbb5d8fe2 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 9 Aug 2015 18:19:51 -0500 Subject: [PATCH 161/315] Move method comments so JavaDoc will pick them up. --- Util.java | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/Util.java b/Util.java index 635b7a625..eb33ab5b2 100644 --- a/Util.java +++ b/Util.java @@ -6,12 +6,16 @@ import org.json.*; +/** + * These are helpful utility methods that perform basic comparisons + * between various objects. In most cases, the comparisons are not + * order-dependent, or else the order is known. + */ public class Util { - - /** - * Compares two json arrays for equality + * Compares two JSONArrays for equality. + * The arrays need not be in the same order. * @param jsonArray created by the code to be tested * @param expectedJsonArray created specifically for comparing */ @@ -27,7 +31,8 @@ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray, } /** - * Compares two json objects for equality + * Compares two JSONObjects for equality. The objects need not be + * in the same order * @param jsonObject created by the code to be tested * @param expectedJsonObject created specifically for comparing */ @@ -68,6 +73,8 @@ private static void compareActualVsExpectedObjects(Object value, * Certain helper classes (e.g. XML) may create Long instead of * Integer for small int values. As long as both are Numbers, * just compare the toString() values. + * TODO: this may not work in the case where the underlying types + * do not have the same precision. */ if (!(value instanceof Number && expectedValue instanceof Number)) { assertTrue("object types should be equal for actual: "+ @@ -78,12 +85,31 @@ private static void compareActualVsExpectedObjects(Object value, value.getClass().toString().equals( expectedValue.getClass().toString())); } + /** + * When in doubt, compare by string + * TODO: should not this be an else to the previous condition? + */ assertTrue("string values should be equal for actual: "+ value.toString()+" expected: "+expectedValue.toString(), value.toString().equals(expectedValue.toString())); } } + /** + * Sometimes test completion requires comparison of JSONArray objects that + * were produced from a JSONObject, and so unordered. This method is + * imperfect since it only compares the array elements and won't catch + * JSON syntax errors but at least it does not rely on ordering + *

+ * It is expected that the arrays to be compared come from JSONArray + * instances which have been rendered by toString(), and whose syntax + * chars have been removed. + *

+ * TODO: why are we not simply comparing the JSONArrays? + *

+ * @param names an array of strings for comparison + * @param expectedNames the other array of strings for comparison + */ public static void compareActualVsExpectedStringArrays(String[] names, String [] expectedNames) { assertTrue("Array lengths should be equal", @@ -97,6 +123,13 @@ public static void compareActualVsExpectedStringArrays(String[] names, } } + /** + * This is stopgap test utility. It is meant to compare strings + * of XML, but it does not take ordering into account and should + * not be expected to work correctly with complex XML. + * @param aXmlStr an XML doc to be compared + * @param bXmlStr the other XML doc to be compared + */ public static void compareXML(String aXmlStr, String bXmlStr) { // TODO For simple tests this may be adequate, but it won't work for // elements with multiple attributes and possibly other cases as well. From ccbec8127c844a8aa41e74a04199ff20b402e6a8 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 9 Aug 2015 18:24:47 -0500 Subject: [PATCH 162/315] Added some class documentation --- MyEnum.java | 3 +++ MyEnumField.java | 3 +++ StringsResourceBundle.java | 3 +++ TestRunner.java | 4 ++++ 4 files changed, 13 insertions(+) diff --git a/MyEnum.java b/MyEnum.java index d2fe08d8e..0952bc246 100644 --- a/MyEnum.java +++ b/MyEnum.java @@ -1,5 +1,8 @@ package org.json.junit; +/** + * An enum with no methods or data + */ public enum MyEnum { VAL1, VAL2, diff --git a/MyEnumField.java b/MyEnumField.java index 7efec54fc..cff565af8 100644 --- a/MyEnumField.java +++ b/MyEnumField.java @@ -1,5 +1,8 @@ package org.json.junit; +/** + * An enum that contains getters and some internal fields + */ public enum MyEnumField { VAL1(1, "val 1"), VAL2(2, "val 2"), diff --git a/StringsResourceBundle.java b/StringsResourceBundle.java index b1b9df4dd..83d932239 100644 --- a/StringsResourceBundle.java +++ b/StringsResourceBundle.java @@ -2,6 +2,9 @@ import java.util.*; +/** + * A resource bundle class + */ public class StringsResourceBundle extends ListResourceBundle { public Object[][] getContents() { return contents; diff --git a/TestRunner.java b/TestRunner.java index 8f9dd8956..d13c63ef6 100644 --- a/TestRunner.java +++ b/TestRunner.java @@ -4,6 +4,10 @@ import org.junit.runner.Result; import org.junit.runner.notification.Failure; +/** + * Invoke this class main method if you want to run unit tests from the + * command line. If successful, will print "true" to stdout. + */ public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(JunitTestSuite.class); From 8f71e01ae3cb64c8d9db11e45cc13dc101c861f0 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 9 Aug 2015 18:30:16 -0500 Subject: [PATCH 163/315] Add method comments so JavaDoc will pick them up. --- PropertyTest.java | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/PropertyTest.java b/PropertyTest.java index 435448894..60d3eb5de 100644 --- a/PropertyTest.java +++ b/PropertyTest.java @@ -9,26 +9,35 @@ /** - * Tests for JSON-Java Property.java + * Tests for JSON-Java Property */ public class PropertyTest { + /** + * JSONObject from null properties object should + * result in an empty JSONObject. + */ @Test public void shouldHandleNullProperties() { - Properties properties = null; JSONObject jsonObject = Property.toJSONObject(properties); assertTrue("jsonObject should be empty", jsonObject.length() == 0); } + /** + * JSONObject from empty properties object should + * result in an empty JSONObject. + */ @Test public void shouldHandleEmptyProperties() { - Properties properties = new Properties(); JSONObject jsonObject = Property.toJSONObject(properties); assertTrue("jsonObject should be empty", jsonObject.length() == 0); } + /** + * JSONObject from simple properties object. + */ @Test public void shouldHandleProperties() { Properties properties = new Properties(); @@ -54,6 +63,10 @@ public void shouldHandleProperties() { "Indianapolis".equals(jsonObject.get("Indiana"))); } + /** + * Null JSONObject toProperties() should result in an empty + * Properties object. + */ @Test public void shouldHandleNullJSONProperty() { JSONObject jsonObject= null; @@ -62,6 +75,10 @@ public void shouldHandleNullJSONProperty() { properties.size() == 0); } + /** + * Properties should convert to JSONObject, and back to + * Properties without changing. + */ @Test public void shouldHandleJSONProperty() { Properties properties = new Properties(); From 1f6e07c91445f4abeae07fed982cfe727918b739 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 19 Aug 2015 19:16:22 -0500 Subject: [PATCH 164/315] sample tests for XML.toJSONObject(Reader) --- XMLTest.java | 200 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 179 insertions(+), 21 deletions(-) diff --git a/XMLTest.java b/XMLTest.java index 58ddee594..b7d747773 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -2,8 +2,11 @@ import static org.junit.Assert.*; +import java.io.*; + import org.json.*; -import org.junit.Test; +import org.junit.*; +import org.junit.rules.*; /** @@ -11,15 +14,27 @@ * Note: noSpace() will be tested by JSONMLTest */ public class XMLTest { + /** + * JUnit supports temporary files and folders that are cleaned up after the test. + * https://garygregory.wordpress.com/2010/01/20/junit-tip-use-rules-to-manage-temporary-files-and-folders/ + */ + @Rule + public TemporaryFolder testFolder = new TemporaryFolder(); + /** + * JSONObject from a null XML string. + * Expects a NullPointerException + */ @Test(expected=NullPointerException.class) public void shouldHandleNullXML() { - String xmlStr = null; JSONObject jsonObject = XML.toJSONObject(xmlStr); assertTrue("jsonObject should be empty", jsonObject.length() == 0); } + /** + * Empty JSONObject from an empty XML string. + */ @Test public void shouldHandleEmptyXML() { @@ -28,6 +43,9 @@ public void shouldHandleEmptyXML() { assertTrue("jsonObject should be empty", jsonObject.length() == 0); } + /** + * Empty JSONObject from a non-XML string. + */ @Test public void shouldHandleNonXML() { String xmlStr = "{ \"this is\": \"not xml\"}"; @@ -35,7 +53,11 @@ public void shouldHandleNonXML() { assertTrue("xml string should be empty", jsonObject.length() == 0); } - @Test(expected=JSONException.class) + /** + * Invalid XML string (tag contains a frontslash). + * Expects a JSONException + */ + @Test public void shouldHandleInvalidSlashInTag() { String xmlStr = "\n"+ @@ -46,10 +68,21 @@ public void shouldHandleInvalidSlashInTag() { " abc street\n"+ " \n"+ ""; - XML.toJSONObject(xmlStr); + try { + XML.toJSONObject(xmlStr); + assertTrue("Expecting a JSONException", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Misshaped tag at 176 [character 14 line 5]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Invalid XML string ('!' char in tag) + * Expects a JSONException + */ + @Test public void shouldHandleInvalidBangInTag() { String xmlStr = "\n"+ @@ -60,10 +93,21 @@ public void shouldHandleInvalidBangInTag() { " \n"+ " \n"+ ""; - XML.toJSONObject(xmlStr); + try { + XML.toJSONObject(xmlStr); + assertTrue("Expecting a JSONException", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Misshaped meta tag at 215 [character 13 line 8]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Invalid XML string ('!' char and no closing tag brace) + * Expects a JSONException + */ + @Test public void shouldHandleInvalidBangNoCloseInTag() { String xmlStr = "\n"+ @@ -74,10 +118,21 @@ public void shouldHandleInvalidBangNoCloseInTag() { " \n"+ ""; - XML.toJSONObject(xmlStr); + try { + XML.toJSONObject(xmlStr); + assertTrue("Expecting a JSONException", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Misshaped meta tag at 214 [character 13 line 8]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Invalid XML string (no end brace for tag) + * Expects JSONException + */ + @Test public void shouldHandleNoCloseStartTag() { String xmlStr = "\n"+ @@ -88,10 +143,21 @@ public void shouldHandleNoCloseStartTag() { " \n"+ ""; - XML.toJSONObject(xmlStr); + try { + XML.toJSONObject(xmlStr); + assertTrue("Expecting a JSONException", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Misplaced '<' at 193 [character 4 line 7]". + equals(e.getMessage())); + } } - @Test(expected=JSONException.class) + /** + * Invalid XML string (partial CDATA chars in tag name) + * Expects JSONException + */ + @Test public void shouldHandleInvalidCDATABangInTag() { String xmlStr = "\n"+ @@ -102,15 +168,29 @@ public void shouldHandleInvalidCDATABangInTag() { " \n"+ " \n"+ ""; - XML.toJSONObject(xmlStr); + try { + XML.toJSONObject(xmlStr); + assertTrue("Expecting a JSONException", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Expected 'CDATA[' at 204 [character 11 line 6]". + equals(e.getMessage())); + } } + /** + * Null JSONObject in XML.toString() + * Expects NullPointerException + */ @Test(expected=NullPointerException.class) public void shouldHandleNullJSONXML() { JSONObject jsonObject= null; XML.toString(jsonObject); } + /** + * Empty JSONObject in XML.toString() + */ @Test public void shouldHandleEmptyJSONXML() { JSONObject jsonObject= new JSONObject(); @@ -118,6 +198,9 @@ public void shouldHandleEmptyJSONXML() { assertTrue("xml string should be empty", xmlStr.length() == 0); } + /** + * No SML start tag. The ending tag ends up being treated as content. + */ @Test public void shouldHandleNoStartTag() { String xmlStr = @@ -138,6 +221,9 @@ public void shouldHandleNoStartTag() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * Valid XML to JSONObject + */ @Test public void shouldHandleSimpleXML() { String xmlStr = @@ -168,12 +254,15 @@ public void shouldHandleSimpleXML() { "},\"xsi:noNamespaceSchemaLocation\":"+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ "XMLSchema-instance\"}}"; - - JSONObject expectedJsonObject = new JSONObject(expectedStr); - JSONObject jsonObject = XML.toJSONObject(xmlStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + + compareStringToJSONObject(xmlStr, expectedStr); + compareReaderToJSONObject(xmlStr, expectedStr); + compareFileToJSONObject(xmlStr, expectedStr); } + /** + * Valid XML with comments to JSONObject + */ @Test public void shouldHandleCommentsInXML() { @@ -197,6 +286,9 @@ public void shouldHandleCommentsInXML() { Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } + /** + * Valid XML to XML.toString() + */ @Test public void shouldHandleToString() { String xmlStr = @@ -226,6 +318,10 @@ public void shouldHandleToString() { Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } + /** + * Converting a JSON doc containing '>' content to JSONObject, then + * XML.toString() should result in valid XML. + */ @Test public void shouldHandleContentNoArraytoString() { String expectedStr = @@ -242,6 +338,11 @@ public void shouldHandleContentNoArraytoString() { finalStr+"]", expectedFinalStr.equals(finalStr)); } + /** + * Converting a JSON doc containing a 'content' array to JSONObject, then + * XML.toString() should result in valid XML. + * TODO: This is probably an error in how the 'content' keyword is used. + */ @Test public void shouldHandleContentArraytoString() { String expectedStr = @@ -259,6 +360,10 @@ public void shouldHandleContentArraytoString() { finalStr+"]", expectedFinalStr.equals(finalStr)); } + /** + * Converting a JSON doc containing a named array to JSONObject, then + * XML.toString() should result in valid XML. + */ @Test public void shouldHandleArraytoString() { String expectedStr = @@ -276,6 +381,10 @@ public void shouldHandleArraytoString() { finalStr+"]", expectedFinalStr.equals(finalStr)); } + /** + * Converting a JSON doc containing a named array of nested arrays to + * JSONObject, then XML.toString() should result in valid XML. + */ @Test public void shouldHandleNestedArraytoString() { String xmlStr = @@ -297,10 +406,10 @@ public void shouldHandleNestedArraytoString() { /** + * Possible bug: * Illegal node-names must be converted to legal XML-node-names. * The given example shows 2 nodes which are valid for JSON, but not for XML. * Therefore illegal arguments should be converted to e.g. an underscore (_). - * */ @Test public void shouldHandleIllegalJSONNodeNames() @@ -322,6 +431,9 @@ public void shouldHandleIllegalJSONNodeNames() assertEquals(expected, result); } + /** + * JSONObject with NULL value, to XML.toString() + */ @Test public void shouldHandleNullNodeValue() { @@ -338,12 +450,11 @@ public void shouldHandleNullNodeValue() assertEquals(actualXML, resultXML); } + /** + * Investigate exactly how the "content" keyword works + */ @Test public void contentOperations() { - /** - * Make sure we understand exactly how the "content" keyword works - */ - /** * When a standalone Date: Sun, 23 Aug 2015 18:54:10 -0500 Subject: [PATCH 165/315] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 75d7861dd..19d0354f8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # JSON-Java-unit-test Unit tests to validate the JSON-Java GitHub project code
+ https://github.com/douglascrockford/JSON-java
*These tests are a work in progress. Help from interested developers is welcome.*
From 1a5718dc396e4cb841aed023c1d059d636d3463e Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 27 Sep 2015 23:55:32 -0500 Subject: [PATCH 166/315] Commenting out some code until JSON-java supports XML.toJSONObject(Reader) --- XMLTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/XMLTest.java b/XMLTest.java index b7d747773..2f770ef6a 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -612,10 +612,14 @@ private void compareStringToJSONObject(String xmlStr, String expectedStr) { * @param expectedStr the expected JSON string */ private void compareReaderToJSONObject(String xmlStr, String expectedStr) { + /* + * Commenting out this method until the JSON-java code is updated + * to support XML.toJSONObject(reader) JSONObject expectedJsonObject = new JSONObject(expectedStr); Reader reader = new StringReader(xmlStr); JSONObject jsonObject = XML.toJSONObject(reader); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + */ } /** @@ -626,6 +630,9 @@ private void compareReaderToJSONObject(String xmlStr, String expectedStr) { * @throws IOException */ private void compareFileToJSONObject(String xmlStr, String expectedStr) { + /* + * Commenting out this method until the JSON-java code is updated + * to support XML.toJSONObject(reader) try { JSONObject expectedJsonObject = new JSONObject(expectedStr); File tempFile = testFolder.newFile("fileToJSONObject.xml"); @@ -638,5 +645,6 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) { } catch (IOException e) { assertTrue("file writer error: " +e.getMessage(), false); } + */ } } \ No newline at end of file From 4b0db65877c18132d6b45b0a147ac5ba0c912fd4 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 12 Oct 2015 13:48:05 -0400 Subject: [PATCH 167/315] Fixes NPE in XML --- XMLTest.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/XMLTest.java b/XMLTest.java index 2f770ef6a..f680e18b1 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -1,12 +1,17 @@ package org.json.junit; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; -import java.io.*; +import java.io.IOException; -import org.json.*; -import org.junit.*; -import org.junit.rules.*; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.XML; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** @@ -180,9 +185,8 @@ public void shouldHandleInvalidCDATABangInTag() { /** * Null JSONObject in XML.toString() - * Expects NullPointerException */ - @Test(expected=NullPointerException.class) + @Test public void shouldHandleNullJSONXML() { JSONObject jsonObject= null; XML.toString(jsonObject); From cb63a968fa68ac39f94dfffb0962d91ed1d28862 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 12 Oct 2015 14:49:18 -0400 Subject: [PATCH 168/315] fixes test case to validate the input/output of the function being tested --- XMLTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/XMLTest.java b/XMLTest.java index f680e18b1..fb3b42c49 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -189,7 +189,8 @@ public void shouldHandleInvalidCDATABangInTag() { @Test public void shouldHandleNullJSONXML() { JSONObject jsonObject= null; - XML.toString(jsonObject); + String actualXml=XML.toString(jsonObject); + assertEquals("generated XML does not equal expected XML","\"null\"",actualXml); } /** From 4a2f9b8cd395cf60582ec44fe0ebd66c3ba26769 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 14 Oct 2015 15:15:24 -0400 Subject: [PATCH 169/315] Adds test cases for corrected generic constructors and put methods --- JSONArrayTest.java | 123 +++++++++++++++++++++++++-- JSONObjectTest.java | 203 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 285 insertions(+), 41 deletions(-) diff --git a/JSONArrayTest.java b/JSONArrayTest.java index 4c2145696..1cb5dc301 100644 --- a/JSONArrayTest.java +++ b/JSONArrayTest.java @@ -1,10 +1,17 @@ package org.json.junit; -import static org.junit.Assert.*; - -import java.util.*; - -import org.json.*; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import org.junit.Test; @@ -80,6 +87,112 @@ public void badObject() { equals(e.getMessage())); } } + + /** + * Verifies that the constructor has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyConstructor() { + + final JSONArray expected = new JSONArray("[10]"); + + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONArray jaRaw = new JSONArray(myRawC); + + Collection myCInt = Collections.singleton(Integer.valueOf(10)); + JSONArray jaInt = new JSONArray(myCInt); + + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONArray jaObj = new JSONArray(myCObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + } + + /** + * Verifies that the put Collection has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyPutCollection() { + + final JSONArray expected = new JSONArray("[[10]]"); + + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONArray jaRaw = new JSONArray(); + jaRaw.put(myRawC); + + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONArray jaObj = new JSONArray(); + jaObj.put(myCObj); + + Collection myCInt = Collections.singleton(Integer.valueOf(10)); + JSONArray jaInt = new JSONArray(); + jaInt.put(myCInt); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); + } + + + /** + * Verifies that the put Map has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyPutMap() { + + final JSONArray expected = new JSONArray("[{\"myKey\":10}]"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONArray jaRaw = new JSONArray(); + jaRaw.put(myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONArray jaStrObj = new JSONArray(); + jaStrObj.put(myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONArray jaStrInt = new JSONArray(); + jaStrInt.put(myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONArray jaObjObj = new JSONArray(); + jaObjObj.put(myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); + } /** * Create a JSONArray doc with a variety of different elements. diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 240bbeba7..1eec1436f 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -1,15 +1,32 @@ package org.json.junit; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import java.io.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.*; - -import org.json.*; -import org.junit.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import org.json.CDL; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONString; +import org.json.XML; +import org.junit.Test; /** * Used in testing when a JSONString is needed @@ -149,6 +166,122 @@ public void jsonObjectByMap() { JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); } + + /** + * Verifies that the constructor has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyConstructor() { + + final JSONObject expected = new JSONObject("{\"myKey\":10}"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONObject jaStrObj = new JSONObject(myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONObject jaStrInt = new JSONObject(myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONObject jaObjObj = new JSONObject(myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); + } + + /** + * Verifies that the put Collection has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyPutCollection() { + + final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); + + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(); + jaRaw.put("myCollection", myRawC); + + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONObject jaObj = new JSONObject(); + jaObj.put("myCollection", myCObj); + + Collection myCInt = Collections.singleton(Integer + .valueOf(10)); + JSONObject jaInt = new JSONObject(); + jaInt.put("myCollection", myCInt); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); + } + + + /** + * Verifies that the put Map has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyPutMap() { + + final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(); + jaRaw.put("myMap", myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONObject jaStrObj = new JSONObject(); + jaStrObj.put("myMap", myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONObject jaStrInt = new JSONObject(); + jaStrInt.put("myMap", myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONObject jaObjObj = new JSONObject(); + jaObjObj.put("myMap", myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); + } + /** * JSONObjects can be built from a Map. @@ -1229,10 +1362,9 @@ public void jsonObjectToString() { * Confirm that map and nested JSONObject have the same contents. */ @Test - @SuppressWarnings("unchecked") public void jsonObjectToStringSuppressWarningOnCastToMap() { JSONObject jsonObject = new JSONObject(); - Map map = new HashMap(); + Map map = new HashMap<>(); map.put("abc", "def"); jsonObject.put("key", map); String toStr = jsonObject.toString(); @@ -1245,7 +1377,7 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { * in the debugger, one is a map and the other is a JSONObject. * TODO: write a util method for such comparisons */ - map = (Map)jsonObject.get("key"); + assertTrue("Maps should be entered as JSONObject", jsonObject.get("key") instanceof JSONObject); JSONObject mapJsonObject = expectedJsonObject.getJSONObject("key"); assertTrue("value size should be equal", map.size() == mapJsonObject.length() && map.size() == 1); @@ -1264,32 +1396,31 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { * Confirm that collection and nested JSONArray have the same contents. */ @Test - @SuppressWarnings("unchecked") public void jsonObjectToStringSuppressWarningOnCastToCollection() { - JSONObject jsonObject = new JSONObject(); - Collection collection = new ArrayList(); - collection.add("abc"); - // ArrayList will be added as an object - jsonObject.put("key", collection); - String toStr = jsonObject.toString(); - // [abc] will be added as a JSONArray - JSONObject expectedJsonObject = new JSONObject(toStr); - /** - * Can't do a Util compare because although they look the same - * in the debugger, one is a collection and the other is a JSONArray. - */ - assertTrue("keys should be equal", - jsonObject.keySet().iterator().next().equals( - expectedJsonObject.keySet().iterator().next())); - collection = (Collection)jsonObject.get("key"); - JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); - assertTrue("value size should be equal", - collection.size() == jsonArray.length()); - Iterator it = collection.iterator(); - for (int i = 0; i < collection.size(); ++i) { - assertTrue("items should be equal for index: "+i, - jsonArray.get(i).toString().equals(it.next().toString())); - } + JSONObject jsonObject = new JSONObject(); + Collection collection = new ArrayList(); + collection.add("abc"); + // ArrayList will be added as an object + jsonObject.put("key", collection); + String toStr = jsonObject.toString(); + // [abc] will be added as a JSONArray + JSONObject expectedJsonObject = new JSONObject(toStr); + /** + * Can't do a Util compare because although they look the same in the + * debugger, one is a collection and the other is a JSONArray. + */ + assertTrue("keys should be equal", jsonObject.keySet().iterator() + .next().equals(expectedJsonObject.keySet().iterator().next())); + assertTrue("Collections should be converted to JSONArray", + jsonObject.get("key") instanceof JSONArray); + JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); + assertTrue("value size should be equal", + collection.size() == jsonArray.length()); + Iterator it = collection.iterator(); + for (int i = 0; i < collection.size(); ++i) { + assertTrue("items should be equal for index: " + i, jsonArray + .get(i).toString().equals(it.next().toString())); + } } /** From 3850b5fd25968142e700058b6d11872e148ab63b Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 26 Oct 2015 18:35:53 -0400 Subject: [PATCH 170/315] Add tests to verify arrays are handled consistently. --- XMLTest.java | 103 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 87 insertions(+), 16 deletions(-) diff --git a/XMLTest.java b/XMLTest.java index fb3b42c49..8069f709e 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -385,6 +385,74 @@ public void shouldHandleArraytoString() { assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ finalStr+"]", expectedFinalStr.equals(finalStr)); } + + /** + * Tests that the XML output for empty arrays is consistent. + */ + @Test + public void shouldHandleEmptyArray(){ + final JSONObject jo1 = new JSONObject(); + jo1.put("array",new Object[]{}); + final JSONObject jo2 = new JSONObject(); + jo2.put("array",new JSONArray()); + + final String expected = ""; + String output1 = XML.toString(jo1,"jo"); + assertTrue("Expected an empty root tag", expected.equals(output1)); + String output2 = XML.toString(jo2,"jo"); + assertTrue("Expected an empty root tag", expected.equals(output2)); + } + + /** + * Tests that the XML output for arrays is consistent when an internal array is empty. + */ + @Test + public void shouldHandleEmptyMultiArray(){ + final JSONObject jo1 = new JSONObject(); + jo1.put("arr",new Object[]{"One", new String[]{}, "Four"}); + final JSONObject jo2 = new JSONObject(); + jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{}), "Four"})); + + final String expected = "OneFour"; + String output1 = XML.toString(jo1,"jo"); + assertTrue("Expected a matching array", expected.equals(output1)); + String output2 = XML.toString(jo2,"jo"); + assertTrue("Expected a matching array", expected.equals(output2)); + } + + /** + * Tests that the XML output for arrays is consistent when arrays are not empty. + */ + @Test + public void shouldHandleNonEmptyArray(){ + final JSONObject jo1 = new JSONObject(); + jo1.put("arr",new String[]{"One", "Two", "Three"}); + final JSONObject jo2 = new JSONObject(); + jo2.put("arr",new JSONArray(new String[]{"One", "Two", "Three"})); + + final String expected = "OneTwoThree"; + String output1 = XML.toString(jo1,"jo"); + assertTrue("Expected a non empty root tag", expected.equals(output1)); + String output2 = XML.toString(jo2,"jo"); + assertTrue("Expected a non empty root tag", expected.equals(output2)); + } + + /** + * Tests that the XML output for arrays is consistent when arrays are not empty and contain internal arrays. + */ + @Test + public void shouldHandleMultiArray(){ + final JSONObject jo1 = new JSONObject(); + jo1.put("arr",new Object[]{"One", new String[]{"Two", "Three"}, "Four"}); + final JSONObject jo2 = new JSONObject(); + jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{"Two", "Three"}), "Four"})); + + final String expected = "OneTwoThreeFour"; + String output1 = XML.toString(jo1,"jo"); + assertTrue("Expected a matching array", expected.equals(output1)); + String output2 = XML.toString(jo2,"jo"); + assertTrue("Expected a matching array", expected.equals(output2)); + } /** * Converting a JSON doc containing a named array of nested arrays to @@ -425,7 +493,7 @@ public void shouldHandleIllegalJSONNodeNames() String result = XML.toString(inputJSON); - /** + /* * This is invalid XML. Names should not begin with digits or contain * certain values, including '@'. One possible solution is to replace * illegal chars with '_', in which case the expected output would be: @@ -460,7 +528,7 @@ public void shouldHandleNullNodeValue() */ @Test public void contentOperations() { - /** + /* * When a standalone 0) then return".equals(jsonArray.get(0))); assertTrue("2. content array entry 1", "here is another cdata".equals(jsonArray.get(1))); - /** + /* * text content is accumulated in a "content" inside a local JSONObject. * If there is only one instance, it is saved in the context (a different JSONObject * from the calling code. and the content element is discarded. @@ -493,7 +561,7 @@ public void contentOperations() { assertTrue("3. 2 items", 1 == jsonObject.length()); assertTrue("3. value tag1", "value 1".equals(jsonObject.get("tag1"))); - /** + /* * array-style text content (multiple tags with the same name) is * accumulated in a local JSONObject with key="content" and value=JSONArray, * saved in the context, and then the local JSONObject is discarded. @@ -508,7 +576,7 @@ public void contentOperations() { assertTrue("4. content array entry 1", jsonArray.getInt(1) == 2); assertTrue("4. content array entry 2", jsonArray.getBoolean(2) == true); - /** + /* * Complex content is accumulated in a "content" field. For example, an element * may contain a mix of child elements and text. Each text segment is * accumulated to content. @@ -526,7 +594,7 @@ public void contentOperations() { assertTrue("5. content array entry 0", "val1".equals(jsonArray.get(0))); assertTrue("5. content array entry 1", "val2".equals(jsonArray.get(1))); - /** + /* * If there is only 1 complex text content, then it is accumulated in a * "content" field as a string. */ @@ -538,7 +606,7 @@ public void contentOperations() { assertTrue("6. contained content found", "val1".equals(jsonObject.get("content"))); assertTrue("6. contained tag2", "".equals(jsonObject.get("tag2"))); - /** + /* * In this corner case, the content sibling happens to have key=content * We end up with an array within an array, and no content element. * This is probably a bug. @@ -555,7 +623,7 @@ public void contentOperations() { assertTrue("7. inner array item 0", "val1".equals(jsonArray.get(0))); assertTrue("7. inner array item 1", "".equals(jsonArray.get(1))); - /** + /* * Confirm behavior of original issue */ String jsonStr = @@ -581,7 +649,7 @@ public void contentOperations() { "}"; jsonObject = new JSONObject(jsonStr); xmlStr = XML.toString(jsonObject); - /** + /* * This is the created XML. Looks like content was mistaken for * complex (child node + text) XML. * @@ -600,7 +668,7 @@ public void contentOperations() { /** * Convenience method, given an input string and expected result, - * convert to JSONBObject and compare actual to expected result. + * convert to JSONObject and compare actual to expected result. * @param xmlStr the string to parse * @param expectedStr the expected JSON string */ @@ -612,7 +680,7 @@ private void compareStringToJSONObject(String xmlStr, String expectedStr) { /** * Convenience method, given an input string and expected result, - * convert to JSONBObject via reader and compare actual to expected result. + * convert to JSONObject via reader and compare actual to expected result. * @param xmlStr the string to parse * @param expectedStr the expected JSON string */ @@ -628,11 +696,14 @@ private void compareReaderToJSONObject(String xmlStr, String expectedStr) { } /** - * Convenience method, given an input string and expected result, - * convert to JSONBObject via file and compare actual to expected result. - * @param xmlStr the string to parse - * @param expectedStr the expected JSON string - * @throws IOException + * Convenience method, given an input string and expected result, convert to + * JSONObject via file and compare actual to expected result. + * + * @param xmlStr + * the string to parse + * @param expectedStr + * the expected JSON string + * @throws IOException */ private void compareFileToJSONObject(String xmlStr, String expectedStr) { /* From 38cbc31624ed6dbce05e5c2710203dc906d99a8a Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 31 Oct 2015 03:58:04 -0500 Subject: [PATCH 171/315] Fix tabs, add valueToString() test to JSONObjectTest --- JSONArrayTest.java | 168 ++++++++++++++-------------- JSONObjectTest.java | 267 ++++++++++++++++++++++++-------------------- 2 files changed, 227 insertions(+), 208 deletions(-) diff --git a/JSONArrayTest.java b/JSONArrayTest.java index 1cb5dc301..8327e18cd 100644 --- a/JSONArrayTest.java +++ b/JSONArrayTest.java @@ -93,29 +93,29 @@ public void badObject() { */ @Test public void verifyConstructor() { - - final JSONArray expected = new JSONArray("[10]"); - - @SuppressWarnings("rawtypes") - Collection myRawC = Collections.singleton(Integer.valueOf(10)); - JSONArray jaRaw = new JSONArray(myRawC); - - Collection myCInt = Collections.singleton(Integer.valueOf(10)); - JSONArray jaInt = new JSONArray(myCInt); - - Collection myCObj = Collections.singleton((Object) Integer - .valueOf(10)); - JSONArray jaObj = new JSONArray(myCObj); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaInt)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObj)); + + final JSONArray expected = new JSONArray("[10]"); + + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONArray jaRaw = new JSONArray(myRawC); + + Collection myCInt = Collections.singleton(Integer.valueOf(10)); + JSONArray jaInt = new JSONArray(myCInt); + + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONArray jaObj = new JSONArray(myCObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); } /** @@ -123,32 +123,32 @@ public void verifyConstructor() { */ @Test public void verifyPutCollection() { - - final JSONArray expected = new JSONArray("[[10]]"); - - @SuppressWarnings("rawtypes") - Collection myRawC = Collections.singleton(Integer.valueOf(10)); - JSONArray jaRaw = new JSONArray(); - jaRaw.put(myRawC); - - Collection myCObj = Collections.singleton((Object) Integer - .valueOf(10)); - JSONArray jaObj = new JSONArray(); - jaObj.put(myCObj); - - Collection myCInt = Collections.singleton(Integer.valueOf(10)); - JSONArray jaInt = new JSONArray(); - jaInt.put(myCInt); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaInt)); + + final JSONArray expected = new JSONArray("[[10]]"); + + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONArray jaRaw = new JSONArray(); + jaRaw.put(myRawC); + + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONArray jaObj = new JSONArray(); + jaObj.put(myCObj); + + Collection myCInt = Collections.singleton(Integer.valueOf(10)); + JSONArray jaInt = new JSONArray(); + jaInt.put(myCInt); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); } @@ -157,41 +157,41 @@ public void verifyPutCollection() { */ @Test public void verifyPutMap() { - - final JSONArray expected = new JSONArray("[{\"myKey\":10}]"); - - @SuppressWarnings("rawtypes") - Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); - JSONArray jaRaw = new JSONArray(); - jaRaw.put(myRawC); - - Map myCStrObj = Collections.singletonMap("myKey", - (Object) Integer.valueOf(10)); - JSONArray jaStrObj = new JSONArray(); - jaStrObj.put(myCStrObj); - - Map myCStrInt = Collections.singletonMap("myKey", - Integer.valueOf(10)); - JSONArray jaStrInt = new JSONArray(); - jaStrInt.put(myCStrInt); - - Map myCObjObj = Collections.singletonMap((Object) "myKey", - (Object) Integer.valueOf(10)); - JSONArray jaObjObj = new JSONArray(); - jaObjObj.put(myCObjObj); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrInt)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObjObj)); + + final JSONArray expected = new JSONArray("[{\"myKey\":10}]"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONArray jaRaw = new JSONArray(); + jaRaw.put(myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONArray jaStrObj = new JSONArray(); + jaStrObj.put(myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONArray jaStrInt = new JSONArray(); + jaStrInt.put(myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONArray jaObjObj = new JSONArray(); + jaObjObj.put(myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); } /** diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 1eec1436f..2d0fc4c76 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -172,37 +172,37 @@ public void jsonObjectByMap() { */ @Test public void verifyConstructor() { - - final JSONObject expected = new JSONObject("{\"myKey\":10}"); - - @SuppressWarnings("rawtypes") - Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); - JSONObject jaRaw = new JSONObject(myRawC); - - Map myCStrObj = Collections.singletonMap("myKey", - (Object) Integer.valueOf(10)); - JSONObject jaStrObj = new JSONObject(myCStrObj); - - Map myCStrInt = Collections.singletonMap("myKey", - Integer.valueOf(10)); - JSONObject jaStrInt = new JSONObject(myCStrInt); - - Map myCObjObj = Collections.singletonMap((Object) "myKey", - (Object) Integer.valueOf(10)); - JSONObject jaObjObj = new JSONObject(myCObjObj); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrInt)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObjObj)); + + final JSONObject expected = new JSONObject("{\"myKey\":10}"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONObject jaStrObj = new JSONObject(myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONObject jaStrInt = new JSONObject(myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONObject jaObjObj = new JSONObject(myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); } /** @@ -210,33 +210,33 @@ public void verifyConstructor() { */ @Test public void verifyPutCollection() { - - final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); - - @SuppressWarnings("rawtypes") - Collection myRawC = Collections.singleton(Integer.valueOf(10)); - JSONObject jaRaw = new JSONObject(); - jaRaw.put("myCollection", myRawC); - - Collection myCObj = Collections.singleton((Object) Integer - .valueOf(10)); - JSONObject jaObj = new JSONObject(); - jaObj.put("myCollection", myCObj); - - Collection myCInt = Collections.singleton(Integer - .valueOf(10)); - JSONObject jaInt = new JSONObject(); - jaInt.put("myCollection", myCInt); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaInt)); + + final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); + + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(); + jaRaw.put("myCollection", myRawC); + + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONObject jaObj = new JSONObject(); + jaObj.put("myCollection", myCObj); + + Collection myCInt = Collections.singleton(Integer + .valueOf(10)); + JSONObject jaInt = new JSONObject(); + jaInt.put("myCollection", myCInt); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); } @@ -245,41 +245,41 @@ public void verifyPutCollection() { */ @Test public void verifyPutMap() { - - final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); - - @SuppressWarnings("rawtypes") - Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); - JSONObject jaRaw = new JSONObject(); - jaRaw.put("myMap", myRawC); - - Map myCStrObj = Collections.singletonMap("myKey", - (Object) Integer.valueOf(10)); - JSONObject jaStrObj = new JSONObject(); - jaStrObj.put("myMap", myCStrObj); - - Map myCStrInt = Collections.singletonMap("myKey", - Integer.valueOf(10)); - JSONObject jaStrInt = new JSONObject(); - jaStrInt.put("myMap", myCStrInt); - - Map myCObjObj = Collections.singletonMap((Object) "myKey", - (Object) Integer.valueOf(10)); - JSONObject jaObjObj = new JSONObject(); - jaObjObj.put("myMap", myCObjObj); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrInt)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObjObj)); + + final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(); + jaRaw.put("myMap", myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONObject jaStrObj = new JSONObject(); + jaStrObj.put("myMap", myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONObject jaStrInt = new JSONObject(); + jaStrInt.put("myMap", myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONObject jaObjObj = new JSONObject(); + jaObjObj.put("myMap", myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); } @@ -640,7 +640,7 @@ public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() { */ @Test public void jsonInvalidNumberValues() { - // Number-notations supported by Java and invalid as JSON + // Number-notations supported by Java and invalid as JSON String str = "{"+ "\"hexNumber\":-0x123,"+ @@ -1180,7 +1180,7 @@ public void jsonObjectIncrement() { JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - /** + /** * float f = 3.1f; * double df = (double) f; * double d = 3.1d; @@ -1220,11 +1220,11 @@ public void jsonObjectIncrement() { assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float ); inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double! // this.put(key, (Float) value + 1); - // 1. The (Object)value will be typecasted to (Float)value since it is an instanceof Float actually nothing is done. - // 2. Float instance will be autoboxed into float because the + operator will work on primitives not Objects! - // 3. A float+float operation will be performed and results into a float primitive. - // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method - // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa + // 1. The (Object)value will be typecasted to (Float)value since it is an instanceof Float actually nothing is done. + // 2. Float instance will be autoboxed into float because the + operator will work on primitives not Objects! + // 3. A float+float operation will be performed and results into a float primitive. + // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method + // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Double ); // correct implementation (with change of behavior) would be: // this.put(key, new Float((Float) value + 1)); @@ -1397,30 +1397,30 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { */ @Test public void jsonObjectToStringSuppressWarningOnCastToCollection() { - JSONObject jsonObject = new JSONObject(); - Collection collection = new ArrayList(); - collection.add("abc"); - // ArrayList will be added as an object - jsonObject.put("key", collection); - String toStr = jsonObject.toString(); - // [abc] will be added as a JSONArray - JSONObject expectedJsonObject = new JSONObject(toStr); - /** - * Can't do a Util compare because although they look the same in the - * debugger, one is a collection and the other is a JSONArray. - */ - assertTrue("keys should be equal", jsonObject.keySet().iterator() - .next().equals(expectedJsonObject.keySet().iterator().next())); - assertTrue("Collections should be converted to JSONArray", - jsonObject.get("key") instanceof JSONArray); - JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); - assertTrue("value size should be equal", - collection.size() == jsonArray.length()); - Iterator it = collection.iterator(); - for (int i = 0; i < collection.size(); ++i) { - assertTrue("items should be equal for index: " + i, jsonArray - .get(i).toString().equals(it.next().toString())); - } + JSONObject jsonObject = new JSONObject(); + Collection collection = new ArrayList(); + collection.add("abc"); + // ArrayList will be added as an object + jsonObject.put("key", collection); + String toStr = jsonObject.toString(); + // [abc] will be added as a JSONArray + JSONObject expectedJsonObject = new JSONObject(toStr); + /** + * Can't do a Util compare because although they look the same in the + * debugger, one is a collection and the other is a JSONArray. + */ + assertTrue("keys should be equal", jsonObject.keySet().iterator() + .next().equals(expectedJsonObject.keySet().iterator().next())); + assertTrue("Collections should be converted to JSONArray", + jsonObject.get("key") instanceof JSONArray); + JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); + assertTrue("value size should be equal", + collection.size() == jsonArray.length()); + Iterator it = collection.iterator(); + for (int i = 0; i < collection.size(); ++i) { + assertTrue("items should be equal for index: " + i, jsonArray + .get(i).toString().equals(it.next().toString())); + } } /** @@ -1471,6 +1471,25 @@ public void valueToString() { jsonArray.toString().equals(JSONObject.valueToString(array))); } + /** + * Confirm that https://github.com/douglascrockford/JSON-java/issues/167 is fixed. + * The following code was throwing a ClassCastException in the + * JSONObject(Map) constructor + */ + @Test + public void valueToStringConfirmException() { + String expectedStr = "{\"1\":\"myValue\"}"; + Map myMap = new HashMap(); + myMap.put(1, "myValue"); + // this is the test, it should not throw an exception + String str = JSONObject.valueToString(myMap); + // confirm result, just in case + JSONObject jsonObject = new JSONObject(str); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, + expectedJsonObject); + } + /** * Exercise the JSONObject wrap() method. Sometimes wrap() will change * the object being wrapped, other times not. The purpose of wrap() is From c6204a9f016c85212eb85a4979fc2fa280e90f78 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 26 Dec 2015 13:21:06 -0600 Subject: [PATCH 172/315] Replace util compare method with JsonPath --- JSONObjectTest.java | 720 +++++++++++++++++++++++++++----------------- 1 file changed, 444 insertions(+), 276 deletions(-) diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 2d0fc4c76..d420e2228 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -10,15 +10,7 @@ import java.io.Writer; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.Map; +import java.util.*; import org.json.CDL; import org.json.JSONArray; @@ -28,6 +20,8 @@ import org.json.XML; import org.junit.Test; +import com.jayway.jsonpath.*; + /** * Used in testing when a JSONString is needed */ @@ -112,17 +106,24 @@ public void jsonObjectByNames() { "\"doubleKey\":-23.45e67"+ "}"; String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"}; - String expectedStr = - "{"+ - "\"falseKey\":false,"+ - "\"nullKey\":null,"+ - "\"stringKey\":\"hello world!\","+ - "\"doubleKey\":-23.45e67"+ - "}"; JSONObject jsonObject = new JSONObject(str); - JSONObject copyJsonObject = new JSONObject(jsonObject, keys); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(copyJsonObject, expectedJsonObject); + + // validate JSON + JSONObject jsonObjectByName = new JSONObject(jsonObject, keys); + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObjectByName.toString()); + Map> docMap = JsonPath.read(doc, "$"); + assertTrue("expected 4 items", docMap.size() == 4); + assertTrue("expected \"falseKey\":false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"nullKey\":null", + null == JsonPath.read(doc, "$.nullKey")); + assertTrue("expected \"stringKey\":\"hello world!\"", + "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue( + "expected \"doubleKey\":-23.45e67", + Double.valueOf("-23.45e67").equals( + JsonPath.read(doc, "$.doubleKey"))); } /** @@ -135,8 +136,7 @@ public void jsonObjectByNames() { public void jsonObjectByNullMap() { Map map = null; JSONObject jsonObject = new JSONObject(map); - JSONObject expectedJsonObject = new JSONObject(); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); } /** @@ -145,26 +145,33 @@ public void jsonObjectByNullMap() { */ @Test public void jsonObjectByMap() { - String expectedStr = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ - "\"intKey\":42,"+ - "\"doubleKey\":-23.45e67"+ - "}"; - Map jsonMap = new HashMap(); - jsonMap.put("trueKey", new Boolean(true)); - jsonMap.put("falseKey", new Boolean(false)); - jsonMap.put("stringKey", "hello world!"); - jsonMap.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - jsonMap.put("intKey", new Long(42)); - jsonMap.put("doubleKey", new Double(-23.45e67)); + Map map = new HashMap(); + map.put("trueKey", new Boolean(true)); + map.put("falseKey", new Boolean(false)); + map.put("stringKey", "hello world!"); + map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); + map.put("intKey", new Long(42)); + map.put("doubleKey", new Double(-23.45e67)); + JSONObject jsonObject = new JSONObject(map); - JSONObject jsonObject = new JSONObject(jsonMap); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 6 items", docMap.size() == 6); + assertTrue("expected \"trueKey\":true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected \"falseKey\":false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", + "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", + "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, + "$.escapeStringKey"))); + assertTrue( + "expected \"doubleKey\":-23.45e67", + Double.valueOf("-23.45e67").equals( + JsonPath.read(doc, "$.doubleKey"))); } /** @@ -290,19 +297,22 @@ public void verifyPutMap() { */ @Test public void jsonObjectByMapWithUnsupportedValues() { - String expectedStr = - "{"+ - "\"key1\":{},"+ - "\"key2\":\"java.lang.Exception\""+ - "}"; Map jsonMap = new HashMap(); // Just insert some random objects jsonMap.put("key1", new CDL()); jsonMap.put("key2", new Exception()); JSONObject jsonObject = new JSONObject(jsonMap); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 2 items", docMap.size() == 2); + assertTrue("expected \"key2\":java.lang.Exception", + "java.lang.Exception".equals(JsonPath.read(doc, "$.key2"))); + docMap = JsonPath.read(doc, "$.key1"); + assertTrue("expected 0 items", docMap.size() == 0); } /** @@ -311,27 +321,36 @@ public void jsonObjectByMapWithUnsupportedValues() { */ @Test public void jsonObjectByMapWithNullValue() { - String expectedStr = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ - "\"intKey\":42,"+ - "\"doubleKey\":-23.45e67"+ - "}"; - Map jsonMap = new HashMap(); - jsonMap.put("trueKey", new Boolean(true)); - jsonMap.put("falseKey", new Boolean(false)); - jsonMap.put("stringKey", "hello world!"); - jsonMap.put("nullKey", null); - jsonMap.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - jsonMap.put("intKey", new Long(42)); - jsonMap.put("doubleKey", new Double(-23.45e67)); + Map map = new HashMap(); + map.put("trueKey", new Boolean(true)); + map.put("falseKey", new Boolean(false)); + map.put("stringKey", "hello world!"); + map.put("nullKey", null); + map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); + map.put("intKey", new Long(42)); + map.put("doubleKey", new Double(-23.45e67)); + JSONObject jsonObject = new JSONObject(map); - JSONObject jsonObject = new JSONObject(jsonMap); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 6 items", docMap.size() == 6); + assertTrue("expected \"trueKey\":true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected \"falseKey\":false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", + "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", + "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, + "$.escapeStringKey"))); + assertTrue("expected \"intKey\":42", + Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); + assertTrue( + "expected \"doubleKey\":-23.45e67", + Double.valueOf("-23.45e67").equals( + JsonPath.read(doc, "$.doubleKey"))); } /** @@ -340,18 +359,6 @@ public void jsonObjectByMapWithNullValue() { */ @Test public void jsonObjectByBean() { - String expectedStr = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ - "\"intKey\":42,"+ - "\"doubleKey\":-23.45e7,"+ - "\"stringReaderKey\":{},"+ - "\"callbacks\":[{\"handler\":{}},{}]"+ // sorry, mockito artifact - "}"; - /** * Default access classes have to be mocked since JSONObject, which is * not in the same package, cannot call MyBean methods by reflection. @@ -377,8 +384,37 @@ public String toString(){ }); JSONObject jsonObject = new JSONObject(myBean); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 8 items", docMap.size() == 8); + assertTrue("expected \"trueKey\":true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected \"falseKey\":false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", + "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", + "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, + "$.escapeStringKey"))); + assertTrue("expected \"intKey\":42", + Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); + assertTrue("expected \"doubleKey\":-23.45e7", Double + .valueOf("-23.45e7").equals(JsonPath.read(doc, "$.doubleKey"))); + assertTrue( + "expected \"stringReaderKey\":{}", + ((Map) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0); + // sorry, mockito artifact + List docList = JsonPath.read(doc, "$.callbacks"); + assertTrue("expected 2 items", docList.size() == 2); + assertTrue("expected \"handler\":{}", + ((Map) (JsonPath.read(doc, + "$.callbacks[0].handler"))).size() == 0); + assertTrue("expected empty object", + ((Map) (JsonPath.read(doc, "$.callbacks[1]"))) + .size() == 0); } /** @@ -389,17 +425,20 @@ public String toString(){ */ @Test public void jsonObjectByObjectAndNames() { - String expectedStr = - "{"+ - "\"publicString\":\"abc\","+ - "\"publicInt\":42"+ - "}"; String[] keys = {"publicString", "publicInt"}; // just need a class that has public data members JSONObjectTest jsonObjectTest = new JSONObjectTest(); JSONObject jsonObject = new JSONObject(jsonObjectTest, keys); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 2 items", docMap.size() == 2); + assertTrue("expected \"publicString\":\"abc\"", + "abc".equals(JsonPath.read(doc, "$.publicString"))); + assertTrue("expected \"publicInt\":42", + Integer.valueOf(42).equals(JsonPath.read(doc, "$.publicInt"))); } /** @@ -408,22 +447,27 @@ public void jsonObjectByObjectAndNames() { @Test public void jsonObjectByResourceBundle() { // TODO: how to improve resource bundle testing? - String expectedStr = - "{"+ - "\"greetings\": {"+ - "\"hello\":\"Hello, \","+ - "\"world\":\"World!\""+ - "},"+ - "\"farewells\": {"+ - "\"later\":\"Later, \","+ - "\"gator\":\"Alligator!\""+ - "}"+ - "}"; - JSONObject jsonObject = new + JSONObject jsonObject = new JSONObject("org.json.junit.StringsResourceBundle", Locale.getDefault()); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 2 items in top level map", docMap.size() == 2); + docMap = JsonPath.read(doc, "$.greetings"); + assertTrue("expected 2 items in greetings map", docMap.size() == 2); + assertTrue("expected \"hello\":\"Hello, \"", + "Hello, ".equals(JsonPath.read(doc, "$.greetings.hello"))); + assertTrue("expected \"world\":\"World!\"", + "World!".equals(JsonPath.read(doc, "$.greetings.world"))); + docMap = JsonPath.read(doc, "$.farewells"); + assertTrue("expected 2 items in farewells map", docMap.size() == 2); + assertTrue("expected \"later\":\"Later, \"", + "Later, ".equals(JsonPath.read(doc, "$.farewells.later"))); + assertTrue("expected \"world\":\"World!\"", + "Alligator!".equals(JsonPath.read(doc, "$.farewells.gator"))); } /** @@ -432,17 +476,7 @@ public void jsonObjectByResourceBundle() { @Test public void jsonObjectAccumulate() { // TODO: should include an unsupported object - String expectedStr = - "{"+ - "\"myArray\": ["+ - "true,"+ - "false,"+ - "\"hello world!\","+ - "\"h\be\tllo w\u1234orld!\","+ - "42,"+ - "-23.45e7"+ - "]"+ - "}"; + JSONObject jsonObject = new JSONObject(); jsonObject.accumulate("myArray", true); jsonObject.accumulate("myArray", false); @@ -450,8 +484,29 @@ public void jsonObjectAccumulate() { jsonObject.accumulate("myArray", "h\be\tllo w\u1234orld!"); jsonObject.accumulate("myArray", 42); jsonObject.accumulate("myArray", -23.45e7); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 1 item in top level object", docMap.size() == 1); + List docList = JsonPath.read(doc, "$.myArray"); + assertTrue("expected 6 items in myArray", docList.size() == 6); + assertTrue("expected true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); + assertTrue("expected false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); + assertTrue("expected hello world!", + "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); + assertTrue("expected h\be\tllo w\u1234orld!", + "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, + "$.myArray[3]"))); + assertTrue("expected 42", + Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); + assertTrue( + "expected -23.45e7", + Double.valueOf(-23.45e7).equals( + JsonPath.read(doc, "$.myArray[5]"))); } /** @@ -460,17 +515,6 @@ public void jsonObjectAccumulate() { @Test public void jsonObjectAppend() { // TODO: should include an unsupported object - String expectedStr = - "{"+ - "\"myArray\": ["+ - "true,"+ - "false,"+ - "\"hello world!\","+ - "\"h\be\tllo w\u1234orld!\","+ - "42,"+ - "-23.45e7"+ - "]"+ - "}"; JSONObject jsonObject = new JSONObject(); jsonObject.append("myArray", true); jsonObject.append("myArray", false); @@ -478,8 +522,29 @@ public void jsonObjectAppend() { jsonObject.append("myArray", "h\be\tllo w\u1234orld!"); jsonObject.append("myArray", 42); jsonObject.append("myArray", -23.45e7); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 1 item in top level object", docMap.size() == 1); + List docList = JsonPath.read(doc, "$.myArray"); + assertTrue("expected 6 items in myArray", docList.size() == 6); + assertTrue("expected true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); + assertTrue("expected false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); + assertTrue("expected hello world!", + "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); + assertTrue("expected h\be\tllo w\u1234orld!", + "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, + "$.myArray[3]"))); + assertTrue("expected 42", + Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); + assertTrue( + "expected -23.45e7", + Double.valueOf(-23.45e7).equals( + JsonPath.read(doc, "$.myArray[5]"))); } /** @@ -1058,19 +1123,47 @@ public void jsonObjectNames() { "\"falseKey\":false,"+ "\"stringKey\":\"hello world!\","+ "}"; - String [] expectedNames = {"trueKey", "falseKey", "stringKey"}; jsonObject = new JSONObject(str); names = JSONObject.getNames(jsonObject); - Util.compareActualVsExpectedStringArrays(names, expectedNames); + JSONArray jsonArray = new JSONArray(names); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + List docList = JsonPath.read(doc, "$"); + assertTrue("expected 3 items", docList.size() == 3); + assertTrue( + "expected to find trueKey", + ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); + assertTrue( + "expected to find falseKey", + ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); + assertTrue( + "expected to find stringKey", + ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); /** * getNames() from an enum with properties has an interesting result. * It returns the enum values, not the selected enum properties */ - MyEnumField myEnumField = MyEnumField.VAL1; - String[] enumExpectedNames = {"VAL1", "VAL2", "VAL3"}; + MyEnumField myEnumField = MyEnumField.VAL1; names = JSONObject.getNames(myEnumField); - Util.compareActualVsExpectedStringArrays(names, enumExpectedNames); + + // validate JSON + jsonArray = new JSONArray(names); + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + docList = JsonPath.read(doc, "$"); + assertTrue("expected 3 items", docList.size() == 3); + assertTrue( + "expected to find VAL1", + ((List) JsonPath.read(doc, "$[?(@=='VAL1')]")).size() == 1); + assertTrue( + "expected to find VAL2", + ((List) JsonPath.read(doc, "$[?(@=='VAL2')]")).size() == 1); + assertTrue( + "expected to find VAL3", + ((List) JsonPath.read(doc, "$[?(@=='VAL3')]")).size() == 1); /** * A bean is also an object. But in order to test the static @@ -1078,9 +1171,20 @@ public void jsonObjectNames() { * data members, which have been added to the class. */ JSONObjectTest jsonObjectTest = new JSONObjectTest(); - String [] jsonObjectTestExpectedNames = {"publicString", "publicInt"}; names = JSONObject.getNames(jsonObjectTest); - Util.compareActualVsExpectedStringArrays(names, jsonObjectTestExpectedNames); + + // validate JSON + jsonArray = new JSONArray(names); + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + docList = JsonPath.read(doc, "$"); + assertTrue("expected 2 items", docList.size() == 2); + assertTrue( + "expected to find publicString", + ((List) JsonPath.read(doc, "$[?(@=='publicString')]")).size() == 1); + assertTrue( + "expected to find publicInt", + ((List) JsonPath.read(doc, "$[?(@=='publicInt')]")).size() == 1); } /** @@ -1106,22 +1210,24 @@ public void jsonObjectNamesToJsonAray() { "\"falseKey\":false,"+ "\"stringKey\":\"hello world!\","+ "}"; - String [] expectedNames = {"trueKey", "falseKey", "stringKey" }; JSONObject jsonObject = new JSONObject(str); JSONArray jsonArray = jsonObject.names(); - /** - * Cannot really compare to an expected JSONArray because the ordering - * of the JSONObject keys is not fixed, and JSONArray comparisons - * presume fixed. Since this test is limited to key strings, a - * string comparison will have to suffice. - */ - String namesStr = jsonArray.toString(); - // remove square brackets, commas, and spaces - namesStr = namesStr.replaceAll("[\\]|\\[|\"]", ""); - String [] names = namesStr.split(","); - Util.compareActualVsExpectedStringArrays(names, expectedNames); + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + List docList = JsonPath.read(doc, "$"); + assertTrue("expected 3 items in array", docList.size() == 3); + assertTrue( + "expected to find trueKey", + ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); + assertTrue( + "expected to find falseKey", + ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); + assertTrue( + "expected to find stringKey", + ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); } /** @@ -1134,38 +1240,6 @@ public void jsonObjectIncrement() { "\"keyLong\":9999999991,"+ "\"keyDouble\":1.1,"+ "}"; - String expectedStr = - "{"+ - "\"keyInt\":3,"+ - "\"keyLong\":9999999993,"+ - "\"keyDouble\":3.1,"+ - /** - * Should work the same way on any platform! @see - * https://docs.oracle - * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is - * the effect of a float to double conversion and is inherent to - * the shortcomings of the IEEE 754 format, when converting - * 32-bit into double-precision 64-bit. Java type-casts float to - * double. A 32 bit float is type-casted to 64 bit double by - * simply appending zero-bits to the mantissa (and extended the - * signed exponent by 3 bits.) and there is no way to obtain - * more information than it is stored in the 32-bits float. - * - * Like 1/3 cannot be represented as base10 number because it is - * periodically, 1/5 (for example) cannot be represented as - * base2 number since it is periodically in base2 (take a look - * at http://www.h-schmidt.net/FloatConverter/) The same happens - * to 3.1, that decimal number (base10 representation) is - * periodic in base2 representation, therefore appending - * zero-bits is inaccurate. Only repeating the periodically - * occuring bits (0110) would be a proper conversion. However - * one cannot detect from a 32 bit IEE754 representation which - * bits would "repeat infinitely", since the missing bits would - * not fit into the 32 bit float, i.e. the information needed - * simply is not there! - */ - "\"keyFloat\":3.0999999046325684,"+ - "}"; JSONObject jsonObject = new JSONObject(str); jsonObject.increment("keyInt"); jsonObject.increment("keyInt"); @@ -1177,16 +1251,56 @@ public void jsonObjectIncrement() { jsonObject.put("keyFloat", new Float(1.1)); jsonObject.increment("keyFloat"); jsonObject.increment("keyFloat"); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - /** - * float f = 3.1f; - * double df = (double) f; - * double d = 3.1d; - * System.out.println(Integer.toBinaryString(Float.floatToRawIntBits(f))); - * System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(df))); - * System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(d))); + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 4 items in object", docMap.size() == 4); + assertTrue("expected to find keyInt:3", + Integer.valueOf(3).equals(JsonPath.read(doc, "$.keyInt"))); + assertTrue( + "expected to find keyLong:9999999993", + Long.valueOf(9999999993L).equals( + JsonPath.read(doc, "$.keyLong"))); + assertTrue("expected to find keyDouble:3.1", Double.valueOf(3.1) + .equals(JsonPath.read(doc, "$.keyDouble"))); + /** + * Should work the same way on any platform! @see https://docs.oracle + * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is the + * effect of a float to double conversion and is inherent to the + * shortcomings of the IEEE 754 format, when converting 32-bit into + * double-precision 64-bit. Java type-casts float to double. A 32 bit + * float is type-casted to 64 bit double by simply appending zero-bits + * to the mantissa (and extended the signed exponent by 3 bits.) and + * there is no way to obtain more information than it is stored in the + * 32-bits float. + * + * Like 1/3 cannot be represented as base10 number because it is + * periodically, 1/5 (for example) cannot be represented as base2 number + * since it is periodically in base2 (take a look at + * http://www.h-schmidt.net/FloatConverter/) The same happens to 3.1, + * that decimal number (base10 representation) is periodic in base2 + * representation, therefore appending zero-bits is inaccurate. Only + * repeating the periodically occuring bits (0110) would be a proper + * conversion. However one cannot detect from a 32 bit IEE754 + * representation which bits would "repeat infinitely", since the + * missing bits would not fit into the 32 bit float, i.e. the + * information needed simply is not there! + */ + assertTrue( + "expected to find keyFloat:3.0999999046325684", + Double.valueOf(3.0999999046325684).equals( + JsonPath.read(doc, "$.keyFloat"))); + + /** + * float f = 3.1f; double df = (double) f; double d = 3.1d; + * System.out.println + * (Integer.toBinaryString(Float.floatToRawIntBits(f))); + * System.out.println + * (Long.toBinaryString(Double.doubleToRawLongBits(df))); + * System.out.println + * (Long.toBinaryString(Double.doubleToRawLongBits(d))); * * - Float: * seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm @@ -1216,7 +1330,7 @@ public void jsonObjectIncrement() { assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) ); JSONObject inc = new JSONObject(); - inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) + inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float ); inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double! // this.put(key, (Float) value + 1); @@ -1272,17 +1386,6 @@ public void jsonObjectPut() { "\"myKey4\":\"myVal4\""+ "}"+ "}"; - String expectedStrAfterRemoval = - "{"+ - "\"falseKey\":false,"+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{"+ - "\"myKey1\":\"myVal1\","+ - "\"myKey2\":\"myVal2\","+ - "\"myKey3\":\"myVal3\","+ - "\"myKey4\":\"myVal4\""+ - "}"+ - "}"; JSONObject jsonObject = new JSONObject(); jsonObject.put("trueKey", true); jsonObject.put("falseKey", false); @@ -1294,19 +1397,40 @@ public void jsonObjectPut() { myMap.put("myKey3", "myVal3"); myMap.put("myKey4", "myVal4"); jsonObject.put("objectKey", myMap); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - assertTrue("equal jsonObjects should be similar", - jsonObject.similar(expectedJsonObject)); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 4 items in object", docMap.size() == 4); + assertTrue("expected to find trueKey:true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected to find falseKey:false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + List docList = JsonPath.read(doc, "$.arrayKey"); + assertTrue("expected 3 items in array", docList.size() == 3); + assertTrue("expected to find 0", + Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); + assertTrue("expected to find 1", + Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); + assertTrue("expected to find 2", + Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); + docMap = JsonPath.read(doc, "$.objectKey"); + assertTrue("expected 4 items in object", docMap.size() == 4); + assertTrue("expected to find myKey1:myVal1", + "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); + assertTrue("expected to find myKey2:myVal2", + "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); + assertTrue("expected to find myKey3:myVal3", + "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); + assertTrue("expected to find myKey4:myVal4", + "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); jsonObject.remove("trueKey"); - JSONObject expectedJsonObjectAfterRemoval = - new JSONObject(expectedStrAfterRemoval); - Util.compareActualVsExpectedJsonObjects(jsonObject, - expectedJsonObjectAfterRemoval); + JSONObject expectedJsonObject = new JSONObject(expectedStr); assertTrue("unequal jsonObjects should not be similar", !jsonObject.similar(expectedJsonObject)); - assertTrue("unequal Objects should not be similar", + assertTrue("jsonObject should not be similar to jsonArray", !jsonObject.similar(new JSONArray())); String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}"; @@ -1349,9 +1473,34 @@ public void jsonObjectToString() { "}"+ "}"; JSONObject jsonObject = new JSONObject(str); - String toStr = jsonObject.toString(); - JSONObject expectedJsonObject = new JSONObject(toStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 4 items in object", docMap.size() == 4); + assertTrue("expected to find \"trueKey\":true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected to find \"falseKey\":false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + List docList = JsonPath.read(doc, "$.arrayKey"); + assertTrue("expected 3 array items", docList.size() == 3); + assertTrue("expected array value 0", + Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); + assertTrue("expected array value 1", + Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); + assertTrue("expected array value 2", + Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); + docMap = JsonPath.read(doc, "$.objectKey"); + assertTrue("expected 4 items in objectKey object", docMap.size() == 4); + assertTrue("expected objectKey myKey1:myVal1", + "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); + assertTrue("expected objectKey myKey2:myVal2", + "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); + assertTrue("expected objectKey myKey3:myVal3", + "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); + assertTrue("expected objectKey myKey4:myVal4", + "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); } /** @@ -1367,25 +1516,16 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { Map map = new HashMap<>(); map.put("abc", "def"); jsonObject.put("key", map); - String toStr = jsonObject.toString(); - JSONObject expectedJsonObject = new JSONObject(toStr); - assertTrue("keys should be equal", - jsonObject.keySet().iterator().next().equals( - expectedJsonObject.keySet().iterator().next())); - /** - * Can't do a Util compare because although they look the same - * in the debugger, one is a map and the other is a JSONObject. - * TODO: write a util method for such comparisons - */ - assertTrue("Maps should be entered as JSONObject", jsonObject.get("key") instanceof JSONObject); - JSONObject mapJsonObject = expectedJsonObject.getJSONObject("key"); - assertTrue("value size should be equal", - map.size() == mapJsonObject.length() && map.size() == 1); - assertTrue("keys should be equal for key: "+map.keySet().iterator().next(), - mapJsonObject.keys().next().equals(map.keySet().iterator().next())); - assertTrue("values should be equal for key: "+map.keySet().iterator().next(), - mapJsonObject.get(mapJsonObject.keys().next()).toString().equals( - map.get(map.keySet().iterator().next()))); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 1 item in object", docMap.size() == 1); + docMap = JsonPath.read(doc, "$.key"); + assertTrue("expected 1 item in key object", docMap.size() == 1); + assertTrue("expected abc:def", + "def".equals(JsonPath.read(doc, "$.key.abc"))); } /** @@ -1402,25 +1542,15 @@ public void jsonObjectToStringSuppressWarningOnCastToCollection() { collection.add("abc"); // ArrayList will be added as an object jsonObject.put("key", collection); - String toStr = jsonObject.toString(); - // [abc] will be added as a JSONArray - JSONObject expectedJsonObject = new JSONObject(toStr); - /** - * Can't do a Util compare because although they look the same in the - * debugger, one is a collection and the other is a JSONArray. - */ - assertTrue("keys should be equal", jsonObject.keySet().iterator() - .next().equals(expectedJsonObject.keySet().iterator().next())); - assertTrue("Collections should be converted to JSONArray", - jsonObject.get("key") instanceof JSONArray); - JSONArray jsonArray = expectedJsonObject.getJSONArray("key"); - assertTrue("value size should be equal", - collection.size() == jsonArray.length()); - Iterator it = collection.iterator(); - for (int i = 0; i < collection.size(); ++i) { - assertTrue("items should be equal for index: " + i, jsonArray - .get(i).toString().equals(it.next().toString())); - } + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 1 item in object", docMap.size() == 1); + List docList = JsonPath.read(doc, "$.key"); + assertTrue("expected 1 item in key object", docList.size() == 1); + assertTrue("expected abc", "abc".equals(JsonPath.read(doc, "$.key[0]"))); } /** @@ -1478,16 +1608,17 @@ public void valueToString() { */ @Test public void valueToStringConfirmException() { - String expectedStr = "{\"1\":\"myValue\"}"; Map myMap = new HashMap(); myMap.put(1, "myValue"); // this is the test, it should not throw an exception String str = JSONObject.valueToString(myMap); // confirm result, just in case - JSONObject jsonObject = new JSONObject(str); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, - expectedJsonObject); + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(str); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 1 item in object", docMap.size() == 1); + assertTrue("expected myValue", + "myValue".equals(JsonPath.read(doc, "$.1"))); } /** @@ -1533,28 +1664,66 @@ public void wrapObject() { collection.add(new Integer(1)); collection.add(new Integer(2)); collection.add(new Integer(3)); - JSONArray jsonArray = (JSONArray)(JSONObject.wrap(collection)); - String expectedCollectionJsonArrayStr = - "[1,2,3]"; - JSONArray expectedCollectionJsonArray = - new JSONArray(expectedCollectionJsonArrayStr); - Util.compareActualVsExpectedJsonArrays(jsonArray, - expectedCollectionJsonArray); + JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection)); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + List docList = JsonPath.read(doc, "$"); + assertTrue("expected 3 items in array", docList.size() == 3); + assertTrue("expected 1", + Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", + Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", + Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // wrap Array returns JSONArray Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); - JSONArray expectedIntegerArrayJsonArray = new JSONArray("[1,2,3]"); - Util.compareActualVsExpectedJsonArrays(integerArrayJsonArray, - expectedIntegerArrayJsonArray); + + // validate JSON + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + docList = JsonPath.read(doc, "$"); + assertTrue("expected 3 items in array", docList.size() == 3); + assertTrue("expected 1", + Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", + Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", + Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + + // validate JSON + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(integerArrayJsonArray.toString()); + docList = JsonPath.read(doc, "$"); + assertTrue("expected 3 items in array", docList.size() == 3); + assertTrue("expected 1", + Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", + Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", + Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // wrap map returns JSONObject Map map = new HashMap(); map.put("key1", "val1"); map.put("key2", "val2"); map.put("key3", "val3"); - JSONObject mapJsonObject = (JSONObject)(JSONObject.wrap(map)); - Util.compareActualVsExpectedJsonObjects(jsonObject, mapJsonObject); + JSONObject mapJsonObject = (JSONObject) (JSONObject.wrap(map)); + + // validate JSON + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(mapJsonObject.toString()); + Map docMap = JsonPath.read(doc, "$"); + assertTrue("expected 3 items in object", docMap.size() == 3); + assertTrue("expected key1:val1", + "val1".equals(JsonPath.read(doc, "$.key1"))); + assertTrue("expected key2:val2", + "val2".equals(JsonPath.read(doc, "$.key2"))); + assertTrue("expected key3:val3", + "val3".equals(JsonPath.read(doc, "$.key3"))); // TODO test wrap(package) } @@ -1720,13 +1889,14 @@ public void jsonObjectputNull() { // put null should remove the item. String str = "{\"myKey\": \"myval\"}"; JSONObject jsonObjectRemove = new JSONObject(str); - JSONObject jsonObjectPutNull = new JSONObject(str); jsonObjectRemove.remove("myKey"); - jsonObjectPutNull.put("myKey", (Object)null); - Util.compareActualVsExpectedJsonObjects(jsonObjectRemove, jsonObjectPutNull); - assertTrue("jsonObject should be empty", - jsonObjectRemove.length() == 0 && - jsonObjectPutNull.length() == 0); + + JSONObject jsonObjectPutNull = new JSONObject(str); + jsonObjectPutNull.put("myKey", (Object) null); + + // validate JSON + assertTrue("jsonObject should be empty", jsonObjectRemove.length() == 0 + && jsonObjectPutNull.length() == 0); } /** @@ -1878,7 +2048,7 @@ public void jsonObjectNullOperations() { obj = null; jsonObjectNull.put("key", obj); value = jsonObjectNull.opt("key"); - assertTrue("opt() null should find null", value == null);; + assertTrue("opt() null should find null", value == null); if (value == null) { value = ""; } @@ -1904,5 +2074,3 @@ public void jsonObjectNullOperations() { assertTrue("null should emit an empty string", "".equals(sNull)); } } - - From a5390a068563e095f6a71452407f4ffd8cde5cd6 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 26 Dec 2015 19:02:02 -0600 Subject: [PATCH 173/315] Replace util compare method with JsonPath --- JSONArrayTest.java | 205 +++++++++++++++++++++++---------------------- 1 file changed, 105 insertions(+), 100 deletions(-) diff --git a/JSONArrayTest.java b/JSONArrayTest.java index 8327e18cd..c042acdca 100644 --- a/JSONArrayTest.java +++ b/JSONArrayTest.java @@ -2,18 +2,15 @@ import static org.junit.Assert.assertTrue; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; +import java.util.*; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.junit.Test; +import com.jayway.jsonpath.*; + /** * Tests for JSON-Java JSONArray.java @@ -306,41 +303,41 @@ public void failedGetArrayValues() { /** * Exercise JSONArray.join() by converting a JSONArray into a * comma-separated string. Since this is very nearly a JSON document, - * array braces are added to the beginning and end, and it is reconverted - * back to a JSONArray for comparison. + * array braces are added to the beginning and end prior to validation. */ @Test public void join() { - String expectedStr = - "["+ - "true,"+ - "false,"+ - "\"true\","+ - "\"false\","+ - "\"hello\","+ - "0.002345,"+ - "\"23.45\","+ - "42,"+ - "\"43\","+ - "["+ - "\"world\""+ - "],"+ - "{"+ - "\"key1\":\"value1\","+ - "\"key2\":\"value2\","+ - "\"key3\":\"value3\","+ - "\"key4\":\"value4\""+ - "},"+ - "0,"+ - "\"-1\""+ - "]"; - JSONArray jsonArray = new JSONArray(arrayStr); String joinStr = jsonArray.join(","); - JSONArray finalJsonArray = new JSONArray("["+joinStr+"]"); - JSONArray expectedJsonArray = new JSONArray(expectedStr); - Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); - Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); + + // validate JSON + /** + * Don't need to remake the JSONArray to perform the parsing + */ + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse("["+joinStr+"]"); + List docList = JsonPath.read(doc, "$"); + assertTrue("expected 13 items in top level object", docList.size() == 13); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected \"true\"", "true".equals(JsonPath.read(doc, "$[2]"))); + assertTrue("expected \"false\"", "false".equals(JsonPath.read(doc, "$[3]"))); + assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[4]"))); + assertTrue("expected 0.002345", Double.valueOf(0.002345).equals(JsonPath.read(doc, "$[5]"))); + assertTrue("expected \"23.45\"", "23.45".equals(JsonPath.read(doc, "$[6]"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[7]"))); + assertTrue("expected \"43\"", "43".equals(JsonPath.read(doc, "$[8]"))); + docList = JsonPath.read(doc, "$[9]"); + assertTrue("expected 1 array item", docList.size() == 1); + assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[9][0]"))); + Map docMap = JsonPath.read(doc, "$[10]"); + assertTrue("expected 4 object items", docMap.size() == 4); + assertTrue("expected value1", "value1".equals(JsonPath.read(doc, "$[10].key1"))); + assertTrue("expected value2", "value2".equals(JsonPath.read(doc, "$[10].key2"))); + assertTrue("expected value3", "value3".equals(JsonPath.read(doc, "$[10].key3"))); + assertTrue("expected value4", "value4".equals(JsonPath.read(doc, "$[10].key4"))); + assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$[11]"))); + assertTrue("expected \"-1\"", "-1".equals(JsonPath.read(doc, "$[12]"))); } /** @@ -419,33 +416,7 @@ public void opt() { */ @Test public void put() { - String expectedStr = - "["+ - "true,"+ - "false,"+ - "["+ - "hello,"+ - "world"+ - "],"+ - "2.5,"+ - "1,"+ - "45,"+ - "\"objectPut\","+ - "{"+ - "\"key10\":\"val10\","+ - "\"key20\":\"val20\","+ - "\"key30\":\"val30\""+ - "},"+ - "{"+ - "\"k1\":\"v1\""+ - "},"+ - "["+ - "1,"+ - "2"+ - "]"+ - "]"; JSONArray jsonArray = new JSONArray(); - JSONArray expectedJsonArray = new JSONArray(expectedStr); // index 0 jsonArray.put(true); @@ -488,9 +459,36 @@ public void put() { Collection collection = new ArrayList(); collection.add(1); collection.add(2); - // 9 + // 9 jsonArray.put(collection); - Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + List docList = JsonPath.read(doc, "$"); + assertTrue("expected 10 items in top level object", docList.size() == 10); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); + docList = JsonPath.read(doc, "$[2]"); + assertTrue("expected 2 items in array", docList.size() == 2); + assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]"))); + assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]"))); + assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]"))); + assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]"))); + assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]"))); + Map docMap = JsonPath.read(doc, "$[7]"); + assertTrue("expected 3 items in object", docMap.size() == 3); + assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[7].key10"))); + assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[7].key20"))); + assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[7].key30"))); + docMap = JsonPath.read(doc, "$[8]"); + assertTrue("expected 1 item in object", docMap.size() == 1); + assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[8].k1"))); + docList = JsonPath.read(doc, "$[9]"); + assertTrue("expected 2 items in array", docList.size() == 2); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]"))); } /** @@ -499,34 +497,7 @@ public void put() { */ @Test public void putIndex() { - String expectedStr = - "["+ - "true,"+ - "false,"+ - "["+ - "hello,"+ - "world"+ - "],"+ - "2.5,"+ - "1,"+ - "45,"+ - "\"objectPut\","+ - "null,"+ - "{"+ - "\"key10\":\"val10\","+ - "\"key20\":\"val20\","+ - "\"key30\":\"val30\""+ - "},"+ - "["+ - "1,"+ - "2"+ - "],"+ - "{"+ - "\"k1\":\"v1\""+ - "},"+ - "]"; JSONArray jsonArray = new JSONArray(); - JSONArray expectedJsonArray = new JSONArray(expectedStr); // 1 jsonArray.put(1, false); @@ -573,7 +544,35 @@ public void putIndex() { jsonArray.put(-1, "abc"); assertTrue("put index < 0 should have thrown exception", false); } catch(Exception ignored) {} - Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + List docList = JsonPath.read(doc, "$"); + assertTrue("expected 11 items in top level object", docList.size() == 11); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); + docList = JsonPath.read(doc, "$[2]"); + assertTrue("expected 2 items in array", docList.size() == 2); + assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]"))); + assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]"))); + assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]"))); + assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]"))); + assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]"))); + assertTrue("expected null", null == JsonPath.read(doc, "$[7]")); + Map docMap = JsonPath.read(doc, "$[8]"); + assertTrue("expected 3 items in object", docMap.size() == 3); + assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[8].key10"))); + assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[8].key20"))); + assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[8].key30"))); + docList = JsonPath.read(doc, "$[9]"); + assertTrue("expected 2 items in array", docList.size() == 2); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]"))); + docMap = JsonPath.read(doc, "$[10]"); + assertTrue("expected 1 item in object", docMap.size() == 1); + assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[10].k1"))); } /** @@ -587,10 +586,9 @@ public void remove() { "1"+ "]"; JSONArray jsonArray = new JSONArray(arrayStr); - JSONArray expectedJsonArray = new JSONArray(); jsonArray.remove(0); assertTrue("array should be empty", null == jsonArray.remove(5)); - Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + assertTrue("jsonArray should be empty", jsonArray.length() == 0); } /** @@ -648,15 +646,22 @@ public void toJSONObject() { */ @Test public void objectArrayVsIsArray() { - String expectedStr = - "["+ - "1,2,3,4,5,6,7"+ - "]"; int[] myInts = { 1, 2, 3, 4, 5, 6, 7 }; Object myObject = myInts; JSONArray jsonArray = new JSONArray(myObject); - JSONArray expectedJsonArray = new JSONArray(expectedStr); - Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + List docList = JsonPath.read(doc, "$"); + assertTrue("expected 7 items in top level object", docList.size() == 7); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$[3]"))); + assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$[4]"))); + assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$[5]"))); + assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$[6]"))); } /** From d329b6514c706cdb4502df609128922b9962c7b0 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 27 Dec 2015 15:17:01 -0600 Subject: [PATCH 174/315] Replace util compare method with JsonPath --- EnumTest.java | 170 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 110 insertions(+), 60 deletions(-) diff --git a/EnumTest.java b/EnumTest.java index d24824bce..2d2ebb977 100644 --- a/EnumTest.java +++ b/EnumTest.java @@ -2,9 +2,13 @@ import static org.junit.Assert.*; +import java.util.*; + import org.json.*; import org.junit.*; +import com.jayway.jsonpath.*; + /** * Enums are not explicitly supported in JSON-Java. But because enums act like * classes, all required behavior is already be present in some form. @@ -25,23 +29,37 @@ public void jsonObjectFromEnum() { assertTrue("simple enum has no getters", jsonObject.length() == 0); // enum with a getters should create a non-empty object - String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}"; MyEnumField myEnumField = MyEnumField.VAL2; jsonObject = new JSONObject(myEnumField); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + // validate JSON content + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expecting 2 items in top level object", + ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expecting val 2", "val 2".equals(JsonPath.read(doc, "$.value"))); + assertTrue("expecting 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal"))); /** * class which contains enum instances. Each enum should be stored * in its own JSONObject */ - expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; MyEnumClass myEnumClass = new MyEnumClass(); myEnumClass.setMyEnum(MyEnum.VAL1); myEnumClass.setMyEnumField(MyEnumField.VAL3); jsonObject = new JSONObject(myEnumClass); - expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + // validate JSON content + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expecting 2 items in top level object", + ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expecting 2 items in myEnumField object", + ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); + assertTrue("expecting 0 items in myEnum object", + ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); + assertTrue("expecting 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal"))); + assertTrue("expecting val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value"))); } /** @@ -51,28 +69,30 @@ public void jsonObjectFromEnum() { @Test public void jsonObjectFromEnumWithNames() { String [] names; - String expectedStr; JSONObject jsonObject; - JSONObject finalJsonObject; - JSONObject expectedJsonObject; - expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; MyEnum myEnum = MyEnum.VAL1; names = JSONObject.getNames(myEnum); - // The values will be MyEnmField fields, so need to convert back to string for comparison + // The values will be MyEnum fields jsonObject = new JSONObject(myEnum, names); - finalJsonObject = new JSONObject(jsonObject.toString()); - expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); - expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; + // validate JSON object + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1"))); + assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2"))); + assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3"))); + MyEnumField myEnumField = MyEnumField.VAL3; names = JSONObject.getNames(myEnumField); - // The values will be MyEnmField fields, so need to convert back to string for comparison + // The values will be MyEnmField fields jsonObject = new JSONObject(myEnumField, names); - finalJsonObject = new JSONObject(jsonObject.toString()); - expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1"))); + assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2"))); + assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3"))); + } /** @@ -81,29 +101,33 @@ public void jsonObjectFromEnumWithNames() { */ @Test public void enumPut() { - String expectedFinalStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL1\"}"; JSONObject jsonObject = new JSONObject(); MyEnum myEnum = MyEnum.VAL2; jsonObject.put("myEnum", myEnum); - assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonObject.get("myEnum"))); - assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonObject.opt("myEnum"))); MyEnumField myEnumField = MyEnumField.VAL1; jsonObject.putOnce("myEnumField", myEnumField); - assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonObject.get("myEnumField"))); - assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonObject.opt("myEnumField"))); - JSONObject finalJsonObject = new JSONObject(jsonObject.toString()); - JSONObject expectedFinalJsonObject = new JSONObject(expectedFinalStr); - Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedFinalJsonObject); + // validate JSON content + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level objects", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnum"))); + assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.myEnumField"))); + JSONArray jsonArray = new JSONArray(); jsonArray.put(myEnum); jsonArray.put(1, myEnumField); + + // validate JSON content + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 2 top level objects", ((List)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$[1]"))); + + /** + * Leaving these tests because they exercise get, opt, and remove + */ assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonArray.get(0))); assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.opt(1))); - JSONArray expectedJsonArray = new JSONArray(); - expectedJsonArray.put(MyEnum.VAL2); - expectedJsonArray.put(MyEnumField.VAL1); - Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1))); } @@ -151,49 +175,66 @@ public void enumToString() { MyEnumField myEnumField = MyEnumField.VAL2; jsonObject = new JSONObject(myEnumField); - expectedStr = "{\"value\":\"val 2\", \"intVal\":2}"; - JSONObject actualJsonObject = new JSONObject(jsonObject.toString()); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); - expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; + // validate JSON content + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected val 2", "val 2".equals(JsonPath.read(doc, "$.value"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal"))); + MyEnumClass myEnumClass = new MyEnumClass(); myEnumClass.setMyEnum(MyEnum.VAL1); myEnumClass.setMyEnumField(MyEnumField.VAL3); jsonObject = new JSONObject(myEnumClass); - actualJsonObject = new JSONObject(jsonObject.toString()); - expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); - expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; + // validate JSON content + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected 2 myEnumField items", ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); + assertTrue("expected 0 myEnum items", ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal"))); + assertTrue("expected val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value"))); + String [] names = JSONObject.getNames(myEnum); jsonObject = new JSONObject(myEnum, names); - actualJsonObject = new JSONObject(jsonObject.toString()); - expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); - expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; + // validate JSON content + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1"))); + assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2"))); + assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3"))); + names = JSONObject.getNames(myEnumField); jsonObject = new JSONObject(myEnumField, names); - actualJsonObject = new JSONObject(jsonObject.toString()); - expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); + + // validate JSON content + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1"))); + assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2"))); + assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3"))); expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}"; jsonObject = new JSONObject(); jsonObject.putOpt("myEnum", myEnum); jsonObject.putOnce("myEnumField", myEnumField); - actualJsonObject = new JSONObject(jsonObject.toString()); - expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); - expectedStr = "[\"VAL2\", \"VAL2\"]"; + // validate JSON content + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnum"))); + assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnumField"))); + JSONArray jsonArray = new JSONArray(); jsonArray.put(myEnum); jsonArray.put(1, myEnumField); - JSONArray actualJsonArray = new JSONArray(jsonArray.toString()); - JSONArray expectedJsonArray = new JSONArray(expectedStr); - Util.compareActualVsExpectedJsonArrays(actualJsonArray, expectedJsonArray); + + // validate JSON content + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 2 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[1]"))); } /** @@ -206,19 +247,28 @@ public void wrap() { JSONObject jsonObject = (JSONObject)JSONObject.wrap(myEnum); assertTrue("simple enum has no getters", jsonObject.length() == 0); - String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}"; MyEnumField myEnumField = MyEnumField.VAL2; jsonObject = (JSONObject)JSONObject.wrap(myEnumField); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); - expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; + // validate JSON content + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected val 2", "val 2".equals(JsonPath.read(doc, "$.value"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal"))); + MyEnumClass myEnumClass = new MyEnumClass(); myEnumClass.setMyEnum(MyEnum.VAL1); myEnumClass.setMyEnumField(MyEnumField.VAL3); jsonObject = (JSONObject)JSONObject.wrap(myEnumClass); - expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + // validate JSON content + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected 2 myEnumField items", ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); + assertTrue("expected 0 myEnum items", ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal"))); + assertTrue("expected val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value"))); + } /** From 7187006bae9135275b2cef6b3dd1213d0c4df395 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 27 Dec 2015 17:01:42 -0600 Subject: [PATCH 175/315] Replace util compare method with JsonPath --- CookieListTest.java | 112 +++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 48 deletions(-) diff --git a/CookieListTest.java b/CookieListTest.java index 1d7ad8a5b..98ede86ce 100644 --- a/CookieListTest.java +++ b/CookieListTest.java @@ -2,9 +2,13 @@ import static org.junit.Assert.*; +import java.util.*; + import org.json.*; import org.junit.Test; +import com.jayway.jsonpath.*; + /** * HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265 *

@@ -60,7 +64,6 @@ public void malFormedCookieListException() { @Test public void emptyStringCookieList() { String cookieStr = ""; - String expectedCookieStr = ""; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); assertTrue(jsonObject.length() == 0); } @@ -71,10 +74,13 @@ public void emptyStringCookieList() { @Test public void simpleCookieList() { String cookieStr = "SID=31d4d96e407aad42"; - String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Object doc = Configuration.defaultConfiguration().jsonProvider(). + parse(jsonObject.toString()); + assertTrue("Expected 1 top level item", + ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 31d4d96e407aad42", + "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID"))); } /** @@ -83,10 +89,13 @@ public void simpleCookieList() { @Test public void simpleCookieListWithDelimiter() { String cookieStr = "SID=31d4d96e407aad42;"; - String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Object doc = Configuration.defaultConfiguration().jsonProvider(). + parse(jsonObject.toString()); + assertTrue("Expected 1 top level item", + ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 31d4d96e407aad42", + "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID"))); } /** @@ -102,18 +111,23 @@ public void multiPartCookieList() { " name4=myCookieValue4; "+ "name5=myCookieValue5;"+ " name6=myCookieValue6;"; - String expectedCookieStr = - "{"+ - "\"name1\":\"myCookieValue1\","+ - "\"name2\":\"myCookieValue2\","+ - "\"name3\":\"myCookieValue3\","+ - "\"name4\":\"myCookieValue4\","+ - "\"name5\":\"myCookieValue5\","+ - "\"name6\":\"myCookieValue6\""+ - "}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Object doc = Configuration.defaultConfiguration().jsonProvider(). + parse(jsonObject.toString()); + assertTrue("Expected 6 top level items", + ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected myCookieValue1", + "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); + assertTrue("expected myCookieValue2", + "myCookieValue2".equals(JsonPath.read(doc, "$.name2"))); + assertTrue("expected myCookieValue3", + "myCookieValue3".equals(JsonPath.read(doc, "$.name3"))); + assertTrue("expected myCookieValue4", + "myCookieValue4".equals(JsonPath.read(doc, "$.name4"))); + assertTrue("expected myCookieValue5", + "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); + assertTrue("expected myCookieValue6", + "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); } /** @@ -139,21 +153,23 @@ public void convertCookieListToString() { " name4=myCookieValue4; "+ "name5=myCookieValue5;"+ " name6=myCookieValue6;"; - String expectedCookieStr = - "{"+ - "\"name1\":\"myCookieValue1\","+ - "\"name2\":\"myCookieValue2\","+ - "\"name3\":\"myCookieValue3\","+ - "\"name4\":\"myCookieValue4\","+ - "\"name5\":\"myCookieValue5\","+ - "\"name6\":\"myCookieValue6\""+ - "}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); - String cookieToStr = CookieList.toString(jsonObject); - JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); - Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + Object doc = Configuration.defaultConfiguration().jsonProvider(). + parse(jsonObject.toString()); + assertTrue("Expected 6 top level items", + ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected myCookieValue1", + "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); + assertTrue("expected myCookieValue2", + "myCookieValue2".equals(JsonPath.read(doc, "$.name2"))); + assertTrue("expected myCookieValue3", + "myCookieValue3".equals(JsonPath.read(doc, "$.name3"))); + assertTrue("expected myCookieValue4", + "myCookieValue4".equals(JsonPath.read(doc, "$.name4"))); + assertTrue("expected myCookieValue5", + "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); + assertTrue("expected myCookieValue6", + "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); } /** @@ -169,22 +185,22 @@ public void convertEncodedCookieListToString() { " name4=my%25CookieValue4; "+ "name5=myCookieValue5;"+ " name6=myCookieValue6;"; - String expectedCookieStr = - "{"+ - "\"name1\":\"myCookieValue1\","+ - "\"name2\":\"my Cookie Value 2\","+ - "\"name3\":\"my+Cookie&Value;3=\","+ - "\"name4\":\"my%CookieValue4\","+ - "\"name5\":\"myCookieValue5\","+ - "\"name6\":\"myCookieValue6\""+ - "}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); - String cookieToStr = CookieList.toString(jsonObject); - JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); - Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + Object doc = Configuration.defaultConfiguration().jsonProvider(). + parse(jsonObject.toString()); + assertTrue("Expected 6 top level items", + ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected myCookieValue1", + "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); + assertTrue("expected my Cookie Value 2", + "my Cookie Value 2".equals(JsonPath.read(doc, "$.name2"))); + assertTrue("expected my+Cookie&Value;3=", + "my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3"))); + assertTrue("expected my%CookieValue4", + "my%CookieValue4".equals(JsonPath.read(doc, "$.name4"))); + assertTrue("expected my%CookieValue5", + "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); + assertTrue("expected myCookieValue6", + "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); } - - } From 48c872f66f4889754da36b209f1e8ade894744ab Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 28 Dec 2015 11:12:41 -0600 Subject: [PATCH 176/315] Replace util compare method with JsonPath --- JSONStringerTest.java | 108 ++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 57 deletions(-) diff --git a/JSONStringerTest.java b/JSONStringerTest.java index 7a3506443..4e9a9dbab 100644 --- a/JSONStringerTest.java +++ b/JSONStringerTest.java @@ -2,9 +2,13 @@ import static org.junit.Assert.*; +import java.util.*; + import org.json.*; import org.junit.Test; +import com.jayway.jsonpath.*; + /** * Tests for JSON-Java JSONStringer. @@ -175,16 +179,6 @@ public void exceedNestDepthException() { */ @Test public void simpleObjectString() { - String expectedStr = - "{"+ - "\"trueValue\":true,"+ - "\"falseValue\":false,"+ - "\"nullValue\":null,"+ - "\"stringValue\":\"hello world!\","+ - "\"complexStringValue\":\"h\be\tllo w\u1234orld!\","+ - "\"intValue\":42,"+ - "\"doubleValue\":-23.45e67"+ - "}"; JSONStringer jsonStringer = new JSONStringer(); jsonStringer.object(); jsonStringer.key("trueValue").value(true); @@ -197,8 +191,16 @@ public void simpleObjectString() { jsonStringer.endObject(); String str = jsonStringer.toString(); JSONObject jsonObject = new JSONObject(str); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 7 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 7); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseValue"))); + assertTrue("expected null", null == JsonPath.read(doc, "$.nullValue")); + assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.stringValue"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, "$.complexStringValue"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.intValue"))); + assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$.doubleValue"))); } /** @@ -207,15 +209,6 @@ public void simpleObjectString() { */ @Test public void simpleArrayString() { - String expectedStr = - "["+ - "true,"+ - "false,"+ - "null,"+ - "\"hello world!\","+ - "42,"+ - "-23.45e67"+ - "]"; JSONStringer jsonStringer = new JSONStringer(); jsonStringer.array(); jsonStringer.value(true); @@ -227,8 +220,15 @@ public void simpleArrayString() { jsonStringer.endArray(); String str = jsonStringer.toString(); JSONArray jsonArray = new JSONArray(str); - JSONArray expectedJsonArray = new JSONArray(expectedStr); - Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 6 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected null", null == JsonPath.read(doc, "$[2]")); + assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$[3]"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[4]"))); + assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$[5]"))); } /** @@ -237,38 +237,6 @@ public void simpleArrayString() { */ @Test public void complexObjectString() { - String expectedStr = - "{"+ - "\"trueValue\":true,"+ - "\"falseValue\":false,"+ - "\"nullValue\":null,"+ - "\"stringValue\":\"hello world!\","+ - "\"object2\":{"+ - "\"k1\":\"v1\","+ - "\"k2\":\"v2\","+ - "\"k3\":\"v3\","+ - "\"array1\":["+ - "1,"+ - "2,"+ - "{"+ - "\"k4\":\"v4\","+ - "\"k5\":\"v5\","+ - "\"k6\":\"v6\","+ - "\"array2\":["+ - "5,"+ - "6,"+ - "7,"+ - "8"+ - "]"+ - "},"+ - "3,"+ - "4"+ - "]"+ - "},"+ - "\"complexStringValue\":\"h\be\tllo w\u1234orld!\","+ - "\"intValue\":42,"+ - "\"doubleValue\":-23.45e67"+ - "}"; JSONStringer jsonStringer = new JSONStringer(); jsonStringer.object(); jsonStringer.key("trueValue").value(true); @@ -303,8 +271,34 @@ public void complexObjectString() { jsonStringer.endObject(); String str = jsonStringer.toString(); JSONObject jsonObject = new JSONObject(str); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 8 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 8); + assertTrue("expected 4 object2 items", ((Map)(JsonPath.read(doc, "$.object2"))).size() == 4); + assertTrue("expected 5 array1 items", ((List)(JsonPath.read(doc, "$.object2.array1"))).size() == 5); + assertTrue("expected 4 array[2] items", ((Map)(JsonPath.read(doc, "$.object2.array1[2]"))).size() == 4); + assertTrue("expected 4 array1[2].array2 items", ((List)(JsonPath.read(doc, "$.object2.array1[2].array2"))).size() == 4); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseValue"))); + assertTrue("expected null", null == JsonPath.read(doc, "$.nullValue")); + assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.stringValue"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.intValue"))); + assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$.doubleValue"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, "$.complexStringValue"))); + assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$.object2.k1"))); + assertTrue("expected v2", "v2".equals(JsonPath.read(doc, "$.object2.k2"))); + assertTrue("expected v3", "v3".equals(JsonPath.read(doc, "$.object2.k3"))); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.object2.array1[0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.object2.array1[1]"))); + assertTrue("expected v4", "v4".equals(JsonPath.read(doc, "$.object2.array1[2].k4"))); + assertTrue("expected v5", "v5".equals(JsonPath.read(doc, "$.object2.array1[2].k5"))); + assertTrue("expected v6", "v6".equals(JsonPath.read(doc, "$.object2.array1[2].k6"))); + assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$.object2.array1[2].array2[0]"))); + assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$.object2.array1[2].array2[1]"))); + assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$.object2.array1[2].array2[2]"))); + assertTrue("expected 8", Integer.valueOf(8).equals(JsonPath.read(doc, "$.object2.array1[2].array2[3]"))); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.object2.array1[3]"))); + assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$.object2.array1[4]"))); } } From abe421e6bbf3fa48833e885f408bdb550b0605d0 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 28 Dec 2015 12:07:44 -0600 Subject: [PATCH 177/315] clean up code --- CookieListTest.java | 95 ++++------ EnumTest.java | 15 +- JSONArrayTest.java | 54 ++---- JSONObjectTest.java | 428 +++++++++++++----------------------------- JSONStringerTest.java | 3 + 5 files changed, 196 insertions(+), 399 deletions(-) diff --git a/CookieListTest.java b/CookieListTest.java index 98ede86ce..5b298e72b 100644 --- a/CookieListTest.java +++ b/CookieListTest.java @@ -75,12 +75,10 @@ public void emptyStringCookieList() { public void simpleCookieList() { String cookieStr = "SID=31d4d96e407aad42"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - Object doc = Configuration.defaultConfiguration().jsonProvider(). - parse(jsonObject.toString()); - assertTrue("Expected 1 top level item", - ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 31d4d96e407aad42", - "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID"))); + // validate JSON content + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("Expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID"))); } /** @@ -90,12 +88,10 @@ public void simpleCookieList() { public void simpleCookieListWithDelimiter() { String cookieStr = "SID=31d4d96e407aad42;"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - Object doc = Configuration.defaultConfiguration().jsonProvider(). - parse(jsonObject.toString()); - assertTrue("Expected 1 top level item", - ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 31d4d96e407aad42", - "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID"))); + // validate JSON content + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("Expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID"))); } /** @@ -112,22 +108,15 @@ public void multiPartCookieList() { "name5=myCookieValue5;"+ " name6=myCookieValue6;"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - Object doc = Configuration.defaultConfiguration().jsonProvider(). - parse(jsonObject.toString()); - assertTrue("Expected 6 top level items", - ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected myCookieValue1", - "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); - assertTrue("expected myCookieValue2", - "myCookieValue2".equals(JsonPath.read(doc, "$.name2"))); - assertTrue("expected myCookieValue3", - "myCookieValue3".equals(JsonPath.read(doc, "$.name3"))); - assertTrue("expected myCookieValue4", - "myCookieValue4".equals(JsonPath.read(doc, "$.name4"))); - assertTrue("expected myCookieValue5", - "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); - assertTrue("expected myCookieValue6", - "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); + // validate JSON content + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("Expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); + assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2"))); + assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3"))); + assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4"))); + assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); + assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); } /** @@ -154,22 +143,15 @@ public void convertCookieListToString() { "name5=myCookieValue5;"+ " name6=myCookieValue6;"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - Object doc = Configuration.defaultConfiguration().jsonProvider(). - parse(jsonObject.toString()); - assertTrue("Expected 6 top level items", - ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected myCookieValue1", - "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); - assertTrue("expected myCookieValue2", - "myCookieValue2".equals(JsonPath.read(doc, "$.name2"))); - assertTrue("expected myCookieValue3", - "myCookieValue3".equals(JsonPath.read(doc, "$.name3"))); - assertTrue("expected myCookieValue4", - "myCookieValue4".equals(JsonPath.read(doc, "$.name4"))); - assertTrue("expected myCookieValue5", - "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); - assertTrue("expected myCookieValue6", - "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); + // validate JSON content + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("Expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); + assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2"))); + assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3"))); + assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4"))); + assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); + assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); } /** @@ -186,21 +168,14 @@ public void convertEncodedCookieListToString() { "name5=myCookieValue5;"+ " name6=myCookieValue6;"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - Object doc = Configuration.defaultConfiguration().jsonProvider(). - parse(jsonObject.toString()); - assertTrue("Expected 6 top level items", - ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected myCookieValue1", - "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); - assertTrue("expected my Cookie Value 2", - "my Cookie Value 2".equals(JsonPath.read(doc, "$.name2"))); - assertTrue("expected my+Cookie&Value;3=", - "my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3"))); - assertTrue("expected my%CookieValue4", - "my%CookieValue4".equals(JsonPath.read(doc, "$.name4"))); - assertTrue("expected my%CookieValue5", - "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); - assertTrue("expected myCookieValue6", - "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); + // validate JSON content + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("Expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); + assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(JsonPath.read(doc, "$.name2"))); + assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3"))); + assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(JsonPath.read(doc, "$.name4"))); + assertTrue("expected my%CookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); + assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); } } diff --git a/EnumTest.java b/EnumTest.java index 2d2ebb977..a591dbaca 100644 --- a/EnumTest.java +++ b/EnumTest.java @@ -35,8 +35,7 @@ public void jsonObjectFromEnum() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider() .parse(jsonObject.toString()); - assertTrue("expecting 2 items in top level object", - ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expecting 2 items in top level object", ((Map)(JsonPath.read(doc, "$"))).size() == 2); assertTrue("expecting val 2", "val 2".equals(JsonPath.read(doc, "$.value"))); assertTrue("expecting 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal"))); @@ -50,14 +49,10 @@ public void jsonObjectFromEnum() { jsonObject = new JSONObject(myEnumClass); // validate JSON content - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expecting 2 items in top level object", - ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expecting 2 items in myEnumField object", - ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); - assertTrue("expecting 0 items in myEnum object", - ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expecting 2 items in top level object", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expecting 2 items in myEnumField object", ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); + assertTrue("expecting 0 items in myEnum object", ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); assertTrue("expecting 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal"))); assertTrue("expecting val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value"))); } diff --git a/JSONArrayTest.java b/JSONArrayTest.java index c042acdca..455d68073 100644 --- a/JSONArrayTest.java +++ b/JSONArrayTest.java @@ -314,10 +314,8 @@ public void join() { /** * Don't need to remake the JSONArray to perform the parsing */ - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse("["+joinStr+"]"); - List docList = JsonPath.read(doc, "$"); - assertTrue("expected 13 items in top level object", docList.size() == 13); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse("["+joinStr+"]"); + assertTrue("expected 13 items in top level object", ((List)(JsonPath.read(doc, "$"))).size() == 13); assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); assertTrue("expected \"true\"", "true".equals(JsonPath.read(doc, "$[2]"))); @@ -327,11 +325,9 @@ public void join() { assertTrue("expected \"23.45\"", "23.45".equals(JsonPath.read(doc, "$[6]"))); assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[7]"))); assertTrue("expected \"43\"", "43".equals(JsonPath.read(doc, "$[8]"))); - docList = JsonPath.read(doc, "$[9]"); - assertTrue("expected 1 array item", docList.size() == 1); + assertTrue("expected 1 item in [9]", ((List)(JsonPath.read(doc, "$[9]"))).size() == 1); assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[9][0]"))); - Map docMap = JsonPath.read(doc, "$[10]"); - assertTrue("expected 4 object items", docMap.size() == 4); + assertTrue("expected 4 items in [10]", ((Map)(JsonPath.read(doc, "$[10]"))).size() == 4); assertTrue("expected value1", "value1".equals(JsonPath.read(doc, "$[10].key1"))); assertTrue("expected value2", "value2".equals(JsonPath.read(doc, "$[10].key2"))); assertTrue("expected value3", "value3".equals(JsonPath.read(doc, "$[10].key3"))); @@ -463,30 +459,24 @@ public void put() { jsonArray.put(collection); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - List docList = JsonPath.read(doc, "$"); - assertTrue("expected 10 items in top level object", docList.size() == 10); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 10 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 10); assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); - docList = JsonPath.read(doc, "$[2]"); - assertTrue("expected 2 items in array", docList.size() == 2); + assertTrue("expected 2 items in [2]", ((List)(JsonPath.read(doc, "$[2]"))).size() == 2); assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]"))); assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]"))); assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]"))); assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]"))); assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]"))); assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]"))); - Map docMap = JsonPath.read(doc, "$[7]"); - assertTrue("expected 3 items in object", docMap.size() == 3); + assertTrue("expected 3 items in [7]", ((Map)(JsonPath.read(doc, "$[7]"))).size() == 3); assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[7].key10"))); assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[7].key20"))); assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[7].key30"))); - docMap = JsonPath.read(doc, "$[8]"); - assertTrue("expected 1 item in object", docMap.size() == 1); + assertTrue("expected 1 item in [8]", ((Map)(JsonPath.read(doc, "$[8]"))).size() == 1); assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[8].k1"))); - docList = JsonPath.read(doc, "$[9]"); - assertTrue("expected 2 items in array", docList.size() == 2); + assertTrue("expected 2 items in [9]", ((List)(JsonPath.read(doc, "$[9]"))).size() == 2); assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]"))); assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]"))); } @@ -546,14 +536,11 @@ public void putIndex() { } catch(Exception ignored) {} // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - List docList = JsonPath.read(doc, "$"); - assertTrue("expected 11 items in top level object", docList.size() == 11); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 11 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 11); assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); - docList = JsonPath.read(doc, "$[2]"); - assertTrue("expected 2 items in array", docList.size() == 2); + assertTrue("expected 2 items in [2]", ((List)(JsonPath.read(doc, "$[2]"))).size() == 2); assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]"))); assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]"))); assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]"))); @@ -561,17 +548,14 @@ public void putIndex() { assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]"))); assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]"))); assertTrue("expected null", null == JsonPath.read(doc, "$[7]")); - Map docMap = JsonPath.read(doc, "$[8]"); - assertTrue("expected 3 items in object", docMap.size() == 3); + assertTrue("expected 3 items in [8]", ((Map)(JsonPath.read(doc, "$[8]"))).size() == 3); assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[8].key10"))); assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[8].key20"))); assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[8].key30"))); - docList = JsonPath.read(doc, "$[9]"); - assertTrue("expected 2 items in array", docList.size() == 2); + assertTrue("expected 2 items in [9]", ((List)(JsonPath.read(doc, "$[9]"))).size() == 2); assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]"))); assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]"))); - docMap = JsonPath.read(doc, "$[10]"); - assertTrue("expected 1 item in object", docMap.size() == 1); + assertTrue("expected 1 item in [10]", ((Map)(JsonPath.read(doc, "$[10]"))).size() == 1); assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[10].k1"))); } @@ -651,10 +635,8 @@ public void objectArrayVsIsArray() { JSONArray jsonArray = new JSONArray(myObject); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - List docList = JsonPath.read(doc, "$"); - assertTrue("expected 7 items in top level object", docList.size() == 7); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 7 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 7); assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); diff --git a/JSONObjectTest.java b/JSONObjectTest.java index d420e2228..4648dce8d 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -110,20 +110,12 @@ public void jsonObjectByNames() { // validate JSON JSONObject jsonObjectByName = new JSONObject(jsonObject, keys); - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObjectByName.toString()); - Map> docMap = JsonPath.read(doc, "$"); - assertTrue("expected 4 items", docMap.size() == 4); - assertTrue("expected \"falseKey\":false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"nullKey\":null", - null == JsonPath.read(doc, "$.nullKey")); - assertTrue("expected \"stringKey\":\"hello world!\"", - "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue( - "expected \"doubleKey\":-23.45e67", - Double.valueOf("-23.45e67").equals( - JsonPath.read(doc, "$.doubleKey"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObjectByName.toString()); + assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"nullKey\":null", null == JsonPath.read(doc, "$.nullKey")); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); } /** @@ -155,23 +147,13 @@ public void jsonObjectByMap() { JSONObject jsonObject = new JSONObject(map); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 6 items", docMap.size() == 6); - assertTrue("expected \"trueKey\":true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected \"falseKey\":false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", - "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", - "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, - "$.escapeStringKey"))); - assertTrue( - "expected \"doubleKey\":-23.45e67", - Double.valueOf("-23.45e67").equals( - JsonPath.read(doc, "$.doubleKey"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); } /** @@ -305,14 +287,10 @@ public void jsonObjectByMapWithUnsupportedValues() { JSONObject jsonObject = new JSONObject(jsonMap); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 2 items", docMap.size() == 2); - assertTrue("expected \"key2\":java.lang.Exception", - "java.lang.Exception".equals(JsonPath.read(doc, "$.key2"))); - docMap = JsonPath.read(doc, "$.key1"); - assertTrue("expected 0 items", docMap.size() == 0); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(JsonPath.read(doc, "$.key2"))); + assertTrue("expected 0 key1 items", ((Map)(JsonPath.read(doc, "$.key1"))).size() == 0); } /** @@ -332,25 +310,14 @@ public void jsonObjectByMapWithNullValue() { JSONObject jsonObject = new JSONObject(map); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 6 items", docMap.size() == 6); - assertTrue("expected \"trueKey\":true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected \"falseKey\":false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", - "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", - "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, - "$.escapeStringKey"))); - assertTrue("expected \"intKey\":42", - Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); - assertTrue( - "expected \"doubleKey\":-23.45e67", - Double.valueOf("-23.45e67").equals( - JsonPath.read(doc, "$.doubleKey"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); + assertTrue("expected \"intKey\":42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); } /** @@ -386,35 +353,19 @@ public String toString(){ JSONObject jsonObject = new JSONObject(myBean); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 8 items", docMap.size() == 8); - assertTrue("expected \"trueKey\":true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected \"falseKey\":false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", - "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", - "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, - "$.escapeStringKey"))); - assertTrue("expected \"intKey\":42", - Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); - assertTrue("expected \"doubleKey\":-23.45e7", Double - .valueOf("-23.45e7").equals(JsonPath.read(doc, "$.doubleKey"))); - assertTrue( - "expected \"stringReaderKey\":{}", - ((Map) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 8 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 8); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected hello world!","hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); + assertTrue("expected 42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); + assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(JsonPath.read(doc, "$.doubleKey"))); + assertTrue("expected 0 items in stringReaderKey", ((Map) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0); // sorry, mockito artifact - List docList = JsonPath.read(doc, "$.callbacks"); - assertTrue("expected 2 items", docList.size() == 2); - assertTrue("expected \"handler\":{}", - ((Map) (JsonPath.read(doc, - "$.callbacks[0].handler"))).size() == 0); - assertTrue("expected empty object", - ((Map) (JsonPath.read(doc, "$.callbacks[1]"))) - .size() == 0); + assertTrue("expected 2 callbacks items", ((List)(JsonPath.read(doc, "$.callbacks"))).size() == 2); + assertTrue("expected 0 handler items", ((Map)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0); + assertTrue("expected 0 callbacks[1] items", ((Map)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0); } /** @@ -431,14 +382,10 @@ public void jsonObjectByObjectAndNames() { JSONObject jsonObject = new JSONObject(jsonObjectTest, keys); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 2 items", docMap.size() == 2); - assertTrue("expected \"publicString\":\"abc\"", - "abc".equals(JsonPath.read(doc, "$.publicString"))); - assertTrue("expected \"publicInt\":42", - Integer.valueOf(42).equals(JsonPath.read(doc, "$.publicInt"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected \"publicString\":\"abc\"", "abc".equals(JsonPath.read(doc, "$.publicString"))); + assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.publicInt"))); } /** @@ -452,22 +399,14 @@ public void jsonObjectByResourceBundle() { Locale.getDefault()); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 2 items in top level map", docMap.size() == 2); - docMap = JsonPath.read(doc, "$.greetings"); - assertTrue("expected 2 items in greetings map", docMap.size() == 2); - assertTrue("expected \"hello\":\"Hello, \"", - "Hello, ".equals(JsonPath.read(doc, "$.greetings.hello"))); - assertTrue("expected \"world\":\"World!\"", - "World!".equals(JsonPath.read(doc, "$.greetings.world"))); - docMap = JsonPath.read(doc, "$.farewells"); - assertTrue("expected 2 items in farewells map", docMap.size() == 2); - assertTrue("expected \"later\":\"Later, \"", - "Later, ".equals(JsonPath.read(doc, "$.farewells.later"))); - assertTrue("expected \"world\":\"World!\"", - "Alligator!".equals(JsonPath.read(doc, "$.farewells.gator"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected 2 greetings items", ((Map)(JsonPath.read(doc, "$.greetings"))).size() == 2); + assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(JsonPath.read(doc, "$.greetings.hello"))); + assertTrue("expected \"world\":\"World!\"", "World!".equals(JsonPath.read(doc, "$.greetings.world"))); + assertTrue("expected 2 farewells items", ((Map)(JsonPath.read(doc, "$.farewells"))).size() == 2); + assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(JsonPath.read(doc, "$.farewells.later"))); + assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(JsonPath.read(doc, "$.farewells.gator"))); } /** @@ -486,27 +425,15 @@ public void jsonObjectAccumulate() { jsonObject.accumulate("myArray", -23.45e7); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 1 item in top level object", docMap.size() == 1); - List docList = JsonPath.read(doc, "$.myArray"); - assertTrue("expected 6 items in myArray", docList.size() == 6); - assertTrue("expected true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); - assertTrue("expected false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); - assertTrue("expected hello world!", - "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); - assertTrue("expected h\be\tllo w\u1234orld!", - "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, - "$.myArray[3]"))); - assertTrue("expected 42", - Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); - assertTrue( - "expected -23.45e7", - Double.valueOf(-23.45e7).equals( - JsonPath.read(doc, "$.myArray[5]"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); + assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); + assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]"))); } /** @@ -524,27 +451,15 @@ public void jsonObjectAppend() { jsonObject.append("myArray", -23.45e7); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 1 item in top level object", docMap.size() == 1); - List docList = JsonPath.read(doc, "$.myArray"); - assertTrue("expected 6 items in myArray", docList.size() == 6); - assertTrue("expected true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); - assertTrue("expected false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); - assertTrue("expected hello world!", - "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); - assertTrue("expected h\be\tllo w\u1234orld!", - "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, - "$.myArray[3]"))); - assertTrue("expected 42", - Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); - assertTrue( - "expected -23.45e7", - Double.valueOf(-23.45e7).equals( - JsonPath.read(doc, "$.myArray[5]"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); + assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); + assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]"))); } /** @@ -1215,19 +1130,11 @@ public void jsonObjectNamesToJsonAray() { JSONArray jsonArray = jsonObject.names(); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - List docList = JsonPath.read(doc, "$"); - assertTrue("expected 3 items in array", docList.size() == 3); - assertTrue( - "expected to find trueKey", - ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); - assertTrue( - "expected to find falseKey", - ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); - assertTrue( - "expected to find stringKey", - ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected to find trueKey", ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); + assertTrue("expected to find falseKey", ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); + assertTrue("expected to find stringKey", ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); } /** @@ -1253,18 +1160,12 @@ public void jsonObjectIncrement() { jsonObject.increment("keyFloat"); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 4 items in object", docMap.size() == 4); - assertTrue("expected to find keyInt:3", - Integer.valueOf(3).equals(JsonPath.read(doc, "$.keyInt"))); - assertTrue( - "expected to find keyLong:9999999993", - Long.valueOf(9999999993L).equals( - JsonPath.read(doc, "$.keyLong"))); - assertTrue("expected to find keyDouble:3.1", Double.valueOf(3.1) - .equals(JsonPath.read(doc, "$.keyDouble"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); + 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"))); + /** * Should work the same way on any platform! @see https://docs.oracle * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is the @@ -1288,10 +1189,7 @@ public void jsonObjectIncrement() { * missing bits would not fit into the 32 bit float, i.e. the * information needed simply is not there! */ - assertTrue( - "expected to find keyFloat:3.0999999046325684", - Double.valueOf(3.0999999046325684).equals( - JsonPath.read(doc, "$.keyFloat"))); + assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(JsonPath.read(doc, "$.keyFloat"))); /** * float f = 3.1f; double df = (double) f; double d = 3.1d; @@ -1399,32 +1297,19 @@ public void jsonObjectPut() { jsonObject.put("objectKey", myMap); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 4 items in object", docMap.size() == 4); - assertTrue("expected to find trueKey:true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected to find falseKey:false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - List docList = JsonPath.read(doc, "$.arrayKey"); - assertTrue("expected 3 items in array", docList.size() == 3); - assertTrue("expected to find 0", - Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); - assertTrue("expected to find 1", - Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); - assertTrue("expected to find 2", - Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); - docMap = JsonPath.read(doc, "$.objectKey"); - assertTrue("expected 4 items in object", docMap.size() == 4); - assertTrue("expected to find myKey1:myVal1", - "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); - assertTrue("expected to find myKey2:myVal2", - "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); - assertTrue("expected to find myKey3:myVal3", - "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); - assertTrue("expected to find myKey4:myVal4", - "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); + assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); + assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); + assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); + assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); + assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); + assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); jsonObject.remove("trueKey"); JSONObject expectedJsonObject = new JSONObject(expectedStr); @@ -1475,32 +1360,19 @@ public void jsonObjectToString() { JSONObject jsonObject = new JSONObject(str); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 4 items in object", docMap.size() == 4); - assertTrue("expected to find \"trueKey\":true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected to find \"falseKey\":false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - List docList = JsonPath.read(doc, "$.arrayKey"); - assertTrue("expected 3 array items", docList.size() == 3); - assertTrue("expected array value 0", - Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); - assertTrue("expected array value 1", - Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); - assertTrue("expected array value 2", - Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); - docMap = JsonPath.read(doc, "$.objectKey"); - assertTrue("expected 4 items in objectKey object", docMap.size() == 4); - assertTrue("expected objectKey myKey1:myVal1", - "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); - assertTrue("expected objectKey myKey2:myVal2", - "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); - assertTrue("expected objectKey myKey3:myVal3", - "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); - assertTrue("expected objectKey myKey4:myVal4", - "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); + assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); + assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); + assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); + assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); + assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); + assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); } /** @@ -1518,14 +1390,10 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { jsonObject.put("key", map); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 1 item in object", docMap.size() == 1); - docMap = JsonPath.read(doc, "$.key"); - assertTrue("expected 1 item in key object", docMap.size() == 1); - assertTrue("expected abc:def", - "def".equals(JsonPath.read(doc, "$.key.abc"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 1 key item", ((Map)(JsonPath.read(doc, "$.key"))).size() == 1); + assertTrue("expected def", "def".equals(JsonPath.read(doc, "$.key.abc"))); } /** @@ -1544,12 +1412,9 @@ public void jsonObjectToStringSuppressWarningOnCastToCollection() { jsonObject.put("key", collection); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 1 item in object", docMap.size() == 1); - List docList = JsonPath.read(doc, "$.key"); - assertTrue("expected 1 item in key object", docList.size() == 1); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 1 key item", ((List)(JsonPath.read(doc, "$.key"))).size() == 1); assertTrue("expected abc", "abc".equals(JsonPath.read(doc, "$.key[0]"))); } @@ -1613,12 +1478,9 @@ public void valueToStringConfirmException() { // this is the test, it should not throw an exception String str = JSONObject.valueToString(myMap); // confirm result, just in case - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(str); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 1 item in object", docMap.size() == 1); - assertTrue("expected myValue", - "myValue".equals(JsonPath.read(doc, "$.1"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(str); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected myValue", "myValue".equals(JsonPath.read(doc, "$.1"))); } /** @@ -1667,44 +1529,29 @@ public void wrapObject() { JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection)); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - List docList = JsonPath.read(doc, "$"); - assertTrue("expected 3 items in array", docList.size() == 3); - assertTrue("expected 1", - Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", - Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", - Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // wrap Array returns JSONArray Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - docList = JsonPath.read(doc, "$"); - assertTrue("expected 3 items in array", docList.size() == 3); - assertTrue("expected 1", - Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", - Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", - Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(integerArrayJsonArray.toString()); - docList = JsonPath.read(doc, "$"); - assertTrue("expected 3 items in array", docList.size() == 3); - assertTrue("expected 1", - Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", - Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", - Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // wrap map returns JSONObject Map map = new HashMap(); @@ -1714,16 +1561,11 @@ public void wrapObject() { JSONObject mapJsonObject = (JSONObject) (JSONObject.wrap(map)); // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(mapJsonObject.toString()); - Map docMap = JsonPath.read(doc, "$"); - assertTrue("expected 3 items in object", docMap.size() == 3); - assertTrue("expected key1:val1", - "val1".equals(JsonPath.read(doc, "$.key1"))); - assertTrue("expected key2:val2", - "val2".equals(JsonPath.read(doc, "$.key2"))); - assertTrue("expected key3:val3", - "val3".equals(JsonPath.read(doc, "$.key3"))); + doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString()); + assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); + 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) } diff --git a/JSONStringerTest.java b/JSONStringerTest.java index 4e9a9dbab..c9419df94 100644 --- a/JSONStringerTest.java +++ b/JSONStringerTest.java @@ -192,6 +192,7 @@ public void simpleObjectString() { String str = jsonStringer.toString(); JSONObject jsonObject = new JSONObject(str); + // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 7 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 7); assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue"))); @@ -221,6 +222,7 @@ public void simpleArrayString() { String str = jsonStringer.toString(); JSONArray jsonArray = new JSONArray(str); + // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); assertTrue("expected 6 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 6); assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); @@ -272,6 +274,7 @@ public void complexObjectString() { String str = jsonStringer.toString(); JSONObject jsonObject = new JSONObject(str); + // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 8 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 8); assertTrue("expected 4 object2 items", ((Map)(JsonPath.read(doc, "$.object2"))).size() == 4); From 472439546a5274a420eb30637bf785516156ef0a Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 28 Dec 2015 12:20:43 -0600 Subject: [PATCH 178/315] Update README.md --- README.md | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 19d0354f8..296d27b6d 100644 --- a/README.md +++ b/README.md @@ -12,15 +12,51 @@ Run individual tests or JunitTestSuite using EclEmma Coverage, or **You will need the following libraries for testing:**
Test harness: http://junit.org
-* hamcrest-core-1.3.jar (for Junit)
+* asm-1.0.2.jar
+* commons-io-2.1.jar
+* commons-lang-2.6.jar
+* hamcrest-core-1.3.jar
+* json-path-2.1.0.jar
+* json-smart-2.1.1.jar
* junit-4.12.jar
- -Mockery: https://github.com/mockito/mockito
* mockito-all-1.9.5.jar
- -Coverage: http://www.eclemma.org/ (just install the latest in Eclipse)
- -JSON-Java.jar (make this jar of the files to be tested yourself)
+* slf4j-api-1.7.12.jar
+* slf-simple-1.7.12.jar
+* JSON-java.jar
+ +**To build from the command line using gradle:** +build.gradle
+'''' +# In this example, both the JSON-java jar and the test code is built
+# from the same build file, in the test code directory. +apply plugin: 'java' +jar.baseName = 'JSON-java' + +sourceSets { + main { + java { + srcDir '../JSON-java/src/org/json' + } + } + test { + java { + srcDir 'src/org/json/junit' + } + } +} + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.+' + testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0' + testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5' +} +'''' + +To measure coverage: http://www.eclemma.org/ (just install the latest in Eclipse)
Conventions
Test filenames should consist of the name of the module being tested, with the suffix "Test". From 633ab108e7708f10c47d86f8b2cfdd5b193ab061 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 28 Dec 2015 12:22:29 -0600 Subject: [PATCH 179/315] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 296d27b6d..d7672ca8e 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,9 @@ Test harness: http://junit.org
**To build from the command line using gradle:** build.gradle
-'''' -# In this example, both the JSON-java jar and the test code is built
-# from the same build file, in the test code directory. +```` +\# In this example, both the JSON-java jar and the test code is built
+\# from the same build file, in the test code directory. apply plugin: 'java' jar.baseName = 'JSON-java' @@ -54,7 +54,7 @@ dependencies { testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0' testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5' } -'''' +```` To measure coverage: http://www.eclemma.org/ (just install the latest in Eclipse)
From 54cd97ded114f8523f198f85a3ce8b6e60f990ba Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 28 Dec 2015 12:24:46 -0600 Subject: [PATCH 180/315] Update README.md --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d7672ca8e..aff849f62 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,11 @@ Test harness: http://junit.org
* JSON-java.jar
**To build from the command line using gradle:** -build.gradle
```` -\# In this example, both the JSON-java jar and the test code is built
-\# from the same build file, in the test code directory. +build.gradle +# In this example, both the JSON-java jar and the test code is created
+# from the same build file, in the test code directory. 3rd party jars are +# obtained from the maven repository. apply plugin: 'java' jar.baseName = 'JSON-java' From 91fcd6092f92873fc45a2031a444a30b59b7e5af Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 28 Dec 2015 12:25:11 -0600 Subject: [PATCH 181/315] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index aff849f62..ac87e41c5 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ Eclipse is the recommended development environment.
Run individual tests or JunitTestSuite using EclEmma Coverage, or execute the **TestRunner** application directly.
**You will need the following libraries for testing:**
-Test harness: http://junit.org
* asm-1.0.2.jar
* commons-io-2.1.jar
* commons-lang-2.6.jar
From 95cf86688de41c611f0a8f5a245899ff8139559c Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 28 Dec 2015 12:25:40 -0600 Subject: [PATCH 182/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ac87e41c5..eee0a71ff 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Run individual tests or JunitTestSuite using EclEmma Coverage, or **To build from the command line using gradle:** ```` build.gradle -# In this example, both the JSON-java jar and the test code is created
+# In this example, both the JSON-java jar and the test code is created # from the same build file, in the test code directory. 3rd party jars are # obtained from the maven repository. apply plugin: 'java' From 7f83a5171837818856e21b610dc192549eb5d306 Mon Sep 17 00:00:00 2001 From: stleary Date: Tue, 29 Dec 2015 17:56:43 -0600 Subject: [PATCH 183/315] refactor test classes to their own modules --- JSONObjectTest.java | 50 +++++--------------------------------------- MyBean.java | 16 ++++++++++++++ MyBigNumberBean.java | 11 ++++++++++ MyJsonString.java | 14 +++++++++++++ MyPublicClass.java | 9 ++++++++ 5 files changed, 55 insertions(+), 45 deletions(-) create mode 100644 MyBean.java create mode 100644 MyBigNumberBean.java create mode 100644 MyJsonString.java create mode 100644 MyPublicClass.java diff --git a/JSONObjectTest.java b/JSONObjectTest.java index d420e2228..80bef48ea 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -16,57 +16,17 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import org.json.JSONString; import org.json.XML; import org.junit.Test; import com.jayway.jsonpath.*; -/** - * Used in testing when a JSONString is needed - */ -class MyJsonString implements JSONString { - - @Override - public String toJSONString() { - return "my string"; - } -} - -/** - * Used in testing when Bean behavior is needed - */ -interface MyBean { - public Integer getIntKey(); - public Double getDoubleKey(); - public String getStringKey(); - public String getEscapeStringKey(); - public Boolean isTrueKey(); - public Boolean isFalseKey(); - public StringReader getStringReaderKey(); -}; - -/** - * Used in testing when a Bean containing big numbers is needed - */ -interface MyBigNumberBean { - public BigInteger getBigInteger(); - public BigDecimal getBigDecimal(); -} - /** * JSONObject, along with JSONArray, are the central classes of the reference app. * All of the other classes interact with them, and JSON functionality would * otherwise be impossible. */ public class JSONObjectTest { - /** - * Need a class with some public data members for testing, so - * JSONObjectTest itself will be used for this purpose. - * TODO: Why not use MyBigNumberBean or MyBean? - */ - public Integer publicInt = 42; - public String publicString = "abc"; /** * JSONObject built from a bean, but only using a null value. @@ -427,8 +387,8 @@ public String toString(){ public void jsonObjectByObjectAndNames() { String[] keys = {"publicString", "publicInt"}; // just need a class that has public data members - JSONObjectTest jsonObjectTest = new JSONObjectTest(); - JSONObject jsonObject = new JSONObject(jsonObjectTest, keys); + MyPublicClass myPublicClass = new MyPublicClass(); + JSONObject jsonObject = new JSONObject(myPublicClass, keys); // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider() @@ -1168,10 +1128,10 @@ public void jsonObjectNames() { /** * A bean is also an object. But in order to test the static * method getNames(), this particular bean needs some public - * data members, which have been added to the class. + * data members. */ - JSONObjectTest jsonObjectTest = new JSONObjectTest(); - names = JSONObject.getNames(jsonObjectTest); + MyPublicClass myPublicClass = new MyPublicClass(); + names = JSONObject.getNames(myPublicClass); // validate JSON jsonArray = new JSONArray(names); diff --git a/MyBean.java b/MyBean.java new file mode 100644 index 000000000..53d150a52 --- /dev/null +++ b/MyBean.java @@ -0,0 +1,16 @@ +package org.json.junit; + +import java.io.*; + +/** + * Used in testing when Bean behavior is needed + */ +interface MyBean { + public Integer getIntKey(); + public Double getDoubleKey(); + public String getStringKey(); + public String getEscapeStringKey(); + public Boolean isTrueKey(); + public Boolean isFalseKey(); + public StringReader getStringReaderKey(); +} \ No newline at end of file diff --git a/MyBigNumberBean.java b/MyBigNumberBean.java new file mode 100644 index 000000000..0ca18704b --- /dev/null +++ b/MyBigNumberBean.java @@ -0,0 +1,11 @@ +package org.json.junit; + +import java.math.*; + +/** + * Used in testing when a Bean containing big numbers is needed + */ +interface MyBigNumberBean { + public BigInteger getBigInteger(); + public BigDecimal getBigDecimal(); +} \ No newline at end of file diff --git a/MyJsonString.java b/MyJsonString.java new file mode 100644 index 000000000..4e636933d --- /dev/null +++ b/MyJsonString.java @@ -0,0 +1,14 @@ +package org.json.junit; + +import org.json.*; + +/** + * Used in testing when a JSONString is needed + */ +class MyJsonString implements JSONString { + + @Override + public String toJSONString() { + return "my string"; + } +} \ No newline at end of file diff --git a/MyPublicClass.java b/MyPublicClass.java new file mode 100644 index 000000000..1f55e3e3e --- /dev/null +++ b/MyPublicClass.java @@ -0,0 +1,9 @@ +package org.json.junit; + +/** + * Need a class with some public data members for testing + */ +public class MyPublicClass { + public Integer publicInt = 42; + public String publicString = "abc"; +} From 0990f340dbf42247f8148a662c5a2164190aaecd Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Wed, 30 Dec 2015 00:00:14 -0600 Subject: [PATCH 184/315] Update README.md --- README.md | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index eee0a71ff..d908b5223 100644 --- a/README.md +++ b/README.md @@ -91,32 +91,37 @@ A unit test has the following stages: | Test file name | Coverage | Comments | | ------------- | ------------- | ---- | -| Total coverage | 88.9% | | | +| Total coverage | 90.6% | | | | | | | -| CDL.java | 98% | Reasonable test cases. | +| CDL.java | 98.8% | Reasonable test cases. | | Cookie.java | 98.9% | Reasonable test cases. | | CookieList.java |96.5% | Reasonable test cases. | | EnumTest.java | n/a | Just documenting how enums are handled. | | HTTP.java | 98.7%| Coverage > 90% | | HTTPTokener.java |93.2% | No test | -| JSONArray.java |95.9% | Coverage > 90% | +| JSONArray.java |95.9% | Reasonable test cases | | JSONException.java | 26.7% | No test | -| JSONML.java | 83.2%| In progress | -| JSONObject | 90.9% | Coverage > 90% | +| JSONML.java | 86.8%| In progress | +| JSONObject | 94.0% | Reasonable test cases | | JSONObject.Null | 87.5% | No test | | JSONString.java | | No test | | JSONStringer.java | 93.8%| Coverage > 90% | | JSONTokener.java | 72.1% | In progress | -| JSONWriter.java | 88.9% | No test | +| JSONWriter.java | 87.5% | No test | | Property.java | 94.8% | Coverage > 90% | -| XML.java | 85.1% | In progress | +| XML.java | 87.4% | In progress | | XMLTokener.java| 82.7%| No test | | Files used in test | | ------------- | +| MyBean.java | +| MyBigNumberBean.java | | MyEnum.java | | MyEnumClass.java | | MyEnumField.java | +| MyJsonString.java | +| MyPublicClass.java | +| PropertyTest.java | | JunitTestSuite.java | | StringsResourceBundle.java | | TestRunner.java | From fc318a765c02bb886f0fe12e1fce6f40e2dec751 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 30 Dec 2015 00:00:58 -0600 Subject: [PATCH 185/315] Fix some todos, clean up some tests, improve coverage --- CookieListTest.java | 5 +++ JSONObjectTest.java | 84 ++++++++++++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/CookieListTest.java b/CookieListTest.java index 5b298e72b..815c76737 100644 --- a/CookieListTest.java +++ b/CookieListTest.java @@ -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); diff --git a/JSONObjectTest.java b/JSONObjectTest.java index 72a8f0658..994c4b35e 100644 --- a/JSONObjectTest.java +++ b/JSONObjectTest.java @@ -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); @@ -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()); @@ -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); @@ -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()); @@ -401,7 +396,6 @@ 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); @@ -409,6 +403,11 @@ public void jsonObjectAppend() { 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()); @@ -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. @@ -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(); @@ -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"); @@ -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 @@ -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) } /** From c88d06eede9a50b2eb0ab8b705abc980b448207c Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 30 Dec 2015 01:01:43 -0600 Subject: [PATCH 186/315] util cleanup --- JSONMLTest.java | 4 +++- Util.java | 45 --------------------------------------------- 2 files changed, 3 insertions(+), 46 deletions(-) diff --git a/JSONMLTest.java b/JSONMLTest.java index a16f0ea53..953a39dd6 100644 --- a/JSONMLTest.java +++ b/JSONMLTest.java @@ -655,7 +655,9 @@ public void toJSONObjectToJSONArray() { // lastly, confirm the restored JSONObject XML and JSONArray XML look // reasonably similar - Util.compareXML(jsonObjectXmlToStr, jsonArrayXmlToStr); + JSONObject jsonObjectFromObject = JSONML.toJSONObject(jsonObjectXmlToStr); + JSONObject jsonObjectFromArray = JSONML.toJSONObject(jsonArrayXmlToStr); + Util.compareActualVsExpectedJsonObjects(jsonObjectFromObject, jsonObjectFromArray); } /** diff --git a/Util.java b/Util.java index eb33ab5b2..844669015 100644 --- a/Util.java +++ b/Util.java @@ -94,49 +94,4 @@ private static void compareActualVsExpectedObjects(Object value, value.toString().equals(expectedValue.toString())); } } - - /** - * Sometimes test completion requires comparison of JSONArray objects that - * were produced from a JSONObject, and so unordered. This method is - * imperfect since it only compares the array elements and won't catch - * JSON syntax errors but at least it does not rely on ordering - *

- * It is expected that the arrays to be compared come from JSONArray - * instances which have been rendered by toString(), and whose syntax - * chars have been removed. - *

- * TODO: why are we not simply comparing the JSONArrays? - *

- * @param names an array of strings for comparison - * @param expectedNames the other array of strings for comparison - */ - public static void compareActualVsExpectedStringArrays(String[] names, - String [] expectedNames) { - assertTrue("Array lengths should be equal", - names.length == expectedNames.length); - List lNames = new ArrayList(Arrays.asList(names)); - for (int i = 0; i < expectedNames.length; ++i) { - String expectedName = expectedNames[i]; - assertTrue("expected to find "+expectedName, - lNames.contains(expectedName)); - lNames.remove(expectedName); - } - } - - /** - * This is stopgap test utility. It is meant to compare strings - * of XML, but it does not take ordering into account and should - * not be expected to work correctly with complex XML. - * @param aXmlStr an XML doc to be compared - * @param bXmlStr the other XML doc to be compared - */ - public static void compareXML(String aXmlStr, String bXmlStr) { - // TODO For simple tests this may be adequate, but it won't work for - // elements with multiple attributes and possibly other cases as well. - // Should use XMLUnit or similar. - assertTrue("expected equal XML strings \naXmlStr: "+ - aXmlStr+ "\nbXmlStr: " +bXmlStr, aXmlStr.equals(bXmlStr)); - - } - } From 871a3e46d77bf6ec51cf3ae8654133ff4aca3597 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 30 Dec 2015 13:50:51 -0600 Subject: [PATCH 187/315] clean up a few more todos --- JSONStringerTest.java | 70 +++++++++++++++++++++---------------------- Util.java | 15 +++++----- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/JSONStringerTest.java b/JSONStringerTest.java index c9419df94..631d79221 100644 --- a/JSONStringerTest.java +++ b/JSONStringerTest.java @@ -11,8 +11,7 @@ /** - * Tests for JSON-Java JSONStringer. - * TODO: Could use a lot more testing. For example, cascade-style productions. + * Tests for JSON-Java JSONStringer and JSONWriter. */ public class JSONStringerTest { @@ -234,43 +233,44 @@ public void simpleArrayString() { } /** - * Build a nested JSON doc using JSONString API calls, - * then convert to JSONObject + * Build a nested JSON doc using JSONString API calls, then convert to + * JSONObject. Will create a long cascade of output by reusing the + * returned values.. */ @Test public void complexObjectString() { JSONStringer jsonStringer = new JSONStringer(); - jsonStringer.object(); - jsonStringer.key("trueValue").value(true); - jsonStringer.key("falseValue").value(false); - jsonStringer.key("nullValue").value(null); - jsonStringer.key("stringValue").value("hello world!"); - jsonStringer.key("object2").object(); - jsonStringer.key("k1").value("v1"); - jsonStringer.key("k2").value("v2"); - jsonStringer.key("k3").value("v3"); - jsonStringer.key("array1").array(); - jsonStringer.value(1); - jsonStringer.value(2); - jsonStringer.object(); - jsonStringer.key("k4").value("v4"); - jsonStringer.key("k5").value("v5"); - jsonStringer.key("k6").value("v6"); - jsonStringer.key("array2").array(); - jsonStringer.value(5); - jsonStringer.value(6); - jsonStringer.value(7); - jsonStringer.value(8); - jsonStringer.endArray(); - jsonStringer.endObject(); - jsonStringer.value(3); - jsonStringer.value(4); - jsonStringer.endArray(); - jsonStringer.endObject(); - jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!"); - jsonStringer.key("intValue").value(42); - jsonStringer.key("doubleValue").value(-23.45e67); - jsonStringer.endObject(); + jsonStringer.object(). + key("trueValue").value(true). + key("falseValue").value(false). + key("nullValue").value(null). + key("stringValue").value("hello world!"). + key("object2").object(). + key("k1").value("v1"). + key("k2").value("v2"). + key("k3").value("v3"). + key("array1").array(). + value(1). + value(2). + object(). + key("k4").value("v4"). + key("k5").value("v5"). + key("k6").value("v6"). + key("array2").array(). + value(5). + value(6). + value(7). + value(8). + endArray(). + endObject(). + value(3). + value(4). + endArray(). + endObject(). + key("complexStringValue").value("h\be\tllo w\u1234orld!"). + key("intValue").value(42). + key("doubleValue").value(-23.45e67). + endObject(); String str = jsonStringer.toString(); JSONObject jsonObject = new JSONObject(str); diff --git a/Util.java b/Util.java index 844669015..6b23d0050 100644 --- a/Util.java +++ b/Util.java @@ -59,24 +59,26 @@ public static void compareActualVsExpectedJsonObjects( private static void compareActualVsExpectedObjects(Object value, Object expectedValue) { if (value instanceof JSONObject && expectedValue instanceof JSONObject) { + // Compare JSONObjects JSONObject jsonObject = (JSONObject)value; JSONObject expectedJsonObject = (JSONObject)expectedValue; compareActualVsExpectedJsonObjects( jsonObject, expectedJsonObject); } else if (value instanceof JSONArray && expectedValue instanceof JSONArray) { + // Compare JSONArrays JSONArray jsonArray = (JSONArray)value; JSONArray expectedJsonArray = (JSONArray)expectedValue; compareActualVsExpectedJsonArrays( jsonArray, expectedJsonArray); } else { /** - * Certain helper classes (e.g. XML) may create Long instead of - * Integer for small int values. As long as both are Numbers, - * just compare the toString() values. - * TODO: this may not work in the case where the underlying types - * do not have the same precision. + * Compare all other types using toString(). First, the types must + * also be equal, unless both are Number type. Certain helper + * classes (e.g. XML) may create Long instead of Integer for small + * int values. */ if (!(value instanceof Number && expectedValue instanceof Number)) { + // Non-Number and non-matching types assertTrue("object types should be equal for actual: "+ value.toString()+" ("+ value.getClass().toString()+") expected: "+ @@ -86,8 +88,7 @@ private static void compareActualVsExpectedObjects(Object value, expectedValue.getClass().toString())); } /** - * When in doubt, compare by string - * TODO: should not this be an else to the previous condition? + * Same types or both Numbers, compare by toString() */ assertTrue("string values should be equal for actual: "+ value.toString()+" expected: "+expectedValue.toString(), From 280ce7128586d6418b2319a491cdec4d5e9327b9 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 1 Jan 2016 13:54:47 -0600 Subject: [PATCH 188/315] tabs to spaces --- XMLTest.java | 80 ++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/XMLTest.java b/XMLTest.java index 8069f709e..d35c8ac23 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -391,16 +391,16 @@ public void shouldHandleArraytoString() { */ @Test public void shouldHandleEmptyArray(){ - final JSONObject jo1 = new JSONObject(); - jo1.put("array",new Object[]{}); - final JSONObject jo2 = new JSONObject(); - jo2.put("array",new JSONArray()); - - final String expected = ""; - String output1 = XML.toString(jo1,"jo"); - assertTrue("Expected an empty root tag", expected.equals(output1)); - String output2 = XML.toString(jo2,"jo"); - assertTrue("Expected an empty root tag", expected.equals(output2)); + final JSONObject jo1 = new JSONObject(); + jo1.put("array",new Object[]{}); + final JSONObject jo2 = new JSONObject(); + jo2.put("array",new JSONArray()); + + final String expected = ""; + String output1 = XML.toString(jo1,"jo"); + assertTrue("Expected an empty root tag", expected.equals(output1)); + String output2 = XML.toString(jo2,"jo"); + assertTrue("Expected an empty root tag", expected.equals(output2)); } /** @@ -408,16 +408,16 @@ public void shouldHandleEmptyArray(){ */ @Test public void shouldHandleEmptyMultiArray(){ - final JSONObject jo1 = new JSONObject(); - jo1.put("arr",new Object[]{"One", new String[]{}, "Four"}); - final JSONObject jo2 = new JSONObject(); - jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{}), "Four"})); - - final String expected = "OneFour"; - String output1 = XML.toString(jo1,"jo"); - assertTrue("Expected a matching array", expected.equals(output1)); - String output2 = XML.toString(jo2,"jo"); - assertTrue("Expected a matching array", expected.equals(output2)); + final JSONObject jo1 = new JSONObject(); + jo1.put("arr",new Object[]{"One", new String[]{}, "Four"}); + final JSONObject jo2 = new JSONObject(); + jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{}), "Four"})); + + final String expected = "OneFour"; + String output1 = XML.toString(jo1,"jo"); + assertTrue("Expected a matching array", expected.equals(output1)); + String output2 = XML.toString(jo2,"jo"); + assertTrue("Expected a matching array", expected.equals(output2)); } /** @@ -425,16 +425,16 @@ public void shouldHandleEmptyMultiArray(){ */ @Test public void shouldHandleNonEmptyArray(){ - final JSONObject jo1 = new JSONObject(); - jo1.put("arr",new String[]{"One", "Two", "Three"}); - final JSONObject jo2 = new JSONObject(); - jo2.put("arr",new JSONArray(new String[]{"One", "Two", "Three"})); - - final String expected = "OneTwoThree"; - String output1 = XML.toString(jo1,"jo"); - assertTrue("Expected a non empty root tag", expected.equals(output1)); - String output2 = XML.toString(jo2,"jo"); - assertTrue("Expected a non empty root tag", expected.equals(output2)); + final JSONObject jo1 = new JSONObject(); + jo1.put("arr",new String[]{"One", "Two", "Three"}); + final JSONObject jo2 = new JSONObject(); + jo2.put("arr",new JSONArray(new String[]{"One", "Two", "Three"})); + + final String expected = "OneTwoThree"; + String output1 = XML.toString(jo1,"jo"); + assertTrue("Expected a non empty root tag", expected.equals(output1)); + String output2 = XML.toString(jo2,"jo"); + assertTrue("Expected a non empty root tag", expected.equals(output2)); } /** @@ -442,16 +442,16 @@ public void shouldHandleNonEmptyArray(){ */ @Test public void shouldHandleMultiArray(){ - final JSONObject jo1 = new JSONObject(); - jo1.put("arr",new Object[]{"One", new String[]{"Two", "Three"}, "Four"}); - final JSONObject jo2 = new JSONObject(); - jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{"Two", "Three"}), "Four"})); - - final String expected = "OneTwoThreeFour"; - String output1 = XML.toString(jo1,"jo"); - assertTrue("Expected a matching array", expected.equals(output1)); - String output2 = XML.toString(jo2,"jo"); - assertTrue("Expected a matching array", expected.equals(output2)); + final JSONObject jo1 = new JSONObject(); + jo1.put("arr",new Object[]{"One", new String[]{"Two", "Three"}, "Four"}); + final JSONObject jo2 = new JSONObject(); + jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{"Two", "Three"}), "Four"})); + + final String expected = "OneTwoThreeFour"; + String output1 = XML.toString(jo1,"jo"); + assertTrue("Expected a matching array", expected.equals(output1)); + String output2 = XML.toString(jo2,"jo"); + assertTrue("Expected a matching array", expected.equals(output2)); } /** From ace08f19449f1f62fe5aff201e5dc4140613be36 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 3 Jan 2016 21:39:30 -0600 Subject: [PATCH 189/315] latest --- build.gradle | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 build.gradle diff --git a/build.gradle b/build.gradle new file mode 100644 index 000000000..2544a69c7 --- /dev/null +++ b/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'java' +apply plugin: 'eclipse' + +jar.baseName = 'JSON-java' + +sourceSets { + test { + java { + srcDir 'src/test' + } + } +} + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.+' + testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0' + testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5' + // Use this line if you are testing a JSON-Java release. + // Otherwise add an external jar from your local repository in Eclipse + // (The gradle build won't work unless you add a main sourceSets entry and a jar.baseName entry + // testCompile group: 'org.json', name: 'json', version: '20151123' +} From 706d898648e3c747740547e78e71ffe48ad8aed7 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 3 Jan 2016 21:39:44 -0600 Subject: [PATCH 190/315] latest --- CDLTest.java => src/test/org/json/junit/CDLTest.java | 0 .../test/org/json/junit/CookieListTest.java | 0 CookieTest.java => src/test/org/json/junit/CookieTest.java | 0 EnumTest.java => src/test/org/json/junit/EnumTest.java | 0 HTTPTest.java => src/test/org/json/junit/HTTPTest.java | 0 JSONArrayTest.java => src/test/org/json/junit/JSONArrayTest.java | 0 JSONMLTest.java => src/test/org/json/junit/JSONMLTest.java | 0 .../test/org/json/junit/JSONObjectTest.java | 0 .../test/org/json/junit/JSONStringerTest.java | 0 .../test/org/json/junit/JunitTestSuite.java | 0 MyBean.java => src/test/org/json/junit/MyBean.java | 0 .../test/org/json/junit/MyBigNumberBean.java | 0 MyEnum.java => src/test/org/json/junit/MyEnum.java | 0 MyEnumClass.java => src/test/org/json/junit/MyEnumClass.java | 0 MyEnumField.java => src/test/org/json/junit/MyEnumField.java | 0 MyJsonString.java => src/test/org/json/junit/MyJsonString.java | 0 MyPublicClass.java => src/test/org/json/junit/MyPublicClass.java | 0 PropertyTest.java => src/test/org/json/junit/PropertyTest.java | 0 .../test/org/json/junit/StringsResourceBundle.java | 0 TestRunner.java => src/test/org/json/junit/TestRunner.java | 0 Util.java => src/test/org/json/junit/Util.java | 0 XMLTest.java => src/test/org/json/junit/XMLTest.java | 0 22 files changed, 0 insertions(+), 0 deletions(-) rename CDLTest.java => src/test/org/json/junit/CDLTest.java (100%) rename CookieListTest.java => src/test/org/json/junit/CookieListTest.java (100%) rename CookieTest.java => src/test/org/json/junit/CookieTest.java (100%) rename EnumTest.java => src/test/org/json/junit/EnumTest.java (100%) rename HTTPTest.java => src/test/org/json/junit/HTTPTest.java (100%) rename JSONArrayTest.java => src/test/org/json/junit/JSONArrayTest.java (100%) rename JSONMLTest.java => src/test/org/json/junit/JSONMLTest.java (100%) rename JSONObjectTest.java => src/test/org/json/junit/JSONObjectTest.java (100%) rename JSONStringerTest.java => src/test/org/json/junit/JSONStringerTest.java (100%) rename JunitTestSuite.java => src/test/org/json/junit/JunitTestSuite.java (100%) rename MyBean.java => src/test/org/json/junit/MyBean.java (100%) rename MyBigNumberBean.java => src/test/org/json/junit/MyBigNumberBean.java (100%) rename MyEnum.java => src/test/org/json/junit/MyEnum.java (100%) rename MyEnumClass.java => src/test/org/json/junit/MyEnumClass.java (100%) rename MyEnumField.java => src/test/org/json/junit/MyEnumField.java (100%) rename MyJsonString.java => src/test/org/json/junit/MyJsonString.java (100%) rename MyPublicClass.java => src/test/org/json/junit/MyPublicClass.java (100%) rename PropertyTest.java => src/test/org/json/junit/PropertyTest.java (100%) rename StringsResourceBundle.java => src/test/org/json/junit/StringsResourceBundle.java (100%) rename TestRunner.java => src/test/org/json/junit/TestRunner.java (100%) rename Util.java => src/test/org/json/junit/Util.java (100%) rename XMLTest.java => src/test/org/json/junit/XMLTest.java (100%) diff --git a/CDLTest.java b/src/test/org/json/junit/CDLTest.java similarity index 100% rename from CDLTest.java rename to src/test/org/json/junit/CDLTest.java diff --git a/CookieListTest.java b/src/test/org/json/junit/CookieListTest.java similarity index 100% rename from CookieListTest.java rename to src/test/org/json/junit/CookieListTest.java diff --git a/CookieTest.java b/src/test/org/json/junit/CookieTest.java similarity index 100% rename from CookieTest.java rename to src/test/org/json/junit/CookieTest.java diff --git a/EnumTest.java b/src/test/org/json/junit/EnumTest.java similarity index 100% rename from EnumTest.java rename to src/test/org/json/junit/EnumTest.java diff --git a/HTTPTest.java b/src/test/org/json/junit/HTTPTest.java similarity index 100% rename from HTTPTest.java rename to src/test/org/json/junit/HTTPTest.java diff --git a/JSONArrayTest.java b/src/test/org/json/junit/JSONArrayTest.java similarity index 100% rename from JSONArrayTest.java rename to src/test/org/json/junit/JSONArrayTest.java diff --git a/JSONMLTest.java b/src/test/org/json/junit/JSONMLTest.java similarity index 100% rename from JSONMLTest.java rename to src/test/org/json/junit/JSONMLTest.java diff --git a/JSONObjectTest.java b/src/test/org/json/junit/JSONObjectTest.java similarity index 100% rename from JSONObjectTest.java rename to src/test/org/json/junit/JSONObjectTest.java diff --git a/JSONStringerTest.java b/src/test/org/json/junit/JSONStringerTest.java similarity index 100% rename from JSONStringerTest.java rename to src/test/org/json/junit/JSONStringerTest.java diff --git a/JunitTestSuite.java b/src/test/org/json/junit/JunitTestSuite.java similarity index 100% rename from JunitTestSuite.java rename to src/test/org/json/junit/JunitTestSuite.java diff --git a/MyBean.java b/src/test/org/json/junit/MyBean.java similarity index 100% rename from MyBean.java rename to src/test/org/json/junit/MyBean.java diff --git a/MyBigNumberBean.java b/src/test/org/json/junit/MyBigNumberBean.java similarity index 100% rename from MyBigNumberBean.java rename to src/test/org/json/junit/MyBigNumberBean.java diff --git a/MyEnum.java b/src/test/org/json/junit/MyEnum.java similarity index 100% rename from MyEnum.java rename to src/test/org/json/junit/MyEnum.java diff --git a/MyEnumClass.java b/src/test/org/json/junit/MyEnumClass.java similarity index 100% rename from MyEnumClass.java rename to src/test/org/json/junit/MyEnumClass.java diff --git a/MyEnumField.java b/src/test/org/json/junit/MyEnumField.java similarity index 100% rename from MyEnumField.java rename to src/test/org/json/junit/MyEnumField.java diff --git a/MyJsonString.java b/src/test/org/json/junit/MyJsonString.java similarity index 100% rename from MyJsonString.java rename to src/test/org/json/junit/MyJsonString.java diff --git a/MyPublicClass.java b/src/test/org/json/junit/MyPublicClass.java similarity index 100% rename from MyPublicClass.java rename to src/test/org/json/junit/MyPublicClass.java diff --git a/PropertyTest.java b/src/test/org/json/junit/PropertyTest.java similarity index 100% rename from PropertyTest.java rename to src/test/org/json/junit/PropertyTest.java diff --git a/StringsResourceBundle.java b/src/test/org/json/junit/StringsResourceBundle.java similarity index 100% rename from StringsResourceBundle.java rename to src/test/org/json/junit/StringsResourceBundle.java diff --git a/TestRunner.java b/src/test/org/json/junit/TestRunner.java similarity index 100% rename from TestRunner.java rename to src/test/org/json/junit/TestRunner.java diff --git a/Util.java b/src/test/org/json/junit/Util.java similarity index 100% rename from Util.java rename to src/test/org/json/junit/Util.java diff --git a/XMLTest.java b/src/test/org/json/junit/XMLTest.java similarity index 100% rename from XMLTest.java rename to src/test/org/json/junit/XMLTest.java From 67d888e9becd116f1e5e28bd3a572f01e4b1268d Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 27 Jan 2016 11:32:11 -0500 Subject: [PATCH 191/315] Adds test cases to verify that -0 and -0.0 are processed as Double --- .gitignore | 5 + src/test/org/json/junit/JSONObjectTest.java | 3203 ++++++++++--------- 2 files changed, 1694 insertions(+), 1514 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..9e7b59c15 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/bin/ +build +.classpath +.project +.settings/ diff --git a/src/test/org/json/junit/JSONObjectTest.java b/src/test/org/json/junit/JSONObjectTest.java index 994c4b35e..dc7445922 100644 --- a/src/test/org/json/junit/JSONObjectTest.java +++ b/src/test/org/json/junit/JSONObjectTest.java @@ -10,7 +10,14 @@ import java.io.Writer; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; import org.json.CDL; import org.json.JSONArray; @@ -19,24 +26,241 @@ import org.json.XML; import org.junit.Test; -import com.jayway.jsonpath.*; +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.JsonPath; /** - * JSONObject, along with JSONArray, are the central classes of the reference app. - * All of the other classes interact with them, and JSON functionality would - * otherwise be impossible. + * JSONObject, along with JSONArray, are the central classes of the reference + * app. All of the other classes interact with them, and JSON functionality + * would otherwise be impossible. */ public class JSONObjectTest { /** - * JSONObject built from a bean, but only using a null value. - * Nothing good is expected to happen. - * Expects NullPointerException + * Document behaviors of big numbers. Includes both JSONObject and JSONArray + * tests */ - @Test(expected=NullPointerException.class) - public void jsonObjectByNullBean() { - MyBean myBean = null; - new JSONObject(myBean); + @Test + public void bigNumberOperations() { + /** + * JSONObject tries to parse BigInteger as a bean, but it only has one + * getter, getLowestBitSet(). The value is lost and an unhelpful value + * is stored. This should be fixed. + */ + final BigInteger bigInteger = new BigInteger( + "123456789012345678901234567890"); + JSONObject jsonObject = new JSONObject(bigInteger); + Object obj = jsonObject.get("lowestSetBit"); + assertTrue("JSONObject only has 1 value", jsonObject.length() == 1); + assertTrue("JSONObject parses BigInteger as the Integer lowestBitSet", + obj instanceof Integer); + assertTrue("this bigInteger lowestBitSet happens to be 1", + obj.equals(1)); + + /** + * JSONObject tries to parse BigDecimal as a bean, but it has no + * getters, The value is lost and no value is stored. This should be + * fixed. + */ + final BigDecimal bigDecimal = new BigDecimal( + "123456789012345678901234567890.12345678901234567890123456789"); + jsonObject = new JSONObject(bigDecimal); + assertTrue("large bigDecimal is not stored", jsonObject.length() == 0); + + /** + * JSONObject put(String, Object) method stores and serializes bigInt + * and bigDec correctly. Nothing needs to change. + */ + 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 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 serializes bigDec correctly", + jsonObject + .toString() + .equals("{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + + /** + * exercise some exceptions + */ + try { + jsonObject.getBigDecimal("bigInt"); + assertTrue("expected an exeption", false); + } catch (final 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 (final JSONException ignored) { + } + jsonObject.put("stringKey", "abc"); + try { + jsonObject.getBigDecimal("stringKey"); + assertTrue("expected an exeption", false); + } catch (final JSONException ignored) { + } + obj = jsonObject.optBigInteger("bigDec", BigInteger.ONE); + assertTrue("expected BigInteger", obj.equals(BigInteger.ONE)); + + /** + * JSONObject.numberToString() works correctly, nothing to change. + */ + String str = JSONObject.numberToString(bigInteger); + assertTrue("numberToString() handles bigInteger correctly", + str.equals("123456789012345678901234567890")); + str = JSONObject.numberToString(bigDecimal); + assertTrue( + "numberToString() handles bigDecimal correctly", + str.equals("123456789012345678901234567890.12345678901234567890123456789")); + + /** + * JSONObject.stringToValue() turns bigInt into an accurate string, and + * rounds bigDec. This incorrect, but users may have come to expect this + * behavior. Change would be marginally better, but might inconvenience + * users. + */ + obj = JSONObject.stringToValue(bigInteger.toString()); + assertTrue("stringToValue() turns bigInteger string into string", + obj instanceof String); + obj = JSONObject.stringToValue(bigDecimal.toString()); + assertTrue("stringToValue() changes bigDecimal string", !obj.toString() + .equals(bigDecimal.toString())); + + /** + * wrap() vs put() big number behavior is now the same. + */ + // bigInt map ctor + Map map = new HashMap(); + map.put("bigInt", bigInteger); + jsonObject = new JSONObject(map); + String actualFromMapStr = jsonObject.toString(); + assertTrue("bigInt in map (or array or bean) is a string", + actualFromMapStr + .equals("{\"bigInt\":123456789012345678901234567890}")); + // bigInt put + jsonObject = new JSONObject(); + jsonObject.put("bigInt", bigInteger); + String actualFromPutStr = jsonObject.toString(); + assertTrue("bigInt from put is a number", + actualFromPutStr + .equals("{\"bigInt\":123456789012345678901234567890}")); + // bigDec map ctor + map = new HashMap(); + map.put("bigDec", bigDecimal); + jsonObject = new JSONObject(map); + actualFromMapStr = jsonObject.toString(); + assertTrue( + "bigDec in map (or array or bean) is a bigDec", + actualFromMapStr + .equals("{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + // bigDec put + jsonObject = new JSONObject(); + jsonObject.put("bigDec", bigDecimal); + actualFromPutStr = jsonObject.toString(); + assertTrue( + "bigDec from put is a number", + actualFromPutStr + .equals("{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + // bigInt,bigDec put + JSONArray jsonArray = new JSONArray(); + jsonArray.put(bigInteger); + jsonArray.put(bigDecimal); + actualFromPutStr = jsonArray.toString(); + assertTrue( + "bigInt, bigDec from put is a number", + actualFromPutStr + .equals("[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); + assertTrue("getBigInt is bigInt", + jsonArray.getBigInteger(0).equals(bigInteger)); + assertTrue("getBigDec is bigDec", + jsonArray.getBigDecimal(1).equals(bigDecimal)); + assertTrue("optBigInt is bigInt", + jsonArray.optBigInteger(0, BigInteger.ONE).equals(bigInteger)); + assertTrue("optBigDec is bigDec", + jsonArray.optBigDecimal(1, BigDecimal.ONE).equals(bigDecimal)); + jsonArray.put(Boolean.TRUE); + try { + jsonArray.getBigInteger(2); + assertTrue("should not be able to get big int", false); + } catch (final Exception ignored) { + } + try { + jsonArray.getBigDecimal(2); + assertTrue("should not be able to get big dec", false); + } catch (final 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)); + + // bigInt,bigDec list ctor + final List list = new ArrayList(); + list.add(bigInteger); + list.add(bigDecimal); + jsonArray = new JSONArray(list); + final String actualFromListStr = jsonArray.toString(); + assertTrue( + "bigInt, bigDec in list is a bigInt, bigDec", + actualFromListStr + .equals("[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); + // bigInt bean ctor + MyBigNumberBean myBigNumberBean = mock(MyBigNumberBean.class); + when(myBigNumberBean.getBigInteger()).thenReturn( + new BigInteger("123456789012345678901234567890")); + jsonObject = new JSONObject(myBigNumberBean); + String actualFromBeanStr = jsonObject.toString(); + // can't do a full string compare because mockery adds an extra + // key/value + assertTrue("bigInt from bean ctor is a bigInt", + actualFromBeanStr.contains("123456789012345678901234567890")); + // bigDec bean ctor + myBigNumberBean = mock(MyBigNumberBean.class); + when(myBigNumberBean.getBigDecimal()) + .thenReturn( + new BigDecimal( + "123456789012345678901234567890.12345678901234567890123456789")); + jsonObject = new JSONObject(myBigNumberBean); + actualFromBeanStr = jsonObject.toString(); + // can't do a full string compare because mockery adds an extra + // key/value + assertTrue( + "bigDec from bean ctor is a bigDec", + actualFromBeanStr + .contains("123456789012345678901234567890.12345678901234567890123456789")); + // bigInt,bigDec wrap() + obj = JSONObject.wrap(bigInteger); + assertTrue("wrap() returns big num", obj.equals(bigInteger)); + obj = JSONObject.wrap(bigDecimal); + assertTrue("wrap() returns string", obj.equals(bigDecimal)); + } /** @@ -44,222 +268,261 @@ public void jsonObjectByNullBean() { */ @Test public void emptyJsonObject() { - JSONObject jsonObject = new JSONObject(); + final JSONObject jsonObject = new JSONObject(); assertTrue("jsonObject should be empty", jsonObject.length() == 0); } /** - * A JSONObject can be created from another JSONObject plus a list of names. - * In this test, some of the starting JSONObject keys are not in the - * names list. + * Populate a JSONArray from an empty JSONObject names() method. It should + * be empty. */ @Test - public void jsonObjectByNames() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"nullKey\":null,"+ - "\"stringKey\":\"hello world!\","+ - "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ - "\"intKey\":42,"+ - "\"doubleKey\":-23.45e67"+ - "}"; - String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"}; - JSONObject jsonObject = new JSONObject(str); + public void emptyJsonObjectNamesToJsonAray() { + final JSONObject jsonObject = new JSONObject(); + final JSONArray jsonArray = jsonObject.names(); + assertTrue("jsonArray should be null", jsonArray == null); + } - // validate JSON - JSONObject jsonObjectByName = new JSONObject(jsonObject, keys); - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObjectByName.toString()); - assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); - assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"nullKey\":null", null == JsonPath.read(doc, "$.nullKey")); - assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); + /** + * Exercise the JSONObject equals() method + */ + @Test + public void equals() { + final String str = "{\"key\":\"value\"}"; + final JSONObject aJsonObject = new JSONObject(str); + assertTrue("Same JSONObject should be equal to itself", + aJsonObject.equals(aJsonObject)); } /** - * JSONObjects can be built from a Map. - * In this test the map is null. - * the JSONObject(JsonTokener) ctor is not tested directly since it already - * has full coverage from other tests. + * This test documents how JSON-Java handles invalid numeric input. */ @Test - public void jsonObjectByNullMap() { - Map map = null; - JSONObject jsonObject = new JSONObject(map); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + public void jsonInvalidNumberValues() { + // Number-notations supported by Java and invalid as JSON + final String str = "{" + "\"hexNumber\":-0x123," + + "\"tooManyZeros\":00," + "\"negativeInfinite\":-Infinity," + + "\"negativeNaN\":-NaN," + "\"negativeFraction\":-.01," + + "\"tooManyZerosFraction\":00.001," + + "\"negativeHexFloat\":-0x1.fffp1," + + "\"hexFloat\":0x1.0P-1074," + "\"floatIdentifier\":0.1f," + + "\"doubleIdentifier\":0.1d" + "}"; + final JSONObject jsonObject = new JSONObject(str); + Object obj; + obj = jsonObject.get("hexNumber"); + assertFalse( + "hexNumber must not be a number (should throw exception!?)", + obj instanceof Number); + assertTrue("hexNumber currently evaluates to string", + obj.equals("-0x123")); + assertTrue("tooManyZeros currently evaluates to string", jsonObject + .get("tooManyZeros").equals("00")); + obj = jsonObject.get("negativeInfinite"); + assertTrue("negativeInfinite currently evaluates to string", + obj.equals("-Infinity")); + obj = jsonObject.get("negativeNaN"); + assertTrue("negativeNaN currently evaluates to string", + obj.equals("-NaN")); + assertTrue("negativeFraction currently evaluates to double -0.01", + jsonObject.get("negativeFraction").equals(new Double(-0.01))); + assertTrue("tooManyZerosFraction currently evaluates to double 0.001", + jsonObject.get("tooManyZerosFraction") + .equals(new Double(0.001))); + assertTrue( + "negativeHexFloat currently evaluates to double -3.99951171875", + jsonObject.get("negativeHexFloat").equals( + new Double(-3.99951171875))); + assertTrue("hexFloat currently evaluates to double 4.9E-324", + jsonObject.get("hexFloat").equals(new Double(4.9E-324))); + assertTrue("floatIdentifier currently evaluates to double 0.1", + jsonObject.get("floatIdentifier").equals(new Double(0.1))); + assertTrue("doubleIdentifier currently evaluates to double 0.1", + jsonObject.get("doubleIdentifier").equals(new Double(0.1))); } /** - * JSONObjects can be built from a Map. - * In this test all of the map entries are valid JSON types. + * Exercise the JSONObject.accumulate() method */ @Test - public void jsonObjectByMap() { - Map map = new HashMap(); - map.put("trueKey", new Boolean(true)); - map.put("falseKey", new Boolean(false)); - map.put("stringKey", "hello world!"); - map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - map.put("intKey", new Long(42)); - map.put("doubleKey", new Double(-23.45e67)); - JSONObject jsonObject = new JSONObject(map); + public void jsonObjectAccumulate() { + + final JSONObject jsonObject = new JSONObject(); + jsonObject.accumulate("myArray", true); + jsonObject.accumulate("myArray", false); + jsonObject.accumulate("myArray", "hello world!"); + 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 (final JSONException ignored) { + } // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); - assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 1 top level item", + ((Map) JsonPath.read(doc, "$")).size() == 1); + assertTrue("expected 6 myArray items", + ((List) JsonPath.read(doc, "$.myArray")).size() == 6); + assertTrue("expected true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); + assertTrue("expected false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); + assertTrue("expected hello world!", + "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); + assertTrue("expected h\be\tllo w\u1234orld!", + "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, + "$.myArray[3]"))); + assertTrue("expected 42", + Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); + assertTrue( + "expected -23.45e7", + Double.valueOf(-23.45e7).equals( + JsonPath.read(doc, "$.myArray[5]"))); } - + /** - * Verifies that the constructor has backwards compatability with RAW types pre-java5. + * Exercise the JSONObject append() functionality */ @Test - public void verifyConstructor() { - - final JSONObject expected = new JSONObject("{\"myKey\":10}"); - - @SuppressWarnings("rawtypes") - Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); - JSONObject jaRaw = new JSONObject(myRawC); + public void jsonObjectAppend() { + final 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 (final JSONException ignored) { + } - Map myCStrObj = Collections.singletonMap("myKey", - (Object) Integer.valueOf(10)); - JSONObject jaStrObj = new JSONObject(myCStrObj); + // validate JSON + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 1 top level item", + ((Map) JsonPath.read(doc, "$")).size() == 1); + assertTrue("expected 6 myArray items", + ((List) JsonPath.read(doc, "$.myArray")).size() == 6); + assertTrue("expected true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); + assertTrue("expected false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); + assertTrue("expected hello world!", + "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); + assertTrue("expected h\be\tllo w\u1234orld!", + "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, + "$.myArray[3]"))); + assertTrue("expected 42", + Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); + assertTrue( + "expected -23.45e7", + Double.valueOf(-23.45e7).equals( + JsonPath.read(doc, "$.myArray[5]"))); + } - Map myCStrInt = Collections.singletonMap("myKey", - Integer.valueOf(10)); - JSONObject jaStrInt = new JSONObject(myCStrInt); + /** + * JSONObject built from a bean. In this case all but one of the bean + * getters return valid JSON types + */ + @Test + public void jsonObjectByBean() { + /** + * Default access classes have to be mocked since JSONObject, which is + * not in the same package, cannot call MyBean methods by reflection. + */ + final MyBean myBean = mock(MyBean.class); + when(myBean.getDoubleKey()).thenReturn(-23.45e7); + when(myBean.getIntKey()).thenReturn(42); + when(myBean.getStringKey()).thenReturn("hello world!"); + when(myBean.getEscapeStringKey()).thenReturn("h\be\tllo w\u1234orld!"); + when(myBean.isTrueKey()).thenReturn(true); + when(myBean.isFalseKey()).thenReturn(false); + when(myBean.getStringReaderKey()).thenReturn(new StringReader("") { + }); - Map myCObjObj = Collections.singletonMap((Object) "myKey", - (Object) Integer.valueOf(10)); - JSONObject jaObjObj = new JSONObject(myCObjObj); + final JSONObject jsonObject = new JSONObject(myBean); + // validate JSON + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 8 top level items", + ((Map) JsonPath.read(doc, "$")).size() == 8); + assertTrue("expected true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected hello world!", + "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected h\be\tllo w\u1234orld!", + "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, + "$.escapeStringKey"))); + assertTrue("expected 42", + Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrInt)); + "expected -23.45e7", + Double.valueOf("-23.45e7").equals( + JsonPath.read(doc, "$.doubleKey"))); assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObjObj)); + "expected 0 items in stringReaderKey", + ((Map) JsonPath.read(doc, "$.stringReaderKey")).size() == 0); + // sorry, mockito artifact + assertTrue("expected 2 callbacks items", + ((List) JsonPath.read(doc, "$.callbacks")).size() == 2); + assertTrue("expected 0 handler items", ((Map) JsonPath.read(doc, + "$.callbacks[0].handler")).size() == 0); + assertTrue("expected 0 callbacks[1] items", + ((Map) JsonPath.read(doc, "$.callbacks[1]")).size() == 0); } - + /** - * Verifies that the put Collection has backwards compatability with RAW types pre-java5. + * JSONObjects can be built from a Map. In this test all of + * the map entries are valid JSON types. */ @Test - public void verifyPutCollection() { - - final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); + public void jsonObjectByMap() { + final Map map = new HashMap(); + map.put("trueKey", new Boolean(true)); + map.put("falseKey", new Boolean(false)); + map.put("stringKey", "hello world!"); + map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); + map.put("intKey", new Long(42)); + map.put("doubleKey", new Double(-23.45e67)); + final JSONObject jsonObject = new JSONObject(map); - @SuppressWarnings("rawtypes") - Collection myRawC = Collections.singleton(Integer.valueOf(10)); - JSONObject jaRaw = new JSONObject(); - jaRaw.put("myCollection", myRawC); - - Collection myCObj = Collections.singleton((Object) Integer - .valueOf(10)); - JSONObject jaObj = new JSONObject(); - jaObj.put("myCollection", myCObj); - - Collection myCInt = Collections.singleton(Integer - .valueOf(10)); - JSONObject jaInt = new JSONObject(); - jaInt.put("myCollection", myCInt); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaInt)); - } - - - /** - * Verifies that the put Map has backwards compatability with RAW types pre-java5. - */ - @Test - public void verifyPutMap() { - - final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); - - @SuppressWarnings("rawtypes") - Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); - JSONObject jaRaw = new JSONObject(); - jaRaw.put("myMap", myRawC); - - Map myCStrObj = Collections.singletonMap("myKey", - (Object) Integer.valueOf(10)); - JSONObject jaStrObj = new JSONObject(); - jaStrObj.put("myMap", myCStrObj); - - Map myCStrInt = Collections.singletonMap("myKey", - Integer.valueOf(10)); - JSONObject jaStrInt = new JSONObject(); - jaStrInt.put("myMap", myCStrInt); - - Map myCObjObj = Collections.singletonMap((Object) "myKey", - (Object) Integer.valueOf(10)); - JSONObject jaObjObj = new JSONObject(); - jaObjObj.put("myMap", myCObjObj); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrInt)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObjObj)); - } - - - /** - * JSONObjects can be built from a Map. - * In this test the map entries are not valid JSON types. - * The actual conversion is kind of interesting. - */ - @Test - public void jsonObjectByMapWithUnsupportedValues() { - Map jsonMap = new HashMap(); - // Just insert some random objects - jsonMap.put("key1", new CDL()); - jsonMap.put("key2", new Exception()); - - JSONObject jsonObject = new JSONObject(jsonMap); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(JsonPath.read(doc, "$.key2"))); - assertTrue("expected 0 key1 items", ((Map)(JsonPath.read(doc, "$.key1"))).size() == 0); - } + // validate JSON + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 6 top level items", + ((Map) JsonPath.read(doc, "$")).size() == 6); + assertTrue("expected \"trueKey\":true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected \"falseKey\":false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", + "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", + "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, + "$.escapeStringKey"))); + assertTrue( + "expected \"doubleKey\":-23.45e67", + Double.valueOf("-23.45e67").equals( + JsonPath.read(doc, "$.doubleKey"))); + } /** - * JSONObjects can be built from a Map. - * In this test one of the map values is null + * JSONObjects can be built from a Map. In this test one of + * the map values is null */ @Test public void jsonObjectByMapWithNullValue() { - Map map = new HashMap(); + final Map map = new HashMap(); map.put("trueKey", new Boolean(true)); map.put("falseKey", new Boolean(false)); map.put("stringKey", "hello world!"); @@ -267,718 +530,332 @@ public void jsonObjectByMapWithNullValue() { map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); map.put("intKey", new Long(42)); map.put("doubleKey", new Double(-23.45e67)); - JSONObject jsonObject = new JSONObject(map); + final JSONObject jsonObject = new JSONObject(map); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); - assertTrue("expected \"intKey\":42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); - assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 6 top level items", + ((Map) JsonPath.read(doc, "$")).size() == 6); + assertTrue("expected \"trueKey\":true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected \"falseKey\":false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", + "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", + "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, + "$.escapeStringKey"))); + assertTrue("expected \"intKey\":42", + Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); + assertTrue( + "expected \"doubleKey\":-23.45e67", + Double.valueOf("-23.45e67").equals( + JsonPath.read(doc, "$.doubleKey"))); } /** - * JSONObject built from a bean. In this case all but one of the - * bean getters return valid JSON types + * JSONObjects can be built from a Map. In this test the map + * entries are not valid JSON types. The actual conversion is kind of + * interesting. */ @Test - public void jsonObjectByBean() { - /** - * Default access classes have to be mocked since JSONObject, which is - * not in the same package, cannot call MyBean methods by reflection. - */ - MyBean myBean = mock(MyBean.class); - when(myBean.getDoubleKey()).thenReturn(-23.45e7); - when(myBean.getIntKey()).thenReturn(42); - when(myBean.getStringKey()).thenReturn("hello world!"); - when(myBean.getEscapeStringKey()).thenReturn("h\be\tllo w\u1234orld!"); - when(myBean.isTrueKey()).thenReturn(true); - when(myBean.isFalseKey()).thenReturn(false); - when(myBean.getStringReaderKey()).thenReturn( - new StringReader("") { - }); + public void jsonObjectByMapWithUnsupportedValues() { + final Map jsonMap = new HashMap(); + // Just insert some random objects + jsonMap.put("key1", new CDL()); + jsonMap.put("key2", new Exception()); - JSONObject jsonObject = new JSONObject(myBean); + final JSONObject jsonObject = new JSONObject(jsonMap); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 8 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 8); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected hello world!","hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); - assertTrue("expected 42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); - assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(JsonPath.read(doc, "$.doubleKey"))); - assertTrue("expected 0 items in stringReaderKey", ((Map) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0); - // sorry, mockito artifact - assertTrue("expected 2 callbacks items", ((List)(JsonPath.read(doc, "$.callbacks"))).size() == 2); - assertTrue("expected 0 handler items", ((Map)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0); - assertTrue("expected 0 callbacks[1] items", ((Map)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0); + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 2 top level items", + ((Map) JsonPath.read(doc, "$")).size() == 2); + assertTrue("expected \"key2\":java.lang.Exception", + "java.lang.Exception".equals(JsonPath.read(doc, "$.key2"))); + assertTrue("expected 0 key1 items", + ((Map) JsonPath.read(doc, "$.key1")).size() == 0); } /** - * A bean is also an object. But in order to test the JSONObject - * ctor that takes an object and a list of names, - * this particular bean needs some public - * data members, which have been added to the class. + * A JSONObject can be created from another JSONObject plus a list of names. + * In this test, some of the starting JSONObject keys are not in the names + * list. */ @Test - public void jsonObjectByObjectAndNames() { - String[] keys = {"publicString", "publicInt"}; - // just need a class that has public data members - MyPublicClass myPublicClass = new MyPublicClass(); - JSONObject jsonObject = new JSONObject(myPublicClass, keys); + public void jsonObjectByNames() { + final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," + + "\"nullKey\":null," + "\"stringKey\":\"hello world!\"," + + "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\"," + + "\"intKey\":42," + "\"doubleKey\":-23.45e67" + "}"; + final String[] keys = { "falseKey", "stringKey", "nullKey", "doubleKey" }; + final JSONObject jsonObject = new JSONObject(str); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected \"publicString\":\"abc\"", "abc".equals(JsonPath.read(doc, "$.publicString"))); - assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.publicInt"))); + final JSONObject jsonObjectByName = new JSONObject(jsonObject, keys); + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObjectByName.toString()); + assertTrue("expected 4 top level items", + ((Map) JsonPath.read(doc, "$")).size() == 4); + assertTrue("expected \"falseKey\":false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"nullKey\":null", + null == JsonPath.read(doc, "$.nullKey")); + assertTrue("expected \"stringKey\":\"hello world!\"", + "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue( + "expected \"doubleKey\":-23.45e67", + Double.valueOf("-23.45e67").equals( + JsonPath.read(doc, "$.doubleKey"))); } /** - * Exercise the JSONObject from resource bundle functionality. - * The test resource bundle is uncomplicated, but provides adequate test coverage. + * JSONObject built from a bean, but only using a null value. Nothing good + * is expected to happen. Expects NullPointerException */ - @Test - public void jsonObjectByResourceBundle() { - JSONObject jsonObject = new - JSONObject("org.json.junit.StringsResourceBundle", - Locale.getDefault()); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected 2 greetings items", ((Map)(JsonPath.read(doc, "$.greetings"))).size() == 2); - assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(JsonPath.read(doc, "$.greetings.hello"))); - assertTrue("expected \"world\":\"World!\"", "World!".equals(JsonPath.read(doc, "$.greetings.world"))); - assertTrue("expected 2 farewells items", ((Map)(JsonPath.read(doc, "$.farewells"))).size() == 2); - assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(JsonPath.read(doc, "$.farewells.later"))); - assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(JsonPath.read(doc, "$.farewells.gator"))); + @Test(expected = NullPointerException.class) + public void jsonObjectByNullBean() { + final MyBean myBean = null; + new JSONObject(myBean); } /** - * Exercise the JSONObject.accumulate() method + * JSONObjects can be built from a Map. In this test the map + * is null. the JSONObject(JsonTokener) ctor is not tested directly since it + * already has full coverage from other tests. */ @Test - public void jsonObjectAccumulate() { - - JSONObject jsonObject = new JSONObject(); - jsonObject.accumulate("myArray", true); - jsonObject.accumulate("myArray", false); - jsonObject.accumulate("myArray", "hello world!"); - 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()); - assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); - assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]"))); - assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); - assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]"))); + public void jsonObjectByNullMap() { + final Map map = null; + final JSONObject jsonObject = new JSONObject(map); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); } /** - * Exercise the JSONObject append() functionality + * A bean is also an object. But in order to test the JSONObject ctor that + * takes an object and a list of names, this particular bean needs some + * public data members, which have been added to the class. */ @Test - public void jsonObjectAppend() { - 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) {} + public void jsonObjectByObjectAndNames() { + final String[] keys = { "publicString", "publicInt" }; + // just need a class that has public data members + final MyPublicClass myPublicClass = new MyPublicClass(); + final JSONObject jsonObject = new JSONObject(myPublicClass, keys); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); - assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]"))); - assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); - assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]"))); + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 2 top level items", + ((Map) JsonPath.read(doc, "$")).size() == 2); + assertTrue("expected \"publicString\":\"abc\"", + "abc".equals(JsonPath.read(doc, "$.publicString"))); + assertTrue("expected \"publicInt\":42", + Integer.valueOf(42).equals(JsonPath.read(doc, "$.publicInt"))); } /** - * Exercise the JSONObject doubleToString() method + * Exercise the JSONObject from resource bundle functionality. The test + * resource bundle is uncomplicated, but provides adequate test coverage. */ @Test - public void jsonObjectDoubleToString() { - String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" }; - Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, - Double.NaN, Double.NEGATIVE_INFINITY }; - for (int i = 0; i < expectedStrs.length; ++i) { - String actualStr = JSONObject.doubleToString(doubles[i]); - assertTrue("value expected ["+expectedStrs[i]+ - "] found ["+actualStr+ "]", - expectedStrs[i].equals(actualStr)); - } - } - - /** - * Exercise some JSONObject get[type] and opt[type] methods - */ - @Test - public void jsonObjectValues() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"trueStrKey\":\"true\","+ - "\"falseStrKey\":\"false\","+ - "\"stringKey\":\"hello world!\","+ - "\"intKey\":42,"+ - "\"intStrKey\":\"43\","+ - "\"longKey\":1234567890123456789,"+ - "\"longStrKey\":\"987654321098765432\","+ - "\"doubleKey\":-23.45e7,"+ - "\"doubleStrKey\":\"00001.000\","+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{\"myKey\":\"myVal\"}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); - assertTrue("opt trueKey should be true", jsonObject.optBoolean("trueKey")); - assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey")); - assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey")); - assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey")); - assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey")); - assertTrue("stringKey should be string", - jsonObject.getString("stringKey").equals("hello world!")); - assertTrue("doubleKey should be double", - jsonObject.getDouble("doubleKey") == -23.45e7); - assertTrue("doubleStrKey should be double", - jsonObject.getDouble("doubleStrKey") == 1); - 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("intKey should be int", - jsonObject.optInt("intKey") == 42); - assertTrue("opt intKey should be int", - jsonObject.optInt("intKey", 0) == 42); - assertTrue("opt intKey with default should be int", - jsonObject.getInt("intKey") == 42); - assertTrue("intStrKey should be int", - jsonObject.getInt("intStrKey") == 43); - assertTrue("longKey should be long", - jsonObject.getLong("longKey") == 1234567890123456789L); - assertTrue("opt longKey should be long", - jsonObject.optLong("longKey") == 1234567890123456789L); - assertTrue("opt longKey with default should be long", - jsonObject.optLong("longKey", 0) == 1234567890123456789L); - assertTrue("longStrKey should be long", - jsonObject.getLong("longStrKey") == 987654321098765432L); - assertTrue("xKey should not exist", - jsonObject.isNull("xKey")); - assertTrue("stringKey should exist", - jsonObject.has("stringKey")); - assertTrue("opt stringKey should string", - jsonObject.optString("stringKey").equals("hello world!")); - assertTrue("opt stringKey with default should string", - jsonObject.optString("stringKey", "not found").equals("hello world!")); - JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); - assertTrue("arrayKey should be JSONArray", - jsonArray.getInt(0) == 0 && - jsonArray.getInt(1) == 1 && - jsonArray.getInt(2) == 2); - jsonArray = jsonObject.optJSONArray("arrayKey"); - assertTrue("opt arrayKey should be JSONArray", - jsonArray.getInt(0) == 0 && - jsonArray.getInt(1) == 1 && - jsonArray.getInt(2) == 2); - JSONObject jsonObjectInner = jsonObject.getJSONObject("objectKey"); - assertTrue("objectKey should be JSONObject", - jsonObjectInner.get("myKey").equals("myVal")); - } - - /** - * Check whether JSONObject handles large or high precision numbers correctly - */ - @Test - public void stringToValueNumbersTest() { - - assertTrue( "0.2 should be a Double!", - JSONObject.stringToValue( "0.2" ) instanceof Double ); - assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!", - JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double ); - /** - * This test documents a need for BigDecimal conversion. - */ - Object obj = JSONObject.stringToValue( "299792.457999999984" ); - assertTrue( "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!", - obj.equals(new Double(299792.458)) ); - assertTrue( "1 should be an Integer!", - JSONObject.stringToValue( "1" ) instanceof Integer ); - assertTrue( "Integer.MAX_VALUE should still be an Integer!", - JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer ); - assertTrue( "Large integers should be a Long!", - JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long ); - assertTrue( "Long.MAX_VALUE should still be an Integer!", - JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long ); - - String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString(); - assertTrue( "Really large integers currently evaluate to string", - JSONObject.stringToValue(str).equals("9223372036854775808")); - } - - /** - * This test documents numeric values which could be numerically - * handled as BigDecimal or BigInteger. It helps determine what outputs - * will change if those types are supported. - */ - @Test - public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() { - // Valid JSON Numbers, probably should return BigDecimal or BigInteger objects - String str = - "{"+ - "\"numberWithDecimals\":299792.457999999984,"+ - "\"largeNumber\":12345678901234567890,"+ - "\"preciseNumber\":0.2000000000000000111,"+ - "\"largeExponent\":-23.45e2327"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - // Comes back as a double, but loses precision - assertTrue( "numberWithDecimals currently evaluates to double 299792.458", - jsonObject.get( "numberWithDecimals" ).equals( new Double( "299792.458" ) ) ); - Object obj = jsonObject.get( "largeNumber" ); - assertTrue("largeNumber currently evaluates to string", - "12345678901234567890".equals(obj)); - // comes back as a double but loses precision - assertTrue( "preciseNumber currently evaluates to double 0.2", - jsonObject.get( "preciseNumber" ).equals(new Double(0.2))); - obj = jsonObject.get( "largeExponent" ); - assertTrue("largeExponent should currently evaluates as a string", - "-23.45e2327".equals(obj)); - } + public void jsonObjectByResourceBundle() { + final JSONObject jsonObject = new JSONObject( + "org.json.junit.StringsResourceBundle", Locale.getDefault()); - /** - * This test documents how JSON-Java handles invalid numeric input. - */ - @Test - public void jsonInvalidNumberValues() { - // Number-notations supported by Java and invalid as JSON - String str = - "{"+ - "\"hexNumber\":-0x123,"+ - "\"tooManyZeros\":00,"+ - "\"negativeInfinite\":-Infinity,"+ - "\"negativeNaN\":-NaN,"+ - "\"negativeFraction\":-.01,"+ - "\"tooManyZerosFraction\":00.001,"+ - "\"negativeHexFloat\":-0x1.fffp1,"+ - "\"hexFloat\":0x1.0P-1074,"+ - "\"floatIdentifier\":0.1f,"+ - "\"doubleIdentifier\":0.1d"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - Object obj; - obj = jsonObject.get( "hexNumber" ); - assertFalse( "hexNumber must not be a number (should throw exception!?)", - obj instanceof Number ); - assertTrue("hexNumber currently evaluates to string", - obj.equals("-0x123")); - assertTrue( "tooManyZeros currently evaluates to string", - jsonObject.get( "tooManyZeros" ).equals("00")); - obj = jsonObject.get("negativeInfinite"); - assertTrue( "negativeInfinite currently evaluates to string", - obj.equals("-Infinity")); - obj = jsonObject.get("negativeNaN"); - assertTrue( "negativeNaN currently evaluates to string", - obj.equals("-NaN")); - assertTrue( "negativeFraction currently evaluates to double -0.01", - jsonObject.get( "negativeFraction" ).equals(new Double(-0.01))); - assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", - jsonObject.get( "tooManyZerosFraction" ).equals(new Double(0.001))); - assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", - jsonObject.get( "negativeHexFloat" ).equals(new Double(-3.99951171875))); - assertTrue("hexFloat currently evaluates to double 4.9E-324", - jsonObject.get("hexFloat").equals(new Double(4.9E-324))); - assertTrue("floatIdentifier currently evaluates to double 0.1", - jsonObject.get("floatIdentifier").equals(new Double(0.1))); - assertTrue("doubleIdentifier currently evaluates to double 0.1", - jsonObject.get("doubleIdentifier").equals(new Double(0.1))); + // validate JSON + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 2 top level items", + ((Map) JsonPath.read(doc, "$")).size() == 2); + assertTrue("expected 2 greetings items", + ((Map) JsonPath.read(doc, "$.greetings")).size() == 2); + assertTrue("expected \"hello\":\"Hello, \"", + "Hello, ".equals(JsonPath.read(doc, "$.greetings.hello"))); + assertTrue("expected \"world\":\"World!\"", + "World!".equals(JsonPath.read(doc, "$.greetings.world"))); + assertTrue("expected 2 farewells items", + ((Map) JsonPath.read(doc, "$.farewells")).size() == 2); + assertTrue("expected \"later\":\"Later, \"", + "Later, ".equals(JsonPath.read(doc, "$.farewells.later"))); + assertTrue("expected \"world\":\"World!\"", + "Alligator!".equals(JsonPath.read(doc, "$.farewells.gator"))); } /** - * Tests how JSONObject get[type] handles incorrect types + * Exercise the JSONObject doubleToString() method */ @Test - public void jsonObjectNonAndWrongValues() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"trueStrKey\":\"true\","+ - "\"falseStrKey\":\"false\","+ - "\"stringKey\":\"hello world!\","+ - "\"intKey\":42,"+ - "\"intStrKey\":\"43\","+ - "\"longKey\":1234567890123456789,"+ - "\"longStrKey\":\"987654321098765432\","+ - "\"doubleKey\":-23.45e7,"+ - "\"doubleStrKey\":\"00001.000\","+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{\"myKey\":\"myVal\"}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - try { - jsonObject.getBoolean("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("expecting an exception message", - "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); - } - try { - jsonObject.getBoolean("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a Boolean.". - equals(e.getMessage())); - } - try { - jsonObject.getString("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getString("trueKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"trueKey\"] not a string.". - equals(e.getMessage())); - } - try { - jsonObject.getDouble("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getDouble("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.". - equals(e.getMessage())); - } - try { - jsonObject.getInt("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getInt("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not an int.". - equals(e.getMessage())); - } - try { - jsonObject.getLong("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getLong("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a long.". - equals(e.getMessage())); - } - try { - jsonObject.getJSONArray("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getJSONArray("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONArray.". - equals(e.getMessage())); - } - try { - jsonObject.getJSONObject("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getJSONObject("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONObject.". - equals(e.getMessage())); + public void jsonObjectDoubleToString() { + final String[] expectedStrs = { "1", "1", "-23.4", "-2.345E68", "null", + "null" }; + final Double[] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, + Double.NaN, Double.NEGATIVE_INFINITY }; + for (int i = 0; i < expectedStrs.length; ++i) { + final String actualStr = JSONObject.doubleToString(doubles[i]); + assertTrue("value expected [" + expectedStrs[i] + "] found [" + + actualStr + "]", expectedStrs[i].equals(actualStr)); } } /** - * This test documents an unexpected numeric behavior. - * A double that ends with .0 is parsed, serialized, then - * parsed again. On the second parse, it has become an int. - */ - @Test - public void unexpectedDoubleToIntConversion() { - String key30 = "key30"; - String key31 = "key31"; - JSONObject jsonObject = new JSONObject(); - jsonObject.put(key30, new Double(3.0)); - jsonObject.put(key31, new Double(3.1)); - - assertTrue("3.0 should remain a double", - jsonObject.getDouble(key30) == 3); - assertTrue("3.1 should remain a double", - jsonObject.getDouble(key31) == 3.1); - - // turns 3.0 into 3. - String serializedString = jsonObject.toString(); - JSONObject deserialized = new JSONObject(serializedString); - assertTrue("3.0 is now an int", deserialized.get(key30) instanceof Integer); - assertTrue("3.0 can still be interpreted as a double", - deserialized.getDouble(key30) == 3.0); - assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1); - } - - /** - * Document behaviors of big numbers. Includes both JSONObject - * and JSONArray tests + * Exercise the JSONObject increment() method. */ @Test - public void bigNumberOperations() { - /** - * JSONObject tries to parse BigInteger as a bean, but it only has - * one getter, getLowestBitSet(). The value is lost and an unhelpful - * value is stored. This should be fixed. - */ - BigInteger bigInteger = new BigInteger("123456789012345678901234567890"); - JSONObject jsonObject = new JSONObject(bigInteger); - Object obj = jsonObject.get("lowestSetBit"); - assertTrue("JSONObject only has 1 value", jsonObject.length() == 1); - assertTrue("JSONObject parses BigInteger as the Integer lowestBitSet", - obj instanceof Integer); - assertTrue("this bigInteger lowestBitSet happens to be 1", - obj.equals(1)); - - /** - * JSONObject tries to parse BigDecimal as a bean, but it has - * no getters, The value is lost and no value is stored. - * This should be fixed. - */ - BigDecimal bigDecimal = new BigDecimal( - "123456789012345678901234567890.12345678901234567890123456789"); - jsonObject = new JSONObject(bigDecimal); - assertTrue("large bigDecimal is not stored", jsonObject.length() == 0); - - /** - * JSONObject put(String, Object) method stores and serializes - * bigInt and bigDec correctly. Nothing needs to change. - */ - 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 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 serializes bigDec correctly", - jsonObject.toString().equals( - "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); - + public void jsonObjectIncrement() { + final String str = "{" + "\"keyLong\":9999999991," + + "\"keyDouble\":1.1" + "}"; + final JSONObject jsonObject = new JSONObject(str); + jsonObject.increment("keyInt"); + jsonObject.increment("keyInt"); + jsonObject.increment("keyLong"); + jsonObject.increment("keyDouble"); + jsonObject.increment("keyInt"); + jsonObject.increment("keyLong"); + jsonObject.increment("keyDouble"); /** - * exercise some exceptions + * JSONObject constructor won't handle these types correctly, but adding + * them via put works. */ - 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.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"); - /** - * JSONObject.numberToString() works correctly, nothing to change. - */ - String str = JSONObject.numberToString(bigInteger); - assertTrue("numberToString() handles bigInteger correctly", - str.equals("123456789012345678901234567890")); - str = JSONObject.numberToString(bigDecimal); - assertTrue("numberToString() handles bigDecimal correctly", - str.equals("123456789012345678901234567890.12345678901234567890123456789")); + // validate JSON + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + 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"))); /** - * JSONObject.stringToValue() turns bigInt into an accurate string, - * and rounds bigDec. This incorrect, but users may have come to - * expect this behavior. Change would be marginally better, but - * might inconvenience users. + * Should work the same way on any platform! @see https://docs.oracle + * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is the + * effect of a float to double conversion and is inherent to the + * shortcomings of the IEEE 754 format, when converting 32-bit into + * double-precision 64-bit. Java type-casts float to double. A 32 bit + * float is type-casted to 64 bit double by simply appending zero-bits + * to the mantissa (and extended the signed exponent by 3 bits.) and + * there is no way to obtain more information than it is stored in the + * 32-bits float. + * + * Like 1/3 cannot be represented as base10 number because it is + * periodically, 1/5 (for example) cannot be represented as base2 number + * since it is periodically in base2 (take a look at + * http://www.h-schmidt.net/FloatConverter/) The same happens to 3.1, + * that decimal number (base10 representation) is periodic in base2 + * representation, therefore appending zero-bits is inaccurate. Only + * repeating the periodically occuring bits (0110) would be a proper + * conversion. However one cannot detect from a 32 bit IEE754 + * representation which bits would "repeat infinitely", since the + * missing bits would not fit into the 32 bit float, i.e. the + * information needed simply is not there! */ - obj = JSONObject.stringToValue(bigInteger.toString()); - assertTrue("stringToValue() turns bigInteger string into string", - obj instanceof String); - obj = JSONObject.stringToValue(bigDecimal.toString()); - assertTrue("stringToValue() changes bigDecimal string", - !obj.toString().equals(bigDecimal.toString())); + assertTrue( + "expected 3.0999999046325684", + Double.valueOf(3.0999999046325684).equals( + JsonPath.read(doc, "$.keyFloat"))); /** - * wrap() vs put() big number behavior is now the same. - */ - // bigInt map ctor - Map map = new HashMap(); - map.put("bigInt", bigInteger); - jsonObject = new JSONObject(map); - String actualFromMapStr = jsonObject.toString(); - assertTrue("bigInt in map (or array or bean) is a string", - actualFromMapStr.equals( - "{\"bigInt\":123456789012345678901234567890}")); - // bigInt put - jsonObject = new JSONObject(); - jsonObject.put("bigInt", bigInteger); - String actualFromPutStr = jsonObject.toString(); - assertTrue("bigInt from put is a number", - actualFromPutStr.equals( - "{\"bigInt\":123456789012345678901234567890}")); - // bigDec map ctor - map = new HashMap(); - map.put("bigDec", bigDecimal); - jsonObject = new JSONObject(map); - actualFromMapStr = jsonObject.toString(); - assertTrue("bigDec in map (or array or bean) is a bigDec", - actualFromMapStr.equals( - "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); - // bigDec put - jsonObject = new JSONObject(); - jsonObject.put("bigDec", bigDecimal); - actualFromPutStr = jsonObject.toString(); - assertTrue("bigDec from put is a number", - actualFromPutStr.equals( - "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); - // bigInt,bigDec put - JSONArray jsonArray = new JSONArray(); - jsonArray.put(bigInteger); - jsonArray.put(bigDecimal); - actualFromPutStr = jsonArray.toString(); - assertTrue("bigInt, bigDec from put is a number", - actualFromPutStr.equals( - "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); - assertTrue("getBigInt is bigInt", jsonArray.getBigInteger(0).equals(bigInteger)); - assertTrue("getBigDec is bigDec", jsonArray.getBigDecimal(1).equals(bigDecimal)); - assertTrue("optBigInt is bigInt", jsonArray.optBigInteger(0, BigInteger.ONE).equals(bigInteger)); - assertTrue("optBigDec is bigDec", jsonArray.optBigDecimal(1, BigDecimal.ONE).equals(bigDecimal)); - jsonArray.put(Boolean.TRUE); - try { - jsonArray.getBigInteger(2); - assertTrue("should not be able to get big int", false); - } catch (Exception ignored) {} - try { - jsonArray.getBigDecimal(2); - assertTrue("should not be able to get big dec", false); - } 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)); - - // bigInt,bigDec list ctor - List list = new ArrayList(); - list.add(bigInteger); - list.add(bigDecimal); - jsonArray = new JSONArray(list); - String actualFromListStr = jsonArray.toString(); - assertTrue("bigInt, bigDec in list is a bigInt, bigDec", - actualFromListStr.equals( - "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); - // bigInt bean ctor - MyBigNumberBean myBigNumberBean = mock(MyBigNumberBean.class); - when(myBigNumberBean.getBigInteger()).thenReturn(new BigInteger("123456789012345678901234567890")); - jsonObject = new JSONObject(myBigNumberBean); - String actualFromBeanStr = jsonObject.toString(); - // can't do a full string compare because mockery adds an extra key/value - assertTrue("bigInt from bean ctor is a bigInt", - actualFromBeanStr.contains("123456789012345678901234567890")); - // bigDec bean ctor - myBigNumberBean = mock(MyBigNumberBean.class); - when(myBigNumberBean.getBigDecimal()).thenReturn(new BigDecimal("123456789012345678901234567890.12345678901234567890123456789")); - jsonObject = new JSONObject(myBigNumberBean); - actualFromBeanStr = jsonObject.toString(); - // can't do a full string compare because mockery adds an extra key/value - assertTrue("bigDec from bean ctor is a bigDec", - actualFromBeanStr.contains("123456789012345678901234567890.12345678901234567890123456789")); - // bigInt,bigDec wrap() - obj = JSONObject.wrap(bigInteger); - assertTrue("wrap() returns big num",obj.equals(bigInteger)); - obj = JSONObject.wrap(bigDecimal); - assertTrue("wrap() returns string",obj.equals(bigDecimal)); + * float f = 3.1f; double df = (double) f; double d = 3.1d; + * System.out.println + * (Integer.toBinaryString(Float.floatToRawIntBits(f))); + * System.out.println + * (Long.toBinaryString(Double.doubleToRawLongBits(df))); + * System.out.println + * (Long.toBinaryString(Double.doubleToRawLongBits(d))); + * + * - Float: seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm + * 1000000010001100110011001100110 - Double + * seeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm + * 10000000 10001100110011001100110 + * 100000000001000110011001100110011000000000000000000000000000000 + * 100000000001000110011001100110011001100110011001100110011001101 + */ + + /** + * Examples of well documented but probably unexpected behavior in java + * / with 32-bit float to 64-bit float conversion. + */ + assertFalse( + "Document unexpected behaviour with explicit type-casting float as double!", + 0.2f == 0.2d); + assertFalse("Document unexpected behaviour with implicit type-cast!", + 0.2f == 0.2d); + final Double d1 = new Double(1.1f); + final Double d2 = new Double("1.1f"); + assertFalse( + "Document implicit type cast from float to double before calling Double(double d) constructor", + d1.equals(d2)); + + assertTrue( + "Correctly converting float to double via base10 (string) representation!", + new Double(3.1d).equals(new Double(new Float(3.1f).toString()))); + + // Pinpointing the not so obvious "buggy" conversion from float to + // double in JSONObject + final JSONObject jo = new JSONObject(); + jo.put("bug", 3.1f); // will call put( String key, double value ) with + // implicit and "buggy" type-cast from float to + // double + assertFalse( + "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", + jo.get("bug").equals(new Double(3.1d))); + + final JSONObject inc = new JSONObject(); + inc.put("bug", new Float(3.1f)); // This will put in instance of Float + // into JSONObject, i.e. call put( + // String key, Object value ) + assertTrue("Everything is ok here!", inc.get("bug") instanceof Float); + inc.increment("bug"); // after adding 1, increment will call put( String + // key, double value ) with implicit and "buggy" + // type-cast from float to double! + // this.put(key, (Float) value + 1); + // 1. The (Object)value will be typecasted to (Float)value since it is + // an instanceof Float actually nothing is done. + // 2. Float instance will be autoboxed into float because the + operator + // will work on primitives not Objects! + // 3. A float+float operation will be performed and results into a float + // primitive. + // 4. There is no method that matches the signature put( String key, + // float value), java-compiler will choose the method + // put( String key, double value) and does an implicit type-cast(!) by + // appending zero-bits to the mantissa + assertTrue("JSONObject increment converts Float to Double", + jo.get("bug") instanceof Double); + // correct implementation (with change of behavior) would be: + // this.put(key, new Float((Float) value + 1)); + // Probably it would be better to deprecate the method and remove some + // day, while convenient processing the "payload" is not + // really in the the scope of a JSON-library (IMHO.) } /** - * The purpose for the static method getNames() methods are not clear. - * This method is not called from within JSON-Java. Most likely - * uses are to prep names arrays for: - * JSONObject(JSONObject jo, String[] names) + * The purpose for the static method getNames() methods are not clear. This + * method is not called from within JSON-Java. Most likely uses are to prep + * names arrays for: JSONObject(JSONObject jo, String[] names) * JSONObject(Object object, String names[]), */ @Test @@ -986,32 +863,27 @@ public void jsonObjectNames() { JSONObject jsonObject; // getNames() from null JSONObject - assertTrue("null names from null Object", - null == JSONObject.getNames((Object)null)); + assertTrue("null names from null Object", + null == JSONObject.getNames((Object) null)); // getNames() from object with no fields - assertTrue("null names from Object with no fields", + assertTrue("null names from Object with no fields", null == JSONObject.getNames(new MyJsonString())); // getNames from new JSONOjbect jsonObject = new JSONObject(); - String [] names = JSONObject.getNames(jsonObject); + String[] names = JSONObject.getNames(jsonObject); assertTrue("names should be null", names == null); - // getNames() from empty JSONObject - String emptyStr = "{}"; + final String emptyStr = "{}"; jsonObject = new JSONObject(emptyStr); assertTrue("empty JSONObject should have null names", null == JSONObject.getNames(jsonObject)); // getNames() from JSONObject - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "}"; + final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," + + "\"stringKey\":\"hello world!\"," + "}"; jsonObject = new JSONObject(str); names = JSONObject.getNames(jsonObject); JSONArray jsonArray = new JSONArray(names); @@ -1032,10 +904,10 @@ public void jsonObjectNames() { ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); /** - * getNames() from an enum with properties has an interesting result. - * It returns the enum values, not the selected enum properties + * getNames() from an enum with properties has an interesting result. It + * returns the enum values, not the selected enum properties */ - MyEnumField myEnumField = MyEnumField.VAL1; + final MyEnumField myEnumField = MyEnumField.VAL1; names = JSONObject.getNames(myEnumField); // validate JSON @@ -1044,22 +916,18 @@ public void jsonObjectNames() { .parse(jsonArray.toString()); docList = JsonPath.read(doc, "$"); assertTrue("expected 3 items", docList.size() == 3); - assertTrue( - "expected to find VAL1", + assertTrue("expected to find VAL1", ((List) JsonPath.read(doc, "$[?(@=='VAL1')]")).size() == 1); - assertTrue( - "expected to find VAL2", + assertTrue("expected to find VAL2", ((List) JsonPath.read(doc, "$[?(@=='VAL2')]")).size() == 1); - assertTrue( - "expected to find VAL3", + assertTrue("expected to find VAL3", ((List) JsonPath.read(doc, "$[?(@=='VAL3')]")).size() == 1); /** - * A bean is also an object. But in order to test the static - * method getNames(), this particular bean needs some public - * data members. + * A bean is also an object. But in order to test the static method + * getNames(), this particular bean needs some public data members. */ - MyPublicClass myPublicClass = new MyPublicClass(); + final MyPublicClass myPublicClass = new MyPublicClass(); names = JSONObject.getNames(myPublicClass); // validate JSON @@ -1068,165 +936,242 @@ public void jsonObjectNames() { .parse(jsonArray.toString()); docList = JsonPath.read(doc, "$"); assertTrue("expected 2 items", docList.size() == 2); - assertTrue( - "expected to find publicString", - ((List) JsonPath.read(doc, "$[?(@=='publicString')]")).size() == 1); + assertTrue("expected to find publicString", ((List) JsonPath.read( + doc, "$[?(@=='publicString')]")).size() == 1); assertTrue( "expected to find publicInt", ((List) JsonPath.read(doc, "$[?(@=='publicInt')]")).size() == 1); } /** - * Populate a JSONArray from an empty JSONObject names() method. - * It should be empty. + * Populate a JSONArray from a JSONObject names() method. Confirm that it + * contains the expected names. */ @Test - public void emptyJsonObjectNamesToJsonAray() { - JSONObject jsonObject = new JSONObject(); - JSONArray jsonArray = jsonObject.names(); - assertTrue("jsonArray should be null", jsonArray == null); + public void jsonObjectNamesToJsonAray() { + final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," + + "\"stringKey\":\"hello world!\"," + "}"; + + final JSONObject jsonObject = new JSONObject(str); + final JSONArray jsonArray = jsonObject.names(); + + // validate JSON + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + assertTrue("expected 3 top level items", + ((List) JsonPath.read(doc, "$")).size() == 3); + assertTrue( + "expected to find trueKey", + ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); + assertTrue( + "expected to find falseKey", + ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); + assertTrue( + "expected to find stringKey", + ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); } /** - * Populate a JSONArray from a JSONObject names() method. - * Confirm that it contains the expected names. + * Tests how JSONObject get[type] handles incorrect types */ @Test - public void jsonObjectNamesToJsonAray() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "}"; - - JSONObject jsonObject = new JSONObject(str); - JSONArray jsonArray = jsonObject.names(); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); - assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected to find trueKey", ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); - assertTrue("expected to find falseKey", ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); - assertTrue("expected to find stringKey", ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); + public void jsonObjectNonAndWrongValues() { + final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," + + "\"trueStrKey\":\"true\"," + "\"falseStrKey\":\"false\"," + + "\"stringKey\":\"hello world!\"," + "\"intKey\":42," + + "\"intStrKey\":\"43\"," + "\"longKey\":1234567890123456789," + + "\"longStrKey\":\"987654321098765432\"," + + "\"doubleKey\":-23.45e7," + "\"doubleStrKey\":\"00001.000\"," + + "\"arrayKey\":[0,1,2]," + + "\"objectKey\":{\"myKey\":\"myVal\"}" + "}"; + final JSONObject jsonObject = new JSONObject(str); + try { + jsonObject.getBoolean("nonKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("expecting an exception message", + "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } + try { + jsonObject.getBoolean("stringKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a Boolean.".equals(e + .getMessage())); + } + try { + jsonObject.getString("nonKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } + try { + jsonObject.getString("trueKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"trueKey\"] not a string.".equals(e + .getMessage())); + } + try { + jsonObject.getDouble("nonKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } + try { + jsonObject.getDouble("stringKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.".equals(e + .getMessage())); + } + try { + jsonObject.getInt("nonKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } + try { + jsonObject.getInt("stringKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not an int.".equals(e + .getMessage())); + } + try { + jsonObject.getLong("nonKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } + try { + jsonObject.getLong("stringKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a long.".equals(e + .getMessage())); + } + try { + jsonObject.getJSONArray("nonKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } + try { + jsonObject.getJSONArray("stringKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONArray.".equals(e + .getMessage())); + } + try { + jsonObject.getJSONObject("nonKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } + try { + jsonObject.getJSONObject("stringKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONObject.".equals(e + .getMessage())); + } } /** - * Exercise the JSONObject increment() method. + * JSON null is not the same as Java null. This test examines the + * differences in how they are handled by JSON-java. */ @Test - public void jsonObjectIncrement() { - String str = - "{"+ - "\"keyLong\":9999999991,"+ - "\"keyDouble\":1.1"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.increment("keyInt"); - jsonObject.increment("keyInt"); - jsonObject.increment("keyLong"); - jsonObject.increment("keyDouble"); - jsonObject.increment("keyInt"); - jsonObject.increment("keyLong"); - jsonObject.increment("keyDouble"); + public void jsonObjectNullOperations() { /** - * JSONObject constructor won't handle these types correctly, but - * adding them via put works. + * The Javadoc for JSONObject.NULL states: "JSONObject.NULL is + * equivalent to the value that JavaScript calls null, whilst Java's + * null is equivalent to the value that JavaScript calls undefined." + * + * Standard ECMA-262 6th Edition / June 2015 (included to help explain + * the javadoc): undefined value: primitive value used when a variable + * has not been assigned a value Undefined type: type whose sole value + * is the undefined value null value: primitive value that represents + * the intentional absence of any object value Null type: type whose + * sole value is the null value Java SE8 language spec (included to help + * explain the javadoc): The Kinds of Types and Values ... There is also + * a special null type, the type of the expression null, which has no + * name. Because the null type has no name, it is impossible to declare + * a variable of the null type or to cast to the null type. The null + * reference is the only possible value of an expression of null type. + * The null reference can always be assigned or cast to any reference + * type. In practice, the programmer can ignore the null type and just + * pretend that null is merely a special literal that can be of any + * reference type. Extensible Markup Language (XML) 1.0 Fifth Edition / + * 26 November 2008 No mention of null ECMA-404 1st Edition / October + * 2013: JSON Text ... These are three literal name tokens: ... null + * + * There seems to be no best practice to follow, it's all about what we + * want the code to do. */ - 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 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"))); + // add JSONObject.NULL then convert to string in the manner of + // XML.toString() + final JSONObject jsonObjectJONull = new JSONObject(); + Object obj = JSONObject.NULL; + jsonObjectJONull.put("key", obj); + Object value = jsonObjectJONull.opt("key"); + assertTrue("opt() JSONObject.NULL should find JSONObject.NULL", + obj.equals(value)); + value = jsonObjectJONull.get("key"); + assertTrue("get() JSONObject.NULL should find JSONObject.NULL", + obj.equals(value)); + if (value == null) { + value = ""; + } + String string = value instanceof String ? (String) value : null; + assertTrue("XML toString() should convert JSONObject.NULL to null", + string == null); - /** - * Should work the same way on any platform! @see https://docs.oracle - * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is the - * effect of a float to double conversion and is inherent to the - * shortcomings of the IEEE 754 format, when converting 32-bit into - * double-precision 64-bit. Java type-casts float to double. A 32 bit - * float is type-casted to 64 bit double by simply appending zero-bits - * to the mantissa (and extended the signed exponent by 3 bits.) and - * there is no way to obtain more information than it is stored in the - * 32-bits float. - * - * Like 1/3 cannot be represented as base10 number because it is - * periodically, 1/5 (for example) cannot be represented as base2 number - * since it is periodically in base2 (take a look at - * http://www.h-schmidt.net/FloatConverter/) The same happens to 3.1, - * that decimal number (base10 representation) is periodic in base2 - * representation, therefore appending zero-bits is inaccurate. Only - * repeating the periodically occuring bits (0110) would be a proper - * conversion. However one cannot detect from a 32 bit IEE754 - * representation which bits would "repeat infinitely", since the - * missing bits would not fit into the 32 bit float, i.e. the - * information needed simply is not there! - */ - assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(JsonPath.read(doc, "$.keyFloat"))); + // now try it with null + final JSONObject jsonObjectNull = new JSONObject(); + obj = null; + jsonObjectNull.put("key", obj); + value = jsonObjectNull.opt("key"); + assertTrue("opt() null should find null", value == null); + if (value == null) { + value = ""; + } + string = value instanceof String ? (String) value : null; + assertTrue("should convert null to empty string", "".equals(string)); + try { + value = jsonObjectNull.get("key"); + assertTrue("get() null should throw exception", false); + } catch (final Exception ignored) { + } /** - * float f = 3.1f; double df = (double) f; double d = 3.1d; - * System.out.println - * (Integer.toBinaryString(Float.floatToRawIntBits(f))); - * System.out.println - * (Long.toBinaryString(Double.doubleToRawLongBits(df))); - * System.out.println - * (Long.toBinaryString(Double.doubleToRawLongBits(d))); - * - * - Float: - * seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm - * 1000000010001100110011001100110 - * - Double - * seeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm - * 10000000 10001100110011001100110 - * 100000000001000110011001100110011000000000000000000000000000000 - * 100000000001000110011001100110011001100110011001100110011001101 + * XML.toString() then goes on to do something with the value if the key + * val is "content", then value.toString() will be called. This will + * evaluate to "null" for JSONObject.NULL, and the empty string for + * null. But if the key is anything else, then JSONObject.NULL will be + * emitted as null and null will be emitted as "" */ - - /** - * Examples of well documented but probably unexpected behavior in - * java / with 32-bit float to 64-bit float conversion. - */ - assertFalse("Document unexpected behaviour with explicit type-casting float as double!", (double)0.2f == 0.2d ); - assertFalse("Document unexpected behaviour with implicit type-cast!", 0.2f == 0.2d ); - Double d1 = new Double( 1.1f ); - Double d2 = new Double( "1.1f" ); - assertFalse( "Document implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) ); - - assertTrue( "Correctly converting float to double via base10 (string) representation!", new Double( 3.1d ).equals( new Double( new Float( 3.1f ).toString() ) ) ); - - // Pinpointing the not so obvious "buggy" conversion from float to double in JSONObject - JSONObject jo = new JSONObject(); - jo.put( "bug", 3.1f ); // will call put( String key, double value ) with implicit and "buggy" type-cast from float to double - assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) ); - - JSONObject inc = new JSONObject(); - inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) - assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float ); - inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double! - // this.put(key, (Float) value + 1); - // 1. The (Object)value will be typecasted to (Float)value since it is an instanceof Float actually nothing is done. - // 2. Float instance will be autoboxed into float because the + operator will work on primitives not Objects! - // 3. A float+float operation will be performed and results into a float primitive. - // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method - // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa - assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Double ); - // correct implementation (with change of behavior) would be: - // this.put(key, new Float((Float) value + 1)); - // Probably it would be better to deprecate the method and remove some day, while convenient processing the "payload" is not - // really in the the scope of a JSON-library (IMHO.) - + final String sJONull = XML.toString(jsonObjectJONull); + assertTrue("JSONObject.NULL should emit a null value", + "null".equals(sJONull)); + final String sNull = XML.toString(jsonObjectNull); + assertTrue("null should emit an empty string", "".equals(sNull)); } /** @@ -1236,19 +1181,161 @@ public void jsonObjectIncrement() { public void jsonObjectNumberToString() { String str; Double dVal; - Integer iVal = 1; + final Integer iVal = 1; str = JSONObject.numberToString(iVal); - assertTrue("expected "+iVal+" actual "+str, iVal.toString().equals(str)); + assertTrue("expected " + iVal + " actual " + str, iVal.toString() + .equals(str)); dVal = 12.34; str = JSONObject.numberToString(dVal); - assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + assertTrue("expected " + dVal + " actual " + str, dVal.toString() + .equals(str)); dVal = 12.34e27; str = JSONObject.numberToString(dVal); - assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + assertTrue("expected " + dVal + " actual " + str, dVal.toString() + .equals(str)); // trailing .0 is truncated, so it doesn't quite match toString() dVal = 5000000.0000000; str = JSONObject.numberToString(dVal); - assertTrue("expected 5000000 actual "+str, str.equals("5000000")); + assertTrue("expected 5000000 actual " + str, str.equals("5000000")); + } + + /** + * Exercise JSONObject opt(key, default) method + */ + @Test + public void jsonObjectOptDefault() { + + final String str = "{\"myKey\": \"myval\"}"; + final JSONObject jsonObject = new JSONObject(str); + + assertTrue("optBoolean() should return default boolean", + Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optLong() should return default long", + 42 == jsonObject.optLong("myKey", 42)); + assertTrue("optDouble() should return default double", + 42.3 == jsonObject.optDouble("myKey", 42.3)); + assertTrue("optString() should return default string", + "hi".equals(jsonObject.optString("hiKey", "hi"))); + } + + /** + * Explore how JSONObject handles parsing errors. + */ + @Test + public void jsonObjectParsingErrors() { + try { + // does not start with '{' + final String str = "abc"; + new JSONObject(str); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "A JSONObject text must begin with '{' at 1 [character 2 line 1]" + .equals(e.getMessage())); + } + try { + // does not end with '}' + final String str = "{"; + new JSONObject(str); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "A JSONObject text must end with '}' at 2 [character 3 line 1]" + .equals(e.getMessage())); + } + try { + // key with no ':' + final String str = "{\"myKey\" = true}"; + new JSONObject(str); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "Expected a ':' after a key at 10 [character 11 line 1]" + .equals(e.getMessage())); + } + try { + // entries with no ',' separator + final String str = "{\"myKey\":true \"myOtherKey\":false}"; + new JSONObject(str); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "Expected a ',' or '}' at 15 [character 16 line 1]" + .equals(e.getMessage())); + } + try { + // append to wrong key + final String str = "{\"myKey\":true, \"myOtherKey\":false}"; + final JSONObject jsonObject = new JSONObject(str); + jsonObject.append("myKey", "hello"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[myKey] is not a JSONArray.".equals(e + .getMessage())); + } + try { + // increment wrong key + final String str = "{\"myKey\":true, \"myOtherKey\":false}"; + final JSONObject jsonObject = new JSONObject(str); + jsonObject.increment("myKey"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "Unable to increment [\"myKey\"].".equals(e.getMessage())); + } + try { + // invalid key + final String str = "{\"myKey\":true, \"myOtherKey\":false}"; + final JSONObject jsonObject = new JSONObject(str); + jsonObject.get(null); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "Null key.".equals(e.getMessage())); + } + try { + // invalid numberToString() + JSONObject.numberToString((Number) null); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("Expecting an exception message", + "Null pointer".equals(e.getMessage())); + } + try { + // null put key + final JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0); + assertTrue("Expected an exception", false); + } catch (final NullPointerException ignored) { + } + try { + // multiple putOnce key + final JSONObject jsonObject = new JSONObject("{}"); + jsonObject.putOnce("hello", "world"); + jsonObject.putOnce("hello", "world!"); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("", true); + } + try { + // test validity of invalid double + JSONObject.testValidity(Double.NaN); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("", true); + } + try { + // test validity of invalid float + JSONObject.testValidity(Float.NEGATIVE_INFINITY); + assertTrue("Expected an exception", false); + } catch (final JSONException e) { + assertTrue("", true); + } } /** @@ -1256,24 +1343,17 @@ public void jsonObjectNumberToString() { */ @Test public void jsonObjectPut() { - String expectedStr = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{"+ - "\"myKey1\":\"myVal1\","+ - "\"myKey2\":\"myVal2\","+ - "\"myKey3\":\"myVal3\","+ - "\"myKey4\":\"myVal4\""+ - "}"+ - "}"; - JSONObject jsonObject = new JSONObject(); + final String expectedStr = "{" + "\"trueKey\":true," + + "\"falseKey\":false," + "\"arrayKey\":[0,1,2]," + + "\"objectKey\":{" + "\"myKey1\":\"myVal1\"," + + "\"myKey2\":\"myVal2\"," + "\"myKey3\":\"myVal3\"," + + "\"myKey4\":\"myVal4\"" + "}" + "}"; + final JSONObject jsonObject = new JSONObject(); jsonObject.put("trueKey", true); jsonObject.put("falseKey", false); - Integer [] intArray = { 0, 1, 2 }; + final Integer[] intArray = { 0, 1, 2 }; jsonObject.put("arrayKey", Arrays.asList(intArray)); - Map myMap = new HashMap(); + final Map myMap = new HashMap(); myMap.put("myKey1", "myVal1"); myMap.put("myKey2", "myVal2"); myMap.put("myKey3", "myVal3"); @@ -1281,197 +1361,608 @@ public void jsonObjectPut() { jsonObject.put("objectKey", myMap); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); - assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); - assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); - assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); - assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); - assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); - assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 4 top level items", + ((Map) JsonPath.read(doc, "$")).size() == 4); + assertTrue("expected true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected 3 arrayKey items", + ((List) JsonPath.read(doc, "$.arrayKey")).size() == 3); + assertTrue("expected 0", + Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); + assertTrue("expected 1", + Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); + assertTrue("expected 2", + Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); + assertTrue("expected 4 objectKey items", + ((Map) JsonPath.read(doc, "$.objectKey")).size() == 4); + assertTrue("expected myVal1", + "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); + assertTrue("expected myVal2", + "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); + assertTrue("expected myVal3", + "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); + assertTrue("expected myVal4", + "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); jsonObject.remove("trueKey"); - JSONObject expectedJsonObject = new JSONObject(expectedStr); + final JSONObject expectedJsonObject = new JSONObject(expectedStr); assertTrue("unequal jsonObjects should not be similar", !jsonObject.similar(expectedJsonObject)); assertTrue("jsonObject should not be similar to jsonArray", !jsonObject.similar(new JSONArray())); - String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}"; - String bCompareValueStr = "{\"a\":\"notAval\",\"b\":true}"; - JSONObject aCompareValueJsonObject = new JSONObject(aCompareValueStr); - JSONObject bCompareValueJsonObject = new JSONObject(bCompareValueStr); + final String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}"; + final String bCompareValueStr = "{\"a\":\"notAval\",\"b\":true}"; + final JSONObject aCompareValueJsonObject = new JSONObject( + aCompareValueStr); + final JSONObject bCompareValueJsonObject = new JSONObject( + bCompareValueStr); assertTrue("different values should not be similar", !aCompareValueJsonObject.similar(bCompareValueJsonObject)); - String aCompareObjectStr = "{\"a\":\"aval\",\"b\":{}}"; - String bCompareObjectStr = "{\"a\":\"aval\",\"b\":true}"; - JSONObject aCompareObjectJsonObject = new JSONObject(aCompareObjectStr); - JSONObject bCompareObjectJsonObject = new JSONObject(bCompareObjectStr); + final String aCompareObjectStr = "{\"a\":\"aval\",\"b\":{}}"; + final String bCompareObjectStr = "{\"a\":\"aval\",\"b\":true}"; + final JSONObject aCompareObjectJsonObject = new JSONObject( + aCompareObjectStr); + final JSONObject bCompareObjectJsonObject = new JSONObject( + bCompareObjectStr); assertTrue("different nested JSONObjects should not be similar", !aCompareObjectJsonObject.similar(bCompareObjectJsonObject)); - String aCompareArrayStr = "{\"a\":\"aval\",\"b\":[]}"; - String bCompareArrayStr = "{\"a\":\"aval\",\"b\":true}"; - JSONObject aCompareArrayJsonObject = new JSONObject(aCompareArrayStr); - JSONObject bCompareArrayJsonObject = new JSONObject(bCompareArrayStr); + final String aCompareArrayStr = "{\"a\":\"aval\",\"b\":[]}"; + final String bCompareArrayStr = "{\"a\":\"aval\",\"b\":true}"; + final JSONObject aCompareArrayJsonObject = new JSONObject( + aCompareArrayStr); + final JSONObject bCompareArrayJsonObject = new JSONObject( + bCompareArrayStr); assertTrue("different nested JSONArrays should not be similar", !aCompareArrayJsonObject.similar(bCompareArrayJsonObject)); } /** - * Exercise JSONObject toString() method + * Confirm behavior when JSONObject put(key, null object) is called */ @Test - public void jsonObjectToString() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{"+ - "\"myKey1\":\"myVal1\","+ - "\"myKey2\":\"myVal2\","+ - "\"myKey3\":\"myVal3\","+ - "\"myKey4\":\"myVal4\""+ - "}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); + public void jsonObjectputNull() { + + // put null should remove the item. + final String str = "{\"myKey\": \"myval\"}"; + final JSONObject jsonObjectRemove = new JSONObject(str); + jsonObjectRemove.remove("myKey"); + + final JSONObject jsonObjectPutNull = new JSONObject(str); + jsonObjectPutNull.put("myKey", (Object) null); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); - assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); - assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); - assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); - assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); - assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); - assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); + assertTrue("jsonObject should be empty", jsonObjectRemove.length() == 0 + && jsonObjectPutNull.length() == 0); } /** - * Explores how JSONObject handles maps. Insert a string/string map - * as a value in a JSONObject. It will remain a map. Convert the - * JSONObject to string, then create a new JSONObject from the string. - * In the new JSONObject, the value will be stored as a nested JSONObject. - * Confirm that map and nested JSONObject have the same contents. + * Confirm behavior when putOnce() is called with null parameters */ @Test - public void jsonObjectToStringSuppressWarningOnCastToMap() { - JSONObject jsonObject = new JSONObject(); - Map map = new HashMap<>(); - map.put("abc", "def"); - jsonObject.put("key", map); + public void jsonObjectPutOnceNull() { + final JSONObject jsonObject = new JSONObject(); + jsonObject.putOnce(null, null); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + /** + * Exercise JSONObject quote() method This purpose of quote() is to ensure + * that for strings with embedded quotes, the quotes are properly escaped. + */ + @Test + public void jsonObjectQuote() { + String str; + str = ""; + String quotedStr; + quotedStr = JSONObject.quote(str); + assertTrue("quote() expected escaped quotes, found " + quotedStr, + "\"\"".equals(quotedStr)); + str = "\"\""; + quotedStr = JSONObject.quote(str); + assertTrue("quote() expected escaped quotes, found " + quotedStr, + "\"\\\"\\\"\"".equals(quotedStr)); + str = ")(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 1 key item", ((Map)(JsonPath.read(doc, "$.key"))).size() == 1); - assertTrue("expected def", "def".equals(JsonPath.read(doc, "$.key.abc"))); + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 4 top level items", + ((Map) JsonPath.read(doc, "$")).size() == 4); + assertTrue("expected true", + Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected false", + Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected 3 arrayKey items", + ((List) JsonPath.read(doc, "$.arrayKey")).size() == 3); + assertTrue("expected 0", + Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); + assertTrue("expected 1", + Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); + assertTrue("expected 2", + Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); + assertTrue("expected 4 objectKey items", + ((Map) JsonPath.read(doc, "$.objectKey")).size() == 4); + assertTrue("expected myVal1", + "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); + assertTrue("expected myVal2", + "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); + assertTrue("expected myVal3", + "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); + assertTrue("expected myVal4", + "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); } /** * Explores how JSONObject handles collections. Insert a string collection - * as a value in a JSONObject. It will remain a collection. Convert the - * JSONObject to string, then create a new JSONObject from the string. - * In the new JSONObject, the value will be stored as a nested JSONArray. + * as a value in a JSONObject. It will remain a collection. Convert the + * JSONObject to string, then create a new JSONObject from the string. In + * the new JSONObject, the value will be stored as a nested JSONArray. * Confirm that collection and nested JSONArray have the same contents. */ @Test public void jsonObjectToStringSuppressWarningOnCastToCollection() { - JSONObject jsonObject = new JSONObject(); - Collection collection = new ArrayList(); + final JSONObject jsonObject = new JSONObject(); + final Collection collection = new ArrayList(); collection.add("abc"); // ArrayList will be added as an object jsonObject.put("key", collection); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 1 key item", ((List)(JsonPath.read(doc, "$.key"))).size() == 1); + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 1 top level item", + ((Map) JsonPath.read(doc, "$")).size() == 1); + assertTrue("expected 1 key item", + ((List) JsonPath.read(doc, "$.key")).size() == 1); assertTrue("expected abc", "abc".equals(JsonPath.read(doc, "$.key[0]"))); } + /** + * Explores how JSONObject handles maps. Insert a string/string map as a + * value in a JSONObject. It will remain a map. Convert the JSONObject to + * string, then create a new JSONObject from the string. In the new + * JSONObject, the value will be stored as a nested JSONObject. Confirm that + * map and nested JSONObject have the same contents. + */ + @Test + public void jsonObjectToStringSuppressWarningOnCastToMap() { + final JSONObject jsonObject = new JSONObject(); + final Map map = new HashMap<>(); + map.put("abc", "def"); + jsonObject.put("key", map); + + // validate JSON + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonObject.toString()); + assertTrue("expected 1 top level item", + ((Map) JsonPath.read(doc, "$")).size() == 1); + assertTrue("expected 1 key item", + ((Map) JsonPath.read(doc, "$.key")).size() == 1); + assertTrue("expected def", + "def".equals(JsonPath.read(doc, "$.key.abc"))); + } + + /** + * Exercise some JSONObject get[type] and opt[type] methods + */ + @Test + public void jsonObjectValues() { + final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," + + "\"trueStrKey\":\"true\"," + "\"falseStrKey\":\"false\"," + + "\"stringKey\":\"hello world!\"," + "\"intKey\":42," + + "\"intStrKey\":\"43\"," + "\"longKey\":1234567890123456789," + + "\"longStrKey\":\"987654321098765432\"," + + "\"doubleKey\":-23.45e7," + "\"doubleStrKey\":\"00001.000\"," + + "\"arrayKey\":[0,1,2]," + + "\"objectKey\":{\"myKey\":\"myVal\"}" + "}"; + final JSONObject jsonObject = new JSONObject(str); + assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); + assertTrue("opt trueKey should be true", + jsonObject.optBoolean("trueKey")); + assertTrue("falseKey should be false", + !jsonObject.getBoolean("falseKey")); + assertTrue("trueStrKey should be true", + jsonObject.getBoolean("trueStrKey")); + assertTrue("trueStrKey should be true", + jsonObject.optBoolean("trueStrKey")); + assertTrue("falseStrKey should be false", + !jsonObject.getBoolean("falseStrKey")); + assertTrue("stringKey should be string", + jsonObject.getString("stringKey").equals("hello world!")); + assertTrue("doubleKey should be double", + jsonObject.getDouble("doubleKey") == -23.45e7); + assertTrue("doubleStrKey should be double", + jsonObject.getDouble("doubleStrKey") == 1); + 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("intKey should be int", jsonObject.optInt("intKey") == 42); + assertTrue("opt intKey should be int", + jsonObject.optInt("intKey", 0) == 42); + assertTrue("opt intKey with default should be int", + jsonObject.getInt("intKey") == 42); + assertTrue("intStrKey should be int", + jsonObject.getInt("intStrKey") == 43); + assertTrue("longKey should be long", + jsonObject.getLong("longKey") == 1234567890123456789L); + assertTrue("opt longKey should be long", + jsonObject.optLong("longKey") == 1234567890123456789L); + assertTrue("opt longKey with default should be long", + jsonObject.optLong("longKey", 0) == 1234567890123456789L); + assertTrue("longStrKey should be long", + jsonObject.getLong("longStrKey") == 987654321098765432L); + assertTrue("xKey should not exist", jsonObject.isNull("xKey")); + assertTrue("stringKey should exist", jsonObject.has("stringKey")); + assertTrue("opt stringKey should string", + jsonObject.optString("stringKey").equals("hello world!")); + assertTrue("opt stringKey with default should string", jsonObject + .optString("stringKey", "not found").equals("hello world!")); + JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); + assertTrue("arrayKey should be JSONArray", jsonArray.getInt(0) == 0 + && jsonArray.getInt(1) == 1 && jsonArray.getInt(2) == 2); + jsonArray = jsonObject.optJSONArray("arrayKey"); + assertTrue("opt arrayKey should be JSONArray", jsonArray.getInt(0) == 0 + && jsonArray.getInt(1) == 1 && jsonArray.getInt(2) == 2); + final JSONObject jsonObjectInner = jsonObject + .getJSONObject("objectKey"); + assertTrue("objectKey should be JSONObject", + jsonObjectInner.get("myKey").equals("myVal")); + } + + /** + * This test documents numeric values which could be numerically handled as + * BigDecimal or BigInteger. It helps determine what outputs will change if + * those types are supported. + */ + @Test + public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() { + // Valid JSON Numbers, probably should return BigDecimal or BigInteger + // objects + final String str = "{" + "\"numberWithDecimals\":299792.457999999984," + + "\"largeNumber\":12345678901234567890," + + "\"preciseNumber\":0.2000000000000000111," + + "\"largeExponent\":-23.45e2327" + "}"; + final JSONObject jsonObject = new JSONObject(str); + // Comes back as a double, but loses precision + assertTrue( + "numberWithDecimals currently evaluates to double 299792.458", + jsonObject.get("numberWithDecimals").equals( + new Double("299792.458"))); + Object obj = jsonObject.get("largeNumber"); + assertTrue("largeNumber currently evaluates to string", + "12345678901234567890".equals(obj)); + // comes back as a double but loses precision + assertTrue("preciseNumber currently evaluates to double 0.2", + jsonObject.get("preciseNumber").equals(new Double(0.2))); + obj = jsonObject.get("largeExponent"); + assertTrue("largeExponent should currently evaluates as a string", + "-23.45e2327".equals(obj)); + } + + /** + * Confirm behavior when JSONObject stringToValue() is called for an empty + * string + */ + @Test + public void stringToValue() { + final String str = ""; + final String valueStr = (String) JSONObject.stringToValue(str); + assertTrue("stringToValue() expected empty String, found " + valueStr, + "".equals(valueStr)); + } + + /** + * Check whether JSONObject handles large or high precision numbers + * correctly + */ + @Test + public void stringToValueNumbersTest() { + assertTrue("-0 Should be a Double!", + JSONObject.stringToValue("-0") instanceof Double); + assertTrue("-0 Should be a Double!", + JSONObject.stringToValue("-0.0") instanceof Double); + assertTrue("'-' Should be a String!", + JSONObject.stringToValue("-") instanceof String); + assertTrue("0.2 should be a Double!", + JSONObject.stringToValue("0.2") instanceof Double); + assertTrue( + "Doubles should be Doubles, even when incorrectly converting floats!", + JSONObject.stringToValue(new Double("0.2f").toString()) instanceof Double); + /** + * This test documents a need for BigDecimal conversion. + */ + final Object obj = JSONObject.stringToValue("299792.457999999984"); + assertTrue( + "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!", + obj.equals(new Double(299792.458))); + assertTrue("1 should be an Integer!", + JSONObject.stringToValue("1") instanceof Integer); + assertTrue("Integer.MAX_VALUE should still be an Integer!", + JSONObject.stringToValue(new Integer(Integer.MAX_VALUE) + .toString()) instanceof Integer); + assertTrue("Large integers should be a Long!", + JSONObject.stringToValue(new Long(Long + .sum(Integer.MAX_VALUE, 1)).toString()) instanceof Long); + assertTrue( + "Long.MAX_VALUE should still be an Integer!", + JSONObject.stringToValue(new Long(Long.MAX_VALUE).toString()) instanceof Long); + + final String str = new BigInteger(new Long(Long.MAX_VALUE).toString()) + .add(BigInteger.ONE).toString(); + assertTrue("Really large integers currently evaluate to string", + JSONObject.stringToValue(str).equals("9223372036854775808")); + } + + /** + * Confirm behavior when toJSONArray is called with a null value + */ + @Test + public void toJSONArray() { + assertTrue("toJSONArray() with null names should be null", + null == new JSONObject().toJSONArray(null)); + } + + /** + * This test documents an unexpected numeric behavior. A double that ends + * with .0 is parsed, serialized, then parsed again. On the second parse, it + * has become an int. + */ + @Test + public void unexpectedDoubleToIntConversion() { + final String key30 = "key30"; + final String key31 = "key31"; + final JSONObject jsonObject = new JSONObject(); + jsonObject.put(key30, new Double(3.0)); + jsonObject.put(key31, new Double(3.1)); + + assertTrue("3.0 should remain a double", + jsonObject.getDouble(key30) == 3); + assertTrue("3.1 should remain a double", + jsonObject.getDouble(key31) == 3.1); + + // turns 3.0 into 3. + final String serializedString = jsonObject.toString(); + final JSONObject deserialized = new JSONObject(serializedString); + assertTrue("3.0 is now an int", + deserialized.get(key30) instanceof Integer); + assertTrue("3.0 can still be interpreted as a double", + deserialized.getDouble(key30) == 3.0); + assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1); + } + /** * Exercises the JSONObject.valueToString() method for various types */ @Test public void valueToString() { - + assertTrue("null valueToString() incorrect", "null".equals(JSONObject.valueToString(null))); - MyJsonString jsonString = new MyJsonString(); + final MyJsonString jsonString = new MyJsonString(); assertTrue("jsonstring valueToString() incorrect", "my string".equals(JSONObject.valueToString(jsonString))); assertTrue("boolean valueToString() incorrect", "true".equals(JSONObject.valueToString(Boolean.TRUE))); - assertTrue("non-numeric double", - "null".equals(JSONObject.doubleToString(Double.POSITIVE_INFINITY))); - String jsonObjectStr = - "{"+ - "\"key1\":\"val1\","+ - "\"key2\":\"val2\","+ - "\"key3\":\"val3\""+ - "}"; - JSONObject jsonObject = new JSONObject(jsonObjectStr); - assertTrue("jsonObject valueToString() incorrect", - JSONObject.valueToString(jsonObject).equals(jsonObject.toString())); - String jsonArrayStr = - "[1,2,3]"; - JSONArray jsonArray = new JSONArray(jsonArrayStr); - assertTrue("jsonArra valueToString() incorrect", - JSONObject.valueToString(jsonArray).equals(jsonArray.toString())); - Map map = new HashMap(); + assertTrue("non-numeric double", "null".equals(JSONObject + .doubleToString(Double.POSITIVE_INFINITY))); + final String jsonObjectStr = "{" + "\"key1\":\"val1\"," + + "\"key2\":\"val2\"," + "\"key3\":\"val3\"" + "}"; + final JSONObject jsonObject = new JSONObject(jsonObjectStr); + assertTrue("jsonObject valueToString() incorrect", JSONObject + .valueToString(jsonObject).equals(jsonObject.toString())); + final String jsonArrayStr = "[1,2,3]"; + final JSONArray jsonArray = new JSONArray(jsonArrayStr); + assertTrue("jsonArra valueToString() incorrect", JSONObject + .valueToString(jsonArray).equals(jsonArray.toString())); + final Map map = new HashMap(); map.put("key1", "val1"); map.put("key2", "val2"); map.put("key3", "val3"); - assertTrue("map valueToString() incorrect", - jsonObject.toString().equals(JSONObject.valueToString(map))); - Collection collection = new ArrayList(); + assertTrue("map valueToString() incorrect", jsonObject.toString() + .equals(JSONObject.valueToString(map))); + final Collection collection = new ArrayList(); collection.add(new Integer(1)); collection.add(new Integer(2)); collection.add(new Integer(3)); - assertTrue("collection valueToString() expected: "+ - jsonArray.toString()+ " actual: "+ - JSONObject.valueToString(collection), - jsonArray.toString().equals(JSONObject.valueToString(collection))); - Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; - assertTrue("array valueToString() incorrect", - jsonArray.toString().equals(JSONObject.valueToString(array))); + assertTrue( + "collection valueToString() expected: " + jsonArray.toString() + + " actual: " + JSONObject.valueToString(collection), + jsonArray.toString().equals( + JSONObject.valueToString(collection))); + final Integer[] array = { new Integer(1), new Integer(2), + new Integer(3) }; + assertTrue("array valueToString() incorrect", jsonArray.toString() + .equals(JSONObject.valueToString(array))); + } + + /** + * Confirm that https://github.com/douglascrockford/JSON-java/issues/167 is + * fixed. The following code was throwing a ClassCastException in the + * JSONObject(Map) constructor + */ + @Test + public void valueToStringConfirmException() { + final Map myMap = new HashMap(); + myMap.put(1, "myValue"); + // this is the test, it should not throw an exception + final String str = JSONObject.valueToString(myMap); + // confirm result, just in case + final Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(str); + assertTrue("expected 1 top level item", + ((Map) JsonPath.read(doc, "$")).size() == 1); + assertTrue("expected myValue", + "myValue".equals(JsonPath.read(doc, "$.1"))); + } + + /** + * Verifies that the constructor has backwards compatability with RAW types + * pre-java5. + */ + @Test + public void verifyConstructor() { + + final JSONObject expected = new JSONObject("{\"myKey\":10}"); + + @SuppressWarnings("rawtypes") + final Map myRawC = Collections.singletonMap("myKey", + Integer.valueOf(10)); + final JSONObject jaRaw = new JSONObject(myRawC); + + final Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + final JSONObject jaStrObj = new JSONObject(myCStrObj); + + final Map myCStrInt = Collections.singletonMap( + "myKey", Integer.valueOf(10)); + final JSONObject jaStrInt = new JSONObject(myCStrInt); + + final Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + final JSONObject jaObjObj = new JSONObject(myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); + } + + /** + * Verifies that the put Collection has backwards compatability with RAW + * types pre-java5. + */ + @Test + public void verifyPutCollection() { + + final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); + + @SuppressWarnings("rawtypes") + final Collection myRawC = Collections.singleton(Integer.valueOf(10)); + final JSONObject jaRaw = new JSONObject(); + jaRaw.put("myCollection", myRawC); + + final Collection myCObj = Collections + .singleton((Object) Integer.valueOf(10)); + final JSONObject jaObj = new JSONObject(); + jaObj.put("myCollection", myCObj); + + final Collection myCInt = Collections.singleton(Integer + .valueOf(10)); + final JSONObject jaInt = new JSONObject(); + jaInt.put("myCollection", myCInt); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); } /** - * Confirm that https://github.com/douglascrockford/JSON-java/issues/167 is fixed. - * The following code was throwing a ClassCastException in the - * JSONObject(Map) constructor + * Verifies that the put Map has backwards compatability with RAW types + * pre-java5. */ @Test - public void valueToStringConfirmException() { - Map myMap = new HashMap(); - myMap.put(1, "myValue"); - // this is the test, it should not throw an exception - String str = JSONObject.valueToString(myMap); - // confirm result, just in case - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(str); - assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected myValue", "myValue".equals(JsonPath.read(doc, "$.1"))); + public void verifyPutMap() { + + final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); + + @SuppressWarnings("rawtypes") + final Map myRawC = Collections.singletonMap("myKey", + Integer.valueOf(10)); + final JSONObject jaRaw = new JSONObject(); + jaRaw.put("myMap", myRawC); + + final Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + final JSONObject jaStrObj = new JSONObject(); + jaStrObj.put("myMap", myCStrObj); + + final Map myCStrInt = Collections.singletonMap( + "myKey", Integer.valueOf(10)); + final JSONObject jaStrInt = new JSONObject(); + jaStrInt.put("myMap", myCStrInt); + + final Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + final JSONObject jaObjObj = new JSONObject(); + jaObjObj.put("myMap", myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); } /** - * Exercise the JSONObject wrap() method. Sometimes wrap() will change - * the object being wrapped, other times not. The purpose of wrap() is - * to ensure the value is packaged in a way that is compatible with how - * a JSONObject value or JSONArray value is supposed to be stored. + * Exercise the JSONObject wrap() method. Sometimes wrap() will change the + * object being wrapped, other times not. The purpose of wrap() is to ensure + * the value is packaged in a way that is compatible with how a JSONObject + * value or JSONArray value is supposed to be stored. */ @Test public void wrapObject() { @@ -1480,421 +1971,105 @@ public void wrapObject() { JSONObject.NULL == JSONObject.wrap(null)); // wrap(Integer) returns Integer - Integer in = new Integer(1); - assertTrue("Integer wrap() incorrect", - in == JSONObject.wrap(in)); + final Integer in = new Integer(1); + assertTrue("Integer wrap() incorrect", in == JSONObject.wrap(in)); /** * This test is to document the preferred behavior if BigDecimal is - * supported. Previously bd returned as a string, since it - * is recognized as being a Java package class. Now with explicit - * support for big numbers, it remains a BigDecimal + * supported. Previously bd returned as a string, since it is recognized + * as being a Java package class. Now with explicit support for big + * numbers, it remains a BigDecimal */ - Object bdWrap = JSONObject.wrap(BigDecimal.ONE); + final Object bdWrap = JSONObject.wrap(BigDecimal.ONE); assertTrue("BigDecimal.ONE evaluates to ONE", bdWrap.equals(BigDecimal.ONE)); // wrap JSONObject returns JSONObject - String jsonObjectStr = - "{"+ - "\"key1\":\"val1\","+ - "\"key2\":\"val2\","+ - "\"key3\":\"val3\""+ - "}"; - JSONObject jsonObject = new JSONObject(jsonObjectStr); + final String jsonObjectStr = "{" + "\"key1\":\"val1\"," + + "\"key2\":\"val2\"," + "\"key3\":\"val3\"" + "}"; + final JSONObject jsonObject = new JSONObject(jsonObjectStr); assertTrue("JSONObject wrap() incorrect", jsonObject == JSONObject.wrap(jsonObject)); // wrap collection returns JSONArray - Collection collection = new ArrayList(); + final Collection collection = new ArrayList(); collection.add(new Integer(1)); collection.add(new Integer(2)); collection.add(new Integer(3)); - JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection)); + final JSONArray jsonArray = (JSONArray) JSONObject.wrap(collection); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); - assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + assertTrue("expected 3 top level items", + ((List) JsonPath.read(doc, "$")).size() == 3); + assertTrue("expected 1", + Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", + Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", + Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // wrap Array returns JSONArray - Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; - JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); + final Integer[] array = { new Integer(1), new Integer(2), + new Integer(3) }; + final JSONArray integerArrayJsonArray = (JSONArray) JSONObject + .wrap(array); // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); - assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + assertTrue("expected 3 top level items", + ((List) JsonPath.read(doc, "$")).size() == 3); + assertTrue("expected 1", + Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", + Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", + Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString()); - assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(integerArrayJsonArray.toString()); + assertTrue("expected 3 top level items", + ((List) JsonPath.read(doc, "$")).size() == 3); + assertTrue("expected 1", + Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", + Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", + Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // wrap map returns JSONObject - Map map = new HashMap(); + final Map map = new HashMap(); map.put("key1", "val1"); map.put("key2", "val2"); map.put("key3", "val3"); - JSONObject mapJsonObject = (JSONObject) (JSONObject.wrap(map)); + final JSONObject mapJsonObject = (JSONObject) JSONObject.wrap(map); // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString()); - assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(mapJsonObject.toString()); + assertTrue("expected 3 top level items", + ((Map) JsonPath.read(doc, "$")).size() == 3); 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"))); } - /** - * Explore how JSONObject handles parsing errors. - */ - @Test - public void jsonObjectParsingErrors() { - try { - // does not start with '{' - String str = "abc"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "A JSONObject text must begin with '{' at 1 [character 2 line 1]". - equals(e.getMessage())); - } - try { - // does not end with '}' - String str = "{"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "A JSONObject text must end with '}' at 2 [character 3 line 1]". - equals(e.getMessage())); - } - try { - // key with no ':' - String str = "{\"myKey\" = true}"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Expected a ':' after a key at 10 [character 11 line 1]". - equals(e.getMessage())); - } - try { - // entries with no ',' separator - String str = "{\"myKey\":true \"myOtherKey\":false}"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Expected a ',' or '}' at 15 [character 16 line 1]". - equals(e.getMessage())); - } - try { - // append to wrong key - String str = "{\"myKey\":true, \"myOtherKey\":false}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.append("myKey", "hello"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[myKey] is not a JSONArray.". - equals(e.getMessage())); - } - try { - // increment wrong key - String str = "{\"myKey\":true, \"myOtherKey\":false}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.increment("myKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Unable to increment [\"myKey\"].". - equals(e.getMessage())); - } - try { - // invalid key - String str = "{\"myKey\":true, \"myOtherKey\":false}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.get(null); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Null key.". - equals(e.getMessage())); - } - try { - // invalid numberToString() - JSONObject.numberToString((Number)null); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Null pointer". - equals(e.getMessage())); - } - try { - // null put key - JSONObject jsonObject = new JSONObject("{}"); - jsonObject.put(null, 0); - assertTrue("Expected an exception", false); - } catch (NullPointerException ignored) { - } - try { - // multiple putOnce key - JSONObject jsonObject = new JSONObject("{}"); - jsonObject.putOnce("hello", "world"); - jsonObject.putOnce("hello", "world!"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("", true); - } - try { - // test validity of invalid double - JSONObject.testValidity(Double.NaN); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("", true); - } - try { - // test validity of invalid float - JSONObject.testValidity(Float.NEGATIVE_INFINITY); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("", true); - } - } - - /** - * Confirm behavior when putOnce() is called with null parameters - */ - @Test - public void jsonObjectPutOnceNull() { - JSONObject jsonObject = new JSONObject(); - jsonObject.putOnce(null, null); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); - } - - /** - * Exercise JSONObject opt(key, default) method - */ - @Test - public void jsonObjectOptDefault() { - - String str = "{\"myKey\": \"myval\"}"; - JSONObject jsonObject = new JSONObject(str); - - assertTrue("optBoolean() should return default boolean", - Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE)); - assertTrue("optInt() should return default int", - 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optInt() should return default int", - 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optLong() should return default long", - 42 == jsonObject.optLong("myKey", 42)); - assertTrue("optDouble() should return default double", - 42.3 == jsonObject.optDouble("myKey", 42.3)); - assertTrue("optString() should return default string", - "hi".equals(jsonObject.optString("hiKey", "hi"))); - } - - /** - * Confirm behavior when JSONObject put(key, null object) is called - */ - @Test - public void jsonObjectputNull() { - - // put null should remove the item. - String str = "{\"myKey\": \"myval\"}"; - JSONObject jsonObjectRemove = new JSONObject(str); - jsonObjectRemove.remove("myKey"); - - JSONObject jsonObjectPutNull = new JSONObject(str); - jsonObjectPutNull.put("myKey", (Object) null); - - // validate JSON - assertTrue("jsonObject should be empty", jsonObjectRemove.length() == 0 - && jsonObjectPutNull.length() == 0); - } - - /** - * Exercise JSONObject quote() method - * This purpose of quote() is to ensure that for strings with embedded - * quotes, the quotes are properly escaped. - */ - @Test - public void jsonObjectQuote() { - String str; - str = ""; - String quotedStr; - quotedStr = JSONObject.quote(str); - assertTrue("quote() expected escaped quotes, found "+quotedStr, - "\"\"".equals(quotedStr)); - str = "\"\""; - quotedStr = JSONObject.quote(str); - assertTrue("quote() expected escaped quotes, found "+quotedStr, - "\"\\\"\\\"\"".equals(quotedStr)); - str = "null and null will be emitted as "" - */ - String sJONull = XML.toString(jsonObjectJONull); - assertTrue("JSONObject.NULL should emit a null value", - "null".equals(sJONull)); - String sNull = XML.toString(jsonObjectNull); - assertTrue("null should emit an empty string", "".equals(sNull)); - } } From bd958e08309ff1e29df90b0921e37886cb76b6af Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 27 Jan 2016 11:36:15 -0500 Subject: [PATCH 192/315] fixes formatting --- src/test/org/json/junit/JSONObjectTest.java | 3237 +++++++++---------- 1 file changed, 1532 insertions(+), 1705 deletions(-) diff --git a/src/test/org/json/junit/JSONObjectTest.java b/src/test/org/json/junit/JSONObjectTest.java index dc7445922..5eedf1a48 100644 --- a/src/test/org/json/junit/JSONObjectTest.java +++ b/src/test/org/json/junit/JSONObjectTest.java @@ -10,14 +10,7 @@ import java.io.Writer; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; +import java.util.*; import org.json.CDL; import org.json.JSONArray; @@ -26,241 +19,24 @@ import org.json.XML; import org.junit.Test; -import com.jayway.jsonpath.Configuration; -import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.*; /** - * JSONObject, along with JSONArray, are the central classes of the reference - * app. All of the other classes interact with them, and JSON functionality - * would otherwise be impossible. + * JSONObject, along with JSONArray, are the central classes of the reference app. + * All of the other classes interact with them, and JSON functionality would + * otherwise be impossible. */ public class JSONObjectTest { /** - * Document behaviors of big numbers. Includes both JSONObject and JSONArray - * tests + * JSONObject built from a bean, but only using a null value. + * Nothing good is expected to happen. + * Expects NullPointerException */ - @Test - public void bigNumberOperations() { - /** - * JSONObject tries to parse BigInteger as a bean, but it only has one - * getter, getLowestBitSet(). The value is lost and an unhelpful value - * is stored. This should be fixed. - */ - final BigInteger bigInteger = new BigInteger( - "123456789012345678901234567890"); - JSONObject jsonObject = new JSONObject(bigInteger); - Object obj = jsonObject.get("lowestSetBit"); - assertTrue("JSONObject only has 1 value", jsonObject.length() == 1); - assertTrue("JSONObject parses BigInteger as the Integer lowestBitSet", - obj instanceof Integer); - assertTrue("this bigInteger lowestBitSet happens to be 1", - obj.equals(1)); - - /** - * JSONObject tries to parse BigDecimal as a bean, but it has no - * getters, The value is lost and no value is stored. This should be - * fixed. - */ - final BigDecimal bigDecimal = new BigDecimal( - "123456789012345678901234567890.12345678901234567890123456789"); - jsonObject = new JSONObject(bigDecimal); - assertTrue("large bigDecimal is not stored", jsonObject.length() == 0); - - /** - * JSONObject put(String, Object) method stores and serializes bigInt - * and bigDec correctly. Nothing needs to change. - */ - 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 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 serializes bigDec correctly", - jsonObject - .toString() - .equals("{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); - - /** - * exercise some exceptions - */ - try { - jsonObject.getBigDecimal("bigInt"); - assertTrue("expected an exeption", false); - } catch (final 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 (final JSONException ignored) { - } - jsonObject.put("stringKey", "abc"); - try { - jsonObject.getBigDecimal("stringKey"); - assertTrue("expected an exeption", false); - } catch (final JSONException ignored) { - } - obj = jsonObject.optBigInteger("bigDec", BigInteger.ONE); - assertTrue("expected BigInteger", obj.equals(BigInteger.ONE)); - - /** - * JSONObject.numberToString() works correctly, nothing to change. - */ - String str = JSONObject.numberToString(bigInteger); - assertTrue("numberToString() handles bigInteger correctly", - str.equals("123456789012345678901234567890")); - str = JSONObject.numberToString(bigDecimal); - assertTrue( - "numberToString() handles bigDecimal correctly", - str.equals("123456789012345678901234567890.12345678901234567890123456789")); - - /** - * JSONObject.stringToValue() turns bigInt into an accurate string, and - * rounds bigDec. This incorrect, but users may have come to expect this - * behavior. Change would be marginally better, but might inconvenience - * users. - */ - obj = JSONObject.stringToValue(bigInteger.toString()); - assertTrue("stringToValue() turns bigInteger string into string", - obj instanceof String); - obj = JSONObject.stringToValue(bigDecimal.toString()); - assertTrue("stringToValue() changes bigDecimal string", !obj.toString() - .equals(bigDecimal.toString())); - - /** - * wrap() vs put() big number behavior is now the same. - */ - // bigInt map ctor - Map map = new HashMap(); - map.put("bigInt", bigInteger); - jsonObject = new JSONObject(map); - String actualFromMapStr = jsonObject.toString(); - assertTrue("bigInt in map (or array or bean) is a string", - actualFromMapStr - .equals("{\"bigInt\":123456789012345678901234567890}")); - // bigInt put - jsonObject = new JSONObject(); - jsonObject.put("bigInt", bigInteger); - String actualFromPutStr = jsonObject.toString(); - assertTrue("bigInt from put is a number", - actualFromPutStr - .equals("{\"bigInt\":123456789012345678901234567890}")); - // bigDec map ctor - map = new HashMap(); - map.put("bigDec", bigDecimal); - jsonObject = new JSONObject(map); - actualFromMapStr = jsonObject.toString(); - assertTrue( - "bigDec in map (or array or bean) is a bigDec", - actualFromMapStr - .equals("{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); - // bigDec put - jsonObject = new JSONObject(); - jsonObject.put("bigDec", bigDecimal); - actualFromPutStr = jsonObject.toString(); - assertTrue( - "bigDec from put is a number", - actualFromPutStr - .equals("{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); - // bigInt,bigDec put - JSONArray jsonArray = new JSONArray(); - jsonArray.put(bigInteger); - jsonArray.put(bigDecimal); - actualFromPutStr = jsonArray.toString(); - assertTrue( - "bigInt, bigDec from put is a number", - actualFromPutStr - .equals("[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); - assertTrue("getBigInt is bigInt", - jsonArray.getBigInteger(0).equals(bigInteger)); - assertTrue("getBigDec is bigDec", - jsonArray.getBigDecimal(1).equals(bigDecimal)); - assertTrue("optBigInt is bigInt", - jsonArray.optBigInteger(0, BigInteger.ONE).equals(bigInteger)); - assertTrue("optBigDec is bigDec", - jsonArray.optBigDecimal(1, BigDecimal.ONE).equals(bigDecimal)); - jsonArray.put(Boolean.TRUE); - try { - jsonArray.getBigInteger(2); - assertTrue("should not be able to get big int", false); - } catch (final Exception ignored) { - } - try { - jsonArray.getBigDecimal(2); - assertTrue("should not be able to get big dec", false); - } catch (final 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)); - - // bigInt,bigDec list ctor - final List list = new ArrayList(); - list.add(bigInteger); - list.add(bigDecimal); - jsonArray = new JSONArray(list); - final String actualFromListStr = jsonArray.toString(); - assertTrue( - "bigInt, bigDec in list is a bigInt, bigDec", - actualFromListStr - .equals("[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); - // bigInt bean ctor - MyBigNumberBean myBigNumberBean = mock(MyBigNumberBean.class); - when(myBigNumberBean.getBigInteger()).thenReturn( - new BigInteger("123456789012345678901234567890")); - jsonObject = new JSONObject(myBigNumberBean); - String actualFromBeanStr = jsonObject.toString(); - // can't do a full string compare because mockery adds an extra - // key/value - assertTrue("bigInt from bean ctor is a bigInt", - actualFromBeanStr.contains("123456789012345678901234567890")); - // bigDec bean ctor - myBigNumberBean = mock(MyBigNumberBean.class); - when(myBigNumberBean.getBigDecimal()) - .thenReturn( - new BigDecimal( - "123456789012345678901234567890.12345678901234567890123456789")); - jsonObject = new JSONObject(myBigNumberBean); - actualFromBeanStr = jsonObject.toString(); - // can't do a full string compare because mockery adds an extra - // key/value - assertTrue( - "bigDec from bean ctor is a bigDec", - actualFromBeanStr - .contains("123456789012345678901234567890.12345678901234567890123456789")); - // bigInt,bigDec wrap() - obj = JSONObject.wrap(bigInteger); - assertTrue("wrap() returns big num", obj.equals(bigInteger)); - obj = JSONObject.wrap(bigDecimal); - assertTrue("wrap() returns string", obj.equals(bigDecimal)); - + @Test(expected=NullPointerException.class) + public void jsonObjectByNullBean() { + MyBean myBean = null; + new JSONObject(myBean); } /** @@ -268,420 +44,381 @@ public void bigNumberOperations() { */ @Test public void emptyJsonObject() { - final JSONObject jsonObject = new JSONObject(); + JSONObject jsonObject = new JSONObject(); assertTrue("jsonObject should be empty", jsonObject.length() == 0); } /** - * Populate a JSONArray from an empty JSONObject names() method. It should - * be empty. + * A JSONObject can be created from another JSONObject plus a list of names. + * In this test, some of the starting JSONObject keys are not in the + * names list. */ @Test - public void emptyJsonObjectNamesToJsonAray() { - final JSONObject jsonObject = new JSONObject(); - final JSONArray jsonArray = jsonObject.names(); - assertTrue("jsonArray should be null", jsonArray == null); - } + public void jsonObjectByNames() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"nullKey\":null,"+ + "\"stringKey\":\"hello world!\","+ + "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e67"+ + "}"; + String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"}; + JSONObject jsonObject = new JSONObject(str); - /** - * Exercise the JSONObject equals() method - */ - @Test - public void equals() { - final String str = "{\"key\":\"value\"}"; - final JSONObject aJsonObject = new JSONObject(str); - assertTrue("Same JSONObject should be equal to itself", - aJsonObject.equals(aJsonObject)); + // validate JSON + JSONObject jsonObjectByName = new JSONObject(jsonObject, keys); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObjectByName.toString()); + assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"nullKey\":null", null == JsonPath.read(doc, "$.nullKey")); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); } /** - * This test documents how JSON-Java handles invalid numeric input. + * JSONObjects can be built from a Map. + * In this test the map is null. + * the JSONObject(JsonTokener) ctor is not tested directly since it already + * has full coverage from other tests. */ @Test - public void jsonInvalidNumberValues() { - // Number-notations supported by Java and invalid as JSON - final String str = "{" + "\"hexNumber\":-0x123," - + "\"tooManyZeros\":00," + "\"negativeInfinite\":-Infinity," - + "\"negativeNaN\":-NaN," + "\"negativeFraction\":-.01," - + "\"tooManyZerosFraction\":00.001," - + "\"negativeHexFloat\":-0x1.fffp1," - + "\"hexFloat\":0x1.0P-1074," + "\"floatIdentifier\":0.1f," - + "\"doubleIdentifier\":0.1d" + "}"; - final JSONObject jsonObject = new JSONObject(str); - Object obj; - obj = jsonObject.get("hexNumber"); - assertFalse( - "hexNumber must not be a number (should throw exception!?)", - obj instanceof Number); - assertTrue("hexNumber currently evaluates to string", - obj.equals("-0x123")); - assertTrue("tooManyZeros currently evaluates to string", jsonObject - .get("tooManyZeros").equals("00")); - obj = jsonObject.get("negativeInfinite"); - assertTrue("negativeInfinite currently evaluates to string", - obj.equals("-Infinity")); - obj = jsonObject.get("negativeNaN"); - assertTrue("negativeNaN currently evaluates to string", - obj.equals("-NaN")); - assertTrue("negativeFraction currently evaluates to double -0.01", - jsonObject.get("negativeFraction").equals(new Double(-0.01))); - assertTrue("tooManyZerosFraction currently evaluates to double 0.001", - jsonObject.get("tooManyZerosFraction") - .equals(new Double(0.001))); - assertTrue( - "negativeHexFloat currently evaluates to double -3.99951171875", - jsonObject.get("negativeHexFloat").equals( - new Double(-3.99951171875))); - assertTrue("hexFloat currently evaluates to double 4.9E-324", - jsonObject.get("hexFloat").equals(new Double(4.9E-324))); - assertTrue("floatIdentifier currently evaluates to double 0.1", - jsonObject.get("floatIdentifier").equals(new Double(0.1))); - assertTrue("doubleIdentifier currently evaluates to double 0.1", - jsonObject.get("doubleIdentifier").equals(new Double(0.1))); + public void jsonObjectByNullMap() { + Map map = null; + JSONObject jsonObject = new JSONObject(map); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); } /** - * Exercise the JSONObject.accumulate() method + * JSONObjects can be built from a Map. + * In this test all of the map entries are valid JSON types. */ @Test - public void jsonObjectAccumulate() { - - final JSONObject jsonObject = new JSONObject(); - jsonObject.accumulate("myArray", true); - jsonObject.accumulate("myArray", false); - jsonObject.accumulate("myArray", "hello world!"); - 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 (final JSONException ignored) { - } + public void jsonObjectByMap() { + Map map = new HashMap(); + map.put("trueKey", new Boolean(true)); + map.put("falseKey", new Boolean(false)); + map.put("stringKey", "hello world!"); + map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); + map.put("intKey", new Long(42)); + map.put("doubleKey", new Double(-23.45e67)); + JSONObject jsonObject = new JSONObject(map); // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 1 top level item", - ((Map) JsonPath.read(doc, "$")).size() == 1); - assertTrue("expected 6 myArray items", - ((List) JsonPath.read(doc, "$.myArray")).size() == 6); - assertTrue("expected true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); - assertTrue("expected false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); - assertTrue("expected hello world!", - "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); - assertTrue("expected h\be\tllo w\u1234orld!", - "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, - "$.myArray[3]"))); - assertTrue("expected 42", - Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); - assertTrue( - "expected -23.45e7", - Double.valueOf(-23.45e7).equals( - JsonPath.read(doc, "$.myArray[5]"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); } - + /** - * Exercise the JSONObject append() functionality + * Verifies that the constructor has backwards compatability with RAW types pre-java5. */ @Test - public void jsonObjectAppend() { - final 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 (final JSONException ignored) { - } + public void verifyConstructor() { + + final JSONObject expected = new JSONObject("{\"myKey\":10}"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(myRawC); - // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 1 top level item", - ((Map) JsonPath.read(doc, "$")).size() == 1); - assertTrue("expected 6 myArray items", - ((List) JsonPath.read(doc, "$.myArray")).size() == 6); - assertTrue("expected true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); - assertTrue("expected false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); - assertTrue("expected hello world!", - "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); - assertTrue("expected h\be\tllo w\u1234orld!", - "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, - "$.myArray[3]"))); - assertTrue("expected 42", - Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); - assertTrue( - "expected -23.45e7", - Double.valueOf(-23.45e7).equals( - JsonPath.read(doc, "$.myArray[5]"))); - } + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONObject jaStrObj = new JSONObject(myCStrObj); - /** - * JSONObject built from a bean. In this case all but one of the bean - * getters return valid JSON types - */ - @Test - public void jsonObjectByBean() { - /** - * Default access classes have to be mocked since JSONObject, which is - * not in the same package, cannot call MyBean methods by reflection. - */ - final MyBean myBean = mock(MyBean.class); - when(myBean.getDoubleKey()).thenReturn(-23.45e7); - when(myBean.getIntKey()).thenReturn(42); - when(myBean.getStringKey()).thenReturn("hello world!"); - when(myBean.getEscapeStringKey()).thenReturn("h\be\tllo w\u1234orld!"); - when(myBean.isTrueKey()).thenReturn(true); - when(myBean.isFalseKey()).thenReturn(false); - when(myBean.getStringReaderKey()).thenReturn(new StringReader("") { - }); + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONObject jaStrInt = new JSONObject(myCStrInt); - final JSONObject jsonObject = new JSONObject(myBean); + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONObject jaObjObj = new JSONObject(myCObjObj); - // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 8 top level items", - ((Map) JsonPath.read(doc, "$")).size() == 8); - assertTrue("expected true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected hello world!", - "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected h\be\tllo w\u1234orld!", - "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, - "$.escapeStringKey"))); - assertTrue("expected 42", - Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); assertTrue( - "expected -23.45e7", - Double.valueOf("-23.45e7").equals( - JsonPath.read(doc, "$.doubleKey"))); + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); assertTrue( - "expected 0 items in stringReaderKey", - ((Map) JsonPath.read(doc, "$.stringReaderKey")).size() == 0); - // sorry, mockito artifact - assertTrue("expected 2 callbacks items", - ((List) JsonPath.read(doc, "$.callbacks")).size() == 2); - assertTrue("expected 0 handler items", ((Map) JsonPath.read(doc, - "$.callbacks[0].handler")).size() == 0); - assertTrue("expected 0 callbacks[1] items", - ((Map) JsonPath.read(doc, "$.callbacks[1]")).size() == 0); + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); } - + /** - * JSONObjects can be built from a Map. In this test all of - * the map entries are valid JSON types. + * Verifies that the put Collection has backwards compatability with RAW types pre-java5. */ @Test - public void jsonObjectByMap() { - final Map map = new HashMap(); - map.put("trueKey", new Boolean(true)); - map.put("falseKey", new Boolean(false)); - map.put("stringKey", "hello world!"); - map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - map.put("intKey", new Long(42)); - map.put("doubleKey", new Double(-23.45e67)); - final JSONObject jsonObject = new JSONObject(map); + public void verifyPutCollection() { + + final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); - // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 6 top level items", - ((Map) JsonPath.read(doc, "$")).size() == 6); - assertTrue("expected \"trueKey\":true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected \"falseKey\":false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", - "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", - "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, - "$.escapeStringKey"))); - assertTrue( - "expected \"doubleKey\":-23.45e67", - Double.valueOf("-23.45e67").equals( - JsonPath.read(doc, "$.doubleKey"))); - } + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(); + jaRaw.put("myCollection", myRawC); + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONObject jaObj = new JSONObject(); + jaObj.put("myCollection", myCObj); + + Collection myCInt = Collections.singleton(Integer + .valueOf(10)); + JSONObject jaInt = new JSONObject(); + jaInt.put("myCollection", myCInt); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); + } + + /** - * JSONObjects can be built from a Map. In this test one of - * the map values is null + * Verifies that the put Map has backwards compatability with RAW types pre-java5. */ @Test - public void jsonObjectByMapWithNullValue() { - final Map map = new HashMap(); - map.put("trueKey", new Boolean(true)); - map.put("falseKey", new Boolean(false)); - map.put("stringKey", "hello world!"); - map.put("nullKey", null); - map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - map.put("intKey", new Long(42)); - map.put("doubleKey", new Double(-23.45e67)); - final JSONObject jsonObject = new JSONObject(map); + public void verifyPutMap() { + + final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); - // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 6 top level items", - ((Map) JsonPath.read(doc, "$")).size() == 6); - assertTrue("expected \"trueKey\":true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected \"falseKey\":false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", - "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", - "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, - "$.escapeStringKey"))); - assertTrue("expected \"intKey\":42", - Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(); + jaRaw.put("myMap", myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONObject jaStrObj = new JSONObject(); + jaStrObj.put("myMap", myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONObject jaStrInt = new JSONObject(); + jaStrInt.put("myMap", myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONObject jaObjObj = new JSONObject(); + jaObjObj.put("myMap", myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); assertTrue( - "expected \"doubleKey\":-23.45e67", - Double.valueOf("-23.45e67").equals( - JsonPath.read(doc, "$.doubleKey"))); + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); } + /** - * JSONObjects can be built from a Map. In this test the map - * entries are not valid JSON types. The actual conversion is kind of - * interesting. + * JSONObjects can be built from a Map. + * In this test the map entries are not valid JSON types. + * The actual conversion is kind of interesting. */ @Test public void jsonObjectByMapWithUnsupportedValues() { - final Map jsonMap = new HashMap(); + Map jsonMap = new HashMap(); // Just insert some random objects jsonMap.put("key1", new CDL()); jsonMap.put("key2", new Exception()); - final JSONObject jsonObject = new JSONObject(jsonMap); + JSONObject jsonObject = new JSONObject(jsonMap); // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 2 top level items", - ((Map) JsonPath.read(doc, "$")).size() == 2); - assertTrue("expected \"key2\":java.lang.Exception", - "java.lang.Exception".equals(JsonPath.read(doc, "$.key2"))); - assertTrue("expected 0 key1 items", - ((Map) JsonPath.read(doc, "$.key1")).size() == 0); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(JsonPath.read(doc, "$.key2"))); + assertTrue("expected 0 key1 items", ((Map)(JsonPath.read(doc, "$.key1"))).size() == 0); } /** - * A JSONObject can be created from another JSONObject plus a list of names. - * In this test, some of the starting JSONObject keys are not in the names - * list. + * JSONObjects can be built from a Map. + * In this test one of the map values is null */ @Test - public void jsonObjectByNames() { - final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," - + "\"nullKey\":null," + "\"stringKey\":\"hello world!\"," - + "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\"," - + "\"intKey\":42," + "\"doubleKey\":-23.45e67" + "}"; - final String[] keys = { "falseKey", "stringKey", "nullKey", "doubleKey" }; - final JSONObject jsonObject = new JSONObject(str); + public void jsonObjectByMapWithNullValue() { + Map map = new HashMap(); + map.put("trueKey", new Boolean(true)); + map.put("falseKey", new Boolean(false)); + map.put("stringKey", "hello world!"); + map.put("nullKey", null); + map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); + map.put("intKey", new Long(42)); + map.put("doubleKey", new Double(-23.45e67)); + JSONObject jsonObject = new JSONObject(map); // validate JSON - final JSONObject jsonObjectByName = new JSONObject(jsonObject, keys); - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObjectByName.toString()); - assertTrue("expected 4 top level items", - ((Map) JsonPath.read(doc, "$")).size() == 4); - assertTrue("expected \"falseKey\":false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"nullKey\":null", - null == JsonPath.read(doc, "$.nullKey")); - assertTrue("expected \"stringKey\":\"hello world!\"", - "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue( - "expected \"doubleKey\":-23.45e67", - Double.valueOf("-23.45e67").equals( - JsonPath.read(doc, "$.doubleKey"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); + assertTrue("expected \"intKey\":42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); } /** - * JSONObject built from a bean, but only using a null value. Nothing good - * is expected to happen. Expects NullPointerException + * JSONObject built from a bean. In this case all but one of the + * bean getters return valid JSON types */ - @Test(expected = NullPointerException.class) - public void jsonObjectByNullBean() { - final MyBean myBean = null; - new JSONObject(myBean); + @Test + public void jsonObjectByBean() { + /** + * Default access classes have to be mocked since JSONObject, which is + * not in the same package, cannot call MyBean methods by reflection. + */ + MyBean myBean = mock(MyBean.class); + when(myBean.getDoubleKey()).thenReturn(-23.45e7); + when(myBean.getIntKey()).thenReturn(42); + when(myBean.getStringKey()).thenReturn("hello world!"); + when(myBean.getEscapeStringKey()).thenReturn("h\be\tllo w\u1234orld!"); + when(myBean.isTrueKey()).thenReturn(true); + when(myBean.isFalseKey()).thenReturn(false); + when(myBean.getStringReaderKey()).thenReturn( + new StringReader("") { + }); + + JSONObject jsonObject = new JSONObject(myBean); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 8 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 8); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected hello world!","hello world!".equals(JsonPath.read(doc, "$.stringKey"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); + assertTrue("expected 42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); + assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(JsonPath.read(doc, "$.doubleKey"))); + assertTrue("expected 0 items in stringReaderKey", ((Map) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0); + // sorry, mockito artifact + assertTrue("expected 2 callbacks items", ((List)(JsonPath.read(doc, "$.callbacks"))).size() == 2); + assertTrue("expected 0 handler items", ((Map)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0); + assertTrue("expected 0 callbacks[1] items", ((Map)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0); } /** - * JSONObjects can be built from a Map. In this test the map - * is null. the JSONObject(JsonTokener) ctor is not tested directly since it - * already has full coverage from other tests. + * A bean is also an object. But in order to test the JSONObject + * ctor that takes an object and a list of names, + * this particular bean needs some public + * data members, which have been added to the class. */ @Test - public void jsonObjectByNullMap() { - final Map map = null; - final JSONObject jsonObject = new JSONObject(map); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + public void jsonObjectByObjectAndNames() { + String[] keys = {"publicString", "publicInt"}; + // just need a class that has public data members + MyPublicClass myPublicClass = new MyPublicClass(); + JSONObject jsonObject = new JSONObject(myPublicClass, keys); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected \"publicString\":\"abc\"", "abc".equals(JsonPath.read(doc, "$.publicString"))); + assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.publicInt"))); } /** - * A bean is also an object. But in order to test the JSONObject ctor that - * takes an object and a list of names, this particular bean needs some - * public data members, which have been added to the class. + * Exercise the JSONObject from resource bundle functionality. + * The test resource bundle is uncomplicated, but provides adequate test coverage. */ @Test - public void jsonObjectByObjectAndNames() { - final String[] keys = { "publicString", "publicInt" }; - // just need a class that has public data members - final MyPublicClass myPublicClass = new MyPublicClass(); - final JSONObject jsonObject = new JSONObject(myPublicClass, keys); + public void jsonObjectByResourceBundle() { + JSONObject jsonObject = new + JSONObject("org.json.junit.StringsResourceBundle", + Locale.getDefault()); // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 2 top level items", - ((Map) JsonPath.read(doc, "$")).size() == 2); - assertTrue("expected \"publicString\":\"abc\"", - "abc".equals(JsonPath.read(doc, "$.publicString"))); - assertTrue("expected \"publicInt\":42", - Integer.valueOf(42).equals(JsonPath.read(doc, "$.publicInt"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected 2 greetings items", ((Map)(JsonPath.read(doc, "$.greetings"))).size() == 2); + assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(JsonPath.read(doc, "$.greetings.hello"))); + assertTrue("expected \"world\":\"World!\"", "World!".equals(JsonPath.read(doc, "$.greetings.world"))); + assertTrue("expected 2 farewells items", ((Map)(JsonPath.read(doc, "$.farewells"))).size() == 2); + assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(JsonPath.read(doc, "$.farewells.later"))); + assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(JsonPath.read(doc, "$.farewells.gator"))); } /** - * Exercise the JSONObject from resource bundle functionality. The test - * resource bundle is uncomplicated, but provides adequate test coverage. + * Exercise the JSONObject.accumulate() method */ @Test - public void jsonObjectByResourceBundle() { - final JSONObject jsonObject = new JSONObject( - "org.json.junit.StringsResourceBundle", Locale.getDefault()); + public void jsonObjectAccumulate() { + + JSONObject jsonObject = new JSONObject(); + jsonObject.accumulate("myArray", true); + jsonObject.accumulate("myArray", false); + jsonObject.accumulate("myArray", "hello world!"); + 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()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); + assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); + assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]"))); + } + + /** + * Exercise the JSONObject append() functionality + */ + @Test + public void jsonObjectAppend() { + 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 - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 2 top level items", - ((Map) JsonPath.read(doc, "$")).size() == 2); - assertTrue("expected 2 greetings items", - ((Map) JsonPath.read(doc, "$.greetings")).size() == 2); - assertTrue("expected \"hello\":\"Hello, \"", - "Hello, ".equals(JsonPath.read(doc, "$.greetings.hello"))); - assertTrue("expected \"world\":\"World!\"", - "World!".equals(JsonPath.read(doc, "$.greetings.world"))); - assertTrue("expected 2 farewells items", - ((Map) JsonPath.read(doc, "$.farewells")).size() == 2); - assertTrue("expected \"later\":\"Later, \"", - "Later, ".equals(JsonPath.read(doc, "$.farewells.later"))); - assertTrue("expected \"world\":\"World!\"", - "Alligator!".equals(JsonPath.read(doc, "$.farewells.gator"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); + assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]"))); + assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); + assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]"))); } /** @@ -689,653 +426,831 @@ public void jsonObjectByResourceBundle() { */ @Test public void jsonObjectDoubleToString() { - final String[] expectedStrs = { "1", "1", "-23.4", "-2.345E68", "null", - "null" }; - final Double[] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, - Double.NaN, Double.NEGATIVE_INFINITY }; + String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" }; + Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, + Double.NaN, Double.NEGATIVE_INFINITY }; for (int i = 0; i < expectedStrs.length; ++i) { - final String actualStr = JSONObject.doubleToString(doubles[i]); - assertTrue("value expected [" + expectedStrs[i] + "] found [" - + actualStr + "]", expectedStrs[i].equals(actualStr)); + String actualStr = JSONObject.doubleToString(doubles[i]); + assertTrue("value expected ["+expectedStrs[i]+ + "] found ["+actualStr+ "]", + expectedStrs[i].equals(actualStr)); } } /** - * Exercise the JSONObject increment() method. + * Exercise some JSONObject get[type] and opt[type] methods */ @Test - public void jsonObjectIncrement() { - final String str = "{" + "\"keyLong\":9999999991," - + "\"keyDouble\":1.1" + "}"; - final JSONObject jsonObject = new JSONObject(str); - jsonObject.increment("keyInt"); - jsonObject.increment("keyInt"); - jsonObject.increment("keyLong"); - jsonObject.increment("keyDouble"); - 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 - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - 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 - * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is the - * effect of a float to double conversion and is inherent to the - * shortcomings of the IEEE 754 format, when converting 32-bit into - * double-precision 64-bit. Java type-casts float to double. A 32 bit - * float is type-casted to 64 bit double by simply appending zero-bits - * to the mantissa (and extended the signed exponent by 3 bits.) and - * there is no way to obtain more information than it is stored in the - * 32-bits float. - * - * Like 1/3 cannot be represented as base10 number because it is - * periodically, 1/5 (for example) cannot be represented as base2 number - * since it is periodically in base2 (take a look at - * http://www.h-schmidt.net/FloatConverter/) The same happens to 3.1, - * that decimal number (base10 representation) is periodic in base2 - * representation, therefore appending zero-bits is inaccurate. Only - * repeating the periodically occuring bits (0110) would be a proper - * conversion. However one cannot detect from a 32 bit IEE754 - * representation which bits would "repeat infinitely", since the - * missing bits would not fit into the 32 bit float, i.e. the - * information needed simply is not there! - */ - assertTrue( - "expected 3.0999999046325684", - Double.valueOf(3.0999999046325684).equals( - JsonPath.read(doc, "$.keyFloat"))); - - /** - * float f = 3.1f; double df = (double) f; double d = 3.1d; - * System.out.println - * (Integer.toBinaryString(Float.floatToRawIntBits(f))); - * System.out.println - * (Long.toBinaryString(Double.doubleToRawLongBits(df))); - * System.out.println - * (Long.toBinaryString(Double.doubleToRawLongBits(d))); - * - * - Float: seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm - * 1000000010001100110011001100110 - Double - * seeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm - * 10000000 10001100110011001100110 - * 100000000001000110011001100110011000000000000000000000000000000 - * 100000000001000110011001100110011001100110011001100110011001101 - */ + public void jsonObjectValues() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"trueStrKey\":\"true\","+ + "\"falseStrKey\":\"false\","+ + "\"stringKey\":\"hello world!\","+ + "\"intKey\":42,"+ + "\"intStrKey\":\"43\","+ + "\"longKey\":1234567890123456789,"+ + "\"longStrKey\":\"987654321098765432\","+ + "\"doubleKey\":-23.45e7,"+ + "\"doubleStrKey\":\"00001.000\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{\"myKey\":\"myVal\"}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); + assertTrue("opt trueKey should be true", jsonObject.optBoolean("trueKey")); + assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey")); + assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey")); + assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey")); + assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey")); + assertTrue("stringKey should be string", + jsonObject.getString("stringKey").equals("hello world!")); + assertTrue("doubleKey should be double", + jsonObject.getDouble("doubleKey") == -23.45e7); + assertTrue("doubleStrKey should be double", + jsonObject.getDouble("doubleStrKey") == 1); + 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("intKey should be int", + jsonObject.optInt("intKey") == 42); + assertTrue("opt intKey should be int", + jsonObject.optInt("intKey", 0) == 42); + assertTrue("opt intKey with default should be int", + jsonObject.getInt("intKey") == 42); + assertTrue("intStrKey should be int", + jsonObject.getInt("intStrKey") == 43); + assertTrue("longKey should be long", + jsonObject.getLong("longKey") == 1234567890123456789L); + assertTrue("opt longKey should be long", + jsonObject.optLong("longKey") == 1234567890123456789L); + assertTrue("opt longKey with default should be long", + jsonObject.optLong("longKey", 0) == 1234567890123456789L); + assertTrue("longStrKey should be long", + jsonObject.getLong("longStrKey") == 987654321098765432L); + assertTrue("xKey should not exist", + jsonObject.isNull("xKey")); + assertTrue("stringKey should exist", + jsonObject.has("stringKey")); + assertTrue("opt stringKey should string", + jsonObject.optString("stringKey").equals("hello world!")); + assertTrue("opt stringKey with default should string", + jsonObject.optString("stringKey", "not found").equals("hello world!")); + JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); + assertTrue("arrayKey should be JSONArray", + jsonArray.getInt(0) == 0 && + jsonArray.getInt(1) == 1 && + jsonArray.getInt(2) == 2); + jsonArray = jsonObject.optJSONArray("arrayKey"); + assertTrue("opt arrayKey should be JSONArray", + jsonArray.getInt(0) == 0 && + jsonArray.getInt(1) == 1 && + jsonArray.getInt(2) == 2); + JSONObject jsonObjectInner = jsonObject.getJSONObject("objectKey"); + assertTrue("objectKey should be JSONObject", + jsonObjectInner.get("myKey").equals("myVal")); + } + /** + * Check whether JSONObject handles large or high precision numbers correctly + */ + @Test + public void stringToValueNumbersTest() { + assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0") instanceof Double); + assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double); + assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String); + assertTrue( "0.2 should be a Double!", + JSONObject.stringToValue( "0.2" ) instanceof Double ); + assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!", + JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double ); /** - * Examples of well documented but probably unexpected behavior in java - * / with 32-bit float to 64-bit float conversion. + * This test documents a need for BigDecimal conversion. */ - assertFalse( - "Document unexpected behaviour with explicit type-casting float as double!", - 0.2f == 0.2d); - assertFalse("Document unexpected behaviour with implicit type-cast!", - 0.2f == 0.2d); - final Double d1 = new Double(1.1f); - final Double d2 = new Double("1.1f"); - assertFalse( - "Document implicit type cast from float to double before calling Double(double d) constructor", - d1.equals(d2)); - - assertTrue( - "Correctly converting float to double via base10 (string) representation!", - new Double(3.1d).equals(new Double(new Float(3.1f).toString()))); - - // Pinpointing the not so obvious "buggy" conversion from float to - // double in JSONObject - final JSONObject jo = new JSONObject(); - jo.put("bug", 3.1f); // will call put( String key, double value ) with - // implicit and "buggy" type-cast from float to - // double - assertFalse( - "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", - jo.get("bug").equals(new Double(3.1d))); - - final JSONObject inc = new JSONObject(); - inc.put("bug", new Float(3.1f)); // This will put in instance of Float - // into JSONObject, i.e. call put( - // String key, Object value ) - assertTrue("Everything is ok here!", inc.get("bug") instanceof Float); - inc.increment("bug"); // after adding 1, increment will call put( String - // key, double value ) with implicit and "buggy" - // type-cast from float to double! - // this.put(key, (Float) value + 1); - // 1. The (Object)value will be typecasted to (Float)value since it is - // an instanceof Float actually nothing is done. - // 2. Float instance will be autoboxed into float because the + operator - // will work on primitives not Objects! - // 3. A float+float operation will be performed and results into a float - // primitive. - // 4. There is no method that matches the signature put( String key, - // float value), java-compiler will choose the method - // put( String key, double value) and does an implicit type-cast(!) by - // appending zero-bits to the mantissa - assertTrue("JSONObject increment converts Float to Double", - jo.get("bug") instanceof Double); - // correct implementation (with change of behavior) would be: - // this.put(key, new Float((Float) value + 1)); - // Probably it would be better to deprecate the method and remove some - // day, while convenient processing the "payload" is not - // really in the the scope of a JSON-library (IMHO.) - + Object obj = JSONObject.stringToValue( "299792.457999999984" ); + assertTrue( "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!", + obj.equals(new Double(299792.458)) ); + assertTrue( "1 should be an Integer!", + JSONObject.stringToValue( "1" ) instanceof Integer ); + assertTrue( "Integer.MAX_VALUE should still be an Integer!", + JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer ); + assertTrue( "Large integers should be a Long!", + JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long ); + assertTrue( "Long.MAX_VALUE should still be an Integer!", + JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long ); + + String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString(); + assertTrue( "Really large integers currently evaluate to string", + JSONObject.stringToValue(str).equals("9223372036854775808")); } /** - * The purpose for the static method getNames() methods are not clear. This - * method is not called from within JSON-Java. Most likely uses are to prep - * names arrays for: JSONObject(JSONObject jo, String[] names) - * JSONObject(Object object, String names[]), + * This test documents numeric values which could be numerically + * handled as BigDecimal or BigInteger. It helps determine what outputs + * will change if those types are supported. */ @Test - public void jsonObjectNames() { - JSONObject jsonObject; - - // getNames() from null JSONObject - assertTrue("null names from null Object", - null == JSONObject.getNames((Object) null)); - - // getNames() from object with no fields - assertTrue("null names from Object with no fields", - null == JSONObject.getNames(new MyJsonString())); - - // getNames from new JSONOjbect - jsonObject = new JSONObject(); - String[] names = JSONObject.getNames(jsonObject); - assertTrue("names should be null", names == null); + public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() { + // Valid JSON Numbers, probably should return BigDecimal or BigInteger objects + String str = + "{"+ + "\"numberWithDecimals\":299792.457999999984,"+ + "\"largeNumber\":12345678901234567890,"+ + "\"preciseNumber\":0.2000000000000000111,"+ + "\"largeExponent\":-23.45e2327"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + // Comes back as a double, but loses precision + assertTrue( "numberWithDecimals currently evaluates to double 299792.458", + jsonObject.get( "numberWithDecimals" ).equals( new Double( "299792.458" ) ) ); + Object obj = jsonObject.get( "largeNumber" ); + assertTrue("largeNumber currently evaluates to string", + "12345678901234567890".equals(obj)); + // comes back as a double but loses precision + assertTrue( "preciseNumber currently evaluates to double 0.2", + jsonObject.get( "preciseNumber" ).equals(new Double(0.2))); + obj = jsonObject.get( "largeExponent" ); + assertTrue("largeExponent should currently evaluates as a string", + "-23.45e2327".equals(obj)); + } - // getNames() from empty JSONObject - final String emptyStr = "{}"; - jsonObject = new JSONObject(emptyStr); - assertTrue("empty JSONObject should have null names", - null == JSONObject.getNames(jsonObject)); - - // getNames() from JSONObject - final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," - + "\"stringKey\":\"hello world!\"," + "}"; - jsonObject = new JSONObject(str); - names = JSONObject.getNames(jsonObject); - JSONArray jsonArray = new JSONArray(names); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - List docList = JsonPath.read(doc, "$"); - assertTrue("expected 3 items", docList.size() == 3); - assertTrue( - "expected to find trueKey", - ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); - assertTrue( - "expected to find falseKey", - ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); - assertTrue( - "expected to find stringKey", - ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); - - /** - * getNames() from an enum with properties has an interesting result. It - * returns the enum values, not the selected enum properties - */ - final MyEnumField myEnumField = MyEnumField.VAL1; - names = JSONObject.getNames(myEnumField); - - // validate JSON - jsonArray = new JSONArray(names); - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - docList = JsonPath.read(doc, "$"); - assertTrue("expected 3 items", docList.size() == 3); - assertTrue("expected to find VAL1", - ((List) JsonPath.read(doc, "$[?(@=='VAL1')]")).size() == 1); - assertTrue("expected to find VAL2", - ((List) JsonPath.read(doc, "$[?(@=='VAL2')]")).size() == 1); - assertTrue("expected to find VAL3", - ((List) JsonPath.read(doc, "$[?(@=='VAL3')]")).size() == 1); - - /** - * A bean is also an object. But in order to test the static method - * getNames(), this particular bean needs some public data members. - */ - final MyPublicClass myPublicClass = new MyPublicClass(); - names = JSONObject.getNames(myPublicClass); - - // validate JSON - jsonArray = new JSONArray(names); - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - docList = JsonPath.read(doc, "$"); - assertTrue("expected 2 items", docList.size() == 2); - assertTrue("expected to find publicString", ((List) JsonPath.read( - doc, "$[?(@=='publicString')]")).size() == 1); - assertTrue( - "expected to find publicInt", - ((List) JsonPath.read(doc, "$[?(@=='publicInt')]")).size() == 1); - } - - /** - * Populate a JSONArray from a JSONObject names() method. Confirm that it - * contains the expected names. - */ - @Test - public void jsonObjectNamesToJsonAray() { - final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," - + "\"stringKey\":\"hello world!\"," + "}"; - - final JSONObject jsonObject = new JSONObject(str); - final JSONArray jsonArray = jsonObject.names(); - - // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - assertTrue("expected 3 top level items", - ((List) JsonPath.read(doc, "$")).size() == 3); - assertTrue( - "expected to find trueKey", - ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); - assertTrue( - "expected to find falseKey", - ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); - assertTrue( - "expected to find stringKey", - ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); - } + /** + * This test documents how JSON-Java handles invalid numeric input. + */ + @Test + public void jsonInvalidNumberValues() { + // Number-notations supported by Java and invalid as JSON + String str = + "{"+ + "\"hexNumber\":-0x123,"+ + "\"tooManyZeros\":00,"+ + "\"negativeInfinite\":-Infinity,"+ + "\"negativeNaN\":-NaN,"+ + "\"negativeFraction\":-.01,"+ + "\"tooManyZerosFraction\":00.001,"+ + "\"negativeHexFloat\":-0x1.fffp1,"+ + "\"hexFloat\":0x1.0P-1074,"+ + "\"floatIdentifier\":0.1f,"+ + "\"doubleIdentifier\":0.1d"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + Object obj; + obj = jsonObject.get( "hexNumber" ); + assertFalse( "hexNumber must not be a number (should throw exception!?)", + obj instanceof Number ); + assertTrue("hexNumber currently evaluates to string", + obj.equals("-0x123")); + assertTrue( "tooManyZeros currently evaluates to string", + jsonObject.get( "tooManyZeros" ).equals("00")); + obj = jsonObject.get("negativeInfinite"); + assertTrue( "negativeInfinite currently evaluates to string", + obj.equals("-Infinity")); + obj = jsonObject.get("negativeNaN"); + assertTrue( "negativeNaN currently evaluates to string", + obj.equals("-NaN")); + assertTrue( "negativeFraction currently evaluates to double -0.01", + jsonObject.get( "negativeFraction" ).equals(new Double(-0.01))); + assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", + jsonObject.get( "tooManyZerosFraction" ).equals(new Double(0.001))); + assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", + jsonObject.get( "negativeHexFloat" ).equals(new Double(-3.99951171875))); + assertTrue("hexFloat currently evaluates to double 4.9E-324", + jsonObject.get("hexFloat").equals(new Double(4.9E-324))); + assertTrue("floatIdentifier currently evaluates to double 0.1", + jsonObject.get("floatIdentifier").equals(new Double(0.1))); + assertTrue("doubleIdentifier currently evaluates to double 0.1", + jsonObject.get("doubleIdentifier").equals(new Double(0.1))); + } /** * Tests how JSONObject get[type] handles incorrect types */ @Test public void jsonObjectNonAndWrongValues() { - final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," - + "\"trueStrKey\":\"true\"," + "\"falseStrKey\":\"false\"," - + "\"stringKey\":\"hello world!\"," + "\"intKey\":42," - + "\"intStrKey\":\"43\"," + "\"longKey\":1234567890123456789," - + "\"longStrKey\":\"987654321098765432\"," - + "\"doubleKey\":-23.45e7," + "\"doubleStrKey\":\"00001.000\"," - + "\"arrayKey\":[0,1,2]," - + "\"objectKey\":{\"myKey\":\"myVal\"}" + "}"; - final JSONObject jsonObject = new JSONObject(str); + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"trueStrKey\":\"true\","+ + "\"falseStrKey\":\"false\","+ + "\"stringKey\":\"hello world!\","+ + "\"intKey\":42,"+ + "\"intStrKey\":\"43\","+ + "\"longKey\":1234567890123456789,"+ + "\"longStrKey\":\"987654321098765432\","+ + "\"doubleKey\":-23.45e7,"+ + "\"doubleStrKey\":\"00001.000\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{\"myKey\":\"myVal\"}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); try { jsonObject.getBoolean("nonKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("expecting an exception message", + } catch (JSONException e) { + assertTrue("expecting an exception message", "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); } try { jsonObject.getBoolean("stringKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a Boolean.".equals(e - .getMessage())); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a Boolean.". + equals(e.getMessage())); } try { jsonObject.getString("nonKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); } try { jsonObject.getString("trueKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"trueKey\"] not a string.".equals(e - .getMessage())); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"trueKey\"] not a string.". + equals(e.getMessage())); } try { jsonObject.getDouble("nonKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { + } catch (JSONException e) { assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); } try { jsonObject.getDouble("stringKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { + } catch (JSONException e) { assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.".equals(e - .getMessage())); + "JSONObject[\"stringKey\"] is not a number.". + equals(e.getMessage())); } try { jsonObject.getInt("nonKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { + } catch (JSONException e) { assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); } try { jsonObject.getInt("stringKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not an int.".equals(e - .getMessage())); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not an int.". + equals(e.getMessage())); } try { jsonObject.getLong("nonKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); } try { jsonObject.getLong("stringKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a long.".equals(e - .getMessage())); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a long.". + equals(e.getMessage())); } try { jsonObject.getJSONArray("nonKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); } try { jsonObject.getJSONArray("stringKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONArray.".equals(e - .getMessage())); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONArray.". + equals(e.getMessage())); } try { jsonObject.getJSONObject("nonKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); } try { jsonObject.getJSONObject("stringKey"); assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONObject.".equals(e - .getMessage())); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONObject.". + equals(e.getMessage())); } } /** - * JSON null is not the same as Java null. This test examines the - * differences in how they are handled by JSON-java. + * This test documents an unexpected numeric behavior. + * A double that ends with .0 is parsed, serialized, then + * parsed again. On the second parse, it has become an int. */ @Test - public void jsonObjectNullOperations() { + public void unexpectedDoubleToIntConversion() { + String key30 = "key30"; + String key31 = "key31"; + JSONObject jsonObject = new JSONObject(); + jsonObject.put(key30, new Double(3.0)); + jsonObject.put(key31, new Double(3.1)); + + assertTrue("3.0 should remain a double", + jsonObject.getDouble(key30) == 3); + assertTrue("3.1 should remain a double", + jsonObject.getDouble(key31) == 3.1); + + // turns 3.0 into 3. + String serializedString = jsonObject.toString(); + JSONObject deserialized = new JSONObject(serializedString); + assertTrue("3.0 is now an int", deserialized.get(key30) instanceof Integer); + assertTrue("3.0 can still be interpreted as a double", + deserialized.getDouble(key30) == 3.0); + assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1); + } + + /** + * Document behaviors of big numbers. Includes both JSONObject + * and JSONArray tests + */ + @Test + public void bigNumberOperations() { /** - * The Javadoc for JSONObject.NULL states: "JSONObject.NULL is - * equivalent to the value that JavaScript calls null, whilst Java's - * null is equivalent to the value that JavaScript calls undefined." - * - * Standard ECMA-262 6th Edition / June 2015 (included to help explain - * the javadoc): undefined value: primitive value used when a variable - * has not been assigned a value Undefined type: type whose sole value - * is the undefined value null value: primitive value that represents - * the intentional absence of any object value Null type: type whose - * sole value is the null value Java SE8 language spec (included to help - * explain the javadoc): The Kinds of Types and Values ... There is also - * a special null type, the type of the expression null, which has no - * name. Because the null type has no name, it is impossible to declare - * a variable of the null type or to cast to the null type. The null - * reference is the only possible value of an expression of null type. - * The null reference can always be assigned or cast to any reference - * type. In practice, the programmer can ignore the null type and just - * pretend that null is merely a special literal that can be of any - * reference type. Extensible Markup Language (XML) 1.0 Fifth Edition / - * 26 November 2008 No mention of null ECMA-404 1st Edition / October - * 2013: JSON Text ... These are three literal name tokens: ... null - * - * There seems to be no best practice to follow, it's all about what we - * want the code to do. + * JSONObject tries to parse BigInteger as a bean, but it only has + * one getter, getLowestBitSet(). The value is lost and an unhelpful + * value is stored. This should be fixed. */ + BigInteger bigInteger = new BigInteger("123456789012345678901234567890"); + JSONObject jsonObject = new JSONObject(bigInteger); + Object obj = jsonObject.get("lowestSetBit"); + assertTrue("JSONObject only has 1 value", jsonObject.length() == 1); + assertTrue("JSONObject parses BigInteger as the Integer lowestBitSet", + obj instanceof Integer); + assertTrue("this bigInteger lowestBitSet happens to be 1", + obj.equals(1)); - // add JSONObject.NULL then convert to string in the manner of - // XML.toString() - final JSONObject jsonObjectJONull = new JSONObject(); - Object obj = JSONObject.NULL; - jsonObjectJONull.put("key", obj); - Object value = jsonObjectJONull.opt("key"); - assertTrue("opt() JSONObject.NULL should find JSONObject.NULL", - obj.equals(value)); - value = jsonObjectJONull.get("key"); - assertTrue("get() JSONObject.NULL should find JSONObject.NULL", - obj.equals(value)); - if (value == null) { - value = ""; - } - String string = value instanceof String ? (String) value : null; - assertTrue("XML toString() should convert JSONObject.NULL to null", - string == null); + /** + * JSONObject tries to parse BigDecimal as a bean, but it has + * no getters, The value is lost and no value is stored. + * This should be fixed. + */ + BigDecimal bigDecimal = new BigDecimal( + "123456789012345678901234567890.12345678901234567890123456789"); + jsonObject = new JSONObject(bigDecimal); + assertTrue("large bigDecimal is not stored", jsonObject.length() == 0); - // now try it with null - final JSONObject jsonObjectNull = new JSONObject(); - obj = null; - jsonObjectNull.put("key", obj); - value = jsonObjectNull.opt("key"); - assertTrue("opt() null should find null", value == null); - if (value == null) { - value = ""; - } - string = value instanceof String ? (String) value : null; - assertTrue("should convert null to empty string", "".equals(string)); + /** + * JSONObject put(String, Object) method stores and serializes + * bigInt and bigDec correctly. Nothing needs to change. + */ + 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 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 serializes bigDec correctly", + jsonObject.toString().equals( + "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + + /** + * exercise some exceptions + */ try { - value = jsonObjectNull.get("key"); - assertTrue("get() null should throw exception", false); - } catch (final Exception ignored) { - } + 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)); /** - * XML.toString() then goes on to do something with the value if the key - * val is "content", then value.toString() will be called. This will - * evaluate to "null" for JSONObject.NULL, and the empty string for - * null. But if the key is anything else, then JSONObject.NULL will be - * emitted as null and null will be emitted as "" + * JSONObject.numberToString() works correctly, nothing to change. */ - final String sJONull = XML.toString(jsonObjectJONull); - assertTrue("JSONObject.NULL should emit a null value", - "null".equals(sJONull)); - final String sNull = XML.toString(jsonObjectNull); - assertTrue("null should emit an empty string", "".equals(sNull)); + String str = JSONObject.numberToString(bigInteger); + assertTrue("numberToString() handles bigInteger correctly", + str.equals("123456789012345678901234567890")); + str = JSONObject.numberToString(bigDecimal); + assertTrue("numberToString() handles bigDecimal correctly", + str.equals("123456789012345678901234567890.12345678901234567890123456789")); + + /** + * JSONObject.stringToValue() turns bigInt into an accurate string, + * and rounds bigDec. This incorrect, but users may have come to + * expect this behavior. Change would be marginally better, but + * might inconvenience users. + */ + obj = JSONObject.stringToValue(bigInteger.toString()); + assertTrue("stringToValue() turns bigInteger string into string", + obj instanceof String); + obj = JSONObject.stringToValue(bigDecimal.toString()); + assertTrue("stringToValue() changes bigDecimal string", + !obj.toString().equals(bigDecimal.toString())); + + /** + * wrap() vs put() big number behavior is now the same. + */ + // bigInt map ctor + Map map = new HashMap(); + map.put("bigInt", bigInteger); + jsonObject = new JSONObject(map); + String actualFromMapStr = jsonObject.toString(); + assertTrue("bigInt in map (or array or bean) is a string", + actualFromMapStr.equals( + "{\"bigInt\":123456789012345678901234567890}")); + // bigInt put + jsonObject = new JSONObject(); + jsonObject.put("bigInt", bigInteger); + String actualFromPutStr = jsonObject.toString(); + assertTrue("bigInt from put is a number", + actualFromPutStr.equals( + "{\"bigInt\":123456789012345678901234567890}")); + // bigDec map ctor + map = new HashMap(); + map.put("bigDec", bigDecimal); + jsonObject = new JSONObject(map); + actualFromMapStr = jsonObject.toString(); + assertTrue("bigDec in map (or array or bean) is a bigDec", + actualFromMapStr.equals( + "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + // bigDec put + jsonObject = new JSONObject(); + jsonObject.put("bigDec", bigDecimal); + actualFromPutStr = jsonObject.toString(); + assertTrue("bigDec from put is a number", + actualFromPutStr.equals( + "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + // bigInt,bigDec put + JSONArray jsonArray = new JSONArray(); + jsonArray.put(bigInteger); + jsonArray.put(bigDecimal); + actualFromPutStr = jsonArray.toString(); + assertTrue("bigInt, bigDec from put is a number", + actualFromPutStr.equals( + "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); + assertTrue("getBigInt is bigInt", jsonArray.getBigInteger(0).equals(bigInteger)); + assertTrue("getBigDec is bigDec", jsonArray.getBigDecimal(1).equals(bigDecimal)); + assertTrue("optBigInt is bigInt", jsonArray.optBigInteger(0, BigInteger.ONE).equals(bigInteger)); + assertTrue("optBigDec is bigDec", jsonArray.optBigDecimal(1, BigDecimal.ONE).equals(bigDecimal)); + jsonArray.put(Boolean.TRUE); + try { + jsonArray.getBigInteger(2); + assertTrue("should not be able to get big int", false); + } catch (Exception ignored) {} + try { + jsonArray.getBigDecimal(2); + assertTrue("should not be able to get big dec", false); + } 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)); + + // bigInt,bigDec list ctor + List list = new ArrayList(); + list.add(bigInteger); + list.add(bigDecimal); + jsonArray = new JSONArray(list); + String actualFromListStr = jsonArray.toString(); + assertTrue("bigInt, bigDec in list is a bigInt, bigDec", + actualFromListStr.equals( + "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); + // bigInt bean ctor + MyBigNumberBean myBigNumberBean = mock(MyBigNumberBean.class); + when(myBigNumberBean.getBigInteger()).thenReturn(new BigInteger("123456789012345678901234567890")); + jsonObject = new JSONObject(myBigNumberBean); + String actualFromBeanStr = jsonObject.toString(); + // can't do a full string compare because mockery adds an extra key/value + assertTrue("bigInt from bean ctor is a bigInt", + actualFromBeanStr.contains("123456789012345678901234567890")); + // bigDec bean ctor + myBigNumberBean = mock(MyBigNumberBean.class); + when(myBigNumberBean.getBigDecimal()).thenReturn(new BigDecimal("123456789012345678901234567890.12345678901234567890123456789")); + jsonObject = new JSONObject(myBigNumberBean); + actualFromBeanStr = jsonObject.toString(); + // can't do a full string compare because mockery adds an extra key/value + assertTrue("bigDec from bean ctor is a bigDec", + actualFromBeanStr.contains("123456789012345678901234567890.12345678901234567890123456789")); + // bigInt,bigDec wrap() + obj = JSONObject.wrap(bigInteger); + assertTrue("wrap() returns big num",obj.equals(bigInteger)); + obj = JSONObject.wrap(bigDecimal); + assertTrue("wrap() returns string",obj.equals(bigDecimal)); + } /** - * Exercise JSONObject numberToString() method + * The purpose for the static method getNames() methods are not clear. + * This method is not called from within JSON-Java. Most likely + * uses are to prep names arrays for: + * JSONObject(JSONObject jo, String[] names) + * JSONObject(Object object, String names[]), */ @Test - public void jsonObjectNumberToString() { - String str; - Double dVal; - final Integer iVal = 1; - str = JSONObject.numberToString(iVal); - assertTrue("expected " + iVal + " actual " + str, iVal.toString() - .equals(str)); - dVal = 12.34; - str = JSONObject.numberToString(dVal); - assertTrue("expected " + dVal + " actual " + str, dVal.toString() - .equals(str)); - dVal = 12.34e27; - str = JSONObject.numberToString(dVal); - assertTrue("expected " + dVal + " actual " + str, dVal.toString() - .equals(str)); - // trailing .0 is truncated, so it doesn't quite match toString() - dVal = 5000000.0000000; - str = JSONObject.numberToString(dVal); - assertTrue("expected 5000000 actual " + str, str.equals("5000000")); + public void jsonObjectNames() { + JSONObject jsonObject; + + // getNames() from null JSONObject + assertTrue("null names from null Object", + null == JSONObject.getNames((Object)null)); + + // getNames() from object with no fields + assertTrue("null names from Object with no fields", + null == JSONObject.getNames(new MyJsonString())); + + // getNames from new JSONOjbect + jsonObject = new JSONObject(); + String [] names = JSONObject.getNames(jsonObject); + assertTrue("names should be null", names == null); + + + // getNames() from empty JSONObject + String emptyStr = "{}"; + jsonObject = new JSONObject(emptyStr); + assertTrue("empty JSONObject should have null names", + null == JSONObject.getNames(jsonObject)); + + // getNames() from JSONObject + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "}"; + jsonObject = new JSONObject(str); + names = JSONObject.getNames(jsonObject); + JSONArray jsonArray = new JSONArray(names); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + List docList = JsonPath.read(doc, "$"); + assertTrue("expected 3 items", docList.size() == 3); + assertTrue( + "expected to find trueKey", + ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); + assertTrue( + "expected to find falseKey", + ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); + assertTrue( + "expected to find stringKey", + ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); + + /** + * getNames() from an enum with properties has an interesting result. + * It returns the enum values, not the selected enum properties + */ + MyEnumField myEnumField = MyEnumField.VAL1; + names = JSONObject.getNames(myEnumField); + + // validate JSON + jsonArray = new JSONArray(names); + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + docList = JsonPath.read(doc, "$"); + assertTrue("expected 3 items", docList.size() == 3); + assertTrue( + "expected to find VAL1", + ((List) JsonPath.read(doc, "$[?(@=='VAL1')]")).size() == 1); + assertTrue( + "expected to find VAL2", + ((List) JsonPath.read(doc, "$[?(@=='VAL2')]")).size() == 1); + assertTrue( + "expected to find VAL3", + ((List) JsonPath.read(doc, "$[?(@=='VAL3')]")).size() == 1); + + /** + * A bean is also an object. But in order to test the static + * method getNames(), this particular bean needs some public + * data members. + */ + MyPublicClass myPublicClass = new MyPublicClass(); + names = JSONObject.getNames(myPublicClass); + + // validate JSON + jsonArray = new JSONArray(names); + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + docList = JsonPath.read(doc, "$"); + assertTrue("expected 2 items", docList.size() == 2); + assertTrue( + "expected to find publicString", + ((List) JsonPath.read(doc, "$[?(@=='publicString')]")).size() == 1); + assertTrue( + "expected to find publicInt", + ((List) JsonPath.read(doc, "$[?(@=='publicInt')]")).size() == 1); } /** - * Exercise JSONObject opt(key, default) method + * Populate a JSONArray from an empty JSONObject names() method. + * It should be empty. */ @Test - public void jsonObjectOptDefault() { + public void emptyJsonObjectNamesToJsonAray() { + JSONObject jsonObject = new JSONObject(); + JSONArray jsonArray = jsonObject.names(); + assertTrue("jsonArray should be null", jsonArray == null); + } - final String str = "{\"myKey\": \"myval\"}"; - final JSONObject jsonObject = new JSONObject(str); + /** + * Populate a JSONArray from a JSONObject names() method. + * Confirm that it contains the expected names. + */ + @Test + public void jsonObjectNamesToJsonAray() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "}"; - assertTrue("optBoolean() should return default boolean", - Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE)); - assertTrue("optInt() should return default int", - 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optInt() should return default int", - 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optLong() should return default long", - 42 == jsonObject.optLong("myKey", 42)); - assertTrue("optDouble() should return default double", - 42.3 == jsonObject.optDouble("myKey", 42.3)); - assertTrue("optString() should return default string", - "hi".equals(jsonObject.optString("hiKey", "hi"))); + JSONObject jsonObject = new JSONObject(str); + JSONArray jsonArray = jsonObject.names(); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected to find trueKey", ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); + assertTrue("expected to find falseKey", ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); + assertTrue("expected to find stringKey", ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); } /** - * Explore how JSONObject handles parsing errors. + * Exercise the JSONObject increment() method. */ @Test - public void jsonObjectParsingErrors() { - try { - // does not start with '{' - final String str = "abc"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "A JSONObject text must begin with '{' at 1 [character 2 line 1]" - .equals(e.getMessage())); - } - try { - // does not end with '}' - final String str = "{"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "A JSONObject text must end with '}' at 2 [character 3 line 1]" - .equals(e.getMessage())); - } - try { - // key with no ':' - final String str = "{\"myKey\" = true}"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "Expected a ':' after a key at 10 [character 11 line 1]" - .equals(e.getMessage())); - } - try { - // entries with no ',' separator - final String str = "{\"myKey\":true \"myOtherKey\":false}"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "Expected a ',' or '}' at 15 [character 16 line 1]" - .equals(e.getMessage())); - } - try { - // append to wrong key - final String str = "{\"myKey\":true, \"myOtherKey\":false}"; - final JSONObject jsonObject = new JSONObject(str); - jsonObject.append("myKey", "hello"); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[myKey] is not a JSONArray.".equals(e - .getMessage())); - } - try { - // increment wrong key - final String str = "{\"myKey\":true, \"myOtherKey\":false}"; - final JSONObject jsonObject = new JSONObject(str); - jsonObject.increment("myKey"); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "Unable to increment [\"myKey\"].".equals(e.getMessage())); - } - try { - // invalid key - final String str = "{\"myKey\":true, \"myOtherKey\":false}"; - final JSONObject jsonObject = new JSONObject(str); - jsonObject.get(null); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "Null key.".equals(e.getMessage())); - } - try { - // invalid numberToString() - JSONObject.numberToString((Number) null); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("Expecting an exception message", - "Null pointer".equals(e.getMessage())); - } - try { - // null put key - final JSONObject jsonObject = new JSONObject("{}"); - jsonObject.put(null, 0); - assertTrue("Expected an exception", false); - } catch (final NullPointerException ignored) { - } - try { - // multiple putOnce key - final JSONObject jsonObject = new JSONObject("{}"); - jsonObject.putOnce("hello", "world"); - jsonObject.putOnce("hello", "world!"); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("", true); - } - try { - // test validity of invalid double - JSONObject.testValidity(Double.NaN); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("", true); - } - try { - // test validity of invalid float - JSONObject.testValidity(Float.NEGATIVE_INFINITY); - assertTrue("Expected an exception", false); - } catch (final JSONException e) { - assertTrue("", true); - } + public void jsonObjectIncrement() { + String str = + "{"+ + "\"keyLong\":9999999991,"+ + "\"keyDouble\":1.1"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.increment("keyInt"); + jsonObject.increment("keyInt"); + jsonObject.increment("keyLong"); + jsonObject.increment("keyDouble"); + 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 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 + * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is the + * effect of a float to double conversion and is inherent to the + * shortcomings of the IEEE 754 format, when converting 32-bit into + * double-precision 64-bit. Java type-casts float to double. A 32 bit + * float is type-casted to 64 bit double by simply appending zero-bits + * to the mantissa (and extended the signed exponent by 3 bits.) and + * there is no way to obtain more information than it is stored in the + * 32-bits float. + * + * Like 1/3 cannot be represented as base10 number because it is + * periodically, 1/5 (for example) cannot be represented as base2 number + * since it is periodically in base2 (take a look at + * http://www.h-schmidt.net/FloatConverter/) The same happens to 3.1, + * that decimal number (base10 representation) is periodic in base2 + * representation, therefore appending zero-bits is inaccurate. Only + * repeating the periodically occuring bits (0110) would be a proper + * conversion. However one cannot detect from a 32 bit IEE754 + * representation which bits would "repeat infinitely", since the + * missing bits would not fit into the 32 bit float, i.e. the + * information needed simply is not there! + */ + assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(JsonPath.read(doc, "$.keyFloat"))); + + /** + * float f = 3.1f; double df = (double) f; double d = 3.1d; + * System.out.println + * (Integer.toBinaryString(Float.floatToRawIntBits(f))); + * System.out.println + * (Long.toBinaryString(Double.doubleToRawLongBits(df))); + * System.out.println + * (Long.toBinaryString(Double.doubleToRawLongBits(d))); + * + * - Float: + * seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm + * 1000000010001100110011001100110 + * - Double + * seeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm + * 10000000 10001100110011001100110 + * 100000000001000110011001100110011000000000000000000000000000000 + * 100000000001000110011001100110011001100110011001100110011001101 + */ + + /** + * Examples of well documented but probably unexpected behavior in + * java / with 32-bit float to 64-bit float conversion. + */ + assertFalse("Document unexpected behaviour with explicit type-casting float as double!", (double)0.2f == 0.2d ); + assertFalse("Document unexpected behaviour with implicit type-cast!", 0.2f == 0.2d ); + Double d1 = new Double( 1.1f ); + Double d2 = new Double( "1.1f" ); + assertFalse( "Document implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) ); + + assertTrue( "Correctly converting float to double via base10 (string) representation!", new Double( 3.1d ).equals( new Double( new Float( 3.1f ).toString() ) ) ); + + // Pinpointing the not so obvious "buggy" conversion from float to double in JSONObject + JSONObject jo = new JSONObject(); + jo.put( "bug", 3.1f ); // will call put( String key, double value ) with implicit and "buggy" type-cast from float to double + assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) ); + + JSONObject inc = new JSONObject(); + inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) + assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float ); + inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double! + // this.put(key, (Float) value + 1); + // 1. The (Object)value will be typecasted to (Float)value since it is an instanceof Float actually nothing is done. + // 2. Float instance will be autoboxed into float because the + operator will work on primitives not Objects! + // 3. A float+float operation will be performed and results into a float primitive. + // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method + // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa + assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Double ); + // correct implementation (with change of behavior) would be: + // this.put(key, new Float((Float) value + 1)); + // Probably it would be better to deprecate the method and remove some day, while convenient processing the "payload" is not + // really in the the scope of a JSON-library (IMHO.) + + } + + /** + * Exercise JSONObject numberToString() method + */ + @Test + public void jsonObjectNumberToString() { + String str; + Double dVal; + Integer iVal = 1; + str = JSONObject.numberToString(iVal); + assertTrue("expected "+iVal+" actual "+str, iVal.toString().equals(str)); + dVal = 12.34; + str = JSONObject.numberToString(dVal); + assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + dVal = 12.34e27; + str = JSONObject.numberToString(dVal); + assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + // trailing .0 is truncated, so it doesn't quite match toString() + dVal = 5000000.0000000; + str = JSONObject.numberToString(dVal); + assertTrue("expected 5000000 actual "+str, str.equals("5000000")); } /** @@ -1343,17 +1258,24 @@ public void jsonObjectParsingErrors() { */ @Test public void jsonObjectPut() { - final String expectedStr = "{" + "\"trueKey\":true," - + "\"falseKey\":false," + "\"arrayKey\":[0,1,2]," - + "\"objectKey\":{" + "\"myKey1\":\"myVal1\"," - + "\"myKey2\":\"myVal2\"," + "\"myKey3\":\"myVal3\"," - + "\"myKey4\":\"myVal4\"" + "}" + "}"; - final JSONObject jsonObject = new JSONObject(); + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(); jsonObject.put("trueKey", true); jsonObject.put("falseKey", false); - final Integer[] intArray = { 0, 1, 2 }; + Integer [] intArray = { 0, 1, 2 }; jsonObject.put("arrayKey", Arrays.asList(intArray)); - final Map myMap = new HashMap(); + Map myMap = new HashMap(); myMap.put("myKey1", "myVal1"); myMap.put("myKey2", "myVal2"); myMap.put("myKey3", "myVal3"); @@ -1361,608 +1283,197 @@ public void jsonObjectPut() { jsonObject.put("objectKey", myMap); // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 4 top level items", - ((Map) JsonPath.read(doc, "$")).size() == 4); - assertTrue("expected true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected 3 arrayKey items", - ((List) JsonPath.read(doc, "$.arrayKey")).size() == 3); - assertTrue("expected 0", - Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); - assertTrue("expected 1", - Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); - assertTrue("expected 2", - Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); - assertTrue("expected 4 objectKey items", - ((Map) JsonPath.read(doc, "$.objectKey")).size() == 4); - assertTrue("expected myVal1", - "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); - assertTrue("expected myVal2", - "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); - assertTrue("expected myVal3", - "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); - assertTrue("expected myVal4", - "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); + assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); + assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); + assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); + assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); + assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); + assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); jsonObject.remove("trueKey"); - final JSONObject expectedJsonObject = new JSONObject(expectedStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); assertTrue("unequal jsonObjects should not be similar", !jsonObject.similar(expectedJsonObject)); assertTrue("jsonObject should not be similar to jsonArray", !jsonObject.similar(new JSONArray())); - final String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}"; - final String bCompareValueStr = "{\"a\":\"notAval\",\"b\":true}"; - final JSONObject aCompareValueJsonObject = new JSONObject( - aCompareValueStr); - final JSONObject bCompareValueJsonObject = new JSONObject( - bCompareValueStr); + String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}"; + String bCompareValueStr = "{\"a\":\"notAval\",\"b\":true}"; + JSONObject aCompareValueJsonObject = new JSONObject(aCompareValueStr); + JSONObject bCompareValueJsonObject = new JSONObject(bCompareValueStr); assertTrue("different values should not be similar", !aCompareValueJsonObject.similar(bCompareValueJsonObject)); - final String aCompareObjectStr = "{\"a\":\"aval\",\"b\":{}}"; - final String bCompareObjectStr = "{\"a\":\"aval\",\"b\":true}"; - final JSONObject aCompareObjectJsonObject = new JSONObject( - aCompareObjectStr); - final JSONObject bCompareObjectJsonObject = new JSONObject( - bCompareObjectStr); + String aCompareObjectStr = "{\"a\":\"aval\",\"b\":{}}"; + String bCompareObjectStr = "{\"a\":\"aval\",\"b\":true}"; + JSONObject aCompareObjectJsonObject = new JSONObject(aCompareObjectStr); + JSONObject bCompareObjectJsonObject = new JSONObject(bCompareObjectStr); assertTrue("different nested JSONObjects should not be similar", !aCompareObjectJsonObject.similar(bCompareObjectJsonObject)); - final String aCompareArrayStr = "{\"a\":\"aval\",\"b\":[]}"; - final String bCompareArrayStr = "{\"a\":\"aval\",\"b\":true}"; - final JSONObject aCompareArrayJsonObject = new JSONObject( - aCompareArrayStr); - final JSONObject bCompareArrayJsonObject = new JSONObject( - bCompareArrayStr); + String aCompareArrayStr = "{\"a\":\"aval\",\"b\":[]}"; + String bCompareArrayStr = "{\"a\":\"aval\",\"b\":true}"; + JSONObject aCompareArrayJsonObject = new JSONObject(aCompareArrayStr); + JSONObject bCompareArrayJsonObject = new JSONObject(bCompareArrayStr); assertTrue("different nested JSONArrays should not be similar", !aCompareArrayJsonObject.similar(bCompareArrayJsonObject)); } /** - * Confirm behavior when JSONObject put(key, null object) is called + * Exercise JSONObject toString() method */ @Test - public void jsonObjectputNull() { - - // put null should remove the item. - final String str = "{\"myKey\": \"myval\"}"; - final JSONObject jsonObjectRemove = new JSONObject(str); - jsonObjectRemove.remove("myKey"); - - final JSONObject jsonObjectPutNull = new JSONObject(str); - jsonObjectPutNull.put("myKey", (Object) null); + public void jsonObjectToString() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); // validate JSON - assertTrue("jsonObject should be empty", jsonObjectRemove.length() == 0 - && jsonObjectPutNull.length() == 0); - } - - /** - * Confirm behavior when putOnce() is called with null parameters - */ - @Test - public void jsonObjectPutOnceNull() { - final JSONObject jsonObject = new JSONObject(); - jsonObject.putOnce(null, null); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); - } - - /** - * Exercise JSONObject quote() method This purpose of quote() is to ensure - * that for strings with embedded quotes, the quotes are properly escaped. - */ - @Test - public void jsonObjectQuote() { - String str; - str = ""; - String quotedStr; - quotedStr = JSONObject.quote(str); - assertTrue("quote() expected escaped quotes, found " + quotedStr, - "\"\"".equals(quotedStr)); - str = "\"\""; - quotedStr = JSONObject.quote(str); - assertTrue("quote() expected escaped quotes, found " + quotedStr, - "\"\\\"\\\"\"".equals(quotedStr)); - str = ")(JsonPath.read(doc, "$"))).size() == 4); + assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); + assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); + assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); + assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); + assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); + assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); + assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); } /** - * Exercise JSONObject toString() method + * Explores how JSONObject handles maps. Insert a string/string map + * as a value in a JSONObject. It will remain a map. Convert the + * JSONObject to string, then create a new JSONObject from the string. + * In the new JSONObject, the value will be stored as a nested JSONObject. + * Confirm that map and nested JSONObject have the same contents. */ @Test - public void jsonObjectToString() { - final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," - + "\"arrayKey\":[0,1,2]," + "\"objectKey\":{" - + "\"myKey1\":\"myVal1\"," + "\"myKey2\":\"myVal2\"," - + "\"myKey3\":\"myVal3\"," + "\"myKey4\":\"myVal4\"" + "}" - + "}"; - final JSONObject jsonObject = new JSONObject(str); + public void jsonObjectToStringSuppressWarningOnCastToMap() { + JSONObject jsonObject = new JSONObject(); + Map map = new HashMap<>(); + map.put("abc", "def"); + jsonObject.put("key", map); // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 4 top level items", - ((Map) JsonPath.read(doc, "$")).size() == 4); - assertTrue("expected true", - Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected false", - Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected 3 arrayKey items", - ((List) JsonPath.read(doc, "$.arrayKey")).size() == 3); - assertTrue("expected 0", - Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); - assertTrue("expected 1", - Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); - assertTrue("expected 2", - Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); - assertTrue("expected 4 objectKey items", - ((Map) JsonPath.read(doc, "$.objectKey")).size() == 4); - assertTrue("expected myVal1", - "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); - assertTrue("expected myVal2", - "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); - assertTrue("expected myVal3", - "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); - assertTrue("expected myVal4", - "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 1 key item", ((Map)(JsonPath.read(doc, "$.key"))).size() == 1); + assertTrue("expected def", "def".equals(JsonPath.read(doc, "$.key.abc"))); } /** * Explores how JSONObject handles collections. Insert a string collection - * as a value in a JSONObject. It will remain a collection. Convert the - * JSONObject to string, then create a new JSONObject from the string. In - * the new JSONObject, the value will be stored as a nested JSONArray. + * as a value in a JSONObject. It will remain a collection. Convert the + * JSONObject to string, then create a new JSONObject from the string. + * In the new JSONObject, the value will be stored as a nested JSONArray. * Confirm that collection and nested JSONArray have the same contents. */ @Test public void jsonObjectToStringSuppressWarningOnCastToCollection() { - final JSONObject jsonObject = new JSONObject(); - final Collection collection = new ArrayList(); + JSONObject jsonObject = new JSONObject(); + Collection collection = new ArrayList(); collection.add("abc"); // ArrayList will be added as an object jsonObject.put("key", collection); // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 1 top level item", - ((Map) JsonPath.read(doc, "$")).size() == 1); - assertTrue("expected 1 key item", - ((List) JsonPath.read(doc, "$.key")).size() == 1); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 1 key item", ((List)(JsonPath.read(doc, "$.key"))).size() == 1); assertTrue("expected abc", "abc".equals(JsonPath.read(doc, "$.key[0]"))); } - /** - * Explores how JSONObject handles maps. Insert a string/string map as a - * value in a JSONObject. It will remain a map. Convert the JSONObject to - * string, then create a new JSONObject from the string. In the new - * JSONObject, the value will be stored as a nested JSONObject. Confirm that - * map and nested JSONObject have the same contents. - */ - @Test - public void jsonObjectToStringSuppressWarningOnCastToMap() { - final JSONObject jsonObject = new JSONObject(); - final Map map = new HashMap<>(); - map.put("abc", "def"); - jsonObject.put("key", map); - - // validate JSON - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonObject.toString()); - assertTrue("expected 1 top level item", - ((Map) JsonPath.read(doc, "$")).size() == 1); - assertTrue("expected 1 key item", - ((Map) JsonPath.read(doc, "$.key")).size() == 1); - assertTrue("expected def", - "def".equals(JsonPath.read(doc, "$.key.abc"))); - } - - /** - * Exercise some JSONObject get[type] and opt[type] methods - */ - @Test - public void jsonObjectValues() { - final String str = "{" + "\"trueKey\":true," + "\"falseKey\":false," - + "\"trueStrKey\":\"true\"," + "\"falseStrKey\":\"false\"," - + "\"stringKey\":\"hello world!\"," + "\"intKey\":42," - + "\"intStrKey\":\"43\"," + "\"longKey\":1234567890123456789," - + "\"longStrKey\":\"987654321098765432\"," - + "\"doubleKey\":-23.45e7," + "\"doubleStrKey\":\"00001.000\"," - + "\"arrayKey\":[0,1,2]," - + "\"objectKey\":{\"myKey\":\"myVal\"}" + "}"; - final JSONObject jsonObject = new JSONObject(str); - assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); - assertTrue("opt trueKey should be true", - jsonObject.optBoolean("trueKey")); - assertTrue("falseKey should be false", - !jsonObject.getBoolean("falseKey")); - assertTrue("trueStrKey should be true", - jsonObject.getBoolean("trueStrKey")); - assertTrue("trueStrKey should be true", - jsonObject.optBoolean("trueStrKey")); - assertTrue("falseStrKey should be false", - !jsonObject.getBoolean("falseStrKey")); - assertTrue("stringKey should be string", - jsonObject.getString("stringKey").equals("hello world!")); - assertTrue("doubleKey should be double", - jsonObject.getDouble("doubleKey") == -23.45e7); - assertTrue("doubleStrKey should be double", - jsonObject.getDouble("doubleStrKey") == 1); - 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("intKey should be int", jsonObject.optInt("intKey") == 42); - assertTrue("opt intKey should be int", - jsonObject.optInt("intKey", 0) == 42); - assertTrue("opt intKey with default should be int", - jsonObject.getInt("intKey") == 42); - assertTrue("intStrKey should be int", - jsonObject.getInt("intStrKey") == 43); - assertTrue("longKey should be long", - jsonObject.getLong("longKey") == 1234567890123456789L); - assertTrue("opt longKey should be long", - jsonObject.optLong("longKey") == 1234567890123456789L); - assertTrue("opt longKey with default should be long", - jsonObject.optLong("longKey", 0) == 1234567890123456789L); - assertTrue("longStrKey should be long", - jsonObject.getLong("longStrKey") == 987654321098765432L); - assertTrue("xKey should not exist", jsonObject.isNull("xKey")); - assertTrue("stringKey should exist", jsonObject.has("stringKey")); - assertTrue("opt stringKey should string", - jsonObject.optString("stringKey").equals("hello world!")); - assertTrue("opt stringKey with default should string", jsonObject - .optString("stringKey", "not found").equals("hello world!")); - JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); - assertTrue("arrayKey should be JSONArray", jsonArray.getInt(0) == 0 - && jsonArray.getInt(1) == 1 && jsonArray.getInt(2) == 2); - jsonArray = jsonObject.optJSONArray("arrayKey"); - assertTrue("opt arrayKey should be JSONArray", jsonArray.getInt(0) == 0 - && jsonArray.getInt(1) == 1 && jsonArray.getInt(2) == 2); - final JSONObject jsonObjectInner = jsonObject - .getJSONObject("objectKey"); - assertTrue("objectKey should be JSONObject", - jsonObjectInner.get("myKey").equals("myVal")); - } - - /** - * This test documents numeric values which could be numerically handled as - * BigDecimal or BigInteger. It helps determine what outputs will change if - * those types are supported. - */ - @Test - public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() { - // Valid JSON Numbers, probably should return BigDecimal or BigInteger - // objects - final String str = "{" + "\"numberWithDecimals\":299792.457999999984," - + "\"largeNumber\":12345678901234567890," - + "\"preciseNumber\":0.2000000000000000111," - + "\"largeExponent\":-23.45e2327" + "}"; - final JSONObject jsonObject = new JSONObject(str); - // Comes back as a double, but loses precision - assertTrue( - "numberWithDecimals currently evaluates to double 299792.458", - jsonObject.get("numberWithDecimals").equals( - new Double("299792.458"))); - Object obj = jsonObject.get("largeNumber"); - assertTrue("largeNumber currently evaluates to string", - "12345678901234567890".equals(obj)); - // comes back as a double but loses precision - assertTrue("preciseNumber currently evaluates to double 0.2", - jsonObject.get("preciseNumber").equals(new Double(0.2))); - obj = jsonObject.get("largeExponent"); - assertTrue("largeExponent should currently evaluates as a string", - "-23.45e2327".equals(obj)); - } - - /** - * Confirm behavior when JSONObject stringToValue() is called for an empty - * string - */ - @Test - public void stringToValue() { - final String str = ""; - final String valueStr = (String) JSONObject.stringToValue(str); - assertTrue("stringToValue() expected empty String, found " + valueStr, - "".equals(valueStr)); - } - - /** - * Check whether JSONObject handles large or high precision numbers - * correctly - */ - @Test - public void stringToValueNumbersTest() { - assertTrue("-0 Should be a Double!", - JSONObject.stringToValue("-0") instanceof Double); - assertTrue("-0 Should be a Double!", - JSONObject.stringToValue("-0.0") instanceof Double); - assertTrue("'-' Should be a String!", - JSONObject.stringToValue("-") instanceof String); - assertTrue("0.2 should be a Double!", - JSONObject.stringToValue("0.2") instanceof Double); - assertTrue( - "Doubles should be Doubles, even when incorrectly converting floats!", - JSONObject.stringToValue(new Double("0.2f").toString()) instanceof Double); - /** - * This test documents a need for BigDecimal conversion. - */ - final Object obj = JSONObject.stringToValue("299792.457999999984"); - assertTrue( - "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!", - obj.equals(new Double(299792.458))); - assertTrue("1 should be an Integer!", - JSONObject.stringToValue("1") instanceof Integer); - assertTrue("Integer.MAX_VALUE should still be an Integer!", - JSONObject.stringToValue(new Integer(Integer.MAX_VALUE) - .toString()) instanceof Integer); - assertTrue("Large integers should be a Long!", - JSONObject.stringToValue(new Long(Long - .sum(Integer.MAX_VALUE, 1)).toString()) instanceof Long); - assertTrue( - "Long.MAX_VALUE should still be an Integer!", - JSONObject.stringToValue(new Long(Long.MAX_VALUE).toString()) instanceof Long); - - final String str = new BigInteger(new Long(Long.MAX_VALUE).toString()) - .add(BigInteger.ONE).toString(); - assertTrue("Really large integers currently evaluate to string", - JSONObject.stringToValue(str).equals("9223372036854775808")); - } - - /** - * Confirm behavior when toJSONArray is called with a null value - */ - @Test - public void toJSONArray() { - assertTrue("toJSONArray() with null names should be null", - null == new JSONObject().toJSONArray(null)); - } - - /** - * This test documents an unexpected numeric behavior. A double that ends - * with .0 is parsed, serialized, then parsed again. On the second parse, it - * has become an int. - */ - @Test - public void unexpectedDoubleToIntConversion() { - final String key30 = "key30"; - final String key31 = "key31"; - final JSONObject jsonObject = new JSONObject(); - jsonObject.put(key30, new Double(3.0)); - jsonObject.put(key31, new Double(3.1)); - - assertTrue("3.0 should remain a double", - jsonObject.getDouble(key30) == 3); - assertTrue("3.1 should remain a double", - jsonObject.getDouble(key31) == 3.1); - - // turns 3.0 into 3. - final String serializedString = jsonObject.toString(); - final JSONObject deserialized = new JSONObject(serializedString); - assertTrue("3.0 is now an int", - deserialized.get(key30) instanceof Integer); - assertTrue("3.0 can still be interpreted as a double", - deserialized.getDouble(key30) == 3.0); - assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1); - } - /** * Exercises the JSONObject.valueToString() method for various types */ @Test public void valueToString() { - + assertTrue("null valueToString() incorrect", "null".equals(JSONObject.valueToString(null))); - final MyJsonString jsonString = new MyJsonString(); + MyJsonString jsonString = new MyJsonString(); assertTrue("jsonstring valueToString() incorrect", "my string".equals(JSONObject.valueToString(jsonString))); assertTrue("boolean valueToString() incorrect", "true".equals(JSONObject.valueToString(Boolean.TRUE))); - assertTrue("non-numeric double", "null".equals(JSONObject - .doubleToString(Double.POSITIVE_INFINITY))); - final String jsonObjectStr = "{" + "\"key1\":\"val1\"," - + "\"key2\":\"val2\"," + "\"key3\":\"val3\"" + "}"; - final JSONObject jsonObject = new JSONObject(jsonObjectStr); - assertTrue("jsonObject valueToString() incorrect", JSONObject - .valueToString(jsonObject).equals(jsonObject.toString())); - final String jsonArrayStr = "[1,2,3]"; - final JSONArray jsonArray = new JSONArray(jsonArrayStr); - assertTrue("jsonArra valueToString() incorrect", JSONObject - .valueToString(jsonArray).equals(jsonArray.toString())); - final Map map = new HashMap(); + assertTrue("non-numeric double", + "null".equals(JSONObject.doubleToString(Double.POSITIVE_INFINITY))); + String jsonObjectStr = + "{"+ + "\"key1\":\"val1\","+ + "\"key2\":\"val2\","+ + "\"key3\":\"val3\""+ + "}"; + JSONObject jsonObject = new JSONObject(jsonObjectStr); + assertTrue("jsonObject valueToString() incorrect", + JSONObject.valueToString(jsonObject).equals(jsonObject.toString())); + String jsonArrayStr = + "[1,2,3]"; + JSONArray jsonArray = new JSONArray(jsonArrayStr); + assertTrue("jsonArra valueToString() incorrect", + JSONObject.valueToString(jsonArray).equals(jsonArray.toString())); + Map map = new HashMap(); map.put("key1", "val1"); map.put("key2", "val2"); map.put("key3", "val3"); - assertTrue("map valueToString() incorrect", jsonObject.toString() - .equals(JSONObject.valueToString(map))); - final Collection collection = new ArrayList(); - collection.add(new Integer(1)); - collection.add(new Integer(2)); - collection.add(new Integer(3)); - assertTrue( - "collection valueToString() expected: " + jsonArray.toString() - + " actual: " + JSONObject.valueToString(collection), - jsonArray.toString().equals( - JSONObject.valueToString(collection))); - final Integer[] array = { new Integer(1), new Integer(2), - new Integer(3) }; - assertTrue("array valueToString() incorrect", jsonArray.toString() - .equals(JSONObject.valueToString(array))); - } - - /** - * Confirm that https://github.com/douglascrockford/JSON-java/issues/167 is - * fixed. The following code was throwing a ClassCastException in the - * JSONObject(Map) constructor - */ - @Test - public void valueToStringConfirmException() { - final Map myMap = new HashMap(); - myMap.put(1, "myValue"); - // this is the test, it should not throw an exception - final String str = JSONObject.valueToString(myMap); - // confirm result, just in case - final Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(str); - assertTrue("expected 1 top level item", - ((Map) JsonPath.read(doc, "$")).size() == 1); - assertTrue("expected myValue", - "myValue".equals(JsonPath.read(doc, "$.1"))); - } - - /** - * Verifies that the constructor has backwards compatability with RAW types - * pre-java5. - */ - @Test - public void verifyConstructor() { - - final JSONObject expected = new JSONObject("{\"myKey\":10}"); - - @SuppressWarnings("rawtypes") - final Map myRawC = Collections.singletonMap("myKey", - Integer.valueOf(10)); - final JSONObject jaRaw = new JSONObject(myRawC); - - final Map myCStrObj = Collections.singletonMap("myKey", - (Object) Integer.valueOf(10)); - final JSONObject jaStrObj = new JSONObject(myCStrObj); - - final Map myCStrInt = Collections.singletonMap( - "myKey", Integer.valueOf(10)); - final JSONObject jaStrInt = new JSONObject(myCStrInt); - - final Map myCObjObj = Collections.singletonMap((Object) "myKey", - (Object) Integer.valueOf(10)); - final JSONObject jaObjObj = new JSONObject(myCObjObj); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrInt)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObjObj)); - } - - /** - * Verifies that the put Collection has backwards compatability with RAW - * types pre-java5. - */ - @Test - public void verifyPutCollection() { - - final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); - - @SuppressWarnings("rawtypes") - final Collection myRawC = Collections.singleton(Integer.valueOf(10)); - final JSONObject jaRaw = new JSONObject(); - jaRaw.put("myCollection", myRawC); - - final Collection myCObj = Collections - .singleton((Object) Integer.valueOf(10)); - final JSONObject jaObj = new JSONObject(); - jaObj.put("myCollection", myCObj); - - final Collection myCInt = Collections.singleton(Integer - .valueOf(10)); - final JSONObject jaInt = new JSONObject(); - jaInt.put("myCollection", myCInt); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaInt)); + assertTrue("map valueToString() incorrect", + jsonObject.toString().equals(JSONObject.valueToString(map))); + Collection collection = new ArrayList(); + collection.add(new Integer(1)); + collection.add(new Integer(2)); + collection.add(new Integer(3)); + assertTrue("collection valueToString() expected: "+ + jsonArray.toString()+ " actual: "+ + JSONObject.valueToString(collection), + jsonArray.toString().equals(JSONObject.valueToString(collection))); + Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + assertTrue("array valueToString() incorrect", + jsonArray.toString().equals(JSONObject.valueToString(array))); } /** - * Verifies that the put Map has backwards compatability with RAW types - * pre-java5. + * Confirm that https://github.com/douglascrockford/JSON-java/issues/167 is fixed. + * The following code was throwing a ClassCastException in the + * JSONObject(Map) constructor */ @Test - public void verifyPutMap() { - - final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); - - @SuppressWarnings("rawtypes") - final Map myRawC = Collections.singletonMap("myKey", - Integer.valueOf(10)); - final JSONObject jaRaw = new JSONObject(); - jaRaw.put("myMap", myRawC); - - final Map myCStrObj = Collections.singletonMap("myKey", - (Object) Integer.valueOf(10)); - final JSONObject jaStrObj = new JSONObject(); - jaStrObj.put("myMap", myCStrObj); - - final Map myCStrInt = Collections.singletonMap( - "myKey", Integer.valueOf(10)); - final JSONObject jaStrInt = new JSONObject(); - jaStrInt.put("myMap", myCStrInt); - - final Map myCObjObj = Collections.singletonMap((Object) "myKey", - (Object) Integer.valueOf(10)); - final JSONObject jaObjObj = new JSONObject(); - jaObjObj.put("myMap", myCObjObj); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrInt)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObjObj)); + public void valueToStringConfirmException() { + Map myMap = new HashMap(); + myMap.put(1, "myValue"); + // this is the test, it should not throw an exception + String str = JSONObject.valueToString(myMap); + // confirm result, just in case + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(str); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected myValue", "myValue".equals(JsonPath.read(doc, "$.1"))); } /** - * Exercise the JSONObject wrap() method. Sometimes wrap() will change the - * object being wrapped, other times not. The purpose of wrap() is to ensure - * the value is packaged in a way that is compatible with how a JSONObject - * value or JSONArray value is supposed to be stored. + * Exercise the JSONObject wrap() method. Sometimes wrap() will change + * the object being wrapped, other times not. The purpose of wrap() is + * to ensure the value is packaged in a way that is compatible with how + * a JSONObject value or JSONArray value is supposed to be stored. */ @Test public void wrapObject() { @@ -1971,105 +1482,421 @@ public void wrapObject() { JSONObject.NULL == JSONObject.wrap(null)); // wrap(Integer) returns Integer - final Integer in = new Integer(1); - assertTrue("Integer wrap() incorrect", in == JSONObject.wrap(in)); + Integer in = new Integer(1); + assertTrue("Integer wrap() incorrect", + in == JSONObject.wrap(in)); /** * This test is to document the preferred behavior if BigDecimal is - * supported. Previously bd returned as a string, since it is recognized - * as being a Java package class. Now with explicit support for big - * numbers, it remains a BigDecimal + * supported. Previously bd returned as a string, since it + * is recognized as being a Java package class. Now with explicit + * support for big numbers, it remains a BigDecimal */ - final Object bdWrap = JSONObject.wrap(BigDecimal.ONE); + Object bdWrap = JSONObject.wrap(BigDecimal.ONE); assertTrue("BigDecimal.ONE evaluates to ONE", bdWrap.equals(BigDecimal.ONE)); // wrap JSONObject returns JSONObject - final String jsonObjectStr = "{" + "\"key1\":\"val1\"," - + "\"key2\":\"val2\"," + "\"key3\":\"val3\"" + "}"; - final JSONObject jsonObject = new JSONObject(jsonObjectStr); + String jsonObjectStr = + "{"+ + "\"key1\":\"val1\","+ + "\"key2\":\"val2\","+ + "\"key3\":\"val3\""+ + "}"; + JSONObject jsonObject = new JSONObject(jsonObjectStr); assertTrue("JSONObject wrap() incorrect", jsonObject == JSONObject.wrap(jsonObject)); // wrap collection returns JSONArray - final Collection collection = new ArrayList(); + Collection collection = new ArrayList(); collection.add(new Integer(1)); collection.add(new Integer(2)); collection.add(new Integer(3)); - final JSONArray jsonArray = (JSONArray) JSONObject.wrap(collection); + JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection)); // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - assertTrue("expected 3 top level items", - ((List) JsonPath.read(doc, "$")).size() == 3); - assertTrue("expected 1", - Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", - Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", - Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // wrap Array returns JSONArray - final Integer[] array = { new Integer(1), new Integer(2), - new Integer(3) }; - final JSONArray integerArrayJsonArray = (JSONArray) JSONObject - .wrap(array); + Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - assertTrue("expected 3 top level items", - ((List) JsonPath.read(doc, "$")).size() == 3); - assertTrue("expected 1", - Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", - Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", - Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(integerArrayJsonArray.toString()); - assertTrue("expected 3 top level items", - ((List) JsonPath.read(doc, "$")).size() == 3); - assertTrue("expected 1", - Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", - Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", - Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); + assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); // wrap map returns JSONObject - final Map map = new HashMap(); + Map map = new HashMap(); map.put("key1", "val1"); map.put("key2", "val2"); map.put("key3", "val3"); - final JSONObject mapJsonObject = (JSONObject) JSONObject.wrap(map); + JSONObject mapJsonObject = (JSONObject) (JSONObject.wrap(map)); // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(mapJsonObject.toString()); - assertTrue("expected 3 top level items", - ((Map) JsonPath.read(doc, "$")).size() == 3); + doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString()); + assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); 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"))); } + /** + * Explore how JSONObject handles parsing errors. + */ + @Test + public void jsonObjectParsingErrors() { + try { + // does not start with '{' + String str = "abc"; + new JSONObject(str); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "A JSONObject text must begin with '{' at 1 [character 2 line 1]". + equals(e.getMessage())); + } + try { + // does not end with '}' + String str = "{"; + new JSONObject(str); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "A JSONObject text must end with '}' at 2 [character 3 line 1]". + equals(e.getMessage())); + } + try { + // key with no ':' + String str = "{\"myKey\" = true}"; + new JSONObject(str); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Expected a ':' after a key at 10 [character 11 line 1]". + equals(e.getMessage())); + } + try { + // entries with no ',' separator + String str = "{\"myKey\":true \"myOtherKey\":false}"; + new JSONObject(str); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Expected a ',' or '}' at 15 [character 16 line 1]". + equals(e.getMessage())); + } + try { + // append to wrong key + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.append("myKey", "hello"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[myKey] is not a JSONArray.". + equals(e.getMessage())); + } + try { + // increment wrong key + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.increment("myKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Unable to increment [\"myKey\"].". + equals(e.getMessage())); + } + try { + // invalid key + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.get(null); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Null key.". + equals(e.getMessage())); + } + try { + // invalid numberToString() + JSONObject.numberToString((Number)null); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Null pointer". + equals(e.getMessage())); + } + try { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0); + assertTrue("Expected an exception", false); + } catch (NullPointerException ignored) { + } + try { + // multiple putOnce key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.putOnce("hello", "world"); + jsonObject.putOnce("hello", "world!"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("", true); + } + try { + // test validity of invalid double + JSONObject.testValidity(Double.NaN); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("", true); + } + try { + // test validity of invalid float + JSONObject.testValidity(Float.NEGATIVE_INFINITY); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("", true); + } + } + + /** + * Confirm behavior when putOnce() is called with null parameters + */ + @Test + public void jsonObjectPutOnceNull() { + JSONObject jsonObject = new JSONObject(); + jsonObject.putOnce(null, null); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + /** + * Exercise JSONObject opt(key, default) method + */ + @Test + public void jsonObjectOptDefault() { + + String str = "{\"myKey\": \"myval\"}"; + JSONObject jsonObject = new JSONObject(str); + + assertTrue("optBoolean() should return default boolean", + Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optLong() should return default long", + 42 == jsonObject.optLong("myKey", 42)); + assertTrue("optDouble() should return default double", + 42.3 == jsonObject.optDouble("myKey", 42.3)); + assertTrue("optString() should return default string", + "hi".equals(jsonObject.optString("hiKey", "hi"))); + } + + /** + * Confirm behavior when JSONObject put(key, null object) is called + */ + @Test + public void jsonObjectputNull() { + + // put null should remove the item. + String str = "{\"myKey\": \"myval\"}"; + JSONObject jsonObjectRemove = new JSONObject(str); + jsonObjectRemove.remove("myKey"); + + JSONObject jsonObjectPutNull = new JSONObject(str); + jsonObjectPutNull.put("myKey", (Object) null); + + // validate JSON + assertTrue("jsonObject should be empty", jsonObjectRemove.length() == 0 + && jsonObjectPutNull.length() == 0); + } + + /** + * Exercise JSONObject quote() method + * This purpose of quote() is to ensure that for strings with embedded + * quotes, the quotes are properly escaped. + */ + @Test + public void jsonObjectQuote() { + String str; + str = ""; + String quotedStr; + quotedStr = JSONObject.quote(str); + assertTrue("quote() expected escaped quotes, found "+quotedStr, + "\"\"".equals(quotedStr)); + str = "\"\""; + quotedStr = JSONObject.quote(str); + assertTrue("quote() expected escaped quotes, found "+quotedStr, + "\"\\\"\\\"\"".equals(quotedStr)); + str = "null and null will be emitted as "" + */ + String sJONull = XML.toString(jsonObjectJONull); + assertTrue("JSONObject.NULL should emit a null value", + "null".equals(sJONull)); + String sNull = XML.toString(jsonObjectNull); + assertTrue("null should emit an empty string", "".equals(sNull)); + } } From 974c09b22a762304cd2ed17a3f73d20f3a0a6c08 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 27 Jan 2016 15:02:37 -0500 Subject: [PATCH 193/315] Fixes typo in assert --- src/test/org/json/junit/JSONObjectTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/org/json/junit/JSONObjectTest.java b/src/test/org/json/junit/JSONObjectTest.java index 5eedf1a48..869fe466e 100644 --- a/src/test/org/json/junit/JSONObjectTest.java +++ b/src/test/org/json/junit/JSONObjectTest.java @@ -520,7 +520,7 @@ public void jsonObjectValues() { @Test public void stringToValueNumbersTest() { assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0") instanceof Double); - assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double); + assertTrue("-0.0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double); assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String); assertTrue( "0.2 should be a Double!", JSONObject.stringToValue( "0.2" ) instanceof Double ); From 9c47ba299d5f20aee73721bb4d4d0a6c908cedc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Mon, 18 Apr 2016 21:49:14 +0200 Subject: [PATCH 194/315] initial test for JSONPointer class --- src/test/org/json/junit/JSONPointerTest.java | 78 ++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/test/org/json/junit/JSONPointerTest.java diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java new file mode 100644 index 000000000..221f03507 --- /dev/null +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -0,0 +1,78 @@ +package org.json.junit; + +import static org.junit.Assert.assertSame; + +import org.json.JSONObject; +import org.json.JSONPointer; +import org.json.JSONPointerException; +import org.junit.Test; + +public class JSONPointerTest { + + private static final JSONObject document = new JSONObject("{" + + "\"foo\": [\"bar\", \"baz\"], " + + "\"\": 0," + + "\"a/b\": 1," + + "\"c%d\": 2," + + "\"e^f\": 3," + + "\"g|h\": 4," + "\"i\\\\j\": 5," + + "\"k\\\"l\": 6," + + "\" \": 7," + + "\"m~n\": 8" + + "}"); + + private Object query(String pointer) { + return new JSONPointer(pointer).queryFrom(document); + } + + @Test + public void emptyPointer() { + assertSame(document, query("")); + } + + @Test(expected = NullPointerException.class) + public void nullPointer() { + new JSONPointer(null); + } + + @Test + public void objectPropertyQuery() { + assertSame(document.get("foo"), query("/foo")); + } + + @Test + public void arrayIndexQuery() { + assertSame(document.getJSONArray("foo").get(0), query("/foo/0")); + } + + @Test(expected = JSONPointerException.class) + public void stringPropOfArrayFailure() { + query("/foo/bar"); + } + + @Test + public void queryByEmptyKey() { + assertSame(document.get(""), query("/")); + } + + @Test + public void slashEscaping() { + assertSame(document.get("a/b"), query("/a~1b")); + } + + @Test + public void tildeEscaping() { + assertSame(document.get("m~n"), query("/m~0n")); + } + + @Test + public void uriFragmentNotation() { + assertSame(document.get("foo"), query("#/foo")); + } + + @Test(expected = IllegalArgumentException.class) + public void syntaxError() { + new JSONPointer("key"); + } + +} From e00191798e93d8fd28201d60affe2f5a46dbe276 Mon Sep 17 00:00:00 2001 From: Brian Russell Date: Sun, 24 Apr 2016 23:04:12 -0400 Subject: [PATCH 195/315] Added unit tests for escaped quotes. --- src/test/org/json/junit/CDLTest.java | 57 +++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/test/org/json/junit/CDLTest.java b/src/test/org/json/junit/CDLTest.java index 340977b4d..a40b0143e 100644 --- a/src/test/org/json/junit/CDLTest.java +++ b/src/test/org/json/junit/CDLTest.java @@ -68,7 +68,7 @@ public void unbalancedQuoteInName() { equals(e.getMessage())); } } - + /** * Attempts to create a JSONArray from a string with unbalanced quotes * in value line. Expects a JSONException. @@ -104,7 +104,62 @@ public void nullInName() { } } + + /** + * Attempt to create a JSONArray with unbalanced quotes and a properly escaped doubled quote. + * Expects a JSONException. + */ + @Test + public void unbalancedEscapedQuote(){ + String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\""; + try { + CDL.toJSONArray(badLine); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Missing close quote '\"'. at 27 [character 16 line 3]". + equals(e.getMessage())); + + } + } + /** + * Assert that there is no error for a single escaped quote within a properly embedded quote. + */ + @Test + public void singleEscapedQuote(){ + String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\""; + JSONArray jsonArray = CDL.toJSONArray(singleEscape); + + String cdlStr = CDL.toString(jsonArray); + assertTrue(cdlStr.contains("Col1")); + assertTrue(cdlStr.contains("Col2")); + assertTrue(cdlStr.contains("Val1")); + assertTrue(cdlStr.contains("\"Val2")); + + } + + /** + * Attempt to create a JSONArray with an escape quote and no enclosing quotes. + * Expects a JSONException. + */ + @Test + public void badEscapedQuote(){ + String badLine = "Col1, Col2\nVal1, \"\"Val2"; + + try { + CDL.toJSONArray(badLine); + assertTrue("Expecting an exception", false); + } catch (JSONException e) { + System.out.println("Message" + e.getMessage()); + assertTrue("Expecting an exception message", + "Bad character 'V' (86). at 20 [character 9 line 3]". + equals(e.getMessage())); + + } + + } + /** * call toString with a null array */ From 6211384f87e8aee878b2a59c9f9d1e9101d9cfd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 26 Apr 2016 23:01:18 +0200 Subject: [PATCH 196/315] tests for url fragment notation handling, moving test document to separate file --- .gitignore | 3 ++ src/test/org/json/junit/JSONPointerTest.java | 54 +++++++++++++++---- .../org/json/junit/jsonpointer-testdoc.json | 16 ++++++ 3 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 src/test/org/json/junit/jsonpointer-testdoc.json diff --git a/.gitignore b/.gitignore index 9e7b59c15..2ede4829c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ build .classpath .project .settings/ +.gitignore +.gradle +src/main diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 221f03507..463b66ea0 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -5,21 +5,30 @@ import org.json.JSONObject; import org.json.JSONPointer; import org.json.JSONPointerException; +import org.json.JSONTokener; import org.junit.Test; public class JSONPointerTest { - private static final JSONObject document = new JSONObject("{" - + "\"foo\": [\"bar\", \"baz\"], " - + "\"\": 0," - + "\"a/b\": 1," - + "\"c%d\": 2," - + "\"e^f\": 3," - + "\"g|h\": 4," + "\"i\\\\j\": 5," - + "\"k\\\"l\": 6," - + "\" \": 7," - + "\"m~n\": 8" - + "}"); + private static final JSONObject document; + + // = new JSONObject("{" + // + "\"foo\": [\"bar\", \"baz\"], " + // + "\"\": 0," + // + "\"a/b\": 1," + // + "\"c%d\": 2," + // + "\"e^f\": 3," + // + "\"g|h\": 4," + // + "\"i\\\\j\": 5," + // + "\"k\\\\\\\"l\": 6," + // + "\" \": 7," + // + "\"m~n\": 8" + // + "}"); + + static { + document = new JSONObject(new JSONTokener( + JSONPointerTest.class.getResourceAsStream("/org/json/junit/jsonpointer-testdoc.json"))); + } private Object query(String pointer) { return new JSONPointer(pointer).queryFrom(document); @@ -65,11 +74,34 @@ public void tildeEscaping() { assertSame(document.get("m~n"), query("/m~0n")); } + @Test + public void backslashEscaping() { + assertSame(document.get("i\\j"), query("/i\\\\j")); + } + + @Test + public void quotationEscaping() { + assertSame(document.get("k\"l"), query("/k\\\\\\\"l")); + } + + @Test + public void whitespaceKey() { + assertSame(document.get(" "), query("/ ")); + } + @Test public void uriFragmentNotation() { assertSame(document.get("foo"), query("#/foo")); } + @Test + public void uriFragmentPercentHandling() { + assertSame(document.get("c%d"), query("#/c%25d")); + assertSame(document.get("e^f"), query("#/e%5Ef")); + assertSame(document.get("g|h"), query("#/g%7Ch")); + assertSame(document.get("m~n"), query("#/m~0n")); + } + @Test(expected = IllegalArgumentException.class) public void syntaxError() { new JSONPointer("key"); diff --git a/src/test/org/json/junit/jsonpointer-testdoc.json b/src/test/org/json/junit/jsonpointer-testdoc.json new file mode 100644 index 000000000..386bdb71b --- /dev/null +++ b/src/test/org/json/junit/jsonpointer-testdoc.json @@ -0,0 +1,16 @@ +{ + "foo": + [ + "bar", + "baz" + ], + "": 0, + "a/b": 1, + "c%d": 2, + "e^f": 3, + "g|h": 4, + "i\\j": 5, + "k\"l": 6, + " ": 7, + "m~n": 8 +} \ No newline at end of file From 66f740eb451066840cef1faf8e051a15c5d16dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 26 Apr 2016 23:02:26 +0200 Subject: [PATCH 197/315] rolling back unwanted gitignore change in previous commit --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index 2ede4829c..9e7b59c15 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,3 @@ build .classpath .project .settings/ -.gitignore -.gradle -src/main From f857dda5d84cf1baa809fa0d52c657266989a6e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 26 Apr 2016 23:03:01 +0200 Subject: [PATCH 198/315] removing some deprecated commented code --- src/test/org/json/junit/JSONPointerTest.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 463b66ea0..103c5a870 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -12,19 +12,6 @@ public class JSONPointerTest { private static final JSONObject document; - // = new JSONObject("{" - // + "\"foo\": [\"bar\", \"baz\"], " - // + "\"\": 0," - // + "\"a/b\": 1," - // + "\"c%d\": 2," - // + "\"e^f\": 3," - // + "\"g|h\": 4," - // + "\"i\\\\j\": 5," - // + "\"k\\\\\\\"l\": 6," - // + "\" \": 7," - // + "\"m~n\": 8" - // + "}"); - static { document = new JSONObject(new JSONTokener( JSONPointerTest.class.getResourceAsStream("/org/json/junit/jsonpointer-testdoc.json"))); From e748c60eb10ba5605ca6c00f37df0d843f9592bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 26 Apr 2016 23:31:43 +0200 Subject: [PATCH 199/315] tests for improved failure handling --- src/test/org/json/junit/JSONPointerTest.java | 10 ++++++++++ src/test/org/json/junit/jsonpointer-testdoc.json | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 103c5a870..3818e8235 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -94,4 +94,14 @@ public void syntaxError() { new JSONPointer("key"); } + @Test(expected = JSONPointerException.class) + public void arrayIndexFailure() { + query("/foo/2"); + } + + @Test(expected = JSONPointerException.class) + public void primitiveFailure() { + query("/obj/key/failure"); + } + } diff --git a/src/test/org/json/junit/jsonpointer-testdoc.json b/src/test/org/json/junit/jsonpointer-testdoc.json index 386bdb71b..621ce9310 100644 --- a/src/test/org/json/junit/jsonpointer-testdoc.json +++ b/src/test/org/json/junit/jsonpointer-testdoc.json @@ -12,5 +12,8 @@ "i\\j": 5, "k\"l": 6, " ": 7, - "m~n": 8 + "m~n": 8, + "obj" : { + "key" : "value" + } } \ No newline at end of file From 4a3565afb316f426f42ea6ed6ce29f1e253d2a62 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 1 May 2016 22:47:23 -0500 Subject: [PATCH 200/315] unit test integration --- build.gradle | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index 2544a69c7..943080fba 100644 --- a/build.gradle +++ b/build.gradle @@ -1,26 +1,44 @@ apply plugin: 'java' apply plugin: 'eclipse' - -jar.baseName = 'JSON-java' +apply plugin: 'jacoco' sourceSets { - test { - java { - srcDir 'src/test' + // Uncomment main if you have merged JSON-Java and JSON-Java-unit-test code + main { + java { + srcDir 'src' + exclude 'test/' + } + } + test { + java { + srcDir 'src/test' + } } - } } repositories { - mavenCentral() + mavenCentral() } dependencies { - testCompile group: 'junit', name: 'junit', version: '4.+' - testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0' - testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5' - // Use this line if you are testing a JSON-Java release. - // Otherwise add an external jar from your local repository in Eclipse - // (The gradle build won't work unless you add a main sourceSets entry and a jar.baseName entry - // testCompile group: 'org.json', name: 'json', version: '20151123' + testCompile group: 'junit', name: 'junit', version: '4.+' + testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0' + testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5' + // Uncomment if you are testing against a JSON-Java release + // testCompile 'org.json:json:20160212' + // Uncomment if you have copied a local JSON-Java jar file into this project + // testCompile files('./JSON-Java.jar') } + +test { finalizedBy jacocoTestReport } +jacocoTestReport{ + additionalSourceDirs = files(sourceSets.main.allJava.srcDirs) + reports { + xml.enabled false + csv.enabled false + html.destination "${buildDir}/reports/jacoco/html" + } + executionData = files('build/jacoco/test.exec') +} + From 052ce94a34186ebd8dffb7b35cc888d515e18786 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 1 May 2016 22:53:53 -0500 Subject: [PATCH 201/315] Update README.md --- README.md | 33 ++------------------------------- 1 file changed, 2 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index d908b5223..0357b4a16 100644 --- a/README.md +++ b/README.md @@ -24,40 +24,11 @@ Run individual tests or JunitTestSuite using EclEmma Coverage, or * JSON-java.jar
**To build from the command line using gradle:** +Until the unit tests are merged into the JSON-Java project, the code has to be wired by hand. After cloning JSON-Java-unit-test, create a directory structure under src and copy the JSON-Java files into: src/org/json. Then execute the unit tests and code coverage with: ```` -build.gradle -# In this example, both the JSON-java jar and the test code is created -# from the same build file, in the test code directory. 3rd party jars are -# obtained from the maven repository. -apply plugin: 'java' -jar.baseName = 'JSON-java' - -sourceSets { - main { - java { - srcDir '../JSON-java/src/org/json' - } - } - test { - java { - srcDir 'src/org/json/junit' - } - } -} - -repositories { - mavenCentral() -} - -dependencies { - testCompile group: 'junit', name: 'junit', version: '4.+' - testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0' - testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5' -} +gradle clean build test jacocoTestReport ```` -To measure coverage: http://www.eclemma.org/ (just install the latest in Eclipse)
- Conventions
Test filenames should consist of the name of the module being tested, with the suffix "Test". For example, Cookie.java is tested by CookieTest.java. From 6edc0938033d736ab03c8e23ccaccda2c32bae7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 3 May 2016 23:20:17 +0200 Subject: [PATCH 202/315] adding unittests for JSPONPointer#toString(), toURIFragment() and its builder class --- src/test/org/json/junit/JSONPointerTest.java | 37 ++++++++++++++++++- .../org/json/junit/jsonpointer-testdoc.json | 7 +++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 3818e8235..4299d8fe2 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -1,5 +1,6 @@ package org.json.junit; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import org.json.JSONObject; @@ -28,7 +29,7 @@ public void emptyPointer() { @Test(expected = NullPointerException.class) public void nullPointer() { - new JSONPointer(null); + new JSONPointer((String) null); } @Test @@ -103,5 +104,39 @@ public void arrayIndexFailure() { public void primitiveFailure() { query("/obj/key/failure"); } + + @Test + public void builderTest() { + JSONPointer pointer = JSONPointer.builder() + .append("obj") + .append("other~key").append("another/key") + .append(0) + .build(); + assertEquals("val", pointer.queryFrom(document)); + } + + @Test + public void toStringEscaping() { + JSONPointer pointer = JSONPointer.builder() + .append("obj") + .append("other~key").append("another/key") + .append("\"") + .append(0) + .build(); + assertEquals("/obj/other~0key/another~1key/\\\"/0", pointer.toString()); + } + + @Test + public void emptyPointerToString() { + assertEquals("", new JSONPointer("").toString()); + } + + @Test + public void toURIFragment() { + assertEquals("#/c%25d", new JSONPointer("/c%d").toURIFragment()); + assertEquals("#/e%5Ef", new JSONPointer("/e^f").toURIFragment()); + assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment()); + assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment()); + } } diff --git a/src/test/org/json/junit/jsonpointer-testdoc.json b/src/test/org/json/junit/jsonpointer-testdoc.json index 621ce9310..d58fe8216 100644 --- a/src/test/org/json/junit/jsonpointer-testdoc.json +++ b/src/test/org/json/junit/jsonpointer-testdoc.json @@ -14,6 +14,11 @@ " ": 7, "m~n": 8, "obj" : { - "key" : "value" + "key" : "value", + "other~key" : { + "another/key" : [ + "val" + ] + } } } \ No newline at end of file From 2eed4be5fca99a8f2ced50b4827f3f1f6ab798b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Tue, 3 May 2016 23:42:26 +0200 Subject: [PATCH 203/315] one more test for null-check in Builder#append(String) --- src/test/org/json/junit/JSONPointerTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 4299d8fe2..f17ba6efa 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -115,6 +115,11 @@ public void builderTest() { assertEquals("val", pointer.queryFrom(document)); } + @Test(expected = NullPointerException.class) + public void nullToken() { + JSONPointer.builder().append(null); + } + @Test public void toStringEscaping() { JSONPointer pointer = JSONPointer.builder() From adb3118d31f84cd4d1b60828a95f64c0c9e282bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Thu, 5 May 2016 16:00:15 +0200 Subject: [PATCH 204/315] added test for checking if the JSONPointer is immutable --- src/test/org/json/junit/JSONPointerTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index f17ba6efa..820023b42 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; import org.json.JSONObject; import org.json.JSONPointer; @@ -143,5 +144,16 @@ public void toURIFragment() { assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment()); assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment()); } + + @Test + public void tokenListIsCopiedInConstructor() { + JSONPointer.Builder b = JSONPointer.builder().append("key1"); + JSONPointer jp1 = b.build(); + b.append("key2"); + JSONPointer jp2 = b.build(); + if(jp1.toString().equals(jp2.toString())) { + fail("Oops, my pointers are sharing a backing array"); + } + } } From ccc7a7af29bb6385180c8647d0b332a804250198 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 7 May 2016 07:02:50 -0500 Subject: [PATCH 205/315] Update README.md --- README.md | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0357b4a16..1be9c6547 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,10 @@ Unit tests to validate the JSON-Java GitHub project code
https://github.com/douglascrockford/JSON-java
-*These tests are a work in progress. Help from interested developers is welcome.*
-More coverage is needed, but more importantly, improvements to test quality is needed.
- -Eclipse is the recommended development environment.
+Gradle and Eclipse is the recommended build tool and IDE.
Run individual tests or JunitTestSuite using EclEmma Coverage, or execute the **TestRunner** application directly.
-**You will need the following libraries for testing:**
+**The following libraries are required:**
* asm-1.0.2.jar
* commons-io-2.1.jar
* commons-lang-2.6.jar
@@ -24,10 +21,31 @@ Run individual tests or JunitTestSuite using EclEmma Coverage, or * JSON-java.jar
**To build from the command line using gradle:** -Until the unit tests are merged into the JSON-Java project, the code has to be wired by hand. After cloning JSON-Java-unit-test, create a directory structure under src and copy the JSON-Java files into: src/org/json. Then execute the unit tests and code coverage with: +Until the unit tests are merged into the JSON-Java project, the code has to be wired by hand. +\# In an empty directory of your choice, clone JSON-Java-unit-test: +```` +git clone https://github.com/stleary/JSON-Java-unit-test.git . +```` +\# Create a directory structure for the JSON-Java code +```` +mkdir -p src\org\json +```` +\# clone JSON-Java +```` +git clone https://github.com/stleary/JSON-Java-unit-test.git src\org\json +```` +\# Build, then execute the unit tests and code coverage ```` gradle clean build test jacocoTestReport ```` +Unit test results will be in build\reports\tests\index.html
+Code coverage will be in build\reports\jacoco\html\index.html + +To create an Eclipse project, you will need the Eclipse Gradle plug-in, available from the Eclipse Marketplace. I am currently using Gradle IDE 3.6.4.201503050952-RELEASE
+Select File > Import > Gradle > Gradle project
+Browse to the directory where you cloned JSON-Java-unit-test
+Select Build model
+Select built project
Conventions
Test filenames should consist of the name of the module being tested, with the suffix "Test". From 691734f342ece27b686046547363e14828b6272d Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 7 May 2016 07:03:30 -0500 Subject: [PATCH 206/315] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1be9c6547..388122ffb 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ Run individual tests or JunitTestSuite using EclEmma Coverage, or * JSON-java.jar
**To build from the command line using gradle:** -Until the unit tests are merged into the JSON-Java project, the code has to be wired by hand. -\# In an empty directory of your choice, clone JSON-Java-unit-test: +Until the unit tests are merged into the JSON-Java project, the code has to be wired by hand.
+\# In an empty directory of your choice, clone JSON-Java-unit-test:
```` git clone https://github.com/stleary/JSON-Java-unit-test.git . ```` From b843d67a921690dcf6fca86a4a23762ed11dd7a4 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 7 May 2016 07:03:50 -0500 Subject: [PATCH 207/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 388122ffb..8cd97b802 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Run individual tests or JunitTestSuite using EclEmma Coverage, or * slf-simple-1.7.12.jar
* JSON-java.jar
-**To build from the command line using gradle:** +**To build from the command line using gradle:**
Until the unit tests are merged into the JSON-Java project, the code has to be wired by hand.
\# In an empty directory of your choice, clone JSON-Java-unit-test:
```` From a9ff159c7804746800982445237a590a6479cbbd Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 13 May 2016 15:17:22 -0500 Subject: [PATCH 208/315] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8cd97b802..d08751dbe 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,12 @@ git clone https://github.com/stleary/JSON-Java-unit-test.git . ```` \# Create a directory structure for the JSON-Java code ```` -mkdir -p src\org\json +# Windows version +md /s src\org\json ```` \# clone JSON-Java ```` -git clone https://github.com/stleary/JSON-Java-unit-test.git src\org\json +git clone https://github.com/stleary/JSON-Java.git src\org\json ```` \# Build, then execute the unit tests and code coverage ```` From 0112d82755f07838de44f31725ba6a697d4355b6 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 13 May 2016 23:13:06 -0500 Subject: [PATCH 209/315] add JSONPointerTest to test suite, fix resource for gradle build --- build.gradle | 3 +++ src/test/org/json/junit/JSONPointerTest.java | 7 ++----- src/test/org/json/junit/JunitTestSuite.java | 5 +++-- .../{org/json/junit => resources}/jsonpointer-testdoc.json | 0 4 files changed, 8 insertions(+), 7 deletions(-) rename src/test/{org/json/junit => resources}/jsonpointer-testdoc.json (100%) diff --git a/build.gradle b/build.gradle index 943080fba..8dbd0fe0f 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,9 @@ sourceSets { java { srcDir 'src/test' } + resources { + srcDir 'resources' + } } } diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index 820023b42..c1006174e 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -4,10 +4,7 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; -import org.json.JSONObject; -import org.json.JSONPointer; -import org.json.JSONPointerException; -import org.json.JSONTokener; +import org.json.*; import org.junit.Test; public class JSONPointerTest { @@ -16,7 +13,7 @@ public class JSONPointerTest { static { document = new JSONObject(new JSONTokener( - JSONPointerTest.class.getResourceAsStream("/org/json/junit/jsonpointer-testdoc.json"))); + JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json"))); } private Object query(String pointer) { diff --git a/src/test/org/json/junit/JunitTestSuite.java b/src/test/org/json/junit/JunitTestSuite.java index ceff6f143..38639314c 100644 --- a/src/test/org/json/junit/JunitTestSuite.java +++ b/src/test/org/json/junit/JunitTestSuite.java @@ -14,7 +14,8 @@ JSONStringerTest.class, JSONObjectTest.class, JSONArrayTest.class, - EnumTest.class + EnumTest.class, + JSONPointerTest.class }) public class JunitTestSuite { -} \ No newline at end of file +} diff --git a/src/test/org/json/junit/jsonpointer-testdoc.json b/src/test/resources/jsonpointer-testdoc.json similarity index 100% rename from src/test/org/json/junit/jsonpointer-testdoc.json rename to src/test/resources/jsonpointer-testdoc.json From 06ae87c456ead203ba78746a8b6c96f2d99c7078 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 13 May 2016 23:22:54 -0500 Subject: [PATCH 210/315] exclude resources from test --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index 8dbd0fe0f..ad186a1c5 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,7 @@ sourceSets { test { java { srcDir 'src/test' + exclude 'resources/' } resources { srcDir 'resources' From 45cbc66f5bd92bfad97d36de6896d6acedb0a3dc Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 14 May 2016 09:26:03 -0500 Subject: [PATCH 211/315] add coverage for JSONObject, JSONArray queryFrom() --- src/test/org/json/junit/JSONPointerTest.java | 62 +++++++++++++++++++- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/org/json/junit/JSONPointerTest.java index c1006174e..95fa73b9c 100644 --- a/src/test/org/json/junit/JSONPointerTest.java +++ b/src/test/org/json/junit/JSONPointerTest.java @@ -1,8 +1,6 @@ package org.json.junit; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import org.json.*; import org.junit.Test; @@ -153,4 +151,62 @@ public void tokenListIsCopiedInConstructor() { } } + /** + * Coverage for JSONObject queryFrom() + */ + @Test + public void queryFromJSONObject() { + String str = "{"+ + "\"stringKey\":\"hello world!\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\": {"+ + "\"a\":\"aVal\","+ + "\"b\":\"bVal\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + Object obj = jsonObject.query("/stringKey"); + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); + obj = jsonObject.query("/arrayKey/1"); + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); + obj = jsonObject.query("/objectKey/b"); + assertTrue("Expected bVal", "bVal".equals(obj)); + try { + obj = jsonObject.query("/a/b/c"); + assertTrue("Expected JSONPointerException", false); + } catch (JSONPointerException e) { + assertTrue("Expected bad key/value exception", + "value [null] is not an array or object therefore its key b cannot be resolved". + equals(e.getMessage())); + } + } + + /** + * Coverage for JSONArray queryFrom() + */ + @Test + public void queryFromJSONArray() { + String str = "["+ + "\"hello world!\","+ + "[0,1,2],"+ + "{"+ + "\"a\":\"aVal\","+ + "\"b\":\"bVal\""+ + "}"+ + "]"; + JSONArray jsonArray = new JSONArray(str); + Object obj = jsonArray.query("/0"); + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); + obj = jsonArray.query("/1/1"); + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); + obj = jsonArray.query("/2/b"); + assertTrue("Expected bVal", "bVal".equals(obj)); + try { + obj = jsonArray.query("/a/b/c"); + assertTrue("Expected JSONPointerException", false); + } catch (JSONPointerException e) { + assertTrue("Expected bad index exception", + "a is not an array index".equals(e.getMessage())); + } + } } From 15f48a05004b171fc556f613ebd3dd86688018fb Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 14 May 2016 11:59:24 -0500 Subject: [PATCH 212/315] convert tests to use JSONPointer where practical --- src/test/org/json/junit/JSONObjectTest.java | 158 ++++++++++---------- 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/src/test/org/json/junit/JSONObjectTest.java b/src/test/org/json/junit/JSONObjectTest.java index 869fe466e..f0155795e 100644 --- a/src/test/org/json/junit/JSONObjectTest.java +++ b/src/test/org/json/junit/JSONObjectTest.java @@ -72,10 +72,10 @@ public void jsonObjectByNames() { JSONObject jsonObjectByName = new JSONObject(jsonObject, keys); Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObjectByName.toString()); assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); - assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"nullKey\":null", null == JsonPath.read(doc, "$.nullKey")); - assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObjectByName.query("/falseKey"))); + assertTrue("expected \"nullKey\":null", JSONObject.NULL.equals(jsonObjectByName.query("/nullKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObjectByName.query("/stringKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObjectByName.query("/doubleKey"))); } /** @@ -109,13 +109,13 @@ public void jsonObjectByMap() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); - assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); + assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey"))); } - + /** * Verifies that the constructor has backwards compatability with RAW types pre-java5. */ @@ -249,8 +249,8 @@ public void jsonObjectByMapWithUnsupportedValues() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(JsonPath.read(doc, "$.key2"))); assertTrue("expected 0 key1 items", ((Map)(JsonPath.read(doc, "$.key1"))).size() == 0); + assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(jsonObject.query("/key2"))); } /** @@ -272,12 +272,12 @@ public void jsonObjectByMapWithNullValue() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); - assertTrue("expected \"intKey\":42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); - assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey"))); + assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); + assertTrue("expected \"intKey\":42", Long.valueOf("42").equals(jsonObject.query("/intKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey"))); } /** @@ -306,13 +306,13 @@ public void jsonObjectByBean() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 8 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 8); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); - assertTrue("expected hello world!","hello world!".equals(JsonPath.read(doc, "$.stringKey"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey"))); - assertTrue("expected 42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey"))); - assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(JsonPath.read(doc, "$.doubleKey"))); assertTrue("expected 0 items in stringReaderKey", ((Map) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); + assertTrue("expected hello world!","hello world!".equals(jsonObject.query("/stringKey"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); + assertTrue("expected 42", Integer.valueOf("42").equals(jsonObject.query("/intKey"))); + assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(jsonObject.query("/doubleKey"))); // sorry, mockito artifact assertTrue("expected 2 callbacks items", ((List)(JsonPath.read(doc, "$.callbacks"))).size() == 2); assertTrue("expected 0 handler items", ((Map)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0); @@ -335,8 +335,8 @@ public void jsonObjectByObjectAndNames() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected \"publicString\":\"abc\"", "abc".equals(JsonPath.read(doc, "$.publicString"))); - assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.publicInt"))); + assertTrue("expected \"publicString\":\"abc\"", "abc".equals(jsonObject.query("/publicString"))); + assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(jsonObject.query("/publicInt"))); } /** @@ -353,11 +353,11 @@ public void jsonObjectByResourceBundle() { Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); assertTrue("expected 2 greetings items", ((Map)(JsonPath.read(doc, "$.greetings"))).size() == 2); - assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(JsonPath.read(doc, "$.greetings.hello"))); - assertTrue("expected \"world\":\"World!\"", "World!".equals(JsonPath.read(doc, "$.greetings.world"))); + assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(jsonObject.query("/greetings/hello"))); + assertTrue("expected \"world\":\"World!\"", "World!".equals(jsonObject.query("/greetings/world"))); assertTrue("expected 2 farewells items", ((Map)(JsonPath.read(doc, "$.farewells"))).size() == 2); - assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(JsonPath.read(doc, "$.farewells.later"))); - assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(JsonPath.read(doc, "$.farewells.gator"))); + assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later"))); + assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator"))); } /** @@ -383,12 +383,12 @@ public void jsonObjectAccumulate() { Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); - assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]"))); - assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); - assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]"))); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1"))); + assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3"))); + assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4"))); + assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5"))); } /** @@ -413,12 +413,12 @@ public void jsonObjectAppend() { Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]"))); - assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]"))); - assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]"))); - assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]"))); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1/"))); + assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3"))); + assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4"))); + assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5"))); } /** @@ -1146,11 +1146,11 @@ public void jsonObjectIncrement() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); 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"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/keyInt"))); + assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(jsonObject.query("/keyLong"))); + assertTrue("expected 3.1", Double.valueOf(3.1).equals(jsonObject.query("/keyDouble"))); + assertTrue("expected 123456789123456789123456789123456781", new BigInteger("123456789123456789123456789123456781").equals(jsonObject.query("/keyBigInt"))); + assertTrue("expected 123456789123456789123456789123456781.1", new BigDecimal("123456789123456789123456789123456781.1").equals(jsonObject.query("/keyBigDec"))); /** * Should work the same way on any platform! @see https://docs.oracle @@ -1175,7 +1175,7 @@ public void jsonObjectIncrement() { * missing bits would not fit into the 32 bit float, i.e. the * information needed simply is not there! */ - assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(JsonPath.read(doc, "$.keyFloat"))); + assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(jsonObject.query("/keyFloat"))); /** * float f = 3.1f; double df = (double) f; double d = 3.1d; @@ -1285,17 +1285,17 @@ public void jsonObjectPut() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); - assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); + assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2"))); assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); - assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); - assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); - assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); - assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); + assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1"))); + assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2"))); + assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3"))); + assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4"))); jsonObject.remove("trueKey"); JSONObject expectedJsonObject = new JSONObject(expectedStr); @@ -1348,17 +1348,17 @@ public void jsonObjectToString() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey"))); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); - assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]"))); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]"))); + assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2"))); assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); - assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1"))); - assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2"))); - assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3"))); - assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4"))); + assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1"))); + assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2"))); + assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3"))); + assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4"))); } /** @@ -1379,7 +1379,7 @@ public void jsonObjectToStringSuppressWarningOnCastToMap() { Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); assertTrue("expected 1 key item", ((Map)(JsonPath.read(doc, "$.key"))).size() == 1); - assertTrue("expected def", "def".equals(JsonPath.read(doc, "$.key.abc"))); + assertTrue("expected def", "def".equals(jsonObject.query("/key/abc"))); } /** @@ -1401,7 +1401,7 @@ public void jsonObjectToStringSuppressWarningOnCastToCollection() { Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); assertTrue("expected 1 key item", ((List)(JsonPath.read(doc, "$.key"))).size() == 1); - assertTrue("expected abc", "abc".equals(JsonPath.read(doc, "$.key[0]"))); + assertTrue("expected abc", "abc".equals(jsonObject.query("/key/0"))); } /** @@ -1517,9 +1517,9 @@ public void wrapObject() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); // wrap Array returns JSONArray Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; @@ -1528,16 +1528,16 @@ public void wrapObject() { // validate JSON doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); // validate JSON doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString()); assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); // wrap map returns JSONObject Map map = new HashMap(); @@ -1549,9 +1549,9 @@ public void wrapObject() { // validate JSON doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString()); assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); - 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"))); + assertTrue("expected val1", "val1".equals(mapJsonObject.query("/key1"))); + assertTrue("expected val2", "val2".equals(mapJsonObject.query("/key2"))); + assertTrue("expected val3", "val3".equals(mapJsonObject.query("/key3"))); } /** From 8ed0362683acd784fbd0a9d75beb86e56ed300d9 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 14 May 2016 13:38:46 -0500 Subject: [PATCH 213/315] convert remaining JsonPath expressions to JSONPointer --- src/test/org/json/junit/CookieListTest.java | 40 +++---- src/test/org/json/junit/EnumTest.java | 64 +++++------ src/test/org/json/junit/JSONArrayTest.java | 104 +++++++++--------- src/test/org/json/junit/JSONStringerTest.java | 68 ++++++------ 4 files changed, 138 insertions(+), 138 deletions(-) diff --git a/src/test/org/json/junit/CookieListTest.java b/src/test/org/json/junit/CookieListTest.java index 815c76737..7a710dbe3 100644 --- a/src/test/org/json/junit/CookieListTest.java +++ b/src/test/org/json/junit/CookieListTest.java @@ -78,7 +78,7 @@ public void simpleCookieList() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("Expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID"))); + assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID"))); } /** @@ -91,7 +91,7 @@ public void simpleCookieListWithDelimiter() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("Expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID"))); + assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID"))); } /** @@ -111,12 +111,12 @@ public void multiPartCookieList() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("Expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); - assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2"))); - assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3"))); - assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4"))); - assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); - assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); + assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1"))); + assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2"))); + assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3"))); + assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4"))); + assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5"))); + assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6"))); } /** @@ -151,12 +151,12 @@ public void convertCookieListToString() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("Expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); - assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2"))); - assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3"))); - assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4"))); - assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); - assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); + assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1"))); + assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2"))); + assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3"))); + assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4"))); + assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5"))); + assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6"))); } /** @@ -176,11 +176,11 @@ public void convertEncodedCookieListToString() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("Expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1"))); - assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(JsonPath.read(doc, "$.name2"))); - assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3"))); - assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(JsonPath.read(doc, "$.name4"))); - assertTrue("expected my%CookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5"))); - assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6"))); + assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1"))); + assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(jsonObject.query("/name2"))); + assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(jsonObject.query("/name3"))); + assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(jsonObject.query("/name4"))); + assertTrue("expected my%CookieValue5", "myCookieValue5".equals(jsonObject.query("/name5"))); + assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6"))); } } diff --git a/src/test/org/json/junit/EnumTest.java b/src/test/org/json/junit/EnumTest.java index a591dbaca..ff4b2940a 100644 --- a/src/test/org/json/junit/EnumTest.java +++ b/src/test/org/json/junit/EnumTest.java @@ -36,8 +36,8 @@ public void jsonObjectFromEnum() { Object doc = Configuration.defaultConfiguration().jsonProvider() .parse(jsonObject.toString()); assertTrue("expecting 2 items in top level object", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expecting val 2", "val 2".equals(JsonPath.read(doc, "$.value"))); - assertTrue("expecting 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal"))); + assertTrue("expecting val 2", "val 2".equals(jsonObject.query("/value"))); + assertTrue("expecting 2", Integer.valueOf(2).equals(jsonObject.query("/intVal"))); /** * class which contains enum instances. Each enum should be stored @@ -53,8 +53,8 @@ public void jsonObjectFromEnum() { assertTrue("expecting 2 items in top level object", ((Map)(JsonPath.read(doc, "$"))).size() == 2); assertTrue("expecting 2 items in myEnumField object", ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); assertTrue("expecting 0 items in myEnum object", ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); - assertTrue("expecting 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal"))); - assertTrue("expecting val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value"))); + assertTrue("expecting 3", Integer.valueOf(3).equals(jsonObject.query("/myEnumField/intVal"))); + assertTrue("expecting val 3", "val 3".equals(jsonObject.query("/myEnumField/value"))); } /** @@ -74,9 +74,9 @@ public void jsonObjectFromEnumWithNames() { // validate JSON object Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1"))); - assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2"))); - assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3"))); + assertTrue("expected VAL1", MyEnum.VAL1.equals(jsonObject.query("/VAL1"))); + assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/VAL2"))); + assertTrue("expected VAL3", MyEnum.VAL3.equals(jsonObject.query("/VAL3"))); MyEnumField myEnumField = MyEnumField.VAL3; names = JSONObject.getNames(myEnumField); @@ -84,9 +84,9 @@ public void jsonObjectFromEnumWithNames() { jsonObject = new JSONObject(myEnumField, names); doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1"))); - assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2"))); - assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3"))); + assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1"))); + assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2"))); + assertTrue("expected VAL3", myEnumField.VAL3.equals(jsonObject.query("/VAL3"))); } @@ -105,8 +105,8 @@ public void enumPut() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 2 top level objects", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnum"))); - assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.myEnumField"))); + assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/myEnum"))); + assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/myEnumField"))); JSONArray jsonArray = new JSONArray(); jsonArray.put(myEnum); @@ -115,8 +115,8 @@ public void enumPut() { // validate JSON content doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); assertTrue("expected 2 top level objects", ((List)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonArray.query("/0"))); + assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonArray.query("/1"))); /** * Leaving these tests because they exercise get, opt, and remove @@ -174,8 +174,8 @@ public void enumToString() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected val 2", "val 2".equals(JsonPath.read(doc, "$.value"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal"))); + assertTrue("expected val 2", "val 2".equals(jsonObject.query("/value"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/intVal"))); MyEnumClass myEnumClass = new MyEnumClass(); myEnumClass.setMyEnum(MyEnum.VAL1); @@ -187,8 +187,8 @@ public void enumToString() { assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); assertTrue("expected 2 myEnumField items", ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); assertTrue("expected 0 myEnum items", ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); - assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal"))); - assertTrue("expected val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/myEnumField/intVal"))); + assertTrue("expected val 3", "val 3".equals(jsonObject.query("/myEnumField/value"))); String [] names = JSONObject.getNames(myEnum); jsonObject = new JSONObject(myEnum, names); @@ -196,9 +196,9 @@ public void enumToString() { // validate JSON content doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1"))); - assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2"))); - assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3"))); + assertTrue("expected VAL1", MyEnum.VAL1.equals(jsonObject.query("/VAL1"))); + assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/VAL2"))); + assertTrue("expected VAL3", MyEnum.VAL3.equals(jsonObject.query("/VAL3"))); names = JSONObject.getNames(myEnumField); jsonObject = new JSONObject(myEnumField, names); @@ -206,9 +206,9 @@ public void enumToString() { // validate JSON content doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1"))); - assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2"))); - assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3"))); + assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1"))); + assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2"))); + assertTrue("expected VAL3", MyEnumField.VAL3.equals(jsonObject.query("/VAL3"))); expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}"; jsonObject = new JSONObject(); @@ -218,8 +218,8 @@ public void enumToString() { // validate JSON content doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnum"))); - assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnumField"))); + assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/myEnum"))); + assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/myEnumField"))); JSONArray jsonArray = new JSONArray(); jsonArray.put(myEnum); @@ -228,8 +228,8 @@ public void enumToString() { // validate JSON content doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); assertTrue("expected 2 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonArray.query("/0"))); + assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonArray.query("/1"))); } /** @@ -248,8 +248,8 @@ public void wrap() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected val 2", "val 2".equals(JsonPath.read(doc, "$.value"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal"))); + assertTrue("expected val 2", "val 2".equals(jsonObject.query("/value"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/intVal"))); MyEnumClass myEnumClass = new MyEnumClass(); myEnumClass.setMyEnum(MyEnum.VAL1); @@ -261,8 +261,8 @@ public void wrap() { assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); assertTrue("expected 2 myEnumField items", ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); assertTrue("expected 0 myEnum items", ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); - assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal"))); - assertTrue("expected val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/myEnumField/intVal"))); + assertTrue("expected val 3", "val 3".equals(jsonObject.query("/myEnumField/value"))); } diff --git a/src/test/org/json/junit/JSONArrayTest.java b/src/test/org/json/junit/JSONArrayTest.java index 455d68073..4bc95ef41 100644 --- a/src/test/org/json/junit/JSONArrayTest.java +++ b/src/test/org/json/junit/JSONArrayTest.java @@ -316,24 +316,24 @@ public void join() { */ Object doc = Configuration.defaultConfiguration().jsonProvider().parse("["+joinStr+"]"); assertTrue("expected 13 items in top level object", ((List)(JsonPath.read(doc, "$"))).size() == 13); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected \"true\"", "true".equals(JsonPath.read(doc, "$[2]"))); - assertTrue("expected \"false\"", "false".equals(JsonPath.read(doc, "$[3]"))); - assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[4]"))); - assertTrue("expected 0.002345", Double.valueOf(0.002345).equals(JsonPath.read(doc, "$[5]"))); - assertTrue("expected \"23.45\"", "23.45".equals(JsonPath.read(doc, "$[6]"))); - assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[7]"))); - assertTrue("expected \"43\"", "43".equals(JsonPath.read(doc, "$[8]"))); + assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1"))); + assertTrue("expected \"true\"", "true".equals(jsonArray.query("/2"))); + assertTrue("expected \"false\"", "false".equals(jsonArray.query("/3"))); + assertTrue("expected hello", "hello".equals(jsonArray.query("/4"))); + assertTrue("expected 0.002345", Double.valueOf(0.002345).equals(jsonArray.query("/5"))); + assertTrue("expected \"23.45\"", "23.45".equals(jsonArray.query("/6"))); + assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/7"))); + assertTrue("expected \"43\"", "43".equals(jsonArray.query("/8"))); assertTrue("expected 1 item in [9]", ((List)(JsonPath.read(doc, "$[9]"))).size() == 1); - assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[9][0]"))); + assertTrue("expected world", "world".equals(jsonArray.query("/9/0"))); assertTrue("expected 4 items in [10]", ((Map)(JsonPath.read(doc, "$[10]"))).size() == 4); - assertTrue("expected value1", "value1".equals(JsonPath.read(doc, "$[10].key1"))); - assertTrue("expected value2", "value2".equals(JsonPath.read(doc, "$[10].key2"))); - assertTrue("expected value3", "value3".equals(JsonPath.read(doc, "$[10].key3"))); - assertTrue("expected value4", "value4".equals(JsonPath.read(doc, "$[10].key4"))); - assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$[11]"))); - assertTrue("expected \"-1\"", "-1".equals(JsonPath.read(doc, "$[12]"))); + assertTrue("expected value1", "value1".equals(jsonArray.query("/10/key1"))); + assertTrue("expected value2", "value2".equals(jsonArray.query("/10/key2"))); + assertTrue("expected value3", "value3".equals(jsonArray.query("/10/key3"))); + assertTrue("expected value4", "value4".equals(jsonArray.query("/10/key4"))); + assertTrue("expected 0", Integer.valueOf(0).equals(jsonArray.query("/11"))); + assertTrue("expected \"-1\"", "-1".equals(jsonArray.query("/12"))); } /** @@ -461,24 +461,24 @@ public void put() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); assertTrue("expected 10 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 10); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1"))); assertTrue("expected 2 items in [2]", ((List)(JsonPath.read(doc, "$[2]"))).size() == 2); - assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]"))); - assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]"))); - assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]"))); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]"))); - assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]"))); - assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]"))); + assertTrue("expected hello", "hello".equals(jsonArray.query("/2/0"))); + assertTrue("expected world", "world".equals(jsonArray.query("/2/1"))); + assertTrue("expected 2.5", Double.valueOf(2.5).equals(jsonArray.query("/3"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/4"))); + assertTrue("expected 45", Long.valueOf(45).equals(jsonArray.query("/5"))); + assertTrue("expected objectPut", "objectPut".equals(jsonArray.query("/6"))); assertTrue("expected 3 items in [7]", ((Map)(JsonPath.read(doc, "$[7]"))).size() == 3); - assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[7].key10"))); - assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[7].key20"))); - assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[7].key30"))); + assertTrue("expected val10", "val10".equals(jsonArray.query("/7/key10"))); + assertTrue("expected val20", "val20".equals(jsonArray.query("/7/key20"))); + assertTrue("expected val30", "val30".equals(jsonArray.query("/7/key30"))); assertTrue("expected 1 item in [8]", ((Map)(JsonPath.read(doc, "$[8]"))).size() == 1); - assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[8].k1"))); + assertTrue("expected v1", "v1".equals(jsonArray.query("/8/k1"))); assertTrue("expected 2 items in [9]", ((List)(JsonPath.read(doc, "$[9]"))).size() == 2); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/9/0"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/9/1"))); } /** @@ -538,25 +538,25 @@ public void putIndex() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); assertTrue("expected 11 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 11); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); + assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1"))); assertTrue("expected 2 items in [2]", ((List)(JsonPath.read(doc, "$[2]"))).size() == 2); - assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]"))); - assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]"))); - assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]"))); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]"))); - assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]"))); - assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]"))); - assertTrue("expected null", null == JsonPath.read(doc, "$[7]")); + assertTrue("expected hello", "hello".equals(jsonArray.query("/2/0"))); + assertTrue("expected world", "world".equals(jsonArray.query("/2/1"))); + assertTrue("expected 2.5", Double.valueOf(2.5).equals(jsonArray.query("/3"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/4"))); + assertTrue("expected 45", Long.valueOf(45).equals(jsonArray.query("/5"))); + assertTrue("expected objectPut", "objectPut".equals(jsonArray.query("/6"))); + assertTrue("expected null", JSONObject.NULL.equals(jsonArray.query("/7"))); assertTrue("expected 3 items in [8]", ((Map)(JsonPath.read(doc, "$[8]"))).size() == 3); - assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[8].key10"))); - assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[8].key20"))); - assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[8].key30"))); + assertTrue("expected val10", "val10".equals(jsonArray.query("/8/key10"))); + assertTrue("expected val20", "val20".equals(jsonArray.query("/8/key20"))); + assertTrue("expected val30", "val30".equals(jsonArray.query("/8/key30"))); assertTrue("expected 2 items in [9]", ((List)(JsonPath.read(doc, "$[9]"))).size() == 2); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/9/0"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/9/1"))); assertTrue("expected 1 item in [10]", ((Map)(JsonPath.read(doc, "$[10]"))).size() == 1); - assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[10].k1"))); + assertTrue("expected v1", "v1".equals(jsonArray.query("/10/k1"))); } /** @@ -637,13 +637,13 @@ public void objectArrayVsIsArray() { // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); assertTrue("expected 7 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 7); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]"))); - assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$[3]"))); - assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$[4]"))); - assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$[5]"))); - assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$[6]"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); + assertTrue("expected 4", Integer.valueOf(4).equals(jsonArray.query("/3"))); + assertTrue("expected 5", Integer.valueOf(5).equals(jsonArray.query("/4"))); + assertTrue("expected 6", Integer.valueOf(6).equals(jsonArray.query("/5"))); + assertTrue("expected 7", Integer.valueOf(7).equals(jsonArray.query("/6"))); } /** diff --git a/src/test/org/json/junit/JSONStringerTest.java b/src/test/org/json/junit/JSONStringerTest.java index 631d79221..19b46de73 100644 --- a/src/test/org/json/junit/JSONStringerTest.java +++ b/src/test/org/json/junit/JSONStringerTest.java @@ -194,13 +194,13 @@ public void simpleObjectString() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 7 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 7); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseValue"))); - assertTrue("expected null", null == JsonPath.read(doc, "$.nullValue")); - assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.stringValue"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, "$.complexStringValue"))); - assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.intValue"))); - assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$.doubleValue"))); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueValue"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseValue"))); + assertTrue("expected null", JSONObject.NULL.equals(jsonObject.query("/nullValue"))); + assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue"))); + assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue"))); + assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue"))); } /** @@ -224,12 +224,12 @@ public void simpleArrayString() { // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); assertTrue("expected 6 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]"))); - assertTrue("expected null", null == JsonPath.read(doc, "$[2]")); - assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$[3]"))); - assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[4]"))); - assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$[5]"))); + assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1"))); + assertTrue("expected null", JSONObject.NULL.equals(jsonArray.query("/2"))); + assertTrue("expected hello world!", "hello world!".equals(jsonArray.query("/3"))); + assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/4"))); + assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(jsonArray.query("/5"))); } /** @@ -281,27 +281,27 @@ public void complexObjectString() { assertTrue("expected 5 array1 items", ((List)(JsonPath.read(doc, "$.object2.array1"))).size() == 5); assertTrue("expected 4 array[2] items", ((Map)(JsonPath.read(doc, "$.object2.array1[2]"))).size() == 4); assertTrue("expected 4 array1[2].array2 items", ((List)(JsonPath.read(doc, "$.object2.array1[2].array2"))).size() == 4); - assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue"))); - assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseValue"))); - assertTrue("expected null", null == JsonPath.read(doc, "$.nullValue")); - assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.stringValue"))); - assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.intValue"))); - assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$.doubleValue"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, "$.complexStringValue"))); - assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$.object2.k1"))); - assertTrue("expected v2", "v2".equals(JsonPath.read(doc, "$.object2.k2"))); - assertTrue("expected v3", "v3".equals(JsonPath.read(doc, "$.object2.k3"))); - assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.object2.array1[0]"))); - assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.object2.array1[1]"))); - assertTrue("expected v4", "v4".equals(JsonPath.read(doc, "$.object2.array1[2].k4"))); - assertTrue("expected v5", "v5".equals(JsonPath.read(doc, "$.object2.array1[2].k5"))); - assertTrue("expected v6", "v6".equals(JsonPath.read(doc, "$.object2.array1[2].k6"))); - assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$.object2.array1[2].array2[0]"))); - assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$.object2.array1[2].array2[1]"))); - assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$.object2.array1[2].array2[2]"))); - assertTrue("expected 8", Integer.valueOf(8).equals(JsonPath.read(doc, "$.object2.array1[2].array2[3]"))); - assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.object2.array1[3]"))); - assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$.object2.array1[4]"))); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueValue"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseValue"))); + assertTrue("expected null", JSONObject.NULL.equals(jsonObject.query("/nullValue"))); + assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue"))); + assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue"))); + assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue"))); + assertTrue("expected v1", "v1".equals(jsonObject.query("/object2/k1"))); + assertTrue("expected v2", "v2".equals(jsonObject.query("/object2/k2"))); + assertTrue("expected v3", "v3".equals(jsonObject.query("/object2/k3"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/object2/array1/0"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/object2/array1/1"))); + assertTrue("expected v4", "v4".equals(jsonObject.query("/object2/array1/2/k4"))); + assertTrue("expected v5", "v5".equals(jsonObject.query("/object2/array1/2/k5"))); + assertTrue("expected v6", "v6".equals(jsonObject.query("/object2/array1/2/k6"))); + assertTrue("expected 5", Integer.valueOf(5).equals(jsonObject.query("/object2/array1/2/array2/0"))); + assertTrue("expected 6", Integer.valueOf(6).equals(jsonObject.query("/object2/array1/2/array2/1"))); + assertTrue("expected 7", Integer.valueOf(7).equals(jsonObject.query("/object2/array1/2/array2/2"))); + assertTrue("expected 8", Integer.valueOf(8).equals(jsonObject.query("/object2/array1/2/array2/3"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/object2/array1/3"))); + assertTrue("expected 4", Integer.valueOf(4).equals(jsonObject.query("/object2/array1/4"))); } } From a1893ebc02fc11b0fa895ee8591ff40e1d24abd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Mon, 16 May 2016 14:54:01 +0200 Subject: [PATCH 214/315] unittests for stlear/JSON-Java#233 --- src/test/org/json/junit/JSONArrayTest.java | 28 ++++++++++++++++++-- src/test/org/json/junit/JSONObjectTest.java | 29 +++++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/test/org/json/junit/JSONArrayTest.java b/src/test/org/json/junit/JSONArrayTest.java index 455d68073..ffc9c2617 100644 --- a/src/test/org/json/junit/JSONArrayTest.java +++ b/src/test/org/json/junit/JSONArrayTest.java @@ -1,15 +1,24 @@ package org.json.junit; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.json.JSONPointerException; import org.junit.Test; -import com.jayway.jsonpath.*; +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.JsonPath; /** @@ -686,4 +695,19 @@ public void iterator() { new Long(-1).equals(Long.parseLong((String) it.next()))); assertTrue("should be at end of array", !it.hasNext()); } + + @Test(expected = JSONPointerException.class) + public void queryWithNoResult() { + new JSONArray().query("/a/b"); + } + + @Test + public void optQueryWithNoResult() { + assertNull(new JSONArray().optQuery("/a/b")); + } + + @Test(expected = IllegalArgumentException.class) + public void optQueryWithSyntaxError() { + new JSONArray().optQuery("invalid"); + } } diff --git a/src/test/org/json/junit/JSONObjectTest.java b/src/test/org/json/junit/JSONObjectTest.java index 869fe466e..7624ffb33 100644 --- a/src/test/org/json/junit/JSONObjectTest.java +++ b/src/test/org/json/junit/JSONObjectTest.java @@ -1,6 +1,7 @@ package org.json.junit; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -10,16 +11,25 @@ import java.io.Writer; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; import org.json.CDL; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.json.JSONPointerException; import org.json.XML; import org.junit.Test; -import com.jayway.jsonpath.*; +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.JsonPath; /** * JSONObject, along with JSONArray, are the central classes of the reference app. @@ -1899,4 +1909,19 @@ public void jsonObjectNullOperations() { String sNull = XML.toString(jsonObjectNull); assertTrue("null should emit an empty string", "".equals(sNull)); } + + @Test(expected = JSONPointerException.class) + public void queryWithNoResult() { + new JSONObject().query("/a/b"); + } + + @Test + public void optQueryWithNoResult() { + assertNull(new JSONObject().optQuery("/a/b")); + } + + @Test(expected = IllegalArgumentException.class) + public void optQueryWithSyntaxError() { + new JSONObject().optQuery("invalid"); + } } From c7fdada0fd4b65ce3beb04e67c8dc64e7e11189a Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Thu, 2 Jun 2016 16:41:43 +0200 Subject: [PATCH 215/315] Add test that an invalid escape sequence results in a JSONException and not a NumberFormatException. --- src/test/org/json/junit/JSONObjectTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/org/json/junit/JSONObjectTest.java b/src/test/org/json/junit/JSONObjectTest.java index 1c8beb26b..6ef361580 100644 --- a/src/test/org/json/junit/JSONObjectTest.java +++ b/src/test/org/json/junit/JSONObjectTest.java @@ -1924,4 +1924,10 @@ public void optQueryWithNoResult() { public void optQueryWithSyntaxError() { new JSONObject().optQuery("invalid"); } + + @Test(expected = JSONException.class) + public void invalidEscapeSequence() { + String json = "{ \"\\url\": \"value\" }"; + new JSONObject(json); + } } From 80f9e48e64e89ca7710257939076c3e9a8000b21 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 16 Jun 2016 12:20:54 -0400 Subject: [PATCH 216/315] Moves src folder to simplify build.gradle configuration. If JSON-Java source is merged, it's src fold would now be src/main/java/org.json/ instead of src/main/org.json as well. --- .gitignore | 6 ++++++ build.gradle | 17 ++--------------- src/test/{ => java}/org/json/junit/CDLTest.java | 0 .../org/json/junit/CookieListTest.java | 0 .../{ => java}/org/json/junit/CookieTest.java | 0 .../{ => java}/org/json/junit/EnumTest.java | 0 .../{ => java}/org/json/junit/HTTPTest.java | 0 .../org/json/junit/JSONArrayTest.java | 0 .../{ => java}/org/json/junit/JSONMLTest.java | 0 .../org/json/junit/JSONObjectTest.java | 0 .../org/json/junit/JSONPointerTest.java | 0 .../org/json/junit/JSONStringerTest.java | 0 .../org/json/junit/JunitTestSuite.java | 0 src/test/{ => java}/org/json/junit/MyBean.java | 0 .../org/json/junit/MyBigNumberBean.java | 0 src/test/{ => java}/org/json/junit/MyEnum.java | 0 .../{ => java}/org/json/junit/MyEnumClass.java | 0 .../{ => java}/org/json/junit/MyEnumField.java | 0 .../{ => java}/org/json/junit/MyJsonString.java | 0 .../org/json/junit/MyPublicClass.java | 0 .../{ => java}/org/json/junit/PropertyTest.java | 0 .../org/json/junit/StringsResourceBundle.java | 0 .../{ => java}/org/json/junit/TestRunner.java | 0 src/test/{ => java}/org/json/junit/Util.java | 0 src/test/{ => java}/org/json/junit/XMLTest.java | 0 25 files changed, 8 insertions(+), 15 deletions(-) rename src/test/{ => java}/org/json/junit/CDLTest.java (100%) rename src/test/{ => java}/org/json/junit/CookieListTest.java (100%) rename src/test/{ => java}/org/json/junit/CookieTest.java (100%) rename src/test/{ => java}/org/json/junit/EnumTest.java (100%) rename src/test/{ => java}/org/json/junit/HTTPTest.java (100%) rename src/test/{ => java}/org/json/junit/JSONArrayTest.java (100%) rename src/test/{ => java}/org/json/junit/JSONMLTest.java (100%) rename src/test/{ => java}/org/json/junit/JSONObjectTest.java (100%) rename src/test/{ => java}/org/json/junit/JSONPointerTest.java (100%) rename src/test/{ => java}/org/json/junit/JSONStringerTest.java (100%) rename src/test/{ => java}/org/json/junit/JunitTestSuite.java (100%) rename src/test/{ => java}/org/json/junit/MyBean.java (100%) rename src/test/{ => java}/org/json/junit/MyBigNumberBean.java (100%) rename src/test/{ => java}/org/json/junit/MyEnum.java (100%) rename src/test/{ => java}/org/json/junit/MyEnumClass.java (100%) rename src/test/{ => java}/org/json/junit/MyEnumField.java (100%) rename src/test/{ => java}/org/json/junit/MyJsonString.java (100%) rename src/test/{ => java}/org/json/junit/MyPublicClass.java (100%) rename src/test/{ => java}/org/json/junit/PropertyTest.java (100%) rename src/test/{ => java}/org/json/junit/StringsResourceBundle.java (100%) rename src/test/{ => java}/org/json/junit/TestRunner.java (100%) rename src/test/{ => java}/org/json/junit/Util.java (100%) rename src/test/{ => java}/org/json/junit/XMLTest.java (100%) diff --git a/.gitignore b/.gitignore index 9e7b59c15..7afd4207e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,9 @@ build .classpath .project .settings/ +/.gradle/ +/gradle/ +/gradlew +/gradlew.bat +.gitmodules +src/main/ diff --git a/build.gradle b/build.gradle index ad186a1c5..d2969d48a 100644 --- a/build.gradle +++ b/build.gradle @@ -4,21 +4,8 @@ apply plugin: 'jacoco' sourceSets { // Uncomment main if you have merged JSON-Java and JSON-Java-unit-test code - main { - java { - srcDir 'src' - exclude 'test/' - } - } - test { - java { - srcDir 'src/test' - exclude 'resources/' - } - resources { - srcDir 'resources' - } - } + main + test } repositories { diff --git a/src/test/org/json/junit/CDLTest.java b/src/test/java/org/json/junit/CDLTest.java similarity index 100% rename from src/test/org/json/junit/CDLTest.java rename to src/test/java/org/json/junit/CDLTest.java diff --git a/src/test/org/json/junit/CookieListTest.java b/src/test/java/org/json/junit/CookieListTest.java similarity index 100% rename from src/test/org/json/junit/CookieListTest.java rename to src/test/java/org/json/junit/CookieListTest.java diff --git a/src/test/org/json/junit/CookieTest.java b/src/test/java/org/json/junit/CookieTest.java similarity index 100% rename from src/test/org/json/junit/CookieTest.java rename to src/test/java/org/json/junit/CookieTest.java diff --git a/src/test/org/json/junit/EnumTest.java b/src/test/java/org/json/junit/EnumTest.java similarity index 100% rename from src/test/org/json/junit/EnumTest.java rename to src/test/java/org/json/junit/EnumTest.java diff --git a/src/test/org/json/junit/HTTPTest.java b/src/test/java/org/json/junit/HTTPTest.java similarity index 100% rename from src/test/org/json/junit/HTTPTest.java rename to src/test/java/org/json/junit/HTTPTest.java diff --git a/src/test/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java similarity index 100% rename from src/test/org/json/junit/JSONArrayTest.java rename to src/test/java/org/json/junit/JSONArrayTest.java diff --git a/src/test/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java similarity index 100% rename from src/test/org/json/junit/JSONMLTest.java rename to src/test/java/org/json/junit/JSONMLTest.java diff --git a/src/test/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java similarity index 100% rename from src/test/org/json/junit/JSONObjectTest.java rename to src/test/java/org/json/junit/JSONObjectTest.java diff --git a/src/test/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java similarity index 100% rename from src/test/org/json/junit/JSONPointerTest.java rename to src/test/java/org/json/junit/JSONPointerTest.java diff --git a/src/test/org/json/junit/JSONStringerTest.java b/src/test/java/org/json/junit/JSONStringerTest.java similarity index 100% rename from src/test/org/json/junit/JSONStringerTest.java rename to src/test/java/org/json/junit/JSONStringerTest.java diff --git a/src/test/org/json/junit/JunitTestSuite.java b/src/test/java/org/json/junit/JunitTestSuite.java similarity index 100% rename from src/test/org/json/junit/JunitTestSuite.java rename to src/test/java/org/json/junit/JunitTestSuite.java diff --git a/src/test/org/json/junit/MyBean.java b/src/test/java/org/json/junit/MyBean.java similarity index 100% rename from src/test/org/json/junit/MyBean.java rename to src/test/java/org/json/junit/MyBean.java diff --git a/src/test/org/json/junit/MyBigNumberBean.java b/src/test/java/org/json/junit/MyBigNumberBean.java similarity index 100% rename from src/test/org/json/junit/MyBigNumberBean.java rename to src/test/java/org/json/junit/MyBigNumberBean.java diff --git a/src/test/org/json/junit/MyEnum.java b/src/test/java/org/json/junit/MyEnum.java similarity index 100% rename from src/test/org/json/junit/MyEnum.java rename to src/test/java/org/json/junit/MyEnum.java diff --git a/src/test/org/json/junit/MyEnumClass.java b/src/test/java/org/json/junit/MyEnumClass.java similarity index 100% rename from src/test/org/json/junit/MyEnumClass.java rename to src/test/java/org/json/junit/MyEnumClass.java diff --git a/src/test/org/json/junit/MyEnumField.java b/src/test/java/org/json/junit/MyEnumField.java similarity index 100% rename from src/test/org/json/junit/MyEnumField.java rename to src/test/java/org/json/junit/MyEnumField.java diff --git a/src/test/org/json/junit/MyJsonString.java b/src/test/java/org/json/junit/MyJsonString.java similarity index 100% rename from src/test/org/json/junit/MyJsonString.java rename to src/test/java/org/json/junit/MyJsonString.java diff --git a/src/test/org/json/junit/MyPublicClass.java b/src/test/java/org/json/junit/MyPublicClass.java similarity index 100% rename from src/test/org/json/junit/MyPublicClass.java rename to src/test/java/org/json/junit/MyPublicClass.java diff --git a/src/test/org/json/junit/PropertyTest.java b/src/test/java/org/json/junit/PropertyTest.java similarity index 100% rename from src/test/org/json/junit/PropertyTest.java rename to src/test/java/org/json/junit/PropertyTest.java diff --git a/src/test/org/json/junit/StringsResourceBundle.java b/src/test/java/org/json/junit/StringsResourceBundle.java similarity index 100% rename from src/test/org/json/junit/StringsResourceBundle.java rename to src/test/java/org/json/junit/StringsResourceBundle.java diff --git a/src/test/org/json/junit/TestRunner.java b/src/test/java/org/json/junit/TestRunner.java similarity index 100% rename from src/test/org/json/junit/TestRunner.java rename to src/test/java/org/json/junit/TestRunner.java diff --git a/src/test/org/json/junit/Util.java b/src/test/java/org/json/junit/Util.java similarity index 100% rename from src/test/org/json/junit/Util.java rename to src/test/java/org/json/junit/Util.java diff --git a/src/test/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java similarity index 100% rename from src/test/org/json/junit/XMLTest.java rename to src/test/java/org/json/junit/XMLTest.java From 46a1c9acf9573d20648f101058f2e4ac51a1163a Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 16 Jun 2016 13:02:08 -0400 Subject: [PATCH 217/315] Adds test case to confirm the parsing of control characters --- .../java/org/json/junit/JSONObjectTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 6ef361580..d55013948 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1564,6 +1564,26 @@ public void wrapObject() { assertTrue("expected val3", "val3".equals(mapJsonObject.query("/key3"))); } + + /** + * RFC 7159 defines control characters to be U+0000 through U+001F. This test verifies that the parser is checking for these in expected ways. + */ + @Test + public void jsonObjectParseControlCharacters(){ + for(int i = 0;i<=0x001f;i++){ + final String charString = String.valueOf((char)i); + final String source = "{\"key\":\""+charString+"\"}"; + try { + JSONObject jo = new JSONObject(source); + assertTrue("Expected "+charString+"("+i+") in the JSON Object but did not find it.",charString.equals(jo.getString("key"))); + } catch (JSONException ex) { + assertTrue("Only \\0 (U+0000), \\n (U+000A), and \\r (U+000D) should cause an error. Instead "+charString+"("+i+") caused an error", + i=='\0' || i=='\n' || i=='\r' + ); + } + } + } + /** * Explore how JSONObject handles parsing errors. */ From c5deff636bd1413fab65ee0e64484c5422fd9a6e Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 16 Jun 2016 23:59:34 -0400 Subject: [PATCH 218/315] updates README for new project layout --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d08751dbe..40a4ebf70 100644 --- a/README.md +++ b/README.md @@ -29,15 +29,22 @@ git clone https://github.com/stleary/JSON-Java-unit-test.git . \# Create a directory structure for the JSON-Java code ```` # Windows version -md /s src\org\json +md /s src\main\java\org\json +# *nix version +mkdir -p src/main/java/org/json ```` \# clone JSON-Java ```` -git clone https://github.com/stleary/JSON-Java.git src\org\json +#Windows version +git clone https://github.com/stleary/JSON-Java.git src\main\java\org\json + +#*Nix version +git clone https://github.com/stleary/JSON-Java.git src/main/java/org/json ```` \# Build, then execute the unit tests and code coverage ```` gradle clean build test jacocoTestReport + ```` Unit test results will be in build\reports\tests\index.html
Code coverage will be in build\reports\jacoco\html\index.html From 1204ea9dcf611b874d7b0feadad86f84da4c2a63 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Fri, 17 Jun 2016 00:04:27 -0400 Subject: [PATCH 219/315] fixes a typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 40a4ebf70..385c222f2 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ When adding a new unit test, don't forget to update JunitTestSuite.java. The fundamental issues with JSON-Java testing are:
* JSONObjects are unordered, making simple string comparison ineffective. -* Comparisons via **equals()** is not currently supported. Neither JSONArray nor JSONObject overrride hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful. +* Comparisons via **equals()** is not currently supported. Neither JSONArray nor JSONObject override hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful. * Access to the JSONArray and JSONObject internal containers for comparison is not currently available. General issues with unit testing are:
From c2de22471172bcc1e3c328c0a171c0155fc98e06 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Fri, 8 Jul 2016 16:58:58 -0400 Subject: [PATCH 220/315] Verify opt method conversions for JSONArray and JSONObject --- src/test/java/org/json/junit/JSONArrayTest.java | 16 ++++++++++++++++ src/test/java/org/json/junit/JSONObjectTest.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 7643ee055..ef3a60881 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -3,6 +3,8 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.math.BigDecimal; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -414,6 +416,20 @@ public void opt() { assertTrue("Array opt string default implicit", "".equals(jsonArray.optString(-1))); } + + /** + * Verifies that the opt methods properly convert string values. + */ + @Test + public void optStringConversion(){ + JSONArray ja = new JSONArray("[\"123\",\"true\",\"false\"]"); + assertTrue("unexpected optBoolean value",ja.optBoolean(1,false)==true); + assertTrue("unexpected optBoolean value",ja.optBoolean(2,true)==false); + assertTrue("unexpected optInt value",ja.optInt(0,0)==123); + assertTrue("unexpected optLong value",ja.optLong(0,0)==123); + assertTrue("unexpected optDouble value",ja.optDouble(0,0.0)==123.0); + assertTrue("unexpected optBigInteger value",ja.optBigInteger(0,BigInteger.ZERO).compareTo(new BigInteger("123"))==0); + assertTrue("unexpected optBigDecimal value",ja.optBigDecimal(0,BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); } /** * Exercise the JSONArray.put(value) method with various parameters diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index d55013948..08ec96469 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1735,6 +1735,22 @@ public void jsonObjectOptDefault() { assertTrue("optString() should return default string", "hi".equals(jsonObject.optString("hiKey", "hi"))); } + + /** + * Verifies that the opt methods properly convert string values. + */ + @Test + public void jsonObjectOptStringConversion() { + JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\"}"); + assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true); + assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false); + assertTrue("unexpected optInt value",jo.optInt("int",0)==123); + assertTrue("unexpected optLong value",jo.optLong("int",0)==123); + assertTrue("unexpected optDouble value",jo.optDouble("int",0.0)==123.0); + assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0); + assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); + + } /** * Confirm behavior when JSONObject put(key, null object) is called From 215321cd28008cfd5b2192efa16cf10f737206c3 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 18 Jul 2016 15:01:36 -0400 Subject: [PATCH 221/315] updates Test cases to support new JSONML and XML conversion options --- src/test/java/org/json/junit/JSONMLTest.java | 37 ++++++++++++++++ src/test/java/org/json/junit/XMLTest.java | 44 ++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 953a39dd6..12985912d 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -698,4 +698,41 @@ public void commentsInXML() { Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } + /** + * JSON string with lost leading zero and converted "True" to true. See test + * result in comment below. + */ + @Test + public void testToJSONArray_jsonOutput() { + final String originalXml = "011000True"; + final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]"; + final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false); + assertEquals(expectedJsonString, actualJsonOutput.toString()); + } + + /** + * JSON string cannot be reverted to original xml. See test result in + * comment below. + */ + @Test + public void testToJSONArray_reversibility() { + final String originalXml = "011000True"; + final String revertedXml = JSONML.toString(JSONML.toJSONArray(originalXml, false)); + assertNotEquals(revertedXml, originalXml); + } + + /** + * test passes when using the new method toJsonML. + */ + @Test + public void testToJsonML() { + final String originalXml = "011000True"; + final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]"; + final JSONArray json = JSONML.toJSONArray(originalXml,true); + assertEquals(expectedJsonString, json.toString()); + + final String reverseXml = JSONML.toString(json); + assertEquals(originalXml, reverseXml); + } + } diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index d35c8ac23..2f3fea752 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1,12 +1,14 @@ package org.json.junit; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import java.io.IOException; import org.json.JSONArray; import org.json.JSONException; +import org.json.JSONML; import org.json.JSONObject; import org.json.XML; import org.junit.Rule; @@ -723,4 +725,46 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) { } */ } + + /** + * JSON string lost leading zero and converted "True" to true. + */ + @Test + public void testToJSONArray_jsonOutput() { + final String originalXml = "011000True"; + final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"; + final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false); + + assertEquals(expectedJsonString, actualJsonOutput.toString()); + } + + /** + * JSON string cannot be reverted to original xml. + */ + @Test + public void testToJSONArray_reversibility() { + final String originalXml = "011000True"; + final String revertedXml = XML.toString(XML.toJSONObject(originalXml, false)); + + assertNotEquals(revertedXml, originalXml); + } + + /** + * test passes when using the new method toJsonArray. + */ + @Test + public void testToJsonXML() { + final String originalXml = "011000True"; + final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}"; + + final JSONObject json = XML.toJSONObject(originalXml,true); + assertEquals(expectedJsonString, json.toString()); + + final String reverseXml = XML.toString(json); + // this reversal isn't exactly the same. use JSONML for an exact reversal + final String expectedReverseXml = "01011000True"; + + assertEquals(expectedReverseXml, reverseXml); + } + } \ No newline at end of file From c3ba4bdbe5321c0cf2eeae9829e8a6508cd4665b Mon Sep 17 00:00:00 2001 From: Nicholas Cull Date: Sat, 23 Jul 2016 19:12:51 +1000 Subject: [PATCH 222/315] Nesting depth test works as expected. --- .../java/org/json/junit/JSONStringerTest.java | 59 ++++++++++++++++--- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/src/test/java/org/json/junit/JSONStringerTest.java b/src/test/java/org/json/junit/JSONStringerTest.java index 19b46de73..d4376df66 100644 --- a/src/test/java/org/json/junit/JSONStringerTest.java +++ b/src/test/java/org/json/junit/JSONStringerTest.java @@ -27,7 +27,7 @@ public void nullKeyException() { jsonStringer.key(null); assertTrue("Expected an exception", false); } catch (JSONException e) { - assertTrue("Expected an exception message", + assertTrue("Expected an exception message", "Null key.". equals(e.getMessage())); } @@ -44,7 +44,7 @@ public void outOfSequenceException() { jsonStringer.key("hi"); assertTrue("Expected an exception", false); } catch (JSONException e) { - assertTrue("Expected an exception message", + assertTrue("Expected an exception message", "Misplaced key.". equals(e.getMessage())); } @@ -61,7 +61,7 @@ public void missplacedArrayException() { try { jsonStringer.array(); } catch (JSONException e) { - assertTrue("Expected an exception message", + assertTrue("Expected an exception message", "Misplaced array.". equals(e.getMessage())); } @@ -78,7 +78,7 @@ public void missplacedEndArrayException() { try { jsonStringer.endArray(); } catch (JSONException e) { - assertTrue("Expected an exception message", + assertTrue("Expected an exception message", "Misplaced endArray.". equals(e.getMessage())); } @@ -95,7 +95,7 @@ public void missplacedEndObjectException() { try { jsonStringer.endObject(); } catch (JSONException e) { - assertTrue("Expected an exception message", + assertTrue("Expected an exception message", "Misplaced endObject.". equals(e.getMessage())); } @@ -112,7 +112,7 @@ public void missplacedObjectException() { try { jsonStringer.object(); } catch (JSONException e) { - assertTrue("Expected an exception message", + assertTrue("Expected an exception message", "Misplaced object.". equals(e.getMessage())); } @@ -125,7 +125,47 @@ public void missplacedObjectException() { @Test public void exceedNestDepthException() { try { - new JSONStringer().object(). + JSONStringer s = new JSONStringer(); + s.object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). + key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(); + s.key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). @@ -165,9 +205,10 @@ public void exceedNestDepthException() { key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(). key("k").object().key("k").object().key("k").object().key("k").object().key("k").object(); + fail("Expected an exception message"); } catch (JSONException e) { - assertTrue("Expected an exception message", - "". + assertTrue("Expected an exception message", + "Nesting too deep.". equals(e.getMessage())); } } From 72c2b911bfba67472c6d03bdb4ffa12c47b56b70 Mon Sep 17 00:00:00 2001 From: Nicholas Cull Date: Sat, 23 Jul 2016 22:33:19 +1000 Subject: [PATCH 223/315] Tests for toString(), write(), toList(), and toMap(). Explicitly test variations of toString() and write() for different indent levels, and different method overloads. Also create some tests for the new toList() and toMap() methods for coverage improvements to JSONArray and JSONObject. --- .../java/org/json/junit/JSONArrayTest.java | 195 ++++++++++++++++++ .../java/org/json/junit/JSONObjectTest.java | 180 +++++++++++++++- 2 files changed, 373 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index ef3a60881..c818e8b80 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -2,7 +2,10 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import java.io.StringWriter; +import java.io.Writer; import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; @@ -639,6 +642,71 @@ public void notSimilar() { !jsonArray.similar(otherJsonArray)); } + /** + * Exercise JSONArray toString() method with various indent levels. + */ + @Test + public void jsonArrayToStringIndent() { + String jsonArray0Str = + "[" + + "[1,2," + + "{\"key3\":true}" + + "]," + + "{\"key1\":\"val1\",\"key2\":" + + "{\"key2\":\"val2\"}" + + "}," + + "[" + + "[1,2.1]" + + "," + + "[null]" + + "]" + + "]"; + + String jsonArray1Str = + "[\n" + + " [\n" + + " 1,\n" + + " 2,\n" + + " {\"key3\": true}\n" + + " ],\n" + + " {\n" + + " \"key1\": \"val1\",\n" + + " \"key2\": {\"key2\": \"val2\"}\n" + + " },\n" + + " [\n" + + " [\n" + + " 1,\n" + + " 2.1\n" + + " ],\n" + + " [null]\n" + + " ]\n" + + "]"; + String jsonArray4Str = + "[\n" + + " [\n" + + " 1,\n" + + " 2,\n" + + " {\"key3\": true}\n" + + " ],\n" + + " {\n" + + " \"key1\": \"val1\",\n" + + " \"key2\": {\"key2\": \"val2\"}\n" + + " },\n" + + " [\n" + + " [\n" + + " 1,\n" + + " 2.1\n" + + " ],\n" + + " [null]\n" + + " ]\n" + + "]"; + JSONArray jsonArray = new JSONArray(jsonArray0Str); + assertEquals(jsonArray0Str, jsonArray.toString()); + assertEquals(jsonArray0Str, jsonArray.toString(0)); + assertEquals(jsonArray1Str, jsonArray.toString(1)); + assertEquals(jsonArray4Str, jsonArray.toString(4)); + } + /** * Convert an empty JSONArray to JSONObject */ @@ -726,4 +794,131 @@ public void optQueryWithNoResult() { public void optQueryWithSyntaxError() { new JSONArray().optQuery("invalid"); } + + + /** + * Exercise the JSONArray write() method + */ + @Test + public void write() { + String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]"; + String expectedStr = str; + JSONArray jsonArray = new JSONArray(str); + StringWriter stringWriter = new StringWriter(); + Writer writer = jsonArray.write(stringWriter); + String actualStr = writer.toString(); + assertTrue("write() expected " + expectedStr + + "but found " + actualStr, + expectedStr.equals(actualStr)); + StringBuilder stringBuilder = new StringBuilder(); + Appendable appendable = jsonArray.write(stringBuilder); + actualStr = appendable.toString(); + assertTrue("write() expected " + expectedStr + + "but found " + actualStr, + expectedStr.equals(actualStr)); + } + + /** + * Exercise the JSONArray write(Appendable, int, int) method + */ + @Test + public void write3Param() { + String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]"; + String str2 = + "[\n" + + " \"value1\",\n" + + " \"value2\",\n" + + " {\n" + + " \"key1\": 1,\n" + + " \"key2\": false,\n" + + " \"key3\": 3.14\n" + + " }\n" + + " ]"; + String expectedStr = str0; + JSONArray jsonArray = new JSONArray(str0); + StringWriter stringWriter = new StringWriter(); + Writer writer = jsonArray.write(stringWriter, 0, 0); + String actualStr = writer.toString(); + assertEquals(expectedStr, actualStr); + expectedStr = str0; + StringBuilder stringBuilder = new StringBuilder(); + Appendable appendable = jsonArray.write(stringBuilder, 0, 0); + actualStr = appendable.toString(); + assertEquals(expectedStr, actualStr); + expectedStr = str2; + stringBuilder = new StringBuilder(); + appendable = jsonArray.write(stringBuilder, 2, 1); + actualStr = appendable.toString(); + assertEquals(expectedStr, actualStr); + } + + /** + * Exercise JSONArray toString() method with various indent levels. + */ + @Test + public void toList() { + String jsonArrayStr = + "[" + + "[1,2," + + "{\"key3\":true}" + + "]," + + "{\"key1\":\"val1\",\"key2\":" + + "{\"key2\":null}," + + "\"key3\":42,\"key4\":[]" + + "}," + + "[" + + "[\"value1\",2.1]" + + "," + + "[null]" + + "]" + + "]"; + + JSONArray jsonArray = new JSONArray(jsonArrayStr); + List list = jsonArray.toList(); + + assertTrue("List should not be null", list != null); + assertTrue("List should have 3 elements", list.size() == 3); + + List val1List = (List) list.get(0); + assertTrue("val1 should not be null", val1List != null); + assertTrue("val1 should have 3 elements", val1List.size() == 3); + + assertTrue("val1 value 1 should be 1", val1List.get(0).equals(Integer.valueOf(1))); + assertTrue("val1 value 2 should be 2", val1List.get(1).equals(Integer.valueOf(2))); + + Map key1Value3Map = (Map)val1List.get(2); + assertTrue("Map should not be null", key1Value3Map != null); + assertTrue("Map should have 1 element", key1Value3Map.size() == 1); + assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE)); + + Map val2Map = (Map) list.get(1); + assertTrue("val2 should not be null", val2Map != null); + assertTrue("val2 should have 4 elements", val2Map.size() == 4); + assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1")); + assertTrue("val2 map key 3 should be 42", val2Map.get("key3").equals(Integer.valueOf(42))); + + Map val2Key2Map = (Map)val2Map.get("key2"); + assertTrue("val2 map key 2 should not be null", val2Key2Map != null); + assertTrue("val2 map key 2 should have an entry", val2Key2Map.containsKey("key2")); + assertTrue("val2 map key 2 value should be null", val2Key2Map.get("key2") == null); + + List val2Key4List = (List)val2Map.get("key4"); + assertTrue("val2 map key 4 should not be null", val2Key4List != null); + assertTrue("val2 map key 4 should be empty", val2Key4List.isEmpty()); + + List val3List = (List) list.get(2); + assertTrue("val3 should not be null", val3List != null); + assertTrue("val3 should have 2 elements", val3List.size() == 2); + + List val3Val1List = (List)val3List.get(0); + assertTrue("val3 list val 1 should not be null", val3Val1List != null); + assertTrue("val3 list val 1 should have 2 elements", val3Val1List.size() == 2); + assertTrue("val3 list val 1 list element 1 should be value1", val3Val1List.get(0).equals("value1")); + assertTrue("val3 list val 1 list element 2 should be 2.1", val3Val1List.get(1).equals(Double.valueOf("2.1"))); + + List val3Val2List = (List)val3List.get(1); + assertTrue("val3 list val 2 should not be null", val3Val2List != null); + assertTrue("val3 list val 2 should have 1 element", val3Val2List.size() == 1); + assertTrue("val3 list val 2 list element 1 should be null", val3Val2List.get(0) == null); + } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 08ec96469..73029e7bd 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -1371,6 +1372,74 @@ public void jsonObjectToString() { assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4"))); } + /** + * Exercise JSONObject toString() method with various indent levels. + */ + @Test + public void jsonObjectToStringIndent() { + String jsonObject0Str = + "{"+ + "\"key1\":" + + "[1,2," + + "{\"key3\":true}" + + "],"+ + "\"key2\":" + + "{\"key1\":\"val1\",\"key2\":" + + "{\"key2\":\"val2\"}" + + "},"+ + "\"key3\":" + + "[" + + "[1,2.1]" + + "," + + "[null]" + + "]"+ + "}"; + + String jsonObject1Str = + "{\n" + + " \"key1\": [\n" + + " 1,\n" + + " 2,\n" + + " {\"key3\": true}\n" + + " ],\n" + + " \"key2\": {\n" + + " \"key1\": \"val1\",\n" + + " \"key2\": {\"key2\": \"val2\"}\n" + + " },\n" + + " \"key3\": [\n" + + " [\n" + + " 1,\n" + + " 2.1\n" + + " ],\n" + + " [null]\n" + + " ]\n" + + "}"; + String jsonObject4Str = + "{\n" + + " \"key1\": [\n" + + " 1,\n" + + " 2,\n" + + " {\"key3\": true}\n" + + " ],\n" + + " \"key2\": {\n" + + " \"key1\": \"val1\",\n" + + " \"key2\": {\"key2\": \"val2\"}\n" + + " },\n" + + " \"key3\": [\n" + + " [\n" + + " 1,\n" + + " 2.1\n" + + " ],\n" + + " [null]\n" + + " ]\n" + + "}"; + JSONObject jsonObject = new JSONObject(jsonObject0Str); + assertEquals(jsonObject0Str, jsonObject.toString()); + assertEquals(jsonObject0Str, jsonObject.toString(0)); + assertEquals(jsonObject1Str, jsonObject.toString(1)); + assertEquals(jsonObject4Str, jsonObject.toString(4)); + } + /** * Explores how JSONObject handles maps. Insert a string/string map * as a value in a JSONObject. It will remain a map. Convert the @@ -1441,7 +1510,7 @@ public void valueToString() { String jsonArrayStr = "[1,2,3]"; JSONArray jsonArray = new JSONArray(jsonArrayStr); - assertTrue("jsonArra valueToString() incorrect", + assertTrue("jsonArray valueToString() incorrect", JSONObject.valueToString(jsonArray).equals(jsonArray.toString())); Map map = new HashMap(); map.put("key1", "val1"); @@ -1840,7 +1909,7 @@ public void toJSONArray() { */ @Test public void write() { - String str = "{\"key\":\"value\"}"; + String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}"; String expectedStr = str; JSONObject jsonObject = new JSONObject(str); StringWriter stringWriter = new StringWriter(); @@ -1849,6 +1918,45 @@ public void write() { assertTrue("write() expected " +expectedStr+ "but found " +actualStr, expectedStr.equals(actualStr)); + StringBuilder stringBuilder = new StringBuilder(); + Appendable appendable = jsonObject.write(stringBuilder); + actualStr = appendable.toString(); + assertTrue("write() expected " +expectedStr+ + "but found " +actualStr, + expectedStr.equals(actualStr)); + } + + /** + * Exercise the JSONObject write(Appendable, int, int) method + */ + @Test + public void write3Param() { + String str0 = "{\"key1\":\"value1\",\"key2\":[1,false,3.14]}"; + String str2 = + "{\n" + + " \"key1\": \"value1\",\n" + + " \"key2\": [\n" + + " 1,\n" + + " false,\n" + + " 3.14\n" + + " ]\n" + + " }"; + String expectedStr = str0; + JSONObject jsonObject = new JSONObject(str0); + StringWriter stringWriter = new StringWriter(); + Writer writer = jsonObject.write(stringWriter,0,0); + String actualStr = writer.toString(); + assertEquals(expectedStr, actualStr); + expectedStr = str0; + StringBuilder stringBuilder = new StringBuilder(); + Appendable appendable = jsonObject.write(stringBuilder,0,0); + actualStr = appendable.toString(); + assertEquals(expectedStr, actualStr); + expectedStr = str2; + stringBuilder = new StringBuilder(); + appendable = jsonObject.write(stringBuilder,2,1); + actualStr = appendable.toString(); + assertEquals(expectedStr, actualStr); } /** @@ -1966,4 +2074,72 @@ public void invalidEscapeSequence() { String json = "{ \"\\url\": \"value\" }"; new JSONObject(json); } + + /** + * Exercise JSONObject toMap() method. + */ + @Test + public void toMap() { + String jsonObjectStr = + "{" + + "\"key1\":" + + "[1,2," + + "{\"key3\":true}" + + "]," + + "\"key2\":" + + "{\"key1\":\"val1\",\"key2\":" + + "{\"key2\":null}," + + "\"key3\":42" + + "}," + + "\"key3\":" + + "[" + + "[\"value1\",2.1]" + + "," + + "[null]" + + "]" + + "}"; + + JSONObject jsonObject = new JSONObject(jsonObjectStr); + Map map = jsonObject.toMap(); + + assertTrue("Map should not be null", map != null); + assertTrue("Map should have 3 elements", map.size() == 3); + + List key1List = (List)map.get("key1"); + assertTrue("key1 should not be null", key1List != null); + assertTrue("key1 list should have 3 elements", key1List.size() == 3); + assertTrue("key1 value 1 should be 1", key1List.get(0).equals(Integer.valueOf(1))); + assertTrue("key1 value 2 should be 2", key1List.get(1).equals(Integer.valueOf(2))); + + Map key1Value3Map = (Map)key1List.get(2); + assertTrue("Map should not be null", key1Value3Map != null); + assertTrue("Map should have 1 element", key1Value3Map.size() == 1); + assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE)); + + Map key2Map = (Map)map.get("key2"); + assertTrue("key2 should not be null", key2Map != null); + assertTrue("key2 map should have 3 elements", key2Map.size() == 3); + assertTrue("key2 map key 1 should be val1", key2Map.get("key1").equals("val1")); + assertTrue("key2 map key 3 should be 42", key2Map.get("key3").equals(Integer.valueOf(42))); + + Map key2Val2Map = (Map)key2Map.get("key2"); + assertTrue("key2 map key 2 should not be null", key2Val2Map != null); + assertTrue("key2 map key 2 should have an entry", key2Val2Map.containsKey("key2")); + assertTrue("key2 map key 2 value should be null", key2Val2Map.get("key2") == null); + + List key3List = (List)map.get("key3"); + assertTrue("key3 should not be null", key3List != null); + assertTrue("key3 list should have 3 elements", key3List.size() == 2); + + List key3Val1List = (List)key3List.get(0); + assertTrue("key3 list val 1 should not be null", key3Val1List != null); + assertTrue("key3 list val 1 should have 2 elements", key3Val1List.size() == 2); + assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1")); + assertTrue("key3 list val 1 list element 2 should be 2.1", key3Val1List.get(1).equals(Double.valueOf("2.1"))); + + List key3Val2List = (List)key3List.get(1); + assertTrue("key3 list val 2 should not be null", key3Val2List != null); + assertTrue("key3 list val 2 should have 1 element", key3Val2List.size() == 1); + assertTrue("key3 list val 2 list element 1 should be null", key3Val2List.get(0) == null); + } } From ae77b5cd83121555152a13407fce230992afc0c0 Mon Sep 17 00:00:00 2001 From: Nicholas Cull Date: Sat, 23 Jul 2016 22:51:50 +1000 Subject: [PATCH 224/315] Tests for deep copy and mutability of toList() and toMap(). Both toMap() and toList() return deep copies, which are also mutable. That is, any changes to the JSONObject or JSONArray do not affect the newly create Map or List, and vice-versa. The resulting objects can be altered. --- src/test/java/org/json/junit/JSONArrayTest.java | 8 ++++++++ src/test/java/org/json/junit/JSONObjectTest.java | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index c818e8b80..9f0e773fc 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -920,5 +920,13 @@ public void toList() { assertTrue("val3 list val 2 should not be null", val3Val2List != null); assertTrue("val3 list val 2 should have 1 element", val3Val2List.size() == 1); assertTrue("val3 list val 2 list element 1 should be null", val3Val2List.get(0) == null); + + // assert that toList() is a deep copy + jsonArray.getJSONObject(1).put("key1", "still val1"); + assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1")); + + // assert that the new list is mutable + assertTrue("Removing an entry should succeed", list.remove(2) != null); + assertTrue("List should have 2 elements", list.size() == 2); } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 73029e7bd..10405b0a8 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2141,5 +2141,14 @@ public void toMap() { assertTrue("key3 list val 2 should not be null", key3Val2List != null); assertTrue("key3 list val 2 should have 1 element", key3Val2List.size() == 1); assertTrue("key3 list val 2 list element 1 should be null", key3Val2List.get(0) == null); + + // Assert that toMap() is a deep copy + jsonObject.getJSONArray("key3").getJSONArray(0).put(0, "still value 1"); + assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1")); + + // assert that the new map is mutable + assertTrue("Removing a key should succeed", map.remove("key3") != null); + assertTrue("Map should have 2 elements", map.size() == 2); + } } From 6b4edbd40c115e2895af9e3c68908e5ede7ac03e Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 23 Jul 2016 10:02:19 -0500 Subject: [PATCH 225/315] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 385c222f2..2e2fcc8ac 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ git clone https://github.com/stleary/JSON-Java-unit-test.git . ```` \# Create a directory structure for the JSON-Java code ```` -# Windows version -md /s src\main\java\org\json +# Windows 10 version +mkdir src\main\java\org\json # *nix version mkdir -p src/main/java/org/json ```` From 2307f6f85e535df6b925056667a7b8f4671e1a99 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 23 Jul 2016 10:12:04 -0500 Subject: [PATCH 226/315] Update README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 2e2fcc8ac..e45f7bff4 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,13 @@ git clone https://github.com/stleary/JSON-Java.git src/main/java/org/json gradle clean build test jacocoTestReport ```` +\# Eclipse setup requires the Gradle IDE plug-in +\# I use Gradle IDE 3.6.4.201503050952-RELEASE org.springsource.ide.eclipse.gradle.feature.feature.group Pivotal Software, Inc. +```` +File > Import > Gradle project > (navigate to your directory) > Build Model > (Select your directory) > Finish +(It is not necessary to run "gradle eclipse" on the project, from the command line) +```` + Unit test results will be in build\reports\tests\index.html
Code coverage will be in build\reports\jacoco\html\index.html From cdfdaba95bb489475ea0aacdbb2f940e1a3dafe0 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 23 Jul 2016 10:12:33 -0500 Subject: [PATCH 227/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e45f7bff4..54e085d05 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ git clone https://github.com/stleary/JSON-Java.git src/main/java/org/json gradle clean build test jacocoTestReport ```` -\# Eclipse setup requires the Gradle IDE plug-in +\# Eclipse setup requires the Gradle IDE plug-in
\# I use Gradle IDE 3.6.4.201503050952-RELEASE org.springsource.ide.eclipse.gradle.feature.feature.group Pivotal Software, Inc. ```` File > Import > Gradle project > (navigate to your directory) > Build Model > (Select your directory) > Finish From 5d8ea6fa4e91467adce884666b0a26c694701800 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 23 Jul 2016 10:13:21 -0500 Subject: [PATCH 228/315] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 54e085d05..faf400a0b 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,8 @@ gradle clean build test jacocoTestReport ```` \# Eclipse setup requires the Gradle IDE plug-in
-\# I use Gradle IDE 3.6.4.201503050952-RELEASE org.springsource.ide.eclipse.gradle.feature.feature.group Pivotal Software, Inc. +\# I use Gradle IDE 3.6.4.201503050952-RELEASE org.springsource.ide.eclipse.gradle.feature.feature.group Pivotal Software, Inc.
+\# From the Eclipse IDE: ```` File > Import > Gradle project > (navigate to your directory) > Build Model > (Select your directory) > Finish (It is not necessary to run "gradle eclipse" on the project, from the command line) From ffcfa66d7798061df9a5450ce9579d6ee6bbce9f Mon Sep 17 00:00:00 2001 From: Nicholas Cull Date: Sun, 24 Jul 2016 18:56:08 +1000 Subject: [PATCH 229/315] Add JSONString test class. This set of tests demonstrates what happens when JSONString returns various results from its toJSONString() method. Tests for null returns and exceptions thrown. Also tests what happens for non-JSONString objects. The intent is to cover JSONObject's valueToString() and writeValue() methods. --- .../java/org/json/junit/JSONStringTest.java | 310 ++++++++++++++++++ .../java/org/json/junit/JunitTestSuite.java | 7 +- 2 files changed, 314 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/json/junit/JSONStringTest.java diff --git a/src/test/java/org/json/junit/JSONStringTest.java b/src/test/java/org/json/junit/JSONStringTest.java new file mode 100644 index 000000000..c7d9d3e2f --- /dev/null +++ b/src/test/java/org/json/junit/JSONStringTest.java @@ -0,0 +1,310 @@ +package org.json.junit; + +import static org.junit.Assert.*; + +import java.io.StringWriter; +import java.util.*; + +import org.json.*; +import org.junit.Test; + +/** + * Tests for JSONString implementations, and the difference between + * {@link JSONObject#valueToString} and {@link JSONObject#writeValue}. + */ +public class JSONStringTest { + + /** + * This tests the JSONObject.writeValue() method. We can't test directly + * due to it being a package-protected method. Instead, we can call + * JSONArray.write(), which delegates the writing of each entry to + * writeValue(). + */ + @Test + public void writeValues() throws Exception { + JSONArray jsonArray = new JSONArray(); + jsonArray.put((Object)null); + + StringWriter writer = new StringWriter(); + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[null]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put(JSONObject.NULL); + writer = new StringWriter(); + output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[null]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put(new JSONObject()); + writer = new StringWriter(); + output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[{}]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put(new JSONArray()); + writer = new StringWriter(); + output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[[]]".equals(output)); + + jsonArray = new JSONArray(); + Map singleMap = Collections.singletonMap("key1", "value1"); + jsonArray.put((Object)singleMap); + writer = new StringWriter(); + output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output)); + + jsonArray = new JSONArray(); + List singleList = Collections.singletonList("entry1"); + jsonArray.put((Object)singleList); + writer = new StringWriter(); + output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output)); + + jsonArray = new JSONArray(); + int[] intArray = new int[] { 1, 2, 3 }; + jsonArray.put(intArray); + writer = new StringWriter(); + output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[[1,2,3]]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put(24); + writer = new StringWriter(); + output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[24]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put("string value"); + writer = new StringWriter(); + output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[\"string value\"]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put(true); + writer = new StringWriter(); + output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[true]".equals(output)); + + } + + /** + * This tests the JSONObject.valueToString() method. These should be + * identical to the values above, except for the enclosing [ and ]. + */ + @Test + public void valuesToString() throws Exception { + + String output = JSONObject.valueToString(null); + assertTrue("String values should be equal", "null".equals(output)); + + output = JSONObject.valueToString(JSONObject.NULL); + assertTrue("String values should be equal", "null".equals(output)); + + output = JSONObject.valueToString(new JSONObject()); + assertTrue("String values should be equal", "{}".equals(output)); + + output = JSONObject.valueToString(new JSONArray()); + assertTrue("String values should be equal", "[]".equals(output)); + + Map singleMap = Collections.singletonMap("key1", "value1"); + output = JSONObject.valueToString(singleMap); + assertTrue("String values should be equal", "{\"key1\":\"value1\"}".equals(output)); + + List singleList = Collections.singletonList("entry1"); + output = JSONObject.valueToString(singleList); + assertTrue("String values should be equal", "[\"entry1\"]".equals(output)); + + int[] intArray = new int[] { 1, 2, 3 }; + output = JSONObject.valueToString(intArray); + assertTrue("String values should be equal", "[1,2,3]".equals(output)); + + output = JSONObject.valueToString(24); + assertTrue("String values should be equal", "24".equals(output)); + + output = JSONObject.valueToString("string value"); + assertTrue("String values should be equal", "\"string value\"".equals(output)); + + output = JSONObject.valueToString(true); + assertTrue("String values should be equal", "true".equals(output)); + + } + + /** + * Test what happens when toJSONString() returns a well-formed JSON value. + * This is the usual case. + */ + @Test + public void testJSONStringValue() throws Exception { + JSONStringValue jsonString = new JSONStringValue(); + JSONArray jsonArray = new JSONArray(); + + jsonArray.put(jsonString); + + StringWriter writer = new StringWriter(); + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output)); + + output = JSONObject.valueToString(jsonString); + assertTrue("String values should be equal", "\"the JSON string value\"".equals(output)); + } + + /** + * Test what happens when toJSONString() returns null. In one case, + * use the object's toString() method. In the other, throw a JSONException. + */ + @Test + public void testJSONNullStringValue() throws Exception { + JSONNullStringValue jsonString = new JSONNullStringValue(); + JSONArray jsonArray = new JSONArray(); + + jsonArray.put(jsonString); + + StringWriter writer = new StringWriter(); + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[\"the toString value\"]".equals(output)); + + // The first different between writeValue() and valueToString(): + // in this case, valueToString throws a JSONException + try { + output = JSONObject.valueToString(jsonString); + fail("Expected an exception, got a String value"); + } catch (Exception e) { + assertTrue("Expected JSONException", e instanceof JSONException); + assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage())); + } + } + + /** + * Test what happens when toJSONString() returns an exception. In both + * cases, a JSONException is thrown, with the cause and message set from + * the original exception. + */ + @Test + public void testJSONStringExceptionValue() throws Exception { + JSONStringExceptionValue jsonString = new JSONStringExceptionValue(); + JSONArray jsonArray = new JSONArray(); + + jsonArray.put(jsonString); + + StringWriter writer = new StringWriter(); + String output = null; + try { + output = jsonArray.write(writer).toString(); + fail("Expected an exception, got a String value"); + } catch (Exception e) { + assertTrue("Expected JSONException", e instanceof JSONException); + assertTrue("Exception message does not match", "the exception value".equals(e.getMessage())); + } + + try { + output = JSONObject.valueToString(jsonString); + fail("Expected an exception, got a String value"); + } catch (Exception e) { + assertTrue("Expected JSONException", e instanceof JSONException); + assertTrue("Exception message does not match", "the exception value".equals(e.getMessage())); + } + } + + /** + * Test what happens when a Java object's toString() returns a String value. + * This is the usual case. + */ + @Test + public void testStringValue() throws Exception { + StringValue nonJsonString = new StringValue(); + JSONArray jsonArray = new JSONArray(); + + jsonArray.put(nonJsonString); + + StringWriter writer = new StringWriter(); + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output)); + + output = JSONObject.valueToString(nonJsonString); + assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output)); + } + + /** + * Test what happens when a Java object's toString() returns null. + * Defaults to empty string. + */ + @Test + public void testNullStringValue() throws Exception { + NullStringValue nonJsonString = new NullStringValue(); + JSONArray jsonArray = new JSONArray(); + + jsonArray.put(nonJsonString); + + StringWriter writer = new StringWriter(); + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[\"\"]".equals(output)); + + output = JSONObject.valueToString(nonJsonString); + assertTrue("String values should be equal", "\"\"".equals(output)); + } + + /** + * A JSONString that returns a valid JSON string value. + */ + private static final class JSONStringValue implements JSONString { + + @Override + public String toJSONString() { + return "\"the JSON string value\""; + } + + @Override + public String toString() { + return "the toString value for JSONStringValue"; + } + } + + /** + * A JSONString that returns null when calling toJSONString(). + */ + private static final class JSONNullStringValue implements JSONString { + + @Override + public String toJSONString() { + return null; + } + + @Override + public String toString() { + return "the toString value"; + } + } + + /** + * A JSONString that throw an exception when calling toJSONString(). + */ + private static final class JSONStringExceptionValue implements JSONString { + + @Override + public String toJSONString() { + throw new IllegalStateException("the exception value"); + } + + @Override + public String toString() { + return "the toString value for JSONStringExceptionValue"; + } + } + + public static final class StringValue { + + @Override + public String toString() { + return "the toString value for StringValue"; + } + } + + public static final class NullStringValue { + + @Override + public String toString() { + return null; + } + } +} diff --git a/src/test/java/org/json/junit/JunitTestSuite.java b/src/test/java/org/json/junit/JunitTestSuite.java index 38639314c..3a7223e19 100644 --- a/src/test/java/org/json/junit/JunitTestSuite.java +++ b/src/test/java/org/json/junit/JunitTestSuite.java @@ -15,7 +15,8 @@ JSONObjectTest.class, JSONArrayTest.class, EnumTest.class, - JSONPointerTest.class + JSONPointerTest.class, + JSONStringTest.class }) -public class JunitTestSuite { -} +public class JunitTestSuite { +} From 1246e12827fce05f70ce6758df647ea436d6578b Mon Sep 17 00:00:00 2001 From: Nicholas Cull Date: Sun, 24 Jul 2016 19:39:52 +1000 Subject: [PATCH 230/315] Factor out Writer from Appendable tests. --- .../java/org/json/junit/JSONArrayTest.java | 56 ++++++++++++++++--- .../java/org/json/junit/JSONObjectTest.java | 53 +++++++++++++++--- 2 files changed, 94 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 9f0e773fc..13dbb3954 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -802,24 +802,36 @@ public void optQueryWithSyntaxError() { @Test public void write() { String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]"; - String expectedStr = str; JSONArray jsonArray = new JSONArray(str); + String expectedStr = str; StringWriter stringWriter = new StringWriter(); Writer writer = jsonArray.write(stringWriter); String actualStr = writer.toString(); assertTrue("write() expected " + expectedStr + - "but found " + actualStr, + " but found " + actualStr, expectedStr.equals(actualStr)); + } + + /** + * Exercise the JSONArray write() method using Appendable. + */ +/* + @Test + public void writeAppendable() { + String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]"; + JSONArray jsonArray = new JSONArray(str); + String expectedStr = str; StringBuilder stringBuilder = new StringBuilder(); Appendable appendable = jsonArray.write(stringBuilder); - actualStr = appendable.toString(); + String actualStr = appendable.toString(); assertTrue("write() expected " + expectedStr + - "but found " + actualStr, + " but found " + actualStr, expectedStr.equals(actualStr)); } +*/ /** - * Exercise the JSONArray write(Appendable, int, int) method + * Exercise the JSONArray write(Writer, int, int) method */ @Test public void write3Param() { @@ -834,23 +846,51 @@ public void write3Param() { " \"key3\": 3.14\n" + " }\n" + " ]"; - String expectedStr = str0; JSONArray jsonArray = new JSONArray(str0); + String expectedStr = str0; StringWriter stringWriter = new StringWriter(); Writer writer = jsonArray.write(stringWriter, 0, 0); String actualStr = writer.toString(); assertEquals(expectedStr, actualStr); - expectedStr = str0; + + expectedStr = str2; + stringWriter = new StringWriter(); + writer = jsonArray.write(stringWriter, 2, 1); + actualStr = writer.toString(); + assertEquals(expectedStr, actualStr); + } + + /** + * Exercise the JSONArray write(Appendable, int, int) method + */ +/* + @Test + public void write3ParamAppendable() { + String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]"; + String str2 = + "[\n" + + " \"value1\",\n" + + " \"value2\",\n" + + " {\n" + + " \"key1\": 1,\n" + + " \"key2\": false,\n" + + " \"key3\": 3.14\n" + + " }\n" + + " ]"; + JSONArray jsonArray = new JSONArray(str0); + String expectedStr = str0; StringBuilder stringBuilder = new StringBuilder(); Appendable appendable = jsonArray.write(stringBuilder, 0, 0); - actualStr = appendable.toString(); + String actualStr = appendable.toString(); assertEquals(expectedStr, actualStr); + expectedStr = str2; stringBuilder = new StringBuilder(); appendable = jsonArray.write(stringBuilder, 2, 1); actualStr = appendable.toString(); assertEquals(expectedStr, actualStr); } +*/ /** * Exercise JSONArray toString() method with various indent levels. diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 10405b0a8..df1b14187 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1916,18 +1916,30 @@ public void write() { Writer writer = jsonObject.write(stringWriter); String actualStr = writer.toString(); assertTrue("write() expected " +expectedStr+ - "but found " +actualStr, + " but found " +actualStr, expectedStr.equals(actualStr)); + } + + /** + * Exercise the JSONObject write() method + */ +/* + @Test + public void writeAppendable() { + String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}"; + String expectedStr = str; + JSONObject jsonObject = new JSONObject(str); StringBuilder stringBuilder = new StringBuilder(); Appendable appendable = jsonObject.write(stringBuilder); - actualStr = appendable.toString(); + String actualStr = appendable.toString(); assertTrue("write() expected " +expectedStr+ - "but found " +actualStr, + " but found " +actualStr, expectedStr.equals(actualStr)); } +*/ /** - * Exercise the JSONObject write(Appendable, int, int) method + * Exercise the JSONObject write(Writer, int, int) method */ @Test public void write3Param() { @@ -1941,23 +1953,50 @@ public void write3Param() { " 3.14\n" + " ]\n" + " }"; - String expectedStr = str0; JSONObject jsonObject = new JSONObject(str0); + String expectedStr = str0; StringWriter stringWriter = new StringWriter(); Writer writer = jsonObject.write(stringWriter,0,0); String actualStr = writer.toString(); assertEquals(expectedStr, actualStr); - expectedStr = str0; + + expectedStr = str2; + stringWriter = new StringWriter(); + writer = jsonObject.write(stringWriter,2,1); + actualStr = writer.toString(); + assertEquals(expectedStr, actualStr); + } + + /** + * Exercise the JSONObject write(Appendable, int, int) method + */ +/* + @Test + public void write3ParamAppendable() { + String str0 = "{\"key1\":\"value1\",\"key2\":[1,false,3.14]}"; + String str2 = + "{\n" + + " \"key1\": \"value1\",\n" + + " \"key2\": [\n" + + " 1,\n" + + " false,\n" + + " 3.14\n" + + " ]\n" + + " }"; + JSONObject jsonObject = new JSONObject(str0); + String expectedStr = str0; StringBuilder stringBuilder = new StringBuilder(); Appendable appendable = jsonObject.write(stringBuilder,0,0); - actualStr = appendable.toString(); + String actualStr = appendable.toString(); assertEquals(expectedStr, actualStr); + expectedStr = str2; stringBuilder = new StringBuilder(); appendable = jsonObject.write(stringBuilder,2,1); actualStr = appendable.toString(); assertEquals(expectedStr, actualStr); } +*/ /** * Exercise the JSONObject equals() method From efe33a1e370917ca571db5b95a0372b44a052afb Mon Sep 17 00:00:00 2001 From: Nicholas Cull Date: Sun, 24 Jul 2016 19:57:01 +1000 Subject: [PATCH 231/315] Fix comment. --- src/test/java/org/json/junit/JSONStringTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONStringTest.java b/src/test/java/org/json/junit/JSONStringTest.java index c7d9d3e2f..cba5d09c4 100644 --- a/src/test/java/org/json/junit/JSONStringTest.java +++ b/src/test/java/org/json/junit/JSONStringTest.java @@ -164,7 +164,7 @@ public void testJSONNullStringValue() throws Exception { String output = jsonArray.write(writer).toString(); assertTrue("String values should be equal", "[\"the toString value\"]".equals(output)); - // The first different between writeValue() and valueToString(): + // The only different between writeValue() and valueToString(): // in this case, valueToString throws a JSONException try { output = JSONObject.valueToString(jsonString); From e57881f8fa9d81ccda83e17d2379f3597b0af213 Mon Sep 17 00:00:00 2001 From: run2000 Date: Mon, 25 Jul 2016 09:44:43 +1000 Subject: [PATCH 232/315] Fail when exceptions are not thrown as expected The idiom was started in the first few methods, but not continued further down where JSONException was expected. False success may have resulted. --- src/test/java/org/json/junit/JSONStringerTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/java/org/json/junit/JSONStringerTest.java b/src/test/java/org/json/junit/JSONStringerTest.java index d4376df66..99cdd6f20 100644 --- a/src/test/java/org/json/junit/JSONStringerTest.java +++ b/src/test/java/org/json/junit/JSONStringerTest.java @@ -60,6 +60,7 @@ public void missplacedArrayException() { jsonStringer.object().endObject(); try { jsonStringer.array(); + assertTrue("Expected an exception", false); } catch (JSONException e) { assertTrue("Expected an exception message", "Misplaced array.". @@ -77,6 +78,7 @@ public void missplacedEndArrayException() { jsonStringer.object(); try { jsonStringer.endArray(); + assertTrue("Expected an exception", false); } catch (JSONException e) { assertTrue("Expected an exception message", "Misplaced endArray.". @@ -94,6 +96,7 @@ public void missplacedEndObjectException() { jsonStringer.array(); try { jsonStringer.endObject(); + assertTrue("Expected an exception", false); } catch (JSONException e) { assertTrue("Expected an exception message", "Misplaced endObject.". @@ -111,6 +114,7 @@ public void missplacedObjectException() { jsonStringer.object().endObject(); try { jsonStringer.object(); + assertTrue("Expected an exception", false); } catch (JSONException e) { assertTrue("Expected an exception message", "Misplaced object.". From a2c311527b1961ec8cefc20d8219bc72728bb7ec Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 9 Aug 2016 15:54:06 -0400 Subject: [PATCH 233/315] Updates tests to include all opt methods and verify for missing keys. --- .../java/org/json/junit/JSONArrayTest.java | 5 +- .../java/org/json/junit/JSONObjectTest.java | 48 +++++++++++++++++-- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index ef3a60881..6e9fd6a89 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -373,7 +373,10 @@ public void opt() { assertTrue("Array opt value out of range", null == jsonArray.opt(-1)); - assertTrue("Array opt boolean", + assertTrue("Array opt value out of range", + null == jsonArray.opt(jsonArray.length())); + + assertTrue("Array opt boolean", Boolean.TRUE == jsonArray.optBoolean(0)); assertTrue("Array opt boolean default", Boolean.FALSE == jsonArray.optBoolean(-1, Boolean.FALSE)); diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 08ec96469..61043ae88 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1714,20 +1714,28 @@ public void jsonObjectPutOnceNull() { } /** - * Exercise JSONObject opt(key, default) method + * Exercise JSONObject opt(key, default) method. */ @Test public void jsonObjectOptDefault() { - String str = "{\"myKey\": \"myval\"}"; + String str = "{\"myKey\": \"myval\", \"hiKey\": null}"; JSONObject jsonObject = new JSONObject(str); + assertTrue("optBigDecimal() should return default ", + BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); + assertTrue(" should return default ", + BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", - Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE)); - assertTrue("optInt() should return default int", - 42 == jsonObject.optInt("myKey", 42)); + true == jsonObject.optBoolean("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optEnum() should return default ", + MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); + assertTrue("optJSONArray() should return null ", + null==jsonObject.optJSONArray("myKey")); + assertTrue("optJSONObject() should return null ", + null==jsonObject.optJSONObject("myKey")); assertTrue("optLong() should return default long", 42 == jsonObject.optLong("myKey", 42)); assertTrue("optDouble() should return default double", @@ -1736,6 +1744,36 @@ public void jsonObjectOptDefault() { "hi".equals(jsonObject.optString("hiKey", "hi"))); } + /** + * Exercise JSONObject opt(key, default) method when the key doesn't exist. + */ + @Test + public void jsonObjectOptNoKey() { + + JSONObject jsonObject = new JSONObject(); + + assertTrue("optBigDecimal() should return default ", + BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); + assertTrue(" should return default ", + BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); + assertTrue("optBoolean() should return default boolean", + true == jsonObject.optBoolean("myKey", true)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optEnum() should return default ", + MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); + assertTrue("optJSONArray() should return null ", + null==jsonObject.optJSONArray("myKey")); + assertTrue("optJSONObject() should return null ", + null==jsonObject.optJSONObject("myKey")); + assertTrue("optLong() should return default long", + 42 == jsonObject.optLong("myKey", 42)); + assertTrue("optDouble() should return default double", + 42.3 == jsonObject.optDouble("myKey", 42.3)); + assertTrue("optString() should return default string", + "hi".equals(jsonObject.optString("hiKey", "hi"))); + } + /** * Verifies that the opt methods properly convert string values. */ From 80e36eb63c976079eda49cfc070fef5bdde89f0c Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 9 Aug 2016 15:59:27 -0400 Subject: [PATCH 234/315] Fixes error messages --- src/test/java/org/json/junit/JSONObjectTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 61043ae88..935b687f0 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1722,15 +1722,15 @@ public void jsonObjectOptDefault() { String str = "{\"myKey\": \"myval\", \"hiKey\": null}"; JSONObject jsonObject = new JSONObject(str); - assertTrue("optBigDecimal() should return default ", + assertTrue("optBigDecimal() should return default BigDecimal", BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); - assertTrue(" should return default ", + assertTrue("optBigInteger() should return default BigInteger", BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", true == jsonObject.optBoolean("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optEnum() should return default ", + assertTrue("optEnum() should return default Enum", MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); assertTrue("optJSONArray() should return null ", null==jsonObject.optJSONArray("myKey")); @@ -1752,15 +1752,15 @@ public void jsonObjectOptNoKey() { JSONObject jsonObject = new JSONObject(); - assertTrue("optBigDecimal() should return default ", + assertTrue("optBigDecimal() should return default BigDecimal", BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); - assertTrue(" should return default ", + assertTrue("optBigInteger() should return default BigInteger", BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", true == jsonObject.optBoolean("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optEnum() should return default ", + assertTrue("optEnum() should return default Enum", MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); assertTrue("optJSONArray() should return null ", null==jsonObject.optJSONArray("myKey")); From 8bae09f81bda5d12f1ea2d625097cdf1a113d84a Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 9 Aug 2016 16:11:46 -0400 Subject: [PATCH 235/315] removes unnecessary comparison to true --- src/test/java/org/json/junit/JSONObjectTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 935b687f0..d55f5df40 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1727,7 +1727,7 @@ public void jsonObjectOptDefault() { assertTrue("optBigInteger() should return default BigInteger", BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", - true == jsonObject.optBoolean("myKey", true)); + jsonObject.optBoolean("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); assertTrue("optEnum() should return default Enum", @@ -1757,7 +1757,7 @@ public void jsonObjectOptNoKey() { assertTrue("optBigInteger() should return default BigInteger", BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", - true == jsonObject.optBoolean("myKey", true)); + jsonObject.optBoolean("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); assertTrue("optEnum() should return default Enum", From 5779400f26716c6c8dfc5b39fc9d4f9406f8da22 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 11 Aug 2016 12:21:49 -0400 Subject: [PATCH 236/315] test updates to make sure Enums are handled consistently. --- src/test/java/org/json/junit/EnumTest.java | 94 +++++++++++++------ src/test/java/org/json/junit/MyEnumField.java | 3 + 2 files changed, 70 insertions(+), 27 deletions(-) diff --git a/src/test/java/org/json/junit/EnumTest.java b/src/test/java/org/json/junit/EnumTest.java index ff4b2940a..ab4a1e55a 100644 --- a/src/test/java/org/json/junit/EnumTest.java +++ b/src/test/java/org/json/junit/EnumTest.java @@ -1,13 +1,18 @@ package org.json.junit; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; -import java.util.*; +import java.util.EnumSet; +import java.util.List; +import java.util.Map; +import java.util.Set; -import org.json.*; -import org.junit.*; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Test; -import com.jayway.jsonpath.*; +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.JsonPath; /** * Enums are not explicitly supported in JSON-Java. But because enums act like @@ -50,11 +55,12 @@ public void jsonObjectFromEnum() { // validate JSON content doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expecting 2 items in top level object", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expecting 2 items in myEnumField object", ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); - assertTrue("expecting 0 items in myEnum object", ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); - assertTrue("expecting 3", Integer.valueOf(3).equals(jsonObject.query("/myEnumField/intVal"))); - assertTrue("expecting val 3", "val 3".equals(jsonObject.query("/myEnumField/value"))); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected 2 myEnumField items", "VAL3".equals((JsonPath.read(doc, "$.myEnumField")))); + assertTrue("expected 0 myEnum items", "VAL1".equals((JsonPath.read(doc, "$.myEnum")))); + + assertTrue("expecting MyEnumField.VAL3", MyEnumField.VAL3.equals(jsonObject.query("/myEnumField"))); + assertTrue("expecting MyEnum.VAL1", MyEnum.VAL1.equals(jsonObject.query("/myEnum"))); } /** @@ -87,6 +93,45 @@ public void jsonObjectFromEnumWithNames() { assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1"))); assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2"))); assertTrue("expected VAL3", myEnumField.VAL3.equals(jsonObject.query("/VAL3"))); + } + + /** + * Verify that enums are handled consistently between JSONArray and JSONObject + */ + @Test + public void verifyEnumConsistency(){ + JSONObject jo = new JSONObject(); + + jo.put("value", MyEnumField.VAL2); + String expected="{\"value\":\"VAL2\"}"; + String actual = jo.toString(); + assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual)); + + jo.accumulate("value", MyEnumField.VAL1); + expected="{\"value\":[\"VAL2\",\"VAL1\"]}"; + actual = jo.toString(); + assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual)); + + jo.remove("value"); + jo.append("value", MyEnumField.VAL1); + expected="{\"value\":[\"VAL1\"]}"; + actual = jo.toString(); + assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual)); + + jo.put("value", EnumSet.of(MyEnumField.VAL2)); + expected="{\"value\":[\"VAL2\"]}"; + actual = jo.toString(); + assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual)); + + JSONArray ja = new JSONArray(); + ja.put(MyEnumField.VAL2); + jo.put("value", ja); + actual = jo.toString(); + assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual)); + + jo.put("value", new MyEnumField[]{MyEnumField.VAL2}); + actual = jo.toString(); + assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual)); } @@ -185,10 +230,8 @@ public void enumToString() { // validate JSON content doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected 2 myEnumField items", ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); - assertTrue("expected 0 myEnum items", ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); - assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/myEnumField/intVal"))); - assertTrue("expected val 3", "val 3".equals(jsonObject.query("/myEnumField/value"))); + assertTrue("expected VAL3", "VAL3".equals((JsonPath.read(doc, "$.myEnumField")))); + assertTrue("expected VAL1", "VAL1".equals((JsonPath.read(doc, "$.myEnum")))); String [] names = JSONObject.getNames(myEnum); jsonObject = new JSONObject(myEnum, names); @@ -233,23 +276,20 @@ public void enumToString() { } /** - * Wrap should handle enums exactly the same way as the JSONObject(Object) - * constructor. + * Wrap should handle enums exactly as a value type like Integer, Boolean, or String. */ @Test public void wrap() { - MyEnum myEnum = MyEnum.VAL2; - JSONObject jsonObject = (JSONObject)JSONObject.wrap(myEnum); - assertTrue("simple enum has no getters", jsonObject.length() == 0); + assertTrue("simple enum has no getters", JSONObject.wrap(MyEnum.VAL2) instanceof MyEnum); MyEnumField myEnumField = MyEnumField.VAL2; - jsonObject = (JSONObject)JSONObject.wrap(myEnumField); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("enum",myEnumField); // validate JSON content Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected val 2", "val 2".equals(jsonObject.query("/value"))); - assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/intVal"))); + assertTrue("expected 1 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/enum"))); MyEnumClass myEnumClass = new MyEnumClass(); myEnumClass.setMyEnum(MyEnum.VAL1); @@ -259,11 +299,11 @@ public void wrap() { // validate JSON content doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected 2 myEnumField items", ((Map)(JsonPath.read(doc, "$.myEnumField"))).size() == 2); - assertTrue("expected 0 myEnum items", ((Map)(JsonPath.read(doc, "$.myEnum"))).size() == 0); - assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/myEnumField/intVal"))); - assertTrue("expected val 3", "val 3".equals(jsonObject.query("/myEnumField/value"))); + assertTrue("expected VAL3", "VAL3".equals((JsonPath.read(doc, "$.myEnumField")))); + assertTrue("expected VAL1", "VAL1".equals((JsonPath.read(doc, "$.myEnum")))); + assertTrue("expecting MyEnumField.VAL3", MyEnumField.VAL3.equals(jsonObject.query("/myEnumField"))); + assertTrue("expecting MyEnum.VAL1", MyEnum.VAL1.equals(jsonObject.query("/myEnum"))); } /** diff --git a/src/test/java/org/json/junit/MyEnumField.java b/src/test/java/org/json/junit/MyEnumField.java index cff565af8..8f2c63311 100644 --- a/src/test/java/org/json/junit/MyEnumField.java +++ b/src/test/java/org/json/junit/MyEnumField.java @@ -20,4 +20,7 @@ public String getValue() { public Integer getIntVal() { return intVal; } + public String toString(){ + return value; + } } From bbd3fd5571344d0330827e3f19ffdb3a17aed37d Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 16 Aug 2016 19:45:26 -0400 Subject: [PATCH 237/315] Adds tests for numbers --- src/test/java/org/json/junit/Fraction.java | 180 ++++++++++++++++++ .../java/org/json/junit/JSONObjectTest.java | 53 +++++- src/test/java/org/json/junit/MyNumber.java | 97 ++++++++++ .../org/json/junit/MyNumberContainer.java | 13 ++ 4 files changed, 341 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/json/junit/Fraction.java create mode 100644 src/test/java/org/json/junit/MyNumber.java create mode 100644 src/test/java/org/json/junit/MyNumberContainer.java diff --git a/src/test/java/org/json/junit/Fraction.java b/src/test/java/org/json/junit/Fraction.java new file mode 100644 index 000000000..d5d9eb659 --- /dev/null +++ b/src/test/java/org/json/junit/Fraction.java @@ -0,0 +1,180 @@ +package org.json.junit; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.RoundingMode; + +/** + * basic fraction class, no frills. + * @author John Aylward + * + */ +public class Fraction extends Number implements Comparable { + /** + * serial id. + */ + private static final long serialVersionUID = 1L; + + /** + * value as a big decimal. + */ + private final BigDecimal bigDecimal; + + /** + * value of the denominator. + */ + private final BigInteger denominator; + /** + * value of the numerator. + */ + private final BigInteger numerator; + + /** + * @param numerator + * numerator + * @param denominator + * denominator + */ + public Fraction(final BigInteger numerator, final BigInteger denominator) { + super(); + if (numerator == null || denominator == null) { + throw new IllegalArgumentException("All values must be non-null"); + } + if (denominator.compareTo(BigInteger.ZERO)==0) { + throw new IllegalArgumentException("Divide by zero"); + } + + final BigInteger n; + final BigInteger d; + // normalize fraction + if (denominator.signum()<0) { + n = numerator.negate(); + d = denominator.negate(); + } else { + n = numerator; + d = denominator; + } + this.numerator = n; + this.denominator = d; + if (n.compareTo(BigInteger.ZERO)==0) { + this.bigDecimal = BigDecimal.ZERO; + } else if (n.compareTo(d)==0) {// i.e. 4/4, 10/10 + this.bigDecimal = BigDecimal.ONE; + } else { + this.bigDecimal = new BigDecimal(this.numerator).divide(new BigDecimal(this.denominator), + RoundingMode.HALF_EVEN); + } + } + + /** + * @param numerator + * numerator + * @param denominator + * denominator + */ + public Fraction(final long numerator, final long denominator) { + this(BigInteger.valueOf(numerator),BigInteger.valueOf(denominator)); + } + + /** + * @return the decimal + */ + public BigDecimal bigDecimalValue() { + return this.bigDecimal; + } + + @Override + public int compareTo(final Fraction o) { + // .equals call this, so no .equals compare allowed + + // if they are the same reference, just return equals + if (this == o) { + return 0; + } + + // if my denominators are already equal, just compare the numerators + if (this.denominator.compareTo(o.denominator)==0) { + return this.numerator.compareTo(o.numerator); + } + + // get numerators of common denominators + // a x ay xb + // --- --- = ---- ---- + // b y by yb + final BigInteger thisN = this.numerator.multiply(o.denominator); + final BigInteger otherN = o.numerator.multiply(this.denominator); + + return thisN.compareTo(otherN); + } + + @Override + public double doubleValue() { + return this.bigDecimal.doubleValue(); + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (this.getClass() != obj.getClass()) { + return false; + } + final Fraction other = (Fraction) obj; + return this.compareTo(other) == 0; + } + + @Override + public float floatValue() { + return this.bigDecimal.floatValue(); + } + + /** + * @return the denominator + */ + public BigInteger getDenominator() { + return this.denominator; + } + + /** + * @return the numerator + */ + public BigInteger getNumerator() { + return this.numerator; + } + + /** + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (this.bigDecimal == null ? 0 : this.bigDecimal.hashCode()); + return result; + } + + @Override + public int intValue() { + return this.bigDecimal.intValue(); + } + + @Override + public long longValue() { + return this.bigDecimal.longValue(); + } + + /** + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return this.numerator + "/" + this.denominator; + } +} diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index e353ae550..8c91b5379 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1,9 +1,9 @@ package org.json.junit; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -20,6 +20,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import org.json.CDL; import org.json.JSONArray; @@ -49,7 +50,7 @@ public void jsonObjectByNullBean() { MyBean myBean = null; new JSONObject(myBean); } - + /** * A JSONObject can be created with no content */ @@ -165,6 +166,54 @@ public void verifyConstructor() { expected.similar(jaObjObj)); } + /** + * Tests Number serialization. + */ + @Test + public void verifyNumberOutput(){ + JSONObject jsonObject = new JSONObject(new MyNumberContainer()); + String actual = jsonObject.toString(); + + // before wrapping of Number is allowed the number was converted as a bean + String expected = "{\"myNumber\":{\"number\":42}}"; + //String expected = "{\"myNumber\":42}"; + assertEquals("Not Equal", expected , actual); + + // put handles objects differently than the constructor. + jsonObject = new JSONObject(); + jsonObject.put("myNumber", new MyNumberContainer()); + actual = jsonObject.toString(); + // the output is the toString of the container. i.e org.json.junit.MyNumberContainer@4f063c0a + // the hex is the memory address which will change each run. + expected = "{\"myNumber\":\""+jsonObject.get("myNumber")+"\"}"; + assertEquals("Not Equal", expected , actual); + + jsonObject = new JSONObject(Collections.singletonMap("myNumber", new AtomicInteger(42))); + actual = jsonObject.toString(); + // before wrapping of Number is allowed the number was converted to a string + expected = "{\"myNumber\":\"42\"}"; + assertEquals("Not Equal", expected , actual); + + // put handles objects differently than the constructor. + jsonObject = new JSONObject(); + jsonObject.put("myNumber", new AtomicInteger(42)); + actual = jsonObject.toString(); + expected = "{\"myNumber\":42}"; + assertEquals("Not Equal", expected , actual); + + // verify Fraction output + jsonObject = new JSONObject(Collections.singletonMap("myNumber", new Fraction(4,2))); + actual = jsonObject.toString(); + expected = "{\"myNumber\":{\"denominator\":2,\"numerator\":4}}"; + assertEquals("Not Equal", expected , actual); + + jsonObject = new JSONObject(); + jsonObject.put("myNumber", new Fraction(4,2)); + actual = jsonObject.toString(); + expected = "{\"myNumber\":4/2}"; // this is NOT valid JSON!!!!!!!!!!! BUG! + assertEquals("Not Equal", expected , actual); + } + /** * Verifies that the put Collection has backwards compatability with RAW types pre-java5. */ diff --git a/src/test/java/org/json/junit/MyNumber.java b/src/test/java/org/json/junit/MyNumber.java new file mode 100644 index 000000000..243a9679b --- /dev/null +++ b/src/test/java/org/json/junit/MyNumber.java @@ -0,0 +1,97 @@ +package org.json.junit; + +import java.math.BigDecimal; + +/** + * Number override for testing. Number overrides should always override + * toString, hashCode, and Equals. + * + * @see The + * Numbers Classes + * @see Formatting + * Numeric Print Output + * + * @author John Aylward + */ +public class MyNumber extends Number { + private Number number = BigDecimal.valueOf(42); + /** + */ + private static final long serialVersionUID = 1L; + + /** + * @return number! + */ + public Number getNumber() { + return this.number; + } + + @Override + public int intValue() { + return getNumber().intValue(); + } + + @Override + public long longValue() { + return getNumber().longValue(); + } + + @Override + public float floatValue() { + return getNumber().floatValue(); + } + + @Override + public double doubleValue() { + return getNumber().doubleValue(); + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + * + * Number overrides should in general always override the toString method. + */ + @Override + public String toString() { + return getNumber().toString(); + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((this.number == null) ? 0 : this.number.hashCode()); + return result; + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof MyNumber)) { + return false; + } + MyNumber other = (MyNumber) obj; + if (this.number == null) { + if (other.number != null) { + return false; + } + } else if (!this.number.equals(other.number)) { + return false; + } + return true; + } + +} diff --git a/src/test/java/org/json/junit/MyNumberContainer.java b/src/test/java/org/json/junit/MyNumberContainer.java new file mode 100644 index 000000000..524f318d1 --- /dev/null +++ b/src/test/java/org/json/junit/MyNumberContainer.java @@ -0,0 +1,13 @@ +package org.json.junit; + +/** + * Class that holds our MyNumber override as a property. + * @author John Aylward + */ +public class MyNumberContainer { + private MyNumber myNumber = new MyNumber(); + /** + * @return a MyNumber. + */ + public Number getMyNumber() {return this.myNumber;} +} From cbd041870432b74c3deb9b4af90c02d6b6358105 Mon Sep 17 00:00:00 2001 From: johnjaylward Date: Tue, 16 Aug 2016 21:33:54 -0400 Subject: [PATCH 238/315] Update JSONObjectTest.java fixes test to be applicable --- src/test/java/org/json/junit/JSONObjectTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 8c91b5379..39e6ae527 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -181,11 +181,10 @@ public void verifyNumberOutput(){ // put handles objects differently than the constructor. jsonObject = new JSONObject(); - jsonObject.put("myNumber", new MyNumberContainer()); + jsonObject.put("myNumber", new MyNumber()); actual = jsonObject.toString(); - // the output is the toString of the container. i.e org.json.junit.MyNumberContainer@4f063c0a - // the hex is the memory address which will change each run. - expected = "{\"myNumber\":\""+jsonObject.get("myNumber")+"\"}"; + // the output is the toString of the number as a string. + expected = "{\"myNumber\":\"42\"}"; assertEquals("Not Equal", expected , actual); jsonObject = new JSONObject(Collections.singletonMap("myNumber", new AtomicInteger(42))); From a66b97f60bbae42133f472d77840e55f76ef3722 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 17 Aug 2016 11:31:12 -0400 Subject: [PATCH 239/315] fix test --- src/test/java/org/json/junit/JSONObjectTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 39e6ae527..55f7446b8 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -183,8 +183,8 @@ public void verifyNumberOutput(){ jsonObject = new JSONObject(); jsonObject.put("myNumber", new MyNumber()); actual = jsonObject.toString(); - // the output is the toString of the number as a string. - expected = "{\"myNumber\":\"42\"}"; + // the output is the toString of the number as a number. + expected = "{\"myNumber\":42}"; assertEquals("Not Equal", expected , actual); jsonObject = new JSONObject(Collections.singletonMap("myNumber", new AtomicInteger(42))); From 0b1dbe9369f22737cd91bec7f8905f909f71f1a3 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 17 Aug 2016 12:13:54 -0400 Subject: [PATCH 240/315] fixes test to not depend on key order --- src/test/java/org/json/junit/JSONObjectTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 55f7446b8..9a1d7b7d1 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -202,10 +202,9 @@ public void verifyNumberOutput(){ // verify Fraction output jsonObject = new JSONObject(Collections.singletonMap("myNumber", new Fraction(4,2))); - actual = jsonObject.toString(); - expected = "{\"myNumber\":{\"denominator\":2,\"numerator\":4}}"; - assertEquals("Not Equal", expected , actual); - + assertEquals("Numerator", BigInteger.valueOf(4) , jsonObject.query("/myNumber/numerator")); + assertEquals("Denominator", BigInteger.valueOf(2) , jsonObject.query("/myNumber/denominator")); + jsonObject = new JSONObject(); jsonObject.put("myNumber", new Fraction(4,2)); actual = jsonObject.toString(); From 58aebaa14fa58c920576db0560dce1c7a880f5dc Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 15 Sep 2016 21:31:28 -0500 Subject: [PATCH 241/315] fixed merge issues --- .../java/org/json/junit/JSONObjectTest.java | 81 ++++++++++++++----- 1 file changed, 61 insertions(+), 20 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 9a1d7b7d1..1b50e7a0b 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -171,44 +171,85 @@ public void verifyConstructor() { */ @Test public void verifyNumberOutput(){ + /** + * MyNumberContainer is a POJO, so call JSONObject(bean), + * which builds a map of getter names/values + * The only getter is getMyNumber (key=myNumber), + * whose return value is MyNumber. MyNumber extends Number, + * but is not recognized as such by wrap() per current + * implementation, so wrap() returns the default new JSONObject(bean). + * The only getter is getNumber (key=number), whose return value is + * BigDecimal(42). + */ JSONObject jsonObject = new JSONObject(new MyNumberContainer()); String actual = jsonObject.toString(); - - // before wrapping of Number is allowed the number was converted as a bean String expected = "{\"myNumber\":{\"number\":42}}"; - //String expected = "{\"myNumber\":42}"; assertEquals("Not Equal", expected , actual); - // put handles objects differently than the constructor. + /** + * JSONObject.put() handles objects differently than the + * bean constructor. Where the bean ctor wraps objects before + * placing them in the map, put() inserts the object without wrapping. + * In this case, a MyNumber instance is the value. + * The MyNumber.toString() method is responsible for + * returning a reasonable value: the string '42'. + */ jsonObject = new JSONObject(); jsonObject.put("myNumber", new MyNumber()); actual = jsonObject.toString(); - // the output is the toString of the number as a number. expected = "{\"myNumber\":42}"; assertEquals("Not Equal", expected , actual); - + + /** + * Calls the JSONObject(Map) ctor, which calls wrap() for values. + * AtomicInteger is a Number, but is not recognized by wrap(), per + * current implementation. However, the type is + * 'java.util.concurrent.atomic', so due to the 'java' prefix, + * wrap() inserts the value as a string. That is why 42 comes back + * wrapped in quotes. + */ jsonObject = new JSONObject(Collections.singletonMap("myNumber", new AtomicInteger(42))); actual = jsonObject.toString(); - // before wrapping of Number is allowed the number was converted to a string expected = "{\"myNumber\":\"42\"}"; assertEquals("Not Equal", expected , actual); - - // put handles objects differently than the constructor. + + /** + * JSONObject.put() inserts the AtomicInteger directly into the + * map not calling wrap(). In toString()->write()->writeValue(), + * AtomicInteger is recognized as a Number, and converted via + * numberToString() into the unquoted string '42'. + */ jsonObject = new JSONObject(); jsonObject.put("myNumber", new AtomicInteger(42)); actual = jsonObject.toString(); expected = "{\"myNumber\":42}"; assertEquals("Not Equal", expected , actual); - - // verify Fraction output + + /** + * Calls the JSONObject(Map) ctor, which calls wrap() for values. + * Fraction is a Number, but is not recognized by wrap(), per + * current implementation. As a POJO, Franction is handled as a + * bean and inserted into a contained JSONObject. It has 2 getters, + * for numerator and denominator. + */ jsonObject = new JSONObject(Collections.singletonMap("myNumber", new Fraction(4,2))); + assertEquals(1, jsonObject.length()); + assertEquals(2, ((JSONObject)(jsonObject.get("myNumber"))).length()); assertEquals("Numerator", BigInteger.valueOf(4) , jsonObject.query("/myNumber/numerator")); assertEquals("Denominator", BigInteger.valueOf(2) , jsonObject.query("/myNumber/denominator")); + /** + * JSONObject.put() inserts the Fraction directly into the + * map not calling wrap(). In toString()->write()->writeValue(), + * Fraction is recognized as a Number, and converted via + * numberToString() into the unquoted string '4/2'. But the + * BigDecimal sanity check fails, so writeValue() defaults + * to returning a safe JSON quoted string. Pretty slick! + */ jsonObject = new JSONObject(); jsonObject.put("myNumber", new Fraction(4,2)); actual = jsonObject.toString(); - expected = "{\"myNumber\":4/2}"; // this is NOT valid JSON!!!!!!!!!!! BUG! + expected = "{\"myNumber\":\"4/2\"}"; // valid JSON, bug fixed assertEquals("Not Equal", expected , actual); } @@ -2224,44 +2265,44 @@ public void toMap() { "}"; JSONObject jsonObject = new JSONObject(jsonObjectStr); - Map map = jsonObject.toMap(); + Map map = jsonObject.toMap(); assertTrue("Map should not be null", map != null); assertTrue("Map should have 3 elements", map.size() == 3); - List key1List = (List)map.get("key1"); + List key1List = (List)map.get("key1"); assertTrue("key1 should not be null", key1List != null); assertTrue("key1 list should have 3 elements", key1List.size() == 3); assertTrue("key1 value 1 should be 1", key1List.get(0).equals(Integer.valueOf(1))); assertTrue("key1 value 2 should be 2", key1List.get(1).equals(Integer.valueOf(2))); - Map key1Value3Map = (Map)key1List.get(2); + Map key1Value3Map = (Map)key1List.get(2); assertTrue("Map should not be null", key1Value3Map != null); assertTrue("Map should have 1 element", key1Value3Map.size() == 1); assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE)); - Map key2Map = (Map)map.get("key2"); + Map key2Map = (Map)map.get("key2"); assertTrue("key2 should not be null", key2Map != null); assertTrue("key2 map should have 3 elements", key2Map.size() == 3); assertTrue("key2 map key 1 should be val1", key2Map.get("key1").equals("val1")); assertTrue("key2 map key 3 should be 42", key2Map.get("key3").equals(Integer.valueOf(42))); - Map key2Val2Map = (Map)key2Map.get("key2"); + Map key2Val2Map = (Map)key2Map.get("key2"); assertTrue("key2 map key 2 should not be null", key2Val2Map != null); assertTrue("key2 map key 2 should have an entry", key2Val2Map.containsKey("key2")); assertTrue("key2 map key 2 value should be null", key2Val2Map.get("key2") == null); - List key3List = (List)map.get("key3"); + List key3List = (List)map.get("key3"); assertTrue("key3 should not be null", key3List != null); assertTrue("key3 list should have 3 elements", key3List.size() == 2); - List key3Val1List = (List)key3List.get(0); + List key3Val1List = (List)key3List.get(0); assertTrue("key3 list val 1 should not be null", key3Val1List != null); assertTrue("key3 list val 1 should have 2 elements", key3Val1List.size() == 2); assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1")); assertTrue("key3 list val 1 list element 2 should be 2.1", key3Val1List.get(1).equals(Double.valueOf("2.1"))); - List key3Val2List = (List)key3List.get(1); + List key3Val2List = (List)key3List.get(1); assertTrue("key3 list val 2 should not be null", key3Val2List != null); assertTrue("key3 list val 2 should have 1 element", key3Val2List.size() == 1); assertTrue("key3 list val 2 list element 1 should be null", key3Val2List.get(0) == null); From c8563ff93daee506201b075faf203f726319afd7 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 22 Sep 2016 12:38:30 -0400 Subject: [PATCH 242/315] new test case for XML changes --- src/test/java/org/json/junit/XMLTest.java | 31 ++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 2f3fea752..11c05d471 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -267,6 +267,35 @@ public void shouldHandleSimpleXML() { compareFileToJSONObject(xmlStr, expectedStr); } + /** + * Tests to verify that supported escapes in XML are converted to actual values. + */ + @Test + public void testXmlEscapeToJson(){ + String xmlStr = + "\n"+ + ""+ + "\""+ + "A €33"+ + "A €22€"+ + "some text ©"+ + "" " & ' < >"+ + ""; + String expectedStr = + "{\"root\":{" + + "\"rawQuote\":\"\\\"\"," + + "\"euro\":\"A €33\"," + + "\"euroX\":\"A €22€\"," + + "\"unknown\":\"some text ©\"," + + "\"known\":\"\\\" \\\" & ' < >\"" + + "}}"; + + compareStringToJSONObject(xmlStr, expectedStr); + compareReaderToJSONObject(xmlStr, expectedStr); + compareFileToJSONObject(xmlStr, expectedStr); + + } + /** * Valid XML with comments to JSONObject */ @@ -675,8 +704,8 @@ public void contentOperations() { * @param expectedStr the expected JSON string */ private void compareStringToJSONObject(String xmlStr, String expectedStr) { - JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject jsonObject = XML.toJSONObject(xmlStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } From 5027a283c1b9c1bc81663441f0a0b23de48115cc Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 22 Sep 2016 13:09:32 -0400 Subject: [PATCH 243/315] Adds test for escaping from a JSONObject to XML --- src/test/java/org/json/junit/XMLTest.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 11c05d471..800dc93ce 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1,6 +1,7 @@ package org.json.junit; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; @@ -293,7 +294,19 @@ public void testXmlEscapeToJson(){ compareStringToJSONObject(xmlStr, expectedStr); compareReaderToJSONObject(xmlStr, expectedStr); compareFileToJSONObject(xmlStr, expectedStr); - + } + + /** + * Tests that certain unicode characters are escaped. + */ + @Test + public void testJsonToXmlEscape(){ + JSONObject json = new JSONObject("{ \"amount\": \"10,00 €\", \"description\": \"Ação Válida\" }"); + String xml = XML.toString(json); + assertFalse("Escaping € failed. Found in XML output.", xml.contains("€")); + assertTrue("Escaping ç failed. Not found in XML output.", xml.contains("ç")); + assertTrue("Escaping ã failed. Not found in XML output.", xml.contains("ã")); + assertTrue("Escaping á failed. Not found in XML output.", xml.contains("á")); } /** From 2b87f334d0343774a77b1779011eaba250a2a0c9 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 22 Sep 2016 14:13:48 -0400 Subject: [PATCH 244/315] Update test cases to support ISO Control encoding changes. --- src/test/java/org/json/junit/XMLTest.java | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 800dc93ce..91ee42051 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -297,16 +297,30 @@ public void testXmlEscapeToJson(){ } /** - * Tests that certain unicode characters are escaped. + * Tests that control characters are escaped. */ @Test public void testJsonToXmlEscape(){ - JSONObject json = new JSONObject("{ \"amount\": \"10,00 €\", \"description\": \"Ação Válida\" }"); + final String jsonSrc = "{\"amount\":\"10,00 €\"," + + "\"description\":\"Ação Válida\u0085\"," + + "\"xmlEntities\":\"\\\" ' & < >\"" + + "}"; + JSONObject json = new JSONObject(jsonSrc); String xml = XML.toString(json); - assertFalse("Escaping € failed. Found in XML output.", xml.contains("€")); + //test control character not existing + assertFalse("Escaping \u0085 failed. Found in XML output.", xml.contains("\u0085")); + assertTrue("Escaping \u0085 failed. Entity not found in XML output.", xml.contains("…")); + // test normal unicode existing + assertTrue("Escaping € failed. Not found in XML output.", xml.contains("€")); assertTrue("Escaping ç failed. Not found in XML output.", xml.contains("ç")); assertTrue("Escaping ã failed. Not found in XML output.", xml.contains("ã")); assertTrue("Escaping á failed. Not found in XML output.", xml.contains("á")); + // test XML Entities converted + assertTrue("Escaping \" failed. Not found in XML output.", xml.contains(""")); + assertTrue("Escaping ' failed. Not found in XML output.", xml.contains("'")); + assertTrue("Escaping & failed. Not found in XML output.", xml.contains("&")); + assertTrue("Escaping < failed. Not found in XML output.", xml.contains("<")); + assertTrue("Escaping > failed. Not found in XML output.", xml.contains(">")); } /** From f6a00e94c77e3b1ca6c74308696600502751efa4 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 22 Sep 2016 16:12:00 -0400 Subject: [PATCH 245/315] adds test for unicode that has surrogate pairs --- src/test/java/org/json/junit/XMLTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 91ee42051..264fa44a5 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -281,6 +281,7 @@ public void testXmlEscapeToJson(){ "A €22€"+ "some text ©"+ "" " & ' < >"+ + "𝄢 𐅥" + ""; String expectedStr = "{\"root\":{" + @@ -288,7 +289,8 @@ public void testXmlEscapeToJson(){ "\"euro\":\"A €33\"," + "\"euroX\":\"A €22€\"," + "\"unknown\":\"some text ©\"," + - "\"known\":\"\\\" \\\" & ' < >\"" + + "\"known\":\"\\\" \\\" & ' < >\"," + + "\"high\":\"𝄢 𐅥\""+ "}}"; compareStringToJSONObject(xmlStr, expectedStr); From 97e3d6c7cee211eac60deb7a3cc3d0994e167135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Wed, 5 Oct 2016 14:59:36 +0200 Subject: [PATCH 246/315] testcase for stleary/JSON-java#292 and adding .idea to .gitiignore --- .gitignore | 1 + src/test/java/org/json/junit/JSONPointerTest.java | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/.gitignore b/.gitignore index 7afd4207e..b7025e6b8 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ build /gradlew.bat .gitmodules src/main/ +.idea diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java index 95fa73b9c..75f4ea8b6 100644 --- a/src/test/java/org/json/junit/JSONPointerTest.java +++ b/src/test/java/org/json/junit/JSONPointerTest.java @@ -78,6 +78,11 @@ public void uriFragmentNotation() { assertSame(document.get("foo"), query("#/foo")); } + @Test + public void uriFragmentNotationRoot() { + assertSame(document, query("#")); + } + @Test public void uriFragmentPercentHandling() { assertSame(document.get("c%d"), query("#/c%25d")); From 928179a1f3d2bd0817803277298af3bf4ee706c9 Mon Sep 17 00:00:00 2001 From: stleary Date: Tue, 14 Feb 2017 08:30:22 -0600 Subject: [PATCH 247/315] locale tests --- build.gradle | 6 +++ .../org/json/junit/JSONObjectLocaleTest.java | 48 +++++++++++++++++++ .../java/org/json/junit/JunitTestSuite.java | 1 + .../java/org/json/junit/MyLocaleBean.java | 12 +++++ 4 files changed, 67 insertions(+) create mode 100755 src/test/java/org/json/junit/JSONObjectLocaleTest.java create mode 100755 src/test/java/org/json/junit/MyLocaleBean.java diff --git a/build.gradle b/build.gradle index d2969d48a..58259f993 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,12 @@ apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'jacoco' +tasks.withType(JavaCompile) { + // this subproject requires -parameters option + options.compilerArgs << '-parameters' + options.encoding = 'UTF-8' +} + sourceSets { // Uncomment main if you have merged JSON-Java and JSON-Java-unit-test code main diff --git a/src/test/java/org/json/junit/JSONObjectLocaleTest.java b/src/test/java/org/json/junit/JSONObjectLocaleTest.java new file mode 100755 index 000000000..a6cb99602 --- /dev/null +++ b/src/test/java/org/json/junit/JSONObjectLocaleTest.java @@ -0,0 +1,48 @@ +package org.json.junit; + +import static org.junit.Assert.*; + +import java.util.*; + +import org.json.*; +import org.junit.*; + +/** + * Note: This file is saved as UTF-8. Do not save as ASCII or the tests will + * fail. + * + */ +public class JSONObjectLocaleTest { + /** + * JSONObject built from a bean with locale-specific keys - that is, the key + * fields are not LANG_ENGLISH. + */ + @Test + public void jsonObjectByLocaleBean() { + + MyLocaleBean myLocaleBean = new MyLocaleBean(); + + Locale.setDefault(new Locale("en")); + JSONObject jsonen = new JSONObject(myLocaleBean); + System.out.println("jsonen " + jsonen); + + Locale.setDefault(new Locale("tr")); + JSONObject jsontr = new JSONObject(myLocaleBean); + System.out.println("jsontr " + jsontr); + /** + * In this test we exercise code that handles keys of 1-char and + * multi-char length that include text from a non-English locale. + * Turkish in this case. The JSONObject code should correctly retain the + * non-EN_LANG chars in the key. + */ + assertTrue("expected beanId", + "Tlocaleüx".equals(jsonObject.getString(""))); + assertTrue("expected Tlocalü", + "Tlocaleü".equals(jsonObject.getString("ü"))); + assertTrue("expected Tlocaleüx", + "Tlocaleüx".equals((String)(jsonObject.query("/üx")))); + assertTrue("expected Tlocalü", + "Tlocaleü".equals((String)(jsonObject.query("/ü")))); + } + +} diff --git a/src/test/java/org/json/junit/JunitTestSuite.java b/src/test/java/org/json/junit/JunitTestSuite.java index 3a7223e19..36bec607a 100644 --- a/src/test/java/org/json/junit/JunitTestSuite.java +++ b/src/test/java/org/json/junit/JunitTestSuite.java @@ -13,6 +13,7 @@ HTTPTest.class, JSONStringerTest.class, JSONObjectTest.class, + JSONObjectLocaleTest.class, JSONArrayTest.class, EnumTest.class, JSONPointerTest.class, diff --git a/src/test/java/org/json/junit/MyLocaleBean.java b/src/test/java/org/json/junit/MyLocaleBean.java new file mode 100755 index 000000000..0d68c39c6 --- /dev/null +++ b/src/test/java/org/json/junit/MyLocaleBean.java @@ -0,0 +1,12 @@ +package org.json.junit; + +public class MyLocaleBean { + private final String id = "beanId"; + private final String i = "beanI"; + public String getId() { + return id; + } + public String getI() { + return i; + } +} From f41e1d012aa5dbc012a22e6890a9755d03d0fae8 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 16 Feb 2017 20:49:37 -0600 Subject: [PATCH 248/315] tests for locale-independent keys --- .../org/json/junit/JSONObjectLocaleTest.java | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectLocaleTest.java b/src/test/java/org/json/junit/JSONObjectLocaleTest.java index a6cb99602..9c80ab6a3 100755 --- a/src/test/java/org/json/junit/JSONObjectLocaleTest.java +++ b/src/test/java/org/json/junit/JSONObjectLocaleTest.java @@ -14,35 +14,42 @@ */ public class JSONObjectLocaleTest { /** - * JSONObject built from a bean with locale-specific keys - that is, the key - * fields are not LANG_ENGLISH. + * JSONObject built from a bean with locale-specific keys. + * In the Turkish alphabet, there are 2 versions of the letter "i". + * 'eh' I ı (dotless i) + * 'ee' İ i (dotted i) + * A problem can occur when parsing the public get methods for a bean. + * If the method starts with getI... then the key name will be lowercased + * to 'i' in English, and 'ı' in Turkish. + * We want the keys to be consistent regardless of locale, so JSON-Java + * lowercase operations are made to be locale-neutral by specifying + * Locale.ROOT. This causes 'I' to be universally lowercased to 'i' + * regardless of the locale currently in effect. */ @Test public void jsonObjectByLocaleBean() { MyLocaleBean myLocaleBean = new MyLocaleBean(); + /** + * This is just the control case which happens when the locale.ROOT + * lowercasing behavior is the same as the current locale. + */ Locale.setDefault(new Locale("en")); JSONObject jsonen = new JSONObject(myLocaleBean); - System.out.println("jsonen " + jsonen); + assertEquals("expected size 2, found: " +jsonen.length(), 2, jsonen.length()); + assertEquals("expected jsonen[i] == beanI", "beanI", jsonen.getString("i")); + assertEquals("expected jsonen[id] == beanId", "beanId", jsonen.getString("id")); - Locale.setDefault(new Locale("tr")); - JSONObject jsontr = new JSONObject(myLocaleBean); - System.out.println("jsontr " + jsontr); /** - * In this test we exercise code that handles keys of 1-char and - * multi-char length that include text from a non-English locale. - * Turkish in this case. The JSONObject code should correctly retain the - * non-EN_LANG chars in the key. + * Without the JSON-Java change, these keys would be stored internally as + * starting with the letter, 'ı' (dotless i), since the lowercasing of + * the getI and getId keys would be specific to the Turkish locale. */ - assertTrue("expected beanId", - "Tlocaleüx".equals(jsonObject.getString(""))); - assertTrue("expected Tlocalü", - "Tlocaleü".equals(jsonObject.getString("ü"))); - assertTrue("expected Tlocaleüx", - "Tlocaleüx".equals((String)(jsonObject.query("/üx")))); - assertTrue("expected Tlocalü", - "Tlocaleü".equals((String)(jsonObject.query("/ü")))); + Locale.setDefault(new Locale("tr")); + JSONObject jsontr = new JSONObject(myLocaleBean); + assertEquals("expected size 2, found: " +jsontr.length(), 2, jsontr.length()); + assertEquals("expected jsontr[i] == beanI", "beanI", jsontr.getString("i")); + assertEquals("expected jsontr[id] == beanId", "beanId", jsontr.getString("id")); } - } From e41972a57426c1e65646a2ab0634693655e33495 Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 26 Feb 2017 11:09:41 -0600 Subject: [PATCH 249/315] add a test for unquoted values --- .../java/org/json/junit/JSONObjectTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 1b50e7a0b..1dec2fd67 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -51,6 +51,23 @@ public void jsonObjectByNullBean() { new JSONObject(myBean); } + /** + * The JSON parser is permissive of unambiguous unquoted keys and values. + * Such JSON text should be allowed, even if it does not strictly conform + * to the spec. However, after being parsed, toString() should emit strictly + * conforming JSON text. + */ + @Test + public void unquotedText() { + String str = "{key1:value1, key2:42}"; + JSONObject jsonObject = new JSONObject(str); + String textStr = jsonObject.toString(); + assertTrue("expected key1", textStr.contains("\"key1\"")); + assertTrue("expected value1", textStr.contains("\"value1\"")); + assertTrue("expected key2", textStr.contains("\"key2\"")); + assertTrue("expected 42", textStr.contains("42")); + } + /** * A JSONObject can be created with no content */ From d1a5f15f0c0e1bb55322816d3e90cb084ba5dead Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 26 Mar 2017 15:03:09 -0500 Subject: [PATCH 250/315] unit tests for query-by-JSONPointer --- .../java/org/json/junit/JSONPointerTest.java | 111 +++++++++++++++++- 1 file changed, 109 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java index 75f4ea8b6..0904b9e38 100644 --- a/src/test/java/org/json/junit/JSONPointerTest.java +++ b/src/test/java/org/json/junit/JSONPointerTest.java @@ -157,7 +157,7 @@ public void tokenListIsCopiedInConstructor() { } /** - * Coverage for JSONObject queryFrom() + * Coverage for JSONObject query(String) */ @Test public void queryFromJSONObject() { @@ -187,7 +187,61 @@ public void queryFromJSONObject() { } /** - * Coverage for JSONArray queryFrom() + * Coverage for JSONObject query(JSONPointer) + */ + @Test + public void queryFromJSONObjectUsingPointer() { + String str = "{"+ + "\"stringKey\":\"hello world!\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\": {"+ + "\"a\":\"aVal\","+ + "\"b\":\"bVal\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + Object obj = jsonObject.query(new JSONPointer("/stringKey")); + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); + obj = jsonObject.query(new JSONPointer("/arrayKey/1")); + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); + obj = jsonObject.query(new JSONPointer("/objectKey/b")); + assertTrue("Expected bVal", "bVal".equals(obj)); + try { + obj = jsonObject.query(new JSONPointer("/a/b/c")); + assertTrue("Expected JSONPointerException", false); + } catch (JSONPointerException e) { + assertTrue("Expected bad key/value exception", + "value [null] is not an array or object therefore its key b cannot be resolved". + equals(e.getMessage())); + } + } + + /** + * Coverage for JSONObject optQuery(JSONPointer) + */ + @Test + public void optQueryFromJSONObjectUsingPointer() { + String str = "{"+ + "\"stringKey\":\"hello world!\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\": {"+ + "\"a\":\"aVal\","+ + "\"b\":\"bVal\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + Object obj = jsonObject.optQuery(new JSONPointer("/stringKey")); + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); + obj = jsonObject.optQuery(new JSONPointer("/arrayKey/1")); + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); + obj = jsonObject.optQuery(new JSONPointer("/objectKey/b")); + assertTrue("Expected bVal", "bVal".equals(obj)); + obj = jsonObject.optQuery(new JSONPointer("/a/b/c")); + assertTrue("Expected null", obj == null); + } + + /** + * Coverage for JSONArray query(String) */ @Test public void queryFromJSONArray() { @@ -214,4 +268,57 @@ public void queryFromJSONArray() { "a is not an array index".equals(e.getMessage())); } } + + /** + * Coverage for JSONArray query(JSONPointer) + */ + @Test + public void queryFromJSONArrayUsingPointer() { + String str = "["+ + "\"hello world!\","+ + "[0,1,2],"+ + "{"+ + "\"a\":\"aVal\","+ + "\"b\":\"bVal\""+ + "}"+ + "]"; + JSONArray jsonArray = new JSONArray(str); + Object obj = jsonArray.query(new JSONPointer("/0")); + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); + obj = jsonArray.query(new JSONPointer("/1/1")); + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); + obj = jsonArray.query(new JSONPointer("/2/b")); + assertTrue("Expected bVal", "bVal".equals(obj)); + try { + obj = jsonArray.query(new JSONPointer("/a/b/c")); + assertTrue("Expected JSONPointerException", false); + } catch (JSONPointerException e) { + assertTrue("Expected bad index exception", + "a is not an array index".equals(e.getMessage())); + } + } + + /** + * Coverage for JSONArray optQuery(JSONPointer) + */ + @Test + public void optQueryFromJSONArrayUsingPointer() { + String str = "["+ + "\"hello world!\","+ + "[0,1,2],"+ + "{"+ + "\"a\":\"aVal\","+ + "\"b\":\"bVal\""+ + "}"+ + "]"; + JSONArray jsonArray = new JSONArray(str); + Object obj = jsonArray.optQuery(new JSONPointer("/0")); + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); + obj = jsonArray.optQuery(new JSONPointer("/1/1")); + assertTrue("Expected 1", Integer.valueOf(1).equals(obj)); + obj = jsonArray.optQuery(new JSONPointer("/2/b")); + assertTrue("Expected bVal", "bVal".equals(obj)); + obj = jsonArray.optQuery(new JSONPointer("/a/b/c")); + assertTrue("Expected null", obj == null); + } } From 9df5d34bbee13a5d228e0feedf22d8b1039d01ed Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 27 Apr 2017 12:39:42 -0400 Subject: [PATCH 251/315] * Update link in the README to the main JSON-Java repo * Cleans up some warnings * Adds new test for bug https://github.com/stleary/JSON-java/issues/332 * Adds some resource handling for string writers using pre-java1.7 support. I know StringWriters don't need a close method called, but the tests should still handle their resources properly. --- README.md | 2 +- src/test/java/org/json/junit/EnumTest.java | 3 +- .../java/org/json/junit/JSONArrayTest.java | 100 +- src/test/java/org/json/junit/JSONMLTest.java | 75 +- .../java/org/json/junit/JSONObjectTest.java | 4707 +++++++++-------- .../java/org/json/junit/JSONStringTest.java | 681 +-- src/test/java/org/json/junit/MyEnumField.java | 8 +- .../java/org/json/junit/MyPublicClass.java | 1 + .../org/json/junit/StringsResourceBundle.java | 1 + src/test/java/org/json/junit/XMLTest.java | 3 - 10 files changed, 2878 insertions(+), 2703 deletions(-) diff --git a/README.md b/README.md index faf400a0b..0772d3bdf 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Unit tests to validate the JSON-Java GitHub project code
-https://github.com/douglascrockford/JSON-java
+https://github.com/stleary/JSON-java
Gradle and Eclipse is the recommended build tool and IDE.
Run individual tests or JunitTestSuite using EclEmma Coverage, or execute the **TestRunner** application directly.
diff --git a/src/test/java/org/json/junit/EnumTest.java b/src/test/java/org/json/junit/EnumTest.java index ab4a1e55a..6b97107f5 100644 --- a/src/test/java/org/json/junit/EnumTest.java +++ b/src/test/java/org/json/junit/EnumTest.java @@ -5,7 +5,6 @@ import java.util.EnumSet; import java.util.List; import java.util.Map; -import java.util.Set; import org.json.JSONArray; import org.json.JSONObject; @@ -92,7 +91,7 @@ public void jsonObjectFromEnumWithNames() { assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1"))); assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2"))); - assertTrue("expected VAL3", myEnumField.VAL3.equals(jsonObject.query("/VAL3"))); + assertTrue("expected VAL3", MyEnumField.VAL3.equals(jsonObject.query("/VAL3"))); } /** diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 244a693e9..80b78a59b 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -1,11 +1,11 @@ package org.json.junit; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertEquals; +import java.io.IOException; import java.io.StringWriter; -import java.io.Writer; import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; @@ -61,7 +61,7 @@ public class JSONArrayTest { @Test(expected=NullPointerException.class) public void nullException() { String str = null; - new JSONArray(str); + assertNull("Should throw an exception", new JSONArray(str)); } /** @@ -72,8 +72,7 @@ public void nullException() { public void emptStr() { String str = ""; try { - new JSONArray(str); - assertTrue("Should throw an exception", false); + assertNull("Should throw an exception", new JSONArray(str)); } catch (JSONException e) { assertTrue("Expected an exception message", "A JSONArray text must start with '[' at 1 [character 2 line 1]". @@ -90,8 +89,7 @@ public void emptStr() { public void badObject() { String str = "abc"; try { - new JSONArray((Object)str); - assertTrue("Should throw an exception", false); + assertNull("Should throw an exception", new JSONArray((Object)str)); } catch (JSONException e) { assertTrue("Expected an exception message", "JSONArray initial value should be a string or collection or array.". @@ -100,7 +98,7 @@ public void badObject() { } /** - * Verifies that the constructor has backwards compatability with RAW types pre-java5. + * Verifies that the constructor has backwards compatibility with RAW types pre-java5. */ @Test public void verifyConstructor() { @@ -130,7 +128,7 @@ public void verifyConstructor() { } /** - * Verifies that the put Collection has backwards compatability with RAW types pre-java5. + * Verifies that the put Collection has backwards compatibility with RAW types pre-java5. */ @Test public void verifyPutCollection() { @@ -164,7 +162,7 @@ public void verifyPutCollection() { /** - * Verifies that the put Map has backwards compatability with RAW types pre-java5. + * Verifies that the put Map has backwards compatibility with RAW types pre-java5. */ @Test public void verifyPutMap() { @@ -209,9 +207,10 @@ public void verifyPutMap() { * Create a JSONArray doc with a variety of different elements. * Confirm that the values can be accessed via the get[type]() API methods */ + @SuppressWarnings("boxing") @Test public void getArrayValues() { - JSONArray jsonArray = new JSONArray(arrayStr); + JSONArray jsonArray = new JSONArray(this.arrayStr); // booleans assertTrue("Array true", true == jsonArray.getBoolean(0)); @@ -255,7 +254,7 @@ public void getArrayValues() { */ @Test public void failedGetArrayValues() { - JSONArray jsonArray = new JSONArray(arrayStr); + JSONArray jsonArray = new JSONArray(this.arrayStr); try { jsonArray.getBoolean(4); assertTrue("expected getBoolean to fail", false); @@ -321,7 +320,7 @@ public void failedGetArrayValues() { */ @Test public void join() { - JSONArray jsonArray = new JSONArray(arrayStr); + JSONArray jsonArray = new JSONArray(this.arrayStr); String joinStr = jsonArray.join(","); // validate JSON @@ -357,7 +356,7 @@ public void join() { public void length() { assertTrue("expected empty JSONArray length 0", new JSONArray().length() == 0); - JSONArray jsonArray = new JSONArray(arrayStr); + JSONArray jsonArray = new JSONArray(this.arrayStr); assertTrue("expected JSONArray length 13", jsonArray.length() == 13); JSONArray nestedJsonArray = jsonArray.getJSONArray(9); assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1); @@ -368,9 +367,10 @@ public void length() { * Confirm that the values can be accessed via the opt[type](index) * and opt[type](index, default) API methods. */ + @SuppressWarnings("boxing") @Test public void opt() { - JSONArray jsonArray = new JSONArray(arrayStr); + JSONArray jsonArray = new JSONArray(this.arrayStr); assertTrue("Array opt value true", Boolean.TRUE == jsonArray.opt(0)); assertTrue("Array opt value out of range", @@ -441,6 +441,7 @@ public void optStringConversion(){ * Exercise the JSONArray.put(value) method with various parameters * and confirm the resulting JSONArray. */ + @SuppressWarnings("boxing") @Test public void put() { JSONArray jsonArray = new JSONArray(); @@ -516,6 +517,7 @@ public void put() { * Exercise the JSONArray.put(index, value) method with various parameters * and confirm the resulting JSONArray. */ + @SuppressWarnings("boxing") @Test public void putIndex() { JSONArray jsonArray = new JSONArray(); @@ -596,11 +598,11 @@ public void putIndex() { */ @Test public void remove() { - String arrayStr = + String arrayStr1 = "["+ "1"+ "]"; - JSONArray jsonArray = new JSONArray(arrayStr); + JSONArray jsonArray = new JSONArray(arrayStr1); jsonArray.remove(0); assertTrue("array should be empty", null == jsonArray.remove(5)); assertTrue("jsonArray should be empty", jsonArray.length() == 0); @@ -612,11 +614,11 @@ public void remove() { */ @Test public void notSimilar() { - String arrayStr = + String arrayStr1 = "["+ "1"+ "]"; - JSONArray jsonArray = new JSONArray(arrayStr); + JSONArray jsonArray = new JSONArray(arrayStr1); JSONArray otherJsonArray = new JSONArray(); assertTrue("arrays lengths differ", !jsonArray.similar(otherJsonArray)); @@ -745,9 +747,10 @@ public void objectArrayVsIsArray() { /** * Exercise the JSONArray iterator. */ + @SuppressWarnings("boxing") @Test public void iterator() { - JSONArray jsonArray = new JSONArray(arrayStr); + JSONArray jsonArray = new JSONArray(this.arrayStr); Iterator it = jsonArray.iterator(); assertTrue("Array true", Boolean.TRUE.equals(it.next())); @@ -803,16 +806,20 @@ public void optQueryWithSyntaxError() { * Exercise the JSONArray write() method */ @Test - public void write() { + public void write() throws IOException { String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]"; JSONArray jsonArray = new JSONArray(str); String expectedStr = str; StringWriter stringWriter = new StringWriter(); - Writer writer = jsonArray.write(stringWriter); - String actualStr = writer.toString(); - assertTrue("write() expected " + expectedStr + - " but found " + actualStr, - expectedStr.equals(actualStr)); + try { + jsonArray.write(stringWriter); + String actualStr = stringWriter.toString(); + assertTrue("write() expected " + expectedStr + + " but found " + actualStr, + expectedStr.equals(actualStr)); + } finally { + stringWriter.close(); + } } /** @@ -837,7 +844,7 @@ public void writeAppendable() { * Exercise the JSONArray write(Writer, int, int) method */ @Test - public void write3Param() { + public void write3Param() throws IOException { String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]"; String str2 = "[\n" + @@ -852,15 +859,20 @@ public void write3Param() { JSONArray jsonArray = new JSONArray(str0); String expectedStr = str0; StringWriter stringWriter = new StringWriter(); - Writer writer = jsonArray.write(stringWriter, 0, 0); - String actualStr = writer.toString(); - assertEquals(expectedStr, actualStr); - - expectedStr = str2; + try { + String actualStr = jsonArray.write(stringWriter, 0, 0).toString(); + assertEquals(expectedStr, actualStr); + } finally { + stringWriter.close(); + } stringWriter = new StringWriter(); - writer = jsonArray.write(stringWriter, 2, 1); - actualStr = writer.toString(); - assertEquals(expectedStr, actualStr); + try { + expectedStr = str2; + String actualStr = jsonArray.write(stringWriter, 2, 1).toString(); + assertEquals(expectedStr, actualStr); + } finally { + stringWriter.close(); + } } /** @@ -917,49 +929,49 @@ public void toList() { "]"; JSONArray jsonArray = new JSONArray(jsonArrayStr); - List list = jsonArray.toList(); + List list = jsonArray.toList(); assertTrue("List should not be null", list != null); assertTrue("List should have 3 elements", list.size() == 3); - List val1List = (List) list.get(0); + List val1List = (List) list.get(0); assertTrue("val1 should not be null", val1List != null); assertTrue("val1 should have 3 elements", val1List.size() == 3); assertTrue("val1 value 1 should be 1", val1List.get(0).equals(Integer.valueOf(1))); assertTrue("val1 value 2 should be 2", val1List.get(1).equals(Integer.valueOf(2))); - Map key1Value3Map = (Map)val1List.get(2); + Map key1Value3Map = (Map)val1List.get(2); assertTrue("Map should not be null", key1Value3Map != null); assertTrue("Map should have 1 element", key1Value3Map.size() == 1); assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE)); - Map val2Map = (Map) list.get(1); + Map val2Map = (Map) list.get(1); assertTrue("val2 should not be null", val2Map != null); assertTrue("val2 should have 4 elements", val2Map.size() == 4); assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1")); assertTrue("val2 map key 3 should be 42", val2Map.get("key3").equals(Integer.valueOf(42))); - Map val2Key2Map = (Map)val2Map.get("key2"); + Map val2Key2Map = (Map)val2Map.get("key2"); assertTrue("val2 map key 2 should not be null", val2Key2Map != null); assertTrue("val2 map key 2 should have an entry", val2Key2Map.containsKey("key2")); assertTrue("val2 map key 2 value should be null", val2Key2Map.get("key2") == null); - List val2Key4List = (List)val2Map.get("key4"); + List val2Key4List = (List)val2Map.get("key4"); assertTrue("val2 map key 4 should not be null", val2Key4List != null); assertTrue("val2 map key 4 should be empty", val2Key4List.isEmpty()); - List val3List = (List) list.get(2); + List val3List = (List) list.get(2); assertTrue("val3 should not be null", val3List != null); assertTrue("val3 should have 2 elements", val3List.size() == 2); - List val3Val1List = (List)val3List.get(0); + List val3Val1List = (List)val3List.get(0); assertTrue("val3 list val 1 should not be null", val3Val1List != null); assertTrue("val3 list val 1 should have 2 elements", val3Val1List.size() == 2); assertTrue("val3 list val 1 list element 1 should be value1", val3Val1List.get(0).equals("value1")); assertTrue("val3 list val 1 list element 2 should be 2.1", val3Val1List.get(1).equals(Double.valueOf("2.1"))); - List val3Val2List = (List)val3List.get(1); + List val3Val2List = (List)val3List.get(1); assertTrue("val3 list val 2 should not be null", val3Val2List != null); assertTrue("val3 list val 2 should have 1 element", val3Val2List.size() == 1); assertTrue("val3 list val 2 list element 1 should be null", val3Val2List.get(0) == null); diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 12985912d..1ad2cb416 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -711,8 +711,7 @@ public void testToJSONArray_jsonOutput() { } /** - * JSON string cannot be reverted to original xml. See test result in - * comment below. + * JSON string cannot be reverted to original xml when type guessing is used. */ @Test public void testToJSONArray_reversibility() { @@ -722,10 +721,11 @@ public void testToJSONArray_reversibility() { } /** - * test passes when using the new method toJsonML. + * JSON string cannot be reverted to original xml when type guessing is used. + * When we force all the values as string, the original text comes back. */ @Test - public void testToJsonML() { + public void testToJSONArray_reversibility2() { final String originalXml = "011000True"; final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]"; final JSONArray json = JSONML.toJSONArray(originalXml,true); @@ -735,4 +735,71 @@ public void testToJsonML() { assertEquals(originalXml, reverseXml); } + /** + * JSON can be reverted to original xml. + */ + @Test + public void testToJSONArray_reversibility3() { + final String originalXml = "400402"; + final JSONArray jsonArray = JSONML.toJSONArray(originalXml, false); + final String revertedXml = JSONML.toString(jsonArray); + assertEquals(revertedXml, originalXml); + } + + /** + * JSON string cannot be reverted to original xml. See test result in + * comment below. + */ + @Test + public void testToJSONObject_reversibility() { + final String originalXml = "400402"; + final JSONObject originalObject=JSONML.toJSONObject(originalXml,false); + final String originalJson = originalObject.toString(); + final String xml = JSONML.toString(originalObject); + final JSONObject revertedObject = JSONML.toJSONObject(xml, false); + final String newJson = revertedObject.toString(); + assertTrue("JSON Objects are not similar",originalObject.similar(revertedObject)); + assertEquals("original JSON does not equal the new JSON",originalJson, newJson); + } + + /** + * Test texts taken from jsonml.org. Currently our implementation FAILS this conversion but shouldn't. + * Technically JsonML should be able to transform any valid xhtml document, but ours only supports + * standard XML entities, not HTML entities. + */ + @Test + public void testAttributeConversionReversabilityHTML() { + final String originalXml = "
#5D28D1Example text here
#AF44EF127310656
#AAD034 © 
"; + final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"\u00A0\",[\"span\",{ \"style\" : \"background-color:maroon\" },\"\u00A9\"],\"\u00A0\"]]]"; + final JSONArray json = JSONML.toJSONArray(originalXml,true); + final String actualJsonString = json.toString(); + + final String reverseXml = JSONML.toString(json); + assertNotEquals(originalXml, reverseXml); + + assertNotEquals(expectedJsonString, actualJsonString); + } + +// this test does not pass for the following reasons: +// 1. Our XML parser does not handle generic HTML entities, only valid XML entities. Hence   +// or other HTML specific entites would fail on reversability +// 2. Our JSON implementation for storing the XML attributes uses the standard unordered map. +// This means that can not be reversed reliably. +// /** +// * Test texts taken from jsonml.org but modified to have XML entities only. +// */ +// @Test +// public void testAttributeConversionReversabilityXML() { +// final String originalXml = "
#5D28D1Example text here
#AF44EF127310656
#AAD034&><
"; +// final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"&\",[\"span\",{ \"style\" : \"background-color:maroon\" },\">\"],\"<\"]]]"; +// final JSONArray jsonML = JSONML.toJSONArray(originalXml,true); +// final String actualJsonString = jsonML.toString(); +// +// final String reverseXml = JSONML.toString(jsonML); +// // currently not equal because the hashing of the attribute objects makes the attribute +// // order not happen the same way twice +// assertEquals(originalXml, reverseXml); +// +// assertEquals(expectedJsonString, actualJsonString); +// } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 1dec2fd67..fb32cda74 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1,2336 +1,2371 @@ -package org.json.junit; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.StringReader; -import java.io.StringWriter; -import java.io.Writer; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; - -import org.json.CDL; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.json.JSONPointerException; -import org.json.XML; -import org.junit.Test; - -import com.jayway.jsonpath.Configuration; -import com.jayway.jsonpath.JsonPath; - -/** - * JSONObject, along with JSONArray, are the central classes of the reference app. - * All of the other classes interact with them, and JSON functionality would - * otherwise be impossible. - */ -public class JSONObjectTest { - - /** - * JSONObject built from a bean, but only using a null value. - * Nothing good is expected to happen. - * Expects NullPointerException - */ - @Test(expected=NullPointerException.class) - public void jsonObjectByNullBean() { - MyBean myBean = null; - new JSONObject(myBean); - } - - /** - * The JSON parser is permissive of unambiguous unquoted keys and values. - * Such JSON text should be allowed, even if it does not strictly conform - * to the spec. However, after being parsed, toString() should emit strictly - * conforming JSON text. - */ - @Test - public void unquotedText() { - String str = "{key1:value1, key2:42}"; - JSONObject jsonObject = new JSONObject(str); - String textStr = jsonObject.toString(); - assertTrue("expected key1", textStr.contains("\"key1\"")); - assertTrue("expected value1", textStr.contains("\"value1\"")); - assertTrue("expected key2", textStr.contains("\"key2\"")); - assertTrue("expected 42", textStr.contains("42")); - } - - /** - * A JSONObject can be created with no content - */ - @Test - public void emptyJsonObject() { - JSONObject jsonObject = new JSONObject(); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); - } - - /** - * A JSONObject can be created from another JSONObject plus a list of names. - * In this test, some of the starting JSONObject keys are not in the - * names list. - */ - @Test - public void jsonObjectByNames() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"nullKey\":null,"+ - "\"stringKey\":\"hello world!\","+ - "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ - "\"intKey\":42,"+ - "\"doubleKey\":-23.45e67"+ - "}"; - String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"}; - JSONObject jsonObject = new JSONObject(str); - - // validate JSON - JSONObject jsonObjectByName = new JSONObject(jsonObject, keys); - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObjectByName.toString()); - assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); - assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObjectByName.query("/falseKey"))); - assertTrue("expected \"nullKey\":null", JSONObject.NULL.equals(jsonObjectByName.query("/nullKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObjectByName.query("/stringKey"))); - assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObjectByName.query("/doubleKey"))); - } - - /** - * JSONObjects can be built from a Map. - * In this test the map is null. - * the JSONObject(JsonTokener) ctor is not tested directly since it already - * has full coverage from other tests. - */ - @Test - public void jsonObjectByNullMap() { - Map map = null; - JSONObject jsonObject = new JSONObject(map); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); - } - - /** - * JSONObjects can be built from a Map. - * In this test all of the map entries are valid JSON types. - */ - @Test - public void jsonObjectByMap() { - Map map = new HashMap(); - map.put("trueKey", new Boolean(true)); - map.put("falseKey", new Boolean(false)); - map.put("stringKey", "hello world!"); - map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - map.put("intKey", new Long(42)); - map.put("doubleKey", new Double(-23.45e67)); - JSONObject jsonObject = new JSONObject(map); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); - assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); - assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey"))); - } - - /** - * Verifies that the constructor has backwards compatability with RAW types pre-java5. - */ - @Test - public void verifyConstructor() { - - final JSONObject expected = new JSONObject("{\"myKey\":10}"); - - @SuppressWarnings("rawtypes") - Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); - JSONObject jaRaw = new JSONObject(myRawC); - - Map myCStrObj = Collections.singletonMap("myKey", - (Object) Integer.valueOf(10)); - JSONObject jaStrObj = new JSONObject(myCStrObj); - - Map myCStrInt = Collections.singletonMap("myKey", - Integer.valueOf(10)); - JSONObject jaStrInt = new JSONObject(myCStrInt); - - Map myCObjObj = Collections.singletonMap((Object) "myKey", - (Object) Integer.valueOf(10)); - JSONObject jaObjObj = new JSONObject(myCObjObj); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrInt)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObjObj)); - } - - /** - * Tests Number serialization. - */ - @Test - public void verifyNumberOutput(){ - /** - * MyNumberContainer is a POJO, so call JSONObject(bean), - * which builds a map of getter names/values - * The only getter is getMyNumber (key=myNumber), - * whose return value is MyNumber. MyNumber extends Number, - * but is not recognized as such by wrap() per current - * implementation, so wrap() returns the default new JSONObject(bean). - * The only getter is getNumber (key=number), whose return value is - * BigDecimal(42). - */ - JSONObject jsonObject = new JSONObject(new MyNumberContainer()); - String actual = jsonObject.toString(); - String expected = "{\"myNumber\":{\"number\":42}}"; - assertEquals("Not Equal", expected , actual); - - /** - * JSONObject.put() handles objects differently than the - * bean constructor. Where the bean ctor wraps objects before - * placing them in the map, put() inserts the object without wrapping. - * In this case, a MyNumber instance is the value. - * The MyNumber.toString() method is responsible for - * returning a reasonable value: the string '42'. - */ - jsonObject = new JSONObject(); - jsonObject.put("myNumber", new MyNumber()); - actual = jsonObject.toString(); - expected = "{\"myNumber\":42}"; - assertEquals("Not Equal", expected , actual); - - /** - * Calls the JSONObject(Map) ctor, which calls wrap() for values. - * AtomicInteger is a Number, but is not recognized by wrap(), per - * current implementation. However, the type is - * 'java.util.concurrent.atomic', so due to the 'java' prefix, - * wrap() inserts the value as a string. That is why 42 comes back - * wrapped in quotes. - */ - jsonObject = new JSONObject(Collections.singletonMap("myNumber", new AtomicInteger(42))); - actual = jsonObject.toString(); - expected = "{\"myNumber\":\"42\"}"; - assertEquals("Not Equal", expected , actual); - - /** - * JSONObject.put() inserts the AtomicInteger directly into the - * map not calling wrap(). In toString()->write()->writeValue(), - * AtomicInteger is recognized as a Number, and converted via - * numberToString() into the unquoted string '42'. - */ - jsonObject = new JSONObject(); - jsonObject.put("myNumber", new AtomicInteger(42)); - actual = jsonObject.toString(); - expected = "{\"myNumber\":42}"; - assertEquals("Not Equal", expected , actual); - - /** - * Calls the JSONObject(Map) ctor, which calls wrap() for values. - * Fraction is a Number, but is not recognized by wrap(), per - * current implementation. As a POJO, Franction is handled as a - * bean and inserted into a contained JSONObject. It has 2 getters, - * for numerator and denominator. - */ - jsonObject = new JSONObject(Collections.singletonMap("myNumber", new Fraction(4,2))); - assertEquals(1, jsonObject.length()); - assertEquals(2, ((JSONObject)(jsonObject.get("myNumber"))).length()); - assertEquals("Numerator", BigInteger.valueOf(4) , jsonObject.query("/myNumber/numerator")); - assertEquals("Denominator", BigInteger.valueOf(2) , jsonObject.query("/myNumber/denominator")); - - /** - * JSONObject.put() inserts the Fraction directly into the - * map not calling wrap(). In toString()->write()->writeValue(), - * Fraction is recognized as a Number, and converted via - * numberToString() into the unquoted string '4/2'. But the - * BigDecimal sanity check fails, so writeValue() defaults - * to returning a safe JSON quoted string. Pretty slick! - */ - jsonObject = new JSONObject(); - jsonObject.put("myNumber", new Fraction(4,2)); - actual = jsonObject.toString(); - expected = "{\"myNumber\":\"4/2\"}"; // valid JSON, bug fixed - assertEquals("Not Equal", expected , actual); - } - - /** - * Verifies that the put Collection has backwards compatability with RAW types pre-java5. - */ - @Test - public void verifyPutCollection() { - - final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); - - @SuppressWarnings("rawtypes") - Collection myRawC = Collections.singleton(Integer.valueOf(10)); - JSONObject jaRaw = new JSONObject(); - jaRaw.put("myCollection", myRawC); - - Collection myCObj = Collections.singleton((Object) Integer - .valueOf(10)); - JSONObject jaObj = new JSONObject(); - jaObj.put("myCollection", myCObj); - - Collection myCInt = Collections.singleton(Integer - .valueOf(10)); - JSONObject jaInt = new JSONObject(); - jaInt.put("myCollection", myCInt); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaInt)); - } - - - /** - * Verifies that the put Map has backwards compatability with RAW types pre-java5. - */ - @Test - public void verifyPutMap() { - - final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); - - @SuppressWarnings("rawtypes") - Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); - JSONObject jaRaw = new JSONObject(); - jaRaw.put("myMap", myRawC); - - Map myCStrObj = Collections.singletonMap("myKey", - (Object) Integer.valueOf(10)); - JSONObject jaStrObj = new JSONObject(); - jaStrObj.put("myMap", myCStrObj); - - Map myCStrInt = Collections.singletonMap("myKey", - Integer.valueOf(10)); - JSONObject jaStrInt = new JSONObject(); - jaStrInt.put("myMap", myCStrInt); - - Map myCObjObj = Collections.singletonMap((Object) "myKey", - (Object) Integer.valueOf(10)); - JSONObject jaObjObj = new JSONObject(); - jaObjObj.put("myMap", myCObjObj); - - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaRaw)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrObj)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaStrInt)); - assertTrue( - "The RAW Collection should give me the same as the Typed Collection", - expected.similar(jaObjObj)); - } - - - /** - * JSONObjects can be built from a Map. - * In this test the map entries are not valid JSON types. - * The actual conversion is kind of interesting. - */ - @Test - public void jsonObjectByMapWithUnsupportedValues() { - Map jsonMap = new HashMap(); - // Just insert some random objects - jsonMap.put("key1", new CDL()); - jsonMap.put("key2", new Exception()); - - JSONObject jsonObject = new JSONObject(jsonMap); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected 0 key1 items", ((Map)(JsonPath.read(doc, "$.key1"))).size() == 0); - assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(jsonObject.query("/key2"))); - } - - /** - * JSONObjects can be built from a Map. - * In this test one of the map values is null - */ - @Test - public void jsonObjectByMapWithNullValue() { - Map map = new HashMap(); - map.put("trueKey", new Boolean(true)); - map.put("falseKey", new Boolean(false)); - map.put("stringKey", "hello world!"); - map.put("nullKey", null); - map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - map.put("intKey", new Long(42)); - map.put("doubleKey", new Double(-23.45e67)); - JSONObject jsonObject = new JSONObject(map); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); - assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); - assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey"))); - assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); - assertTrue("expected \"intKey\":42", Long.valueOf("42").equals(jsonObject.query("/intKey"))); - assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey"))); - } - - /** - * JSONObject built from a bean. In this case all but one of the - * bean getters return valid JSON types - */ - @Test - public void jsonObjectByBean() { - /** - * Default access classes have to be mocked since JSONObject, which is - * not in the same package, cannot call MyBean methods by reflection. - */ - MyBean myBean = mock(MyBean.class); - when(myBean.getDoubleKey()).thenReturn(-23.45e7); - when(myBean.getIntKey()).thenReturn(42); - when(myBean.getStringKey()).thenReturn("hello world!"); - when(myBean.getEscapeStringKey()).thenReturn("h\be\tllo w\u1234orld!"); - when(myBean.isTrueKey()).thenReturn(true); - when(myBean.isFalseKey()).thenReturn(false); - when(myBean.getStringReaderKey()).thenReturn( - new StringReader("") { - }); - - JSONObject jsonObject = new JSONObject(myBean); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 8 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 8); - assertTrue("expected 0 items in stringReaderKey", ((Map) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0); - assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); - assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); - assertTrue("expected hello world!","hello world!".equals(jsonObject.query("/stringKey"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); - assertTrue("expected 42", Integer.valueOf("42").equals(jsonObject.query("/intKey"))); - assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(jsonObject.query("/doubleKey"))); - // sorry, mockito artifact - assertTrue("expected 2 callbacks items", ((List)(JsonPath.read(doc, "$.callbacks"))).size() == 2); - assertTrue("expected 0 handler items", ((Map)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0); - assertTrue("expected 0 callbacks[1] items", ((Map)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0); - } - - /** - * A bean is also an object. But in order to test the JSONObject - * ctor that takes an object and a list of names, - * this particular bean needs some public - * data members, which have been added to the class. - */ - @Test - public void jsonObjectByObjectAndNames() { - String[] keys = {"publicString", "publicInt"}; - // just need a class that has public data members - MyPublicClass myPublicClass = new MyPublicClass(); - JSONObject jsonObject = new JSONObject(myPublicClass, keys); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected \"publicString\":\"abc\"", "abc".equals(jsonObject.query("/publicString"))); - assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(jsonObject.query("/publicInt"))); - } - - /** - * Exercise the JSONObject from resource bundle functionality. - * The test resource bundle is uncomplicated, but provides adequate test coverage. - */ - @Test - public void jsonObjectByResourceBundle() { - JSONObject jsonObject = new - JSONObject("org.json.junit.StringsResourceBundle", - Locale.getDefault()); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); - assertTrue("expected 2 greetings items", ((Map)(JsonPath.read(doc, "$.greetings"))).size() == 2); - assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(jsonObject.query("/greetings/hello"))); - assertTrue("expected \"world\":\"World!\"", "World!".equals(jsonObject.query("/greetings/world"))); - assertTrue("expected 2 farewells items", ((Map)(JsonPath.read(doc, "$.farewells"))).size() == 2); - assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later"))); - assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator"))); - } - - /** - * Exercise the JSONObject.accumulate() method - */ - @Test - public void jsonObjectAccumulate() { - - JSONObject jsonObject = new JSONObject(); - jsonObject.accumulate("myArray", true); - jsonObject.accumulate("myArray", false); - jsonObject.accumulate("myArray", "hello world!"); - 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()); - assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); - assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0"))); - assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1"))); - assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3"))); - assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4"))); - assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5"))); - } - - /** - * Exercise the JSONObject append() functionality - */ - @Test - public void jsonObjectAppend() { - 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()); - assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); - assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0"))); - assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1/"))); - assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2"))); - assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3"))); - assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4"))); - assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5"))); - } - - /** - * Exercise the JSONObject doubleToString() method - */ - @Test - public void jsonObjectDoubleToString() { - String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" }; - Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, - Double.NaN, Double.NEGATIVE_INFINITY }; - for (int i = 0; i < expectedStrs.length; ++i) { - String actualStr = JSONObject.doubleToString(doubles[i]); - assertTrue("value expected ["+expectedStrs[i]+ - "] found ["+actualStr+ "]", - expectedStrs[i].equals(actualStr)); - } - } - - /** - * Exercise some JSONObject get[type] and opt[type] methods - */ - @Test - public void jsonObjectValues() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"trueStrKey\":\"true\","+ - "\"falseStrKey\":\"false\","+ - "\"stringKey\":\"hello world!\","+ - "\"intKey\":42,"+ - "\"intStrKey\":\"43\","+ - "\"longKey\":1234567890123456789,"+ - "\"longStrKey\":\"987654321098765432\","+ - "\"doubleKey\":-23.45e7,"+ - "\"doubleStrKey\":\"00001.000\","+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{\"myKey\":\"myVal\"}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); - assertTrue("opt trueKey should be true", jsonObject.optBoolean("trueKey")); - assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey")); - assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey")); - assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey")); - assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey")); - assertTrue("stringKey should be string", - jsonObject.getString("stringKey").equals("hello world!")); - assertTrue("doubleKey should be double", - jsonObject.getDouble("doubleKey") == -23.45e7); - assertTrue("doubleStrKey should be double", - jsonObject.getDouble("doubleStrKey") == 1); - 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("intKey should be int", - jsonObject.optInt("intKey") == 42); - assertTrue("opt intKey should be int", - jsonObject.optInt("intKey", 0) == 42); - assertTrue("opt intKey with default should be int", - jsonObject.getInt("intKey") == 42); - assertTrue("intStrKey should be int", - jsonObject.getInt("intStrKey") == 43); - assertTrue("longKey should be long", - jsonObject.getLong("longKey") == 1234567890123456789L); - assertTrue("opt longKey should be long", - jsonObject.optLong("longKey") == 1234567890123456789L); - assertTrue("opt longKey with default should be long", - jsonObject.optLong("longKey", 0) == 1234567890123456789L); - assertTrue("longStrKey should be long", - jsonObject.getLong("longStrKey") == 987654321098765432L); - assertTrue("xKey should not exist", - jsonObject.isNull("xKey")); - assertTrue("stringKey should exist", - jsonObject.has("stringKey")); - assertTrue("opt stringKey should string", - jsonObject.optString("stringKey").equals("hello world!")); - assertTrue("opt stringKey with default should string", - jsonObject.optString("stringKey", "not found").equals("hello world!")); - JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); - assertTrue("arrayKey should be JSONArray", - jsonArray.getInt(0) == 0 && - jsonArray.getInt(1) == 1 && - jsonArray.getInt(2) == 2); - jsonArray = jsonObject.optJSONArray("arrayKey"); - assertTrue("opt arrayKey should be JSONArray", - jsonArray.getInt(0) == 0 && - jsonArray.getInt(1) == 1 && - jsonArray.getInt(2) == 2); - JSONObject jsonObjectInner = jsonObject.getJSONObject("objectKey"); - assertTrue("objectKey should be JSONObject", - jsonObjectInner.get("myKey").equals("myVal")); - } - - /** - * Check whether JSONObject handles large or high precision numbers correctly - */ - @Test - public void stringToValueNumbersTest() { - assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0") instanceof Double); - assertTrue("-0.0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double); - assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String); - assertTrue( "0.2 should be a Double!", - JSONObject.stringToValue( "0.2" ) instanceof Double ); - assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!", - JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double ); - /** - * This test documents a need for BigDecimal conversion. - */ - Object obj = JSONObject.stringToValue( "299792.457999999984" ); - assertTrue( "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!", - obj.equals(new Double(299792.458)) ); - assertTrue( "1 should be an Integer!", - JSONObject.stringToValue( "1" ) instanceof Integer ); - assertTrue( "Integer.MAX_VALUE should still be an Integer!", - JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer ); - assertTrue( "Large integers should be a Long!", - JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long ); - assertTrue( "Long.MAX_VALUE should still be an Integer!", - JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long ); - - String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString(); - assertTrue( "Really large integers currently evaluate to string", - JSONObject.stringToValue(str).equals("9223372036854775808")); - } - - /** - * This test documents numeric values which could be numerically - * handled as BigDecimal or BigInteger. It helps determine what outputs - * will change if those types are supported. - */ - @Test - public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() { - // Valid JSON Numbers, probably should return BigDecimal or BigInteger objects - String str = - "{"+ - "\"numberWithDecimals\":299792.457999999984,"+ - "\"largeNumber\":12345678901234567890,"+ - "\"preciseNumber\":0.2000000000000000111,"+ - "\"largeExponent\":-23.45e2327"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - // Comes back as a double, but loses precision - assertTrue( "numberWithDecimals currently evaluates to double 299792.458", - jsonObject.get( "numberWithDecimals" ).equals( new Double( "299792.458" ) ) ); - Object obj = jsonObject.get( "largeNumber" ); - assertTrue("largeNumber currently evaluates to string", - "12345678901234567890".equals(obj)); - // comes back as a double but loses precision - assertTrue( "preciseNumber currently evaluates to double 0.2", - jsonObject.get( "preciseNumber" ).equals(new Double(0.2))); - obj = jsonObject.get( "largeExponent" ); - assertTrue("largeExponent should currently evaluates as a string", - "-23.45e2327".equals(obj)); - } - - /** - * This test documents how JSON-Java handles invalid numeric input. - */ - @Test - public void jsonInvalidNumberValues() { - // Number-notations supported by Java and invalid as JSON - String str = - "{"+ - "\"hexNumber\":-0x123,"+ - "\"tooManyZeros\":00,"+ - "\"negativeInfinite\":-Infinity,"+ - "\"negativeNaN\":-NaN,"+ - "\"negativeFraction\":-.01,"+ - "\"tooManyZerosFraction\":00.001,"+ - "\"negativeHexFloat\":-0x1.fffp1,"+ - "\"hexFloat\":0x1.0P-1074,"+ - "\"floatIdentifier\":0.1f,"+ - "\"doubleIdentifier\":0.1d"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - Object obj; - obj = jsonObject.get( "hexNumber" ); - assertFalse( "hexNumber must not be a number (should throw exception!?)", - obj instanceof Number ); - assertTrue("hexNumber currently evaluates to string", - obj.equals("-0x123")); - assertTrue( "tooManyZeros currently evaluates to string", - jsonObject.get( "tooManyZeros" ).equals("00")); - obj = jsonObject.get("negativeInfinite"); - assertTrue( "negativeInfinite currently evaluates to string", - obj.equals("-Infinity")); - obj = jsonObject.get("negativeNaN"); - assertTrue( "negativeNaN currently evaluates to string", - obj.equals("-NaN")); - assertTrue( "negativeFraction currently evaluates to double -0.01", - jsonObject.get( "negativeFraction" ).equals(new Double(-0.01))); - assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", - jsonObject.get( "tooManyZerosFraction" ).equals(new Double(0.001))); - assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", - jsonObject.get( "negativeHexFloat" ).equals(new Double(-3.99951171875))); - assertTrue("hexFloat currently evaluates to double 4.9E-324", - jsonObject.get("hexFloat").equals(new Double(4.9E-324))); - assertTrue("floatIdentifier currently evaluates to double 0.1", - jsonObject.get("floatIdentifier").equals(new Double(0.1))); - assertTrue("doubleIdentifier currently evaluates to double 0.1", - jsonObject.get("doubleIdentifier").equals(new Double(0.1))); - } - - /** - * Tests how JSONObject get[type] handles incorrect types - */ - @Test - public void jsonObjectNonAndWrongValues() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"trueStrKey\":\"true\","+ - "\"falseStrKey\":\"false\","+ - "\"stringKey\":\"hello world!\","+ - "\"intKey\":42,"+ - "\"intStrKey\":\"43\","+ - "\"longKey\":1234567890123456789,"+ - "\"longStrKey\":\"987654321098765432\","+ - "\"doubleKey\":-23.45e7,"+ - "\"doubleStrKey\":\"00001.000\","+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{\"myKey\":\"myVal\"}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - try { - jsonObject.getBoolean("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("expecting an exception message", - "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); - } - try { - jsonObject.getBoolean("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a Boolean.". - equals(e.getMessage())); - } - try { - jsonObject.getString("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getString("trueKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"trueKey\"] not a string.". - equals(e.getMessage())); - } - try { - jsonObject.getDouble("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getDouble("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.". - equals(e.getMessage())); - } - try { - jsonObject.getInt("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getInt("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not an int.". - equals(e.getMessage())); - } - try { - jsonObject.getLong("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getLong("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a long.". - equals(e.getMessage())); - } - try { - jsonObject.getJSONArray("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getJSONArray("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONArray.". - equals(e.getMessage())); - } - try { - jsonObject.getJSONObject("nonKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); - } - try { - jsonObject.getJSONObject("stringKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONObject.". - equals(e.getMessage())); - } - } - - /** - * This test documents an unexpected numeric behavior. - * A double that ends with .0 is parsed, serialized, then - * parsed again. On the second parse, it has become an int. - */ - @Test - public void unexpectedDoubleToIntConversion() { - String key30 = "key30"; - String key31 = "key31"; - JSONObject jsonObject = new JSONObject(); - jsonObject.put(key30, new Double(3.0)); - jsonObject.put(key31, new Double(3.1)); - - assertTrue("3.0 should remain a double", - jsonObject.getDouble(key30) == 3); - assertTrue("3.1 should remain a double", - jsonObject.getDouble(key31) == 3.1); - - // turns 3.0 into 3. - String serializedString = jsonObject.toString(); - JSONObject deserialized = new JSONObject(serializedString); - assertTrue("3.0 is now an int", deserialized.get(key30) instanceof Integer); - assertTrue("3.0 can still be interpreted as a double", - deserialized.getDouble(key30) == 3.0); - assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1); - } - - /** - * Document behaviors of big numbers. Includes both JSONObject - * and JSONArray tests - */ - @Test - public void bigNumberOperations() { - /** - * JSONObject tries to parse BigInteger as a bean, but it only has - * one getter, getLowestBitSet(). The value is lost and an unhelpful - * value is stored. This should be fixed. - */ - BigInteger bigInteger = new BigInteger("123456789012345678901234567890"); - JSONObject jsonObject = new JSONObject(bigInteger); - Object obj = jsonObject.get("lowestSetBit"); - assertTrue("JSONObject only has 1 value", jsonObject.length() == 1); - assertTrue("JSONObject parses BigInteger as the Integer lowestBitSet", - obj instanceof Integer); - assertTrue("this bigInteger lowestBitSet happens to be 1", - obj.equals(1)); - - /** - * JSONObject tries to parse BigDecimal as a bean, but it has - * no getters, The value is lost and no value is stored. - * This should be fixed. - */ - BigDecimal bigDecimal = new BigDecimal( - "123456789012345678901234567890.12345678901234567890123456789"); - jsonObject = new JSONObject(bigDecimal); - assertTrue("large bigDecimal is not stored", jsonObject.length() == 0); - - /** - * JSONObject put(String, Object) method stores and serializes - * bigInt and bigDec correctly. Nothing needs to change. - */ - 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 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 serializes bigDec correctly", - jsonObject.toString().equals( - "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); - - /** - * 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. - */ - String str = JSONObject.numberToString(bigInteger); - assertTrue("numberToString() handles bigInteger correctly", - str.equals("123456789012345678901234567890")); - str = JSONObject.numberToString(bigDecimal); - assertTrue("numberToString() handles bigDecimal correctly", - str.equals("123456789012345678901234567890.12345678901234567890123456789")); - - /** - * JSONObject.stringToValue() turns bigInt into an accurate string, - * and rounds bigDec. This incorrect, but users may have come to - * expect this behavior. Change would be marginally better, but - * might inconvenience users. - */ - obj = JSONObject.stringToValue(bigInteger.toString()); - assertTrue("stringToValue() turns bigInteger string into string", - obj instanceof String); - obj = JSONObject.stringToValue(bigDecimal.toString()); - assertTrue("stringToValue() changes bigDecimal string", - !obj.toString().equals(bigDecimal.toString())); - - /** - * wrap() vs put() big number behavior is now the same. - */ - // bigInt map ctor - Map map = new HashMap(); - map.put("bigInt", bigInteger); - jsonObject = new JSONObject(map); - String actualFromMapStr = jsonObject.toString(); - assertTrue("bigInt in map (or array or bean) is a string", - actualFromMapStr.equals( - "{\"bigInt\":123456789012345678901234567890}")); - // bigInt put - jsonObject = new JSONObject(); - jsonObject.put("bigInt", bigInteger); - String actualFromPutStr = jsonObject.toString(); - assertTrue("bigInt from put is a number", - actualFromPutStr.equals( - "{\"bigInt\":123456789012345678901234567890}")); - // bigDec map ctor - map = new HashMap(); - map.put("bigDec", bigDecimal); - jsonObject = new JSONObject(map); - actualFromMapStr = jsonObject.toString(); - assertTrue("bigDec in map (or array or bean) is a bigDec", - actualFromMapStr.equals( - "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); - // bigDec put - jsonObject = new JSONObject(); - jsonObject.put("bigDec", bigDecimal); - actualFromPutStr = jsonObject.toString(); - assertTrue("bigDec from put is a number", - actualFromPutStr.equals( - "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); - // bigInt,bigDec put - JSONArray jsonArray = new JSONArray(); - jsonArray.put(bigInteger); - jsonArray.put(bigDecimal); - actualFromPutStr = jsonArray.toString(); - assertTrue("bigInt, bigDec from put is a number", - actualFromPutStr.equals( - "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); - assertTrue("getBigInt is bigInt", jsonArray.getBigInteger(0).equals(bigInteger)); - assertTrue("getBigDec is bigDec", jsonArray.getBigDecimal(1).equals(bigDecimal)); - assertTrue("optBigInt is bigInt", jsonArray.optBigInteger(0, BigInteger.ONE).equals(bigInteger)); - assertTrue("optBigDec is bigDec", jsonArray.optBigDecimal(1, BigDecimal.ONE).equals(bigDecimal)); - jsonArray.put(Boolean.TRUE); - try { - jsonArray.getBigInteger(2); - assertTrue("should not be able to get big int", false); - } catch (Exception ignored) {} - try { - jsonArray.getBigDecimal(2); - assertTrue("should not be able to get big dec", false); - } 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)); - - // bigInt,bigDec list ctor - List list = new ArrayList(); - list.add(bigInteger); - list.add(bigDecimal); - jsonArray = new JSONArray(list); - String actualFromListStr = jsonArray.toString(); - assertTrue("bigInt, bigDec in list is a bigInt, bigDec", - actualFromListStr.equals( - "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); - // bigInt bean ctor - MyBigNumberBean myBigNumberBean = mock(MyBigNumberBean.class); - when(myBigNumberBean.getBigInteger()).thenReturn(new BigInteger("123456789012345678901234567890")); - jsonObject = new JSONObject(myBigNumberBean); - String actualFromBeanStr = jsonObject.toString(); - // can't do a full string compare because mockery adds an extra key/value - assertTrue("bigInt from bean ctor is a bigInt", - actualFromBeanStr.contains("123456789012345678901234567890")); - // bigDec bean ctor - myBigNumberBean = mock(MyBigNumberBean.class); - when(myBigNumberBean.getBigDecimal()).thenReturn(new BigDecimal("123456789012345678901234567890.12345678901234567890123456789")); - jsonObject = new JSONObject(myBigNumberBean); - actualFromBeanStr = jsonObject.toString(); - // can't do a full string compare because mockery adds an extra key/value - assertTrue("bigDec from bean ctor is a bigDec", - actualFromBeanStr.contains("123456789012345678901234567890.12345678901234567890123456789")); - // bigInt,bigDec wrap() - obj = JSONObject.wrap(bigInteger); - assertTrue("wrap() returns big num",obj.equals(bigInteger)); - obj = JSONObject.wrap(bigDecimal); - assertTrue("wrap() returns string",obj.equals(bigDecimal)); - - } - - /** - * The purpose for the static method getNames() methods are not clear. - * This method is not called from within JSON-Java. Most likely - * uses are to prep names arrays for: - * JSONObject(JSONObject jo, String[] names) - * JSONObject(Object object, String names[]), - */ - @Test - public void jsonObjectNames() { - JSONObject jsonObject; - - // getNames() from null JSONObject - assertTrue("null names from null Object", - null == JSONObject.getNames((Object)null)); - - // getNames() from object with no fields - assertTrue("null names from Object with no fields", - null == JSONObject.getNames(new MyJsonString())); - - // getNames from new JSONOjbect - jsonObject = new JSONObject(); - String [] names = JSONObject.getNames(jsonObject); - assertTrue("names should be null", names == null); - - - // getNames() from empty JSONObject - String emptyStr = "{}"; - jsonObject = new JSONObject(emptyStr); - assertTrue("empty JSONObject should have null names", - null == JSONObject.getNames(jsonObject)); - - // getNames() from JSONObject - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "}"; - jsonObject = new JSONObject(str); - names = JSONObject.getNames(jsonObject); - JSONArray jsonArray = new JSONArray(names); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - List docList = JsonPath.read(doc, "$"); - assertTrue("expected 3 items", docList.size() == 3); - assertTrue( - "expected to find trueKey", - ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); - assertTrue( - "expected to find falseKey", - ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); - assertTrue( - "expected to find stringKey", - ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); - - /** - * getNames() from an enum with properties has an interesting result. - * It returns the enum values, not the selected enum properties - */ - MyEnumField myEnumField = MyEnumField.VAL1; - names = JSONObject.getNames(myEnumField); - - // validate JSON - jsonArray = new JSONArray(names); - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - docList = JsonPath.read(doc, "$"); - assertTrue("expected 3 items", docList.size() == 3); - assertTrue( - "expected to find VAL1", - ((List) JsonPath.read(doc, "$[?(@=='VAL1')]")).size() == 1); - assertTrue( - "expected to find VAL2", - ((List) JsonPath.read(doc, "$[?(@=='VAL2')]")).size() == 1); - assertTrue( - "expected to find VAL3", - ((List) JsonPath.read(doc, "$[?(@=='VAL3')]")).size() == 1); - - /** - * A bean is also an object. But in order to test the static - * method getNames(), this particular bean needs some public - * data members. - */ - MyPublicClass myPublicClass = new MyPublicClass(); - names = JSONObject.getNames(myPublicClass); - - // validate JSON - jsonArray = new JSONArray(names); - doc = Configuration.defaultConfiguration().jsonProvider() - .parse(jsonArray.toString()); - docList = JsonPath.read(doc, "$"); - assertTrue("expected 2 items", docList.size() == 2); - assertTrue( - "expected to find publicString", - ((List) JsonPath.read(doc, "$[?(@=='publicString')]")).size() == 1); - assertTrue( - "expected to find publicInt", - ((List) JsonPath.read(doc, "$[?(@=='publicInt')]")).size() == 1); - } - - /** - * Populate a JSONArray from an empty JSONObject names() method. - * It should be empty. - */ - @Test - public void emptyJsonObjectNamesToJsonAray() { - JSONObject jsonObject = new JSONObject(); - JSONArray jsonArray = jsonObject.names(); - assertTrue("jsonArray should be null", jsonArray == null); - } - - /** - * Populate a JSONArray from a JSONObject names() method. - * Confirm that it contains the expected names. - */ - @Test - public void jsonObjectNamesToJsonAray() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"stringKey\":\"hello world!\","+ - "}"; - - JSONObject jsonObject = new JSONObject(str); - JSONArray jsonArray = jsonObject.names(); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); - assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected to find trueKey", ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); - assertTrue("expected to find falseKey", ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); - assertTrue("expected to find stringKey", ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); - } - - /** - * Exercise the JSONObject increment() method. - */ - @Test - public void jsonObjectIncrement() { - String str = - "{"+ - "\"keyLong\":9999999991,"+ - "\"keyDouble\":1.1"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.increment("keyInt"); - jsonObject.increment("keyInt"); - jsonObject.increment("keyLong"); - jsonObject.increment("keyDouble"); - 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 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); - assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/keyInt"))); - assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(jsonObject.query("/keyLong"))); - assertTrue("expected 3.1", Double.valueOf(3.1).equals(jsonObject.query("/keyDouble"))); - assertTrue("expected 123456789123456789123456789123456781", new BigInteger("123456789123456789123456789123456781").equals(jsonObject.query("/keyBigInt"))); - assertTrue("expected 123456789123456789123456789123456781.1", new BigDecimal("123456789123456789123456789123456781.1").equals(jsonObject.query("/keyBigDec"))); - - /** - * Should work the same way on any platform! @see https://docs.oracle - * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is the - * effect of a float to double conversion and is inherent to the - * shortcomings of the IEEE 754 format, when converting 32-bit into - * double-precision 64-bit. Java type-casts float to double. A 32 bit - * float is type-casted to 64 bit double by simply appending zero-bits - * to the mantissa (and extended the signed exponent by 3 bits.) and - * there is no way to obtain more information than it is stored in the - * 32-bits float. - * - * Like 1/3 cannot be represented as base10 number because it is - * periodically, 1/5 (for example) cannot be represented as base2 number - * since it is periodically in base2 (take a look at - * http://www.h-schmidt.net/FloatConverter/) The same happens to 3.1, - * that decimal number (base10 representation) is periodic in base2 - * representation, therefore appending zero-bits is inaccurate. Only - * repeating the periodically occuring bits (0110) would be a proper - * conversion. However one cannot detect from a 32 bit IEE754 - * representation which bits would "repeat infinitely", since the - * missing bits would not fit into the 32 bit float, i.e. the - * information needed simply is not there! - */ - assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(jsonObject.query("/keyFloat"))); - - /** - * float f = 3.1f; double df = (double) f; double d = 3.1d; - * System.out.println - * (Integer.toBinaryString(Float.floatToRawIntBits(f))); - * System.out.println - * (Long.toBinaryString(Double.doubleToRawLongBits(df))); - * System.out.println - * (Long.toBinaryString(Double.doubleToRawLongBits(d))); - * - * - Float: - * seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm - * 1000000010001100110011001100110 - * - Double - * seeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm - * 10000000 10001100110011001100110 - * 100000000001000110011001100110011000000000000000000000000000000 - * 100000000001000110011001100110011001100110011001100110011001101 - */ - - /** - * Examples of well documented but probably unexpected behavior in - * java / with 32-bit float to 64-bit float conversion. - */ - assertFalse("Document unexpected behaviour with explicit type-casting float as double!", (double)0.2f == 0.2d ); - assertFalse("Document unexpected behaviour with implicit type-cast!", 0.2f == 0.2d ); - Double d1 = new Double( 1.1f ); - Double d2 = new Double( "1.1f" ); - assertFalse( "Document implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) ); - - assertTrue( "Correctly converting float to double via base10 (string) representation!", new Double( 3.1d ).equals( new Double( new Float( 3.1f ).toString() ) ) ); - - // Pinpointing the not so obvious "buggy" conversion from float to double in JSONObject - JSONObject jo = new JSONObject(); - jo.put( "bug", 3.1f ); // will call put( String key, double value ) with implicit and "buggy" type-cast from float to double - assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) ); - - JSONObject inc = new JSONObject(); - inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) - assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float ); - inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double! - // this.put(key, (Float) value + 1); - // 1. The (Object)value will be typecasted to (Float)value since it is an instanceof Float actually nothing is done. - // 2. Float instance will be autoboxed into float because the + operator will work on primitives not Objects! - // 3. A float+float operation will be performed and results into a float primitive. - // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method - // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa - assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Double ); - // correct implementation (with change of behavior) would be: - // this.put(key, new Float((Float) value + 1)); - // Probably it would be better to deprecate the method and remove some day, while convenient processing the "payload" is not - // really in the the scope of a JSON-library (IMHO.) - - } - - /** - * Exercise JSONObject numberToString() method - */ - @Test - public void jsonObjectNumberToString() { - String str; - Double dVal; - Integer iVal = 1; - str = JSONObject.numberToString(iVal); - assertTrue("expected "+iVal+" actual "+str, iVal.toString().equals(str)); - dVal = 12.34; - str = JSONObject.numberToString(dVal); - assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); - dVal = 12.34e27; - str = JSONObject.numberToString(dVal); - assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); - // trailing .0 is truncated, so it doesn't quite match toString() - dVal = 5000000.0000000; - str = JSONObject.numberToString(dVal); - assertTrue("expected 5000000 actual "+str, str.equals("5000000")); - } - - /** - * Exercise JSONObject put() and similar() methods - */ - @Test - public void jsonObjectPut() { - String expectedStr = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{"+ - "\"myKey1\":\"myVal1\","+ - "\"myKey2\":\"myVal2\","+ - "\"myKey3\":\"myVal3\","+ - "\"myKey4\":\"myVal4\""+ - "}"+ - "}"; - JSONObject jsonObject = new JSONObject(); - jsonObject.put("trueKey", true); - jsonObject.put("falseKey", false); - Integer [] intArray = { 0, 1, 2 }; - jsonObject.put("arrayKey", Arrays.asList(intArray)); - Map myMap = new HashMap(); - myMap.put("myKey1", "myVal1"); - myMap.put("myKey2", "myVal2"); - myMap.put("myKey3", "myVal3"); - myMap.put("myKey4", "myVal4"); - jsonObject.put("objectKey", myMap); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); - assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); - assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); - assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); - assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0"))); - assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1"))); - assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2"))); - assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); - assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1"))); - assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2"))); - assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3"))); - assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4"))); - - jsonObject.remove("trueKey"); - JSONObject expectedJsonObject = new JSONObject(expectedStr); - assertTrue("unequal jsonObjects should not be similar", - !jsonObject.similar(expectedJsonObject)); - assertTrue("jsonObject should not be similar to jsonArray", - !jsonObject.similar(new JSONArray())); - - String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}"; - String bCompareValueStr = "{\"a\":\"notAval\",\"b\":true}"; - JSONObject aCompareValueJsonObject = new JSONObject(aCompareValueStr); - JSONObject bCompareValueJsonObject = new JSONObject(bCompareValueStr); - assertTrue("different values should not be similar", - !aCompareValueJsonObject.similar(bCompareValueJsonObject)); - - String aCompareObjectStr = "{\"a\":\"aval\",\"b\":{}}"; - String bCompareObjectStr = "{\"a\":\"aval\",\"b\":true}"; - JSONObject aCompareObjectJsonObject = new JSONObject(aCompareObjectStr); - JSONObject bCompareObjectJsonObject = new JSONObject(bCompareObjectStr); - assertTrue("different nested JSONObjects should not be similar", - !aCompareObjectJsonObject.similar(bCompareObjectJsonObject)); - - String aCompareArrayStr = "{\"a\":\"aval\",\"b\":[]}"; - String bCompareArrayStr = "{\"a\":\"aval\",\"b\":true}"; - JSONObject aCompareArrayJsonObject = new JSONObject(aCompareArrayStr); - JSONObject bCompareArrayJsonObject = new JSONObject(bCompareArrayStr); - assertTrue("different nested JSONArrays should not be similar", - !aCompareArrayJsonObject.similar(bCompareArrayJsonObject)); - } - - /** - * Exercise JSONObject toString() method - */ - @Test - public void jsonObjectToString() { - String str = - "{"+ - "\"trueKey\":true,"+ - "\"falseKey\":false,"+ - "\"arrayKey\":[0,1,2],"+ - "\"objectKey\":{"+ - "\"myKey1\":\"myVal1\","+ - "\"myKey2\":\"myVal2\","+ - "\"myKey3\":\"myVal3\","+ - "\"myKey4\":\"myVal4\""+ - "}"+ - "}"; - JSONObject jsonObject = new JSONObject(str); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); - assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); - assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); - assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); - assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0"))); - assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1"))); - assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2"))); - assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); - assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1"))); - assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2"))); - assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3"))); - assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4"))); - } - - /** - * Exercise JSONObject toString() method with various indent levels. - */ - @Test - public void jsonObjectToStringIndent() { - String jsonObject0Str = - "{"+ - "\"key1\":" + - "[1,2," + - "{\"key3\":true}" + - "],"+ - "\"key2\":" + - "{\"key1\":\"val1\",\"key2\":" + - "{\"key2\":\"val2\"}" + - "},"+ - "\"key3\":" + - "[" + - "[1,2.1]" + - "," + - "[null]" + - "]"+ - "}"; - - String jsonObject1Str = - "{\n" + - " \"key1\": [\n" + - " 1,\n" + - " 2,\n" + - " {\"key3\": true}\n" + - " ],\n" + - " \"key2\": {\n" + - " \"key1\": \"val1\",\n" + - " \"key2\": {\"key2\": \"val2\"}\n" + - " },\n" + - " \"key3\": [\n" + - " [\n" + - " 1,\n" + - " 2.1\n" + - " ],\n" + - " [null]\n" + - " ]\n" + - "}"; - String jsonObject4Str = - "{\n" + - " \"key1\": [\n" + - " 1,\n" + - " 2,\n" + - " {\"key3\": true}\n" + - " ],\n" + - " \"key2\": {\n" + - " \"key1\": \"val1\",\n" + - " \"key2\": {\"key2\": \"val2\"}\n" + - " },\n" + - " \"key3\": [\n" + - " [\n" + - " 1,\n" + - " 2.1\n" + - " ],\n" + - " [null]\n" + - " ]\n" + - "}"; - JSONObject jsonObject = new JSONObject(jsonObject0Str); - assertEquals(jsonObject0Str, jsonObject.toString()); - assertEquals(jsonObject0Str, jsonObject.toString(0)); - assertEquals(jsonObject1Str, jsonObject.toString(1)); - assertEquals(jsonObject4Str, jsonObject.toString(4)); - } - - /** - * Explores how JSONObject handles maps. Insert a string/string map - * as a value in a JSONObject. It will remain a map. Convert the - * JSONObject to string, then create a new JSONObject from the string. - * In the new JSONObject, the value will be stored as a nested JSONObject. - * Confirm that map and nested JSONObject have the same contents. - */ - @Test - public void jsonObjectToStringSuppressWarningOnCastToMap() { - JSONObject jsonObject = new JSONObject(); - Map map = new HashMap<>(); - map.put("abc", "def"); - jsonObject.put("key", map); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 1 key item", ((Map)(JsonPath.read(doc, "$.key"))).size() == 1); - assertTrue("expected def", "def".equals(jsonObject.query("/key/abc"))); - } - - /** - * Explores how JSONObject handles collections. Insert a string collection - * as a value in a JSONObject. It will remain a collection. Convert the - * JSONObject to string, then create a new JSONObject from the string. - * In the new JSONObject, the value will be stored as a nested JSONArray. - * Confirm that collection and nested JSONArray have the same contents. - */ - @Test - public void jsonObjectToStringSuppressWarningOnCastToCollection() { - JSONObject jsonObject = new JSONObject(); - Collection collection = new ArrayList(); - collection.add("abc"); - // ArrayList will be added as an object - jsonObject.put("key", collection); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); - assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected 1 key item", ((List)(JsonPath.read(doc, "$.key"))).size() == 1); - assertTrue("expected abc", "abc".equals(jsonObject.query("/key/0"))); - } - - /** - * Exercises the JSONObject.valueToString() method for various types - */ - @Test - public void valueToString() { - - assertTrue("null valueToString() incorrect", - "null".equals(JSONObject.valueToString(null))); - MyJsonString jsonString = new MyJsonString(); - assertTrue("jsonstring valueToString() incorrect", - "my string".equals(JSONObject.valueToString(jsonString))); - assertTrue("boolean valueToString() incorrect", - "true".equals(JSONObject.valueToString(Boolean.TRUE))); - assertTrue("non-numeric double", - "null".equals(JSONObject.doubleToString(Double.POSITIVE_INFINITY))); - String jsonObjectStr = - "{"+ - "\"key1\":\"val1\","+ - "\"key2\":\"val2\","+ - "\"key3\":\"val3\""+ - "}"; - JSONObject jsonObject = new JSONObject(jsonObjectStr); - assertTrue("jsonObject valueToString() incorrect", - JSONObject.valueToString(jsonObject).equals(jsonObject.toString())); - String jsonArrayStr = - "[1,2,3]"; - JSONArray jsonArray = new JSONArray(jsonArrayStr); - assertTrue("jsonArray valueToString() incorrect", - JSONObject.valueToString(jsonArray).equals(jsonArray.toString())); - Map map = new HashMap(); - map.put("key1", "val1"); - map.put("key2", "val2"); - map.put("key3", "val3"); - assertTrue("map valueToString() incorrect", - jsonObject.toString().equals(JSONObject.valueToString(map))); - Collection collection = new ArrayList(); - collection.add(new Integer(1)); - collection.add(new Integer(2)); - collection.add(new Integer(3)); - assertTrue("collection valueToString() expected: "+ - jsonArray.toString()+ " actual: "+ - JSONObject.valueToString(collection), - jsonArray.toString().equals(JSONObject.valueToString(collection))); - Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; - assertTrue("array valueToString() incorrect", - jsonArray.toString().equals(JSONObject.valueToString(array))); - } - - /** - * Confirm that https://github.com/douglascrockford/JSON-java/issues/167 is fixed. - * The following code was throwing a ClassCastException in the - * JSONObject(Map) constructor - */ - @Test - public void valueToStringConfirmException() { - Map myMap = new HashMap(); - myMap.put(1, "myValue"); - // this is the test, it should not throw an exception - String str = JSONObject.valueToString(myMap); - // confirm result, just in case - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(str); - assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); - assertTrue("expected myValue", "myValue".equals(JsonPath.read(doc, "$.1"))); - } - - /** - * Exercise the JSONObject wrap() method. Sometimes wrap() will change - * the object being wrapped, other times not. The purpose of wrap() is - * to ensure the value is packaged in a way that is compatible with how - * a JSONObject value or JSONArray value is supposed to be stored. - */ - @Test - public void wrapObject() { - // wrap(null) returns NULL - assertTrue("null wrap() incorrect", - JSONObject.NULL == JSONObject.wrap(null)); - - // wrap(Integer) returns Integer - Integer in = new Integer(1); - assertTrue("Integer wrap() incorrect", - in == JSONObject.wrap(in)); - - /** - * This test is to document the preferred behavior if BigDecimal is - * supported. Previously bd returned as a string, since it - * is recognized as being a Java package class. Now with explicit - * support for big numbers, it remains a BigDecimal - */ - Object bdWrap = JSONObject.wrap(BigDecimal.ONE); - assertTrue("BigDecimal.ONE evaluates to ONE", - bdWrap.equals(BigDecimal.ONE)); - - // wrap JSONObject returns JSONObject - String jsonObjectStr = - "{"+ - "\"key1\":\"val1\","+ - "\"key2\":\"val2\","+ - "\"key3\":\"val3\""+ - "}"; - JSONObject jsonObject = new JSONObject(jsonObjectStr); - assertTrue("JSONObject wrap() incorrect", - jsonObject == JSONObject.wrap(jsonObject)); - - // wrap collection returns JSONArray - Collection collection = new ArrayList(); - collection.add(new Integer(1)); - collection.add(new Integer(2)); - collection.add(new Integer(3)); - JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection)); - - // validate JSON - Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); - assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0"))); - assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1"))); - assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); - - // wrap Array returns JSONArray - Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; - JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); - - // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); - assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0"))); - assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1"))); - assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); - - // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString()); - assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0"))); - assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1"))); - assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); - - // wrap map returns JSONObject - Map map = new HashMap(); - map.put("key1", "val1"); - map.put("key2", "val2"); - map.put("key3", "val3"); - JSONObject mapJsonObject = (JSONObject) (JSONObject.wrap(map)); - - // validate JSON - doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString()); - assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); - assertTrue("expected val1", "val1".equals(mapJsonObject.query("/key1"))); - assertTrue("expected val2", "val2".equals(mapJsonObject.query("/key2"))); - assertTrue("expected val3", "val3".equals(mapJsonObject.query("/key3"))); - } - - - /** - * RFC 7159 defines control characters to be U+0000 through U+001F. This test verifies that the parser is checking for these in expected ways. - */ - @Test - public void jsonObjectParseControlCharacters(){ - for(int i = 0;i<=0x001f;i++){ - final String charString = String.valueOf((char)i); - final String source = "{\"key\":\""+charString+"\"}"; - try { - JSONObject jo = new JSONObject(source); - assertTrue("Expected "+charString+"("+i+") in the JSON Object but did not find it.",charString.equals(jo.getString("key"))); - } catch (JSONException ex) { - assertTrue("Only \\0 (U+0000), \\n (U+000A), and \\r (U+000D) should cause an error. Instead "+charString+"("+i+") caused an error", - i=='\0' || i=='\n' || i=='\r' - ); - } - } - } - - /** - * Explore how JSONObject handles parsing errors. - */ - @Test - public void jsonObjectParsingErrors() { - try { - // does not start with '{' - String str = "abc"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "A JSONObject text must begin with '{' at 1 [character 2 line 1]". - equals(e.getMessage())); - } - try { - // does not end with '}' - String str = "{"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "A JSONObject text must end with '}' at 2 [character 3 line 1]". - equals(e.getMessage())); - } - try { - // key with no ':' - String str = "{\"myKey\" = true}"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Expected a ':' after a key at 10 [character 11 line 1]". - equals(e.getMessage())); - } - try { - // entries with no ',' separator - String str = "{\"myKey\":true \"myOtherKey\":false}"; - new JSONObject(str); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Expected a ',' or '}' at 15 [character 16 line 1]". - equals(e.getMessage())); - } - try { - // append to wrong key - String str = "{\"myKey\":true, \"myOtherKey\":false}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.append("myKey", "hello"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[myKey] is not a JSONArray.". - equals(e.getMessage())); - } - try { - // increment wrong key - String str = "{\"myKey\":true, \"myOtherKey\":false}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.increment("myKey"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Unable to increment [\"myKey\"].". - equals(e.getMessage())); - } - try { - // invalid key - String str = "{\"myKey\":true, \"myOtherKey\":false}"; - JSONObject jsonObject = new JSONObject(str); - jsonObject.get(null); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Null key.". - equals(e.getMessage())); - } - try { - // invalid numberToString() - JSONObject.numberToString((Number)null); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Null pointer". - equals(e.getMessage())); - } - try { - // null put key - JSONObject jsonObject = new JSONObject("{}"); - jsonObject.put(null, 0); - assertTrue("Expected an exception", false); - } catch (NullPointerException ignored) { - } - try { - // multiple putOnce key - JSONObject jsonObject = new JSONObject("{}"); - jsonObject.putOnce("hello", "world"); - jsonObject.putOnce("hello", "world!"); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("", true); - } - try { - // test validity of invalid double - JSONObject.testValidity(Double.NaN); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("", true); - } - try { - // test validity of invalid float - JSONObject.testValidity(Float.NEGATIVE_INFINITY); - assertTrue("Expected an exception", false); - } catch (JSONException e) { - assertTrue("", true); - } - } - - /** - * Confirm behavior when putOnce() is called with null parameters - */ - @Test - public void jsonObjectPutOnceNull() { - JSONObject jsonObject = new JSONObject(); - jsonObject.putOnce(null, null); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); - } - - /** - * Exercise JSONObject opt(key, default) method. - */ - @Test - public void jsonObjectOptDefault() { - - String str = "{\"myKey\": \"myval\", \"hiKey\": null}"; - JSONObject jsonObject = new JSONObject(str); - - assertTrue("optBigDecimal() should return default BigDecimal", - BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); - assertTrue("optBigInteger() should return default BigInteger", - BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); - assertTrue("optBoolean() should return default boolean", - jsonObject.optBoolean("myKey", true)); - assertTrue("optInt() should return default int", - 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optEnum() should return default Enum", - MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); - assertTrue("optJSONArray() should return null ", - null==jsonObject.optJSONArray("myKey")); - assertTrue("optJSONObject() should return null ", - null==jsonObject.optJSONObject("myKey")); - assertTrue("optLong() should return default long", - 42 == jsonObject.optLong("myKey", 42)); - assertTrue("optDouble() should return default double", - 42.3 == jsonObject.optDouble("myKey", 42.3)); - assertTrue("optString() should return default string", - "hi".equals(jsonObject.optString("hiKey", "hi"))); - } - - /** - * Exercise JSONObject opt(key, default) method when the key doesn't exist. - */ - @Test - public void jsonObjectOptNoKey() { - - JSONObject jsonObject = new JSONObject(); - - assertTrue("optBigDecimal() should return default BigDecimal", - BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); - assertTrue("optBigInteger() should return default BigInteger", - BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); - assertTrue("optBoolean() should return default boolean", - jsonObject.optBoolean("myKey", true)); - assertTrue("optInt() should return default int", - 42 == jsonObject.optInt("myKey", 42)); - assertTrue("optEnum() should return default Enum", - MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); - assertTrue("optJSONArray() should return null ", - null==jsonObject.optJSONArray("myKey")); - assertTrue("optJSONObject() should return null ", - null==jsonObject.optJSONObject("myKey")); - assertTrue("optLong() should return default long", - 42 == jsonObject.optLong("myKey", 42)); - assertTrue("optDouble() should return default double", - 42.3 == jsonObject.optDouble("myKey", 42.3)); - assertTrue("optString() should return default string", - "hi".equals(jsonObject.optString("hiKey", "hi"))); - } - - /** - * Verifies that the opt methods properly convert string values. - */ - @Test - public void jsonObjectOptStringConversion() { - JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\"}"); - assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true); - assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false); - assertTrue("unexpected optInt value",jo.optInt("int",0)==123); - assertTrue("unexpected optLong value",jo.optLong("int",0)==123); - assertTrue("unexpected optDouble value",jo.optDouble("int",0.0)==123.0); - assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0); - assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); - - } - - /** - * Confirm behavior when JSONObject put(key, null object) is called - */ - @Test - public void jsonObjectputNull() { - - // put null should remove the item. - String str = "{\"myKey\": \"myval\"}"; - JSONObject jsonObjectRemove = new JSONObject(str); - jsonObjectRemove.remove("myKey"); - - JSONObject jsonObjectPutNull = new JSONObject(str); - jsonObjectPutNull.put("myKey", (Object) null); - - // validate JSON - assertTrue("jsonObject should be empty", jsonObjectRemove.length() == 0 - && jsonObjectPutNull.length() == 0); - } - - /** - * Exercise JSONObject quote() method - * This purpose of quote() is to ensure that for strings with embedded - * quotes, the quotes are properly escaped. - */ - @Test - public void jsonObjectQuote() { - String str; - str = ""; - String quotedStr; - quotedStr = JSONObject.quote(str); - assertTrue("quote() expected escaped quotes, found "+quotedStr, - "\"\"".equals(quotedStr)); - str = "\"\""; - quotedStr = JSONObject.quote(str); - assertTrue("quote() expected escaped quotes, found "+quotedStr, - "\"\\\"\\\"\"".equals(quotedStr)); - str = "null and null will be emitted as "" - */ - String sJONull = XML.toString(jsonObjectJONull); - assertTrue("JSONObject.NULL should emit a null value", - "null".equals(sJONull)); - String sNull = XML.toString(jsonObjectNull); - assertTrue("null should emit an empty string", "".equals(sNull)); - } - - @Test(expected = JSONPointerException.class) - public void queryWithNoResult() { - new JSONObject().query("/a/b"); - } - - @Test - public void optQueryWithNoResult() { - assertNull(new JSONObject().optQuery("/a/b")); - } - - @Test(expected = IllegalArgumentException.class) - public void optQueryWithSyntaxError() { - new JSONObject().optQuery("invalid"); - } - - @Test(expected = JSONException.class) - public void invalidEscapeSequence() { - String json = "{ \"\\url\": \"value\" }"; - new JSONObject(json); - } - - /** - * Exercise JSONObject toMap() method. - */ - @Test - public void toMap() { - String jsonObjectStr = - "{" + - "\"key1\":" + - "[1,2," + - "{\"key3\":true}" + - "]," + - "\"key2\":" + - "{\"key1\":\"val1\",\"key2\":" + - "{\"key2\":null}," + - "\"key3\":42" + - "}," + - "\"key3\":" + - "[" + - "[\"value1\",2.1]" + - "," + - "[null]" + - "]" + - "}"; - - JSONObject jsonObject = new JSONObject(jsonObjectStr); - Map map = jsonObject.toMap(); - - assertTrue("Map should not be null", map != null); - assertTrue("Map should have 3 elements", map.size() == 3); - - List key1List = (List)map.get("key1"); - assertTrue("key1 should not be null", key1List != null); - assertTrue("key1 list should have 3 elements", key1List.size() == 3); - assertTrue("key1 value 1 should be 1", key1List.get(0).equals(Integer.valueOf(1))); - assertTrue("key1 value 2 should be 2", key1List.get(1).equals(Integer.valueOf(2))); - - Map key1Value3Map = (Map)key1List.get(2); - assertTrue("Map should not be null", key1Value3Map != null); - assertTrue("Map should have 1 element", key1Value3Map.size() == 1); - assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE)); - - Map key2Map = (Map)map.get("key2"); - assertTrue("key2 should not be null", key2Map != null); - assertTrue("key2 map should have 3 elements", key2Map.size() == 3); - assertTrue("key2 map key 1 should be val1", key2Map.get("key1").equals("val1")); - assertTrue("key2 map key 3 should be 42", key2Map.get("key3").equals(Integer.valueOf(42))); - - Map key2Val2Map = (Map)key2Map.get("key2"); - assertTrue("key2 map key 2 should not be null", key2Val2Map != null); - assertTrue("key2 map key 2 should have an entry", key2Val2Map.containsKey("key2")); - assertTrue("key2 map key 2 value should be null", key2Val2Map.get("key2") == null); - - List key3List = (List)map.get("key3"); - assertTrue("key3 should not be null", key3List != null); - assertTrue("key3 list should have 3 elements", key3List.size() == 2); - - List key3Val1List = (List)key3List.get(0); - assertTrue("key3 list val 1 should not be null", key3Val1List != null); - assertTrue("key3 list val 1 should have 2 elements", key3Val1List.size() == 2); - assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1")); - assertTrue("key3 list val 1 list element 2 should be 2.1", key3Val1List.get(1).equals(Double.valueOf("2.1"))); - - List key3Val2List = (List)key3List.get(1); - assertTrue("key3 list val 2 should not be null", key3Val2List != null); - assertTrue("key3 list val 2 should have 1 element", key3Val2List.size() == 1); - assertTrue("key3 list val 2 list element 1 should be null", key3Val2List.get(0) == null); - - // Assert that toMap() is a deep copy - jsonObject.getJSONArray("key3").getJSONArray(0).put(0, "still value 1"); - assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1")); - - // assert that the new map is mutable - assertTrue("Removing a key should succeed", map.remove("key3") != null); - assertTrue("Map should have 2 elements", map.size() == 2); - - } -} +package org.json.junit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +import org.json.CDL; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONPointerException; +import org.json.XML; +import org.junit.Test; + +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.JsonPath; + +/** + * JSONObject, along with JSONArray, are the central classes of the reference app. + * All of the other classes interact with them, and JSON functionality would + * otherwise be impossible. + */ +public class JSONObjectTest { + + /** + * JSONObject built from a bean, but only using a null value. + * Nothing good is expected to happen. + * Expects NullPointerException + */ + @Test(expected=NullPointerException.class) + public void jsonObjectByNullBean() { + assertNull("Expected an exception",new JSONObject((MyBean)null)); + } + + /** + * The JSON parser is permissive of unambiguous unquoted keys and values. + * Such JSON text should be allowed, even if it does not strictly conform + * to the spec. However, after being parsed, toString() should emit strictly + * conforming JSON text. + */ + @Test + public void unquotedText() { + String str = "{key1:value1, key2:42}"; + JSONObject jsonObject = new JSONObject(str); + String textStr = jsonObject.toString(); + assertTrue("expected key1", textStr.contains("\"key1\"")); + assertTrue("expected value1", textStr.contains("\"value1\"")); + assertTrue("expected key2", textStr.contains("\"key2\"")); + assertTrue("expected 42", textStr.contains("42")); + } + + @Test + public void testLongFromString(){ + String str = "26315000000253009"; + JSONObject json = new JSONObject(); + json.put("key", str); + + final Object actualKey = json.opt("key"); + assert str.equals(actualKey) : "Incorrect key value. Got " + actualKey + + " expected " + str; + + final long actualLong = json.optLong("key"); + assert actualLong != 0 : "Unable to extract long value for string " + str; + assert 26315000000253009L == actualLong : "Incorrect key value. Got " + + actualLong + " expected " + str; + + final String actualString = json.optString("key"); + assert str.equals(actualString) : "Incorrect key value. Got " + + actualString + " expected " + str; + } + + /** + * A JSONObject can be created with no content + */ + @Test + public void emptyJsonObject() { + JSONObject jsonObject = new JSONObject(); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + /** + * A JSONObject can be created from another JSONObject plus a list of names. + * In this test, some of the starting JSONObject keys are not in the + * names list. + */ + @Test + public void jsonObjectByNames() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"nullKey\":null,"+ + "\"stringKey\":\"hello world!\","+ + "\"escapeStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e67"+ + "}"; + String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"}; + JSONObject jsonObject = new JSONObject(str); + + // validate JSON + JSONObject jsonObjectByName = new JSONObject(jsonObject, keys); + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObjectByName.toString()); + assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObjectByName.query("/falseKey"))); + assertTrue("expected \"nullKey\":null", JSONObject.NULL.equals(jsonObjectByName.query("/nullKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObjectByName.query("/stringKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObjectByName.query("/doubleKey"))); + } + + /** + * JSONObjects can be built from a Map. + * In this test the map is null. + * the JSONObject(JsonTokener) ctor is not tested directly since it already + * has full coverage from other tests. + */ + @Test + public void jsonObjectByNullMap() { + Map map = null; + JSONObject jsonObject = new JSONObject(map); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + /** + * JSONObjects can be built from a Map. + * In this test all of the map entries are valid JSON types. + */ + @Test + public void jsonObjectByMap() { + Map map = new HashMap(); + map.put("trueKey", new Boolean(true)); + map.put("falseKey", new Boolean(false)); + map.put("stringKey", "hello world!"); + map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); + map.put("intKey", new Long(42)); + map.put("doubleKey", new Double(-23.45e67)); + JSONObject jsonObject = new JSONObject(map); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey"))); + } + + /** + * Verifies that the constructor has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyConstructor() { + + final JSONObject expected = new JSONObject("{\"myKey\":10}"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONObject jaStrObj = new JSONObject(myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONObject jaStrInt = new JSONObject(myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONObject jaObjObj = new JSONObject(myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); + } + + /** + * Tests Number serialization. + */ + @Test + public void verifyNumberOutput(){ + /** + * MyNumberContainer is a POJO, so call JSONObject(bean), + * which builds a map of getter names/values + * The only getter is getMyNumber (key=myNumber), + * whose return value is MyNumber. MyNumber extends Number, + * but is not recognized as such by wrap() per current + * implementation, so wrap() returns the default new JSONObject(bean). + * The only getter is getNumber (key=number), whose return value is + * BigDecimal(42). + */ + JSONObject jsonObject = new JSONObject(new MyNumberContainer()); + String actual = jsonObject.toString(); + String expected = "{\"myNumber\":{\"number\":42}}"; + assertEquals("Not Equal", expected , actual); + + /** + * JSONObject.put() handles objects differently than the + * bean constructor. Where the bean ctor wraps objects before + * placing them in the map, put() inserts the object without wrapping. + * In this case, a MyNumber instance is the value. + * The MyNumber.toString() method is responsible for + * returning a reasonable value: the string '42'. + */ + jsonObject = new JSONObject(); + jsonObject.put("myNumber", new MyNumber()); + actual = jsonObject.toString(); + expected = "{\"myNumber\":42}"; + assertEquals("Not Equal", expected , actual); + + /** + * Calls the JSONObject(Map) ctor, which calls wrap() for values. + * AtomicInteger is a Number, but is not recognized by wrap(), per + * current implementation. However, the type is + * 'java.util.concurrent.atomic', so due to the 'java' prefix, + * wrap() inserts the value as a string. That is why 42 comes back + * wrapped in quotes. + */ + jsonObject = new JSONObject(Collections.singletonMap("myNumber", new AtomicInteger(42))); + actual = jsonObject.toString(); + expected = "{\"myNumber\":\"42\"}"; + assertEquals("Not Equal", expected , actual); + + /** + * JSONObject.put() inserts the AtomicInteger directly into the + * map not calling wrap(). In toString()->write()->writeValue(), + * AtomicInteger is recognized as a Number, and converted via + * numberToString() into the unquoted string '42'. + */ + jsonObject = new JSONObject(); + jsonObject.put("myNumber", new AtomicInteger(42)); + actual = jsonObject.toString(); + expected = "{\"myNumber\":42}"; + assertEquals("Not Equal", expected , actual); + + /** + * Calls the JSONObject(Map) ctor, which calls wrap() for values. + * Fraction is a Number, but is not recognized by wrap(), per + * current implementation. As a POJO, Fraction is handled as a + * bean and inserted into a contained JSONObject. It has 2 getters, + * for numerator and denominator. + */ + jsonObject = new JSONObject(Collections.singletonMap("myNumber", new Fraction(4,2))); + assertEquals(1, jsonObject.length()); + assertEquals(2, ((JSONObject)(jsonObject.get("myNumber"))).length()); + assertEquals("Numerator", BigInteger.valueOf(4) , jsonObject.query("/myNumber/numerator")); + assertEquals("Denominator", BigInteger.valueOf(2) , jsonObject.query("/myNumber/denominator")); + + /** + * JSONObject.put() inserts the Fraction directly into the + * map not calling wrap(). In toString()->write()->writeValue(), + * Fraction is recognized as a Number, and converted via + * numberToString() into the unquoted string '4/2'. But the + * BigDecimal sanity check fails, so writeValue() defaults + * to returning a safe JSON quoted string. Pretty slick! + */ + jsonObject = new JSONObject(); + jsonObject.put("myNumber", new Fraction(4,2)); + actual = jsonObject.toString(); + expected = "{\"myNumber\":\"4/2\"}"; // valid JSON, bug fixed + assertEquals("Not Equal", expected , actual); + } + + /** + * Verifies that the put Collection has backwards compatibility with RAW types pre-java5. + */ + @Test + public void verifyPutCollection() { + + final JSONObject expected = new JSONObject("{\"myCollection\":[10]}"); + + @SuppressWarnings("rawtypes") + Collection myRawC = Collections.singleton(Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(); + jaRaw.put("myCollection", myRawC); + + Collection myCObj = Collections.singleton((Object) Integer + .valueOf(10)); + JSONObject jaObj = new JSONObject(); + jaObj.put("myCollection", myCObj); + + Collection myCInt = Collections.singleton(Integer + .valueOf(10)); + JSONObject jaInt = new JSONObject(); + jaInt.put("myCollection", myCInt); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaInt)); + } + + + /** + * Verifies that the put Map has backwards compatability with RAW types pre-java5. + */ + @Test + public void verifyPutMap() { + + final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}"); + + @SuppressWarnings("rawtypes") + Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10)); + JSONObject jaRaw = new JSONObject(); + jaRaw.put("myMap", myRawC); + + Map myCStrObj = Collections.singletonMap("myKey", + (Object) Integer.valueOf(10)); + JSONObject jaStrObj = new JSONObject(); + jaStrObj.put("myMap", myCStrObj); + + Map myCStrInt = Collections.singletonMap("myKey", + Integer.valueOf(10)); + JSONObject jaStrInt = new JSONObject(); + jaStrInt.put("myMap", myCStrInt); + + Map myCObjObj = Collections.singletonMap((Object) "myKey", + (Object) Integer.valueOf(10)); + JSONObject jaObjObj = new JSONObject(); + jaObjObj.put("myMap", myCObjObj); + + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaRaw)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrObj)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaStrInt)); + assertTrue( + "The RAW Collection should give me the same as the Typed Collection", + expected.similar(jaObjObj)); + } + + + /** + * JSONObjects can be built from a Map. + * In this test the map entries are not valid JSON types. + * The actual conversion is kind of interesting. + */ + @Test + public void jsonObjectByMapWithUnsupportedValues() { + Map jsonMap = new HashMap(); + // Just insert some random objects + jsonMap.put("key1", new CDL()); + jsonMap.put("key2", new Exception()); + + JSONObject jsonObject = new JSONObject(jsonMap); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected 0 key1 items", ((Map)(JsonPath.read(doc, "$.key1"))).size() == 0); + assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(jsonObject.query("/key2"))); + } + + /** + * JSONObjects can be built from a Map. + * In this test one of the map values is null + */ + @Test + public void jsonObjectByMapWithNullValue() { + Map map = new HashMap(); + map.put("trueKey", new Boolean(true)); + map.put("falseKey", new Boolean(false)); + map.put("stringKey", "hello world!"); + map.put("nullKey", null); + map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); + map.put("intKey", new Long(42)); + map.put("doubleKey", new Double(-23.45e67)); + JSONObject jsonObject = new JSONObject(map); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); + assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); + assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey"))); + assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); + assertTrue("expected \"intKey\":42", Long.valueOf("42").equals(jsonObject.query("/intKey"))); + assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey"))); + } + + /** + * JSONObject built from a bean. In this case all but one of the + * bean getters return valid JSON types + */ + @SuppressWarnings("boxing") + @Test + public void jsonObjectByBean() { + /** + * Default access classes have to be mocked since JSONObject, which is + * not in the same package, cannot call MyBean methods by reflection. + */ + MyBean myBean = mock(MyBean.class); + when(myBean.getDoubleKey()).thenReturn(-23.45e7); + when(myBean.getIntKey()).thenReturn(42); + when(myBean.getStringKey()).thenReturn("hello world!"); + when(myBean.getEscapeStringKey()).thenReturn("h\be\tllo w\u1234orld!"); + when(myBean.isTrueKey()).thenReturn(true); + when(myBean.isFalseKey()).thenReturn(false); + when(myBean.getStringReaderKey()).thenReturn( + new StringReader("") { + }); + + JSONObject jsonObject = new JSONObject(myBean); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 8 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 8); + assertTrue("expected 0 items in stringReaderKey", ((Map) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); + assertTrue("expected hello world!","hello world!".equals(jsonObject.query("/stringKey"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); + assertTrue("expected 42", Integer.valueOf("42").equals(jsonObject.query("/intKey"))); + assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(jsonObject.query("/doubleKey"))); + // sorry, mockito artifact + assertTrue("expected 2 callbacks items", ((List)(JsonPath.read(doc, "$.callbacks"))).size() == 2); + assertTrue("expected 0 handler items", ((Map)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0); + assertTrue("expected 0 callbacks[1] items", ((Map)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0); + } + + /** + * A bean is also an object. But in order to test the JSONObject + * ctor that takes an object and a list of names, + * this particular bean needs some public + * data members, which have been added to the class. + */ + @Test + public void jsonObjectByObjectAndNames() { + String[] keys = {"publicString", "publicInt"}; + // just need a class that has public data members + MyPublicClass myPublicClass = new MyPublicClass(); + JSONObject jsonObject = new JSONObject(myPublicClass, keys); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected \"publicString\":\"abc\"", "abc".equals(jsonObject.query("/publicString"))); + assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(jsonObject.query("/publicInt"))); + } + + /** + * Exercise the JSONObject from resource bundle functionality. + * The test resource bundle is uncomplicated, but provides adequate test coverage. + */ + @Test + public void jsonObjectByResourceBundle() { + JSONObject jsonObject = new + JSONObject("org.json.junit.StringsResourceBundle", + Locale.getDefault()); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 2 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 2); + assertTrue("expected 2 greetings items", ((Map)(JsonPath.read(doc, "$.greetings"))).size() == 2); + assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(jsonObject.query("/greetings/hello"))); + assertTrue("expected \"world\":\"World!\"", "World!".equals(jsonObject.query("/greetings/world"))); + assertTrue("expected 2 farewells items", ((Map)(JsonPath.read(doc, "$.farewells"))).size() == 2); + assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later"))); + assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator"))); + } + + /** + * Exercise the JSONObject.accumulate() method + */ + @SuppressWarnings("boxing") + @Test + public void jsonObjectAccumulate() { + + JSONObject jsonObject = new JSONObject(); + jsonObject.accumulate("myArray", true); + jsonObject.accumulate("myArray", false); + jsonObject.accumulate("myArray", "hello world!"); + 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()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1"))); + assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3"))); + assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4"))); + assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5"))); + } + + /** + * Exercise the JSONObject append() functionality + */ + @SuppressWarnings("boxing") + @Test + public void jsonObjectAppend() { + 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()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1/"))); + assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2"))); + assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3"))); + assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4"))); + assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5"))); + } + + /** + * Exercise the JSONObject doubleToString() method + */ + @SuppressWarnings("boxing") + @Test + public void jsonObjectDoubleToString() { + String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" }; + Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, + Double.NaN, Double.NEGATIVE_INFINITY }; + for (int i = 0; i < expectedStrs.length; ++i) { + String actualStr = JSONObject.doubleToString(doubles[i]); + assertTrue("value expected ["+expectedStrs[i]+ + "] found ["+actualStr+ "]", + expectedStrs[i].equals(actualStr)); + } + } + + /** + * Exercise some JSONObject get[type] and opt[type] methods + */ + @Test + public void jsonObjectValues() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"trueStrKey\":\"true\","+ + "\"falseStrKey\":\"false\","+ + "\"stringKey\":\"hello world!\","+ + "\"intKey\":42,"+ + "\"intStrKey\":\"43\","+ + "\"longKey\":1234567890123456789,"+ + "\"longStrKey\":\"987654321098765432\","+ + "\"doubleKey\":-23.45e7,"+ + "\"doubleStrKey\":\"00001.000\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{\"myKey\":\"myVal\"}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); + assertTrue("opt trueKey should be true", jsonObject.optBoolean("trueKey")); + assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey")); + assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey")); + assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey")); + assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey")); + assertTrue("stringKey should be string", + jsonObject.getString("stringKey").equals("hello world!")); + assertTrue("doubleKey should be double", + jsonObject.getDouble("doubleKey") == -23.45e7); + assertTrue("doubleStrKey should be double", + jsonObject.getDouble("doubleStrKey") == 1); + 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("intKey should be int", + jsonObject.optInt("intKey") == 42); + assertTrue("opt intKey should be int", + jsonObject.optInt("intKey", 0) == 42); + assertTrue("opt intKey with default should be int", + jsonObject.getInt("intKey") == 42); + assertTrue("intStrKey should be int", + jsonObject.getInt("intStrKey") == 43); + assertTrue("longKey should be long", + jsonObject.getLong("longKey") == 1234567890123456789L); + assertTrue("opt longKey should be long", + jsonObject.optLong("longKey") == 1234567890123456789L); + assertTrue("opt longKey with default should be long", + jsonObject.optLong("longKey", 0) == 1234567890123456789L); + assertTrue("longStrKey should be long", + jsonObject.getLong("longStrKey") == 987654321098765432L); + assertTrue("xKey should not exist", + jsonObject.isNull("xKey")); + assertTrue("stringKey should exist", + jsonObject.has("stringKey")); + assertTrue("opt stringKey should string", + jsonObject.optString("stringKey").equals("hello world!")); + assertTrue("opt stringKey with default should string", + jsonObject.optString("stringKey", "not found").equals("hello world!")); + JSONArray jsonArray = jsonObject.getJSONArray("arrayKey"); + assertTrue("arrayKey should be JSONArray", + jsonArray.getInt(0) == 0 && + jsonArray.getInt(1) == 1 && + jsonArray.getInt(2) == 2); + jsonArray = jsonObject.optJSONArray("arrayKey"); + assertTrue("opt arrayKey should be JSONArray", + jsonArray.getInt(0) == 0 && + jsonArray.getInt(1) == 1 && + jsonArray.getInt(2) == 2); + JSONObject jsonObjectInner = jsonObject.getJSONObject("objectKey"); + assertTrue("objectKey should be JSONObject", + jsonObjectInner.get("myKey").equals("myVal")); + } + + /** + * Check whether JSONObject handles large or high precision numbers correctly + */ + @Test + public void stringToValueNumbersTest() { + assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0") instanceof Double); + assertTrue("-0.0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double); + assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String); + assertTrue( "0.2 should be a Double!", + JSONObject.stringToValue( "0.2" ) instanceof Double ); + assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!", + JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double ); + /** + * This test documents a need for BigDecimal conversion. + */ + Object obj = JSONObject.stringToValue( "299792.457999999984" ); + assertTrue( "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!", + obj.equals(new Double(299792.458)) ); + assertTrue( "1 should be an Integer!", + JSONObject.stringToValue( "1" ) instanceof Integer ); + assertTrue( "Integer.MAX_VALUE should still be an Integer!", + JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer ); + assertTrue( "Large integers should be a Long!", + JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long ); + assertTrue( "Long.MAX_VALUE should still be an Integer!", + JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long ); + + String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString(); + assertTrue( "Really large integers currently evaluate to string", + JSONObject.stringToValue(str).equals("9223372036854775808")); + } + + /** + * This test documents numeric values which could be numerically + * handled as BigDecimal or BigInteger. It helps determine what outputs + * will change if those types are supported. + */ + @Test + public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() { + // Valid JSON Numbers, probably should return BigDecimal or BigInteger objects + String str = + "{"+ + "\"numberWithDecimals\":299792.457999999984,"+ + "\"largeNumber\":12345678901234567890,"+ + "\"preciseNumber\":0.2000000000000000111,"+ + "\"largeExponent\":-23.45e2327"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + // Comes back as a double, but loses precision + assertTrue( "numberWithDecimals currently evaluates to double 299792.458", + jsonObject.get( "numberWithDecimals" ).equals( new Double( "299792.458" ) ) ); + Object obj = jsonObject.get( "largeNumber" ); + assertTrue("largeNumber currently evaluates to string", + "12345678901234567890".equals(obj)); + // comes back as a double but loses precision + assertTrue( "preciseNumber currently evaluates to double 0.2", + jsonObject.get( "preciseNumber" ).equals(new Double(0.2))); + obj = jsonObject.get( "largeExponent" ); + assertTrue("largeExponent should currently evaluates as a string", + "-23.45e2327".equals(obj)); + } + + /** + * This test documents how JSON-Java handles invalid numeric input. + */ + @Test + public void jsonInvalidNumberValues() { + // Number-notations supported by Java and invalid as JSON + String str = + "{"+ + "\"hexNumber\":-0x123,"+ + "\"tooManyZeros\":00,"+ + "\"negativeInfinite\":-Infinity,"+ + "\"negativeNaN\":-NaN,"+ + "\"negativeFraction\":-.01,"+ + "\"tooManyZerosFraction\":00.001,"+ + "\"negativeHexFloat\":-0x1.fffp1,"+ + "\"hexFloat\":0x1.0P-1074,"+ + "\"floatIdentifier\":0.1f,"+ + "\"doubleIdentifier\":0.1d"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + Object obj; + obj = jsonObject.get( "hexNumber" ); + assertFalse( "hexNumber must not be a number (should throw exception!?)", + obj instanceof Number ); + assertTrue("hexNumber currently evaluates to string", + obj.equals("-0x123")); + assertTrue( "tooManyZeros currently evaluates to string", + jsonObject.get( "tooManyZeros" ).equals("00")); + obj = jsonObject.get("negativeInfinite"); + assertTrue( "negativeInfinite currently evaluates to string", + obj.equals("-Infinity")); + obj = jsonObject.get("negativeNaN"); + assertTrue( "negativeNaN currently evaluates to string", + obj.equals("-NaN")); + assertTrue( "negativeFraction currently evaluates to double -0.01", + jsonObject.get( "negativeFraction" ).equals(new Double(-0.01))); + assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", + jsonObject.get( "tooManyZerosFraction" ).equals(new Double(0.001))); + assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", + jsonObject.get( "negativeHexFloat" ).equals(new Double(-3.99951171875))); + assertTrue("hexFloat currently evaluates to double 4.9E-324", + jsonObject.get("hexFloat").equals(new Double(4.9E-324))); + assertTrue("floatIdentifier currently evaluates to double 0.1", + jsonObject.get("floatIdentifier").equals(new Double(0.1))); + assertTrue("doubleIdentifier currently evaluates to double 0.1", + jsonObject.get("doubleIdentifier").equals(new Double(0.1))); + } + + /** + * Tests how JSONObject get[type] handles incorrect types + */ + @Test + public void jsonObjectNonAndWrongValues() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"trueStrKey\":\"true\","+ + "\"falseStrKey\":\"false\","+ + "\"stringKey\":\"hello world!\","+ + "\"intKey\":42,"+ + "\"intStrKey\":\"43\","+ + "\"longKey\":1234567890123456789,"+ + "\"longStrKey\":\"987654321098765432\","+ + "\"doubleKey\":-23.45e7,"+ + "\"doubleStrKey\":\"00001.000\","+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{\"myKey\":\"myVal\"}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + try { + jsonObject.getBoolean("nonKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("expecting an exception message", + "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + } + try { + jsonObject.getBoolean("stringKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a Boolean.". + equals(e.getMessage())); + } + try { + jsonObject.getString("nonKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } + try { + jsonObject.getString("trueKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"trueKey\"] not a string.". + equals(e.getMessage())); + } + try { + jsonObject.getDouble("nonKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } + try { + jsonObject.getDouble("stringKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.". + equals(e.getMessage())); + } + try { + jsonObject.getInt("nonKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } + try { + jsonObject.getInt("stringKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not an int.". + equals(e.getMessage())); + } + try { + jsonObject.getLong("nonKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } + try { + jsonObject.getLong("stringKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a long.". + equals(e.getMessage())); + } + try { + jsonObject.getJSONArray("nonKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } + try { + jsonObject.getJSONArray("stringKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONArray.". + equals(e.getMessage())); + } + try { + jsonObject.getJSONObject("nonKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.". + equals(e.getMessage())); + } + try { + jsonObject.getJSONObject("stringKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONObject.". + equals(e.getMessage())); + } + } + + /** + * This test documents an unexpected numeric behavior. + * A double that ends with .0 is parsed, serialized, then + * parsed again. On the second parse, it has become an int. + */ + @Test + public void unexpectedDoubleToIntConversion() { + String key30 = "key30"; + String key31 = "key31"; + JSONObject jsonObject = new JSONObject(); + jsonObject.put(key30, new Double(3.0)); + jsonObject.put(key31, new Double(3.1)); + + assertTrue("3.0 should remain a double", + jsonObject.getDouble(key30) == 3); + assertTrue("3.1 should remain a double", + jsonObject.getDouble(key31) == 3.1); + + // turns 3.0 into 3. + String serializedString = jsonObject.toString(); + JSONObject deserialized = new JSONObject(serializedString); + assertTrue("3.0 is now an int", deserialized.get(key30) instanceof Integer); + assertTrue("3.0 can still be interpreted as a double", + deserialized.getDouble(key30) == 3.0); + assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1); + } + + /** + * Document behaviors of big numbers. Includes both JSONObject + * and JSONArray tests + */ + @SuppressWarnings("boxing") + @Test + public void bigNumberOperations() { + /** + * JSONObject tries to parse BigInteger as a bean, but it only has + * one getter, getLowestBitSet(). The value is lost and an unhelpful + * value is stored. This should be fixed. + */ + BigInteger bigInteger = new BigInteger("123456789012345678901234567890"); + JSONObject jsonObject = new JSONObject(bigInteger); + Object obj = jsonObject.get("lowestSetBit"); + assertTrue("JSONObject only has 1 value", jsonObject.length() == 1); + assertTrue("JSONObject parses BigInteger as the Integer lowestBitSet", + obj instanceof Integer); + assertTrue("this bigInteger lowestBitSet happens to be 1", + obj.equals(1)); + + /** + * JSONObject tries to parse BigDecimal as a bean, but it has + * no getters, The value is lost and no value is stored. + * This should be fixed. + */ + BigDecimal bigDecimal = new BigDecimal( + "123456789012345678901234567890.12345678901234567890123456789"); + jsonObject = new JSONObject(bigDecimal); + assertTrue("large bigDecimal is not stored", jsonObject.length() == 0); + + /** + * JSONObject put(String, Object) method stores and serializes + * bigInt and bigDec correctly. Nothing needs to change. + */ + 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 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 serializes bigDec correctly", + jsonObject.toString().equals( + "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + + /** + * 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. + */ + String str = JSONObject.numberToString(bigInteger); + assertTrue("numberToString() handles bigInteger correctly", + str.equals("123456789012345678901234567890")); + str = JSONObject.numberToString(bigDecimal); + assertTrue("numberToString() handles bigDecimal correctly", + str.equals("123456789012345678901234567890.12345678901234567890123456789")); + + /** + * JSONObject.stringToValue() turns bigInt into an accurate string, + * and rounds bigDec. This incorrect, but users may have come to + * expect this behavior. Change would be marginally better, but + * might inconvenience users. + */ + obj = JSONObject.stringToValue(bigInteger.toString()); + assertTrue("stringToValue() turns bigInteger string into string", + obj instanceof String); + obj = JSONObject.stringToValue(bigDecimal.toString()); + assertTrue("stringToValue() changes bigDecimal string", + !obj.toString().equals(bigDecimal.toString())); + + /** + * wrap() vs put() big number behavior is now the same. + */ + // bigInt map ctor + Map map = new HashMap(); + map.put("bigInt", bigInteger); + jsonObject = new JSONObject(map); + String actualFromMapStr = jsonObject.toString(); + assertTrue("bigInt in map (or array or bean) is a string", + actualFromMapStr.equals( + "{\"bigInt\":123456789012345678901234567890}")); + // bigInt put + jsonObject = new JSONObject(); + jsonObject.put("bigInt", bigInteger); + String actualFromPutStr = jsonObject.toString(); + assertTrue("bigInt from put is a number", + actualFromPutStr.equals( + "{\"bigInt\":123456789012345678901234567890}")); + // bigDec map ctor + map = new HashMap(); + map.put("bigDec", bigDecimal); + jsonObject = new JSONObject(map); + actualFromMapStr = jsonObject.toString(); + assertTrue("bigDec in map (or array or bean) is a bigDec", + actualFromMapStr.equals( + "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + // bigDec put + jsonObject = new JSONObject(); + jsonObject.put("bigDec", bigDecimal); + actualFromPutStr = jsonObject.toString(); + assertTrue("bigDec from put is a number", + actualFromPutStr.equals( + "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}")); + // bigInt,bigDec put + JSONArray jsonArray = new JSONArray(); + jsonArray.put(bigInteger); + jsonArray.put(bigDecimal); + actualFromPutStr = jsonArray.toString(); + assertTrue("bigInt, bigDec from put is a number", + actualFromPutStr.equals( + "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); + assertTrue("getBigInt is bigInt", jsonArray.getBigInteger(0).equals(bigInteger)); + assertTrue("getBigDec is bigDec", jsonArray.getBigDecimal(1).equals(bigDecimal)); + assertTrue("optBigInt is bigInt", jsonArray.optBigInteger(0, BigInteger.ONE).equals(bigInteger)); + assertTrue("optBigDec is bigDec", jsonArray.optBigDecimal(1, BigDecimal.ONE).equals(bigDecimal)); + jsonArray.put(Boolean.TRUE); + try { + jsonArray.getBigInteger(2); + assertTrue("should not be able to get big int", false); + } catch (Exception ignored) {} + try { + jsonArray.getBigDecimal(2); + assertTrue("should not be able to get big dec", false); + } 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)); + + // bigInt,bigDec list ctor + List list = new ArrayList(); + list.add(bigInteger); + list.add(bigDecimal); + jsonArray = new JSONArray(list); + String actualFromListStr = jsonArray.toString(); + assertTrue("bigInt, bigDec in list is a bigInt, bigDec", + actualFromListStr.equals( + "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]")); + // bigInt bean ctor + MyBigNumberBean myBigNumberBean = mock(MyBigNumberBean.class); + when(myBigNumberBean.getBigInteger()).thenReturn(new BigInteger("123456789012345678901234567890")); + jsonObject = new JSONObject(myBigNumberBean); + String actualFromBeanStr = jsonObject.toString(); + // can't do a full string compare because mockery adds an extra key/value + assertTrue("bigInt from bean ctor is a bigInt", + actualFromBeanStr.contains("123456789012345678901234567890")); + // bigDec bean ctor + myBigNumberBean = mock(MyBigNumberBean.class); + when(myBigNumberBean.getBigDecimal()).thenReturn(new BigDecimal("123456789012345678901234567890.12345678901234567890123456789")); + jsonObject = new JSONObject(myBigNumberBean); + actualFromBeanStr = jsonObject.toString(); + // can't do a full string compare because mockery adds an extra key/value + assertTrue("bigDec from bean ctor is a bigDec", + actualFromBeanStr.contains("123456789012345678901234567890.12345678901234567890123456789")); + // bigInt,bigDec wrap() + obj = JSONObject.wrap(bigInteger); + assertTrue("wrap() returns big num",obj.equals(bigInteger)); + obj = JSONObject.wrap(bigDecimal); + assertTrue("wrap() returns string",obj.equals(bigDecimal)); + + } + + /** + * The purpose for the static method getNames() methods are not clear. + * This method is not called from within JSON-Java. Most likely + * uses are to prep names arrays for: + * JSONObject(JSONObject jo, String[] names) + * JSONObject(Object object, String names[]), + */ + @Test + public void jsonObjectNames() { + JSONObject jsonObject; + + // getNames() from null JSONObject + assertTrue("null names from null Object", + null == JSONObject.getNames((Object)null)); + + // getNames() from object with no fields + assertTrue("null names from Object with no fields", + null == JSONObject.getNames(new MyJsonString())); + + // getNames from new JSONOjbect + jsonObject = new JSONObject(); + String [] names = JSONObject.getNames(jsonObject); + assertTrue("names should be null", names == null); + + + // getNames() from empty JSONObject + String emptyStr = "{}"; + jsonObject = new JSONObject(emptyStr); + assertTrue("empty JSONObject should have null names", + null == JSONObject.getNames(jsonObject)); + + // getNames() from JSONObject + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "}"; + jsonObject = new JSONObject(str); + names = JSONObject.getNames(jsonObject); + JSONArray jsonArray = new JSONArray(names); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + List docList = JsonPath.read(doc, "$"); + assertTrue("expected 3 items", docList.size() == 3); + assertTrue( + "expected to find trueKey", + ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); + assertTrue( + "expected to find falseKey", + ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); + assertTrue( + "expected to find stringKey", + ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); + + /** + * getNames() from an enum with properties has an interesting result. + * It returns the enum values, not the selected enum properties + */ + MyEnumField myEnumField = MyEnumField.VAL1; + names = JSONObject.getNames(myEnumField); + + // validate JSON + jsonArray = new JSONArray(names); + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + docList = JsonPath.read(doc, "$"); + assertTrue("expected 3 items", docList.size() == 3); + assertTrue( + "expected to find VAL1", + ((List) JsonPath.read(doc, "$[?(@=='VAL1')]")).size() == 1); + assertTrue( + "expected to find VAL2", + ((List) JsonPath.read(doc, "$[?(@=='VAL2')]")).size() == 1); + assertTrue( + "expected to find VAL3", + ((List) JsonPath.read(doc, "$[?(@=='VAL3')]")).size() == 1); + + /** + * A bean is also an object. But in order to test the static + * method getNames(), this particular bean needs some public + * data members. + */ + MyPublicClass myPublicClass = new MyPublicClass(); + names = JSONObject.getNames(myPublicClass); + + // validate JSON + jsonArray = new JSONArray(names); + doc = Configuration.defaultConfiguration().jsonProvider() + .parse(jsonArray.toString()); + docList = JsonPath.read(doc, "$"); + assertTrue("expected 2 items", docList.size() == 2); + assertTrue( + "expected to find publicString", + ((List) JsonPath.read(doc, "$[?(@=='publicString')]")).size() == 1); + assertTrue( + "expected to find publicInt", + ((List) JsonPath.read(doc, "$[?(@=='publicInt')]")).size() == 1); + } + + /** + * Populate a JSONArray from an empty JSONObject names() method. + * It should be empty. + */ + @Test + public void emptyJsonObjectNamesToJsonAray() { + JSONObject jsonObject = new JSONObject(); + JSONArray jsonArray = jsonObject.names(); + assertTrue("jsonArray should be null", jsonArray == null); + } + + /** + * Populate a JSONArray from a JSONObject names() method. + * Confirm that it contains the expected names. + */ + @Test + public void jsonObjectNamesToJsonAray() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "}"; + + JSONObject jsonObject = new JSONObject(str); + JSONArray jsonArray = jsonObject.names(); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected to find trueKey", ((List) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1); + assertTrue("expected to find falseKey", ((List) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1); + assertTrue("expected to find stringKey", ((List) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1); + } + + /** + * Exercise the JSONObject increment() method. + */ + @SuppressWarnings("cast") + @Test + public void jsonObjectIncrement() { + String str = + "{"+ + "\"keyLong\":9999999991,"+ + "\"keyDouble\":1.1"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.increment("keyInt"); + jsonObject.increment("keyInt"); + jsonObject.increment("keyLong"); + jsonObject.increment("keyDouble"); + 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 6 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 6); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/keyInt"))); + assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(jsonObject.query("/keyLong"))); + assertTrue("expected 3.1", Double.valueOf(3.1).equals(jsonObject.query("/keyDouble"))); + assertTrue("expected 123456789123456789123456789123456781", new BigInteger("123456789123456789123456789123456781").equals(jsonObject.query("/keyBigInt"))); + assertTrue("expected 123456789123456789123456789123456781.1", new BigDecimal("123456789123456789123456789123456781.1").equals(jsonObject.query("/keyBigDec"))); + + /** + * Should work the same way on any platform! @see https://docs.oracle + * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is the + * effect of a float to double conversion and is inherent to the + * shortcomings of the IEEE 754 format, when converting 32-bit into + * double-precision 64-bit. Java type-casts float to double. A 32 bit + * float is type-casted to 64 bit double by simply appending zero-bits + * to the mantissa (and extended the signed exponent by 3 bits.) and + * there is no way to obtain more information than it is stored in the + * 32-bits float. + * + * Like 1/3 cannot be represented as base10 number because it is + * periodically, 1/5 (for example) cannot be represented as base2 number + * since it is periodically in base2 (take a look at + * http://www.h-schmidt.net/FloatConverter/) The same happens to 3.1, + * that decimal number (base10 representation) is periodic in base2 + * representation, therefore appending zero-bits is inaccurate. Only + * repeating the periodically occurring bits (0110) would be a proper + * conversion. However one cannot detect from a 32 bit IEE754 + * representation which bits would "repeat infinitely", since the + * missing bits would not fit into the 32 bit float, i.e. the + * information needed simply is not there! + */ + assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(jsonObject.query("/keyFloat"))); + + /** + * float f = 3.1f; double df = (double) f; double d = 3.1d; + * System.out.println + * (Integer.toBinaryString(Float.floatToRawIntBits(f))); + * System.out.println + * (Long.toBinaryString(Double.doubleToRawLongBits(df))); + * System.out.println + * (Long.toBinaryString(Double.doubleToRawLongBits(d))); + * + * - Float: + * seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm + * 1000000010001100110011001100110 + * - Double + * seeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm + * 10000000 10001100110011001100110 + * 100000000001000110011001100110011000000000000000000000000000000 + * 100000000001000110011001100110011001100110011001100110011001101 + */ + + /** + * Examples of well documented but probably unexpected behavior in + * java / with 32-bit float to 64-bit float conversion. + */ + assertFalse("Document unexpected behaviour with explicit type-casting float as double!", (double)0.2f == 0.2d ); + assertFalse("Document unexpected behaviour with implicit type-cast!", 0.2f == 0.2d ); + Double d1 = new Double( 1.1f ); + Double d2 = new Double( "1.1f" ); + assertFalse( "Document implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) ); + + assertTrue( "Correctly converting float to double via base10 (string) representation!", new Double( 3.1d ).equals( new Double( new Float( 3.1f ).toString() ) ) ); + + // Pinpointing the not so obvious "buggy" conversion from float to double in JSONObject + JSONObject jo = new JSONObject(); + jo.put( "bug", 3.1f ); // will call put( String key, double value ) with implicit and "buggy" type-cast from float to double + assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) ); + + JSONObject inc = new JSONObject(); + inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) + assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float ); + inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double! + // this.put(key, (Float) value + 1); + // 1. The (Object)value will be typecasted to (Float)value since it is an instanceof Float actually nothing is done. + // 2. Float instance will be autoboxed into float because the + operator will work on primitives not Objects! + // 3. A float+float operation will be performed and results into a float primitive. + // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method + // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa + assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Double ); + // correct implementation (with change of behavior) would be: + // this.put(key, new Float((Float) value + 1)); + // Probably it would be better to deprecate the method and remove some day, while convenient processing the "payload" is not + // really in the the scope of a JSON-library (IMHO.) + + } + + /** + * Exercise JSONObject numberToString() method + */ + @SuppressWarnings("boxing") + @Test + public void jsonObjectNumberToString() { + String str; + Double dVal; + Integer iVal = 1; + str = JSONObject.numberToString(iVal); + assertTrue("expected "+iVal+" actual "+str, iVal.toString().equals(str)); + dVal = 12.34; + str = JSONObject.numberToString(dVal); + assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + dVal = 12.34e27; + str = JSONObject.numberToString(dVal); + assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str)); + // trailing .0 is truncated, so it doesn't quite match toString() + dVal = 5000000.0000000; + str = JSONObject.numberToString(dVal); + assertTrue("expected 5000000 actual "+str, str.equals("5000000")); + } + + /** + * Exercise JSONObject put() and similar() methods + */ + @SuppressWarnings("boxing") + @Test + public void jsonObjectPut() { + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(); + jsonObject.put("trueKey", true); + jsonObject.put("falseKey", false); + Integer [] intArray = { 0, 1, 2 }; + jsonObject.put("arrayKey", Arrays.asList(intArray)); + Map myMap = new HashMap(); + myMap.put("myKey1", "myVal1"); + myMap.put("myKey2", "myVal2"); + myMap.put("myKey3", "myVal3"); + myMap.put("myKey4", "myVal4"); + jsonObject.put("objectKey", myMap); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); + assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); + assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2"))); + assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); + assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1"))); + assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2"))); + assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3"))); + assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4"))); + + jsonObject.remove("trueKey"); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + assertTrue("unequal jsonObjects should not be similar", + !jsonObject.similar(expectedJsonObject)); + assertTrue("jsonObject should not be similar to jsonArray", + !jsonObject.similar(new JSONArray())); + + String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}"; + String bCompareValueStr = "{\"a\":\"notAval\",\"b\":true}"; + JSONObject aCompareValueJsonObject = new JSONObject(aCompareValueStr); + JSONObject bCompareValueJsonObject = new JSONObject(bCompareValueStr); + assertTrue("different values should not be similar", + !aCompareValueJsonObject.similar(bCompareValueJsonObject)); + + String aCompareObjectStr = "{\"a\":\"aval\",\"b\":{}}"; + String bCompareObjectStr = "{\"a\":\"aval\",\"b\":true}"; + JSONObject aCompareObjectJsonObject = new JSONObject(aCompareObjectStr); + JSONObject bCompareObjectJsonObject = new JSONObject(bCompareObjectStr); + assertTrue("different nested JSONObjects should not be similar", + !aCompareObjectJsonObject.similar(bCompareObjectJsonObject)); + + String aCompareArrayStr = "{\"a\":\"aval\",\"b\":[]}"; + String bCompareArrayStr = "{\"a\":\"aval\",\"b\":true}"; + JSONObject aCompareArrayJsonObject = new JSONObject(aCompareArrayStr); + JSONObject bCompareArrayJsonObject = new JSONObject(bCompareArrayStr); + assertTrue("different nested JSONArrays should not be similar", + !aCompareArrayJsonObject.similar(bCompareArrayJsonObject)); + } + + /** + * Exercise JSONObject toString() method + */ + @Test + public void jsonObjectToString() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"arrayKey\":[0,1,2],"+ + "\"objectKey\":{"+ + "\"myKey1\":\"myVal1\","+ + "\"myKey2\":\"myVal2\","+ + "\"myKey3\":\"myVal3\","+ + "\"myKey4\":\"myVal4\""+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 4 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 4); + assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); + assertTrue("expected 3 arrayKey items", ((List)(JsonPath.read(doc, "$.arrayKey"))).size() == 3); + assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0"))); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2"))); + assertTrue("expected 4 objectKey items", ((Map)(JsonPath.read(doc, "$.objectKey"))).size() == 4); + assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1"))); + assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2"))); + assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3"))); + assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4"))); + } + + /** + * Exercise JSONObject toString() method with various indent levels. + */ + @Test + public void jsonObjectToStringIndent() { + String jsonObject0Str = + "{"+ + "\"key1\":" + + "[1,2," + + "{\"key3\":true}" + + "],"+ + "\"key2\":" + + "{\"key1\":\"val1\",\"key2\":" + + "{\"key2\":\"val2\"}" + + "},"+ + "\"key3\":" + + "[" + + "[1,2.1]" + + "," + + "[null]" + + "]"+ + "}"; + + String jsonObject1Str = + "{\n" + + " \"key1\": [\n" + + " 1,\n" + + " 2,\n" + + " {\"key3\": true}\n" + + " ],\n" + + " \"key2\": {\n" + + " \"key1\": \"val1\",\n" + + " \"key2\": {\"key2\": \"val2\"}\n" + + " },\n" + + " \"key3\": [\n" + + " [\n" + + " 1,\n" + + " 2.1\n" + + " ],\n" + + " [null]\n" + + " ]\n" + + "}"; + String jsonObject4Str = + "{\n" + + " \"key1\": [\n" + + " 1,\n" + + " 2,\n" + + " {\"key3\": true}\n" + + " ],\n" + + " \"key2\": {\n" + + " \"key1\": \"val1\",\n" + + " \"key2\": {\"key2\": \"val2\"}\n" + + " },\n" + + " \"key3\": [\n" + + " [\n" + + " 1,\n" + + " 2.1\n" + + " ],\n" + + " [null]\n" + + " ]\n" + + "}"; + JSONObject jsonObject = new JSONObject(jsonObject0Str); + assertEquals(jsonObject0Str, jsonObject.toString()); + assertEquals(jsonObject0Str, jsonObject.toString(0)); + assertEquals(jsonObject1Str, jsonObject.toString(1)); + assertEquals(jsonObject4Str, jsonObject.toString(4)); + } + + /** + * Explores how JSONObject handles maps. Insert a string/string map + * as a value in a JSONObject. It will remain a map. Convert the + * JSONObject to string, then create a new JSONObject from the string. + * In the new JSONObject, the value will be stored as a nested JSONObject. + * Confirm that map and nested JSONObject have the same contents. + */ + @Test + public void jsonObjectToStringSuppressWarningOnCastToMap() { + JSONObject jsonObject = new JSONObject(); + Map map = new HashMap<>(); + map.put("abc", "def"); + jsonObject.put("key", map); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 1 key item", ((Map)(JsonPath.read(doc, "$.key"))).size() == 1); + assertTrue("expected def", "def".equals(jsonObject.query("/key/abc"))); + } + + /** + * Explores how JSONObject handles collections. Insert a string collection + * as a value in a JSONObject. It will remain a collection. Convert the + * JSONObject to string, then create a new JSONObject from the string. + * In the new JSONObject, the value will be stored as a nested JSONArray. + * Confirm that collection and nested JSONArray have the same contents. + */ + @Test + public void jsonObjectToStringSuppressWarningOnCastToCollection() { + JSONObject jsonObject = new JSONObject(); + Collection collection = new ArrayList(); + collection.add("abc"); + // ArrayList will be added as an object + jsonObject.put("key", collection); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected 1 key item", ((List)(JsonPath.read(doc, "$.key"))).size() == 1); + assertTrue("expected abc", "abc".equals(jsonObject.query("/key/0"))); + } + + /** + * Exercises the JSONObject.valueToString() method for various types + */ + @Test + public void valueToString() { + + assertTrue("null valueToString() incorrect", + "null".equals(JSONObject.valueToString(null))); + MyJsonString jsonString = new MyJsonString(); + assertTrue("jsonstring valueToString() incorrect", + "my string".equals(JSONObject.valueToString(jsonString))); + assertTrue("boolean valueToString() incorrect", + "true".equals(JSONObject.valueToString(Boolean.TRUE))); + assertTrue("non-numeric double", + "null".equals(JSONObject.doubleToString(Double.POSITIVE_INFINITY))); + String jsonObjectStr = + "{"+ + "\"key1\":\"val1\","+ + "\"key2\":\"val2\","+ + "\"key3\":\"val3\""+ + "}"; + JSONObject jsonObject = new JSONObject(jsonObjectStr); + assertTrue("jsonObject valueToString() incorrect", + JSONObject.valueToString(jsonObject).equals(jsonObject.toString())); + String jsonArrayStr = + "[1,2,3]"; + JSONArray jsonArray = new JSONArray(jsonArrayStr); + assertTrue("jsonArray valueToString() incorrect", + JSONObject.valueToString(jsonArray).equals(jsonArray.toString())); + Map map = new HashMap(); + map.put("key1", "val1"); + map.put("key2", "val2"); + map.put("key3", "val3"); + assertTrue("map valueToString() incorrect", + jsonObject.toString().equals(JSONObject.valueToString(map))); + Collection collection = new ArrayList(); + collection.add(new Integer(1)); + collection.add(new Integer(2)); + collection.add(new Integer(3)); + assertTrue("collection valueToString() expected: "+ + jsonArray.toString()+ " actual: "+ + JSONObject.valueToString(collection), + jsonArray.toString().equals(JSONObject.valueToString(collection))); + Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + assertTrue("array valueToString() incorrect", + jsonArray.toString().equals(JSONObject.valueToString(array))); + } + + /** + * Confirm that https://github.com/douglascrockford/JSON-java/issues/167 is fixed. + * The following code was throwing a ClassCastException in the + * JSONObject(Map) constructor + */ + @SuppressWarnings("boxing") + @Test + public void valueToStringConfirmException() { + Map myMap = new HashMap(); + myMap.put(1, "myValue"); + // this is the test, it should not throw an exception + String str = JSONObject.valueToString(myMap); + // confirm result, just in case + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(str); + assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); + assertTrue("expected myValue", "myValue".equals(JsonPath.read(doc, "$.1"))); + } + + /** + * Exercise the JSONObject wrap() method. Sometimes wrap() will change + * the object being wrapped, other times not. The purpose of wrap() is + * to ensure the value is packaged in a way that is compatible with how + * a JSONObject value or JSONArray value is supposed to be stored. + */ + @Test + public void wrapObject() { + // wrap(null) returns NULL + assertTrue("null wrap() incorrect", + JSONObject.NULL == JSONObject.wrap(null)); + + // wrap(Integer) returns Integer + Integer in = new Integer(1); + assertTrue("Integer wrap() incorrect", + in == JSONObject.wrap(in)); + + /** + * This test is to document the preferred behavior if BigDecimal is + * supported. Previously bd returned as a string, since it + * is recognized as being a Java package class. Now with explicit + * support for big numbers, it remains a BigDecimal + */ + Object bdWrap = JSONObject.wrap(BigDecimal.ONE); + assertTrue("BigDecimal.ONE evaluates to ONE", + bdWrap.equals(BigDecimal.ONE)); + + // wrap JSONObject returns JSONObject + String jsonObjectStr = + "{"+ + "\"key1\":\"val1\","+ + "\"key2\":\"val2\","+ + "\"key3\":\"val3\""+ + "}"; + JSONObject jsonObject = new JSONObject(jsonObjectStr); + assertTrue("JSONObject wrap() incorrect", + jsonObject == JSONObject.wrap(jsonObject)); + + // wrap collection returns JSONArray + Collection collection = new ArrayList(); + collection.add(new Integer(1)); + collection.add(new Integer(2)); + collection.add(new Integer(3)); + JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection)); + + // validate JSON + Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); + + // wrap Array returns JSONArray + Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); + + // validate JSON + doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); + + // validate JSON + doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString()); + assertTrue("expected 3 top level items", ((List)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0"))); + assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1"))); + assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); + + // wrap map returns JSONObject + Map map = new HashMap(); + map.put("key1", "val1"); + map.put("key2", "val2"); + map.put("key3", "val3"); + JSONObject mapJsonObject = (JSONObject) (JSONObject.wrap(map)); + + // validate JSON + doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString()); + assertTrue("expected 3 top level items", ((Map)(JsonPath.read(doc, "$"))).size() == 3); + assertTrue("expected val1", "val1".equals(mapJsonObject.query("/key1"))); + assertTrue("expected val2", "val2".equals(mapJsonObject.query("/key2"))); + assertTrue("expected val3", "val3".equals(mapJsonObject.query("/key3"))); + } + + + /** + * RFC 7159 defines control characters to be U+0000 through U+001F. This test verifies that the parser is checking for these in expected ways. + */ + @Test + public void jsonObjectParseControlCharacters(){ + for(int i = 0;i<=0x001f;i++){ + final String charString = String.valueOf((char)i); + final String source = "{\"key\":\""+charString+"\"}"; + try { + JSONObject jo = new JSONObject(source); + assertTrue("Expected "+charString+"("+i+") in the JSON Object but did not find it.",charString.equals(jo.getString("key"))); + } catch (JSONException ex) { + assertTrue("Only \\0 (U+0000), \\n (U+000A), and \\r (U+000D) should cause an error. Instead "+charString+"("+i+") caused an error", + i=='\0' || i=='\n' || i=='\r' + ); + } + } + } + + /** + * Explore how JSONObject handles parsing errors. + */ + @SuppressWarnings("boxing") + @Test + public void jsonObjectParsingErrors() { + try { + // does not start with '{' + String str = "abc"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "A JSONObject text must begin with '{' at 1 [character 2 line 1]". + equals(e.getMessage())); + } + try { + // does not end with '}' + String str = "{"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "A JSONObject text must end with '}' at 2 [character 3 line 1]". + equals(e.getMessage())); + } + try { + // key with no ':' + String str = "{\"myKey\" = true}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Expected a ':' after a key at 10 [character 11 line 1]". + equals(e.getMessage())); + } + try { + // entries with no ',' separator + String str = "{\"myKey\":true \"myOtherKey\":false}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Expected a ',' or '}' at 15 [character 16 line 1]". + equals(e.getMessage())); + } + try { + // append to wrong key + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.append("myKey", "hello"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "JSONObject[myKey] is not a JSONArray.". + equals(e.getMessage())); + } + try { + // increment wrong key + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.increment("myKey"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Unable to increment [\"myKey\"].". + equals(e.getMessage())); + } + try { + // invalid key + String str = "{\"myKey\":true, \"myOtherKey\":false}"; + JSONObject jsonObject = new JSONObject(str); + jsonObject.get(null); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Null key.". + equals(e.getMessage())); + } + try { + // invalid numberToString() + JSONObject.numberToString((Number)null); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("Expecting an exception message", + "Null pointer". + equals(e.getMessage())); + } + try { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0); + assertTrue("Expected an exception", false); + } catch (NullPointerException ignored) { + } + try { + // multiple putOnce key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.putOnce("hello", "world"); + jsonObject.putOnce("hello", "world!"); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("", true); + } + try { + // test validity of invalid double + JSONObject.testValidity(Double.NaN); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("", true); + } + try { + // test validity of invalid float + JSONObject.testValidity(Float.NEGATIVE_INFINITY); + assertTrue("Expected an exception", false); + } catch (JSONException e) { + assertTrue("", true); + } + } + + /** + * Confirm behavior when putOnce() is called with null parameters + */ + @Test + public void jsonObjectPutOnceNull() { + JSONObject jsonObject = new JSONObject(); + jsonObject.putOnce(null, null); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + /** + * Exercise JSONObject opt(key, default) method. + */ + @Test + public void jsonObjectOptDefault() { + + String str = "{\"myKey\": \"myval\", \"hiKey\": null}"; + JSONObject jsonObject = new JSONObject(str); + + assertTrue("optBigDecimal() should return default BigDecimal", + BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); + assertTrue("optBigInteger() should return default BigInteger", + BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); + assertTrue("optBoolean() should return default boolean", + jsonObject.optBoolean("myKey", true)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optEnum() should return default Enum", + MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); + assertTrue("optJSONArray() should return null ", + null==jsonObject.optJSONArray("myKey")); + assertTrue("optJSONObject() should return null ", + null==jsonObject.optJSONObject("myKey")); + assertTrue("optLong() should return default long", + 42 == jsonObject.optLong("myKey", 42)); + assertTrue("optDouble() should return default double", + 42.3 == jsonObject.optDouble("myKey", 42.3)); + assertTrue("optString() should return default string", + "hi".equals(jsonObject.optString("hiKey", "hi"))); + } + + /** + * Exercise JSONObject opt(key, default) method when the key doesn't exist. + */ + @Test + public void jsonObjectOptNoKey() { + + JSONObject jsonObject = new JSONObject(); + + assertTrue("optBigDecimal() should return default BigDecimal", + BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0); + assertTrue("optBigInteger() should return default BigInteger", + BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); + assertTrue("optBoolean() should return default boolean", + jsonObject.optBoolean("myKey", true)); + assertTrue("optInt() should return default int", + 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optEnum() should return default Enum", + MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); + assertTrue("optJSONArray() should return null ", + null==jsonObject.optJSONArray("myKey")); + assertTrue("optJSONObject() should return null ", + null==jsonObject.optJSONObject("myKey")); + assertTrue("optLong() should return default long", + 42 == jsonObject.optLong("myKey", 42)); + assertTrue("optDouble() should return default double", + 42.3 == jsonObject.optDouble("myKey", 42.3)); + assertTrue("optString() should return default string", + "hi".equals(jsonObject.optString("hiKey", "hi"))); + } + + /** + * Verifies that the opt methods properly convert string values. + */ + @Test + public void jsonObjectOptStringConversion() { + JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\"}"); + assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true); + assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false); + assertTrue("unexpected optInt value",jo.optInt("int",0)==123); + assertTrue("unexpected optLong value",jo.optLong("int",0)==123); + assertTrue("unexpected optDouble value",jo.optDouble("int",0.0)==123.0); + assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0); + assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); + + } + + /** + * Confirm behavior when JSONObject put(key, null object) is called + */ + @Test + public void jsonObjectputNull() { + + // put null should remove the item. + String str = "{\"myKey\": \"myval\"}"; + JSONObject jsonObjectRemove = new JSONObject(str); + jsonObjectRemove.remove("myKey"); + + JSONObject jsonObjectPutNull = new JSONObject(str); + jsonObjectPutNull.put("myKey", (Object) null); + + // validate JSON + assertTrue("jsonObject should be empty", jsonObjectRemove.length() == 0 + && jsonObjectPutNull.length() == 0); + } + + /** + * Exercise JSONObject quote() method + * This purpose of quote() is to ensure that for strings with embedded + * quotes, the quotes are properly escaped. + */ + @Test + public void jsonObjectQuote() { + String str; + str = ""; + String quotedStr; + quotedStr = JSONObject.quote(str); + assertTrue("quote() expected escaped quotes, found "+quotedStr, + "\"\"".equals(quotedStr)); + str = "\"\""; + quotedStr = JSONObject.quote(str); + assertTrue("quote() expected escaped quotes, found "+quotedStr, + "\"\\\"\\\"\"".equals(quotedStr)); + str = "null and null will be emitted as "" + */ + String sJONull = XML.toString(jsonObjectJONull); + assertTrue("JSONObject.NULL should emit a null value", + "null".equals(sJONull)); + String sNull = XML.toString(jsonObjectNull); + assertTrue("null should emit an empty string", "".equals(sNull)); + } + + @Test(expected = JSONPointerException.class) + public void queryWithNoResult() { + new JSONObject().query("/a/b"); + } + + @Test + public void optQueryWithNoResult() { + assertNull(new JSONObject().optQuery("/a/b")); + } + + @Test(expected = IllegalArgumentException.class) + public void optQueryWithSyntaxError() { + new JSONObject().optQuery("invalid"); + } + + @Test(expected = JSONException.class) + public void invalidEscapeSequence() { + String json = "{ \"\\url\": \"value\" }"; + assertNull("Expected an exception",new JSONObject(json)); + } + + /** + * Exercise JSONObject toMap() method. + */ + @Test + public void toMap() { + String jsonObjectStr = + "{" + + "\"key1\":" + + "[1,2," + + "{\"key3\":true}" + + "]," + + "\"key2\":" + + "{\"key1\":\"val1\",\"key2\":" + + "{\"key2\":null}," + + "\"key3\":42" + + "}," + + "\"key3\":" + + "[" + + "[\"value1\",2.1]" + + "," + + "[null]" + + "]" + + "}"; + + JSONObject jsonObject = new JSONObject(jsonObjectStr); + Map map = jsonObject.toMap(); + + assertTrue("Map should not be null", map != null); + assertTrue("Map should have 3 elements", map.size() == 3); + + List key1List = (List)map.get("key1"); + assertTrue("key1 should not be null", key1List != null); + assertTrue("key1 list should have 3 elements", key1List.size() == 3); + assertTrue("key1 value 1 should be 1", key1List.get(0).equals(Integer.valueOf(1))); + assertTrue("key1 value 2 should be 2", key1List.get(1).equals(Integer.valueOf(2))); + + Map key1Value3Map = (Map)key1List.get(2); + assertTrue("Map should not be null", key1Value3Map != null); + assertTrue("Map should have 1 element", key1Value3Map.size() == 1); + assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE)); + + Map key2Map = (Map)map.get("key2"); + assertTrue("key2 should not be null", key2Map != null); + assertTrue("key2 map should have 3 elements", key2Map.size() == 3); + assertTrue("key2 map key 1 should be val1", key2Map.get("key1").equals("val1")); + assertTrue("key2 map key 3 should be 42", key2Map.get("key3").equals(Integer.valueOf(42))); + + Map key2Val2Map = (Map)key2Map.get("key2"); + assertTrue("key2 map key 2 should not be null", key2Val2Map != null); + assertTrue("key2 map key 2 should have an entry", key2Val2Map.containsKey("key2")); + assertTrue("key2 map key 2 value should be null", key2Val2Map.get("key2") == null); + + List key3List = (List)map.get("key3"); + assertTrue("key3 should not be null", key3List != null); + assertTrue("key3 list should have 3 elements", key3List.size() == 2); + + List key3Val1List = (List)key3List.get(0); + assertTrue("key3 list val 1 should not be null", key3Val1List != null); + assertTrue("key3 list val 1 should have 2 elements", key3Val1List.size() == 2); + assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1")); + assertTrue("key3 list val 1 list element 2 should be 2.1", key3Val1List.get(1).equals(Double.valueOf("2.1"))); + + List key3Val2List = (List)key3List.get(1); + assertTrue("key3 list val 2 should not be null", key3Val2List != null); + assertTrue("key3 list val 2 should have 1 element", key3Val2List.size() == 1); + assertTrue("key3 list val 2 list element 1 should be null", key3Val2List.get(0) == null); + + // Assert that toMap() is a deep copy + jsonObject.getJSONArray("key3").getJSONArray(0).put(0, "still value 1"); + assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1")); + + // assert that the new map is mutable + assertTrue("Removing a key should succeed", map.remove("key3") != null); + assertTrue("Map should have 2 elements", map.size() == 2); + + } +} diff --git a/src/test/java/org/json/junit/JSONStringTest.java b/src/test/java/org/json/junit/JSONStringTest.java index cba5d09c4..617b9e38b 100644 --- a/src/test/java/org/json/junit/JSONStringTest.java +++ b/src/test/java/org/json/junit/JSONStringTest.java @@ -1,310 +1,371 @@ -package org.json.junit; - -import static org.junit.Assert.*; - -import java.io.StringWriter; -import java.util.*; - -import org.json.*; -import org.junit.Test; - -/** - * Tests for JSONString implementations, and the difference between - * {@link JSONObject#valueToString} and {@link JSONObject#writeValue}. - */ -public class JSONStringTest { - - /** - * This tests the JSONObject.writeValue() method. We can't test directly - * due to it being a package-protected method. Instead, we can call - * JSONArray.write(), which delegates the writing of each entry to - * writeValue(). - */ - @Test - public void writeValues() throws Exception { - JSONArray jsonArray = new JSONArray(); - jsonArray.put((Object)null); - - StringWriter writer = new StringWriter(); - String output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[null]".equals(output)); - - jsonArray = new JSONArray(); - jsonArray.put(JSONObject.NULL); - writer = new StringWriter(); - output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[null]".equals(output)); - - jsonArray = new JSONArray(); - jsonArray.put(new JSONObject()); - writer = new StringWriter(); - output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[{}]".equals(output)); - - jsonArray = new JSONArray(); - jsonArray.put(new JSONArray()); - writer = new StringWriter(); - output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[[]]".equals(output)); - - jsonArray = new JSONArray(); - Map singleMap = Collections.singletonMap("key1", "value1"); - jsonArray.put((Object)singleMap); - writer = new StringWriter(); - output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output)); - - jsonArray = new JSONArray(); - List singleList = Collections.singletonList("entry1"); - jsonArray.put((Object)singleList); - writer = new StringWriter(); - output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output)); - - jsonArray = new JSONArray(); - int[] intArray = new int[] { 1, 2, 3 }; - jsonArray.put(intArray); - writer = new StringWriter(); - output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[[1,2,3]]".equals(output)); - - jsonArray = new JSONArray(); - jsonArray.put(24); - writer = new StringWriter(); - output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[24]".equals(output)); - - jsonArray = new JSONArray(); - jsonArray.put("string value"); - writer = new StringWriter(); - output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[\"string value\"]".equals(output)); - - jsonArray = new JSONArray(); - jsonArray.put(true); - writer = new StringWriter(); - output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[true]".equals(output)); - - } - - /** - * This tests the JSONObject.valueToString() method. These should be - * identical to the values above, except for the enclosing [ and ]. - */ - @Test - public void valuesToString() throws Exception { - - String output = JSONObject.valueToString(null); - assertTrue("String values should be equal", "null".equals(output)); - - output = JSONObject.valueToString(JSONObject.NULL); - assertTrue("String values should be equal", "null".equals(output)); - - output = JSONObject.valueToString(new JSONObject()); - assertTrue("String values should be equal", "{}".equals(output)); - - output = JSONObject.valueToString(new JSONArray()); - assertTrue("String values should be equal", "[]".equals(output)); - - Map singleMap = Collections.singletonMap("key1", "value1"); - output = JSONObject.valueToString(singleMap); - assertTrue("String values should be equal", "{\"key1\":\"value1\"}".equals(output)); - - List singleList = Collections.singletonList("entry1"); - output = JSONObject.valueToString(singleList); - assertTrue("String values should be equal", "[\"entry1\"]".equals(output)); - - int[] intArray = new int[] { 1, 2, 3 }; - output = JSONObject.valueToString(intArray); - assertTrue("String values should be equal", "[1,2,3]".equals(output)); - - output = JSONObject.valueToString(24); - assertTrue("String values should be equal", "24".equals(output)); - - output = JSONObject.valueToString("string value"); - assertTrue("String values should be equal", "\"string value\"".equals(output)); - - output = JSONObject.valueToString(true); - assertTrue("String values should be equal", "true".equals(output)); - - } - - /** - * Test what happens when toJSONString() returns a well-formed JSON value. - * This is the usual case. - */ - @Test - public void testJSONStringValue() throws Exception { - JSONStringValue jsonString = new JSONStringValue(); - JSONArray jsonArray = new JSONArray(); - - jsonArray.put(jsonString); - - StringWriter writer = new StringWriter(); - String output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output)); - - output = JSONObject.valueToString(jsonString); - assertTrue("String values should be equal", "\"the JSON string value\"".equals(output)); - } - - /** - * Test what happens when toJSONString() returns null. In one case, - * use the object's toString() method. In the other, throw a JSONException. - */ - @Test - public void testJSONNullStringValue() throws Exception { - JSONNullStringValue jsonString = new JSONNullStringValue(); - JSONArray jsonArray = new JSONArray(); - - jsonArray.put(jsonString); - - StringWriter writer = new StringWriter(); - String output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[\"the toString value\"]".equals(output)); - - // The only different between writeValue() and valueToString(): - // in this case, valueToString throws a JSONException - try { - output = JSONObject.valueToString(jsonString); - fail("Expected an exception, got a String value"); - } catch (Exception e) { - assertTrue("Expected JSONException", e instanceof JSONException); - assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage())); - } - } - - /** - * Test what happens when toJSONString() returns an exception. In both - * cases, a JSONException is thrown, with the cause and message set from - * the original exception. - */ - @Test - public void testJSONStringExceptionValue() throws Exception { - JSONStringExceptionValue jsonString = new JSONStringExceptionValue(); - JSONArray jsonArray = new JSONArray(); - - jsonArray.put(jsonString); - - StringWriter writer = new StringWriter(); - String output = null; - try { - output = jsonArray.write(writer).toString(); - fail("Expected an exception, got a String value"); - } catch (Exception e) { - assertTrue("Expected JSONException", e instanceof JSONException); - assertTrue("Exception message does not match", "the exception value".equals(e.getMessage())); - } - - try { - output = JSONObject.valueToString(jsonString); - fail("Expected an exception, got a String value"); - } catch (Exception e) { - assertTrue("Expected JSONException", e instanceof JSONException); - assertTrue("Exception message does not match", "the exception value".equals(e.getMessage())); - } - } - - /** - * Test what happens when a Java object's toString() returns a String value. - * This is the usual case. - */ - @Test - public void testStringValue() throws Exception { - StringValue nonJsonString = new StringValue(); - JSONArray jsonArray = new JSONArray(); - - jsonArray.put(nonJsonString); - - StringWriter writer = new StringWriter(); - String output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output)); - - output = JSONObject.valueToString(nonJsonString); - assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output)); - } - - /** - * Test what happens when a Java object's toString() returns null. - * Defaults to empty string. - */ - @Test - public void testNullStringValue() throws Exception { - NullStringValue nonJsonString = new NullStringValue(); - JSONArray jsonArray = new JSONArray(); - - jsonArray.put(nonJsonString); - - StringWriter writer = new StringWriter(); - String output = jsonArray.write(writer).toString(); - assertTrue("String values should be equal", "[\"\"]".equals(output)); - - output = JSONObject.valueToString(nonJsonString); - assertTrue("String values should be equal", "\"\"".equals(output)); - } - - /** - * A JSONString that returns a valid JSON string value. - */ - private static final class JSONStringValue implements JSONString { - - @Override - public String toJSONString() { - return "\"the JSON string value\""; - } - - @Override - public String toString() { - return "the toString value for JSONStringValue"; - } - } - - /** - * A JSONString that returns null when calling toJSONString(). - */ - private static final class JSONNullStringValue implements JSONString { - - @Override - public String toJSONString() { - return null; - } - - @Override - public String toString() { - return "the toString value"; - } - } - - /** - * A JSONString that throw an exception when calling toJSONString(). - */ - private static final class JSONStringExceptionValue implements JSONString { - - @Override - public String toJSONString() { - throw new IllegalStateException("the exception value"); - } - - @Override - public String toString() { - return "the toString value for JSONStringExceptionValue"; - } - } - - public static final class StringValue { - - @Override - public String toString() { - return "the toString value for StringValue"; - } - } - - public static final class NullStringValue { - - @Override - public String toString() { - return null; - } - } -} +package org.json.junit; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.*; + +import org.json.*; +import org.junit.Test; + +/** + * Tests for JSONString implementations, and the difference between + * {@link JSONObject#valueToString} and {@link JSONObject#writeValue}. + */ +public class JSONStringTest { + + /** + * This tests the JSONObject.writeValue() method. We can't test directly + * due to it being a package-protected method. Instead, we can call + * JSONArray.write(), which delegates the writing of each entry to + * writeValue(). + */ + @Test + public void writeValues() throws Exception { + JSONArray jsonArray = new JSONArray(); + jsonArray.put((Object)null); + + StringWriter writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[null]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put(JSONObject.NULL); + } finally { + writer.close(); + } + writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[null]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put(new JSONObject()); + } finally { + writer.close(); + } + writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[{}]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put(new JSONArray()); + } finally { + writer.close(); + } + writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[[]]".equals(output)); + + jsonArray = new JSONArray(); + Map singleMap = Collections.singletonMap("key1", "value1"); + jsonArray.put((Object)singleMap); + } finally { + writer.close(); + } + writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output)); + + jsonArray = new JSONArray(); + List singleList = Collections.singletonList("entry1"); + jsonArray.put((Object)singleList); + } finally { + writer.close(); + } + writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output)); + + jsonArray = new JSONArray(); + int[] intArray = new int[] { 1, 2, 3 }; + jsonArray.put(intArray); + } finally { + writer.close(); + } + writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[[1,2,3]]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put(24); + } finally { + writer.close(); + } + writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[24]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put("string value"); + } finally { + writer.close(); + } + writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[\"string value\"]".equals(output)); + + jsonArray = new JSONArray(); + jsonArray.put(true); + } finally { + writer.close(); + } + writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[true]".equals(output)); + } finally { + writer.close(); + } + + } + + /** + * This tests the JSONObject.valueToString() method. These should be + * identical to the values above, except for the enclosing [ and ]. + */ + @SuppressWarnings("boxing") + @Test + public void valuesToString() throws Exception { + + String output = JSONObject.valueToString(null); + assertTrue("String values should be equal", "null".equals(output)); + + output = JSONObject.valueToString(JSONObject.NULL); + assertTrue("String values should be equal", "null".equals(output)); + + output = JSONObject.valueToString(new JSONObject()); + assertTrue("String values should be equal", "{}".equals(output)); + + output = JSONObject.valueToString(new JSONArray()); + assertTrue("String values should be equal", "[]".equals(output)); + + Map singleMap = Collections.singletonMap("key1", "value1"); + output = JSONObject.valueToString(singleMap); + assertTrue("String values should be equal", "{\"key1\":\"value1\"}".equals(output)); + + List singleList = Collections.singletonList("entry1"); + output = JSONObject.valueToString(singleList); + assertTrue("String values should be equal", "[\"entry1\"]".equals(output)); + + int[] intArray = new int[] { 1, 2, 3 }; + output = JSONObject.valueToString(intArray); + assertTrue("String values should be equal", "[1,2,3]".equals(output)); + + output = JSONObject.valueToString(24); + assertTrue("String values should be equal", "24".equals(output)); + + output = JSONObject.valueToString("string value"); + assertTrue("String values should be equal", "\"string value\"".equals(output)); + + output = JSONObject.valueToString(true); + assertTrue("String values should be equal", "true".equals(output)); + + } + + /** + * Test what happens when toJSONString() returns a well-formed JSON value. + * This is the usual case. + */ + @Test + public void testJSONStringValue() throws Exception { + JSONStringValue jsonString = new JSONStringValue(); + JSONArray jsonArray = new JSONArray(); + + jsonArray.put(jsonString); + + StringWriter writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output)); + + output = JSONObject.valueToString(jsonString); + assertTrue("String values should be equal", "\"the JSON string value\"".equals(output)); + } finally { + writer.close(); + } + } + + /** + * Test what happens when toJSONString() returns null. In one case, + * use the object's toString() method. In the other, throw a JSONException. + */ + @Test + public void testJSONNullStringValue() throws Exception { + JSONNullStringValue jsonString = new JSONNullStringValue(); + JSONArray jsonArray = new JSONArray(); + + jsonArray.put(jsonString); + + StringWriter writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[\"the toString value\"]".equals(output)); + + // The only different between writeValue() and valueToString(): + // in this case, valueToString throws a JSONException + try { + output = JSONObject.valueToString(jsonString); + fail("Expected an exception, got a String value"); + } catch (Exception e) { + assertTrue("Expected JSONException", e instanceof JSONException); + assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage())); + } + } finally { + writer.close(); + } + } + + /** + * Test what happens when toJSONString() returns an exception. In both + * cases, a JSONException is thrown, with the cause and message set from + * the original exception. + */ + @Test + public void testJSONStringExceptionValue() throws IOException { + JSONStringExceptionValue jsonString = new JSONStringExceptionValue(); + JSONArray jsonArray = new JSONArray(); + + jsonArray.put(jsonString); + + StringWriter writer = new StringWriter(); + try { + 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())); + } catch(Exception e) { + fail("Expected JSONException"); + } finally { + writer.close(); + } + + try { + JSONObject.valueToString(jsonString); + fail("Expected an exception, got a String value"); + } catch (JSONException e) { + assertTrue("Exception message does not match", "the exception value".equals(e.getMessage())); + } catch(Exception e) { + fail("Expected JSONException"); + } + } + + /** + * Test what happens when a Java object's toString() returns a String value. + * This is the usual case. + */ + @Test + public void testStringValue() throws Exception { + StringValue nonJsonString = new StringValue(); + JSONArray jsonArray = new JSONArray(); + + jsonArray.put(nonJsonString); + + StringWriter writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output)); + + output = JSONObject.valueToString(nonJsonString); + assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output)); + } finally { + writer.close(); + } + } + + /** + * Test what happens when a Java object's toString() returns null. + * Defaults to empty string. + */ + @Test + public void testNullStringValue() throws Exception { + NullStringValue nonJsonString = new NullStringValue(); + JSONArray jsonArray = new JSONArray(); + + jsonArray.put(nonJsonString); + + StringWriter writer = new StringWriter(); + try { + String output = jsonArray.write(writer).toString(); + assertTrue("String values should be equal", "[\"\"]".equals(output)); + + output = JSONObject.valueToString(nonJsonString); + assertTrue("String values should be equal", "\"\"".equals(output)); + } finally { + writer.close(); + } + } + + /** + * A JSONString that returns a valid JSON string value. + */ + private static final class JSONStringValue implements JSONString { + + @Override + public String toJSONString() { + return "\"the JSON string value\""; + } + + @Override + public String toString() { + return "the toString value for JSONStringValue"; + } + } + + /** + * A JSONString that returns null when calling toJSONString(). + */ + private static final class JSONNullStringValue implements JSONString { + + @Override + public String toJSONString() { + return null; + } + + @Override + public String toString() { + return "the toString value"; + } + } + + /** + * A JSONString that throw an exception when calling toJSONString(). + */ + private static final class JSONStringExceptionValue implements JSONString { + + @Override + public String toJSONString() { + throw new IllegalStateException("the exception value"); + } + + @Override + public String toString() { + return "the toString value for JSONStringExceptionValue"; + } + } + + public static final class StringValue { + + @Override + public String toString() { + return "the toString value for StringValue"; + } + } + + public static final class NullStringValue { + + @Override + public String toString() { + return null; + } + } +} diff --git a/src/test/java/org/json/junit/MyEnumField.java b/src/test/java/org/json/junit/MyEnumField.java index 8f2c63311..f0833ef6c 100644 --- a/src/test/java/org/json/junit/MyEnumField.java +++ b/src/test/java/org/json/junit/MyEnumField.java @@ -3,6 +3,7 @@ /** * An enum that contains getters and some internal fields */ +@SuppressWarnings("boxing") public enum MyEnumField { VAL1(1, "val 1"), VAL2(2, "val 2"), @@ -15,12 +16,13 @@ private MyEnumField(Integer intVal, String value) { this.intVal = intVal; } public String getValue() { - return value; + return this.value; } public Integer getIntVal() { - return intVal; + return this.intVal; } + @Override public String toString(){ - return value; + return this.value; } } diff --git a/src/test/java/org/json/junit/MyPublicClass.java b/src/test/java/org/json/junit/MyPublicClass.java index 1f55e3e3e..e483d4c19 100644 --- a/src/test/java/org/json/junit/MyPublicClass.java +++ b/src/test/java/org/json/junit/MyPublicClass.java @@ -3,6 +3,7 @@ /** * Need a class with some public data members for testing */ +@SuppressWarnings("boxing") public class MyPublicClass { public Integer publicInt = 42; public String publicString = "abc"; diff --git a/src/test/java/org/json/junit/StringsResourceBundle.java b/src/test/java/org/json/junit/StringsResourceBundle.java index 83d932239..d04aeaf85 100644 --- a/src/test/java/org/json/junit/StringsResourceBundle.java +++ b/src/test/java/org/json/junit/StringsResourceBundle.java @@ -6,6 +6,7 @@ * A resource bundle class */ public class StringsResourceBundle extends ListResourceBundle { + @Override public Object[][] getContents() { return contents; } diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 2f3fea752..dd827bdb0 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -4,11 +4,8 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; -import java.io.IOException; - import org.json.JSONArray; import org.json.JSONException; -import org.json.JSONML; import org.json.JSONObject; import org.json.XML; import org.junit.Rule; From 95da4246a279d19db770f27dbb8e3d7eb5befdce Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 27 Apr 2017 12:48:43 -0400 Subject: [PATCH 252/315] fix spelling in comment --- src/test/java/org/json/junit/JSONMLTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 1ad2cb416..df5244df2 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -782,7 +782,7 @@ public void testAttributeConversionReversabilityHTML() { // this test does not pass for the following reasons: // 1. Our XML parser does not handle generic HTML entities, only valid XML entities. Hence   -// or other HTML specific entites would fail on reversability +// or other HTML specific entities would fail on reversability // 2. Our JSON implementation for storing the XML attributes uses the standard unordered map. // This means that can not be reversed reliably. // /** From c233ae709e6d4c6da2296fc366e37de290935090 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 27 Apr 2017 12:52:02 -0400 Subject: [PATCH 253/315] comment out second unreliable test --- src/test/java/org/json/junit/JSONMLTest.java | 43 ++++++++++---------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index df5244df2..8ece8e56f 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -762,29 +762,30 @@ public void testToJSONObject_reversibility() { assertEquals("original JSON does not equal the new JSON",originalJson, newJson); } - /** - * Test texts taken from jsonml.org. Currently our implementation FAILS this conversion but shouldn't. - * Technically JsonML should be able to transform any valid xhtml document, but ours only supports - * standard XML entities, not HTML entities. - */ - @Test - public void testAttributeConversionReversabilityHTML() { - final String originalXml = "
#5D28D1Example text here
#AF44EF127310656
#AAD034 © 
"; - final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"\u00A0\",[\"span\",{ \"style\" : \"background-color:maroon\" },\"\u00A9\"],\"\u00A0\"]]]"; - final JSONArray json = JSONML.toJSONArray(originalXml,true); - final String actualJsonString = json.toString(); - - final String reverseXml = JSONML.toString(json); - assertNotEquals(originalXml, reverseXml); - - assertNotEquals(expectedJsonString, actualJsonString); - } - -// this test does not pass for the following reasons: +// these tests do not pass for the following reasons: // 1. Our XML parser does not handle generic HTML entities, only valid XML entities. Hence   -// or other HTML specific entities would fail on reversability +// or other HTML specific entities would fail on reversability // 2. Our JSON implementation for storing the XML attributes uses the standard unordered map. -// This means that can not be reversed reliably. +// This means that can not be reversed reliably. +// +// /** +// * Test texts taken from jsonml.org. Currently our implementation FAILS this conversion but shouldn't. +// * Technically JsonML should be able to transform any valid xhtml document, but ours only supports +// * standard XML entities, not HTML entities. +// */ +// @Test +// public void testAttributeConversionReversabilityHTML() { +// final String originalXml = "
#5D28D1Example text here
#AF44EF127310656
#AAD034 © 
"; +// final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"\u00A0\",[\"span\",{ \"style\" : \"background-color:maroon\" },\"\u00A9\"],\"\u00A0\"]]]"; +// final JSONArray json = JSONML.toJSONArray(originalXml,true); +// final String actualJsonString = json.toString(); +// +// final String reverseXml = JSONML.toString(json); +// assertNotEquals(originalXml, reverseXml); +// +// assertNotEquals(expectedJsonString, actualJsonString); +// } +// // /** // * Test texts taken from jsonml.org but modified to have XML entities only. // */ From 1d040ec407bd5d6e2b8339af0769ad1047415ad8 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 16 May 2017 18:16:07 -0400 Subject: [PATCH 254/315] fixes errors with tests relating to https://github.com/stleary/JSON-java/pull/336 --- src/test/java/org/json/junit/JSONObjectTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index fb32cda74..4af66bfba 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1276,7 +1276,7 @@ public void jsonObjectIncrement() { * JSONObject constructor won't handle these types correctly, but * adding them via put works. */ - jsonObject.put("keyFloat", new Float(1.1)); + jsonObject.put("keyFloat", 1.1f); jsonObject.put("keyBigInt", new BigInteger("123456789123456789123456789123456780")); jsonObject.put("keyBigDec", new BigDecimal("123456789123456789123456789123456780.1")); jsonObject.increment("keyFloat"); @@ -1316,7 +1316,7 @@ public void jsonObjectIncrement() { * missing bits would not fit into the 32 bit float, i.e. the * information needed simply is not there! */ - assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(jsonObject.query("/keyFloat"))); + assertEquals(Float.valueOf(3.1f), jsonObject.query("/keyFloat")); /** * float f = 3.1f; double df = (double) f; double d = 3.1d; @@ -1364,7 +1364,7 @@ public void jsonObjectIncrement() { // 3. A float+float operation will be performed and results into a float primitive. // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa - assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Double ); + assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Float ); // correct implementation (with change of behavior) would be: // this.put(key, new Float((Float) value + 1)); // Probably it would be better to deprecate the method and remove some day, while convenient processing the "payload" is not From 49d47e3ff281a5fecc9889e2d8d37ca76e355cbd Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 16 May 2017 19:42:46 -0400 Subject: [PATCH 255/315] Adjustments to tests for https://github.com/stleary/JSON-java/pull/337/ --- src/test/java/org/json/junit/JSONObjectTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index fb32cda74..f6a562dc6 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1001,7 +1001,8 @@ public void bigNumberOperations() { assertTrue("expected an exeption", false); } catch (JSONException ignored) {} obj = jsonObject.optBigInteger("bigDec", BigInteger.ONE); - assertTrue("expected BigInteger", obj.equals(BigInteger.ONE)); + assertTrue("expected BigInteger", obj instanceof BigInteger); + assertEquals(bigDecimal.toBigInteger(), obj); /** * JSONObject.numberToString() works correctly, nothing to change. From 2867aaa8c8c219b2eddb755df4dfda667f955f4b Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 17 May 2017 12:33:59 -0400 Subject: [PATCH 256/315] Updates test cases to support new optFloat and optNumber --- .../java/org/json/junit/JSONArrayTest.java | 14 +++++++ .../java/org/json/junit/JSONObjectTest.java | 40 +++++++++++++++---- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 80b78a59b..666c03bc4 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -393,6 +393,20 @@ public void opt() { assertTrue("Array opt double default implicit", new Double(jsonArray.optDouble(99)).isNaN()); + assertTrue("Array opt float", + new Float(23.45e-4).equals(jsonArray.optFloat(5))); + assertTrue("Array opt float default", + new Float(1).equals(jsonArray.optFloat(0, 1))); + assertTrue("Array opt float default implicit", + new Float(jsonArray.optFloat(99)).isNaN()); + + assertTrue("Array opt Number", + new Double(23.45e-4).equals(jsonArray.optNumber(5))); + assertTrue("Array opt Number default", + new Double(1).equals(jsonArray.optNumber(0, 1d))); + assertTrue("Array opt Number default implicit", + new Double(jsonArray.optNumber(99,Double.NaN).doubleValue()).isNaN()); + assertTrue("Array opt int", new Integer(42).equals(jsonArray.optInt(7))); assertTrue("Array opt int default", diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index f6a562dc6..2718edfdc 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -614,6 +614,10 @@ public void jsonObjectValues() { jsonObject.optDouble("doubleKey") == -23.45e7); assertTrue("opt doubleKey with Default should be double", jsonObject.optDouble("doubleStrKey", Double.NaN) == 1); + assertTrue("optFloat doubleKey should be float", + jsonObject.optFloat("doubleKey") == -23.45e7f); + assertTrue("optFloat doubleKey with Default should be float", + jsonObject.optFloat("doubleStrKey", Float.NaN) == 1f); assertTrue("intKey should be int", jsonObject.optInt("intKey") == 42); assertTrue("opt intKey should be int", @@ -630,6 +634,18 @@ public void jsonObjectValues() { jsonObject.optLong("longKey", 0) == 1234567890123456789L); assertTrue("longStrKey should be long", jsonObject.getLong("longStrKey") == 987654321098765432L); + assertTrue("optNumber int should return Integer", + jsonObject.optNumber("intKey") instanceof Integer); + assertTrue("optNumber long should return Long", + 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("xKey should not exist", jsonObject.isNull("xKey")); assertTrue("stringKey should exist", @@ -1937,9 +1953,13 @@ public void jsonObjectOptDefault() { assertTrue("optJSONObject() should return null ", null==jsonObject.optJSONObject("myKey")); assertTrue("optLong() should return default long", - 42 == jsonObject.optLong("myKey", 42)); + 42l == jsonObject.optLong("myKey", 42l)); assertTrue("optDouble() should return default double", - 42.3 == jsonObject.optDouble("myKey", 42.3)); + 42.3d == jsonObject.optDouble("myKey", 42.3d)); + assertTrue("optFloat() should return default float", + 42.3f == jsonObject.optFloat("myKey", 42.3f)); + assertTrue("optNumber() should return default Number", + 42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue()); assertTrue("optString() should return default string", "hi".equals(jsonObject.optString("hiKey", "hi"))); } @@ -1967,9 +1987,13 @@ public void jsonObjectOptNoKey() { assertTrue("optJSONObject() should return null ", null==jsonObject.optJSONObject("myKey")); assertTrue("optLong() should return default long", - 42 == jsonObject.optLong("myKey", 42)); + 42l == jsonObject.optLong("myKey", 42l)); assertTrue("optDouble() should return default double", - 42.3 == jsonObject.optDouble("myKey", 42.3)); + 42.3d == jsonObject.optDouble("myKey", 42.3d)); + assertTrue("optFloat() should return default float", + 42.3f == jsonObject.optFloat("myKey", 42.3f)); + assertTrue("optNumber() should return default Number", + 42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue()); assertTrue("optString() should return default string", "hi".equals(jsonObject.optString("hiKey", "hi"))); } @@ -1983,11 +2007,13 @@ public void jsonObjectOptStringConversion() { assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true); assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false); assertTrue("unexpected optInt value",jo.optInt("int",0)==123); - assertTrue("unexpected optLong value",jo.optLong("int",0)==123); - assertTrue("unexpected optDouble value",jo.optDouble("int",0.0)==123.0); + assertTrue("unexpected optLong value",jo.optLong("int",0)==123l); + assertTrue("unexpected optDouble value",jo.optDouble("int",0.0d)==123.0d); + assertTrue("unexpected optFloat value",jo.optFloat("int",0.0f)==123.0f); assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0); assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); - + assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); + assertTrue("unexpected optNumber value",jo.optNumber("int",BigInteger.ZERO).longValue()==123l); } /** From bdb11634459de86e67390a3519770c814c23631b Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 18 May 2017 11:38:42 -0400 Subject: [PATCH 257/315] Adds conversion tests to ensure downward type coercions are handled sanely --- src/test/java/org/json/junit/JSONObjectTest.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 2718edfdc..13e4f5fb8 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2003,7 +2003,7 @@ public void jsonObjectOptNoKey() { */ @Test public void jsonObjectOptStringConversion() { - JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\"}"); + JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\",\"largeNumber\":\"19007199254740993.35481234487103587486413587843213584\"}"); assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true); assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false); assertTrue("unexpected optInt value",jo.optInt("int",0)==123); @@ -2014,6 +2014,15 @@ public void jsonObjectOptStringConversion() { assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); assertTrue("unexpected optNumber value",jo.optNumber("int",BigInteger.ZERO).longValue()==123l); + + // Test type coercion from larger to smaller + final BigDecimal largeValue = new BigDecimal("19007199254740993.35481234487103587486413587843213584"); + assertEquals(largeValue,jo.optBigDecimal("largeNumber", null)); + assertEquals(largeValue.toBigInteger(),jo.optBigInteger("largeNumber", null)); + assertEquals(largeValue.doubleValue(), jo.optDouble("largeNumber"), 0.0d); + assertEquals(largeValue.floatValue(), jo.optFloat("largeNumber"), 0.0f); + assertEquals(largeValue.longValue(), jo.optLong("largeNumber")); + assertEquals(largeValue.intValue(), jo.optInt("largeNumber")); } /** From 0150639119b76c2fc2b44d2be5ffa64976286262 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 18 May 2017 11:58:28 -0400 Subject: [PATCH 258/315] update the new coercion test to use actual values and show the parseDouble method is not robust enough for large numbers --- src/test/java/org/json/junit/JSONObjectTest.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 13e4f5fb8..83e0276a0 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; 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.mockito.Mockito.mock; @@ -2016,13 +2017,14 @@ public void jsonObjectOptStringConversion() { assertTrue("unexpected optNumber value",jo.optNumber("int",BigInteger.ZERO).longValue()==123l); // Test type coercion from larger to smaller - final BigDecimal largeValue = new BigDecimal("19007199254740993.35481234487103587486413587843213584"); - assertEquals(largeValue,jo.optBigDecimal("largeNumber", null)); - assertEquals(largeValue.toBigInteger(),jo.optBigInteger("largeNumber", null)); - assertEquals(largeValue.doubleValue(), jo.optDouble("largeNumber"), 0.0d); - assertEquals(largeValue.floatValue(), jo.optFloat("largeNumber"), 0.0f); - assertEquals(largeValue.longValue(), jo.optLong("largeNumber")); - assertEquals(largeValue.intValue(), jo.optInt("largeNumber")); + assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumber",null)); + assertEquals(1.9007199254740992E16, jo.optDouble("largeNumber"),0.0); + assertEquals(1.90071995E16f, jo.optFloat("largeNumber"),0.0f); + assertEquals(19007199254740993l, jo.optLong("largeNumber")); + assertEquals(1874919425, jo.optInt("largeNumber")); + + // the integer portion of the actual value is larger than a double can hold. + assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumber")); } /** From 1967bee23690ee48a8b8d2f2dccdff52cb771308 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 18 May 2017 12:11:43 -0400 Subject: [PATCH 259/315] expands the coercion tests a little more --- src/test/java/org/json/junit/JSONObjectTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 83e0276a0..1417e6b22 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2025,6 +2025,9 @@ public void jsonObjectOptStringConversion() { // the integer portion of the actual value is larger than a double can hold. assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumber")); + assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumber")); + assertEquals(19007199254740992l, (long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")); + assertEquals(2147483647, (int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")); } /** From cfe6851d8c9c1ff70eb7d3c98d5ffe94a40d2a11 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 18 May 2017 14:25:42 -0400 Subject: [PATCH 260/315] Adds testing for -0 with optNumber --- .../java/org/json/junit/JSONObjectTest.java | 110 ++++++++++++------ 1 file changed, 74 insertions(+), 36 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 1417e6b22..1f7a5c93b 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.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 void jsonObjectAccumulate() { // 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 void jsonObjectAppend() { // 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 void jsonObjectValues() { "\"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 void jsonObjectValues() { 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 void jsonObjectValues() { 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 void jsonObjectNonAndWrongValues() { 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void jsonObjectNonAndWrongValues() { } 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 void bigNumberOperations() { */ 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 void bigNumberOperations() { 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 void jsonObjectParsingErrors() { 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 void jsonObjectParsingErrors() { 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 void jsonObjectParsingErrors() { 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 void jsonObjectParsingErrors() { 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 void jsonObjectParsingErrors() { // 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 void jsonObjectParsingErrors() { 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 void jsonObjectNullOperations() { // 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) {} /** From 04d76b638beada214229666d10ee24ecd3670268 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Fri, 19 May 2017 15:01:37 -0400 Subject: [PATCH 261/315] split out tests for better readability --- .../java/org/json/junit/JSONObjectTest.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 1f7a5c93b..c1ea5a177 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -708,7 +708,7 @@ public void stringToValueNumbersTest() { * This test documents a need for BigDecimal conversion. */ Object obj = JSONObject.stringToValue( "299792.457999999984" ); - assertTrue( "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!", + assertTrue( "evaluates to 299792.458 double instead of 299792.457999999984 BigDecimal!", obj.equals(new Double(299792.458)) ); assertTrue( "1 should be an Integer!", JSONObject.stringToValue( "1" ) instanceof Integer ); @@ -2042,7 +2042,7 @@ public void jsonObjectOptNoKey() { */ @Test public void jsonObjectOptStringConversion() { - JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\",\"largeNumber\":\"19007199254740993.35481234487103587486413587843213584\"}"); + JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\"}"); assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true); assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false); assertTrue("unexpected optInt value",jo.optInt("int",0)==123); @@ -2053,17 +2053,38 @@ public void jsonObjectOptStringConversion() { assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); assertTrue("unexpected optNumber value",jo.optNumber("int",BigInteger.ZERO).longValue()==123l); + } + + /** + * Verifies that the opt methods properly convert string values to numbers and coerce them consistently. + */ + @Test + public void jsonObjectOptCoercion() { + JSONObject jo = new JSONObject("{\"largeNumberStr\":\"19007199254740993.35481234487103587486413587843213584\"}"); + // currently the parser doesn't recognize BigDecimal, to we have to put it manually + jo.put("largeNumber", new BigDecimal("19007199254740993.35481234487103587486413587843213584")); // Test type coercion from larger to smaller + assertEquals(new BigDecimal("19007199254740993.35481234487103587486413587843213584"), jo.optBigDecimal("largeNumber",null)); assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumber",null)); assertEquals(1.9007199254740992E16, jo.optDouble("largeNumber"),0.0); assertEquals(1.90071995E16f, jo.optFloat("largeNumber"),0.0f); assertEquals(19007199254740993l, jo.optLong("largeNumber")); assertEquals(1874919425, jo.optInt("largeNumber")); - + + // conversion from a string + assertEquals(new BigDecimal("19007199254740993.35481234487103587486413587843213584"), jo.optBigDecimal("largeNumberStr",null)); + assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumberStr",null)); + assertEquals(1.9007199254740992E16, jo.optDouble("largeNumberStr"),0.0); + assertEquals(1.90071995E16f, jo.optFloat("largeNumberStr"),0.0f); + assertEquals(19007199254740993l, jo.optLong("largeNumberStr")); + assertEquals(1874919425, jo.optInt("largeNumberStr")); + // the integer portion of the actual value is larger than a double can hold. assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumber")); assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumber")); + assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumberStr")); + assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumberStr")); assertEquals(19007199254740992l, (long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")); assertEquals(2147483647, (int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")); } From c5e4b91fa410687205205032a9b903824790f733 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 8 Jun 2017 02:25:59 -0400 Subject: [PATCH 262/315] 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 6b97107f5..53ac303e9 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 void enumAPI() { 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 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 diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index b2d136210..ebd1cd158 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -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); @@ -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 @@ -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); } /** @@ -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 @@ -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!"); + } + } } diff --git a/src/test/java/org/json/junit/JSONStringTest.java b/src/test/java/org/json/junit/JSONStringTest.java index 617b9e38b..ec40dbb57 100644 --- a/src/test/java/org/json/junit/JSONStringTest.java +++ b/src/test/java/org/json/junit/JSONStringTest.java @@ -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 { From 3081b4bd960886b1d3f81103ddc9d741869df51a Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 21 Jun 2017 14:59:42 -0400 Subject: [PATCH 263/315] Fixes for failing tests due to android integration --- src/test/java/org/json/junit/JSONMLTest.java | 64 ++++++++++---------- src/test/java/org/json/junit/XMLTest.java | 57 ++++++++--------- 2 files changed, 61 insertions(+), 60 deletions(-) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 8ece8e56f..833e9344e 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -42,11 +42,11 @@ public void emptyXMLException() { String xmlStr = ""; try { JSONML.toJSONArray(xmlStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Bad XML at 1 [character 2 line 1]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Bad XML at 0 [character 1 line 1]", + e.getMessage()); } } @@ -95,11 +95,11 @@ public void nonXMLException() { String xmlStr = "{ \"this is\": \"not xml\"}"; try { JSONML.toJSONArray(xmlStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Bad XML at 25 [character 26 line 1]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Bad XML at 23 [character 24 line 1]", + e.getMessage()); } } @@ -198,11 +198,11 @@ public void invalidSlashInTagException() { ""; try { JSONML.toJSONArray(xmlStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Misshaped tag at 176 [character 14 line 7]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Misshaped tag at 176 [character 14 line 4]", + e.getMessage()); } } @@ -223,11 +223,11 @@ public void invalidBangInTagException() { ""; try { JSONML.toJSONArray(xmlStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Misshaped meta tag at 216 [character 13 line 11]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Misshaped meta tag at 216 [character 13 line 7]", + e.getMessage()); } } @@ -253,11 +253,11 @@ public void invalidBangNoCloseInTagException() { ""; try { JSONML.toJSONArray(xmlStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Misshaped meta tag at 215 [character 13 line 11]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Misshaped meta tag at 215 [character 13 line 7]", + e.getMessage()); } } @@ -283,11 +283,11 @@ public void noCloseStartTagException() { ""; try { JSONML.toJSONArray(xmlStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Misplaced '<' at 194 [character 5 line 10]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Misplaced '<' at 194 [character 5 line 6]", + e.getMessage()); } } @@ -343,11 +343,11 @@ public void noCloseEndBraceException() { ""; try { JSONML.toJSONArray(xmlStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Misplaced '<' at 206 [character 1 line 12]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Misplaced '<' at 206 [character 1 line 7]", + e.getMessage()); } } @@ -373,11 +373,11 @@ public void invalidCDATABangInTagException() { ""; try { JSONML.toJSONArray(xmlStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Expected 'CDATA[' at 204 [character 11 line 9]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Expected 'CDATA[' at 204 [character 11 line 5]", + e.getMessage()); } } diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index dd827bdb0..11566c61f 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.json.JSONArray; import org.json.JSONException; @@ -74,11 +75,11 @@ public void shouldHandleInvalidSlashInTag() { ""; try { XML.toJSONObject(xmlStr); - assertTrue("Expecting a JSONException", false); + fail("Expecting a JSONException"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Misshaped tag at 176 [character 14 line 5]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Misshaped tag at 176 [character 14 line 4]", + e.getMessage()); } } @@ -99,11 +100,11 @@ public void shouldHandleInvalidBangInTag() { ""; try { XML.toJSONObject(xmlStr); - assertTrue("Expecting a JSONException", false); + fail("Expecting a JSONException"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Misshaped meta tag at 215 [character 13 line 8]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Misshaped meta tag at 215 [character 13 line 7]", + e.getMessage()); } } @@ -124,11 +125,11 @@ public void shouldHandleInvalidBangNoCloseInTag() { ""; try { XML.toJSONObject(xmlStr); - assertTrue("Expecting a JSONException", false); + fail("Expecting a JSONException"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Misshaped meta tag at 214 [character 13 line 8]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Misshaped meta tag at 214 [character 13 line 7]", + e.getMessage()); } } @@ -149,11 +150,11 @@ public void shouldHandleNoCloseStartTag() { ""; try { XML.toJSONObject(xmlStr); - assertTrue("Expecting a JSONException", false); + fail("Expecting a JSONException"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Misplaced '<' at 193 [character 4 line 7]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Misplaced '<' at 193 [character 4 line 6]", + e.getMessage()); } } @@ -174,11 +175,11 @@ public void shouldHandleInvalidCDATABangInTag() { ""; try { XML.toJSONObject(xmlStr); - assertTrue("Expecting a JSONException", false); + fail("Expecting a JSONException"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Expected 'CDATA[' at 204 [character 11 line 6]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Expected 'CDATA[' at 204 [character 11 line 5]", + e.getMessage()); } } @@ -397,9 +398,9 @@ public void shouldHandleEmptyArray(){ final String expected = ""; String output1 = XML.toString(jo1,"jo"); - assertTrue("Expected an empty root tag", expected.equals(output1)); + assertEquals("Expected an empty root tag", expected, output1); String output2 = XML.toString(jo2,"jo"); - assertTrue("Expected an empty root tag", expected.equals(output2)); + assertEquals("Expected an empty root tag", expected, output2); } /** @@ -414,9 +415,9 @@ public void shouldHandleEmptyMultiArray(){ final String expected = "OneFour"; String output1 = XML.toString(jo1,"jo"); - assertTrue("Expected a matching array", expected.equals(output1)); + assertEquals("Expected a matching array", expected, output1); String output2 = XML.toString(jo2,"jo"); - assertTrue("Expected a matching array", expected.equals(output2)); + assertEquals("Expected a matching array", expected, output2); } /** @@ -431,9 +432,9 @@ public void shouldHandleNonEmptyArray(){ final String expected = "OneTwoThree"; String output1 = XML.toString(jo1,"jo"); - assertTrue("Expected a non empty root tag", expected.equals(output1)); + assertEquals("Expected a non empty root tag", expected, output1); String output2 = XML.toString(jo2,"jo"); - assertTrue("Expected a non empty root tag", expected.equals(output2)); + assertEquals("Expected a non empty root tag", expected, output2); } /** @@ -448,9 +449,9 @@ public void shouldHandleMultiArray(){ final String expected = "OneTwoThreeFour"; String output1 = XML.toString(jo1,"jo"); - assertTrue("Expected a matching array", expected.equals(output1)); + assertEquals("Expected a matching array", expected, output1); String output2 = XML.toString(jo2,"jo"); - assertTrue("Expected a matching array", expected.equals(output2)); + assertEquals("Expected a matching array", expected, output2); } /** From 971614ac8b28b40e14ab27615b098ccebcb80eb0 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 21 Jun 2017 18:28:04 -0400 Subject: [PATCH 264/315] fix expected exception message --- src/test/java/org/json/junit/JSONMLTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 833e9344e..518c94953 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -98,7 +98,7 @@ public void nonXMLException() { fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Bad XML at 23 [character 24 line 1]", + "Bad XML at 24 [character 25 line 1]", e.getMessage()); } } From 0e612ba8a4767cf9e5b6cae399133c234dd5a9f9 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 21 Jun 2017 19:56:00 -0400 Subject: [PATCH 265/315] More test corrections for correct position reports in error messages --- src/test/java/org/json/junit/CDLTest.java | 73 +++++++++++-------- .../java/org/json/junit/CookieListTest.java | 8 +- src/test/java/org/json/junit/CookieTest.java | 24 +++--- .../java/org/json/junit/JSONArrayTest.java | 6 +- src/test/java/org/json/junit/JSONMLTest.java | 6 +- .../java/org/json/junit/JSONObjectTest.java | 48 ++++++------ src/test/java/org/json/junit/XMLTest.java | 4 +- 7 files changed, 92 insertions(+), 77 deletions(-) diff --git a/src/test/java/org/json/junit/CDLTest.java b/src/test/java/org/json/junit/CDLTest.java index a40b0143e..b99e17c9d 100644 --- a/src/test/java/org/json/junit/CDLTest.java +++ b/src/test/java/org/json/junit/CDLTest.java @@ -61,11 +61,11 @@ public void unbalancedQuoteInName() { String badLine = "Col1, \"Col2\nVal1, Val2"; try { CDL.toJSONArray(badLine); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Missing close quote '\"'. at 12 [character 0 line 2]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Missing close quote '\"'. at 12 [character 0 line 2]", + e.getMessage()); } } @@ -78,11 +78,11 @@ public void unbalancedQuoteInValue() { String badLine = "Col1, Col2\n\"Val1, Val2"; try { CDL.toJSONArray(badLine); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Missing close quote '\"'. at 23 [character 12 line 3]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Missing close quote '\"'. at 22 [character 11 line 3]", + e.getMessage()); } } @@ -96,11 +96,11 @@ public void nullInName() { String badLine = "C\0ol1, Col2\nVal1, Val2"; try { CDL.toJSONArray(badLine); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Bad character 'o' (111). at 3 [character 4 line 1]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Bad character 'o' (111). at 2 [character 3 line 1]", + e.getMessage()); } } @@ -114,11 +114,11 @@ public void unbalancedEscapedQuote(){ String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\""; try { CDL.toJSONArray(badLine); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Missing close quote '\"'. at 27 [character 16 line 3]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Missing close quote '\"'. at 26 [character 15 line 3]", + e.getMessage()); } } @@ -128,7 +128,7 @@ public void unbalancedEscapedQuote(){ */ @Test public void singleEscapedQuote(){ - String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\""; + String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\""; JSONArray jsonArray = CDL.toJSONArray(singleEscape); String cdlStr = CDL.toString(jsonArray); @@ -136,7 +136,22 @@ public void singleEscapedQuote(){ assertTrue(cdlStr.contains("Col2")); assertTrue(cdlStr.contains("Val1")); assertTrue(cdlStr.contains("\"Val2")); + } + + /** + * Assert that there is no error for a single escaped quote within a properly + * embedded quote when not the last value. + */ + @Test + public void singleEscapedQuoteMiddleString(){ + String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"\nVal 3,Val 4"; + JSONArray jsonArray = CDL.toJSONArray(singleEscape); + String cdlStr = CDL.toString(jsonArray); + assertTrue(cdlStr.contains("Col1")); + assertTrue(cdlStr.contains("Col2")); + assertTrue(cdlStr.contains("Val1")); + assertTrue(cdlStr.contains("\"Val2")); } /** @@ -149,12 +164,12 @@ public void badEscapedQuote(){ try { CDL.toJSONArray(badLine); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { System.out.println("Message" + e.getMessage()); - assertTrue("Expecting an exception message", - "Bad character 'V' (86). at 20 [character 9 line 3]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Bad character 'V' (86). at 20 [character 9 line 3]", + e.getMessage()); } @@ -186,8 +201,8 @@ public void emptyString() { public void onlyColumnNames() { String columnNameStr = "col1, col2, col3"; JSONArray jsonArray = CDL.toJSONArray(columnNameStr); - assertTrue("CDL should return null when only 1 row is given", - jsonArray == null); + assertNull("CDL should return null when only 1 row is given", + jsonArray); } /** @@ -197,8 +212,8 @@ public void onlyColumnNames() { public void emptyLinesToJSONArray() { String str = " , , , \n , , , "; JSONArray jsonArray = CDL.toJSONArray(str); - assertTrue("JSONArray should be null for no content", - jsonArray == null); + assertNull("JSONArray should be null for no content", + jsonArray); } /** @@ -208,8 +223,8 @@ public void emptyLinesToJSONArray() { public void emptyJSONArrayToString() { JSONArray jsonArray = new JSONArray(); String str = CDL.toString(jsonArray); - assertTrue("CDL should return null for toString(null)", - str == null); + assertNull("CDL should return null for toString(null)", + str); } /** @@ -218,8 +233,8 @@ public void emptyJSONArrayToString() { @Test public void nullJSONArraysToString() { String str = CDL.toString(null, null); - assertTrue("CDL should return null for toString(null)", - str == null); + assertNull("CDL should return null for toString(null)", + str); } /** diff --git a/src/test/java/org/json/junit/CookieListTest.java b/src/test/java/org/json/junit/CookieListTest.java index 7a710dbe3..80cbaa8ed 100644 --- a/src/test/java/org/json/junit/CookieListTest.java +++ b/src/test/java/org/json/junit/CookieListTest.java @@ -47,14 +47,14 @@ public void malFormedCookieListException() { String cookieStr = "thisCookieHasNoEqualsChar"; try { CookieList.toJSONObject(cookieStr); - assertTrue("should throw an exception", false); + fail("should throw an exception"); } catch (JSONException e) { /** * Not sure of the missing char, but full string compare fails */ - assertTrue("Expecting an exception message", - e.getMessage().startsWith("Expected '=' and instead saw '") && - e.getMessage().endsWith("' at 27 [character 28 line 1]")); + assertEquals("Expecting an exception message", + "Expected '=' and instead saw '' at 25 [character 26 line 1]", + e.getMessage()); } } diff --git a/src/test/java/org/json/junit/CookieTest.java b/src/test/java/org/json/junit/CookieTest.java index 9104b60e1..4b7ca4442 100644 --- a/src/test/java/org/json/junit/CookieTest.java +++ b/src/test/java/org/json/junit/CookieTest.java @@ -43,11 +43,11 @@ public void malFormedNameValueException() { String cookieStr = "thisCookieHasNoEqualsChar"; try { Cookie.toJSONObject(cookieStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - e.getMessage().startsWith("Expected '=' and instead saw '") - && e.getMessage().endsWith("' at 27 [character 28 line 1]")); + assertEquals("Expecting an exception message", + "Expected '=' and instead saw '' at 25 [character 26 line 1]", + e.getMessage()); } } @@ -61,11 +61,11 @@ public void malFormedAttributeException() { String cookieStr = "this=Cookie;myAttribute"; try { Cookie.toJSONObject(cookieStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Missing '=' in cookie parameter. at 25 [character 26 line 1]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Missing '=' in cookie parameter. at 23 [character 24 line 1]", + e.getMessage()); } } @@ -79,11 +79,11 @@ public void emptyStringCookieException() { String cookieStr = ""; try { Cookie.toJSONObject(cookieStr); - assertTrue("Expecting an exception", false); + fail("Expecting an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - e.getMessage().startsWith("Expected '=' and instead saw '") && - e.getMessage().endsWith("' at 2 [character 3 line 1]")); + assertEquals("Expecting an exception message", + "Expected '=' and instead saw '' at 0 [character 1 line 1]", + e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 666c03bc4..0df7c5d52 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -74,9 +74,9 @@ public void emptStr() { try { assertNull("Should throw an exception", new JSONArray(str)); } catch (JSONException e) { - assertTrue("Expected an exception message", - "A JSONArray text must start with '[' at 1 [character 2 line 1]". - equals(e.getMessage())); + assertEquals("Expected an exception message", + "A JSONArray text must start with '[' at 0 [character 1 line 1]", + e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 518c94953..84b33ba3d 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -98,7 +98,7 @@ public void nonXMLException() { fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Bad XML at 24 [character 25 line 1]", + "Bad XML at 23 [character 24 line 1]", e.getMessage()); } } @@ -226,7 +226,7 @@ public void invalidBangInTagException() { fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Misshaped meta tag at 216 [character 13 line 7]", + "Misshaped meta tag at 215 [character 12 line 7]", e.getMessage()); } } @@ -256,7 +256,7 @@ public void invalidBangNoCloseInTagException() { fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Misshaped meta tag at 215 [character 13 line 7]", + "Misshaped meta tag at 214 [character 12 line 7]", e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index ebd1cd158..cabd41c3b 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1853,36 +1853,36 @@ public void jsonObjectParsingErrors() { String str = "abc"; assertNull("Expected an exception",new JSONObject(str)); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "A JSONObject text must begin with '{' at 1 [character 2 line 1]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "A JSONObject text must begin with '{' at 1 [character 2 line 1]", + e.getMessage()); } try { // does not end with '}' String str = "{"; assertNull("Expected an exception",new JSONObject(str)); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "A JSONObject text must end with '}' at 2 [character 3 line 1]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "A JSONObject text must end with '}' at 1 [character 2 line 1]", + e.getMessage()); } try { // key with no ':' String str = "{\"myKey\" = true}"; assertNull("Expected an exception",new JSONObject(str)); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Expected a ':' after a key at 10 [character 11 line 1]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Expected a ':' after a key at 10 [character 11 line 1]", + e.getMessage()); } try { // entries with no ',' separator String str = "{\"myKey\":true \"myOtherKey\":false}"; assertNull("Expected an exception",new JSONObject(str)); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Expected a ',' or '}' at 15 [character 16 line 1]". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Expected a ',' or '}' at 15 [character 16 line 1]", + e.getMessage()); } try { // append to wrong key @@ -1891,9 +1891,9 @@ public void jsonObjectParsingErrors() { jsonObject.append("myKey", "hello"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[myKey] is not a JSONArray.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[myKey] is not a JSONArray.", + e.getMessage()); } try { // increment wrong key @@ -1902,9 +1902,9 @@ public void jsonObjectParsingErrors() { jsonObject.increment("myKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Unable to increment [\"myKey\"].". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Unable to increment [\"myKey\"].", + e.getMessage()); } try { // invalid key @@ -1913,18 +1913,18 @@ public void jsonObjectParsingErrors() { jsonObject.get(null); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Null key.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Null key.", + e.getMessage()); } try { // invalid numberToString() JSONObject.numberToString((Number)null); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "Null pointer". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "Null pointer", + e.getMessage()); } try { // null put key diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 11566c61f..244c9e932 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -103,7 +103,7 @@ public void shouldHandleInvalidBangInTag() { fail("Expecting a JSONException"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Misshaped meta tag at 215 [character 13 line 7]", + "Misshaped meta tag at 214 [character 12 line 7]", e.getMessage()); } } @@ -128,7 +128,7 @@ public void shouldHandleInvalidBangNoCloseInTag() { fail("Expecting a JSONException"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Misshaped meta tag at 214 [character 13 line 7]", + "Misshaped meta tag at 213 [character 12 line 7]", e.getMessage()); } } From af39376d926b8a1aaf21646e63131e9acecfd996 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Fri, 23 Jun 2017 23:25:11 -0400 Subject: [PATCH 266/315] more fixes for testing postition information --- src/test/java/org/json/junit/CDLTest.java | 6 +- .../java/org/json/junit/JSONPointerTest.java | 22 ++- .../java/org/json/junit/JSONTokenerTest.java | 158 ++++++++++++++++++ 3 files changed, 179 insertions(+), 7 deletions(-) create mode 100644 src/test/java/org/json/junit/JSONTokenerTest.java diff --git a/src/test/java/org/json/junit/CDLTest.java b/src/test/java/org/json/junit/CDLTest.java index b99e17c9d..b1f9561f4 100644 --- a/src/test/java/org/json/junit/CDLTest.java +++ b/src/test/java/org/json/junit/CDLTest.java @@ -81,7 +81,7 @@ public void unbalancedQuoteInValue() { fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Missing close quote '\"'. at 22 [character 11 line 3]", + "Missing close quote '\"'. at 22 [character 11 line 2]", e.getMessage()); } @@ -117,7 +117,7 @@ public void unbalancedEscapedQuote(){ fail("Expecting an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Missing close quote '\"'. at 26 [character 15 line 3]", + "Missing close quote '\"'. at 26 [character 15 line 2]", e.getMessage()); } @@ -168,7 +168,7 @@ public void badEscapedQuote(){ } catch (JSONException e) { System.out.println("Message" + e.getMessage()); assertEquals("Expecting an exception message", - "Bad character 'V' (86). at 20 [character 9 line 3]", + "Bad character 'V' (86). at 20 [character 9 line 2]", e.getMessage()); } diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java index 0904b9e38..19dac47ae 100644 --- a/src/test/java/org/json/junit/JSONPointerTest.java +++ b/src/test/java/org/json/junit/JSONPointerTest.java @@ -1,8 +1,18 @@ package org.json.junit; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; -import org.json.*; +import java.io.IOException; +import java.io.InputStream; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONPointer; +import org.json.JSONPointerException; +import org.json.JSONTokener; import org.junit.Test; public class JSONPointerTest { @@ -10,8 +20,12 @@ public class JSONPointerTest { private static final JSONObject document; static { - document = new JSONObject(new JSONTokener( - JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json"))); + @SuppressWarnings("resource") + InputStream resourceAsStream = JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json"); + if(resourceAsStream == null) { + throw new ExceptionInInitializerError("Unable to locate test file. Please check your development environment configuration"); + } + document = new JSONObject(new JSONTokener(resourceAsStream)); } private Object query(String pointer) { diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java new file mode 100644 index 000000000..5fe225988 --- /dev/null +++ b/src/test/java/org/json/junit/JSONTokenerTest.java @@ -0,0 +1,158 @@ +package org.json.junit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; + +import org.json.JSONException; +import org.json.JSONTokener; +import org.junit.Test; + +/** + * Test specific to the {@link org.json.JSONTokener} class. + * @author John Aylward + * + */ +public class JSONTokenerTest { + + /** + * verify that back() fails as expected. + * @throws IOException thrown if something unexpected happens. + */ + @Test + public void verifyBackFailureZeroIndex() throws IOException { + try(Reader reader = new StringReader("some test string")) { + final JSONTokener tokener = new JSONTokener(reader); + try { + // this should fail since the index is 0; + tokener.back(); + fail("Expected an exception"); + } catch (JSONException e) { + assertEquals("Stepping back two steps is not supported", e.getMessage()); + } catch (Exception e) { + fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage()); + } + + } + } + /** + * verify that back() fails as expected. + * @throws IOException thrown if something unexpected happens. + */ + @Test + public void verifyBackFailureDoubleBack() throws IOException { + try(Reader reader = new StringReader("some test string")) { + final JSONTokener tokener = new JSONTokener(reader); + tokener.next(); + tokener.back(); + try { + // this should fail since the index is 0; + tokener.back(); + fail("Expected an exception"); + } catch (JSONException e) { + assertEquals("Stepping back two steps is not supported", e.getMessage()); + } catch (Exception e) { + fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage()); + } + } + } + + /** + * Tests the failure of the skipTo method with a buffered reader. Preferably + * we'd like this not to fail but at this time we don't have a good recovery. + * + * @throws IOException thrown if something unexpected happens. + */ + @Test + public void testSkipToFailureWithBufferedReader() throws IOException { + final byte[] superLongBuffer = new byte[1000001]; + // fill our buffer + for(int i=0;i Date: Fri, 23 Jun 2017 23:55:22 -0400 Subject: [PATCH 267/315] Updates test coverage table --- README.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0772d3bdf..79f872442 100644 --- a/README.md +++ b/README.md @@ -101,24 +101,26 @@ A unit test has the following stages: | CDL.java | 98.8% | Reasonable test cases. | | Cookie.java | 98.9% | Reasonable test cases. | | CookieList.java |96.5% | Reasonable test cases. | -| EnumTest.java | n/a | Just documenting how enums are handled. | -| HTTP.java | 98.7%| Coverage > 90% | +| HTTP.java | 98.8%| Coverage > 90% | | HTTPTokener.java |93.2% | No test | -| JSONArray.java |95.9% | Reasonable test cases | -| JSONException.java | 26.7% | No test | -| JSONML.java | 86.8%| In progress | -| JSONObject | 94.0% | Reasonable test cases | -| JSONObject.Null | 87.5% | No test | +| JSONArray.java |88.3% | Reasonable test cases. Need new tests for newer API functions | +| JSONException.java | 100% | No test | +| JSONML.java | 84.4%| In progress | +| JSONObject | 96.7% | Reasonable test cases | +| JSONObject.Null | 77.8% | No test | +| JSONPointer | 96.3% | Reasonable test cases | +| JSONPointerException | 100% | No test | | JSONString.java | | No test | | JSONStringer.java | 93.8%| Coverage > 90% | -| JSONTokener.java | 72.1% | In progress | -| JSONWriter.java | 87.5% | No test | -| Property.java | 94.8% | Coverage > 90% | -| XML.java | 87.4% | In progress | -| XMLTokener.java| 82.7%| No test | +| JSONTokener.java | 87.5% | In progress | +| JSONWriter.java | 89.15% | No test | +| Property.java | 95.8% | Coverage > 90% | +| XML.java | 77.3% | In progress | +| XMLTokener.java| 82.4%| No test | | Files used in test | | ------------- | +| EnumTest.java | | MyBean.java | | MyBigNumberBean.java | | MyEnum.java | From 899cf528df591420ba107af445bed46d80cd316f Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Sat, 24 Jun 2017 13:10:14 -0400 Subject: [PATCH 268/315] More test cases for position information --- .../java/org/json/junit/JSONTokenerTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java index 5fe225988..dced89f7d 100644 --- a/src/test/java/org/json/junit/JSONTokenerTest.java +++ b/src/test/java/org/json/junit/JSONTokenerTest.java @@ -1,6 +1,8 @@ package org.json.junit; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.BufferedReader; @@ -121,7 +123,12 @@ public void testSkipToSuccessWithStringReader() throws IOException { @Test public void testNextBackComboWithNewLines() { final String testString = "this is\nA test\r\nWith some different\rNew Lines"; + // ^ ^ ^ ^ + // index positions 0 8 16 36 final JSONTokener tokener = new JSONTokener(testString); + assertEquals(" at 0 [character 1 line 1]", tokener.toString()); + assertEquals('t',tokener.next()); + assertEquals(" at 1 [character 2 line 1]", tokener.toString()); tokener.skipTo('\n'); assertEquals("skipTo() improperly modifying indexes"," at 7 [character 8 line 1]", tokener.toString()); assertEquals('\n',tokener.next()); @@ -132,10 +139,12 @@ public void testNextBackComboWithNewLines() { assertEquals(" at 8 [character 0 line 2]", tokener.toString()); tokener.skipTo('\r'); assertEquals("skipTo() improperly modifying indexes"," at 14 [character 6 line 2]", tokener.toString()); + // verify \r\n combo doesn't increment the line twice assertEquals('\r', tokener.next()); assertEquals(" at 15 [character 0 line 3]", tokener.toString()); assertEquals('\n', tokener.next()); assertEquals(" at 16 [character 0 line 3]", tokener.toString()); + // verify stepping back after reading the \n of an \r\n combo doesn't increment the line incorrectly tokener.back(); assertEquals(" at 15 [character 6 line 2]", tokener.toString()); assertEquals('\n', tokener.next()); @@ -154,5 +163,39 @@ public void testNextBackComboWithNewLines() { assertEquals(" at 36 [character 0 line 4]", tokener.toString()); assertEquals('N', tokener.next()); assertEquals(" at 37 [character 1 line 4]", tokener.toString()); + + // verify we get the same data just walking though, no calls to back + final JSONTokener t2 = new JSONTokener(testString); + for(int i=0; i<7; i++) { + assertTrue(t2.toString().startsWith(" at " + i + " ")); + assertEquals(testString.charAt(i), t2.next()); + } + assertEquals(" at 7 [character 8 line 1]", t2.toString()); + assertEquals(testString.charAt(7), t2.next()); + assertEquals(" at 8 [character 0 line 2]", t2.toString()); + for(int i=8; i<14; i++) { + assertTrue(t2.toString().startsWith(" at " + i + " ")); + assertEquals(testString.charAt(i), t2.next()); + } + assertEquals(" at 14 [character 6 line 2]", t2.toString()); + assertEquals('\r', t2.next()); + assertEquals(" at 15 [character 0 line 3]", t2.toString()); + assertEquals('\n', t2.next()); + assertEquals(" at 16 [character 0 line 3]", t2.toString()); + assertEquals('W', t2.next()); + assertEquals(" at 17 [character 1 line 3]", t2.toString()); + for(int i=17; i<37; i++) { + assertTrue(t2.toString().startsWith(" at " + i + " ")); + assertEquals(testString.charAt(i), t2.next()); + } + assertEquals(" at 37 [character 1 line 4]", t2.toString()); + for(int i=37; i Date: Sun, 9 Jul 2017 16:33:39 -0400 Subject: [PATCH 269/315] reorganize classes so test data is separate from test cases --- src/test/java/org/json/junit/EnumTest.java | 5 +++- .../org/json/junit/JSONObjectLocaleTest.java | 1 + .../java/org/json/junit/JSONObjectTest.java | 25 ++++++++----------- .../org/json/junit/data/BrokenToString.java | 13 ++++++++++ .../org/json/junit/{ => data}/Fraction.java | 2 +- .../org/json/junit/{ => data}/MyBean.java | 4 +-- .../junit/{ => data}/MyBigNumberBean.java | 4 +-- .../org/json/junit/{ => data}/MyEnum.java | 2 +- .../json/junit/{ => data}/MyEnumClass.java | 2 +- .../json/junit/{ => data}/MyEnumField.java | 2 +- .../json/junit/{ => data}/MyJsonString.java | 4 +-- .../json/junit/{ => data}/MyLocaleBean.java | 2 +- .../org/json/junit/{ => data}/MyNumber.java | 2 +- .../junit/{ => data}/MyNumberContainer.java | 2 +- .../json/junit/{ => data}/MyPublicClass.java | 2 +- .../{ => data}/StringsResourceBundle.java | 2 +- 16 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 src/test/java/org/json/junit/data/BrokenToString.java rename src/test/java/org/json/junit/{ => data}/Fraction.java (99%) rename src/test/java/org/json/junit/{ => data}/MyBean.java (85%) rename src/test/java/org/json/junit/{ => data}/MyBigNumberBean.java (72%) rename src/test/java/org/json/junit/{ => data}/MyEnum.java (76%) rename src/test/java/org/json/junit/{ => data}/MyEnumClass.java (94%) rename src/test/java/org/json/junit/{ => data}/MyEnumField.java (95%) rename src/test/java/org/json/junit/{ => data}/MyJsonString.java (67%) rename src/test/java/org/json/junit/{ => data}/MyLocaleBean.java (88%) rename src/test/java/org/json/junit/{ => data}/MyNumber.java (98%) rename src/test/java/org/json/junit/{ => data}/MyNumberContainer.java (90%) rename src/test/java/org/json/junit/{ => data}/MyPublicClass.java (87%) rename src/test/java/org/json/junit/{ => data}/StringsResourceBundle.java (93%) diff --git a/src/test/java/org/json/junit/EnumTest.java b/src/test/java/org/json/junit/EnumTest.java index 53ac303e9..cd0d8c0fc 100644 --- a/src/test/java/org/json/junit/EnumTest.java +++ b/src/test/java/org/json/junit/EnumTest.java @@ -10,6 +10,9 @@ import org.json.JSONArray; import org.json.JSONObject; +import org.json.junit.data.MyEnum; +import org.json.junit.data.MyEnumClass; +import org.json.junit.data.MyEnumField; import org.junit.Test; import com.jayway.jsonpath.Configuration; @@ -195,7 +198,7 @@ public void enumValueToString() { * However, an enum within another class will not be rendered * unless that class overrides default toString() */ - String expectedStr3 = "\"org.json.junit.MyEnumClass@"; + String expectedStr3 = "\"org.json.junit.data.MyEnumClass@"; myEnumClass.setMyEnum(MyEnum.VAL1); myEnumClass.setMyEnumField(MyEnumField.VAL1); String str3 = JSONObject.valueToString(myEnumClass); diff --git a/src/test/java/org/json/junit/JSONObjectLocaleTest.java b/src/test/java/org/json/junit/JSONObjectLocaleTest.java index 9c80ab6a3..52ef7d503 100755 --- a/src/test/java/org/json/junit/JSONObjectLocaleTest.java +++ b/src/test/java/org/json/junit/JSONObjectLocaleTest.java @@ -5,6 +5,7 @@ import java.util.*; import org.json.*; +import org.json.junit.data.MyLocaleBean; import org.junit.*; /** diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index cabd41c3b..372e3624a 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -30,6 +30,16 @@ import org.json.JSONObject; import org.json.JSONPointerException; import org.json.XML; +import org.json.junit.data.BrokenToString; +import org.json.junit.data.Fraction; +import org.json.junit.data.MyBean; +import org.json.junit.data.MyBigNumberBean; +import org.json.junit.data.MyEnum; +import org.json.junit.data.MyEnumField; +import org.json.junit.data.MyJsonString; +import org.json.junit.data.MyNumber; +import org.json.junit.data.MyNumberContainer; +import org.json.junit.data.MyPublicClass; import org.junit.Test; import com.jayway.jsonpath.Configuration; @@ -484,7 +494,7 @@ public void jsonObjectByObjectAndNames() { @Test public void jsonObjectByResourceBundle() { JSONObject jsonObject = new - JSONObject("org.json.junit.StringsResourceBundle", + JSONObject("org.json.junit.data.StringsResourceBundle", Locale.getDefault()); // validate JSON @@ -2572,18 +2582,5 @@ public void toMap() { // assert that the new map is mutable assertTrue("Removing a key should succeed", map.remove("key3") != null); 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/data/BrokenToString.java b/src/test/java/org/json/junit/data/BrokenToString.java new file mode 100644 index 000000000..585d7518a --- /dev/null +++ b/src/test/java/org/json/junit/data/BrokenToString.java @@ -0,0 +1,13 @@ +package org.json.junit.data; + +/** + * test class for verifying write errors. + * @author John Aylward + * + */ +public class BrokenToString { + @Override + public String toString() { + throw new IllegalStateException("Something went horribly wrong!"); + } +} \ No newline at end of file diff --git a/src/test/java/org/json/junit/Fraction.java b/src/test/java/org/json/junit/data/Fraction.java similarity index 99% rename from src/test/java/org/json/junit/Fraction.java rename to src/test/java/org/json/junit/data/Fraction.java index d5d9eb659..c418179f9 100644 --- a/src/test/java/org/json/junit/Fraction.java +++ b/src/test/java/org/json/junit/data/Fraction.java @@ -1,4 +1,4 @@ -package org.json.junit; +package org.json.junit.data; import java.math.BigDecimal; import java.math.BigInteger; diff --git a/src/test/java/org/json/junit/MyBean.java b/src/test/java/org/json/junit/data/MyBean.java similarity index 85% rename from src/test/java/org/json/junit/MyBean.java rename to src/test/java/org/json/junit/data/MyBean.java index 53d150a52..31909810f 100644 --- a/src/test/java/org/json/junit/MyBean.java +++ b/src/test/java/org/json/junit/data/MyBean.java @@ -1,11 +1,11 @@ -package org.json.junit; +package org.json.junit.data; import java.io.*; /** * Used in testing when Bean behavior is needed */ -interface MyBean { +public interface MyBean { public Integer getIntKey(); public Double getDoubleKey(); public String getStringKey(); diff --git a/src/test/java/org/json/junit/MyBigNumberBean.java b/src/test/java/org/json/junit/data/MyBigNumberBean.java similarity index 72% rename from src/test/java/org/json/junit/MyBigNumberBean.java rename to src/test/java/org/json/junit/data/MyBigNumberBean.java index 0ca18704b..934dfee03 100644 --- a/src/test/java/org/json/junit/MyBigNumberBean.java +++ b/src/test/java/org/json/junit/data/MyBigNumberBean.java @@ -1,11 +1,11 @@ -package org.json.junit; +package org.json.junit.data; import java.math.*; /** * Used in testing when a Bean containing big numbers is needed */ -interface MyBigNumberBean { +public interface MyBigNumberBean { public BigInteger getBigInteger(); public BigDecimal getBigDecimal(); } \ No newline at end of file diff --git a/src/test/java/org/json/junit/MyEnum.java b/src/test/java/org/json/junit/data/MyEnum.java similarity index 76% rename from src/test/java/org/json/junit/MyEnum.java rename to src/test/java/org/json/junit/data/MyEnum.java index 0952bc246..50d9a4faa 100644 --- a/src/test/java/org/json/junit/MyEnum.java +++ b/src/test/java/org/json/junit/data/MyEnum.java @@ -1,4 +1,4 @@ -package org.json.junit; +package org.json.junit.data; /** * An enum with no methods or data diff --git a/src/test/java/org/json/junit/MyEnumClass.java b/src/test/java/org/json/junit/data/MyEnumClass.java similarity index 94% rename from src/test/java/org/json/junit/MyEnumClass.java rename to src/test/java/org/json/junit/data/MyEnumClass.java index 8e71663ec..4d403c874 100644 --- a/src/test/java/org/json/junit/MyEnumClass.java +++ b/src/test/java/org/json/junit/data/MyEnumClass.java @@ -1,4 +1,4 @@ -package org.json.junit; +package org.json.junit.data; /** * this is simply a class that contains some enum instances diff --git a/src/test/java/org/json/junit/MyEnumField.java b/src/test/java/org/json/junit/data/MyEnumField.java similarity index 95% rename from src/test/java/org/json/junit/MyEnumField.java rename to src/test/java/org/json/junit/data/MyEnumField.java index f0833ef6c..60e89de23 100644 --- a/src/test/java/org/json/junit/MyEnumField.java +++ b/src/test/java/org/json/junit/data/MyEnumField.java @@ -1,4 +1,4 @@ -package org.json.junit; +package org.json.junit.data; /** * An enum that contains getters and some internal fields diff --git a/src/test/java/org/json/junit/MyJsonString.java b/src/test/java/org/json/junit/data/MyJsonString.java similarity index 67% rename from src/test/java/org/json/junit/MyJsonString.java rename to src/test/java/org/json/junit/data/MyJsonString.java index 4e636933d..4ddde5385 100644 --- a/src/test/java/org/json/junit/MyJsonString.java +++ b/src/test/java/org/json/junit/data/MyJsonString.java @@ -1,11 +1,11 @@ -package org.json.junit; +package org.json.junit.data; import org.json.*; /** * Used in testing when a JSONString is needed */ -class MyJsonString implements JSONString { +public class MyJsonString implements JSONString { @Override public String toJSONString() { diff --git a/src/test/java/org/json/junit/MyLocaleBean.java b/src/test/java/org/json/junit/data/MyLocaleBean.java similarity index 88% rename from src/test/java/org/json/junit/MyLocaleBean.java rename to src/test/java/org/json/junit/data/MyLocaleBean.java index 0d68c39c6..846e1c5d7 100755 --- a/src/test/java/org/json/junit/MyLocaleBean.java +++ b/src/test/java/org/json/junit/data/MyLocaleBean.java @@ -1,4 +1,4 @@ -package org.json.junit; +package org.json.junit.data; public class MyLocaleBean { private final String id = "beanId"; diff --git a/src/test/java/org/json/junit/MyNumber.java b/src/test/java/org/json/junit/data/MyNumber.java similarity index 98% rename from src/test/java/org/json/junit/MyNumber.java rename to src/test/java/org/json/junit/data/MyNumber.java index 243a9679b..4b625affc 100644 --- a/src/test/java/org/json/junit/MyNumber.java +++ b/src/test/java/org/json/junit/data/MyNumber.java @@ -1,4 +1,4 @@ -package org.json.junit; +package org.json.junit.data; import java.math.BigDecimal; diff --git a/src/test/java/org/json/junit/MyNumberContainer.java b/src/test/java/org/json/junit/data/MyNumberContainer.java similarity index 90% rename from src/test/java/org/json/junit/MyNumberContainer.java rename to src/test/java/org/json/junit/data/MyNumberContainer.java index 524f318d1..65276529b 100644 --- a/src/test/java/org/json/junit/MyNumberContainer.java +++ b/src/test/java/org/json/junit/data/MyNumberContainer.java @@ -1,4 +1,4 @@ -package org.json.junit; +package org.json.junit.data; /** * Class that holds our MyNumber override as a property. diff --git a/src/test/java/org/json/junit/MyPublicClass.java b/src/test/java/org/json/junit/data/MyPublicClass.java similarity index 87% rename from src/test/java/org/json/junit/MyPublicClass.java rename to src/test/java/org/json/junit/data/MyPublicClass.java index e483d4c19..1f303860c 100644 --- a/src/test/java/org/json/junit/MyPublicClass.java +++ b/src/test/java/org/json/junit/data/MyPublicClass.java @@ -1,4 +1,4 @@ -package org.json.junit; +package org.json.junit.data; /** * Need a class with some public data members for testing diff --git a/src/test/java/org/json/junit/StringsResourceBundle.java b/src/test/java/org/json/junit/data/StringsResourceBundle.java similarity index 93% rename from src/test/java/org/json/junit/StringsResourceBundle.java rename to src/test/java/org/json/junit/data/StringsResourceBundle.java index d04aeaf85..4479350ec 100644 --- a/src/test/java/org/json/junit/StringsResourceBundle.java +++ b/src/test/java/org/json/junit/data/StringsResourceBundle.java @@ -1,4 +1,4 @@ -package org.json.junit; +package org.json.junit.data; import java.util.*; From 49117f33dcf5ff5800a244799bc5fd4fd3f1e2ee Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Sun, 9 Jul 2017 17:35:46 -0400 Subject: [PATCH 270/315] Adds new tests for testing bean->JSONObject mapping --- .../java/org/json/junit/JSONObjectTest.java | 80 ++++++++++++++++++ .../java/org/json/junit/data/GenericBean.java | 72 +++++++++++++++++ .../org/json/junit/data/GenericBeanInt.java | 29 +++++++ .../java/org/json/junit/data/Singleton.java | 81 +++++++++++++++++++ .../org/json/junit/data/SingletonEnum.java | 49 +++++++++++ .../java/org/json/junit/data/WeirdList.java | 45 +++++++++++ 6 files changed, 356 insertions(+) create mode 100644 src/test/java/org/json/junit/data/GenericBean.java create mode 100644 src/test/java/org/json/junit/data/GenericBeanInt.java create mode 100644 src/test/java/org/json/junit/data/Singleton.java create mode 100644 src/test/java/org/json/junit/data/SingletonEnum.java create mode 100644 src/test/java/org/json/junit/data/WeirdList.java diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 372e3624a..ae6a8b687 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -32,6 +33,8 @@ import org.json.XML; import org.json.junit.data.BrokenToString; import org.json.junit.data.Fraction; +import org.json.junit.data.GenericBean; +import org.json.junit.data.GenericBeanInt; import org.json.junit.data.MyBean; import org.json.junit.data.MyBigNumberBean; import org.json.junit.data.MyEnum; @@ -40,6 +43,9 @@ import org.json.junit.data.MyNumber; import org.json.junit.data.MyNumberContainer; import org.json.junit.data.MyPublicClass; +import org.json.junit.data.Singleton; +import org.json.junit.data.SingletonEnum; +import org.json.junit.data.WeirdList; import org.junit.Test; import com.jayway.jsonpath.Configuration; @@ -2583,4 +2589,78 @@ public void toMap() { assertTrue("Removing a key should succeed", map.remove("key3") != null); assertTrue("Map should have 2 elements", map.size() == 2); } + + @Test + public void testSingletonBean() { + final JSONObject jo = new JSONObject(Singleton.getInstance()); + assertEquals(jo.keySet().toString(), 1, jo.length()); + assertEquals(0, jo.get("someInt")); + assertEquals(null, jo.opt("someString")); + + // Update the singleton values + Singleton.getInstance().setSomeInt(42); + Singleton.getInstance().setSomeString("Something"); + final JSONObject jo2 = new JSONObject(Singleton.getInstance()); + assertEquals(2, jo2.length()); + assertEquals(42, jo2.get("someInt")); + assertEquals("Something", jo2.get("someString")); + + // ensure our original jo hasn't changed. + assertEquals(0, jo.get("someInt")); + assertEquals(null, jo.opt("someString")); + } + @Test + public void testSingletonEnumBean() { + final JSONObject jo = new JSONObject(SingletonEnum.getInstance()); + assertEquals(jo.keySet().toString(), 1, jo.length()); + assertEquals(0, jo.get("someInt")); + assertEquals(null, jo.opt("someString")); + + // Update the singleton values + SingletonEnum.getInstance().setSomeInt(42); + SingletonEnum.getInstance().setSomeString("Something"); + final JSONObject jo2 = new JSONObject(SingletonEnum.getInstance()); + assertEquals(2, jo2.length()); + assertEquals(42, jo2.get("someInt")); + assertEquals("Something", jo2.get("someString")); + + // ensure our original jo hasn't changed. + assertEquals(0, jo.get("someInt")); + assertEquals(null, jo.opt("someString")); + } + + @Test + public void testGenericBean() { + GenericBean bean = new GenericBean<>(42); + final JSONObject jo = new JSONObject(bean); + assertEquals(jo.keySet().toString(), 8, jo.length()); + assertEquals(42, jo.get("genericValue")); + assertEquals("Expected the getter to only be called once", + 1, bean.genericGetCounter); + assertEquals(0, bean.genericSetCounter); + } + + @Test + public void testGenericIntBean() { + GenericBeanInt bean = new GenericBeanInt(42); + final JSONObject jo = new JSONObject(bean); + assertEquals(jo.keySet().toString(), 9, jo.length()); + assertEquals(42, jo.get("genericValue")); + assertEquals("Expected the getter to only be called once", + 1, bean.genericGetCounter); + assertEquals(0, bean.genericSetCounter); + } + + @Test + public void testWierdListBean() { + WeirdList bean = new WeirdList(42, 43, 44); + final JSONObject jo = new JSONObject(bean); + // get() should have a key of 0 length + // get(int) should be ignored base on parameter count + // getInt(int) should also be ignored based on parameter count + // add(Integer) should be ignore as it doesn't start with get/is and also has a parameter + // getALL should be mapped + assertEquals("Expected 1 key to mapped "+jo.keySet().toString(), 1, jo.length()); + assertNotNull(jo.get("ALL")); + } } diff --git a/src/test/java/org/json/junit/data/GenericBean.java b/src/test/java/org/json/junit/data/GenericBean.java new file mode 100644 index 000000000..17f6def17 --- /dev/null +++ b/src/test/java/org/json/junit/data/GenericBean.java @@ -0,0 +1,72 @@ +package org.json.junit.data; + +import java.io.StringReader; + +/** + * + * @author John Aylward + * + * @param + * generic number value + */ +public class GenericBean> implements MyBean { + public GenericBean(T genericValue) { + super(); + this.genericValue = genericValue; + } + + /** */ + private T genericValue; + /** to be used by the calling test to see how often the getter is called */ + public int genericGetCounter; + /** to be used by the calling test to see how often the setter is called */ + public int genericSetCounter; + + /** @return the genericValue */ + public T getGenericValue() { + this.genericGetCounter++; + return this.genericValue; + } + + /** sets the generic value */ + public void setGenericValue(T genericValue) { + this.genericSetCounter++; + this.genericValue = genericValue; + } + + @Override + public Integer getIntKey() { + return Integer.valueOf(42); + } + + @Override + public Double getDoubleKey() { + return Double.valueOf(4.2); + } + + @Override + public String getStringKey() { + return "MyString Key"; + } + + @Override + public String getEscapeStringKey() { + return "\"My String with \"s"; + } + + @Override + public Boolean isTrueKey() { + return Boolean.TRUE; + } + + @Override + public Boolean isFalseKey() { + return Boolean.FALSE; + } + + @Override + public StringReader getStringReaderKey() { + return new StringReader("Some String Value in a reader"); + } + +} diff --git a/src/test/java/org/json/junit/data/GenericBeanInt.java b/src/test/java/org/json/junit/data/GenericBeanInt.java new file mode 100644 index 000000000..70dfb2828 --- /dev/null +++ b/src/test/java/org/json/junit/data/GenericBeanInt.java @@ -0,0 +1,29 @@ +/** + * + */ +package org.json.junit.data; + +/** + * @author john + * + */ +public class GenericBeanInt extends GenericBean { + /** */ + final char a = 'A'; + + /** return the a */ + public char getA() { + return a; + } + + /** return false. should not be beanable */ + public boolean getable() { + return false; + } + + /** */ + public GenericBeanInt(Integer genericValue) { + super(genericValue); + } + +} diff --git a/src/test/java/org/json/junit/data/Singleton.java b/src/test/java/org/json/junit/data/Singleton.java new file mode 100644 index 000000000..55b37f9ad --- /dev/null +++ b/src/test/java/org/json/junit/data/Singleton.java @@ -0,0 +1,81 @@ +package org.json.junit.data; + +/** + * Sample singleton for use with bean testing. + * + * @author John Aylward + * + */ +public final class Singleton { + /** */ + private int someInt; + /** */ + private String someString; + /** single instance. */ + private static final Singleton INSTANCE = new Singleton(); + + /** @return the singleton instance. */ + public static final Singleton getInstance() { + return INSTANCE; + } + + /** */ + private Singleton() { + if (INSTANCE != null) { + throw new IllegalStateException("Already instantiated"); + } + } + + @Override + protected Object clone() throws CloneNotSupportedException { + return INSTANCE; + } + + /** @return someInt */ + public int getSomeInt() { + return someInt; + } + + /** sets someInt */ + public void setSomeInt(int someInt) { + this.someInt = someInt; + } + + /** @return someString */ + public String getSomeString() { + return someString; + } + + /** sets someString */ + public void setSomeString(String someString) { + this.someString = someString; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + someInt; + result = prime * result + ((someString == null) ? 0 : someString.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Singleton other = (Singleton) obj; + if (someInt != other.someInt) + return false; + if (someString == null) { + if (other.someString != null) + return false; + } else if (!someString.equals(other.someString)) + return false; + return true; + } +} diff --git a/src/test/java/org/json/junit/data/SingletonEnum.java b/src/test/java/org/json/junit/data/SingletonEnum.java new file mode 100644 index 000000000..55c0e6c55 --- /dev/null +++ b/src/test/java/org/json/junit/data/SingletonEnum.java @@ -0,0 +1,49 @@ +package org.json.junit.data; + +/** + * Sample singleton done as an Enum for use with bean testing. + * + * @author John Aylward + * + */ +public enum SingletonEnum { + INSTANCE; + /** */ + private int someInt; + /** */ + private String someString; + + /** single instance. */ + + /** + * @return the singleton instance. I a real application, I'd hope no one did + * this to an enum singleton. + */ + public static final SingletonEnum getInstance() { + return INSTANCE; + } + + /** */ + private SingletonEnum() { + } + + /** @return someInt */ + public int getSomeInt() { + return someInt; + } + + /** sets someInt */ + public void setSomeInt(int someInt) { + this.someInt = someInt; + } + + /** @return someString */ + public String getSomeString() { + return someString; + } + + /** sets someString */ + public void setSomeString(String someString) { + this.someString = someString; + } +} diff --git a/src/test/java/org/json/junit/data/WeirdList.java b/src/test/java/org/json/junit/data/WeirdList.java new file mode 100644 index 000000000..315c144c8 --- /dev/null +++ b/src/test/java/org/json/junit/data/WeirdList.java @@ -0,0 +1,45 @@ +/** + * + */ +package org.json.junit.data; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @author John Aylward + */ +public class WeirdList { + /** */ + private final List list = new ArrayList<>(); + + public WeirdList(Integer... vals) { + this.list.addAll(Arrays.asList(vals)); + } + + /** gets a copy of the list */ + public List get() { + return new ArrayList<>(this.list); + } + + /** gets a copy of the list */ + public List getALL() { + return new ArrayList<>(this.list); + } + + /** get an index */ + public Integer get(int i) { + return this.list.get(i); + } + + /** get an index */ + public int getInt(int i) { + return this.list.get(i); + } + + /** adds a new value to the end of the list */ + public void add(Integer value) { + this.list.add(value); + } +} \ No newline at end of file From 7bc8f4102360c6d143f6ea826863c4840b3b1f07 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Sun, 9 Jul 2017 18:07:11 -0400 Subject: [PATCH 271/315] Add override of the generic getter to generate a Bridge method. --- src/test/java/org/json/junit/data/GenericBeanInt.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/org/json/junit/data/GenericBeanInt.java b/src/test/java/org/json/junit/data/GenericBeanInt.java index 70dfb2828..8549f19b1 100644 --- a/src/test/java/org/json/junit/data/GenericBeanInt.java +++ b/src/test/java/org/json/junit/data/GenericBeanInt.java @@ -26,4 +26,10 @@ public GenericBeanInt(Integer genericValue) { super(genericValue); } + /** override to generate a bridge method */ + @Override + public Integer getGenericValue() { + return super.getGenericValue(); + } + } From e94783f91b5423d728a11c3323461481883bf209 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Sun, 9 Jul 2017 18:19:27 -0400 Subject: [PATCH 272/315] Updates javadocs --- .../java/org/json/junit/JSONObjectTest.java | 19 ++++++++++- .../java/org/json/junit/data/GenericBean.java | 9 +++++- .../org/json/junit/data/GenericBeanInt.java | 15 ++++++--- .../java/org/json/junit/data/Singleton.java | 14 ++++++-- .../org/json/junit/data/SingletonEnum.java | 17 ++++++++-- .../java/org/json/junit/data/WeirdList.java | 32 ++++++++++++++++--- 6 files changed, 91 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index ae6a8b687..190f32a4f 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2590,6 +2590,9 @@ public void toMap() { assertTrue("Map should have 2 elements", map.size() == 2); } + /** + * test that validates a singleton can be serialized as a bean. + */ @Test public void testSingletonBean() { final JSONObject jo = new JSONObject(Singleton.getInstance()); @@ -2609,6 +2612,10 @@ public void testSingletonBean() { assertEquals(0, jo.get("someInt")); assertEquals(null, jo.opt("someString")); } + + /** + * test that validates a singleton can be serialized as a bean. + */ @Test public void testSingletonEnumBean() { final JSONObject jo = new JSONObject(SingletonEnum.getInstance()); @@ -2629,6 +2636,9 @@ public void testSingletonEnumBean() { assertEquals(null, jo.opt("someString")); } + /** + * Test to validate that a generic class can be serialized as a bean. + */ @Test public void testGenericBean() { GenericBean bean = new GenericBean<>(42); @@ -2640,6 +2650,9 @@ public void testGenericBean() { assertEquals(0, bean.genericSetCounter); } + /** + * Test to validate that a generic class can be serialized as a bean. + */ @Test public void testGenericIntBean() { GenericBeanInt bean = new GenericBeanInt(42); @@ -2651,6 +2664,9 @@ public void testGenericIntBean() { assertEquals(0, bean.genericSetCounter); } + /** + * Test to verify key limitations in the JSONObject bean serializer. + */ @Test public void testWierdListBean() { WeirdList bean = new WeirdList(42, 43, 44); @@ -2660,7 +2676,8 @@ public void testWierdListBean() { // getInt(int) should also be ignored based on parameter count // add(Integer) should be ignore as it doesn't start with get/is and also has a parameter // getALL should be mapped - assertEquals("Expected 1 key to mapped "+jo.keySet().toString(), 1, jo.length()); + assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(), + 1, jo.length()); assertNotNull(jo.get("ALL")); } } diff --git a/src/test/java/org/json/junit/data/GenericBean.java b/src/test/java/org/json/junit/data/GenericBean.java index 17f6def17..474003034 100644 --- a/src/test/java/org/json/junit/data/GenericBean.java +++ b/src/test/java/org/json/junit/data/GenericBean.java @@ -10,6 +10,10 @@ * generic number value */ public class GenericBean> implements MyBean { + /** + * @param genericValue + * value to initiate with + */ public GenericBean(T genericValue) { super(); this.genericValue = genericValue; @@ -28,7 +32,10 @@ public T getGenericValue() { return this.genericValue; } - /** sets the generic value */ + /** + * @param genericValue + * generic value to set + */ public void setGenericValue(T genericValue) { this.genericSetCounter++; this.genericValue = genericValue; diff --git a/src/test/java/org/json/junit/data/GenericBeanInt.java b/src/test/java/org/json/junit/data/GenericBeanInt.java index 8549f19b1..8f0248d8c 100644 --- a/src/test/java/org/json/junit/data/GenericBeanInt.java +++ b/src/test/java/org/json/junit/data/GenericBeanInt.java @@ -11,17 +11,24 @@ public class GenericBeanInt extends GenericBean { /** */ final char a = 'A'; - /** return the a */ + /** @return the a */ public char getA() { return a; } - - /** return false. should not be beanable */ + + /** + * Should not be beanable + * + * @return false + */ public boolean getable() { return false; } - /** */ + /** + * @param genericValue + * the value to initiate with. + */ public GenericBeanInt(Integer genericValue) { super(genericValue); } diff --git a/src/test/java/org/json/junit/data/Singleton.java b/src/test/java/org/json/junit/data/Singleton.java index 55b37f9ad..36a98240c 100644 --- a/src/test/java/org/json/junit/data/Singleton.java +++ b/src/test/java/org/json/junit/data/Singleton.java @@ -36,7 +36,12 @@ public int getSomeInt() { return someInt; } - /** sets someInt */ + /** + * sets someInt. + * + * @param someInt + * the someInt to set + */ public void setSomeInt(int someInt) { this.someInt = someInt; } @@ -46,7 +51,12 @@ public String getSomeString() { return someString; } - /** sets someString */ + /** + * sets someString. + * + * @param someString + * the someString to set + */ public void setSomeString(String someString) { this.someString = someString; } diff --git a/src/test/java/org/json/junit/data/SingletonEnum.java b/src/test/java/org/json/junit/data/SingletonEnum.java index 55c0e6c55..8147cc631 100644 --- a/src/test/java/org/json/junit/data/SingletonEnum.java +++ b/src/test/java/org/json/junit/data/SingletonEnum.java @@ -7,6 +7,9 @@ * */ public enum SingletonEnum { + /** + * the singleton instance. + */ INSTANCE; /** */ private int someInt; @@ -32,7 +35,12 @@ public int getSomeInt() { return someInt; } - /** sets someInt */ + /** + * sets someInt. + * + * @param someInt + * the someInt to set + */ public void setSomeInt(int someInt) { this.someInt = someInt; } @@ -42,7 +50,12 @@ public String getSomeString() { return someString; } - /** sets someString */ + /** + * sets someString. + * + * @param someString + * the someString to set + */ public void setSomeString(String someString) { this.someString = someString; } diff --git a/src/test/java/org/json/junit/data/WeirdList.java b/src/test/java/org/json/junit/data/WeirdList.java index 315c144c8..77cd17fc8 100644 --- a/src/test/java/org/json/junit/data/WeirdList.java +++ b/src/test/java/org/json/junit/data/WeirdList.java @@ -14,31 +14,53 @@ public class WeirdList { /** */ private final List list = new ArrayList<>(); + /** + * @param vals + */ public WeirdList(Integer... vals) { this.list.addAll(Arrays.asList(vals)); } - /** gets a copy of the list */ + /** + * @return a copy of the list + */ public List get() { return new ArrayList<>(this.list); } - /** gets a copy of the list */ + /** + * @return a copy of the list + */ public List getALL() { return new ArrayList<>(this.list); } - /** get an index */ + /** + * get a value at an index. + * + * @param i + * index to get + * @return the value at the index + */ public Integer get(int i) { return this.list.get(i); } - /** get an index */ + /** + * get a value at an index. + * + * @param i + * index to get + * @return the value at the index + */ public int getInt(int i) { return this.list.get(i); } - /** adds a new value to the end of the list */ + /** + * @param value + * new value to add to the end of the list + */ public void add(Integer value) { this.list.add(value); } From 38d11227ee5608536b78af410e50d4af293626a8 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Sun, 9 Jul 2017 19:05:00 -0400 Subject: [PATCH 273/315] Adds exception tests --- .../java/org/json/junit/JSONObjectTest.java | 14 ++++ .../org/json/junit/data/ExceptionalBean.java | 69 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/test/java/org/json/junit/data/ExceptionalBean.java diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 190f32a4f..8e61c5332 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -32,6 +32,7 @@ import org.json.JSONPointerException; import org.json.XML; import org.json.junit.data.BrokenToString; +import org.json.junit.data.ExceptionalBean; import org.json.junit.data.Fraction; import org.json.junit.data.GenericBean; import org.json.junit.data.GenericBeanInt; @@ -2680,4 +2681,17 @@ public void testWierdListBean() { 1, jo.length()); assertNotNull(jo.get("ALL")); } + + /** + * Tests the exception portions of populateMap. + */ + @Test + public void testExceptionalBean() { + ExceptionalBean bean = new ExceptionalBean(); + final JSONObject jo = new JSONObject(bean); + assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(), + 1, jo.length()); + assertTrue(jo.get("closeable") instanceof JSONObject); + assertTrue(jo.getJSONObject("closeable").has("string")); + } } diff --git a/src/test/java/org/json/junit/data/ExceptionalBean.java b/src/test/java/org/json/junit/data/ExceptionalBean.java new file mode 100644 index 000000000..74d78a7ca --- /dev/null +++ b/src/test/java/org/json/junit/data/ExceptionalBean.java @@ -0,0 +1,69 @@ +/** + * + */ +package org.json.junit.data; + +import java.io.Closeable; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; + +import org.json.JSONObject; + +/** + * Object for testing the exception handling in {@link JSONObject#populateMap}. + * + * @author John Aylward + */ +public class ExceptionalBean { + /** + * @return a closeable. + */ + public Closeable getCloseable() { + // anonymous inner class did not work... + return new MyCloseable(); + } + + /** + * @return Nothing really. Just can't be void. + * @throws IllegalAccessException + * always thrown + */ + public int getIllegalAccessException() throws IllegalAccessException { + throw new IllegalAccessException("Yup, it's illegal"); + } + + /** + * @return Nothing really. Just can't be void. + * @throws IllegalArgumentException + * always thrown + */ + public int getIllegalArgumentException() throws IllegalArgumentException { + throw new IllegalArgumentException("Yup, it's illegal"); + } + + /** + * @return Nothing really. Just can't be void. + * @throws InvocationTargetException + * always thrown + */ + public int getInvocationTargetException() throws InvocationTargetException { + throw new InvocationTargetException(new Exception("Yup, it's illegal")); + } + + /** My closeable class. */ + public static final class MyCloseable implements Closeable { + + /** + * @return a string + */ + @SuppressWarnings("unused") + public String getString() { + return "Yup, it's closeable"; + } + + @Override + public void close() throws IOException { + throw new IOException("Closing is too hard!"); + } + } +} From aa562b5ec3d87f4f4f0bff8ac9fb4552c8d271f8 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Sat, 15 Jul 2017 12:19:02 -0400 Subject: [PATCH 274/315] Update test for issue https://github.com/stleary/JSON-java/issues/356 --- src/test/java/org/json/junit/JSONObjectTest.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index cabd41c3b..0e6a69134 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1622,10 +1622,13 @@ public void jsonObjectToStringIndent() { " ]\n" + "}"; JSONObject jsonObject = new JSONObject(jsonObject0Str); - assertEquals(jsonObject0Str, jsonObject.toString()); - assertEquals(jsonObject0Str, jsonObject.toString(0)); - assertEquals(jsonObject1Str, jsonObject.toString(1)); - assertEquals(jsonObject4Str, jsonObject.toString(4)); + assertEquals("toString()",jsonObject0Str, jsonObject.toString()); + assertEquals("toString(0)",jsonObject0Str, jsonObject.toString(0)); + assertEquals("toString(1)",jsonObject1Str, jsonObject.toString(1)); + assertEquals("toString(4)",jsonObject4Str, jsonObject.toString(4)); + + JSONObject jo = new JSONObject().put("TABLE", new JSONObject().put("yhoo", new JSONObject())); + assertEquals("toString(2)","{\"TABLE\": {\"yhoo\": {}}}", jo.toString(2)); } /** From f2f6ad3b1f9324439489b5454811108faa3a3fd2 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 19 Jul 2017 20:34:59 -0400 Subject: [PATCH 275/315] * Fixes Gradle config so tests are only run once * Adds missing test to the test suite --- build.gradle | 5 ++++- src/test/java/org/json/junit/JunitTestSuite.java | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 58259f993..43656aee4 100644 --- a/build.gradle +++ b/build.gradle @@ -28,7 +28,10 @@ dependencies { // testCompile files('./JSON-Java.jar') } -test { finalizedBy jacocoTestReport } +test { + include "org/json/junit/JunitTestSuite.class" + finalizedBy jacocoTestReport +} jacocoTestReport{ additionalSourceDirs = files(sourceSets.main.allJava.srcDirs) reports { diff --git a/src/test/java/org/json/junit/JunitTestSuite.java b/src/test/java/org/json/junit/JunitTestSuite.java index 36bec607a..9c9a3259a 100644 --- a/src/test/java/org/json/junit/JunitTestSuite.java +++ b/src/test/java/org/json/junit/JunitTestSuite.java @@ -17,7 +17,8 @@ JSONArrayTest.class, EnumTest.class, JSONPointerTest.class, - JSONStringTest.class + JSONStringTest.class, + JSONTokenerTest.class }) public class JunitTestSuite { } From fefd616d73c063de3ffb4749e6b46b363eea3e93 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 9 Aug 2017 21:51:46 -0400 Subject: [PATCH 276/315] Unit tests for JSONTokener --- build.gradle | 1 + .../java/org/json/junit/JSONObjectTest.java | 170 ++++++++++++++++++ 2 files changed, 171 insertions(+) diff --git a/build.gradle b/build.gradle index 43656aee4..53fbdb35e 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,7 @@ dependencies { testCompile group: 'junit', name: 'junit', version: '4.+' testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0' testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5' + testCompile group: 'slf4j.org', name: 'slf4j', version: '1.6.1' // Uncomment if you are testing against a JSON-Java release // testCompile 'org.json:json:20160212' // Uncomment if you have copied a local JSON-Java jar file into this project diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 1231ec9f5..35cf493e6 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1976,6 +1976,176 @@ public void jsonObjectParsingErrors() { } catch (JSONException e) { assertTrue("", true); } + try { + // test exception message when including a duplicate key (level 0) + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr03\":\"value-04\"\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); + } catch (JSONException e) { + assertEquals("Expecting an expection message", + "Duplicate key \"attr03\" at 90 [character 13 line 5]", + e.getMessage()); + } + try { + // test exception message when including a duplicate key (level 0) holding an object + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr03\": {" + +" \"attr04-01\":\"value-04-01\",n" + +" \"attr04-02\":\"value-04-02\",n" + +" \"attr04-03\":\"value-04-03\"n" + + " }\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); + } catch (JSONException e) { + assertEquals("Expecting an expection message", + "Duplicate key \"attr03\" at 90 [character 13 line 5]", + e.getMessage()); + } + try { + // test exception message when including a duplicate key (level 0) holding an array + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr03\": [\n" + +" {" + +" \"attr04-01\":\"value-04-01\",n" + +" \"attr04-02\":\"value-04-02\",n" + +" \"attr04-03\":\"value-04-03\"n" + +" }\n" + + " ]\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); + } catch (JSONException e) { + assertEquals("Expecting an expection message", + "Duplicate key \"attr03\" at 90 [character 13 line 5]", + e.getMessage()); + } + try { + // test exception message when including a duplicate key (level 1) + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr04\": {\n" + +" \"attr04-01\":\"value04-01\",\n" + +" \"attr04-02\":\"value04-02\",\n" + +" \"attr04-03\":\"value04-03\",\n" + +" \"attr04-03\":\"value04-04\"\n" + + " }\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); + } catch (JSONException e) { + assertEquals("Expecting an expection message", + "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", + e.getMessage()); + } + try { + // test exception message when including a duplicate key (level 1) holding an object + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr04\": {\n" + +" \"attr04-01\":\"value04-01\",\n" + +" \"attr04-02\":\"value04-02\",\n" + +" \"attr04-03\":\"value04-03\",\n" + +" \"attr04-03\": {\n" + +" \"attr04-04-01\":\"value04-04-01\",\n" + +" \"attr04-04-02\":\"value04-04-02\",\n" + +" \"attr04-04-03\":\"value04-04-03\",\n" + +" }\n" + +" }\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); + } catch (JSONException e) { + assertEquals("Expecting an expection message", + "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", + e.getMessage()); + } + try { + // test exception message when including a duplicate key (level 1) holding an array + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr04\": {\n" + +" \"attr04-01\":\"value04-01\",\n" + +" \"attr04-02\":\"value04-02\",\n" + +" \"attr04-03\":\"value04-03\",\n" + +" \"attr04-03\": [\n" + +" {\n" + +" \"attr04-04-01\":\"value04-04-01\",\n" + +" \"attr04-04-02\":\"value04-04-02\",\n" + +" \"attr04-04-03\":\"value04-04-03\",\n" + +" }\n" + +" ]\n" + +" }\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); + } catch (JSONException e) { + assertEquals("Expecting an expection message", + "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", + e.getMessage()); + } + try { + // test exception message when including a duplicate key in object (level 0) within an array + String str = "[\n" + +" {\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\"\n" + +" },\n" + +" {\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr01\":\"value-02\"\n" + +" }\n" + + "]"; + new JSONArray(str); + fail("Expected an exception"); + } catch (JSONException e) { + assertEquals("Expecting an expection message", + "Duplicate key \"attr01\" at 124 [character 17 line 8]", + e.getMessage()); + } + try { + // test exception message when including a duplicate key in object (level 1) within an array + String str = "[\n" + +" {\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\": {\n" + +" \"attr02-01\":\"value-02-01\",\n" + +" \"attr02-02\":\"value-02-02\"\n" + +" }\n" + +" },\n" + +" {\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\": {\n" + +" \"attr02-01\":\"value-02-01\",\n" + +" \"attr02-01\":\"value-02-02\"\n" + +" }\n" + +" }\n" + + "]"; + System.out.println(str); + new JSONArray(str); + fail("Expected an exception"); + } catch (JSONException e) { + assertEquals("Expecting an expection message", + "Duplicate key \"attr02-01\" at 269 [character 24 line 13]", + e.getMessage()); + } } /** From 1acb18091a7006fd8a113c474a912cfd4cc6a375 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 9 Aug 2017 21:57:10 -0400 Subject: [PATCH 277/315] Remove System.out.println --- src/test/java/org/json/junit/JSONObjectTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 35cf493e6..6470ebb53 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2138,7 +2138,6 @@ public void jsonObjectParsingErrors() { +" }\n" +" }\n" + "]"; - System.out.println(str); new JSONArray(str); fail("Expected an exception"); } catch (JSONException e) { From df466db7b9630e689813dd82de5f512ca0713273 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 9 Aug 2017 21:59:08 -0400 Subject: [PATCH 278/315] Replacing tabs with 4 spaces --- .../java/org/json/junit/JSONObjectTest.java | 290 +++++++++--------- 1 file changed, 145 insertions(+), 145 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 6470ebb53..2dcebe408 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1977,173 +1977,173 @@ public void jsonObjectParsingErrors() { assertTrue("", true); } try { - // test exception message when including a duplicate key (level 0) - String str = "{\n" - +" \"attr01\":\"value-01\",\n" - +" \"attr02\":\"value-02\",\n" - +" \"attr03\":\"value-03\",\n" - +" \"attr03\":\"value-04\"\n" - + "}"; - new JSONObject(str); - fail("Expected an exception"); + // test exception message when including a duplicate key (level 0) + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr03\":\"value-04\"\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); } catch (JSONException e) { - assertEquals("Expecting an expection message", - "Duplicate key \"attr03\" at 90 [character 13 line 5]", - e.getMessage()); + assertEquals("Expecting an expection message", + "Duplicate key \"attr03\" at 90 [character 13 line 5]", + e.getMessage()); } try { - // test exception message when including a duplicate key (level 0) holding an object - String str = "{\n" - +" \"attr01\":\"value-01\",\n" - +" \"attr02\":\"value-02\",\n" - +" \"attr03\":\"value-03\",\n" - +" \"attr03\": {" - +" \"attr04-01\":\"value-04-01\",n" - +" \"attr04-02\":\"value-04-02\",n" - +" \"attr04-03\":\"value-04-03\"n" - + " }\n" - + "}"; - new JSONObject(str); - fail("Expected an exception"); + // test exception message when including a duplicate key (level 0) holding an object + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr03\": {" + +" \"attr04-01\":\"value-04-01\",n" + +" \"attr04-02\":\"value-04-02\",n" + +" \"attr04-03\":\"value-04-03\"n" + + " }\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); } catch (JSONException e) { - assertEquals("Expecting an expection message", - "Duplicate key \"attr03\" at 90 [character 13 line 5]", - e.getMessage()); + assertEquals("Expecting an expection message", + "Duplicate key \"attr03\" at 90 [character 13 line 5]", + e.getMessage()); } try { - // test exception message when including a duplicate key (level 0) holding an array - String str = "{\n" - +" \"attr01\":\"value-01\",\n" - +" \"attr02\":\"value-02\",\n" - +" \"attr03\":\"value-03\",\n" - +" \"attr03\": [\n" - +" {" - +" \"attr04-01\":\"value-04-01\",n" - +" \"attr04-02\":\"value-04-02\",n" - +" \"attr04-03\":\"value-04-03\"n" - +" }\n" - + " ]\n" - + "}"; - new JSONObject(str); - fail("Expected an exception"); + // test exception message when including a duplicate key (level 0) holding an array + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr03\": [\n" + +" {" + +" \"attr04-01\":\"value-04-01\",n" + +" \"attr04-02\":\"value-04-02\",n" + +" \"attr04-03\":\"value-04-03\"n" + +" }\n" + + " ]\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); } catch (JSONException e) { - assertEquals("Expecting an expection message", - "Duplicate key \"attr03\" at 90 [character 13 line 5]", - e.getMessage()); + assertEquals("Expecting an expection message", + "Duplicate key \"attr03\" at 90 [character 13 line 5]", + e.getMessage()); } try { - // test exception message when including a duplicate key (level 1) - String str = "{\n" - +" \"attr01\":\"value-01\",\n" - +" \"attr02\":\"value-02\",\n" - +" \"attr03\":\"value-03\",\n" - +" \"attr04\": {\n" - +" \"attr04-01\":\"value04-01\",\n" - +" \"attr04-02\":\"value04-02\",\n" - +" \"attr04-03\":\"value04-03\",\n" - +" \"attr04-03\":\"value04-04\"\n" - + " }\n" - + "}"; - new JSONObject(str); - fail("Expected an exception"); + // test exception message when including a duplicate key (level 1) + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr04\": {\n" + +" \"attr04-01\":\"value04-01\",\n" + +" \"attr04-02\":\"value04-02\",\n" + +" \"attr04-03\":\"value04-03\",\n" + +" \"attr04-03\":\"value04-04\"\n" + + " }\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); } catch (JSONException e) { - assertEquals("Expecting an expection message", - "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", - e.getMessage()); + assertEquals("Expecting an expection message", + "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", + e.getMessage()); } try { - // test exception message when including a duplicate key (level 1) holding an object - String str = "{\n" - +" \"attr01\":\"value-01\",\n" - +" \"attr02\":\"value-02\",\n" - +" \"attr03\":\"value-03\",\n" - +" \"attr04\": {\n" - +" \"attr04-01\":\"value04-01\",\n" - +" \"attr04-02\":\"value04-02\",\n" - +" \"attr04-03\":\"value04-03\",\n" - +" \"attr04-03\": {\n" - +" \"attr04-04-01\":\"value04-04-01\",\n" - +" \"attr04-04-02\":\"value04-04-02\",\n" - +" \"attr04-04-03\":\"value04-04-03\",\n" - +" }\n" - +" }\n" - + "}"; - new JSONObject(str); - fail("Expected an exception"); + // test exception message when including a duplicate key (level 1) holding an object + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr04\": {\n" + +" \"attr04-01\":\"value04-01\",\n" + +" \"attr04-02\":\"value04-02\",\n" + +" \"attr04-03\":\"value04-03\",\n" + +" \"attr04-03\": {\n" + +" \"attr04-04-01\":\"value04-04-01\",\n" + +" \"attr04-04-02\":\"value04-04-02\",\n" + +" \"attr04-04-03\":\"value04-04-03\",\n" + +" }\n" + +" }\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); } catch (JSONException e) { - assertEquals("Expecting an expection message", - "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", - e.getMessage()); + assertEquals("Expecting an expection message", + "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", + e.getMessage()); } try { - // test exception message when including a duplicate key (level 1) holding an array - String str = "{\n" - +" \"attr01\":\"value-01\",\n" - +" \"attr02\":\"value-02\",\n" - +" \"attr03\":\"value-03\",\n" - +" \"attr04\": {\n" - +" \"attr04-01\":\"value04-01\",\n" - +" \"attr04-02\":\"value04-02\",\n" - +" \"attr04-03\":\"value04-03\",\n" - +" \"attr04-03\": [\n" - +" {\n" - +" \"attr04-04-01\":\"value04-04-01\",\n" - +" \"attr04-04-02\":\"value04-04-02\",\n" - +" \"attr04-04-03\":\"value04-04-03\",\n" - +" }\n" - +" ]\n" - +" }\n" - + "}"; - new JSONObject(str); - fail("Expected an exception"); + // test exception message when including a duplicate key (level 1) holding an array + String str = "{\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\",\n" + +" \"attr03\":\"value-03\",\n" + +" \"attr04\": {\n" + +" \"attr04-01\":\"value04-01\",\n" + +" \"attr04-02\":\"value04-02\",\n" + +" \"attr04-03\":\"value04-03\",\n" + +" \"attr04-03\": [\n" + +" {\n" + +" \"attr04-04-01\":\"value04-04-01\",\n" + +" \"attr04-04-02\":\"value04-04-02\",\n" + +" \"attr04-04-03\":\"value04-04-03\",\n" + +" }\n" + +" ]\n" + +" }\n" + + "}"; + new JSONObject(str); + fail("Expected an exception"); } catch (JSONException e) { - assertEquals("Expecting an expection message", - "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", - e.getMessage()); + assertEquals("Expecting an expection message", + "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", + e.getMessage()); } try { - // test exception message when including a duplicate key in object (level 0) within an array - String str = "[\n" - +" {\n" - +" \"attr01\":\"value-01\",\n" - +" \"attr02\":\"value-02\"\n" - +" },\n" - +" {\n" - +" \"attr01\":\"value-01\",\n" - +" \"attr01\":\"value-02\"\n" - +" }\n" - + "]"; - new JSONArray(str); - fail("Expected an exception"); + // test exception message when including a duplicate key in object (level 0) within an array + String str = "[\n" + +" {\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\":\"value-02\"\n" + +" },\n" + +" {\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr01\":\"value-02\"\n" + +" }\n" + + "]"; + new JSONArray(str); + fail("Expected an exception"); } catch (JSONException e) { - assertEquals("Expecting an expection message", - "Duplicate key \"attr01\" at 124 [character 17 line 8]", - e.getMessage()); + assertEquals("Expecting an expection message", + "Duplicate key \"attr01\" at 124 [character 17 line 8]", + e.getMessage()); } try { - // test exception message when including a duplicate key in object (level 1) within an array - String str = "[\n" - +" {\n" - +" \"attr01\":\"value-01\",\n" - +" \"attr02\": {\n" - +" \"attr02-01\":\"value-02-01\",\n" - +" \"attr02-02\":\"value-02-02\"\n" - +" }\n" - +" },\n" - +" {\n" - +" \"attr01\":\"value-01\",\n" - +" \"attr02\": {\n" - +" \"attr02-01\":\"value-02-01\",\n" - +" \"attr02-01\":\"value-02-02\"\n" - +" }\n" - +" }\n" - + "]"; - new JSONArray(str); - fail("Expected an exception"); + // test exception message when including a duplicate key in object (level 1) within an array + String str = "[\n" + +" {\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\": {\n" + +" \"attr02-01\":\"value-02-01\",\n" + +" \"attr02-02\":\"value-02-02\"\n" + +" }\n" + +" },\n" + +" {\n" + +" \"attr01\":\"value-01\",\n" + +" \"attr02\": {\n" + +" \"attr02-01\":\"value-02-01\",\n" + +" \"attr02-01\":\"value-02-02\"\n" + +" }\n" + +" }\n" + + "]"; + new JSONArray(str); + fail("Expected an exception"); } catch (JSONException e) { - assertEquals("Expecting an expection message", - "Duplicate key \"attr02-01\" at 269 [character 24 line 13]", - e.getMessage()); + assertEquals("Expecting an expection message", + "Duplicate key \"attr02-01\" at 269 [character 24 line 13]", + e.getMessage()); } } From c365e2a774693a8bdf8b19c6ddcd039678856ed4 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 9 Aug 2017 22:03:09 -0400 Subject: [PATCH 279/315] Remov slf4j reference --- build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/build.gradle b/build.gradle index 53fbdb35e..43656aee4 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,6 @@ dependencies { testCompile group: 'junit', name: 'junit', version: '4.+' testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0' testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5' - testCompile group: 'slf4j.org', name: 'slf4j', version: '1.6.1' // Uncomment if you are testing against a JSON-Java release // testCompile 'org.json:json:20160212' // Uncomment if you have copied a local JSON-Java jar file into this project From 68b262914df7b3a98b2d970edb1adc9b72fed4fb Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 10 Aug 2017 19:06:55 -0400 Subject: [PATCH 280/315] JSONObject(JSONTokener) now points to last character of duplicate key Updating exception message accordingly (position -1) --- src/test/java/org/json/junit/JSONObjectTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 2dcebe408..5d49daa5b 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1988,7 +1988,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr03\" at 90 [character 13 line 5]", + "Duplicate key \"attr03\" at 89 [character 12 line 5]", e.getMessage()); } try { @@ -2007,7 +2007,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr03\" at 90 [character 13 line 5]", + "Duplicate key \"attr03\" at 89 [character 12 line 5]", e.getMessage()); } try { @@ -2028,7 +2028,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr03\" at 90 [character 13 line 5]", + "Duplicate key \"attr03\" at 89 [character 12 line 5]", e.getMessage()); } try { @@ -2048,7 +2048,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", + "Duplicate key \"attr04-03\" at 214 [character 19 line 9]", e.getMessage()); } try { @@ -2072,7 +2072,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", + "Duplicate key \"attr04-03\" at 214 [character 19 line 9]", e.getMessage()); } try { @@ -2098,7 +2098,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", + "Duplicate key \"attr04-03\" at 214 [character 19 line 9]", e.getMessage()); } try { @@ -2117,7 +2117,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr01\" at 124 [character 17 line 8]", + "Duplicate key \"attr01\" at 123 [character 16 line 8]", e.getMessage()); } try { @@ -2142,7 +2142,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr02-01\" at 269 [character 24 line 13]", + "Duplicate key \"attr02-01\" at 268 [character 23 line 13]", e.getMessage()); } } From b90bee0f225e2502c8b62d494f571fd27ce5bea4 Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 14 Aug 2017 13:05:23 -0400 Subject: [PATCH 281/315] Update error message location (+1) `JSONTokener.back()` call removed from `JSONObject(JSONTokener)` constructor. --- src/test/java/org/json/junit/JSONObjectTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 5d49daa5b..2dcebe408 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1988,7 +1988,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr03\" at 89 [character 12 line 5]", + "Duplicate key \"attr03\" at 90 [character 13 line 5]", e.getMessage()); } try { @@ -2007,7 +2007,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr03\" at 89 [character 12 line 5]", + "Duplicate key \"attr03\" at 90 [character 13 line 5]", e.getMessage()); } try { @@ -2028,7 +2028,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr03\" at 89 [character 12 line 5]", + "Duplicate key \"attr03\" at 90 [character 13 line 5]", e.getMessage()); } try { @@ -2048,7 +2048,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr04-03\" at 214 [character 19 line 9]", + "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", e.getMessage()); } try { @@ -2072,7 +2072,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr04-03\" at 214 [character 19 line 9]", + "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", e.getMessage()); } try { @@ -2098,7 +2098,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr04-03\" at 214 [character 19 line 9]", + "Duplicate key \"attr04-03\" at 215 [character 20 line 9]", e.getMessage()); } try { @@ -2117,7 +2117,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr01\" at 123 [character 16 line 8]", + "Duplicate key \"attr01\" at 124 [character 17 line 8]", e.getMessage()); } try { @@ -2142,7 +2142,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an expection message", - "Duplicate key \"attr02-01\" at 268 [character 23 line 13]", + "Duplicate key \"attr02-01\" at 269 [character 24 line 13]", e.getMessage()); } } From cb61bbf720253b2d93832544b189d0a78501c604 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Sat, 19 Aug 2017 18:19:22 -0400 Subject: [PATCH 282/315] New tests for XML unescaping --- src/test/java/org/json/junit/XMLTest.java | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 244c9e932..5daca5ccd 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -764,5 +764,29 @@ public void testToJsonXML() { assertEquals(expectedReverseXml, reverseXml); } + + /** + * test to validate certain conditions of XML unescaping. + */ + @Test + public void testUnescape() { + assertEquals("{\"xml\":\"Can cope <;\"}", + XML.toJSONObject("Can cope <; ").toString()); + assertEquals("Can cope <; ", XML.unescape("Can cope <; ")); + + assertEquals("{\"xml\":\"Can cope & ;\"}", + XML.toJSONObject("Can cope & ; ").toString()); + assertEquals("Can cope & ; ", XML.unescape("Can cope & ; ")); + + assertEquals("{\"xml\":\"Can cope &;\"}", + XML.toJSONObject("Can cope &; ").toString()); + assertEquals("Can cope &; ", XML.unescape("Can cope &; ")); + + // double escaped + assertEquals("{\"xml\":\"Can cope <\"}", + XML.toJSONObject("Can cope &lt; ").toString()); + assertEquals("Can cope < ", XML.unescape("Can cope &lt; ")); + + } } \ No newline at end of file From 2713f2e2a4a5fa93cdf47b4f926ff5ea088abe68 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Sat, 19 Aug 2017 18:45:53 -0400 Subject: [PATCH 283/315] Adds testing for unicode entities --- src/test/java/org/json/junit/XMLTest.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 5daca5ccd..c34bf0f85 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -782,11 +782,20 @@ public void testUnescape() { XML.toJSONObject("Can cope &; ").toString()); assertEquals("Can cope &; ", XML.unescape("Can cope &; ")); + // unicode entity + assertEquals("{\"xml\":\"Can cope 4;\"}", + XML.toJSONObject("Can cope 4; ").toString()); + assertEquals("Can cope 4; ", XML.unescape("Can cope 4; ")); + // double escaped assertEquals("{\"xml\":\"Can cope <\"}", XML.toJSONObject("Can cope &lt; ").toString()); assertEquals("Can cope < ", XML.unescape("Can cope &lt; ")); - } + assertEquals("{\"xml\":\"Can cope 4\"}", + XML.toJSONObject("Can cope &#x34; ").toString()); + assertEquals("Can cope 4 ", XML.unescape("Can cope &#x34; ")); + + } } \ No newline at end of file From 52ecc8970237e50f52632b42f4b30ef98e565b51 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 30 Oct 2017 07:27:42 -0400 Subject: [PATCH 284/315] New test to verify unclosed array --- src/test/java/org/json/junit/JSONArrayTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 0df7c5d52..1c147740a 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -79,6 +79,21 @@ public void emptStr() { e.getMessage()); } } + + /** + * Attempt to create a JSONArray with an unclosed array. + * Expects an exception + */ + @Test + public void unclosedArray() { + try { + assertNull("Should throw an exception", new JSONArray("[")); + } catch (JSONException e) { + assertEquals("Expected an exception message", + "A JSONArray text must start with '[' at 0 [character 1 line 1]", + e.getMessage()); + } + } /** * Attempt to create a JSONArray with a string as object that is From bde6ba1c52b1393526a24d51b04a2295e786ebfa Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 30 Oct 2017 07:41:11 -0400 Subject: [PATCH 285/315] Updates exception expected message --- src/test/java/org/json/junit/JSONArrayTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 1c147740a..442fb0ab9 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -90,7 +90,7 @@ public void unclosedArray() { assertNull("Should throw an exception", new JSONArray("[")); } catch (JSONException e) { assertEquals("Expected an exception message", - "A JSONArray text must start with '[' at 0 [character 1 line 1]", + "Expected a ',' or ']' at 1 [character 2 line 1]", e.getMessage()); } } From dfa37a298f019516224fe1b563a4fec66253e103 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 30 Oct 2017 08:09:42 -0400 Subject: [PATCH 286/315] Add more tests for unclosed arrays --- .../java/org/json/junit/JSONArrayTest.java | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 442fb0ab9..4a0249de6 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -30,7 +30,7 @@ * Tests for JSON-Java JSONArray.java */ public class JSONArrayTest { - String arrayStr = + private final String arrayStr = "["+ "true,"+ "false,"+ @@ -94,6 +94,36 @@ public void unclosedArray() { e.getMessage()); } } + + /** + * Attempt to create a JSONArray with an unclosed array. + * Expects an exception + */ + @Test + public void unclosedArray2() { + try { + assertNull("Should throw an exception", new JSONArray("[\"test\"")); + } catch (JSONException e) { + assertEquals("Expected an exception message", + "Expected a ',' or ']' at 7 [character 8 line 1]", + e.getMessage()); + } + } + + /** + * Attempt to create a JSONArray with an unclosed array. + * Expects an exception + */ + @Test + public void unclosedArray3() { + try { + assertNull("Should throw an exception", new JSONArray("[\"test\",")); + } catch (JSONException e) { + assertEquals("Expected an exception message", + "Expected a ',' or ']' at 8 [character 9 line 1]", + e.getMessage()); + } + } /** * Attempt to create a JSONArray with a string as object that is @@ -372,7 +402,7 @@ public void length() { assertTrue("expected empty JSONArray length 0", new JSONArray().length() == 0); JSONArray jsonArray = new JSONArray(this.arrayStr); - assertTrue("expected JSONArray length 13", jsonArray.length() == 13); + assertTrue("expected JSONArray length 13. instead found "+jsonArray.length(), jsonArray.length() == 13); JSONArray nestedJsonArray = jsonArray.getJSONArray(9); assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1); } From 578a442ef764691db51f2a0e880af9c86b675409 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 30 Oct 2017 08:43:56 -0500 Subject: [PATCH 287/315] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 79f872442..5f83c6cbc 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,11 @@ When adding a new unit test, don't forget to update JunitTestSuite.java. * If you have unit test results along with pull requests, the reviewer has an easier time understanding your code and determining if the it works as intended. When you start working on a test, add the empty file to the repository and update the readme, so that others will know that test is taken. +**Caveats:** +JSON-Java is Java 1.6-compatible, but JSON-Java-unit-tests requests Java 1.8. If you see this error, make sure you have 1.8 installed, on your path, and set in JAVA_HOME: +Execution failed for task ':compileJava'. +> invalid flag: -parameters + A unit test has the following stages: From ee3aa03da156e4f8d03eb40cf86c11810f2ac120 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 30 Oct 2017 08:44:21 -0500 Subject: [PATCH 288/315] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5f83c6cbc..86dd85cce 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ When adding a new unit test, don't forget to update JunitTestSuite.java. * If you have unit test results along with pull requests, the reviewer has an easier time understanding your code and determining if the it works as intended. When you start working on a test, add the empty file to the repository and update the readme, so that others will know that test is taken. + **Caveats:** JSON-Java is Java 1.6-compatible, but JSON-Java-unit-tests requests Java 1.8. If you see this error, make sure you have 1.8 installed, on your path, and set in JAVA_HOME: Execution failed for task ':compileJava'. From e0801befe5981dfd2b93b04c4980d0fcf73c46cf Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 30 Oct 2017 08:45:41 -0500 Subject: [PATCH 289/315] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 86dd85cce..07f944523 100644 --- a/README.md +++ b/README.md @@ -84,9 +84,10 @@ When you start working on a test, add the empty file to the repository and updat **Caveats:** JSON-Java is Java 1.6-compatible, but JSON-Java-unit-tests requests Java 1.8. If you see this error, make sure you have 1.8 installed, on your path, and set in JAVA_HOME: +``` Execution failed for task ':compileJava'. > invalid flag: -parameters - +``` A unit test has the following stages: From 936db93445a9481533494f0333738f1fc5198a7e Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 30 Oct 2017 08:46:43 -0500 Subject: [PATCH 290/315] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 07f944523..1ca5c64a5 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ When adding a new unit test, don't forget to update JunitTestSuite.java. When you start working on a test, add the empty file to the repository and update the readme, so that others will know that test is taken. **Caveats:** -JSON-Java is Java 1.6-compatible, but JSON-Java-unit-tests requests Java 1.8. If you see this error, make sure you have 1.8 installed, on your path, and set in JAVA_HOME: +JSON-Java is Java 1.6-compatible, but JSON-Java-unit-tests requires Java 1.8. If you see this error when building JSON-Java-unit-test, make sure you have 1.8 installed, on your path, and set in JAVA_HOME: ``` Execution failed for task ':compileJava'. > invalid flag: -parameters From 08d93f3eb5db5e3bde58fe3ca1da9da2acd4f1ed Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 6 Nov 2017 10:27:45 -0500 Subject: [PATCH 291/315] test cases for issue https://github.com/stleary/JSON-java/issues/379 --- .../java/org/json/junit/JSONArrayTest.java | 27 +++++++++++++++++++ .../java/org/json/junit/JSONObjectTest.java | 27 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 4a0249de6..48050b94f 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -1,6 +1,7 @@ package org.json.junit; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -54,6 +55,32 @@ public class JSONArrayTest { "\"-1\""+ "]"; + /** + * Tests that the similar method is working as expected. + */ + @Test + public void aaaVerifySimilar() { + final String string1 = "HasSameRef"; + JSONArray obj1 = new JSONArray() + .put("abc") + .put(string1) + .put(2); + + JSONArray obj2 = new JSONArray() + .put("abc") + .put(string1) + .put(3); + + JSONArray obj3 = new JSONArray() + .put("abc") + .put(new String(string1)) + .put(2); + + assertFalse("Should eval to false", obj1.similar(obj2)); + + assertTrue("Should eval to true", obj1.similar(obj3)); + } + /** * Attempt to create a JSONArray with a null string. * Expects a NullPointerException. diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 2dcebe408..6b248ad81 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -58,6 +58,33 @@ * otherwise be impossible. */ public class JSONObjectTest { + + /** + * Tests that the similar method is working as expected. + */ + @Test + public void aaaVerifySimilar() { + final String string1 = "HasSameRef"; + JSONObject obj1 = new JSONObject() + .put("key1", "abc") + .put("key2", 2) + .put("key3", string1); + + JSONObject obj2 = new JSONObject() + .put("key1", "abc") + .put("key2", 3) + .put("key3", string1); + + JSONObject obj3 = new JSONObject() + .put("key1", "abc") + .put("key2", 2) + .put("key3", new String(string1)); + + assertFalse("Should eval to false", obj1.similar(obj2)); + + assertTrue("Should eval to true", obj1.similar(obj3)); + + } /** * JSONObject built from a bean, but only using a null value. From dae88d7c5cbf5569a28c5653d4d6d354d1fc2ce6 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 6 Nov 2017 10:35:49 -0500 Subject: [PATCH 292/315] fix method names --- src/test/java/org/json/junit/JSONArrayTest.java | 2 +- src/test/java/org/json/junit/JSONObjectTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 48050b94f..8a31c877b 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -59,7 +59,7 @@ public class JSONArrayTest { * Tests that the similar method is working as expected. */ @Test - public void aaaVerifySimilar() { + public void verifySimilar() { final String string1 = "HasSameRef"; JSONArray obj1 = new JSONArray() .put("abc") diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 6b248ad81..85c37fd12 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -63,7 +63,7 @@ public class JSONObjectTest { * Tests that the similar method is working as expected. */ @Test - public void aaaVerifySimilar() { + public void verifySimilar() { final String string1 = "HasSameRef"; JSONObject obj1 = new JSONObject() .put("key1", "abc") From cc2ed79e57dea53bb21418daaf3bbf39a68b1bed Mon Sep 17 00:00:00 2001 From: dengjianbao Date: Fri, 2 Feb 2018 22:54:08 +0800 Subject: [PATCH 293/315] Correct the message to match the function --- src/test/java/org/json/junit/JSONObjectTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 85c37fd12..80283ae27 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -266,7 +266,7 @@ public void verifyNumberOutput(){ JSONObject jsonObject = new JSONObject(new MyNumberContainer()); String actual = jsonObject.toString(); String expected = "{\"myNumber\":{\"number\":42}}"; - assertEquals("Not Equal", expected , actual); + assertEquals("Equal", expected , actual); /** * JSONObject.put() handles objects differently than the @@ -280,7 +280,7 @@ public void verifyNumberOutput(){ jsonObject.put("myNumber", new MyNumber()); actual = jsonObject.toString(); expected = "{\"myNumber\":42}"; - assertEquals("Not Equal", expected , actual); + assertEquals("Equal", expected , actual); /** * Calls the JSONObject(Map) ctor, which calls wrap() for values. @@ -293,7 +293,7 @@ public void verifyNumberOutput(){ jsonObject = new JSONObject(Collections.singletonMap("myNumber", new AtomicInteger(42))); actual = jsonObject.toString(); expected = "{\"myNumber\":\"42\"}"; - assertEquals("Not Equal", expected , actual); + assertEquals("Equal", expected , actual); /** * JSONObject.put() inserts the AtomicInteger directly into the @@ -305,7 +305,7 @@ public void verifyNumberOutput(){ jsonObject.put("myNumber", new AtomicInteger(42)); actual = jsonObject.toString(); expected = "{\"myNumber\":42}"; - assertEquals("Not Equal", expected , actual); + assertEquals("Equal", expected , actual); /** * Calls the JSONObject(Map) ctor, which calls wrap() for values. @@ -332,7 +332,7 @@ public void verifyNumberOutput(){ jsonObject.put("myNumber", new Fraction(4,2)); actual = jsonObject.toString(); expected = "{\"myNumber\":\"4/2\"}"; // valid JSON, bug fixed - assertEquals("Not Equal", expected , actual); + assertEquals("Equal", expected , actual); } /** From aa5e80bc8d694bf8a552401534d93d8938eaa665 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 7 Mar 2018 12:11:17 -0500 Subject: [PATCH 294/315] add test cases for null keys --- .../java/org/json/junit/JSONObjectTest.java | 72 ++++++++++++++++--- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 80283ae27..5ada98cea 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1973,13 +1973,7 @@ public void jsonObjectParsingErrors() { "Null pointer", e.getMessage()); } - try { - // null put key - JSONObject jsonObject = new JSONObject("{}"); - jsonObject.put(null, 0); - fail("Expected an exception"); - } catch (NullPointerException ignored) { - } + try { // multiple putOnce key JSONObject jsonObject = new JSONObject("{}"); @@ -2182,6 +2176,10 @@ public void jsonObjectPutOnceNull() { JSONObject jsonObject = new JSONObject(); jsonObject.putOnce(null, null); assertTrue("jsonObject should be empty", jsonObject.length() == 0); + jsonObject.putOnce("", null); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + jsonObject.putOnce(null, ""); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); } /** @@ -2453,7 +2451,7 @@ public void write() throws IOException { * Confirms that exceptions thrown when writing values are wrapped properly. */ @Test - public void testJSONWriterException() throws IOException { + public void testJSONWriterException() { final JSONObject jsonObject = new JSONObject(); jsonObject.put("someKey",new BrokenToString()); @@ -2893,4 +2891,62 @@ public void testExceptionalBean() { assertTrue(jo.get("closeable") instanceof JSONObject); assertTrue(jo.getJSONObject("closeable").has("string")); } + + @Test(expected=NullPointerException.class) + public void testPutNullBoolean() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, false); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullCollection() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, Collections.emptySet()); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullDouble() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0.0d); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullFloat() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0.0f); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullInt() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullLong() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, 0L); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullMap() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, Collections.emptyMap()); + fail("Expected an exception"); + } + @Test(expected=NullPointerException.class) + public void testPutNullObject() { + // null put key + JSONObject jsonObject = new JSONObject("{}"); + jsonObject.put(null, new Object()); + fail("Expected an exception"); + } + } From 193a3823b53ad6684987671ee3da54f6d9a2705b Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 7 Mar 2018 14:52:50 -0500 Subject: [PATCH 295/315] new test cases to support bean annotation --- .../java/org/json/junit/JSONObjectTest.java | 77 ++++++++++++++++++- .../java/org/json/junit/data/GenericBean.java | 2 +- .../org/json/junit/data/GenericBeanInt.java | 29 ++++++- .../org/json/junit/data/MyBeanCustomName.java | 20 +++++ .../junit/data/MyBeanCustomNameInterface.java | 11 +++ .../junit/data/MyBeanCustomNameSubClass.java | 32 ++++++++ 6 files changed, 165 insertions(+), 6 deletions(-) create mode 100644 src/test/java/org/json/junit/data/MyBeanCustomName.java create mode 100644 src/test/java/org/json/junit/data/MyBeanCustomNameInterface.java create mode 100644 src/test/java/org/json/junit/data/MyBeanCustomNameSubClass.java diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 80283ae27..0579acbde 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -37,6 +37,8 @@ import org.json.junit.data.GenericBean; import org.json.junit.data.GenericBeanInt; import org.json.junit.data.MyBean; +import org.json.junit.data.MyBeanCustomName; +import org.json.junit.data.MyBeanCustomNameSubClass; import org.json.junit.data.MyBigNumberBean; import org.json.junit.data.MyEnum; import org.json.junit.data.MyEnumField; @@ -371,7 +373,7 @@ public void verifyPutCollection() { /** - * Verifies that the put Map has backwards compatability with RAW types pre-java5. + * Verifies that the put Map has backwards compatibility with RAW types pre-java5. */ @Test public void verifyPutMap() { @@ -467,7 +469,7 @@ public void jsonObjectByMapWithNullValue() { */ @SuppressWarnings("boxing") @Test - public void jsonObjectByBean() { + public void jsonObjectByBean1() { /** * Default access classes have to be mocked since JSONObject, which is * not in the same package, cannot call MyBean methods by reflection. @@ -501,6 +503,73 @@ public void jsonObjectByBean() { assertTrue("expected 0 callbacks[1] items", ((Map)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0); } + /** + * JSONObject built from a bean that has custom field names. + */ + @Test + public void jsonObjectByBean2() { + JSONObject jsonObject = new JSONObject(new MyBeanCustomName()); + assertNotNull(jsonObject); + assertEquals("Wrong number of keys found:", + 5, + jsonObject.keySet().size()); + assertFalse("Normal field name (someString) processing did not work", + jsonObject.has("someString")); + assertFalse("Normal field name (myDouble) processing did not work", + jsonObject.has("myDouble")); + assertFalse("Normal field name (someFloat) found", + jsonObject.has("someFloat")); + assertFalse("Ignored field found!", + jsonObject.has("ignoredInt")); + assertTrue("Normal field name (someInt) processing did not work", + jsonObject.has("someInt")); + assertTrue("Normal field name (someLong) processing did not work", + jsonObject.has("someLong")); + assertTrue("Overridden String field name (myStringField) not found", + jsonObject.has("myStringField")); + assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not found", + jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); + assertTrue("Overridden String field name (InterfaceField) not found", + jsonObject.has("InterfaceField")); + } + + /** + * JSONObject built from a bean that has custom field names inherited from a parent class. + */ + @Test + public void jsonObjectByBean3() { + JSONObject jsonObject = new JSONObject(new MyBeanCustomNameSubClass()); + assertNotNull(jsonObject); + assertEquals("Wrong number of keys found:", + 7, + jsonObject.keySet().size()); + assertFalse("Normal int field name (someInt) found, but was overridden", + jsonObject.has("someInt")); + assertFalse("Normal field name (myDouble) processing did not work", + jsonObject.has("myDouble")); + assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) FOUND!", + jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); + assertFalse("Normal field name (someFloat) found", + jsonObject.has("someFloat")); + assertFalse("Ignored field found!", + jsonObject.has("ignoredInt")); + assertFalse("Ignored field at the same level as forced name found", + jsonObject.has("ShouldBeIgnored")); + assertTrue("Overridden int field name (newIntFieldName) not found", + jsonObject.has("newIntFieldName")); + assertTrue("Normal field name (someLong) processing did not work", + jsonObject.has("someLong")); + assertTrue("Overridden String field name (myStringField) not found", + jsonObject.has("myStringField")); + assertTrue(jsonObject.has("AMoreNormalName")); + assertTrue("Overridden String field name (InterfaceField) not found", + jsonObject.has("InterfaceField")); + assertTrue("Forced field not found!", + jsonObject.has("forcedInt")); + assertTrue("Normally ignored field (getable) with explicit property name not found", + jsonObject.has("Getable")); + } + /** * A bean is also an object. But in order to test the JSONObject * ctor that takes an object and a list of names, @@ -541,7 +610,7 @@ public void jsonObjectByResourceBundle() { assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later"))); assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator"))); } - + /** * Exercise the JSONObject.accumulate() method */ @@ -2857,7 +2926,7 @@ public void testGenericBean() { public void testGenericIntBean() { GenericBeanInt bean = new GenericBeanInt(42); final JSONObject jo = new JSONObject(bean); - assertEquals(jo.keySet().toString(), 9, jo.length()); + assertEquals(jo.keySet().toString(), 10, jo.length()); assertEquals(42, jo.get("genericValue")); assertEquals("Expected the getter to only be called once", 1, bean.genericGetCounter); diff --git a/src/test/java/org/json/junit/data/GenericBean.java b/src/test/java/org/json/junit/data/GenericBean.java index 474003034..da6370d48 100644 --- a/src/test/java/org/json/junit/data/GenericBean.java +++ b/src/test/java/org/json/junit/data/GenericBean.java @@ -20,7 +20,7 @@ public GenericBean(T genericValue) { } /** */ - private T genericValue; + protected T genericValue; /** to be used by the calling test to see how often the getter is called */ public int genericGetCounter; /** to be used by the calling test to see how often the setter is called */ diff --git a/src/test/java/org/json/junit/data/GenericBeanInt.java b/src/test/java/org/json/junit/data/GenericBeanInt.java index 8f0248d8c..505661162 100644 --- a/src/test/java/org/json/junit/data/GenericBeanInt.java +++ b/src/test/java/org/json/junit/data/GenericBeanInt.java @@ -13,7 +13,7 @@ public class GenericBeanInt extends GenericBean { /** @return the a */ public char getA() { - return a; + return this.a; } /** @@ -25,6 +25,33 @@ public boolean getable() { return false; } + /** + * Should not be beanable + * + * @return false + */ + public boolean get() { + return false; + } + + /** + * Should not be beanable + * + * @return false + */ + public boolean is() { + return false; + } + + /** + * Should be beanable + * + * @return false + */ + public boolean isB() { + return this.genericValue.equals((Integer.valueOf(this.a+1))); + } + /** * @param genericValue * the value to initiate with. diff --git a/src/test/java/org/json/junit/data/MyBeanCustomName.java b/src/test/java/org/json/junit/data/MyBeanCustomName.java new file mode 100644 index 000000000..56756c2b3 --- /dev/null +++ b/src/test/java/org/json/junit/data/MyBeanCustomName.java @@ -0,0 +1,20 @@ +package org.json.junit.data; + +import org.json.JSONPropertyName; + +/** + * Test bean for the {@link JSONPropertyName} annotation. + */ +public class MyBeanCustomName implements MyBeanCustomNameInterface { + public int getSomeInt() { return 42; } + @JSONPropertyName("") + public long getSomeLong() { return 42L; } + @JSONPropertyName("myStringField") + public String getSomeString() { return "someStringValue"; } + @JSONPropertyName("Some Weird NAme that Normally Wouldn't be possible!") + public double getMyDouble() { return 0.0d; } + @Override + public float getSomeFloat() { return 2.0f; } + @Override + public int getIgnoredInt() { return 40; } +} diff --git a/src/test/java/org/json/junit/data/MyBeanCustomNameInterface.java b/src/test/java/org/json/junit/data/MyBeanCustomNameInterface.java new file mode 100644 index 000000000..b25b57873 --- /dev/null +++ b/src/test/java/org/json/junit/data/MyBeanCustomNameInterface.java @@ -0,0 +1,11 @@ +package org.json.junit.data; + +import org.json.JSONPropertyIgnore; +import org.json.JSONPropertyName; + +public interface MyBeanCustomNameInterface { + @JSONPropertyName("InterfaceField") + float getSomeFloat(); + @JSONPropertyIgnore + int getIgnoredInt(); +} \ No newline at end of file diff --git a/src/test/java/org/json/junit/data/MyBeanCustomNameSubClass.java b/src/test/java/org/json/junit/data/MyBeanCustomNameSubClass.java new file mode 100644 index 000000000..8f0500cce --- /dev/null +++ b/src/test/java/org/json/junit/data/MyBeanCustomNameSubClass.java @@ -0,0 +1,32 @@ +/** + * + */ +package org.json.junit.data; + +import org.json.JSONPropertyIgnore; +import org.json.JSONPropertyName; + +/** + * Test bean to verify that the {@link org.json.JSONPropertyName} annotation + * is inherited. + */ +public class MyBeanCustomNameSubClass extends MyBeanCustomName { + @Override + @JSONPropertyName("forcedInt") + public int getIgnoredInt() { return 42*42; } + @Override + @JSONPropertyName("newIntFieldName") + public int getSomeInt() { return 43; } + @Override + public String getSomeString() { return "subClassString"; } + @Override + @JSONPropertyName("AMoreNormalName") + public double getMyDouble() { return 1.0d; } + @Override + public float getSomeFloat() { return 3.0f; } + @JSONPropertyIgnore + @JSONPropertyName("ShouldBeIgnored") + public boolean getShouldNotBeJSON() { return true; } + @JSONPropertyName("Getable") + public boolean getable() { return true; } +} From f4201cf318cff5fba56d1e1ef687ffb3f1a7d421 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 19 Mar 2018 09:34:13 -0400 Subject: [PATCH 296/315] Test cases for issue described in https://github.com/stleary/JSON-java/issues/410. --- src/test/java/org/json/junit/JSONPointerTest.java | 13 +++++++++++++ src/test/resources/jsonpointer-testdoc.json | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java index 19dac47ae..c4a878130 100644 --- a/src/test/java/org/json/junit/JSONPointerTest.java +++ b/src/test/java/org/json/junit/JSONPointerTest.java @@ -62,6 +62,19 @@ public void queryByEmptyKey() { assertSame(document.get(""), query("/")); } + @Test + public void queryByEmptyKeySubObject() { + assertSame(document.getJSONObject("obj").getJSONObject(""), query("/obj/")); + } + + @Test + public void queryByEmptyKeySubObjectSubOject() { + assertSame( + document.getJSONObject("obj").getJSONObject("").get(""), + query("/obj//") + ); + } + @Test public void slashEscaping() { assertSame(document.get("a/b"), query("/a~1b")); diff --git a/src/test/resources/jsonpointer-testdoc.json b/src/test/resources/jsonpointer-testdoc.json index d58fe8216..6c1ce28c3 100644 --- a/src/test/resources/jsonpointer-testdoc.json +++ b/src/test/resources/jsonpointer-testdoc.json @@ -19,6 +19,7 @@ "another/key" : [ "val" ] - } + }, + "" : { "" : "empty key of an object with an empty key" } } } \ No newline at end of file From 43f3f5e80bb845db09e34ce467c38052b08866cf Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 19 Mar 2018 09:48:50 -0400 Subject: [PATCH 297/315] Add another test --- src/test/java/org/json/junit/JSONPointerTest.java | 8 ++++++++ src/test/resources/jsonpointer-testdoc.json | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java index c4a878130..5ddd089c0 100644 --- a/src/test/java/org/json/junit/JSONPointerTest.java +++ b/src/test/java/org/json/junit/JSONPointerTest.java @@ -74,6 +74,14 @@ public void queryByEmptyKeySubObjectSubOject() { query("/obj//") ); } + + @Test + public void queryByEmptyKeySubObjectValue() { + assertSame( + document.getJSONObject("obj").getJSONObject("").get("subKey"), + query("/obj//subKey") + ); + } @Test public void slashEscaping() { diff --git a/src/test/resources/jsonpointer-testdoc.json b/src/test/resources/jsonpointer-testdoc.json index 6c1ce28c3..657ccdd34 100644 --- a/src/test/resources/jsonpointer-testdoc.json +++ b/src/test/resources/jsonpointer-testdoc.json @@ -20,6 +20,9 @@ "val" ] }, - "" : { "" : "empty key of an object with an empty key" } + "" : { + "" : "empty key of an object with an empty key", + "subKey" : "Some other value" + } } } \ No newline at end of file From 3fe4a767e6665d8290eb1359c4a9e2d7a13e4997 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 20 Mar 2018 22:15:25 -0400 Subject: [PATCH 298/315] Fixes incorrect syntax for JSONPointer in test. --- src/test/java/org/json/junit/JSONObjectTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 303a19ac9..5dc31c775 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -667,7 +667,7 @@ public void jsonObjectAppend() { assertTrue("expected 1 top level item", ((Map)(JsonPath.read(doc, "$"))).size() == 1); assertTrue("expected 6 myArray items", ((List)(JsonPath.read(doc, "$.myArray"))).size() == 6); assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0"))); - assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1/"))); + assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1"))); assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2"))); assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3"))); assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4"))); From d00501eabd9b60c8097fd466b99796b27fbdb7e9 Mon Sep 17 00:00:00 2001 From: Andrei Paikin Date: Fri, 25 May 2018 22:47:05 +0300 Subject: [PATCH 299/315] add usage of isEmpty method --- src/test/java/org/json/junit/CookieListTest.java | 2 +- src/test/java/org/json/junit/EnumTest.java | 2 +- src/test/java/org/json/junit/JSONArrayTest.java | 2 +- src/test/java/org/json/junit/JSONObjectTest.java | 16 ++++++++-------- src/test/java/org/json/junit/PropertyTest.java | 4 ++-- src/test/java/org/json/junit/XMLTest.java | 8 ++++---- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/test/java/org/json/junit/CookieListTest.java b/src/test/java/org/json/junit/CookieListTest.java index 80cbaa8ed..71496440b 100644 --- a/src/test/java/org/json/junit/CookieListTest.java +++ b/src/test/java/org/json/junit/CookieListTest.java @@ -65,7 +65,7 @@ public void malFormedCookieListException() { public void emptyStringCookieList() { String cookieStr = ""; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); - assertTrue(jsonObject.length() == 0); + assertTrue(jsonObject.isEmpty()); } /** diff --git a/src/test/java/org/json/junit/EnumTest.java b/src/test/java/org/json/junit/EnumTest.java index cd0d8c0fc..366643ed8 100644 --- a/src/test/java/org/json/junit/EnumTest.java +++ b/src/test/java/org/json/junit/EnumTest.java @@ -35,7 +35,7 @@ public void jsonObjectFromEnum() { // If there are no getters then the object is empty. MyEnum myEnum = MyEnum.VAL2; JSONObject jsonObject = new JSONObject(myEnum); - assertTrue("simple enum has no getters", jsonObject.length() == 0); + assertTrue("simple enum has no getters", jsonObject.isEmpty()); // enum with a getters should create a non-empty object MyEnumField myEnumField = MyEnumField.VAL2; diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 8a31c877b..845f4e792 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -691,7 +691,7 @@ public void remove() { JSONArray jsonArray = new JSONArray(arrayStr1); jsonArray.remove(0); assertTrue("array should be empty", null == jsonArray.remove(5)); - assertTrue("jsonArray should be empty", jsonArray.length() == 0); + assertTrue("jsonArray should be empty", jsonArray.isEmpty()); } /** diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 5dc31c775..e3b9529ac 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -141,7 +141,7 @@ public void testLongFromString(){ @Test public void emptyJsonObject() { JSONObject jsonObject = new JSONObject(); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); } /** @@ -184,7 +184,7 @@ public void jsonObjectByNames() { public void jsonObjectByNullMap() { Map map = null; JSONObject jsonObject = new JSONObject(map); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); } /** @@ -1122,7 +1122,7 @@ public void bigNumberOperations() { BigDecimal bigDecimal = new BigDecimal( "123456789012345678901234567890.12345678901234567890123456789"); jsonObject = new JSONObject(bigDecimal); - assertTrue("large bigDecimal is not stored", jsonObject.length() == 0); + assertTrue("large bigDecimal is not stored", jsonObject.isEmpty()); /** * JSONObject put(String, Object) method stores and serializes @@ -2244,11 +2244,11 @@ public void jsonObjectParsingErrors() { public void jsonObjectPutOnceNull() { JSONObject jsonObject = new JSONObject(); jsonObject.putOnce(null, null); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); jsonObject.putOnce("", null); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); jsonObject.putOnce(null, ""); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); } /** @@ -2424,11 +2424,11 @@ public void jsonObjectputNull() { String str = "{\"myKey\": \"myval\"}"; JSONObject jsonObjectRemove = new JSONObject(str); jsonObjectRemove.remove("myKey"); - assertEquals("jsonObject should be empty",0 ,jsonObjectRemove.length()); + assertTrue("jsonObject should be empty", jsonObjectRemove.isEmpty()); JSONObject jsonObjectPutNull = new JSONObject(str); jsonObjectPutNull.put("myKey", (Object) null); - assertEquals("jsonObject should be empty",0 ,jsonObjectPutNull.length()); + assertTrue("jsonObject should be empty", jsonObjectPutNull.isEmpty()); } diff --git a/src/test/java/org/json/junit/PropertyTest.java b/src/test/java/org/json/junit/PropertyTest.java index 60d3eb5de..880428414 100644 --- a/src/test/java/org/json/junit/PropertyTest.java +++ b/src/test/java/org/json/junit/PropertyTest.java @@ -21,7 +21,7 @@ public class PropertyTest { public void shouldHandleNullProperties() { Properties properties = null; JSONObject jsonObject = Property.toJSONObject(properties); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); } /** @@ -32,7 +32,7 @@ public void shouldHandleNullProperties() { public void shouldHandleEmptyProperties() { Properties properties = new Properties(); JSONObject jsonObject = Property.toJSONObject(properties); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); } /** diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index c34bf0f85..651d1a75d 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -34,7 +34,7 @@ public class XMLTest { public void shouldHandleNullXML() { String xmlStr = null; JSONObject jsonObject = XML.toJSONObject(xmlStr); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); } /** @@ -45,7 +45,7 @@ public void shouldHandleEmptyXML() { String xmlStr = ""; JSONObject jsonObject = XML.toJSONObject(xmlStr); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); } /** @@ -55,7 +55,7 @@ public void shouldHandleEmptyXML() { public void shouldHandleNonXML() { String xmlStr = "{ \"this is\": \"not xml\"}"; JSONObject jsonObject = XML.toJSONObject(xmlStr); - assertTrue("xml string should be empty", jsonObject.length() == 0); + assertTrue("xml string should be empty", jsonObject.isEmpty()); } /** @@ -200,7 +200,7 @@ public void shouldHandleNullJSONXML() { public void shouldHandleEmptyJSONXML() { JSONObject jsonObject= new JSONObject(); String xmlStr = XML.toString(jsonObject); - assertTrue("xml string should be empty", xmlStr.length() == 0); + assertTrue("xml string should be empty", xmlStr.isEmpty()); } /** From 3b8b0a681c349f12ea83b490927c0da219a7550d Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 2 Oct 2018 12:38:17 -0400 Subject: [PATCH 300/315] Update test cases to verify performance change and verify opt/getBigDecimal match --- .../java/org/json/junit/JSONObjectTest.java | 95 +++++++++++++++++-- 1 file changed, 86 insertions(+), 9 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index e3b9529ac..63385a552 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -24,6 +24,7 @@ import java.util.Locale; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; +import java.util.regex.Pattern; import org.json.CDL; import org.json.JSONArray; @@ -60,7 +61,13 @@ * otherwise be impossible. */ public class JSONObjectTest { - + + /** + * Regular Expression Pattern that matches JSON Numbers. This is primarily used for + * output to guarantee that we are always writing valid JSON. + */ + static final Pattern NUMBER_PATTERN = Pattern.compile("-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?"); + /** * Tests that the similar method is working as expected. */ @@ -87,6 +94,67 @@ public void verifySimilar() { assertTrue("Should eval to true", obj1.similar(obj3)); } + + @Test + public void timeNumberParsing() { + // test data to use + final String[] testData = new String[] { + null, + "", + "100", + "-100", + "abc123", + "012345", + "100.5e199", + "-100.5e199", + "DEADBEEF", + "0xDEADBEEF", + "1234567890.1234567890", + "-1234567890.1234567890", + "adloghakuidghauiehgauioehgdkjfb nsruoh aeu noerty384 nkljfgh " + + "395h tdfn kdz8yt3 4hkls gn.ey85 4hzfhnz.o8y5a84 onvklt " + + "yh389thub nkz8y49lihv al4itlaithknty8hnbl" + // long (in length) number sequences with invalid data at the end of the + // string offer very poor performance for the REGEX. + ,"123467890123467890123467890123467890123467890123467890123467" + + "8901234678901234678901234678901234678901234678901234678" + + "9012346789012346789012346789012346789012346789012346789" + + "0a" + }; + final int testDataLength = testData.length; + final int iterations = 1000000; + + // 10 million iterations 1,000,000 * 10 + long startTime = System.nanoTime(); + for(int i = 0; i < iterations; i++) { + for(int j = 0; j < testDataLength; j++) { + try { + BigDecimal v1 = new BigDecimal(testData[j]); + v1.signum(); + } catch(Exception ignore) { + //do nothing + } + } + } + final long elapsedNano1 = System.nanoTime() - startTime; + System.out.println("new BigDecimal(testData[]) : " + elapsedNano1 / 1000000 + " ms"); + + startTime = System.nanoTime(); + for(int i = 0; i < iterations; i++) { + for(int j = 0; j < testDataLength; j++) { + try { + boolean v2 = NUMBER_PATTERN.matcher(testData[j]).matches(); + assert v2 == !!v2; + } catch(Exception ignore) { + //do nothing + } + } + } + final long elapsedNano2 = System.nanoTime() - startTime; + System.out.println("NUMBER_PATTERN.matcher(testData[]).matches() : " + elapsedNano2 / 1000000 + " ms"); + // don't assert normally as the testing is machine dependent. + // assertTrue("Expected Pattern matching to be faster than BigDecimal constructor",elapsedNano2 Date: Thu, 4 Oct 2018 16:02:50 -0400 Subject: [PATCH 301/315] update expected exception text in tests to match unified number getters --- .../java/org/json/junit/JSONArrayTest.java | 32 +++---- .../java/org/json/junit/JSONObjectTest.java | 94 +++++++++---------- 2 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 845f4e792..3b70446f4 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -331,57 +331,57 @@ public void failedGetArrayValues() { jsonArray.getBoolean(4); assertTrue("expected getBoolean to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a boolean.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a boolean.",e.getMessage()); } try { jsonArray.get(-1); assertTrue("expected get to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[-1] not found.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[-1] not found.",e.getMessage()); } try { jsonArray.getDouble(4); assertTrue("expected getDouble to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a number.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a number.",e.getMessage()); } try { jsonArray.getInt(4); assertTrue("expected getInt to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a number.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a number.",e.getMessage()); } try { jsonArray.getJSONArray(4); assertTrue("expected getJSONArray to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a JSONArray.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a JSONArray.",e.getMessage()); } try { jsonArray.getJSONObject(4); assertTrue("expected getJSONObject to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a JSONObject.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a JSONObject.",e.getMessage()); } try { jsonArray.getLong(4); assertTrue("expected getLong to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a number.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a number.",e.getMessage()); } try { jsonArray.getString(5); assertTrue("expected getString to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[5] not a string.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[5] not a string.",e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 63385a552..c6cb580db 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1013,128 +1013,128 @@ public void jsonObjectNonAndWrongValues() { jsonObject.getBoolean("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("expecting an exception message", - "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + assertEquals("expecting an exception message", + "JSONObject[\"nonKey\"] not found.", e.getMessage()); } try { jsonObject.getBoolean("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a Boolean.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a Boolean.", + e.getMessage()); } try { jsonObject.getString("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getString("trueKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"trueKey\"] not a string.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"trueKey\"] not a string.", + e.getMessage()); } try { jsonObject.getDouble("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getDouble("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.", + 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())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getFloat("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.", + e.getMessage()); } try { jsonObject.getInt("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getInt("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not an int.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.", + e.getMessage()); } try { jsonObject.getLong("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getLong("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a long.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.", + e.getMessage()); } try { jsonObject.getJSONArray("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getJSONArray("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONArray.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONArray.", + e.getMessage()); } try { jsonObject.getJSONObject("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getJSONObject("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONObject.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONObject.", + e.getMessage()); } } From e4186e072ad2407d5dee25eeacef68884154ec5b Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 8 Dec 2018 11:29:44 -0600 Subject: [PATCH 302/315] reduce number of iterations to shorten test time --- src/test/java/org/json/junit/JSONObjectTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index c6cb580db..8d32649bf 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -122,9 +122,13 @@ public void timeNumberParsing() { + "0a" }; final int testDataLength = testData.length; - final int iterations = 1000000; + /** + * Changed to 1000 for faster test runs + */ + // final int iterations = 1000000; + final int iterations = 1000; - // 10 million iterations 1,000,000 * 10 + // 10 million iterations 1,000,000 * 10 (currently 100,000) long startTime = System.nanoTime(); for(int i = 0; i < iterations; i++) { for(int j = 0; j < testDataLength; j++) { From d0ea807884728782b2bc1722ca4a79e56e58e245 Mon Sep 17 00:00:00 2001 From: stleary Date: Sat, 8 Dec 2018 14:51:01 -0600 Subject: [PATCH 303/315] xml parser config tests --- README.md | 39 +- .../java/org/json/junit/JunitTestSuite.java | 3 +- .../org/json/junit/XMLConfigurationTest.java | 930 ++++++++++++++++++ 3 files changed, 934 insertions(+), 38 deletions(-) create mode 100755 src/test/java/org/json/junit/XMLConfigurationTest.java diff --git a/README.md b/README.md index 1ca5c64a5..e6e61b58f 100644 --- a/README.md +++ b/README.md @@ -89,43 +89,8 @@ Execution failed for task ':compileJava'. > invalid flag: -parameters ``` -A unit test has the following stages: - -| Test phase |Description | -|----|----| -| No test | No test specifically for this class has been written, or the class contains no executable code. | -| In progress | Unit tests have been started for this class. | -| Coverage > 90% | Initial goal of 90% coverage has been reached. Test quality may be questionable | -| Reasonable test cases | 90% coverage. Functionality and behavior has been confirmed | -| Checked against previous unit tests | Historical unit tests have been checked in case something important was missed | -| Completed | The unit test is completed | - - -| Test file name | Coverage | Comments | -| ------------- | ------------- | ---- | -| Total coverage | 90.6% | | | -| | | | -| CDL.java | 98.8% | Reasonable test cases. | -| Cookie.java | 98.9% | Reasonable test cases. | -| CookieList.java |96.5% | Reasonable test cases. | -| HTTP.java | 98.8%| Coverage > 90% | -| HTTPTokener.java |93.2% | No test | -| JSONArray.java |88.3% | Reasonable test cases. Need new tests for newer API functions | -| JSONException.java | 100% | No test | -| JSONML.java | 84.4%| In progress | -| JSONObject | 96.7% | Reasonable test cases | -| JSONObject.Null | 77.8% | No test | -| JSONPointer | 96.3% | Reasonable test cases | -| JSONPointerException | 100% | No test | -| JSONString.java | | No test | -| JSONStringer.java | 93.8%| Coverage > 90% | -| JSONTokener.java | 87.5% | In progress | -| JSONWriter.java | 89.15% | No test | -| Property.java | 95.8% | Coverage > 90% | -| XML.java | 77.3% | In progress | -| XMLTokener.java| 82.4%| No test | - -| Files used in test | + +| Resource files used in test | | ------------- | | EnumTest.java | | MyBean.java | diff --git a/src/test/java/org/json/junit/JunitTestSuite.java b/src/test/java/org/json/junit/JunitTestSuite.java index 9c9a3259a..68b5acb37 100644 --- a/src/test/java/org/json/junit/JunitTestSuite.java +++ b/src/test/java/org/json/junit/JunitTestSuite.java @@ -18,7 +18,8 @@ EnumTest.class, JSONPointerTest.class, JSONStringTest.class, - JSONTokenerTest.class + JSONTokenerTest.class, + XMLConfigurationTest.class }) public class JunitTestSuite { } diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java new file mode 100755 index 000000000..a2d0b85a8 --- /dev/null +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -0,0 +1,930 @@ +package org.json.junit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.XML; +import org.json.XMLParserConfiguration; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + + +/** + * Tests for JSON-Java XML.java with XMLParserConfiguration.java + */ +public class XMLConfigurationTest { + /** + * JUnit supports temporary files and folders that are cleaned up after the test. + * https://garygregory.wordpress.com/2010/01/20/junit-tip-use-rules-to-manage-temporary-files-and-folders/ + */ + @Rule + public TemporaryFolder testFolder = new TemporaryFolder(); + + /** + * JSONObject from a null XML string. + * Expects a NullPointerException + */ + @Test(expected=NullPointerException.class) + public void shouldHandleNullXML() { + String xmlStr = null; + JSONObject jsonObject = + XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); + } + + /** + * Empty JSONObject from an empty XML string. + */ + @Test + public void shouldHandleEmptyXML() { + + String xmlStr = ""; + JSONObject jsonObject = + XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS); + assertTrue("jsonObject should be empty", jsonObject.isEmpty()); + } + + /** + * Empty JSONObject from a non-XML string. + */ + @Test + public void shouldHandleNonXML() { + String xmlStr = "{ \"this is\": \"not xml\"}"; + JSONObject jsonObject = + XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS); + assertTrue("xml string should be empty", jsonObject.isEmpty()); + } + + /** + * Invalid XML string (tag contains a frontslash). + * Expects a JSONException + */ + @Test + public void shouldHandleInvalidSlashInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " abc street\n"+ + "
\n"+ + "
"; + try { + XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS); + fail("Expecting a JSONException"); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Misshaped tag at 176 [character 14 line 4]", + e.getMessage()); + } + } + + /** + * Invalid XML string ('!' char in tag) + * Expects a JSONException + */ + @Test + public void shouldHandleInvalidBangInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + "
\n"+ + "
"; + try { + XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS); + fail("Expecting a JSONException"); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Misshaped meta tag at 214 [character 12 line 7]", + e.getMessage()); + } + } + + /** + * Invalid XML string ('!' char and no closing tag brace) + * Expects a JSONException + */ + @Test + public void shouldHandleInvalidBangNoCloseInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + ""; + try { + XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS); + fail("Expecting a JSONException"); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Misshaped meta tag at 213 [character 12 line 7]", + e.getMessage()); + } + } + + /** + * Invalid XML string (no end brace for tag) + * Expects JSONException + */ + @Test + public void shouldHandleNoCloseStartTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + ""; + try { + XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS); + fail("Expecting a JSONException"); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Misplaced '<' at 193 [character 4 line 6]", + e.getMessage()); + } + } + + /** + * Invalid XML string (partial CDATA chars in tag name) + * Expects JSONException + */ + @Test + public void shouldHandleInvalidCDATABangInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " \n"+ + "
\n"+ + "
"; + try { + XMLParserConfiguration config = + new XMLParserConfiguration("altContent"); + XML.toJSONObject(xmlStr, config); + fail("Expecting a JSONException"); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Expected 'CDATA[' at 204 [character 11 line 5]", + e.getMessage()); + } + } + + /** + * Null JSONObject in XML.toString() + */ + @Test + public void shouldHandleNullJSONXML() { + JSONObject jsonObject= null; + String actualXml = XML.toString(jsonObject, null, + XMLParserConfiguration.KEEP_STRINGS); + assertEquals("generated XML does not equal expected XML","\"null\"",actualXml); + } + + /** + * Empty JSONObject in XML.toString() + */ + @Test + public void shouldHandleEmptyJSONXML() { + JSONObject jsonObject= new JSONObject(); + String xmlStr = XML.toString(jsonObject, null, + XMLParserConfiguration.KEEP_STRINGS); + assertTrue("xml string should be empty", xmlStr.isEmpty()); + } + + /** + * No SML start tag. The ending tag ends up being treated as content. + */ + @Test + public void shouldHandleNoStartTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " >\n"+ + "
\n"+ + "
"; + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject jsonObject = XML.toJSONObject(xmlStr, + XMLParserConfiguration.KEEP_STRINGS); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + /** + * Valid XML to JSONObject + */ + @Test + public void shouldHandleSimpleXML() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " [CDATA[Baker street 5]\n"+ + " \n"+ + " true\n"+ + " false\n"+ + " null\n"+ + " 42\n"+ + " -23\n"+ + " -23.45\n"+ + " -23x.45\n"+ + " 1, 2, 3, 4.1, 5.2\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+ + "\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+ + "\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+ + "\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+ + "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ + "},\"xsi:noNamespaceSchemaLocation\":"+ + "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ + "XMLSchema-instance\"}}"; + + XMLParserConfiguration config = + new XMLParserConfiguration("altContent"); + compareStringToJSONObject(xmlStr, expectedStr, config); + compareReaderToJSONObject(xmlStr, expectedStr, config); + compareFileToJSONObject(xmlStr, expectedStr); + } + + /** + * Valid XML with comments to JSONObject + */ + @Test + public void shouldHandleCommentsInXML() { + + String xmlStr = + "\n"+ + "\n"+ + "\n"+ + "
\n"+ + " comment ]]>\n"+ + " Joe Tester\n"+ + " \n"+ + " Baker street 5\n"+ + "
\n"+ + "
"; + XMLParserConfiguration config = + new XMLParserConfiguration("altContent"); + JSONObject jsonObject = XML.toJSONObject(xmlStr, config); + String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+ + "street 5\",\"name\":\"Joe Tester\",\"altContent\":\" this is -- "+ + " comment \"}}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + /** + * Valid XML to XML.toString() + */ + @Test + public void shouldHandleToString() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " [CDATA[Joe & T > e < s " t ' er]]\n"+ + " Baker street 5\n"+ + " 1, 2, 3, 4.1, 5.2\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ + "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+ + "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ + "},\"xsi:noNamespaceSchemaLocation\":"+ + "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ + "XMLSchema-instance\"}}"; + + JSONObject jsonObject = XML.toJSONObject(xmlStr, + XMLParserConfiguration.KEEP_STRINGS); + String xmlToStr = XML.toString(jsonObject, null, + XMLParserConfiguration.KEEP_STRINGS); + JSONObject finalJsonObject = XML.toJSONObject(xmlToStr, + XMLParserConfiguration.KEEP_STRINGS); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } + + /** + * Converting a JSON doc containing '>' content to JSONObject, then + * XML.toString() should result in valid XML. + */ + @Test + public void shouldHandleContentNoArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "altContent\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + XMLParserConfiguration config = new XMLParserConfiguration("altContent"); + String finalStr = XML.toString(expectedJsonObject, null, config); + String expectedFinalStr = "
>"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + /** + * Converting a JSON doc containing a 'content' array to JSONObject, then + * XML.toString() should result in valid XML. + * TODO: This is probably an error in how the 'content' keyword is used. + */ + @Test + public void shouldHandleContentArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "altContent\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + XMLParserConfiguration config = new XMLParserConfiguration("altContent"); + String finalStr = XML.toString(expectedJsonObject, null, config); + String expectedFinalStr = "
"+ + "1\n2\n3"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + /** + * Converting a JSON doc containing a named array to JSONObject, then + * XML.toString() should result in valid XML. + */ + @Test + public void shouldHandleArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ + "\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + String finalStr = XML.toString(expectedJsonObject, null, + XMLParserConfiguration.KEEP_STRINGS); + String expectedFinalStr = "
"+ + "123"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + /** + * Tests that the XML output for empty arrays is consistent. + */ + @Test + public void shouldHandleEmptyArray(){ + final JSONObject jo1 = new JSONObject(); + jo1.put("array",new Object[]{}); + final JSONObject jo2 = new JSONObject(); + jo2.put("array",new JSONArray()); + + final String expected = ""; + String output1 = XML.toString(jo1, "jo", + XMLParserConfiguration.KEEP_STRINGS); + assertEquals("Expected an empty root tag", expected, output1); + String output2 = XML.toString(jo2, "jo", + XMLParserConfiguration.KEEP_STRINGS); + assertEquals("Expected an empty root tag", expected, output2); + } + + /** + * Tests that the XML output for arrays is consistent when an internal array is empty. + */ + @Test + public void shouldHandleEmptyMultiArray(){ + final JSONObject jo1 = new JSONObject(); + jo1.put("arr",new Object[]{"One", new String[]{}, "Four"}); + final JSONObject jo2 = new JSONObject(); + jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{}), "Four"})); + + final String expected = "OneFour"; + String output1 = XML.toString(jo1, "jo", + XMLParserConfiguration.KEEP_STRINGS); + assertEquals("Expected a matching array", expected, output1); + String output2 = XML.toString(jo2, "jo", + XMLParserConfiguration.KEEP_STRINGS); + + assertEquals("Expected a matching array", expected, output2); + } + + /** + * Tests that the XML output for arrays is consistent when arrays are not empty. + */ + @Test + public void shouldHandleNonEmptyArray(){ + final JSONObject jo1 = new JSONObject(); + jo1.put("arr",new String[]{"One", "Two", "Three"}); + final JSONObject jo2 = new JSONObject(); + jo2.put("arr",new JSONArray(new String[]{"One", "Two", "Three"})); + + final String expected = "OneTwoThree"; + String output1 = XML.toString(jo1, "jo", + XMLParserConfiguration.KEEP_STRINGS); + assertEquals("Expected a non empty root tag", expected, output1); + String output2 = XML.toString(jo2, "jo", + XMLParserConfiguration.KEEP_STRINGS); + assertEquals("Expected a non empty root tag", expected, output2); + } + + /** + * Tests that the XML output for arrays is consistent when arrays are not empty and contain internal arrays. + */ + @Test + public void shouldHandleMultiArray(){ + final JSONObject jo1 = new JSONObject(); + jo1.put("arr",new Object[]{"One", new String[]{"Two", "Three"}, "Four"}); + final JSONObject jo2 = new JSONObject(); + jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{"Two", "Three"}), "Four"})); + + final String expected = "OneTwoThreeFour"; + String output1 = XML.toString(jo1, "jo", + XMLParserConfiguration.KEEP_STRINGS); + assertEquals("Expected a matching array", expected, output1); + String output2 = XML.toString(jo2, "jo", + XMLParserConfiguration.KEEP_STRINGS); + assertEquals("Expected a matching array", expected, output2); + } + + /** + * Converting a JSON doc containing a named array of nested arrays to + * JSONObject, then XML.toString() should result in valid XML. + */ + @Test + public void shouldHandleNestedArraytoString() { + String xmlStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ + "\"outer\":[[1], [2], [3]]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject jsonObject = new JSONObject(xmlStr); + String finalStr = XML.toString(jsonObject, null, + XMLParserConfiguration.ORIGINAL); + JSONObject finalJsonObject = XML.toJSONObject(finalStr); + String expectedStr = "
"+ + "12"+ + "3"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + JSONObject expectedJsonObject = XML.toJSONObject(expectedStr, + XMLParserConfiguration.ORIGINAL); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } + + + /** + * Possible bug: + * Illegal node-names must be converted to legal XML-node-names. + * The given example shows 2 nodes which are valid for JSON, but not for XML. + * Therefore illegal arguments should be converted to e.g. an underscore (_). + */ + @Test + public void shouldHandleIllegalJSONNodeNames() + { + JSONObject inputJSON = new JSONObject(); + inputJSON.append("123IllegalNode", "someValue1"); + inputJSON.append("Illegal@node", "someValue2"); + + String result = XML.toString(inputJSON, null, + XMLParserConfiguration.KEEP_STRINGS); + + /* + * This is invalid XML. Names should not begin with digits or contain + * certain values, including '@'. One possible solution is to replace + * illegal chars with '_', in which case the expected output would be: + * <___IllegalNode>someValue1someValue2 + */ + String expected = "<123IllegalNode>someValue1someValue2"; + + assertEquals(expected, result); + } + + /** + * JSONObject with NULL value, to XML.toString() + */ + @Test + public void shouldHandleNullNodeValue() + { + JSONObject inputJSON = new JSONObject(); + inputJSON.put("nullValue", JSONObject.NULL); + // This is a possible preferred result + // String expectedXML = ""; + /** + * This is the current behavior. JSONObject.NULL is emitted as + * the string, "null". + */ + String actualXML = "null"; + String resultXML = XML.toString(inputJSON, null, + XMLParserConfiguration.KEEP_STRINGS); + assertEquals(actualXML, resultXML); + } + + /** + * Investigate exactly how the "content" keyword works + */ + @Test + public void contentOperations() { + /* + * When a standalone 0) then return]]>"; + JSONObject jsonObject = XML.toJSONObject(xmlStr, + XMLParserConfiguration.KEEP_STRINGS); + assertTrue("1. 3 items", 3 == jsonObject.length()); + assertTrue("1. empty tag1", "".equals(jsonObject.get("tag1"))); + assertTrue("1. empty tag2", "".equals(jsonObject.get("tag2"))); + assertTrue("1. content found", "if (a < b && a > 0) then return".equals(jsonObject.get("content"))); + + // multiple consecutive standalone cdatas are accumulated into an array + xmlStr = " 0) then return]]>"; + jsonObject = XML.toJSONObject(xmlStr, + new XMLParserConfiguration(true, "altContent")); + assertTrue("2. 3 items", 3 == jsonObject.length()); + assertTrue("2. empty tag1", "".equals(jsonObject.get("tag1"))); + assertTrue("2. empty tag2", "".equals(jsonObject.get("tag2"))); + assertTrue("2. content array found", jsonObject.get("altContent") instanceof JSONArray); + JSONArray jsonArray = jsonObject.getJSONArray("altContent"); + assertTrue("2. array size", jsonArray.length() == 2); + assertTrue("2. content array entry 0", "if (a < b && a > 0) then return".equals(jsonArray.get(0))); + assertTrue("2. content array entry 1", "here is another cdata".equals(jsonArray.get(1))); + + /* + * text content is accumulated in a "content" inside a local JSONObject. + * If there is only one instance, it is saved in the context (a different JSONObject + * from the calling code. and the content element is discarded. + */ + xmlStr = "value 1"; + jsonObject = XML.toJSONObject(xmlStr, + new XMLParserConfiguration(true, "altContent")); + assertTrue("3. 2 items", 1 == jsonObject.length()); + assertTrue("3. value tag1", "value 1".equals(jsonObject.get("tag1"))); + + /* + * array-style text content (multiple tags with the same name) is + * accumulated in a local JSONObject with key="content" and value=JSONArray, + * saved in the context, and then the local JSONObject is discarded. + */ + xmlStr = "value 12true"; + jsonObject = XML.toJSONObject(xmlStr, + new XMLParserConfiguration(true, "altContent")); + assertTrue("4. 1 item", 1 == jsonObject.length()); + assertTrue("4. content array found", jsonObject.get("tag1") instanceof JSONArray); + jsonArray = jsonObject.getJSONArray("tag1"); + assertTrue("4. array size", jsonArray.length() == 3); + assertTrue("4. content array entry 0", "value 1".equals(jsonArray.get(0))); + assertTrue("4. content array entry 1", jsonArray.getInt(1) == 2); + assertTrue("4. content array entry 2", jsonArray.getBoolean(2) == true); + + /* + * Complex content is accumulated in a "content" field. For example, an element + * may contain a mix of child elements and text. Each text segment is + * accumulated to content. + */ + xmlStr = "val1val2"; + jsonObject = XML.toJSONObject(xmlStr, + new XMLParserConfiguration(true, "altContent")); + assertTrue("5. 1 item", 1 == jsonObject.length()); + assertTrue("5. jsonObject found", jsonObject.get("tag1") + instanceof JSONObject); + jsonObject = jsonObject.getJSONObject("tag1"); + assertTrue("5. 2 contained items", 2 == jsonObject.length()); + assertTrue("5. contained tag", "".equals(jsonObject.get("tag2"))); + assertTrue("5. contained content jsonArray found", + jsonObject.get("altContent") instanceof JSONArray); + jsonArray = jsonObject.getJSONArray("altContent"); + assertTrue("5. array size", jsonArray.length() == 2); + assertTrue("5. content array entry 0", "val1".equals(jsonArray.get(0))); + assertTrue("5. content array entry 1", "val2".equals(jsonArray.get(1))); + + /* + * If there is only 1 complex text content, then it is accumulated in a + * "content" field as a string. + */ + xmlStr = "val1"; + jsonObject = XML.toJSONObject(xmlStr, + new XMLParserConfiguration(true, "altContent")); + assertTrue("6. 1 item", 1 == jsonObject.length()); + assertTrue("6. jsonObject found", jsonObject.get("tag1") instanceof JSONObject); + jsonObject = jsonObject.getJSONObject("tag1"); + assertTrue("6. contained content found", + "val1".equals(jsonObject.get("altContent"))); + assertTrue("6. contained tag2", "".equals(jsonObject.get("tag2"))); + + /* + * In this corner case, the content sibling happens to have key=content + * We end up with an array within an array, and no content element. + * This is probably a bug. + */ + xmlStr = "val1"; + jsonObject = XML.toJSONObject(xmlStr, + new XMLParserConfiguration(true, "altContent")); + assertTrue("7. 1 item", 1 == jsonObject.length()); + assertTrue("7. jsonArray found", + jsonObject.get("tag1") instanceof JSONArray); + jsonArray = jsonObject.getJSONArray("tag1"); + assertTrue("array size 1", jsonArray.length() == 1); + assertTrue("7. contained array found", jsonArray.get(0) + instanceof JSONArray); + jsonArray = jsonArray.getJSONArray(0); + assertTrue("7. inner array size 2", jsonArray.length() == 2); + assertTrue("7. inner array item 0", "val1".equals(jsonArray.get(0))); + assertTrue("7. inner array item 1", "".equals(jsonArray.get(1))); + + /* + * Confirm behavior of original issue + */ + String jsonStr = + "{"+ + "\"Profile\": {"+ + "\"list\": {"+ + "\"history\": {"+ + "\"entries\": ["+ + "{"+ + "\"deviceId\": \"id\","+ + "\"altContent\": {"+ + "\"material\": ["+ + "{"+ + "\"stuff\": false"+ + "}"+ + "]"+ + "}"+ + "}"+ + "]"+ + "}"+ + "}"+ + "}"+ + "}"; + jsonObject = new JSONObject(jsonStr); + xmlStr = XML.toString(jsonObject, null, + new XMLParserConfiguration(true, "altContent")); + /* + * This is the created XML. Looks like content was mistaken for + * complex (child node + text) XML. + * + * + * + * + * id + * {"material":[{"stuff":false}]} + * + * + * + * + */ + assertTrue("nothing to test here, see comment on created XML, above", true); + } + + /** + * JSON string lost leading zero and converted "True" to true. + */ + @Test + public void testToJSONArray_jsonOutput() { + final String originalXml = "011000True"; + final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"; + final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, + new XMLParserConfiguration(false)); + assertEquals(expectedJsonString, actualJsonOutput.toString()); + } + + /** + * JSON string cannot be reverted to original xml. + */ + @Test + public void testToJSONArray_reversibility() { + final String originalXml = "011000True"; + XMLParserConfiguration config = new XMLParserConfiguration(false); + final String revertedXml = + XML.toString(XML.toJSONObject(originalXml, config), + null, config); + assertNotEquals(revertedXml, originalXml); + } + + /** + * test passes when using the new method toJsonArray. + */ + @Test + public void testToJsonXML() { + final String originalXml = "011000True"; + final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}"; + + final JSONObject json = XML.toJSONObject(originalXml, + new XMLParserConfiguration(true)); + assertEquals(expectedJsonString, json.toString()); + + final String reverseXml = XML.toString(json); + // this reversal isn't exactly the same. use JSONML for an exact reversal + final String expectedReverseXml = "01011000True"; + + assertEquals(expectedReverseXml, reverseXml); + } + + /** + * test to validate certain conditions of XML unescaping. + */ + @Test + public void testUnescape() { + assertEquals("{\"xml\":\"Can cope <;\"}", + XML.toJSONObject("Can cope <; ", + XMLParserConfiguration.KEEP_STRINGS).toString()); + assertEquals("Can cope <; ", XML.unescape("Can cope <; ")); + + assertEquals("{\"xml\":\"Can cope & ;\"}", + XML.toJSONObject("Can cope & ; ", + XMLParserConfiguration.KEEP_STRINGS).toString()); + assertEquals("Can cope & ; ", XML.unescape("Can cope & ; ")); + + assertEquals("{\"xml\":\"Can cope &;\"}", + XML.toJSONObject("Can cope &; ", + XMLParserConfiguration.KEEP_STRINGS).toString()); + assertEquals("Can cope &; ", XML.unescape("Can cope &; ")); + + // unicode entity + assertEquals("{\"xml\":\"Can cope 4;\"}", + XML.toJSONObject("Can cope 4; ", + XMLParserConfiguration.KEEP_STRINGS).toString()); + assertEquals("Can cope 4; ", XML.unescape("Can cope 4; ")); + + // double escaped + assertEquals("{\"xml\":\"Can cope <\"}", + XML.toJSONObject("Can cope &lt; ", + XMLParserConfiguration.KEEP_STRINGS).toString()); + assertEquals("Can cope < ", XML.unescape("Can cope &lt; ")); + + assertEquals("{\"xml\":\"Can cope 4\"}", + XML.toJSONObject("Can cope &#x34; ", + XMLParserConfiguration.KEEP_STRINGS).toString()); + assertEquals("Can cope 4 ", XML.unescape("Can cope &#x34; ")); + + } + + /** + * Confirm XMLParserConfiguration functionality + */ + @Test + public void testConfig() { + /** + * 1st param is whether to keep the raw string, or call + * XML.stringToValue(), which may convert the token to + * boolean, null, or number. + * 2nd param is what JSON name to use for strings that are + * evaluated as xml content data in complex objects, e.g. + * + * value + * content data + * + */ + + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " content 1\n"+ + " Sherlock Holmes\n"+ + " content 2\n"+ + " Baker street 5\n"+ + " content 3\n"+ + " 1\n"+ + "
\n"+ + "
"; + + // keep strings, use the altContent tag + XMLParserConfiguration config = + new XMLParserConfiguration(true, "altContent"); + JSONObject jsonObject = XML.toJSONObject(xmlStr, config); + // num is parsed as a string + assertEquals(jsonObject.getJSONObject("addresses"). + getJSONObject("address").getString("num"), "1"); + // complex content is collected in an 'altContent' array + JSONArray jsonArray = jsonObject.getJSONObject("addresses"). + getJSONObject("address").getJSONArray("altContent"); + String expectedStr = "[\"content 1\", \"content 2\", \"content 3\"]"; + JSONArray expectedJsonArray = new JSONArray(expectedStr); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + + // keepstrings only + jsonObject = XML.toJSONObject(xmlStr, + XMLParserConfiguration.KEEP_STRINGS); + // num is parsed as a string + assertEquals(jsonObject.getJSONObject("addresses"). + getJSONObject("address").getString("num"), "1"); + // complex content is collected in an 'content' array + jsonArray = jsonObject.getJSONObject("addresses"). + getJSONObject("address").getJSONArray("content"); + expectedJsonArray = new JSONArray(expectedStr); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + + // use alternate content name + config = new XMLParserConfiguration("altContent"); + jsonObject = XML.toJSONObject(xmlStr, config); + // num is parsed as a number + assertEquals(jsonObject.getJSONObject("addresses"). + getJSONObject("address").getInt("num"), 1); + // complex content is collected in an 'altContent' array + jsonArray = jsonObject.getJSONObject("addresses"). + getJSONObject("address").getJSONArray("altContent"); + expectedJsonArray = new JSONArray(expectedStr); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + + } + + + /** + * Convenience method, given an input string and expected result, + * convert to JSONObject and compare actual to expected result. + * @param xmlStr the string to parse + * @param expectedStr the expected JSON string + * @param config provides more flexible XML parsing + * flexible XML parsing. + */ + private void compareStringToJSONObject(String xmlStr, String expectedStr, + XMLParserConfiguration config) { + JSONObject expectedJsonObject = new JSONObject(expectedStr); + JSONObject jsonObject = XML.toJSONObject(xmlStr, config); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + /** + * Convenience method, given an input string and expected result, + * convert to JSONObject via reader and compare actual to expected result. + * @param xmlStr the string to parse + * @param expectedStr the expected JSON string + * @param config provides more flexible XML parsing + */ + private void compareReaderToJSONObject(String xmlStr, String expectedStr, + XMLParserConfiguration config) { + /* + * Commenting out this method until the JSON-java code is updated + * to support XML.toJSONObject(reader) + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Reader reader = new StringReader(xmlStr); + JSONObject jsonObject = XML.toJSONObject(reader); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + */ + } + + /** + * Convenience method, given an input string and expected result, convert to + * JSONObject via file and compare actual to expected result. + * + * @param xmlStr + * the string to parse + * @param expectedStr + * the expected JSON string + * @throws IOException + */ + private void compareFileToJSONObject(String xmlStr, String expectedStr) { + /* + * Commenting out this method until the JSON-java code is updated + * to support XML.toJSONObject(reader) + try { + JSONObject expectedJsonObject = new JSONObject(expectedStr); + File tempFile = testFolder.newFile("fileToJSONObject.xml"); + FileWriter fileWriter = new FileWriter(tempFile); + fileWriter.write(xmlStr); + fileWriter.close(); + Reader reader = new FileReader(tempFile); + JSONObject jsonObject = XML.toJSONObject(reader); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } catch (IOException e) { + assertTrue("file writer error: " +e.getMessage(), false); + } + */ + } +} \ No newline at end of file From e7f7d348cd13e2e6d168e13398f6d7384294cc2a Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 10 Dec 2018 11:45:10 -0500 Subject: [PATCH 304/315] * updates tests to cover more cases of tokenizing * uncomments tests that should now work --- src/test/java/org/json/junit/CDLTest.java | 10 +- .../java/org/json/junit/JSONTokenerTest.java | 94 +++++++++++++++++++ src/test/java/org/json/junit/XMLTest.java | 32 +++---- 3 files changed, 115 insertions(+), 21 deletions(-) diff --git a/src/test/java/org/json/junit/CDLTest.java b/src/test/java/org/json/junit/CDLTest.java index b1f9561f4..721fd3cb3 100644 --- a/src/test/java/org/json/junit/CDLTest.java +++ b/src/test/java/org/json/junit/CDLTest.java @@ -30,7 +30,7 @@ public class CDLTest { ); /** - * CDL.toJSONArray() adds all values asstrings, with no filtering or + * CDL.toJSONArray() adds all values as strings, with no filtering or * conversions. For testing, this means that the expected JSONObject * values all must be quoted in the cases where the JSONObject parsing * might normally convert the value into a non-string. @@ -264,8 +264,8 @@ public void checkSpecialChars() { */ @Test public void textToJSONArray() { - JSONArray jsonArray = CDL.toJSONArray(lines); - JSONArray expectedJsonArray = new JSONArray(expectedLines); + JSONArray jsonArray = CDL.toJSONArray(this.lines); + JSONArray expectedJsonArray = new JSONArray(this.expectedLines); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } @@ -289,10 +289,10 @@ public void jsonArrayToJSONArray() { */ @Test public void textToJSONArrayAndBackToString() { - JSONArray jsonArray = CDL.toJSONArray(lines); + JSONArray jsonArray = CDL.toJSONArray(this.lines); String jsonStr = CDL.toString(jsonArray); JSONArray finalJsonArray = CDL.toJSONArray(jsonStr); - JSONArray expectedJsonArray = new JSONArray(expectedLines); + JSONArray expectedJsonArray = new JSONArray(this.expectedLines); Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java index dced89f7d..de1564d40 100644 --- a/src/test/java/org/json/junit/JSONTokenerTest.java +++ b/src/test/java/org/json/junit/JSONTokenerTest.java @@ -12,7 +12,9 @@ import java.io.Reader; import java.io.StringReader; +import org.json.JSONArray; import org.json.JSONException; +import org.json.JSONObject; import org.json.JSONTokener; import org.junit.Test; @@ -64,7 +66,99 @@ public void verifyBackFailureDoubleBack() throws IOException { } } } + + @Test + public void testValid() { + checkValid("0",Number.class); + checkValid(" 0 ",Number.class); + checkValid("23",Number.class); + checkValid("23.5",Number.class); + checkValid(" 23.5 ",Number.class); + checkValid("null",null); + checkValid(" null ",null); + checkValid("true",Boolean.class); + checkValid(" true\n",Boolean.class); + checkValid("false",Boolean.class); + checkValid("\nfalse ",Boolean.class); + checkValid("{}",JSONObject.class); + checkValid(" {} ",JSONObject.class); + checkValid("{\"a\":1}",JSONObject.class); + checkValid(" {\"a\":1} ",JSONObject.class); + checkValid("[]",JSONArray.class); + checkValid(" [] ",JSONArray.class); + checkValid("[1,2]",JSONArray.class); + checkValid("\n\n[1,2]\n\n",JSONArray.class); + checkValid("1 2", String.class); + } + + @Test + public void testErrors() { + // Check that stream can detect that a value is found after + // the first one + checkError(" { \"a\":1 } 4 "); + checkError("null \"a\""); + checkError("{} true"); + } + + private Object checkValid(String testStr, Class aClass) { + Object result = nextValue(testStr); + // Check class of object returned + if( null == aClass ) { + if(JSONObject.NULL.equals(result)) { + // OK + } else { + throw new JSONException("Unexpected class: "+result.getClass().getSimpleName()); + } + } else { + if( null == result ) { + throw new JSONException("Unexpected null result"); + } else if(!aClass.isAssignableFrom(result.getClass()) ) { + throw new JSONException("Unexpected class: "+result.getClass().getSimpleName()); + } + } + + return result; + } + + private void checkError(String testStr) { + try { + nextValue(testStr); + + fail("Error should be triggered: (\""+testStr+"\")"); + } catch (JSONException e) { + // OK + } + } + + /** + * Verifies that JSONTokener can read a stream that contains a value. After + * the reading is done, check that the stream is left in the correct state + * by reading the characters after. All valid cases should reach end of stream. + * @param testStr + * @return + * @throws Exception + */ + private Object nextValue(String testStr) throws JSONException { + try(StringReader sr = new StringReader(testStr);){ + JSONTokener tokener = new JSONTokener(sr); + + Object result = tokener.nextValue(); + + if( result == null ) { + throw new JSONException("Unable to find value token in JSON stream: ("+tokener+"): "+testStr); + } + + char c = tokener.nextClean(); + if( 0 != c ) { + throw new JSONException("Unexpected character found at end of JSON stream: "+c+ " ("+tokener+"): "+testStr); + } + + return result; + } + + } + /** * Tests the failure of the skipTo method with a buffered reader. Preferably * we'd like this not to fail but at this time we don't have a good recovery. diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 1cff3267d..83a7cc252 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -6,6 +6,13 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -743,14 +750,10 @@ private void compareStringToJSONObject(String xmlStr, String expectedStr) { * @param expectedStr the expected JSON string */ private void compareReaderToJSONObject(String xmlStr, String expectedStr) { - /* - * Commenting out this method until the JSON-java code is updated - * to support XML.toJSONObject(reader) JSONObject expectedJsonObject = new JSONObject(expectedStr); Reader reader = new StringReader(xmlStr); JSONObject jsonObject = XML.toJSONObject(reader); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); - */ } /** @@ -764,22 +767,19 @@ private void compareReaderToJSONObject(String xmlStr, String expectedStr) { * @throws IOException */ private void compareFileToJSONObject(String xmlStr, String expectedStr) { - /* - * Commenting out this method until the JSON-java code is updated - * to support XML.toJSONObject(reader) try { JSONObject expectedJsonObject = new JSONObject(expectedStr); - File tempFile = testFolder.newFile("fileToJSONObject.xml"); - FileWriter fileWriter = new FileWriter(tempFile); - fileWriter.write(xmlStr); - fileWriter.close(); - Reader reader = new FileReader(tempFile); - JSONObject jsonObject = XML.toJSONObject(reader); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + File tempFile = this.testFolder.newFile("fileToJSONObject.xml"); + try(FileWriter fileWriter = new FileWriter(tempFile);){ + fileWriter.write(xmlStr); + } + try(Reader reader = new FileReader(tempFile);){ + JSONObject jsonObject = XML.toJSONObject(reader); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } } catch (IOException e) { - assertTrue("file writer error: " +e.getMessage(), false); + fail("file writer error: " +e.getMessage()); } - */ } /** From 614e8359b91eb997142d313a0469aa69b707394e Mon Sep 17 00:00:00 2001 From: meiskalt7 Date: Thu, 18 Apr 2019 21:42:57 +0700 Subject: [PATCH 305/315] add test for xsi:nil to null conversion --- src/test/java/org/json/junit/XMLTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 83a7cc252..195f70605 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -17,6 +17,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.json.XML; +import org.json.XMLParserConfiguration; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -856,4 +857,15 @@ public void testUnescape() { } + /** + * test passes when xsi:nil="true" converting to null (JSON specification-like conversion) + */ + @Test + public void testToJsonWithNull() { + final String originalXml = ""; + final String expectedJsonString = "{\"root\":{\"id\":null}}"; + + final JSONObject json = XML.toJSONObject(originalXml,new XMLParserConfiguration(false, "content", true)); + assertEquals(expectedJsonString, json.toString()); + } } \ No newline at end of file From fa173fa51a61ba68c5c721c3170869f5ae93a927 Mon Sep 17 00:00:00 2001 From: meiskalt7 Date: Sun, 21 Apr 2019 00:53:39 +0700 Subject: [PATCH 306/315] add test for xsi:nil to null conversion disabled --- src/test/java/org/json/junit/XMLTest.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 195f70605..b74daffe7 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -858,14 +858,26 @@ public void testUnescape() { } /** - * test passes when xsi:nil="true" converting to null (JSON specification-like conversion) + * test passes when xsi:nil="true" converting to null (JSON specification-like nil conversion enabled) */ @Test - public void testToJsonWithNull() { + public void testToJsonWithNullWhenNilConversionEnabled() { final String originalXml = ""; final String expectedJsonString = "{\"root\":{\"id\":null}}"; - final JSONObject json = XML.toJSONObject(originalXml,new XMLParserConfiguration(false, "content", true)); + final JSONObject json = XML.toJSONObject(originalXml, new XMLParserConfiguration(false, "content", true)); + assertEquals(expectedJsonString, json.toString()); + } + + /** + * test passes when xsi:nil="true" not converting to null (JSON specification-like nil conversion disabled) + */ + @Test + public void testToJsonWithNullWhenNilConversionDisabled() { + final String originalXml = ""; + final String expectedJsonString = "{\"root\":{\"id\":{\"xsi:nil\":true}}}"; + + final JSONObject json = XML.toJSONObject(originalXml, new XMLParserConfiguration()); assertEquals(expectedJsonString, json.toString()); } } \ No newline at end of file From 3e7a0b13d1c2ed0b18d5ba143ddaaf171c72932a Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 17 Sep 2019 10:36:48 -0400 Subject: [PATCH 307/315] new test case for issue 484 --- src/test/java/org/json/junit/JSONMLTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 84b33ba3d..26f4f906e 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -803,4 +803,16 @@ public void testToJSONObject_reversibility() { // // assertEquals(expectedJsonString, actualJsonString); // } + + @Test (timeout = 6000) + public void testIssue484InfinteLoop() { + try { + JSONML.toJSONObject("??*^M??|?CglR^F??`??>?w??PIlr^E??D^X^]?$?-^R?o??O?*??{OD?^FY??`2a????NM?b^Tq?:O?>S$^K?J?^FB.gUK?m^H??zE??^??!v]?^A???^[^A??^U?c??????h???s???g^Z???`?q^Dbi??:^QZl?)?}1^??k?0??:$V?$?Ovs(}J??^V????2;^QgQ?^_^A?^D?^U?Tg?K?`?h%c?hmGA? Date: Tue, 17 Sep 2019 10:47:16 -0400 Subject: [PATCH 308/315] Test cases updates for standardized exception messages --- src/test/java/org/json/junit/JSONArrayTest.java | 8 ++++---- src/test/java/org/json/junit/JSONMLTest.java | 6 +++--- src/test/java/org/json/junit/JSONObjectTest.java | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 3b70446f4..5aef3401d 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -346,14 +346,14 @@ public void failedGetArrayValues() { assertTrue("expected getDouble to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a number.",e.getMessage()); + "JSONArray[4] is not a double.",e.getMessage()); } try { jsonArray.getInt(4); assertTrue("expected getInt to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a number.",e.getMessage()); + "JSONArray[4] is not a int.",e.getMessage()); } try { jsonArray.getJSONArray(4); @@ -374,14 +374,14 @@ public void failedGetArrayValues() { assertTrue("expected getLong to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a number.",e.getMessage()); + "JSONArray[4] is not a long.",e.getMessage()); } try { jsonArray.getString(5); assertTrue("expected getString to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[5] not a string.",e.getMessage()); + "JSONArray[5] is not a String.",e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 84b33ba3d..fe3cd87e0 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -133,9 +133,9 @@ public void emptyTagException() { JSONML.toString(jsonArray); assertTrue("Expecting an exception", false); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONArray[0] not a string.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONArray[0] is not a String.", + e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 8d32649bf..ac679806d 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1041,7 +1041,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"trueKey\"] not a string.", + "JSONObject[\"trueKey\"] is not a string.", e.getMessage()); } try { @@ -1057,7 +1057,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.", + "JSONObject[\"stringKey\"] is not a double.", e.getMessage()); } try { @@ -1073,7 +1073,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.", + "JSONObject[\"stringKey\"] is not a float.", e.getMessage()); } try { @@ -1089,7 +1089,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.", + "JSONObject[\"stringKey\"] is not a int.", e.getMessage()); } try { @@ -1105,7 +1105,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.", + "JSONObject[\"stringKey\"] is not a long.", e.getMessage()); } try { @@ -2087,7 +2087,7 @@ public void jsonObjectParsingErrors() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[myKey] is not a JSONArray.", + "JSONObject[\"myKey\"] is not a JSONArray (null).", e.getMessage()); } try { From 67e59888a2764d5b36fa133473563d70da4136ac Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 17 Sep 2019 11:14:41 -0400 Subject: [PATCH 309/315] add second case for data in #484 --- src/test/java/org/json/junit/JSONMLTest.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 26f4f906e..b7afe40b1 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -2,6 +2,8 @@ import static org.junit.Assert.*; +import java.util.Base64; + import org.json.*; import org.junit.Test; @@ -805,7 +807,7 @@ public void testToJSONObject_reversibility() { // } @Test (timeout = 6000) - public void testIssue484InfinteLoop() { + public void testIssue484InfinteLoop1() { try { JSONML.toJSONObject("??*^M??|?CglR^F??`??>?w??PIlr^E??D^X^]?$?-^R?o??O?*??{OD?^FY??`2a????NM?b^Tq?:O?>S$^K?J?^FB.gUK?m^H??zE??^??!v]?^A???^[^A??^U?c??????h???s???g^Z???`?q^Dbi??:^QZl?)?}1^??k?0??:$V?$?Ovs(}J??^V????2;^QgQ?^_^A?^D?^U?Tg?K?`?h%c?hmGA??w??PIlr??D?$?-?o??O?*??{OD?Y??`2a????NM?bq?:O?>S$ ?J?B.gUK?m\b??zE???!v]???????c??????h???s???g???`?qbi??:Zl?)?}1^??k?0??:$V?$?Ovs(}J??????2;gQ????Tg?K?`?h%c?hmGA? Date: Tue, 17 Sep 2019 11:15:25 -0400 Subject: [PATCH 310/315] remove unused import --- src/test/java/org/json/junit/JSONMLTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index b7afe40b1..3687fe5e0 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -2,8 +2,6 @@ import static org.junit.Assert.*; -import java.util.Base64; - import org.json.*; import org.junit.Test; From 16da56eb34b6036f145dea7e6bf638b6ad418b5e Mon Sep 17 00:00:00 2001 From: Alanscut Date: Sat, 28 Dec 2019 17:53:27 +0800 Subject: [PATCH 311/315] improve the confused assert message --- .../java/org/json/junit/JSONObjectTest.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index ac679806d..438e55ec7 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -589,19 +589,19 @@ public void jsonObjectByBean2() { jsonObject.has("someString")); assertFalse("Normal field name (myDouble) processing did not work", jsonObject.has("myDouble")); - assertFalse("Normal field name (someFloat) found", + assertFalse("Normal field name (someFloat) processing did not work", jsonObject.has("someFloat")); - assertFalse("Ignored field found!", + assertFalse("Ignored field not found!", jsonObject.has("ignoredInt")); - assertTrue("Normal field name (someInt) processing did not work", + assertTrue("Normal field name (someInt) found", jsonObject.has("someInt")); - assertTrue("Normal field name (someLong) processing did not work", + assertTrue("Normal field name (someLong) found", jsonObject.has("someLong")); - assertTrue("Overridden String field name (myStringField) not found", + assertTrue("Overridden String field name (myStringField) found", jsonObject.has("myStringField")); - assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not found", + assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) found", jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); - assertTrue("Overridden String field name (InterfaceField) not found", + assertTrue("Overridden String field name (InterfaceField) found", jsonObject.has("InterfaceField")); } @@ -619,26 +619,29 @@ public void jsonObjectByBean3() { jsonObject.has("someInt")); assertFalse("Normal field name (myDouble) processing did not work", jsonObject.has("myDouble")); - assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) FOUND!", + assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not FOUND!", jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); - assertFalse("Normal field name (someFloat) found", + assertFalse("Normal field name (someFloat) found, but was overridden", jsonObject.has("someFloat")); - assertFalse("Ignored field found!", + assertFalse("Ignored field found! but was overridden", jsonObject.has("ignoredInt")); - assertFalse("Ignored field at the same level as forced name found", + assertFalse("Ignored field at the same level as forced name not found", jsonObject.has("ShouldBeIgnored")); - assertTrue("Overridden int field name (newIntFieldName) not found", + assertFalse("Normally ignored field (able) with explicit property name not found", + jsonObject.has("able")); + assertTrue("Overridden int field name (newIntFieldName) found", jsonObject.has("newIntFieldName")); - assertTrue("Normal field name (someLong) processing did not work", + assertTrue("Normal field name (someLong) found", jsonObject.has("someLong")); - assertTrue("Overridden String field name (myStringField) not found", + assertTrue("Overridden String field name (myStringField) found", jsonObject.has("myStringField")); - assertTrue(jsonObject.has("AMoreNormalName")); - assertTrue("Overridden String field name (InterfaceField) not found", + assertTrue("Overridden double field name (AMoreNormalName) found", + jsonObject.has("AMoreNormalName")); + assertTrue("Overridden String field name (InterfaceField) found", jsonObject.has("InterfaceField")); - assertTrue("Forced field not found!", + assertTrue("Forced field found!", jsonObject.has("forcedInt")); - assertTrue("Normally ignored field (getable) with explicit property name not found", + assertTrue("Overridden boolean field name (Getable) found", jsonObject.has("Getable")); } From 08719d4b3a6d73918631297030e8e03cc960de24 Mon Sep 17 00:00:00 2001 From: Alan Wang <948467222@qq.com> Date: Mon, 30 Dec 2019 09:51:08 +0800 Subject: [PATCH 312/315] Apply suggestions from code review Co-Authored-By: Sean Leary --- .../java/org/json/junit/JSONObjectTest.java | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 438e55ec7..b2f501e2f 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -593,15 +593,20 @@ public void jsonObjectByBean2() { jsonObject.has("someFloat")); assertFalse("Ignored field not found!", jsonObject.has("ignoredInt")); - assertTrue("Normal field name (someInt) found", + // getSomeInt() has no user-defined annotation + assertTrue("Normal field name (someInt) should have been found", jsonObject.has("someInt")); - assertTrue("Normal field name (someLong) found", + // the user-defined annotation does not replace any value, so someLong should be found + assertTrue("Normal field name (someLong) should have been found", jsonObject.has("someLong")); - assertTrue("Overridden String field name (myStringField) found", + // myStringField replaces someString property name via user-defined annotation + assertTrue("Overridden String field name (myStringField) should have been found", jsonObject.has("myStringField")); - assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) found", + // weird name replaces myDouble property name via user-defined annotation + assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) should have been found", jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); - assertTrue("Overridden String field name (InterfaceField) found", + // InterfaceField replaces someFloat property name via user-defined annotation + assertTrue("Overridden String field name (InterfaceField) should have been found", jsonObject.has("InterfaceField")); } @@ -619,29 +624,39 @@ public void jsonObjectByBean3() { jsonObject.has("someInt")); assertFalse("Normal field name (myDouble) processing did not work", jsonObject.has("myDouble")); - assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not FOUND!", + // myDouble was replaced by weird name, and then replaced again by AMoreNormalName via user-defined annotation + assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) should not be FOUND!", jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); assertFalse("Normal field name (someFloat) found, but was overridden", jsonObject.has("someFloat")); assertFalse("Ignored field found! but was overridden", jsonObject.has("ignoredInt")); - assertFalse("Ignored field at the same level as forced name not found", + // shouldNotBeJSON property name was first ignored, then replaced by ShouldBeIgnored via user-defined annotations + assertFalse("Ignored field at the same level as forced name should not have been found", jsonObject.has("ShouldBeIgnored")); - assertFalse("Normally ignored field (able) with explicit property name not found", + // able property name was replaced by Getable via user-defined annotation + assertFalse("Normally ignored field (able) with explicit property name should not have been found", jsonObject.has("able")); - assertTrue("Overridden int field name (newIntFieldName) found", + // property name someInt was replaced by newIntFieldName via user-defined annotation + assertTrue("Overridden int field name (newIntFieldName) should have been found", jsonObject.has("newIntFieldName")); - assertTrue("Normal field name (someLong) found", + // property name someLong was not replaced via user-defined annotation + assertTrue("Normal field name (someLong) should have been found", jsonObject.has("someLong")); - assertTrue("Overridden String field name (myStringField) found", + // property name someString was replaced by myStringField via user-defined annotation + assertTrue("Overridden String field name (myStringField) should have been found", jsonObject.has("myStringField")); - assertTrue("Overridden double field name (AMoreNormalName) found", + // property name myDouble was replaced by a weird name, followed by AMoreNormalName via user-defined annotations + assertTrue("Overridden double field name (AMoreNormalName) should have been found", jsonObject.has("AMoreNormalName")); - assertTrue("Overridden String field name (InterfaceField) found", + // property name someFloat was replaced by InterfaceField via user-defined annotation + assertTrue("Overridden String field name (InterfaceField) should have been found", jsonObject.has("InterfaceField")); - assertTrue("Forced field found!", + // property name ignoredInt was replaced by none, followed by forcedInt via user-defined annotations + assertTrue("Forced field should have been found!", jsonObject.has("forcedInt")); - assertTrue("Overridden boolean field name (Getable) found", + // property name able was replaced by Getable via user-defined annotation + assertTrue("Overridden boolean field name (Getable) should have been found", jsonObject.has("Getable")); } From 74e4932cfc74e084706564d1b82c1567bf4fa35e Mon Sep 17 00:00:00 2001 From: Benjamin Gehrels Date: Wed, 29 Apr 2020 19:24:44 +0200 Subject: [PATCH 313/315] Transform the repository into standard maven format and merge the pom.xml of the release repo --- .gitignore | 1 + pom.xml | 192 ++++ CDL.java => src/main/java/org/json/CDL.java | 0 .../main/java/org/json/Cookie.java | 0 .../main/java/org/json/CookieList.java | 0 HTTP.java => src/main/java/org/json/HTTP.java | 324 +++---- .../main/java/org/json/HTTPTokener.java | 0 .../main/java/org/json/JSONArray.java | 0 .../main/java/org/json/JSONException.java | 0 .../main/java/org/json/JSONML.java | 0 .../main/java/org/json/JSONObject.java | 0 .../main/java/org/json/JSONPointer.java | 0 .../java/org/json/JSONPointerException.java | 0 .../java/org/json/JSONPropertyIgnore.java | 0 .../main/java/org/json/JSONPropertyName.java | 0 .../main/java/org/json/JSONString.java | 0 .../main/java/org/json/JSONStringer.java | 158 ++-- .../main/java/org/json/JSONTokener.java | 0 .../main/java/org/json/JSONWriter.java | 826 +++++++++--------- .../main/java/org/json/Property.java | 0 XML.java => src/main/java/org/json/XML.java | 0 .../java/org/json/XMLParserConfiguration.java | 0 .../main/java/org/json/XMLTokener.java | 0 23 files changed, 847 insertions(+), 654 deletions(-) create mode 100644 pom.xml rename CDL.java => src/main/java/org/json/CDL.java (100%) rename Cookie.java => src/main/java/org/json/Cookie.java (100%) rename CookieList.java => src/main/java/org/json/CookieList.java (100%) rename HTTP.java => src/main/java/org/json/HTTP.java (97%) rename HTTPTokener.java => src/main/java/org/json/HTTPTokener.java (100%) rename JSONArray.java => src/main/java/org/json/JSONArray.java (100%) rename JSONException.java => src/main/java/org/json/JSONException.java (100%) rename JSONML.java => src/main/java/org/json/JSONML.java (100%) rename JSONObject.java => src/main/java/org/json/JSONObject.java (100%) rename JSONPointer.java => src/main/java/org/json/JSONPointer.java (100%) rename JSONPointerException.java => src/main/java/org/json/JSONPointerException.java (100%) rename JSONPropertyIgnore.java => src/main/java/org/json/JSONPropertyIgnore.java (100%) rename JSONPropertyName.java => src/main/java/org/json/JSONPropertyName.java (100%) rename JSONString.java => src/main/java/org/json/JSONString.java (100%) rename JSONStringer.java => src/main/java/org/json/JSONStringer.java (97%) rename JSONTokener.java => src/main/java/org/json/JSONTokener.java (100%) rename JSONWriter.java => src/main/java/org/json/JSONWriter.java (97%) rename Property.java => src/main/java/org/json/Property.java (100%) rename XML.java => src/main/java/org/json/XML.java (100%) rename XMLParserConfiguration.java => src/main/java/org/json/XMLParserConfiguration.java (100%) rename XMLTokener.java => src/main/java/org/json/XMLTokener.java (100%) diff --git a/.gitignore b/.gitignore index 50b216e17..4f807a9d9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ # ignore Intellij Idea project files .idea *.iml +/target/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 000000000..c2579f283 --- /dev/null +++ b/pom.xml @@ -0,0 +1,192 @@ + + 4.0.0 + + org.json + json + v20200429-SNAPSHOT + bundle + + JSON in Java + + JSON is a light-weight, language independent, data interchange format. + See http://www.JSON.org/ + + The files in this package implement JSON encoders/decoders in Java. + It also includes the capability to convert between JSON and XML, HTTP + headers, Cookies, and CDL. + + This is a reference implementation. There is a large number of JSON packages + in Java. Perhaps someday the Java community will standardize on one. Until + then, choose carefully. + + The license includes this restriction: "The software shall be used for good, + not evil." If your conscience cannot live with that, then choose a different + package. + + https://github.com/douglascrockford/JSON-java + + + org.sonatype.oss + oss-parent + 9 + + + + https://github.com/douglascrockford/JSON-java.git + scm:git:git://github.com/douglascrockford/JSON-java.git + scm:git:git@github.com:douglascrockford/JSON-java.git + + + + + The JSON License + http://json.org/license.html + repo + Copyright (c) 2002 JSON.org + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + associated documentation files (the "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the + following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial + portions of the Software. + + The Software shall be used for Good, not Evil. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT + LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + + + + + + Douglas Crockford + douglas@crockford.com + + + + + UTF-8 + + + + + + junit + junit + 4.12 + test + + + com.jayway.jsonpath + json-path + 2.1.0 + test + + + org.mockito + mockito-core + 1.9.5 + test + + + + + + + org.apache.felix + maven-bundle-plugin + 3.0.1 + true + + + + org.json + + ${project.artifactId} + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-source-plugin + 2.1.2 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.7 + + + attach-javadocs + + jar + + + -Xdoclint:none + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.3 + true + + ossrh + https://oss.sonatype.org/ + false + + + + org.apache.maven.plugins + maven-jar-plugin + + + + org.json + + + + + + + diff --git a/CDL.java b/src/main/java/org/json/CDL.java similarity index 100% rename from CDL.java rename to src/main/java/org/json/CDL.java diff --git a/Cookie.java b/src/main/java/org/json/Cookie.java similarity index 100% rename from Cookie.java rename to src/main/java/org/json/Cookie.java diff --git a/CookieList.java b/src/main/java/org/json/CookieList.java similarity index 100% rename from CookieList.java rename to src/main/java/org/json/CookieList.java diff --git a/HTTP.java b/src/main/java/org/json/HTTP.java similarity index 97% rename from HTTP.java rename to src/main/java/org/json/HTTP.java index 70b88ee6c..84ed53bae 100644 --- a/HTTP.java +++ b/src/main/java/org/json/HTTP.java @@ -1,162 +1,162 @@ -package org.json; - -/* -Copyright (c) 2002 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -import java.util.Locale; - -/** - * Convert an HTTP header to a JSONObject and back. - * @author JSON.org - * @version 2015-12-09 - */ -public class HTTP { - - /** Carriage return/line feed. */ - public static final String CRLF = "\r\n"; - - /** - * Convert an HTTP header string into a JSONObject. It can be a request - * header or a response header. A request header will contain - *
{
-     *    Method: "POST" (for example),
-     *    "Request-URI": "/" (for example),
-     *    "HTTP-Version": "HTTP/1.1" (for example)
-     * }
- * A response header will contain - *
{
-     *    "HTTP-Version": "HTTP/1.1" (for example),
-     *    "Status-Code": "200" (for example),
-     *    "Reason-Phrase": "OK" (for example)
-     * }
- * In addition, the other parameters in the header will be captured, using - * the HTTP field names as JSON names, so that
-     *    Date: Sun, 26 May 2002 18:06:04 GMT
-     *    Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
-     *    Cache-Control: no-cache
- * become - *
{...
-     *    Date: "Sun, 26 May 2002 18:06:04 GMT",
-     *    Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
-     *    "Cache-Control": "no-cache",
-     * ...}
- * It does no further checking or conversion. It does not parse dates. - * It does not do '%' transforms on URLs. - * @param string An HTTP header string. - * @return A JSONObject containing the elements and attributes - * of the XML string. - * @throws JSONException - */ - public static JSONObject toJSONObject(String string) throws JSONException { - JSONObject jo = new JSONObject(); - HTTPTokener x = new HTTPTokener(string); - String token; - - token = x.nextToken(); - if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) { - -// Response - - jo.put("HTTP-Version", token); - jo.put("Status-Code", x.nextToken()); - jo.put("Reason-Phrase", x.nextTo('\0')); - x.next(); - - } else { - -// Request - - jo.put("Method", token); - jo.put("Request-URI", x.nextToken()); - jo.put("HTTP-Version", x.nextToken()); - } - -// Fields - - while (x.more()) { - String name = x.nextTo(':'); - x.next(':'); - jo.put(name, x.nextTo('\0')); - x.next(); - } - return jo; - } - - - /** - * Convert a JSONObject into an HTTP header. A request header must contain - *
{
-     *    Method: "POST" (for example),
-     *    "Request-URI": "/" (for example),
-     *    "HTTP-Version": "HTTP/1.1" (for example)
-     * }
- * A response header must contain - *
{
-     *    "HTTP-Version": "HTTP/1.1" (for example),
-     *    "Status-Code": "200" (for example),
-     *    "Reason-Phrase": "OK" (for example)
-     * }
- * Any other members of the JSONObject will be output as HTTP fields. - * The result will end with two CRLF pairs. - * @param jo A JSONObject - * @return An HTTP header string. - * @throws JSONException if the object does not contain enough - * information. - */ - public static String toString(JSONObject jo) throws JSONException { - StringBuilder sb = new StringBuilder(); - if (jo.has("Status-Code") && jo.has("Reason-Phrase")) { - sb.append(jo.getString("HTTP-Version")); - sb.append(' '); - sb.append(jo.getString("Status-Code")); - sb.append(' '); - sb.append(jo.getString("Reason-Phrase")); - } else if (jo.has("Method") && jo.has("Request-URI")) { - sb.append(jo.getString("Method")); - sb.append(' '); - sb.append('"'); - sb.append(jo.getString("Request-URI")); - sb.append('"'); - sb.append(' '); - sb.append(jo.getString("HTTP-Version")); - } else { - throw new JSONException("Not enough material for an HTTP header."); - } - sb.append(CRLF); - // Don't use the new entrySet API to maintain Android support - for (final String key : jo.keySet()) { - String value = jo.optString(key); - if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) && - !"Reason-Phrase".equals(key) && !"Method".equals(key) && - !"Request-URI".equals(key) && !JSONObject.NULL.equals(value)) { - sb.append(key); - sb.append(": "); - sb.append(jo.optString(key)); - sb.append(CRLF); - } - } - sb.append(CRLF); - return sb.toString(); - } -} +package org.json; + +/* +Copyright (c) 2002 JSON.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +The Software shall be used for Good, not Evil. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +import java.util.Locale; + +/** + * Convert an HTTP header to a JSONObject and back. + * @author JSON.org + * @version 2015-12-09 + */ +public class HTTP { + + /** Carriage return/line feed. */ + public static final String CRLF = "\r\n"; + + /** + * Convert an HTTP header string into a JSONObject. It can be a request + * header or a response header. A request header will contain + *
{
+     *    Method: "POST" (for example),
+     *    "Request-URI": "/" (for example),
+     *    "HTTP-Version": "HTTP/1.1" (for example)
+     * }
+ * A response header will contain + *
{
+     *    "HTTP-Version": "HTTP/1.1" (for example),
+     *    "Status-Code": "200" (for example),
+     *    "Reason-Phrase": "OK" (for example)
+     * }
+ * In addition, the other parameters in the header will be captured, using + * the HTTP field names as JSON names, so that
+     *    Date: Sun, 26 May 2002 18:06:04 GMT
+     *    Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
+     *    Cache-Control: no-cache
+ * become + *
{...
+     *    Date: "Sun, 26 May 2002 18:06:04 GMT",
+     *    Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
+     *    "Cache-Control": "no-cache",
+     * ...}
+ * It does no further checking or conversion. It does not parse dates. + * It does not do '%' transforms on URLs. + * @param string An HTTP header string. + * @return A JSONObject containing the elements and attributes + * of the XML string. + * @throws JSONException + */ + public static JSONObject toJSONObject(String string) throws JSONException { + JSONObject jo = new JSONObject(); + HTTPTokener x = new HTTPTokener(string); + String token; + + token = x.nextToken(); + if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) { + +// Response + + jo.put("HTTP-Version", token); + jo.put("Status-Code", x.nextToken()); + jo.put("Reason-Phrase", x.nextTo('\0')); + x.next(); + + } else { + +// Request + + jo.put("Method", token); + jo.put("Request-URI", x.nextToken()); + jo.put("HTTP-Version", x.nextToken()); + } + +// Fields + + while (x.more()) { + String name = x.nextTo(':'); + x.next(':'); + jo.put(name, x.nextTo('\0')); + x.next(); + } + return jo; + } + + + /** + * Convert a JSONObject into an HTTP header. A request header must contain + *
{
+     *    Method: "POST" (for example),
+     *    "Request-URI": "/" (for example),
+     *    "HTTP-Version": "HTTP/1.1" (for example)
+     * }
+ * A response header must contain + *
{
+     *    "HTTP-Version": "HTTP/1.1" (for example),
+     *    "Status-Code": "200" (for example),
+     *    "Reason-Phrase": "OK" (for example)
+     * }
+ * Any other members of the JSONObject will be output as HTTP fields. + * The result will end with two CRLF pairs. + * @param jo A JSONObject + * @return An HTTP header string. + * @throws JSONException if the object does not contain enough + * information. + */ + public static String toString(JSONObject jo) throws JSONException { + StringBuilder sb = new StringBuilder(); + if (jo.has("Status-Code") && jo.has("Reason-Phrase")) { + sb.append(jo.getString("HTTP-Version")); + sb.append(' '); + sb.append(jo.getString("Status-Code")); + sb.append(' '); + sb.append(jo.getString("Reason-Phrase")); + } else if (jo.has("Method") && jo.has("Request-URI")) { + sb.append(jo.getString("Method")); + sb.append(' '); + sb.append('"'); + sb.append(jo.getString("Request-URI")); + sb.append('"'); + sb.append(' '); + sb.append(jo.getString("HTTP-Version")); + } else { + throw new JSONException("Not enough material for an HTTP header."); + } + sb.append(CRLF); + // Don't use the new entrySet API to maintain Android support + for (final String key : jo.keySet()) { + String value = jo.optString(key); + if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) && + !"Reason-Phrase".equals(key) && !"Method".equals(key) && + !"Request-URI".equals(key) && !JSONObject.NULL.equals(value)) { + sb.append(key); + sb.append(": "); + sb.append(jo.optString(key)); + sb.append(CRLF); + } + } + sb.append(CRLF); + return sb.toString(); + } +} diff --git a/HTTPTokener.java b/src/main/java/org/json/HTTPTokener.java similarity index 100% rename from HTTPTokener.java rename to src/main/java/org/json/HTTPTokener.java diff --git a/JSONArray.java b/src/main/java/org/json/JSONArray.java similarity index 100% rename from JSONArray.java rename to src/main/java/org/json/JSONArray.java diff --git a/JSONException.java b/src/main/java/org/json/JSONException.java similarity index 100% rename from JSONException.java rename to src/main/java/org/json/JSONException.java diff --git a/JSONML.java b/src/main/java/org/json/JSONML.java similarity index 100% rename from JSONML.java rename to src/main/java/org/json/JSONML.java diff --git a/JSONObject.java b/src/main/java/org/json/JSONObject.java similarity index 100% rename from JSONObject.java rename to src/main/java/org/json/JSONObject.java diff --git a/JSONPointer.java b/src/main/java/org/json/JSONPointer.java similarity index 100% rename from JSONPointer.java rename to src/main/java/org/json/JSONPointer.java diff --git a/JSONPointerException.java b/src/main/java/org/json/JSONPointerException.java similarity index 100% rename from JSONPointerException.java rename to src/main/java/org/json/JSONPointerException.java diff --git a/JSONPropertyIgnore.java b/src/main/java/org/json/JSONPropertyIgnore.java similarity index 100% rename from JSONPropertyIgnore.java rename to src/main/java/org/json/JSONPropertyIgnore.java diff --git a/JSONPropertyName.java b/src/main/java/org/json/JSONPropertyName.java similarity index 100% rename from JSONPropertyName.java rename to src/main/java/org/json/JSONPropertyName.java diff --git a/JSONString.java b/src/main/java/org/json/JSONString.java similarity index 100% rename from JSONString.java rename to src/main/java/org/json/JSONString.java diff --git a/JSONStringer.java b/src/main/java/org/json/JSONStringer.java similarity index 97% rename from JSONStringer.java rename to src/main/java/org/json/JSONStringer.java index 6e05d228f..bb9e7a4cf 100644 --- a/JSONStringer.java +++ b/src/main/java/org/json/JSONStringer.java @@ -1,79 +1,79 @@ -package org.json; - -/* -Copyright (c) 2006 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -import java.io.StringWriter; - -/** - * JSONStringer provides a quick and convenient way of producing JSON text. - * The texts produced strictly conform to JSON syntax rules. No whitespace is - * added, so the results are ready for transmission or storage. Each instance of - * JSONStringer can produce one JSON text. - *

- * A JSONStringer instance provides a value method for appending - * values to the - * text, and a key - * method for adding keys before values in objects. There are array - * and endArray methods that make and bound array values, and - * object and endObject methods which make and bound - * object values. All of these methods return the JSONWriter instance, - * permitting cascade style. For example,

- * myString = new JSONStringer()
- *     .object()
- *         .key("JSON")
- *         .value("Hello, World!")
- *     .endObject()
- *     .toString();
which produces the string
- * {"JSON":"Hello, World!"}
- *

- * The first method called must be array or object. - * There are no methods for adding commas or colons. JSONStringer adds them for - * you. Objects and arrays can be nested up to 20 levels deep. - *

- * This can sometimes be easier than using a JSONObject to build a string. - * @author JSON.org - * @version 2015-12-09 - */ -public class JSONStringer extends JSONWriter { - /** - * Make a fresh JSONStringer. It can be used to build one JSON text. - */ - public JSONStringer() { - super(new StringWriter()); - } - - /** - * Return the JSON text. This method is used to obtain the product of the - * JSONStringer instance. It will return null if there was a - * problem in the construction of the JSON text (such as the calls to - * array were not properly balanced with calls to - * endArray). - * @return The JSON text. - */ - @Override - public String toString() { - return this.mode == 'd' ? this.writer.toString() : null; - } -} +package org.json; + +/* +Copyright (c) 2006 JSON.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +The Software shall be used for Good, not Evil. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +import java.io.StringWriter; + +/** + * JSONStringer provides a quick and convenient way of producing JSON text. + * The texts produced strictly conform to JSON syntax rules. No whitespace is + * added, so the results are ready for transmission or storage. Each instance of + * JSONStringer can produce one JSON text. + *

+ * A JSONStringer instance provides a value method for appending + * values to the + * text, and a key + * method for adding keys before values in objects. There are array + * and endArray methods that make and bound array values, and + * object and endObject methods which make and bound + * object values. All of these methods return the JSONWriter instance, + * permitting cascade style. For example,

+ * myString = new JSONStringer()
+ *     .object()
+ *         .key("JSON")
+ *         .value("Hello, World!")
+ *     .endObject()
+ *     .toString();
which produces the string
+ * {"JSON":"Hello, World!"}
+ *

+ * The first method called must be array or object. + * There are no methods for adding commas or colons. JSONStringer adds them for + * you. Objects and arrays can be nested up to 20 levels deep. + *

+ * This can sometimes be easier than using a JSONObject to build a string. + * @author JSON.org + * @version 2015-12-09 + */ +public class JSONStringer extends JSONWriter { + /** + * Make a fresh JSONStringer. It can be used to build one JSON text. + */ + public JSONStringer() { + super(new StringWriter()); + } + + /** + * Return the JSON text. This method is used to obtain the product of the + * JSONStringer instance. It will return null if there was a + * problem in the construction of the JSON text (such as the calls to + * array were not properly balanced with calls to + * endArray). + * @return The JSON text. + */ + @Override + public String toString() { + return this.mode == 'd' ? this.writer.toString() : null; + } +} diff --git a/JSONTokener.java b/src/main/java/org/json/JSONTokener.java similarity index 100% rename from JSONTokener.java rename to src/main/java/org/json/JSONTokener.java diff --git a/JSONWriter.java b/src/main/java/org/json/JSONWriter.java similarity index 97% rename from JSONWriter.java rename to src/main/java/org/json/JSONWriter.java index 19f2dc816..b61a6f13c 100644 --- a/JSONWriter.java +++ b/src/main/java/org/json/JSONWriter.java @@ -1,413 +1,413 @@ -package org.json; - -import java.io.IOException; -import java.util.Collection; -import java.util.Map; - -/* -Copyright (c) 2006 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -/** - * JSONWriter provides a quick and convenient way of producing JSON text. - * The texts produced strictly conform to JSON syntax rules. No whitespace is - * added, so the results are ready for transmission or storage. Each instance of - * JSONWriter can produce one JSON text. - *

- * A JSONWriter instance provides a value method for appending - * values to the - * text, and a key - * method for adding keys before values in objects. There are array - * and endArray methods that make and bound array values, and - * object and endObject methods which make and bound - * object values. All of these methods return the JSONWriter instance, - * permitting a cascade style. For example,

- * new JSONWriter(myWriter)
- *     .object()
- *         .key("JSON")
- *         .value("Hello, World!")
- *     .endObject();
which writes
- * {"JSON":"Hello, World!"}
- *

- * The first method called must be array or object. - * There are no methods for adding commas or colons. JSONWriter adds them for - * you. Objects and arrays can be nested up to 200 levels deep. - *

- * This can sometimes be easier than using a JSONObject to build a string. - * @author JSON.org - * @version 2016-08-08 - */ -public class JSONWriter { - private static final int maxdepth = 200; - - /** - * The comma flag determines if a comma should be output before the next - * value. - */ - private boolean comma; - - /** - * The current mode. Values: - * 'a' (array), - * 'd' (done), - * 'i' (initial), - * 'k' (key), - * 'o' (object). - */ - protected char mode; - - /** - * The object/array stack. - */ - private final JSONObject stack[]; - - /** - * The stack top index. A value of 0 indicates that the stack is empty. - */ - private int top; - - /** - * The writer that will receive the output. - */ - protected Appendable writer; - - /** - * Make a fresh JSONWriter. It can be used to build one JSON text. - */ - public JSONWriter(Appendable w) { - this.comma = false; - this.mode = 'i'; - this.stack = new JSONObject[maxdepth]; - this.top = 0; - this.writer = w; - } - - /** - * Append a value. - * @param string A string value. - * @return this - * @throws JSONException If the value is out of sequence. - */ - private JSONWriter append(String string) throws JSONException { - if (string == null) { - throw new JSONException("Null pointer"); - } - if (this.mode == 'o' || this.mode == 'a') { - try { - if (this.comma && this.mode == 'a') { - this.writer.append(','); - } - this.writer.append(string); - } catch (IOException e) { - // Android as of API 25 does not support this exception constructor - // however we won't worry about it. If an exception is happening here - // it will just throw a "Method not found" exception instead. - throw new JSONException(e); - } - if (this.mode == 'o') { - this.mode = 'k'; - } - this.comma = true; - return this; - } - throw new JSONException("Value out of sequence."); - } - - /** - * Begin appending a new array. All values until the balancing - * endArray will be appended to this array. The - * endArray method must be called to mark the array's end. - * @return this - * @throws JSONException If the nesting is too deep, or if the object is - * started in the wrong place (for example as a key or after the end of the - * outermost array or object). - */ - public JSONWriter array() throws JSONException { - if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') { - this.push(null); - this.append("["); - this.comma = false; - return this; - } - throw new JSONException("Misplaced array."); - } - - /** - * End something. - * @param m Mode - * @param c Closing character - * @return this - * @throws JSONException If unbalanced. - */ - private JSONWriter end(char m, char c) throws JSONException { - if (this.mode != m) { - throw new JSONException(m == 'a' - ? "Misplaced endArray." - : "Misplaced endObject."); - } - this.pop(m); - try { - this.writer.append(c); - } catch (IOException e) { - // Android as of API 25 does not support this exception constructor - // however we won't worry about it. If an exception is happening here - // it will just throw a "Method not found" exception instead. - throw new JSONException(e); - } - this.comma = true; - return this; - } - - /** - * End an array. This method most be called to balance calls to - * array. - * @return this - * @throws JSONException If incorrectly nested. - */ - public JSONWriter endArray() throws JSONException { - return this.end('a', ']'); - } - - /** - * End an object. This method most be called to balance calls to - * object. - * @return this - * @throws JSONException If incorrectly nested. - */ - public JSONWriter endObject() throws JSONException { - return this.end('k', '}'); - } - - /** - * Append a key. The key will be associated with the next value. In an - * object, every value must be preceded by a key. - * @param string A key string. - * @return this - * @throws JSONException If the key is out of place. For example, keys - * do not belong in arrays or if the key is null. - */ - public JSONWriter key(String string) throws JSONException { - if (string == null) { - throw new JSONException("Null key."); - } - if (this.mode == 'k') { - try { - JSONObject topObject = this.stack[this.top - 1]; - // don't use the built in putOnce method to maintain Android support - if(topObject.has(string)) { - throw new JSONException("Duplicate key \"" + string + "\""); - } - topObject.put(string, true); - if (this.comma) { - this.writer.append(','); - } - this.writer.append(JSONObject.quote(string)); - this.writer.append(':'); - this.comma = false; - this.mode = 'o'; - return this; - } catch (IOException e) { - // Android as of API 25 does not support this exception constructor - // however we won't worry about it. If an exception is happening here - // it will just throw a "Method not found" exception instead. - throw new JSONException(e); - } - } - throw new JSONException("Misplaced key."); - } - - - /** - * Begin appending a new object. All keys and values until the balancing - * endObject will be appended to this object. The - * endObject method must be called to mark the object's end. - * @return this - * @throws JSONException If the nesting is too deep, or if the object is - * started in the wrong place (for example as a key or after the end of the - * outermost array or object). - */ - public JSONWriter object() throws JSONException { - if (this.mode == 'i') { - this.mode = 'o'; - } - if (this.mode == 'o' || this.mode == 'a') { - this.append("{"); - this.push(new JSONObject()); - this.comma = false; - return this; - } - throw new JSONException("Misplaced object."); - - } - - - /** - * Pop an array or object scope. - * @param c The scope to close. - * @throws JSONException If nesting is wrong. - */ - private void pop(char c) throws JSONException { - if (this.top <= 0) { - throw new JSONException("Nesting error."); - } - char m = this.stack[this.top - 1] == null ? 'a' : 'k'; - if (m != c) { - throw new JSONException("Nesting error."); - } - this.top -= 1; - this.mode = this.top == 0 - ? 'd' - : this.stack[this.top - 1] == null - ? 'a' - : 'k'; - } - - /** - * Push an array or object scope. - * @param jo The scope to open. - * @throws JSONException If nesting is too deep. - */ - private void push(JSONObject jo) throws JSONException { - if (this.top >= maxdepth) { - throw new JSONException("Nesting too deep."); - } - this.stack[this.top] = jo; - this.mode = jo == null ? 'a' : 'k'; - this.top += 1; - } - - /** - * Make a JSON text of an Object value. If the object has an - * value.toJSONString() method, then that method will be used to produce the - * JSON text. The method is required to produce a strictly conforming text. - * If the object does not contain a toJSONString method (which is the most - * common case), then a text will be produced by other means. If the value - * is an array or Collection, then a JSONArray will be made from it and its - * toJSONString method will be called. If the value is a MAP, then a - * JSONObject will be made from it and its toJSONString method will be - * called. Otherwise, the value's toString method will be called, and the - * result will be quoted. - * - *

- * Warning: This method assumes that the data structure is acyclical. - * - * @param value - * The value to be serialized. - * @return a printable, displayable, transmittable representation of the - * object, beginning with { (left - * brace) and ending with } (right - * brace). - * @throws JSONException - * If the value is or contains an invalid number. - */ - public static String valueToString(Object value) throws JSONException { - if (value == null || value.equals(null)) { - return "null"; - } - if (value instanceof JSONString) { - String object; - try { - object = ((JSONString) value).toJSONString(); - } catch (Exception e) { - throw new JSONException(e); - } - if (object != null) { - return object; - } - throw new JSONException("Bad value from toJSONString: " + object); - } - if (value instanceof Number) { - // not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex - final String numberAsString = JSONObject.numberToString((Number) value); - if(JSONObject.NUMBER_PATTERN.matcher(numberAsString).matches()) { - // Close enough to a JSON number that we will return it unquoted - return numberAsString; - } - // The Number value is not a valid JSON number. - // Instead we will quote it as a string - return JSONObject.quote(numberAsString); - } - if (value instanceof Boolean || value instanceof JSONObject - || value instanceof JSONArray) { - return value.toString(); - } - if (value instanceof Map) { - Map map = (Map) value; - return new JSONObject(map).toString(); - } - if (value instanceof Collection) { - Collection coll = (Collection) value; - return new JSONArray(coll).toString(); - } - if (value.getClass().isArray()) { - return new JSONArray(value).toString(); - } - if(value instanceof Enum){ - return JSONObject.quote(((Enum)value).name()); - } - return JSONObject.quote(value.toString()); - } - - /** - * Append either the value true or the value - * false. - * @param b A boolean. - * @return this - * @throws JSONException - */ - public JSONWriter value(boolean b) throws JSONException { - return this.append(b ? "true" : "false"); - } - - /** - * Append a double value. - * @param d A double. - * @return this - * @throws JSONException If the number is not finite. - */ - public JSONWriter value(double d) throws JSONException { - return this.value(Double.valueOf(d)); - } - - /** - * Append a long value. - * @param l A long. - * @return this - * @throws JSONException - */ - public JSONWriter value(long l) throws JSONException { - return this.append(Long.toString(l)); - } - - - /** - * Append an object value. - * @param object The object to append. It can be null, or a Boolean, Number, - * String, JSONObject, or JSONArray, or an object that implements JSONString. - * @return this - * @throws JSONException If the value is out of sequence. - */ - public JSONWriter value(Object object) throws JSONException { - return this.append(valueToString(object)); - } -} +package org.json; + +import java.io.IOException; +import java.util.Collection; +import java.util.Map; + +/* +Copyright (c) 2006 JSON.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +The Software shall be used for Good, not Evil. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +/** + * JSONWriter provides a quick and convenient way of producing JSON text. + * The texts produced strictly conform to JSON syntax rules. No whitespace is + * added, so the results are ready for transmission or storage. Each instance of + * JSONWriter can produce one JSON text. + *

+ * A JSONWriter instance provides a value method for appending + * values to the + * text, and a key + * method for adding keys before values in objects. There are array + * and endArray methods that make and bound array values, and + * object and endObject methods which make and bound + * object values. All of these methods return the JSONWriter instance, + * permitting a cascade style. For example,

+ * new JSONWriter(myWriter)
+ *     .object()
+ *         .key("JSON")
+ *         .value("Hello, World!")
+ *     .endObject();
which writes
+ * {"JSON":"Hello, World!"}
+ *

+ * The first method called must be array or object. + * There are no methods for adding commas or colons. JSONWriter adds them for + * you. Objects and arrays can be nested up to 200 levels deep. + *

+ * This can sometimes be easier than using a JSONObject to build a string. + * @author JSON.org + * @version 2016-08-08 + */ +public class JSONWriter { + private static final int maxdepth = 200; + + /** + * The comma flag determines if a comma should be output before the next + * value. + */ + private boolean comma; + + /** + * The current mode. Values: + * 'a' (array), + * 'd' (done), + * 'i' (initial), + * 'k' (key), + * 'o' (object). + */ + protected char mode; + + /** + * The object/array stack. + */ + private final JSONObject stack[]; + + /** + * The stack top index. A value of 0 indicates that the stack is empty. + */ + private int top; + + /** + * The writer that will receive the output. + */ + protected Appendable writer; + + /** + * Make a fresh JSONWriter. It can be used to build one JSON text. + */ + public JSONWriter(Appendable w) { + this.comma = false; + this.mode = 'i'; + this.stack = new JSONObject[maxdepth]; + this.top = 0; + this.writer = w; + } + + /** + * Append a value. + * @param string A string value. + * @return this + * @throws JSONException If the value is out of sequence. + */ + private JSONWriter append(String string) throws JSONException { + if (string == null) { + throw new JSONException("Null pointer"); + } + if (this.mode == 'o' || this.mode == 'a') { + try { + if (this.comma && this.mode == 'a') { + this.writer.append(','); + } + this.writer.append(string); + } catch (IOException e) { + // Android as of API 25 does not support this exception constructor + // however we won't worry about it. If an exception is happening here + // it will just throw a "Method not found" exception instead. + throw new JSONException(e); + } + if (this.mode == 'o') { + this.mode = 'k'; + } + this.comma = true; + return this; + } + throw new JSONException("Value out of sequence."); + } + + /** + * Begin appending a new array. All values until the balancing + * endArray will be appended to this array. The + * endArray method must be called to mark the array's end. + * @return this + * @throws JSONException If the nesting is too deep, or if the object is + * started in the wrong place (for example as a key or after the end of the + * outermost array or object). + */ + public JSONWriter array() throws JSONException { + if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') { + this.push(null); + this.append("["); + this.comma = false; + return this; + } + throw new JSONException("Misplaced array."); + } + + /** + * End something. + * @param m Mode + * @param c Closing character + * @return this + * @throws JSONException If unbalanced. + */ + private JSONWriter end(char m, char c) throws JSONException { + if (this.mode != m) { + throw new JSONException(m == 'a' + ? "Misplaced endArray." + : "Misplaced endObject."); + } + this.pop(m); + try { + this.writer.append(c); + } catch (IOException e) { + // Android as of API 25 does not support this exception constructor + // however we won't worry about it. If an exception is happening here + // it will just throw a "Method not found" exception instead. + throw new JSONException(e); + } + this.comma = true; + return this; + } + + /** + * End an array. This method most be called to balance calls to + * array. + * @return this + * @throws JSONException If incorrectly nested. + */ + public JSONWriter endArray() throws JSONException { + return this.end('a', ']'); + } + + /** + * End an object. This method most be called to balance calls to + * object. + * @return this + * @throws JSONException If incorrectly nested. + */ + public JSONWriter endObject() throws JSONException { + return this.end('k', '}'); + } + + /** + * Append a key. The key will be associated with the next value. In an + * object, every value must be preceded by a key. + * @param string A key string. + * @return this + * @throws JSONException If the key is out of place. For example, keys + * do not belong in arrays or if the key is null. + */ + public JSONWriter key(String string) throws JSONException { + if (string == null) { + throw new JSONException("Null key."); + } + if (this.mode == 'k') { + try { + JSONObject topObject = this.stack[this.top - 1]; + // don't use the built in putOnce method to maintain Android support + if(topObject.has(string)) { + throw new JSONException("Duplicate key \"" + string + "\""); + } + topObject.put(string, true); + if (this.comma) { + this.writer.append(','); + } + this.writer.append(JSONObject.quote(string)); + this.writer.append(':'); + this.comma = false; + this.mode = 'o'; + return this; + } catch (IOException e) { + // Android as of API 25 does not support this exception constructor + // however we won't worry about it. If an exception is happening here + // it will just throw a "Method not found" exception instead. + throw new JSONException(e); + } + } + throw new JSONException("Misplaced key."); + } + + + /** + * Begin appending a new object. All keys and values until the balancing + * endObject will be appended to this object. The + * endObject method must be called to mark the object's end. + * @return this + * @throws JSONException If the nesting is too deep, or if the object is + * started in the wrong place (for example as a key or after the end of the + * outermost array or object). + */ + public JSONWriter object() throws JSONException { + if (this.mode == 'i') { + this.mode = 'o'; + } + if (this.mode == 'o' || this.mode == 'a') { + this.append("{"); + this.push(new JSONObject()); + this.comma = false; + return this; + } + throw new JSONException("Misplaced object."); + + } + + + /** + * Pop an array or object scope. + * @param c The scope to close. + * @throws JSONException If nesting is wrong. + */ + private void pop(char c) throws JSONException { + if (this.top <= 0) { + throw new JSONException("Nesting error."); + } + char m = this.stack[this.top - 1] == null ? 'a' : 'k'; + if (m != c) { + throw new JSONException("Nesting error."); + } + this.top -= 1; + this.mode = this.top == 0 + ? 'd' + : this.stack[this.top - 1] == null + ? 'a' + : 'k'; + } + + /** + * Push an array or object scope. + * @param jo The scope to open. + * @throws JSONException If nesting is too deep. + */ + private void push(JSONObject jo) throws JSONException { + if (this.top >= maxdepth) { + throw new JSONException("Nesting too deep."); + } + this.stack[this.top] = jo; + this.mode = jo == null ? 'a' : 'k'; + this.top += 1; + } + + /** + * Make a JSON text of an Object value. If the object has an + * value.toJSONString() method, then that method will be used to produce the + * JSON text. The method is required to produce a strictly conforming text. + * If the object does not contain a toJSONString method (which is the most + * common case), then a text will be produced by other means. If the value + * is an array or Collection, then a JSONArray will be made from it and its + * toJSONString method will be called. If the value is a MAP, then a + * JSONObject will be made from it and its toJSONString method will be + * called. Otherwise, the value's toString method will be called, and the + * result will be quoted. + * + *

+ * Warning: This method assumes that the data structure is acyclical. + * + * @param value + * The value to be serialized. + * @return a printable, displayable, transmittable representation of the + * object, beginning with { (left + * brace) and ending with } (right + * brace). + * @throws JSONException + * If the value is or contains an invalid number. + */ + public static String valueToString(Object value) throws JSONException { + if (value == null || value.equals(null)) { + return "null"; + } + if (value instanceof JSONString) { + String object; + try { + object = ((JSONString) value).toJSONString(); + } catch (Exception e) { + throw new JSONException(e); + } + if (object != null) { + return object; + } + throw new JSONException("Bad value from toJSONString: " + object); + } + if (value instanceof Number) { + // not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex + final String numberAsString = JSONObject.numberToString((Number) value); + if(JSONObject.NUMBER_PATTERN.matcher(numberAsString).matches()) { + // Close enough to a JSON number that we will return it unquoted + return numberAsString; + } + // The Number value is not a valid JSON number. + // Instead we will quote it as a string + return JSONObject.quote(numberAsString); + } + if (value instanceof Boolean || value instanceof JSONObject + || value instanceof JSONArray) { + return value.toString(); + } + if (value instanceof Map) { + Map map = (Map) value; + return new JSONObject(map).toString(); + } + if (value instanceof Collection) { + Collection coll = (Collection) value; + return new JSONArray(coll).toString(); + } + if (value.getClass().isArray()) { + return new JSONArray(value).toString(); + } + if(value instanceof Enum){ + return JSONObject.quote(((Enum)value).name()); + } + return JSONObject.quote(value.toString()); + } + + /** + * Append either the value true or the value + * false. + * @param b A boolean. + * @return this + * @throws JSONException + */ + public JSONWriter value(boolean b) throws JSONException { + return this.append(b ? "true" : "false"); + } + + /** + * Append a double value. + * @param d A double. + * @return this + * @throws JSONException If the number is not finite. + */ + public JSONWriter value(double d) throws JSONException { + return this.value(Double.valueOf(d)); + } + + /** + * Append a long value. + * @param l A long. + * @return this + * @throws JSONException + */ + public JSONWriter value(long l) throws JSONException { + return this.append(Long.toString(l)); + } + + + /** + * Append an object value. + * @param object The object to append. It can be null, or a Boolean, Number, + * String, JSONObject, or JSONArray, or an object that implements JSONString. + * @return this + * @throws JSONException If the value is out of sequence. + */ + public JSONWriter value(Object object) throws JSONException { + return this.append(valueToString(object)); + } +} diff --git a/Property.java b/src/main/java/org/json/Property.java similarity index 100% rename from Property.java rename to src/main/java/org/json/Property.java diff --git a/XML.java b/src/main/java/org/json/XML.java similarity index 100% rename from XML.java rename to src/main/java/org/json/XML.java diff --git a/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java similarity index 100% rename from XMLParserConfiguration.java rename to src/main/java/org/json/XMLParserConfiguration.java diff --git a/XMLTokener.java b/src/main/java/org/json/XMLTokener.java similarity index 100% rename from XMLTokener.java rename to src/main/java/org/json/XMLTokener.java From 89d4681e41b9d06294340d6436f2d70eeeb97bf0 Mon Sep 17 00:00:00 2001 From: Benjamin Gehrels Date: Wed, 29 Apr 2020 21:02:52 +0200 Subject: [PATCH 314/315] Added information on how to release to the pom --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 0a2cccee2..aca54a443 100644 --- a/README.md +++ b/README.md @@ -197,4 +197,12 @@ Execution failed for task ':compileJava'. | TestRunner.java | | Util.java | +## How to release +- Adapt the version number in the pom file +- Run +``` +mvn clean deploy +``` + +You will need permissions for the org.json library given by the sonatype maven central team. From 82202dbf65e5c5e3c4855b6e511aa2484c957424 Mon Sep 17 00:00:00 2001 From: Benjamin Gehrels Date: Thu, 30 Apr 2020 12:20:48 +0200 Subject: [PATCH 315/315] Added Jar plugin version to get rid of a warning --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index c2579f283..1dde059d7 100644 --- a/pom.xml +++ b/pom.xml @@ -179,6 +179,7 @@ org.apache.maven.plugins maven-jar-plugin + 3.2.0