Skip to content

Commit

Permalink
Merge pull request #52 from johnjaylward/OptionalTypeConversion
Browse files Browse the repository at this point in the history
updates Test cases to support new JSONML and XML conversion options
  • Loading branch information
stleary authored Aug 1, 2016
2 parents 5d8ea6f + 215321c commit 62524b5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/test/java/org/json/junit/JSONMLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -698,4 +698,41 @@ public void commentsInXML() {
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
}

/**
* JSON string with lost leading zero and converted "True" to true. See test
* result in comment below.
*/
@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\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]";
final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false);
assertEquals(expectedJsonString, actualJsonOutput.toString());
}

/**
* JSON string cannot be reverted to original xml. See test result in
* comment below.
*/
@Test
public void testToJSONArray_reversibility() {
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 revertedXml = JSONML.toString(JSONML.toJSONArray(originalXml, false));
assertNotEquals(revertedXml, originalXml);
}

/**
* test passes when using the new method toJsonML.
*/
@Test
public void testToJsonML() {
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\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]";
final JSONArray json = JSONML.toJSONArray(originalXml,true);
assertEquals(expectedJsonString, json.toString());

final String reverseXml = JSONML.toString(json);
assertEquals(originalXml, reverseXml);
}

}
44 changes: 44 additions & 0 deletions src/test/java/org/json/junit/XMLTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.json.junit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONML;
import org.json.JSONObject;
import org.json.XML;
import org.junit.Rule;
Expand Down Expand Up @@ -723,4 +725,46 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) {
}
*/
}

/**
* JSON string lost leading zero and converted "True" to true.
*/
@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 actualJsonOutput = XML.toJSONObject(originalXml, false);

assertEquals(expectedJsonString, actualJsonOutput.toString());
}

/**
* JSON string cannot be reverted to original xml.
*/
@Test
public void testToJSONArray_reversibility() {
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 revertedXml = XML.toString(XML.toJSONObject(originalXml, false));

assertNotEquals(revertedXml, originalXml);
}

/**
* test passes when using the new method toJsonArray.
*/
@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 json = XML.toJSONObject(originalXml,true);
assertEquals(expectedJsonString, json.toString());

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);
}

}

0 comments on commit 62524b5

Please sign in to comment.