diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 2fa5dae..25b0a0e 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1426,6 +1426,81 @@ public class XMLTest { assertEquals(jsonObject3.getJSONObject("color").getString("value"), "008E97"); } + /** + * Tests that empty numeric character reference &#; throws JSONException. + * Previously threw StringIndexOutOfBoundsException. + * Related to issue #1035 + */ + @Test(expected = JSONException.class) + public void testEmptyNumericEntityThrowsJSONException() { + String xmlStr = "&#;"; + XML.toJSONObject(xmlStr); + } + + /** + * Tests that malformed decimal entity &#txx; throws JSONException. + * Previously threw NumberFormatException. + * Related to issue #1036 + */ + @Test(expected = JSONException.class) + public void testInvalidDecimalEntityThrowsJSONException() { + String xmlStr = "&#txx;"; + XML.toJSONObject(xmlStr); + } + + /** + * Tests that empty hex entity &#x; throws JSONException. + * Validates proper input validation for hex entities. + */ + @Test(expected = JSONException.class) + public void testEmptyHexEntityThrowsJSONException() { + String xmlStr = "&#x;"; + XML.toJSONObject(xmlStr); + } + + /** + * Tests that invalid hex entity &#xGGG; throws JSONException. + * Validates hex digit validation. + */ + @Test(expected = JSONException.class) + public void testInvalidHexEntityThrowsJSONException() { + String xmlStr = "&#xGGG;"; + XML.toJSONObject(xmlStr); + } + + /** + * Tests that valid decimal numeric entity A works correctly. + * Should decode to character 'A'. + */ + @Test + public void testValidDecimalEntity() { + String xmlStr = "A"; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertEquals("A", jsonObject.getString("a")); + } + + /** + * Tests that valid hex numeric entity A works correctly. + * Should decode to character 'A'. + */ + @Test + public void testValidHexEntity() { + String xmlStr = "A"; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertEquals("A", jsonObject.getString("a")); + } + + /** + * Tests that valid uppercase hex entity A works correctly. + * Should decode to character 'A'. + */ + @Test + public void testValidUppercaseHexEntity() { + String xmlStr = "A"; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertEquals("A", jsonObject.getString("a")); + } + }