1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

fix tests to not depend on HashSet key order

This commit is contained in:
John J. Aylward 2020-06-03 19:03:38 -04:00
parent 96bf788515
commit c11c006e88
6 changed files with 131 additions and 194 deletions

View file

@ -43,13 +43,32 @@ jobs:
matrix: matrix:
# build against supported Java LTS versions: # build against supported Java LTS versions:
java: [ 1.7, 8, 11 ] java: [ 1.7, 8, 11 ]
name: Java ${{ matrix.java }} Compile name: Java ${{ matrix.java }}
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Setup java - name: Setup java
uses: actions/setup-java@v1 uses: actions/setup-java@v1
with: with:
java-version: ${{ matrix.java }} java-version: ${{ matrix.java }}
- run: mvn clean compile -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true - name: Compile Java ${{ matrix.java }}
- run: mvn test -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true run: mvn clean compile -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true
- name: Run Tests ${{ matrix.java }}
run: |
mvn test -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }}
- name: Build Test Report ${{ matrix.java }}
if: ${{ always() }}
run: |
mvn surefire-report:report-only -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }}
mvn site -DgenerateReports=false -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }}
- name: Upload Test Results ${{ matrix.java }}
if: ${{ always() }}
uses: actions/upload-artifact@v1
with:
name: Test Results ${{ matrix.java }}
path: target/surefire-reports/
- name: Upload Test Report ${{ matrix.java }}
if: ${{ always() }}
uses: actions/upload-artifact@v1
with:
name: Test Report ${{ matrix.java }}
path: target/site/

View file

