Skip to content

Commit 0737e04

Browse files
committed
Add unit tests for JSONML ClassCastException fix
Added comprehensive test coverage for safe type casting: Exception cases (should throw JSONException, not ClassCastException): - Malformed XML causing type mismatch in toJSONArray() - Type mismatch in toJSONObject() Valid cases (should continue to work): - Valid XML to JSONArray conversion - Valid XML to JSONObject conversion These tests verify the fix for issue #1034 where ClassCastException was thrown when parse() returned unexpected types.
1 parent 9d14246 commit 0737e04

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/test/java/org/json/junit/JSONMLTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,4 +986,70 @@ public void testToJSONObjectMaxNestingDepthWithValidFittingXML() {
986986
}
987987
}
988988

989+
/**
990+
* Tests that malformed XML causing type mismatch throws JSONException.
991+
* Previously threw ClassCastException when parse() returned String instead of JSONArray.
992+
* Related to issue #1034
993+
*/
994+
@Test(expected = JSONException.class)
995+
public void testMalformedXMLThrowsJSONExceptionNotClassCast() {
996+
// This malformed XML causes parse() to return wrong type
997+
byte[] data = {0x3c, 0x0a, 0x2f, (byte)0xff, (byte)0xff, (byte)0xff,
998+
(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
999+
(byte)0xff, 0x3e, 0x42};
1000+
String xmlStr = new String(data);
1001+
JSONML.toJSONArray(xmlStr);
1002+
}
1003+
1004+
/**
1005+
* Tests that type mismatch in toJSONObject throws JSONException.
1006+
* Validates safe type casting in toJSONObject methods.
1007+
*/
1008+
@Test
1009+
public void testToJSONObjectTypeMismatch() {
1010+
// Create XML that would cause parse() to return wrong type
1011+
String xmlStr = "<\n/\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff>B";
1012+
try {
1013+
JSONML.toJSONObject(xmlStr);
1014+
fail("Expected JSONException for type mismatch");
1015+
} catch (ClassCastException e) {
1016+
fail("Should throw JSONException, not ClassCastException");
1017+
} catch (JSONException e) {
1018+
// Expected - verify it's about type mismatch
1019+
assertTrue("Exception message should mention type error",
1020+
e.getMessage().contains("Expected") || e.getMessage().contains("got"));
1021+
}
1022+
}
1023+
1024+
/**
1025+
* Tests that valid XML still works correctly after the fix.
1026+
* Ensures the type checking doesn't break normal operation.
1027+
*/
1028+
@Test
1029+
public void testValidXMLStillWorks() {
1030+
String xmlStr = "<root><item>value</item></root>";
1031+
try {
1032+
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
1033+
assertNotNull("JSONArray should not be null", jsonArray);
1034+
assertEquals("root", jsonArray.getString(0));
1035+
} catch (Exception e) {
1036+
fail("Valid XML should not throw exception: " + e.getMessage());
1037+
}
1038+
}
1039+
1040+
/**
1041+
* Tests that valid XML to JSONObject still works correctly.
1042+
*/
1043+
@Test
1044+
public void testValidXMLToJSONObjectStillWorks() {
1045+
String xmlStr = "<root attr=\"value\"><item>content</item></root>";
1046+
try {
1047+
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
1048+
assertNotNull("JSONObject should not be null", jsonObject);
1049+
assertEquals("root", jsonObject.getString("tagName"));
1050+
} catch (Exception e) {
1051+
fail("Valid XML should not throw exception: " + e.getMessage());
1052+
}
1053+
}
1054+
9891055
}

0 commit comments

Comments
 (0)