|
1 | 1 | package org.json.junit;
|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals;
|
| 4 | +import static org.junit.Assert.assertFalse; |
4 | 5 | import static org.junit.Assert.assertNotEquals;
|
5 | 6 | import static org.junit.Assert.assertTrue;
|
6 | 7 | import static org.junit.Assert.fail;
|
@@ -265,6 +266,63 @@ public void shouldHandleSimpleXML() {
|
265 | 266 | compareFileToJSONObject(xmlStr, expectedStr);
|
266 | 267 | }
|
267 | 268 |
|
| 269 | + /** |
| 270 | + * Tests to verify that supported escapes in XML are converted to actual values. |
| 271 | + */ |
| 272 | + @Test |
| 273 | + public void testXmlEscapeToJson(){ |
| 274 | + String xmlStr = |
| 275 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ |
| 276 | + "<root>"+ |
| 277 | + "<rawQuote>\"</rawQuote>"+ |
| 278 | + "<euro>A €33</euro>"+ |
| 279 | + "<euroX>A €22€</euroX>"+ |
| 280 | + "<unknown>some text ©</unknown>"+ |
| 281 | + "<known>" " & ' < ></known>"+ |
| 282 | + "<high>𝄢 𐅥</high>" + |
| 283 | + "</root>"; |
| 284 | + String expectedStr = |
| 285 | + "{\"root\":{" + |
| 286 | + "\"rawQuote\":\"\\\"\"," + |
| 287 | + "\"euro\":\"A €33\"," + |
| 288 | + "\"euroX\":\"A €22€\"," + |
| 289 | + "\"unknown\":\"some text ©\"," + |
| 290 | + "\"known\":\"\\\" \\\" & ' < >\"," + |
| 291 | + "\"high\":\"𝄢 𐅥\""+ |
| 292 | + "}}"; |
| 293 | + |
| 294 | + compareStringToJSONObject(xmlStr, expectedStr); |
| 295 | + compareReaderToJSONObject(xmlStr, expectedStr); |
| 296 | + compareFileToJSONObject(xmlStr, expectedStr); |
| 297 | + } |
| 298 | + |
| 299 | + /** |
| 300 | + * Tests that control characters are escaped. |
| 301 | + */ |
| 302 | + @Test |
| 303 | + public void testJsonToXmlEscape(){ |
| 304 | + final String jsonSrc = "{\"amount\":\"10,00 €\"," |
| 305 | + + "\"description\":\"Ação Válida\u0085\"," |
| 306 | + + "\"xmlEntities\":\"\\\" ' & < >\"" |
| 307 | + + "}"; |
| 308 | + JSONObject json = new JSONObject(jsonSrc); |
| 309 | + String xml = XML.toString(json); |
| 310 | + //test control character not existing |
| 311 | + assertFalse("Escaping \u0085 failed. Found in XML output.", xml.contains("\u0085")); |
| 312 | + assertTrue("Escaping \u0085 failed. Entity not found in XML output.", xml.contains("…")); |
| 313 | + // test normal unicode existing |
| 314 | + assertTrue("Escaping € failed. Not found in XML output.", xml.contains("€")); |
| 315 | + assertTrue("Escaping ç failed. Not found in XML output.", xml.contains("ç")); |
| 316 | + assertTrue("Escaping ã failed. Not found in XML output.", xml.contains("ã")); |
| 317 | + assertTrue("Escaping á failed. Not found in XML output.", xml.contains("á")); |
| 318 | + // test XML Entities converted |
| 319 | + assertTrue("Escaping \" failed. Not found in XML output.", xml.contains(""")); |
| 320 | + assertTrue("Escaping ' failed. Not found in XML output.", xml.contains("'")); |
| 321 | + assertTrue("Escaping & failed. Not found in XML output.", xml.contains("&")); |
| 322 | + assertTrue("Escaping < failed. Not found in XML output.", xml.contains("<")); |
| 323 | + assertTrue("Escaping > failed. Not found in XML output.", xml.contains(">")); |
| 324 | + } |
| 325 | + |
268 | 326 | /**
|
269 | 327 | * Valid XML with comments to JSONObject
|
270 | 328 | */
|
@@ -673,8 +731,8 @@ public void contentOperations() {
|
673 | 731 | * @param expectedStr the expected JSON string
|
674 | 732 | */
|
675 | 733 | private void compareStringToJSONObject(String xmlStr, String expectedStr) {
|
676 |
| - JSONObject expectedJsonObject = new JSONObject(expectedStr); |
677 | 734 | JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
| 735 | + JSONObject expectedJsonObject = new JSONObject(expectedStr); |
678 | 736 | Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
679 | 737 | }
|
680 | 738 |
|
|
0 commit comments