mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 16:00:51 -07:00
XMLTest, in progress
This commit is contained in:
parent
8b9c3cbf47
commit
4fbe651e57
4 changed files with 160 additions and 83 deletions
93
XMLTest.java
93
XMLTest.java
|
@ -0,0 +1,93 @@
|
|||
package org.json.junit;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for JSON-Java XML.java
|
||||
*/
|
||||
public class XMLTest {
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void shouldHandleNullXML() {
|
||||
|
||||
String xmlStr = null;
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleEmptyXML() {
|
||||
|
||||
String xmlStr = "";
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
|
||||
}
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void shouldHandleNullJSONXML() {
|
||||
JSONObject jsonObject= null;
|
||||
String xmlStr = XML.toString(jsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleEmptyJSONXML() {
|
||||
JSONObject jsonObject= new JSONObject();
|
||||
String xmlStr = XML.toString(jsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleSimpleXML() {
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
" <address>\n"+
|
||||
" <name>Joe Tester</name>\n"+
|
||||
" <street>Baker street 5</street>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
|
||||
String expectedStr =
|
||||
"{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+
|
||||
"\"name\":\"Joe Tester\"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
||||
"XMLSchema-instance\"}}";
|
||||
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleToString() {
|
||||
String xmlStr =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addr&esses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
|
||||
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
|
||||
" <address>\n"+
|
||||
" <name>[CDATA[Joe Tester]]</name>\n"+
|
||||
" <street>Baker street 5</street>\n"+
|
||||
" </address>\n"+
|
||||
"</addr&esses>";
|
||||
|
||||
String expectedStr =
|
||||
"{\"addr&esses\":{\"address\":{\"street\":\"Baker street 5\","+
|
||||
"\"name\":\"[CDATA[Joe Tester]]\"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
||||
"XMLSchema-instance\"}}";
|
||||
|
||||
JSONObject jsonObject = XML.toJSONObject(xmlStr);
|
||||
String xmlToStr = XML.toString(jsonObject);
|
||||
JSONObject finalJsonObject = XML.toJSONObject(xmlToStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue