Skip to content

Commit

Permalink
Merge pull request stleary#22 from stleary/xml-reader-remote
Browse files Browse the repository at this point in the history
sample tests for XML.toJSONObject(Reader)
  • Loading branch information
stleary committed Aug 24, 2015
2 parents 9dd0ca7 + 1f6e07c commit 3f78a85
Showing 1 changed file with 179 additions and 21 deletions.
200 changes: 179 additions & 21 deletions XMLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,39 @@

import static org.junit.Assert.*;

import java.io.*;

import org.json.*;
import org.junit.Test;
import org.junit.*;
import org.junit.rules.*;


/**
* Tests for JSON-Java XML.java
* Note: noSpace() will be tested by JSONMLTest
*/
public class XMLTest {
/**
* JUnit supports temporary files and folders that are cleaned up after the test.
* https://garygregory.wordpress.com/2010/01/20/junit-tip-use-rules-to-manage-temporary-files-and-folders/
*/
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();

/**
* JSONObject from a null XML string.
* Expects a NullPointerException
*/
@Test(expected=NullPointerException.class)
public void shouldHandleNullXML() {

String xmlStr = null;
JSONObject jsonObject = XML.toJSONObject(xmlStr);
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
}

/**
* Empty JSONObject from an empty XML string.
*/
@Test
public void shouldHandleEmptyXML() {

Expand All @@ -28,14 +43,21 @@ public void shouldHandleEmptyXML() {
assertTrue("jsonObject should be empty", jsonObject.length() == 0);
}

/**
* Empty JSONObject from a non-XML string.
*/
@Test
public void shouldHandleNonXML() {
String xmlStr = "{ \"this is\": \"not xml\"}";
JSONObject jsonObject = XML.toJSONObject(xmlStr);
assertTrue("xml string should be empty", jsonObject.length() == 0);
}

@Test(expected=JSONException.class)
/**
* Invalid XML string (tag contains a frontslash).
* Expects a JSONException
*/
@Test
public void shouldHandleInvalidSlashInTag() {
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
Expand All @@ -46,10 +68,21 @@ public void shouldHandleInvalidSlashInTag() {
" <street>abc street</street>\n"+
" </address>\n"+
"</addresses>";
XML.toJSONObject(xmlStr);
try {
XML.toJSONObject(xmlStr);
assertTrue("Expecting a JSONException", false);
} catch (JSONException e) {
assertTrue("Expecting an exception message",
"Misshaped tag at 176 [character 14 line 5]".
equals(e.getMessage()));
}
}

@Test(expected=JSONException.class)
/**
* Invalid XML string ('!' char in tag)
* Expects a JSONException
*/
@Test
public void shouldHandleInvalidBangInTag() {
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
Expand All @@ -60,10 +93,21 @@ public void shouldHandleInvalidBangInTag() {
" <!>\n"+
" </address>\n"+
"</addresses>";
XML.toJSONObject(xmlStr);
try {
XML.toJSONObject(xmlStr);
assertTrue("Expecting a JSONException", false);
} catch (JSONException e) {
assertTrue("Expecting an exception message",
"Misshaped meta tag at 215 [character 13 line 8]".
equals(e.getMessage()));
}
}

@Test(expected=JSONException.class)
/**
* Invalid XML string ('!' char and no closing tag brace)
* Expects a JSONException
*/
@Test
public void shouldHandleInvalidBangNoCloseInTag() {
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
Expand All @@ -74,10 +118,21 @@ public void shouldHandleInvalidBangNoCloseInTag() {
" <!\n"+
" </address>\n"+
"</addresses>";
XML.toJSONObject(xmlStr);
try {
XML.toJSONObject(xmlStr);
assertTrue("Expecting a JSONException", false);
} catch (JSONException e) {
assertTrue("Expecting an exception message",
"Misshaped meta tag at 214 [character 13 line 8]".
equals(e.getMessage()));
}
}

@Test(expected=JSONException.class)
/**
* Invalid XML string (no end brace for tag)
* Expects JSONException
*/
@Test
public void shouldHandleNoCloseStartTag() {
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
Expand All @@ -88,10 +143,21 @@ public void shouldHandleNoCloseStartTag() {
" <abc\n"+
" </address>\n"+
"</addresses>";
XML.toJSONObject(xmlStr);
try {
XML.toJSONObject(xmlStr);
assertTrue("Expecting a JSONException", false);
} catch (JSONException e) {
assertTrue("Expecting an exception message",
"Misplaced '<' at 193 [character 4 line 7]".
equals(e.getMessage()));
}
}

@Test(expected=JSONException.class)
/**
* Invalid XML string (partial CDATA chars in tag name)
* Expects JSONException
*/
@Test
public void shouldHandleInvalidCDATABangInTag() {
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
Expand All @@ -102,22 +168,39 @@ public void shouldHandleInvalidCDATABangInTag() {
" <![[]>\n"+
" </address>\n"+
"</addresses>";
XML.toJSONObject(xmlStr);
try {
XML.toJSONObject(xmlStr);
assertTrue("Expecting a JSONException", false);
} catch (JSONException e) {
assertTrue("Expecting an exception message",
"Expected 'CDATA[' at 204 [character 11 line 6]".
equals(e.getMessage()));
}
}

/**
* Null JSONObject in XML.toString()
* Expects NullPointerException
*/
@Test(expected=NullPointerException.class)
public void shouldHandleNullJSONXML() {
JSONObject jsonObject= null;
XML.toString(jsonObject);
}

/**
* Empty JSONObject in XML.toString()
*/
@Test
public void shouldHandleEmptyJSONXML() {
JSONObject jsonObject= new JSONObject();
String xmlStr = XML.toString(jsonObject);
assertTrue("xml string should be empty", xmlStr.length() == 0);
}

/**
* No SML start tag. The ending tag ends up being treated as content.
*/
@Test
public void shouldHandleNoStartTag() {
String xmlStr =
Expand All @@ -138,6 +221,9 @@ public void shouldHandleNoStartTag() {
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}

/**
* Valid XML to JSONObject
*/
@Test
public void shouldHandleSimpleXML() {
String xmlStr =
Expand Down Expand Up @@ -168,12 +254,15 @@ public void shouldHandleSimpleXML() {
"},\"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);

compareStringToJSONObject(xmlStr, expectedStr);
compareReaderToJSONObject(xmlStr, expectedStr);
compareFileToJSONObject(xmlStr, expectedStr);
}

/**
* Valid XML with comments to JSONObject
*/
@Test
public void shouldHandleCommentsInXML() {

Expand All @@ -197,6 +286,9 @@ public void shouldHandleCommentsInXML() {
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}

/**
* Valid XML to XML.toString()
*/
@Test
public void shouldHandleToString() {
String xmlStr =
Expand Down Expand Up @@ -226,6 +318,10 @@ public void shouldHandleToString() {
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
}

/**
* Converting a JSON doc containing '>' content to JSONObject, then
* XML.toString() should result in valid XML.
*/
@Test
public void shouldHandleContentNoArraytoString() {
String expectedStr =
Expand All @@ -242,6 +338,11 @@ public void shouldHandleContentNoArraytoString() {
finalStr+"]", expectedFinalStr.equals(finalStr));
}

/**
* Converting a JSON doc containing a 'content' array to JSONObject, then
* XML.toString() should result in valid XML.
* TODO: This is probably an error in how the 'content' keyword is used.
*/
@Test
public void shouldHandleContentArraytoString() {
String expectedStr =
Expand All @@ -259,6 +360,10 @@ public void shouldHandleContentArraytoString() {
finalStr+"]", expectedFinalStr.equals(finalStr));
}

/**
* Converting a JSON doc containing a named array to JSONObject, then
* XML.toString() should result in valid XML.
*/
@Test
public void shouldHandleArraytoString() {
String expectedStr =
Expand All @@ -276,6 +381,10 @@ public void shouldHandleArraytoString() {
finalStr+"]", expectedFinalStr.equals(finalStr));
}

/**
* Converting a JSON doc containing a named array of nested arrays to
* JSONObject, then XML.toString() should result in valid XML.
*/
@Test
public void shouldHandleNestedArraytoString() {
String xmlStr =
Expand All @@ -297,10 +406,10 @@ public void shouldHandleNestedArraytoString() {


/**
* Possible bug:
* Illegal node-names must be converted to legal XML-node-names.
* The given example shows 2 nodes which are valid for JSON, but not for XML.
* Therefore illegal arguments should be converted to e.g. an underscore (_).
*
*/
@Test
public void shouldHandleIllegalJSONNodeNames()
Expand All @@ -322,6 +431,9 @@ public void shouldHandleIllegalJSONNodeNames()
assertEquals(expected, result);
}

/**
* JSONObject with NULL value, to XML.toString()
*/
@Test
public void shouldHandleNullNodeValue()
{
Expand All @@ -338,12 +450,11 @@ public void shouldHandleNullNodeValue()
assertEquals(actualXML, resultXML);
}

/**
* Investigate exactly how the "content" keyword works
*/
@Test
public void contentOperations() {
/**
* Make sure we understand exactly how the "content" keyword works
*/

/**
* When a standalone <!CDATA[...]] structure is found while parsing XML into a
* JSONObject, the contents are placed in a string value with key="content".
Expand Down Expand Up @@ -481,4 +592,51 @@ public void contentOperations() {
*/
assertTrue("nothing to test here, see comment on created XML, above", true);
}

/**
* Convenience method, given an input string and expected result,
* convert to JSONBObject and compare actual to expected result.
* @param xmlStr the string to parse
* @param expectedStr the expected JSON string
*/
private void compareStringToJSONObject(String xmlStr, String expectedStr) {
JSONObject expectedJsonObject = new JSONObject(expectedStr);
JSONObject jsonObject = XML.toJSONObject(xmlStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}

/**
* Convenience method, given an input string and expected result,
* convert to JSONBObject via reader and compare actual to expected result.
* @param xmlStr the string to parse
* @param expectedStr the expected JSON string
*/
private void compareReaderToJSONObject(String xmlStr, String expectedStr) {
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Reader reader = new StringReader(xmlStr);
JSONObject jsonObject = XML.toJSONObject(reader);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}

/**
* Convenience method, given an input string and expected result,
* convert to JSONBObject via file and compare actual to expected result.
* @param xmlStr the string to parse
* @param expectedStr the expected JSON string
* @throws IOException
*/
private void compareFileToJSONObject(String xmlStr, String expectedStr) {
try {
JSONObject expectedJsonObject = new JSONObject(expectedStr);
File tempFile = testFolder.newFile("fileToJSONObject.xml");
FileWriter fileWriter = new FileWriter(tempFile);
fileWriter.write(xmlStr);
fileWriter.close();
Reader reader = new FileReader(tempFile);
JSONObject jsonObject = XML.toJSONObject(reader);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
} catch (IOException e) {
assertTrue("file writer error: " +e.getMessage(), false);
}
}
}

0 comments on commit 3f78a85

Please sign in to comment.