|
7 | 7 | import static org.junit.Assert.assertEquals; |
8 | 8 | import static org.junit.Assert.assertFalse; |
9 | 9 | import static org.junit.Assert.assertNotEquals; |
| 10 | +import static org.junit.Assert.assertThrows; |
10 | 11 | import static org.junit.Assert.assertTrue; |
11 | 12 | import static org.junit.Assert.fail; |
12 | 13 |
|
@@ -1610,6 +1611,76 @@ public void testValidWhitespaceNumericEntityToJSONObject() { |
1610 | 1611 | XML.toJSONObject("<a> </a>"); |
1611 | 1612 | } |
1612 | 1613 |
|
| 1614 | + /** |
| 1615 | + * Tests that numeric character references to C1 control characters are |
| 1616 | + * still rejected. #x7F-#x9F is inside the XML 1.0 {@code Char} range, but |
| 1617 | + * this library has always refused those references while parsing |
| 1618 | + * ({@code …} is NEL, #x85), and fixing the serialization of TAB/LF/CR |
| 1619 | + * must not change what the parser accepts. |
| 1620 | + */ |
| 1621 | + @Test |
| 1622 | + public void testRejectsC1ControlCharacterReference() { |
| 1623 | + assertThrows(JSONException.class, |
| 1624 | + () -> XML.toJSONObject("<name>Joe … Tester</name>")); |
| 1625 | + // the neighbouring boundary codepoints behave the same way |
| 1626 | + assertThrows(JSONException.class, |
| 1627 | + () -> XML.toJSONObject("<name>Joe  Tester</name>")); |
| 1628 | + assertThrows(JSONException.class, |
| 1629 | + () -> XML.toJSONObject("<name>Joe Ÿ Tester</name>")); |
| 1630 | + // ... while the characters the parser does accept still work |
| 1631 | + XML.toJSONObject("<name>Joe   Tester</name>"); |
| 1632 | + } |
| 1633 | + |
| 1634 | + /** |
| 1635 | + * Tests that serializing a string value containing literal TAB, LF and CR |
| 1636 | + * characters writes numeric character references (&#9;, &#10;, |
| 1637 | + * &#13;) instead of the literal characters. Literal occurrences of |
| 1638 | + * these characters are subject to whitespace normalization by conforming |
| 1639 | + * XML parsers, so the original value would be lost when the generated XML |
| 1640 | + * is read back. Regression test for #1092, where #1070 stopped |
| 1641 | + * XML.toString() and JSONML.toString() from escaping these characters. |
| 1642 | + */ |
| 1643 | + @Test |
| 1644 | + public void testEscapesLiteralWhitespaceControlCharsOnSerialization() { |
| 1645 | + // LF in a text node must survive as a numeric character reference |
| 1646 | + JSONObject jo = new JSONObject().put("value", "Lettuce\nTomato"); |
| 1647 | + String xmlOutput = XML.toString(jo); |
| 1648 | + assertTrue("XML should escape literal LF to preserve it, got: " + xmlOutput, |
| 1649 | + xmlOutput.contains("
")); |
| 1650 | + |
| 1651 | + // TAB and CR likewise |
| 1652 | + JSONObject tabJo = new JSONObject().put("value", "a\tb\rc"); |
| 1653 | + String tabXml = XML.toString(tabJo); |
| 1654 | + assertTrue("XML should escape literal TAB, got: " + tabXml, tabXml.contains("	")); |
| 1655 | + assertTrue("XML should escape literal CR, got: " + tabXml, tabXml.contains("
")); |
| 1656 | + } |
| 1657 | + |
| 1658 | + /** |
| 1659 | + * Tests that the generated XML round-trips: the numeric character |
| 1660 | + * references produced for TAB/LF/CR are read back by |
| 1661 | + * {@code XML.toJSONObject} as the original characters. |
| 1662 | + */ |
| 1663 | + @Test |
| 1664 | + public void testWhitespaceControlCharsRoundTripThroughXml() { |
| 1665 | + String original = "Lettuce\nTomato\tMustard"; |
| 1666 | + JSONObject jo = new JSONObject().put("value", original); |
| 1667 | + String xmlOutput = XML.toString(jo); |
| 1668 | + JSONObject reparsed = XML.toJSONObject(xmlOutput); |
| 1669 | + assertEquals("LF/TAB must survive the XML round trip", |
| 1670 | + original, reparsed.getString("value")); |
| 1671 | + } |
| 1672 | + |
| 1673 | + /** |
| 1674 | + * Same escaping requirement for {@code JSONML.toString}. |
| 1675 | + */ |
| 1676 | + @Test |
| 1677 | + public void testJSONMLToStringEscapesLiteralNewlines() { |
| 1678 | + JSONObject jo = new JSONObject("{\"tagName\":\"MyTag\", \"value\":\"Lettuce\\nTomato\"}"); |
| 1679 | + String xmlOutput = JSONML.toString(jo); |
| 1680 | + assertTrue("JSONML should escape literal LF to preserve it, got: " + xmlOutput, |
| 1681 | + xmlOutput.contains("
")); |
| 1682 | + } |
| 1683 | + |
1613 | 1684 | } |
1614 | 1685 |
|
1615 | 1686 |
|
|
0 commit comments