From 778f134047a22839f596f140303e3196f42caf9f Mon Sep 17 00:00:00 2001 From: rikkarth Date: Tue, 6 Aug 2024 15:39:11 +0100 Subject: [PATCH] cleanup, initial unit-tests --- src/main/java/org/json/JSONObject.java | 2 - .../junit/JSONParserConfigurationTest.java | 48 +++++------- src/test/java/org/json/junit/Util.java | 78 ++++++++++++------- 3 files changed, 67 insertions(+), 61 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index b3abef821..a5dd7e0ab 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -209,9 +209,7 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration case '}': if (jsonParserConfiguration.isStrictMode()) { c = x.nextClean(); - System.out.println(c); if (c != 0) { - System.out.println("yes"); throw x.syntaxError(String.format("Invalid char '%s' after '}'", c)); } x.back(); diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java index f3f4b0e23..dfd9bf384 100644 --- a/src/test/java/org/json/junit/JSONParserConfigurationTest.java +++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java @@ -47,41 +47,31 @@ public void givenInvalidInputArrays_testStrictModeTrue_shouldThrowJsonException( } @Test - public void givenInvalidJsonObjects_testStrictModeTrue_shouldThrowJsonException() throws IOException { - /*assertThrows(JSONException.class, - () -> new JSONObject("{}abc", new JSONParserConfiguration().withStrictMode(true)));*/ - - try (final Stream lines = Files.lines(Paths.get("src/test/resources/Issue884-validJsonObj.json"))) { - final String resultJsonAsString = lines.collect(Collectors.joining()); - - final JSONObject jsonObject = new JSONObject(resultJsonAsString, new JSONParserConfiguration().withStrictMode(true)); - - System.out.println(jsonObject.toString(2)); - } catch (Exception e){ - e.printStackTrace(); - } - - //JSONObject jsonObject = new JSONObject("{\"test\": {\"key\": \"val\", \"key2\"}}", new JSONParserConfiguration().withStrictMode(true)); - //JSONObject jsonObject2 = new JSONObject("{}}", new JSONParserConfiguration().withStrictMode(true)); + public void givenInvalidJsonObjects_testStrictModeTrue_shouldThrowJsonException() { + final List jsonObjects = Arrays.asList( + "{}}", + "{}abc" + ); + + jsonObjects.forEach(jsonObject -> + assertThrows(JSONException.class, + () -> new JSONObject(jsonObject, new JSONParserConfiguration().withStrictMode(true)))); } @Test - public void givenValidJsonObject_testStrictModeTrue_shouldThrowJsonException() throws IOException { - /*assertThrows(JSONException.class, - () -> new JSONObject("{}abc", new JSONParserConfiguration().withStrictMode(true)));*/ - - try (final Stream lines = Files.lines(Paths.get("src/test/resources/Issue884-validJsonObj.json"))) { - final String resultJsonAsString = lines.collect(Collectors.joining()); + public void givenValidJsonObjects_testStrictModeTrue() { + final List jsonPaths = Arrays.asList( + "src/test/resources/Issue884-validJsonObj.json" + ); - final JSONObject jsonObject = new JSONObject(resultJsonAsString, new JSONParserConfiguration().withStrictMode(true)); + jsonPaths.forEach(path -> { + final String resultJsonAsString = Util.getJsonStringFromFilePath(Paths.get(path)); - System.out.println(jsonObject.toString(2)); - } catch (Exception e){ - e.printStackTrace(); - } + final JSONObject resultJsonObject = new JSONObject(resultJsonAsString, + new JSONParserConfiguration().withStrictMode(true)); - //JSONObject jsonObject = new JSONObject("{\"test\": {\"key\": \"val\", \"key2\"}}", new JSONParserConfiguration().withStrictMode(true)); - //JSONObject jsonObject2 = new JSONObject("{}}", new JSONParserConfiguration().withStrictMode(true)); + assertEquals(resultJsonAsString.replaceAll("\\s", "").length(), resultJsonObject.toString().length()); + }); } @Test diff --git a/src/test/java/org/json/junit/Util.java b/src/test/java/org/json/junit/Util.java index b676045b8..7226cad7d 100644 --- a/src/test/java/org/json/junit/Util.java +++ b/src/test/java/org/json/junit/Util.java @@ -6,27 +6,40 @@ import static org.junit.Assert.*; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; 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. + * 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 { + public static String getJsonStringFromFilePath(final Path path) { + try (final Stream lines = Files.lines(path)) { + return lines.collect(Collectors.joining()); + } catch (final IOException ioe) { + throw new IllegalStateException(String.format("Unable to retrieve path: %s", path)); + } + } + /** - * Compares two JSONArrays for equality. - * The arrays need not be in the same order. - * @param jsonArray created by the code to be tested + * 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 */ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray, - JSONArray expectedJsonArray) { + JSONArray expectedJsonArray) { assertTrue("jsonArray lengths should be equal", - jsonArray.length() == expectedJsonArray.length()); + jsonArray.length() == expectedJsonArray.length()); for (int i = 0; i < jsonArray.length(); ++i) { Object value = jsonArray.get(i); Object expectedValue = expectedJsonArray.get(i); @@ -35,15 +48,15 @@ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray, } /** - * Compares two JSONObjects for equality. The objects need not be - * in the same order - * @param jsonObject created by the code to be tested + * 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 */ public static void compareActualVsExpectedJsonObjects( - JSONObject jsonObject, JSONObject expectedJsonObject) { + JSONObject jsonObject, JSONObject expectedJsonObject) { assertTrue("jsonObjects should have the same length", - jsonObject.length() == expectedJsonObject.length()); + jsonObject.length() == expectedJsonObject.length()); Iterator keys = jsonObject.keys(); while (keys.hasNext()) { String key = keys.next(); @@ -54,25 +67,25 @@ public static void compareActualVsExpectedJsonObjects( } /** - * Compare two objects for equality. Might be JSONArray, JSONObject, - * or something else. - * @param value created by the code to be tested + * 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 */ private static void compareActualVsExpectedObjects(Object value, - Object expectedValue) { + Object expectedValue) { if (value instanceof JSONObject && expectedValue instanceof JSONObject) { // Compare JSONObjects - JSONObject jsonObject = (JSONObject)value; - JSONObject expectedJsonObject = (JSONObject)expectedValue; + JSONObject jsonObject = (JSONObject) value; + JSONObject expectedJsonObject = (JSONObject) expectedValue; compareActualVsExpectedJsonObjects( - jsonObject, expectedJsonObject); + jsonObject, expectedJsonObject); } else if (value instanceof JSONArray && expectedValue instanceof JSONArray) { // Compare JSONArrays - JSONArray jsonArray = (JSONArray)value; - JSONArray expectedJsonArray = (JSONArray)expectedValue; + JSONArray jsonArray = (JSONArray) value; + JSONArray expectedJsonArray = (JSONArray) expectedValue; compareActualVsExpectedJsonArrays( - jsonArray, expectedJsonArray); + jsonArray, expectedJsonArray); } else { /** * Compare all other types using toString(). First, the types must @@ -99,6 +112,7 @@ private static void compareActualVsExpectedObjects(Object value, /** * Asserts that all JSONObject maps are the same as the default ctor + * * @param jsonObjects list of objects to be tested */ public static void checkJSONObjectsMaps(List jsonObjects) { @@ -116,6 +130,7 @@ public static void checkJSONObjectsMaps(List jsonObjects) { /** * Asserts that all JSONObject maps are the same as the default ctor + * * @param jsonObject the object to be tested */ public static void checkJSONObjectMaps(JSONObject jsonObject) { @@ -126,8 +141,9 @@ public static void checkJSONObjectMaps(JSONObject jsonObject) { /** * Asserts that all JSONObject maps are the same as mapType + * * @param jsonObject object to be tested - * @param mapType mapType to test against + * @param mapType mapType to test against */ public static void checkJSONObjectMaps(JSONObject jsonObject, Class mapType) { if (mapType == null) { @@ -141,7 +157,7 @@ public static void checkJSONObjectMaps(JSONObject jsonObject, Class jsonArrays) { @@ -165,8 +182,9 @@ public static void checkJSONArraysMaps(List jsonArrays) { /** * Asserts that all JSONObject maps in the JSONArray object match mapType + * * @param jsonArray object to be tested - * @param mapType map type to be tested against + * @param mapType map type to be tested against */ public static void checkJSONArrayMaps(JSONArray jsonArray, Class mapType) { if (jsonArray == null) { @@ -179,18 +197,18 @@ public static void checkJSONArrayMaps(JSONArray jsonArray, Class while (it.hasNext()) { Object val = it.next(); if (val instanceof JSONObject) { - JSONObject jsonObjectVal = (JSONObject)val; + JSONObject jsonObjectVal = (JSONObject) val; checkJSONObjectMaps(jsonObjectVal, mapType); } else if (val instanceof JSONArray) { - JSONArray jsonArrayVal = (JSONArray)val; + JSONArray jsonArrayVal = (JSONArray) val; checkJSONArrayMaps(jsonArrayVal, mapType); } } } /** - * Asserts that all JSONObject maps nested in the JSONArray match - * the default mapType + * Asserts that all JSONObject maps nested in the JSONArray match the default mapType + * * @param jsonArray the object to be tested */ public static void checkJSONArrayMaps(JSONArray jsonArray) {