@ -1856,10 +1856,12 @@ public class JSONObjectTest {
" ]\n" + " ]\n" +
"}"; "}";
JSONObject jsonObject = new JSONObject(jsonObject0Str); JSONObject jsonObject = new JSONObject(jsonObject0Str);
assertEquals("toString()",jsonObject0Str, jsonObject.toString()); // contents are tested in other methods, in this case just validate the spacing by
assertEquals("toString(0)",jsonObject0Str, jsonObject.toString(0)); // checking length
assertEquals("toString(1)",jsonObject1Str, jsonObject.toString(1)); assertEquals("toString() length",jsonObject0Str.length(), jsonObject.toString().length());
assertEquals("toString(4)",jsonObject4Str, jsonObject.toString(4)); assertEquals("toString(0) length",jsonObject0Str.length(), jsonObject.toString(0).length());
assertEquals("toString(1) length",jsonObject1Str.length(), jsonObject.toString(1).length());
assertEquals("toString(4) length",jsonObject4Str.length(), jsonObject.toString(4).length());
JSONObject jo = new JSONObject().put("TABLE", new JSONObject().put("yhoo", new JSONObject())); JSONObject jo = new JSONObject().put("TABLE", new JSONObject().put("yhoo", new JSONObject()));
assertEquals("toString(2)","{\"TABLE\": {\"yhoo\": {}}}", jo.toString(2)); assertEquals("toString(2)","{\"TABLE\": {\"yhoo\": {}}}", jo.toString(2));
@ -2630,9 +2632,10 @@ public class JSONObjectTest {
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
try (StringWriter stringWriter = new StringWriter()) { try (StringWriter stringWriter = new StringWriter()) {
String actualStr = jsonObject.write(stringWriter).toString(); String actualStr = jsonObject.write(stringWriter).toString();
assertTrue("write() expected " +expectedStr+ // key order may change. verify length and individual key content
" but found " +actualStr, assertEquals("length", expectedStr.length(), actualStr.length());
expectedStr.equals(actualStr)); assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
assertTrue("key2", actualStr.contains("\"key2\":[1,2,3]"));
} }
} }
@ -2734,16 +2737,25 @@ public class JSONObjectTest {
" ]\n" + " ]\n" +
" }"; " }";
JSONObject jsonObject = new JSONObject(str0); JSONObject jsonObject = new JSONObject(str0);
String expectedStr = str0;
try (StringWriter stringWriter = new StringWriter();) { try (StringWriter stringWriter = new StringWriter();) {
String actualStr = jsonObject.write(stringWriter,0,0).toString(); String actualStr = jsonObject.write(stringWriter,0,0).toString();
assertEquals(expectedStr, actualStr);
assertEquals("length", str0.length(), actualStr.length());
assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
assertTrue("key2", actualStr.contains("\"key2\":[1,false,3.14]"));
} }
expectedStr = str2;
try (StringWriter stringWriter = new StringWriter();) { try (StringWriter stringWriter = new StringWriter();) {
String actualStr = jsonObject.write(stringWriter,2,1).toString(); String actualStr = jsonObject.write(stringWriter,2,1).toString();
assertEquals(expectedStr, actualStr);
assertEquals("length", str2.length(), actualStr.length());
assertTrue("key1", actualStr.contains(" \"key1\": \"value1\""));
assertTrue("key2", actualStr.contains(" \"key2\": [\n" +
" 1,\n" +
" false,\n" +
" 3.14\n" +
" ]")
);
} }
} }
@ -2978,9 +2990,9 @@ public class JSONObjectTest {
@Test @Test
public void testSingletonBean() { public void testSingletonBean() {
final JSONObject jo = new JSONObject(Singleton.getInstance()); final JSONObject jo = new JSONObject(Singleton.getInstance());
// assertEquals(jo.keySet().toString(), 1, jo.length()); assertEquals(jo.keySet().toString(), 1, jo.length());
// assertEquals(0, jo.get("someInt")); assertEquals(0, jo.get("someInt"));
// assertEquals(null, jo.opt("someString")); assertEquals(null, jo.opt("someString"));
// Update the singleton values // Update the singleton values
Singleton.getInstance().setSomeInt(42); Singleton.getInstance().setSomeInt(42);
@ -2991,8 +3003,8 @@ public class JSONObjectTest {
assertEquals("Something", jo2.get("someString")); assertEquals("Something", jo2.get("someString"));
// ensure our original jo hasn't changed. // ensure our original jo hasn't changed.
// assertEquals(0, jo.get("someInt")); assertEquals(0, jo.get("someInt"));
// assertEquals(null, jo.opt("someString")); assertEquals(null, jo.opt("someString"));
} }
/** /**
@ -3002,9 +3014,9 @@ public class JSONObjectTest {
@Test @Test
public void testSingletonEnumBean() { public void testSingletonEnumBean() {
final JSONObject jo = new JSONObject(SingletonEnum.getInstance()); final JSONObject jo = new JSONObject(SingletonEnum.getInstance());
// assertEquals(jo.keySet().toString(), 1, jo.length()); assertEquals(jo.keySet().toString(), 1, jo.length());
// assertEquals(0, jo.get("someInt")); assertEquals(0, jo.get("someInt"));
// assertEquals(null, jo.opt("someString")); assertEquals(null, jo.opt("someString"));
// Update the singleton values // Update the singleton values
SingletonEnum.getInstance().setSomeInt(42); SingletonEnum.getInstance().setSomeInt(42);
@ -3015,8 +3027,8 @@ public class JSONObjectTest {
assertEquals("Something", jo2.get("someString")); assertEquals("Something", jo2.get("someString"));
// ensure our original jo hasn't changed. // ensure our original jo hasn't changed.
// assertEquals(0, jo.get("someInt")); assertEquals(0, jo.get("someInt"));
// assertEquals(null, jo.opt("someString")); assertEquals(null, jo.opt("someString"));
} }
/** /**

View file

@ -1,49 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
CDLTest.class,
CookieTest.class,
CookieListTest.class,
PropertyTest.class,
XMLTest.class,
JSONMLTest.class,
HTTPTest.class,
JSONStringerTest.class,
JSONObjectTest.class,
JSONObjectLocaleTest.class,
JSONArrayTest.class,
EnumTest.class,
JSONPointerTest.class,
JSONStringTest.class,
JSONTokenerTest.class,
XMLConfigurationTest.class
})
public class JunitTestSuite {
}

View file

@ -1,43 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
/**
* Invoke this class main method if you want to run unit tests from the
* command line. If successful, will print "true" to stdout.
*/
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(JunitTestSuite.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}

View file

@ -29,6 +29,13 @@ import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -369,16 +376,11 @@ public class XMLConfigurationTest {
@Test @Test
public void shouldHandleContentNoArraytoString() { public void shouldHandleContentNoArraytoString() {
String expectedStr = String expectedStr =
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ "{\"addresses\":{\"altContent\":\">\"}}";
"altContent\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject expectedJsonObject = new JSONObject(expectedStr);
XMLParserConfiguration config = new XMLParserConfiguration("altContent"); XMLParserConfiguration config = new XMLParserConfiguration("altContent");
String finalStr = XML.toString(expectedJsonObject, null, config); String finalStr = XML.toString(expectedJsonObject, null, config);
String expectedFinalStr = "<addresses><address><name/><nocontent/>&gt;"+ String expectedFinalStr = "<addresses>&gt;</addresses>";
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
"ma-instance</xmlns:xsi></addresses>";
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
finalStr+"]", expectedFinalStr.equals(finalStr)); finalStr+"]", expectedFinalStr.equals(finalStr));
} }
@ -391,17 +393,13 @@ public class XMLConfigurationTest {
@Test @Test
public void shouldHandleContentArraytoString() { public void shouldHandleContentArraytoString() {
String expectedStr = String expectedStr =
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ "{\"addresses\":{\"altContent\":[1, 2, 3]}}";
"altContent\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject expectedJsonObject = new JSONObject(expectedStr);
XMLParserConfiguration config = new XMLParserConfiguration("altContent"); XMLParserConfiguration config = new XMLParserConfiguration("altContent");
String finalStr = XML.toString(expectedJsonObject, null, config); String finalStr = XML.toString(expectedJsonObject, null, config);
String expectedFinalStr = "<addresses><address><name/><nocontent/>"+ String expectedFinalStr = "<addresses>"+
"1\n2\n3"+ "1\n2\n3"+
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+ "</addresses>";
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
"ma-instance</xmlns:xsi></addresses>";
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
finalStr+"]", expectedFinalStr.equals(finalStr)); finalStr+"]", expectedFinalStr.equals(finalStr));
} }
@ -413,17 +411,14 @@ public class XMLConfigurationTest {
@Test @Test
public void shouldHandleArraytoString() { public void shouldHandleArraytoString() {
String expectedStr = String expectedStr =
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ "{\"addresses\":{"+
"\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ "\"something\":[1, 2, 3]}}";
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject expectedJsonObject = new JSONObject(expectedStr);
String finalStr = XML.toString(expectedJsonObject, null, String finalStr = XML.toString(expectedJsonObject, null,
XMLParserConfiguration.KEEP_STRINGS); XMLParserConfiguration.KEEP_STRINGS);
String expectedFinalStr = "<addresses><address><name/><nocontent/>"+ String expectedFinalStr = "<addresses>"+
"<something>1</something><something>2</something><something>3</something>"+ "<something>1</something><something>2</something><something>3</something>"+
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+ "</addresses>";
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
"ma-instance</xmlns:xsi></addresses>";
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
finalStr+"]", expectedFinalStr.equals(finalStr)); finalStr+"]", expectedFinalStr.equals(finalStr));
} }
@ -555,7 +550,9 @@ public class XMLConfigurationTest {
*/ */
String expected = "<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>"; String expected = "<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>";
assertEquals(expected, result); assertEquals("Length", expected.length(), result.length());
assertTrue("123IllegalNode", result.contains("<123IllegalNode>someValue1</123IllegalNode>"));
assertTrue("Illegal@node", result.contains("<Illegal@node>someValue2</Illegal@node>"));
} }
/** /**
@ -740,10 +737,10 @@ public class XMLConfigurationTest {
@Test @Test
public void testToJSONArray_jsonOutput() { public void testToJSONArray_jsonOutput() {
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>"; final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"; final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}");
final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, final JSONObject actualJsonOutput = XML.toJSONObject(originalXml,
new XMLParserConfiguration(false)); new XMLParserConfiguration(false));
assertEquals(expectedJsonString, actualJsonOutput.toString()); Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
} }
/** /**
@ -765,17 +762,20 @@ public class XMLConfigurationTest {
@Test @Test
public void testToJsonXML() { public void testToJsonXML() {
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>"; final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}"; final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}");
final JSONObject json = XML.toJSONObject(originalXml, final JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration(true)); new XMLParserConfiguration(true));
assertEquals(expectedJsonString, json.toString()); Util.compareActualVsExpectedJsonObjects(json, expected);
final String reverseXml = XML.toString(json); final String reverseXml = XML.toString(json);
// this reversal isn't exactly the same. use JSONML for an exact reversal // this reversal isn't exactly the same. use JSONML for an exact reversal
final String expectedReverseXml = "<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>"; final String expectedReverseXml = "<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>";
assertEquals(expectedReverseXml, reverseXml); assertEquals("length",expectedReverseXml.length(), reverseXml.length());
assertTrue("array contents", reverseXml.contains("<id>01</id><id>1</id><id>00</id><id>0</id>"));
assertTrue("item contents", reverseXml.contains("<item><id>01</id></item>"));
assertTrue("title contents", reverseXml.contains("<title>True</title>"));
} }
/** /**
@ -916,11 +916,14 @@ public class XMLConfigurationTest {
/* /*
* Commenting out this method until the JSON-java code is updated * Commenting out this method until the JSON-java code is updated
* to support XML.toJSONObject(reader) * to support XML.toJSONObject(reader)
*/
JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject expectedJsonObject = new JSONObject(expectedStr);
Reader reader = new StringReader(xmlStr); try(Reader reader = new StringReader(xmlStr);) {
JSONObject jsonObject = XML.toJSONObject(reader); JSONObject jsonObject = XML.toJSONObject(reader, config);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
*/ } catch (IOException e) {
assertTrue("IO Reader error: " +e.getMessage(), false);
}
} }
/** /**
@ -937,18 +940,19 @@ public class XMLConfigurationTest {
/* /*
* Commenting out this method until the JSON-java code is updated * Commenting out this method until the JSON-java code is updated
* to support XML.toJSONObject(reader) * to support XML.toJSONObject(reader)
*/
try { try {
JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject expectedJsonObject = new JSONObject(expectedStr);
File tempFile = testFolder.newFile("fileToJSONObject.xml"); File tempFile = this.testFolder.newFile("fileToJSONObject.xml");
FileWriter fileWriter = new FileWriter(tempFile); try(FileWriter fileWriter = new FileWriter(tempFile);){
fileWriter.write(xmlStr); fileWriter.write(xmlStr);
fileWriter.close(); }
Reader reader = new FileReader(tempFile); try(Reader reader = new FileReader(tempFile);){
JSONObject jsonObject = XML.toJSONObject(reader); JSONObject jsonObject = XML.toJSONObject(reader);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
} catch (IOException e) { } catch (IOException e) {
assertTrue("file writer error: " +e.getMessage(), false); assertTrue("file writer error: " +e.getMessage(), false);
} }
*/
} }
} }

View file

@ -419,18 +419,12 @@ public class XMLTest {
*/ */
@Test @Test
public void shouldHandleContentNoArraytoString() { public void shouldHandleContentNoArraytoString() {
String expectedStr = String expectedStr = "{\"addresses\":{\"content\":\">\"}}";
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+
"content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject expectedJsonObject = new JSONObject(expectedStr);
String finalStr = XML.toString(expectedJsonObject); String finalStr = XML.toString(expectedJsonObject);
String expectedFinalStr = "<addresses><address><name/><nocontent/>&gt;"+ String expectedFinalStr = "<addresses>&gt;</addresses>";
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+ assertEquals("Should handle expectedFinal: ["+expectedStr+"] final: ["+
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+ finalStr+"]", expectedFinalStr, finalStr);
"ma-instance</xmlns:xsi></addresses>";
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
finalStr+"]", expectedFinalStr.equals(finalStr));
} }
/** /**
@ -441,18 +435,14 @@ public class XMLTest {
@Test @Test
public void shouldHandleContentArraytoString() { public void shouldHandleContentArraytoString() {
String expectedStr = String expectedStr =
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ "{\"addresses\":{" +
"content\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ "\"content\":[1, 2, 3]}}";
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject expectedJsonObject = new JSONObject(expectedStr);
String finalStr = XML.toString(expectedJsonObject); String finalStr = XML.toString(expectedJsonObject);
String expectedFinalStr = "<addresses><address><name/><nocontent/>"+ String expectedFinalStr = "<addresses>"+
"1\n2\n3"+ "1\n2\n3</addresses>";
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+ assertEquals("Should handle expectedFinal: ["+expectedStr+"] final: ["+
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+ finalStr+"]", expectedFinalStr, finalStr);
"ma-instance</xmlns:xsi></addresses>";
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
finalStr+"]", expectedFinalStr.equals(finalStr));
} }
/** /**
@ -462,18 +452,15 @@ public class XMLTest {
@Test @Test
public void shouldHandleArraytoString() { public void shouldHandleArraytoString() {
String expectedStr = String expectedStr =
"{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ "{\"addresses\":{"+
"\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ "\"something\":[1, 2, 3]}}";
"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
JSONObject expectedJsonObject = new JSONObject(expectedStr); JSONObject expectedJsonObject = new JSONObject(expectedStr);
String finalStr = XML.toString(expectedJsonObject); String finalStr = XML.toString(expectedJsonObject);
String expectedFinalStr = "<addresses><address><name/><nocontent/>"+ String expectedFinalStr = "<addresses>"+
"<something>1</something><something>2</something><something>3</something>"+ "<something>1</something><something>2</something><something>3</something>"+
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+ "</addresses>";
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+ assertEquals("Should handle expectedFinal: ["+expectedStr+"] final: ["+
"ma-instance</xmlns:xsi></addresses>"; finalStr+"]", expectedFinalStr, finalStr);
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
finalStr+"]", expectedFinalStr.equals(finalStr));
} }
/** /**
@ -591,7 +578,9 @@ public class XMLTest {
*/ */
String expected = "<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>"; String expected = "<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>";
assertEquals(expected, result); assertEquals("length",expected.length(), result.length());
assertTrue("123IllegalNode",result.contains("<123IllegalNode>someValue1</123IllegalNode>"));
assertTrue("Illegal@node",result.contains("<Illegal@node>someValue2</Illegal@node>"));
} }
/** /**
@ -813,10 +802,10 @@ public class XMLTest {
@Test @Test
public void testToJSONArray_jsonOutput() { public void testToJSONArray_jsonOutput() {
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>"; final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"; final JSONObject expectedJson = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}");
final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false); final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false);
assertEquals(expectedJsonString, actualJsonOutput.toString()); Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expectedJson);
} }
/** /**
@ -836,16 +825,21 @@ public class XMLTest {
@Test @Test
public void testToJsonXML() { public void testToJsonXML() {
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>"; final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}"; final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}");
final JSONObject json = XML.toJSONObject(originalXml,true); final JSONObject actual = XML.toJSONObject(originalXml,true);
assertEquals(expectedJsonString, json.toString());
final String reverseXml = XML.toString(json); Util.compareActualVsExpectedJsonObjects(actual, expected);
final String reverseXml = XML.toString(actual);
// this reversal isn't exactly the same. use JSONML for an exact reversal // this reversal isn't exactly the same. use JSONML for an exact reversal
// the order of the elements may be differnet as well.
final String expectedReverseXml = "<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>"; final String expectedReverseXml = "<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>";
assertEquals(expectedReverseXml, reverseXml); assertEquals("length",expectedReverseXml.length(), reverseXml.length());
assertTrue("array contents", reverseXml.contains("<id>01</id><id>1</id><id>00</id><id>0</id>"));
assertTrue("item contents", reverseXml.contains("<item><id>01</id></item>"));
assertTrue("title contents", reverseXml.contains("<title>True</title>"));
} }
/** /**