Skip to content

Commit

Permalink
Merge pull request #2 from stleary/master
Browse files Browse the repository at this point in the history
Update My Master Branch
  • Loading branch information
viveksacademia4git committed Jun 6, 2020
2 parents eafde39 + 601114e commit 70fcc78
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 89 deletions.
74 changes: 74 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# 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:
# 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: 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-16.04
strategy:
matrix:
# build against supported Java LTS versions:
java: [ 1.7, 8, 11 ]
name: Java ${{ matrix.java }}
steps:
- uses: actions/checkout@v2
- name: Setup java
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- 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/
38 changes: 25 additions & 13 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ public void stringToValueNumbersTest() {
assertTrue( "Integer.MAX_VALUE should still be an Integer!",
JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer );
assertTrue( "Large integers should be a Long!",
JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long );
JSONObject.stringToValue( Long.valueOf(((long)Integer.MAX_VALUE) + 1 ) .toString() ) instanceof Long );
assertTrue( "Long.MAX_VALUE should still be an Integer!",
JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long );

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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]"));
}
}

Expand Down Expand Up @@ -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" +
" ]")
);
}
}

Expand Down
82 changes: 43 additions & 39 deletions src/test/java/org/json/junit/XMLConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = "<addresses><address><name/><nocontent/>&gt;"+
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
"ma-instance</xmlns:xsi></addresses>";
String expectedFinalStr = "<addresses>&gt;</addresses>";
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
finalStr+"]", expectedFinalStr.equals(finalStr));
}
Expand All @@ -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 = "<addresses><address><name/><nocontent/>"+
String expectedFinalStr = "<addresses>"+
"1\n2\n3"+
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
"ma-instance</xmlns:xsi></addresses>";
"</addresses>";
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
finalStr+"]", expectedFinalStr.equals(finalStr));
}
Expand All @@ -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 = "<addresses><address><name/><nocontent/>"+
String expectedFinalStr = "<addresses>"+
"<something>1</something><something>2</something><something>3</something>"+
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
"ma-instance</xmlns:xsi></addresses>";
"</addresses>";
assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
finalStr+"]", expectedFinalStr.equals(finalStr));
}
Expand Down Expand Up @@ -555,7 +550,9 @@ public void shouldHandleIllegalJSONNodeNames()
*/
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>"));
}

/**
Expand Down Expand Up @@ -740,10 +737,10 @@ public void contentOperations() {
@Test
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 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);
}

/**
Expand All @@ -765,17 +762,20 @@ public void testToJSONArray_reversibility() {
@Test
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 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 = "<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>"));
}

/**
Expand Down Expand Up @@ -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);
}
}

/**
Expand All @@ -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);
}
*/
}
}

0 comments on commit 70fcc78

Please sign in to comment.