Skip to content

Commit

Permalink
cleanup, initial unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkarth committed Aug 6, 2024
1 parent c5b9b6c commit 778f134
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 61 deletions.
2 changes: 0 additions & 2 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
48 changes: 19 additions & 29 deletions src/test/java/org/json/junit/JSONParserConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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<String> lines = Files.lines(Paths.get("src/test/resources/Issue884-validJsonObj.json"))) {
final String resultJsonAsString = lines.collect(Collectors.joining());
public void givenValidJsonObjects_testStrictModeTrue() {
final List<String> 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
Expand Down
78 changes: 48 additions & 30 deletions src/test/java/org/json/junit/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
Expand All @@ -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<String> keys = jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
Expand All @@ -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
Expand All @@ -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<JSONObject> jsonObjects) {
Expand All @@ -116,6 +130,7 @@ public static void checkJSONObjectsMaps(List<JSONObject> 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) {
Expand All @@ -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<? extends Map> mapType) {
if (mapType == null) {
Expand All @@ -141,14 +157,15 @@ public static void checkJSONObjectMaps(JSONObject jsonObject, Class<? extends Ma
assertTrue(mapType == ((JSONObject) val).getMapType());
checkJSONObjectMaps(jsonObjectVal, mapType);
} else if (val instanceof JSONArray) {
JSONArray jsonArrayVal = (JSONArray)val;
JSONArray jsonArrayVal = (JSONArray) val;
checkJSONArrayMaps(jsonArrayVal, mapType);
}
}
}

/**
* Asserts that all JSONObject maps in the JSONArray object match the default map
*
* @param jsonArrays list of JSONArray objects to be tested
*/
public static void checkJSONArraysMaps(List<JSONArray> jsonArrays) {
Expand All @@ -165,8 +182,9 @@ public static void checkJSONArraysMaps(List<JSONArray> 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<? extends Map> mapType) {
if (jsonArray == null) {
Expand All @@ -179,18 +197,18 @@ public static void checkJSONArrayMaps(JSONArray jsonArray, Class<? extends Map>
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) {
Expand Down

0 comments on commit 778f134

Please sign in to comment.