mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Merge pull request #61 from johnjaylward/XmlEscape
Test cases Xml escapes
This commit is contained in:
commit
e699abb1c6
1 changed files with 59 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
|||
package org.json.junit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
@ -265,6 +266,63 @@ public class XMLTest {
|
|||
compareFileToJSONObject(xmlStr, expectedStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to verify that supported escapes in XML are converted to actual values.
|
||||
*/
|
||||
@Test
|
||||
public void testXmlEscapeToJson(){
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<root>"+
|
||||
"<rawQuote>\"</rawQuote>"+
|
||||
"<euro>A €33</euro>"+
|
||||
"<euroX>A €22€</euroX>"+
|
||||
"<unknown>some text ©</unknown>"+
|
||||
"<known>" " & ' < ></known>"+
|
||||
"<high>𝄢 𐅥</high>" +
|
||||
"</root>";
|
||||
String expectedStr =
|
||||
"{\"root\":{" +
|
||||
"\"rawQuote\":\"\\\"\"," +
|
||||
"\"euro\":\"A €33\"," +
|
||||
"\"euroX\":\"A €22€\"," +
|
||||
"\"unknown\":\"some text ©\"," +
|
||||
"\"known\":\"\\\" \\\" & ' < >\"," +
|
||||
"\"high\":\"𝄢 𐅥\""+
|
||||
"}}";
|
||||
|
||||
compareStringToJSONObject(xmlStr, expectedStr);
|
||||
compareReaderToJSONObject(xmlStr, expectedStr);
|
||||
compareFileToJSONObject(xmlStr, expectedStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that control characters are escaped.
|
||||
*/
|
||||
@Test
|
||||
public void testJsonToXmlEscape(){
|
||||
final String jsonSrc = "{\"amount\":\"10,00 €\","
|
||||
+ "\"description\":\"Ação Válida\u0085\","
|
||||
+ "\"xmlEntities\":\"\\\" ' & < >\""
|
||||
+ "}";
|
||||
JSONObject json = new JSONObject(jsonSrc);
|
||||
String xml = XML.toString(json);
|
||||
//test control character not existing
|
||||
assertFalse("Escaping \u0085 failed. Found in XML output.", xml.contains("\u0085"));
|
||||
assertTrue("Escaping \u0085 failed. Entity not found in XML output.", xml.contains("…"));
|
||||
// test normal unicode existing
|
||||
assertTrue("Escaping € failed. Not found in XML output.", xml.contains("€"));
|
||||
assertTrue("Escaping ç failed. Not found in XML output.", xml.contains("ç"));
|
||||
assertTrue("Escaping ã failed. Not found in XML output.", xml.contains("ã"));
|
||||
assertTrue("Escaping á failed. Not found in XML output.", xml.contains("á"));
|
||||
// test XML Entities converted
|
||||
assertTrue("Escaping \" failed. Not found in XML output.", xml.contains("""));
|
||||
assertTrue("Escaping ' failed. Not found in XML output.", xml.contains("'"));
|
||||
assertTrue("Escaping & failed. Not found in XML output.", xml.contains("&"));
|
||||
assertTrue("Escaping < failed. Not found in XML output.", xml.contains("<"));
|
||||
assertTrue("Escaping > failed. Not found in XML output.", xml.contains(">"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Valid XML with comments to JSONObject
|
||||
*/
|
||||
|
@ -673,8 +731,8 @@ public class XMLTest {
|
|||
* @param expectedStr the expected JSON string
|
||||
*/
|
||||
private void compareStringToJSONObject(String xmlStr, String expectedStr) {
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue