From f4261add919fcec3a72635a5a26c9fe3b558bf2a Mon Sep 17 00:00:00 2001 From: viveksacademia4git <45398326+viveksacademia4git@users.noreply.github.com> Date: Wed, 3 Jun 2020 11:58:28 +0200 Subject: [PATCH 1/3] Create pipeline.yml --- .github/workflows/pipeline.yml | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 .github/workflows/pipeline.yml diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml new file mode 100644 index 000000000..28df9f3d4 --- /dev/null +++ b/.github/workflows/pipeline.yml @@ -0,0 +1,53 @@ +# This workflow will build a Java project with Maven +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven + +name: Java CI with Maven + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + compile: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 1.8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + - name: Compile with Maven + run: mvn compile + - name: Assemble with Gradle + run: sh ./gradlew assemble + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 1.8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + - name: Test with Maven + run: mvn test + - name: Gradle Check, Gradle Test is inclusive + run: sh ./gradlew assemble check + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 1.8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + - name: Build with Maven + run: mvn -B package --file pom.xml + - name: Build with Gradle + run: sh ./gradlew build + #- name: Publish to GitHub Packages Apache Maven + # run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml + # env: + # GITHUB_TOKEN: ${{ github.token }} + From 96bf7885154029254bc4361a8cdc4e63172306cb Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 3 Jun 2020 18:29:29 -0400 Subject: [PATCH 2/3] update pipline to use matrix configuration --- .github/workflows/pipeline.yml | 80 +++++++++++++++++----------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 28df9f3d4..715d7d993 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -5,49 +5,51 @@ name: Java CI with Maven on: push: - branches: [ master ] + # branches: [ master ] pull_request: branches: [ master ] jobs: - compile: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Set up JDK 1.8 - uses: actions/setup-java@v1 - with: - java-version: 1.8 - - name: Compile with Maven - run: mvn compile - - name: Assemble with Gradle - run: sh ./gradlew assemble - test: - runs-on: ubuntu-latest + # old-school build and jar method. No tests run or compiled. + build-1_6: + runs-on: ubuntu-16.04 + strategy: + matrix: + # build for java 1.6, however don't run any tests + java: [ 1.6 ] + name: Java ${{ matrix.java }} steps: - - uses: actions/checkout@v2 - - name: Set up JDK 1.8 - uses: actions/setup-java@v1 - with: - java-version: 1.8 - - name: Test with Maven - run: mvn test - - name: Gradle Check, Gradle Test is inclusive - run: sh ./gradlew assemble check + - uses: actions/checkout@v2 + - name: Setup java + uses: actions/setup-java@v1 + with: + java-version: ${{ matrix.java }} + - name: Compile Java ${{ matrix.java }} + run: | + mkdir -p target/classes + javac -d target/classes/ src/main/java/org/json/*.java + - name: Create java ${{ matrix.java }} JAR + run: | + jar cvf target/org.json.jar -C target/classes . + - name: Upload Java ${{ matrix.java }} JAR + uses: actions/upload-artifact@v1 + with: + name: Java ${{ matrix.java }} JAR + path: target/org.json.jar + build: - runs-on: ubuntu-latest + runs-on: ubuntu-16.04 + strategy: + matrix: + # build against supported Java LTS versions: + java: [ 1.7, 8, 11 ] + name: Java ${{ matrix.java }} Compile steps: - - uses: actions/checkout@v2 - - name: Set up JDK 1.8 - uses: actions/setup-java@v1 - with: - java-version: 1.8 - - name: Build with Maven - run: mvn -B package --file pom.xml - - name: Build with Gradle - run: sh ./gradlew build - #- name: Publish to GitHub Packages Apache Maven - # run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml - # env: - # GITHUB_TOKEN: ${{ github.token }} - + - uses: actions/checkout@v2 + - name: Setup java + uses: actions/setup-java@v1 + with: + 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 + - 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 + \ No newline at end of file From c11c006e880f1499a08abea78494d8f9ad613346 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Wed, 3 Jun 2020 19:03:38 -0400 Subject: [PATCH 3/3] fix tests to not depend on HashSet key order --- .github/workflows/pipeline.yml | 27 +++++- .../java/org/json/junit/JSONObjectTest.java | 56 ++++++++----- .../java/org/json/junit/JunitTestSuite.java | 49 ----------- src/test/java/org/json/junit/TestRunner.java | 43 ---------- .../org/json/junit/XMLConfigurationTest.java | 82 ++++++++++--------- src/test/java/org/json/junit/XMLTest.java | 68 +++++++-------- 6 files changed, 131 insertions(+), 194 deletions(-) delete mode 100644 src/test/java/org/json/junit/JunitTestSuite.java delete mode 100644 src/test/java/org/json/junit/TestRunner.java diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 715d7d993..98ee6c185 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -43,13 +43,32 @@ jobs: matrix: # build against supported Java LTS versions: java: [ 1.7, 8, 11 ] - name: Java ${{ matrix.java }} Compile + name: Java ${{ matrix.java }} steps: - uses: actions/checkout@v2 - name: Setup java uses: actions/setup-java@v1 with: 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 - - 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 - \ No newline at end of file + - name: Compile Java ${{ 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: 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/ \ No newline at end of file diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 5cfc8de8e..c24d138c6 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1856,10 +1856,12 @@ public void jsonObjectToStringIndent() { " ]\n" + "}"; JSONObject jsonObject = new JSONObject(jsonObject0Str); - assertEquals("toString()",jsonObject0Str, jsonObject.toString()); - assertEquals("toString(0)",jsonObject0Str, jsonObject.toString(0)); - assertEquals("toString(1)",jsonObject1Str, jsonObject.toString(1)); - assertEquals("toString(4)",jsonObject4Str, jsonObject.toString(4)); + // contents are tested in other methods, in this case just validate the spacing by + // checking length + assertEquals("toString() length",jsonObject0Str.length(), jsonObject.toString().length()); + 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())); assertEquals("toString(2)","{\"TABLE\": {\"yhoo\": {}}}", jo.toString(2)); @@ -2630,9 +2632,10 @@ public void write() throws IOException { JSONObject jsonObject = new JSONObject(str); try (StringWriter stringWriter = new StringWriter()) { String actualStr = jsonObject.write(stringWriter).toString(); - assertTrue("write() expected " +expectedStr+ - " but found " +actualStr, - expectedStr.equals(actualStr)); + // key order may change. verify length and individual key content + assertEquals("length", expectedStr.length(), actualStr.length()); + assertTrue("key1", actualStr.contains("\"key1\":\"value1\"")); + assertTrue("key2", actualStr.contains("\"key2\":[1,2,3]")); } } @@ -2734,16 +2737,25 @@ public void write3Param() throws IOException { " ]\n" + " }"; JSONObject jsonObject = new JSONObject(str0); - String expectedStr = str0; try (StringWriter stringWriter = new StringWriter();) { 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();) { 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 void toMap() { @Test public void testSingletonBean() { final JSONObject jo = new JSONObject(Singleton.getInstance()); -// assertEquals(jo.keySet().toString(), 1, jo.length()); -// assertEquals(0, jo.get("someInt")); -// assertEquals(null, jo.opt("someString")); + assertEquals(jo.keySet().toString(), 1, jo.length()); + assertEquals(0, jo.get("someInt")); + assertEquals(null, jo.opt("someString")); // Update the singleton values Singleton.getInstance().setSomeInt(42); @@ -2991,8 +3003,8 @@ public void testSingletonBean() { assertEquals("Something", jo2.get("someString")); // ensure our original jo hasn't changed. -// assertEquals(0, jo.get("someInt")); -// assertEquals(null, jo.opt("someString")); + assertEquals(0, jo.get("someInt")); + assertEquals(null, jo.opt("someString")); } /** @@ -3002,9 +3014,9 @@ public void testSingletonBean() { @Test public void testSingletonEnumBean() { final JSONObject jo = new JSONObject(SingletonEnum.getInstance()); -// assertEquals(jo.keySet().toString(), 1, jo.length()); -// assertEquals(0, jo.get("someInt")); -// assertEquals(null, jo.opt("someString")); + assertEquals(jo.keySet().toString(), 1, jo.length()); + assertEquals(0, jo.get("someInt")); + assertEquals(null, jo.opt("someString")); // Update the singleton values SingletonEnum.getInstance().setSomeInt(42); @@ -3015,8 +3027,8 @@ public void testSingletonEnumBean() { assertEquals("Something", jo2.get("someString")); // ensure our original jo hasn't changed. -// assertEquals(0, jo.get("someInt")); -// assertEquals(null, jo.opt("someString")); + assertEquals(0, jo.get("someInt")); + assertEquals(null, jo.opt("someString")); } /** diff --git a/src/test/java/org/json/junit/JunitTestSuite.java b/src/test/java/org/json/junit/JunitTestSuite.java deleted file mode 100644 index 12816250f..000000000 --- a/src/test/java/org/json/junit/JunitTestSuite.java +++ /dev/null @@ -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 { -} diff --git a/src/test/java/org/json/junit/TestRunner.java b/src/test/java/org/json/junit/TestRunner.java deleted file mode 100644 index 3b4aeef31..000000000 --- a/src/test/java/org/json/junit/TestRunner.java +++ /dev/null @@ -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()); - } -} \ No newline at end of file diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 6919b3185..14c4ba048 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -29,6 +29,13 @@ of this software and associated documentation files (the "Software"), to deal import static org.junit.Assert.assertTrue; 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.JSONException; import org.json.JSONObject; @@ -369,16 +376,11 @@ public void shouldHandleToString() { @Test public void shouldHandleContentNoArraytoString() { String expectedStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ - "altContent\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + "{\"addresses\":{\"altContent\":\">\"}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); XMLParserConfiguration config = new XMLParserConfiguration("altContent"); String finalStr = XML.toString(expectedJsonObject, null, config); - String expectedFinalStr = "
>"+ - "
test.xsdhttp://www.w3.org/2001/XMLSche"+ - "ma-instance
"; + String expectedFinalStr = ">"; assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ finalStr+"]", expectedFinalStr.equals(finalStr)); } @@ -391,17 +393,13 @@ public void shouldHandleContentNoArraytoString() { @Test public void shouldHandleContentArraytoString() { String expectedStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ - "altContent\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + "{\"addresses\":{\"altContent\":[1, 2, 3]}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); XMLParserConfiguration config = new XMLParserConfiguration("altContent"); String finalStr = XML.toString(expectedJsonObject, null, config); - String expectedFinalStr = "
"+ + String expectedFinalStr = ""+ "1\n2\n3"+ - "
test.xsdhttp://www.w3.org/2001/XMLSche"+ - "ma-instance
"; + ""; assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ finalStr+"]", expectedFinalStr.equals(finalStr)); } @@ -413,17 +411,14 @@ public void shouldHandleContentArraytoString() { @Test public void shouldHandleArraytoString() { String expectedStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ - "\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + "{\"addresses\":{"+ + "\"something\":[1, 2, 3]}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); String finalStr = XML.toString(expectedJsonObject, null, XMLParserConfiguration.KEEP_STRINGS); - String expectedFinalStr = "
"+ + String expectedFinalStr = ""+ "123"+ - "
test.xsdhttp://www.w3.org/2001/XMLSche"+ - "ma-instance
"; + ""; assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ finalStr+"]", expectedFinalStr.equals(finalStr)); } @@ -555,7 +550,9 @@ public void shouldHandleIllegalJSONNodeNames() */ String expected = "<123IllegalNode>someValue1someValue2"; - assertEquals(expected, result); + assertEquals("Length", expected.length(), result.length()); + assertTrue("123IllegalNode", result.contains("<123IllegalNode>someValue1")); + assertTrue("Illegal@node", result.contains("someValue2")); } /** @@ -740,10 +737,10 @@ public void contentOperations() { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - 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, new XMLParserConfiguration(false)); - assertEquals(expectedJsonString, actualJsonOutput.toString()); + Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected); } /** @@ -765,17 +762,20 @@ public void testToJSONArray_reversibility() { @Test public void testToJsonXML() { final String originalXml = "011000True"; - 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, new XMLParserConfiguration(true)); - assertEquals(expectedJsonString, json.toString()); + Util.compareActualVsExpectedJsonObjects(json, expected); final String reverseXml = XML.toString(json); // this reversal isn't exactly the same. use JSONML for an exact reversal final String expectedReverseXml = "01011000True"; - assertEquals(expectedReverseXml, reverseXml); + assertEquals("length",expectedReverseXml.length(), reverseXml.length()); + assertTrue("array contents", reverseXml.contains("011000")); + assertTrue("item contents", reverseXml.contains("01")); + assertTrue("title contents", reverseXml.contains("True")); } /** @@ -916,11 +916,14 @@ private void compareReaderToJSONObject(String xmlStr, String expectedStr, /* * Commenting out this method until the JSON-java code is updated * to support XML.toJSONObject(reader) + */ JSONObject expectedJsonObject = new JSONObject(expectedStr); - Reader reader = new StringReader(xmlStr); - JSONObject jsonObject = XML.toJSONObject(reader); - Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); - */ + try(Reader reader = new StringReader(xmlStr);) { + JSONObject jsonObject = XML.toJSONObject(reader, config); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } catch (IOException e) { + assertTrue("IO Reader error: " +e.getMessage(), false); + } } /** @@ -937,18 +940,19 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) { /* * Commenting out this method until the JSON-java code is updated * to support XML.toJSONObject(reader) + */ 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); + File tempFile = this.testFolder.newFile("fileToJSONObject.xml"); + try(FileWriter fileWriter = new FileWriter(tempFile);){ + fileWriter.write(xmlStr); + } + try(Reader reader = new FileReader(tempFile);){ + JSONObject jsonObject = XML.toJSONObject(reader); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } } catch (IOException e) { assertTrue("file writer error: " +e.getMessage(), false); } - */ } } \ No newline at end of file diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index d8ef0d061..d59496133 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -419,18 +419,12 @@ public void shouldHandleToString() { */ @Test public void shouldHandleContentNoArraytoString() { - String expectedStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ - "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + String expectedStr = "{\"addresses\":{\"content\":\">\"}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); String finalStr = XML.toString(expectedJsonObject); - String expectedFinalStr = "
>"+ - "
test.xsdhttp://www.w3.org/2001/XMLSche"+ - "ma-instance
"; - assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ - finalStr+"]", expectedFinalStr.equals(finalStr)); + String expectedFinalStr = ">"; + assertEquals("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr, finalStr); } /** @@ -441,18 +435,14 @@ public void shouldHandleContentNoArraytoString() { @Test public void shouldHandleContentArraytoString() { String expectedStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ - "content\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + "{\"addresses\":{" + + "\"content\":[1, 2, 3]}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); String finalStr = XML.toString(expectedJsonObject); - String expectedFinalStr = "
"+ - "1\n2\n3"+ - "
test.xsdhttp://www.w3.org/2001/XMLSche"+ - "ma-instance
"; - assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ - finalStr+"]", expectedFinalStr.equals(finalStr)); + String expectedFinalStr = ""+ + "1\n2\n3"; + assertEquals("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr, finalStr); } /** @@ -462,18 +452,15 @@ public void shouldHandleContentArraytoString() { @Test public void shouldHandleArraytoString() { String expectedStr = - "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ - "\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ - "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + "{\"addresses\":{"+ + "\"something\":[1, 2, 3]}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); String finalStr = XML.toString(expectedJsonObject); - String expectedFinalStr = "
"+ + String expectedFinalStr = ""+ "123"+ - "
test.xsdhttp://www.w3.org/2001/XMLSche"+ - "ma-instance
"; - assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ - finalStr+"]", expectedFinalStr.equals(finalStr)); + ""; + assertEquals("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr, finalStr); } /** @@ -591,7 +578,9 @@ public void shouldHandleIllegalJSONNodeNames() */ String expected = "<123IllegalNode>someValue1someValue2"; - assertEquals(expected, result); + assertEquals("length",expected.length(), result.length()); + assertTrue("123IllegalNode",result.contains("<123IllegalNode>someValue1")); + assertTrue("Illegal@node",result.contains("someValue2")); } /** @@ -813,10 +802,10 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - 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); - assertEquals(expectedJsonString, actualJsonOutput.toString()); + Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expectedJson); } /** @@ -836,16 +825,21 @@ public void testToJSONArray_reversibility() { @Test public void testToJsonXML() { final String originalXml = "011000True"; - 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); - assertEquals(expectedJsonString, json.toString()); + final JSONObject actual = XML.toJSONObject(originalXml,true); + + Util.compareActualVsExpectedJsonObjects(actual, expected); - final String reverseXml = XML.toString(json); + final String reverseXml = XML.toString(actual); // 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 = "01011000True"; - assertEquals(expectedReverseXml, reverseXml); + assertEquals("length",expectedReverseXml.length(), reverseXml.length()); + assertTrue("array contents", reverseXml.contains("011000")); + assertTrue("item contents", reverseXml.contains("01")); + assertTrue("title contents", reverseXml.contains("True")); } /**