fix(#887): regressions, unit tests

- JSONArray now evaluates EOF accordingly for empty Array inputs.
- JSONTokener fixed indentation
- externalized two JSONMLTest cases
This commit is contained in:
rikkarth
2024-04-28 23:30:05 +01:00
parent cf00ef3e8a
commit 1ae43bdb90
8 changed files with 218 additions and 81 deletions

View File

@@ -6,6 +6,11 @@ Public Domain.
import static org.junit.Assert.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.json.*;
import org.junit.Test;
@@ -648,16 +653,10 @@ public class JSONMLTest {
// create a JSON array from the original string and make sure it
// looks as expected
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
// TODO: The next 2 test cases fail due to a strictMode regression. They should be isolated in separate
// test cases and fixed.
// 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
@@ -666,6 +665,31 @@ public class JSONMLTest {
Util.compareActualVsExpectedJsonObjects(jsonObjectFromObject, jsonObjectFromArray);
}
@Test
public void givenXmlStr_testToJSONArray_shouldEqualExpectedArray() throws IOException {
try (Stream<String> jsonLines = Files.lines(
Paths.get("src/test/resources/JSONArrayExpectedTestCaseForToJsonArrayTest.json"));
Stream<String> xmlLines = Files.lines(Paths.get("src/test/resources/XmlTestCaseTestToJsonArray.xml"))) {
String xmlStr = xmlLines.collect(Collectors.joining());
String expectedJSONArrayStr = jsonLines.collect(Collectors.joining());
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
JSONArray expectedJsonArray = new JSONArray(expectedJSONArrayStr);
assertEquals(expectedJsonArray.toString(), jsonArray.toString());
//TODO Util.compareActualVsExpectedJsonArrays can be replaced with above assertEquals(expectedJsonArray.toString(), jsonArray.toString())
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
String jsonArrayXmlToStr = JSONML.toString(jsonArray);
JSONArray finalJsonArray = JSONML.toJSONArray(jsonArrayXmlToStr);
//TODO Util.compareActualVsExpectedJsonArrays can be replaced with assertEquals(expectedJsonArray.toString(), finalJsonArray.toString())
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
}
}
/**
* Convert an XML document which contains embedded comments into
* a JSONArray. Use JSONML.toString() to turn it into a string, then