Skip to content

Commit 62524b5

Browse files
authored
Merge pull request #52 from johnjaylward/OptionalTypeConversion
updates Test cases to support new JSONML and XML conversion options
2 parents 5d8ea6f + 215321c commit 62524b5

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/test/java/org/json/junit/JSONMLTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,4 +698,41 @@ public void commentsInXML() {
698698
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
699699
}
700700

701+
/**
702+
* JSON string with lost leading zero and converted "True" to true. See test
703+
* result in comment below.
704+
*/
705+
@Test
706+
public void testToJSONArray_jsonOutput() {
707+
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
708+
final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]";
709+
final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false);
710+
assertEquals(expectedJsonString, actualJsonOutput.toString());
711+
}
712+
713+
/**
714+
* JSON string cannot be reverted to original xml. See test result in
715+
* comment below.
716+
*/
717+
@Test
718+
public void testToJSONArray_reversibility() {
719+
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
720+
final String revertedXml = JSONML.toString(JSONML.toJSONArray(originalXml, false));
721+
assertNotEquals(revertedXml, originalXml);
722+
}
723+
724+
/**
725+
* test passes when using the new method toJsonML.
726+
*/
727+
@Test
728+
public void testToJsonML() {
729+
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
730+
final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]";
731+
final JSONArray json = JSONML.toJSONArray(originalXml,true);
732+
assertEquals(expectedJsonString, json.toString());
733+
734+
final String reverseXml = JSONML.toString(json);
735+
assertEquals(originalXml, reverseXml);
736+
}
737+
701738
}

src/test/java/org/json/junit/XMLTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.json.junit;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotEquals;
45
import static org.junit.Assert.assertTrue;
56

67
import java.io.IOException;
78

89
import org.json.JSONArray;
910
import org.json.JSONException;
11+
import org.json.JSONML;
1012
import org.json.JSONObject;
1113
import org.json.XML;
1214
import org.junit.Rule;
@@ -723,4 +725,46 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) {
723725
}
724726
*/
725727
}
728+
729+
/**
730+
* JSON string lost leading zero and converted "True" to true.
731+
*/
732+
@Test
733+
public void testToJSONArray_jsonOutput() {
734+
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
735+
final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}";
736+
final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false);
737+
738+
assertEquals(expectedJsonString, actualJsonOutput.toString());
739+
}
740+
741+
/**
742+
* JSON string cannot be reverted to original xml.
743+
*/
744+
@Test
745+
public void testToJSONArray_reversibility() {
746+
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
747+
final String revertedXml = XML.toString(XML.toJSONObject(originalXml, false));
748+
749+
assertNotEquals(revertedXml, originalXml);
750+
}
751+
752+
/**
753+
* test passes when using the new method toJsonArray.
754+
*/
755+
@Test
756+
public void testToJsonXML() {
757+
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
758+
final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}";
759+
760+
final JSONObject json = XML.toJSONObject(originalXml,true);
761+
assertEquals(expectedJsonString, json.toString());
762+
763+
final String reverseXml = XML.toString(json);
764+
// this reversal isn't exactly the same. use JSONML for an exact reversal
765+
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>";
766+
767+
assertEquals(expectedReverseXml, reverseXml);
768+
}
769+
726770
}

0 commit comments

Comments
 (0)