Skip to content

Commit e699abb

Browse files
authored
Merge pull request #61 from johnjaylward/XmlEscape
Test cases Xml escapes
2 parents 0d053a0 + f6a00e9 commit e699abb

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.json.junit;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertNotEquals;
56
import static org.junit.Assert.assertTrue;
67
import static org.junit.Assert.fail;
@@ -265,6 +266,63 @@ public void shouldHandleSimpleXML() {
265266
compareFileToJSONObject(xmlStr, expectedStr);
266267
}
267268

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 &#8364;33</euro>"+
279+
"<euroX>A &#x20ac;22&#x20AC;</euroX>"+
280+
"<unknown>some text &copy;</unknown>"+
281+
"<known>&#x0022; &quot; &amp; &apos; &lt; &gt;</known>"+
282+
"<high>&#x1D122; &#x10165;</high>" +
283+
"</root>";
284+
String expectedStr =
285+
"{\"root\":{" +
286+
"\"rawQuote\":\"\\\"\"," +
287+
"\"euro\":\"A €33\"," +
288+
"\"euroX\":\"A €22€\"," +
289+
"\"unknown\":\"some text &copy;\"," +
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("&#x85;"));
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("&quot;"));
320+
assertTrue("Escaping ' failed. Not found in XML output.", xml.contains("&apos;"));
321+
assertTrue("Escaping & failed. Not found in XML output.", xml.contains("&amp;"));
322+
assertTrue("Escaping < failed. Not found in XML output.", xml.contains("&lt;"));
323+
assertTrue("Escaping > failed. Not found in XML output.", xml.contains("&gt;"));
324+
}
325+
268326
/**
269327
* Valid XML with comments to JSONObject
270328
*/
@@ -673,8 +731,8 @@ public void contentOperations() {
673731
* @param expectedStr the expected JSON string
674732
*/
675733
private void compareStringToJSONObject(String xmlStr, String expectedStr) {
676-
JSONObject expectedJsonObject = new JSONObject(expectedStr);
677734
JSONObject jsonObject = XML.toJSONObject(xmlStr);
735+
JSONObject expectedJsonObject = new JSONObject(expectedStr);
678736
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
679737
}
680738

0 commit comments

Comments
 (0)