@@ -35,6 +35,8 @@ of this software and associated documentation files (the "Software"), to deal
35
35
import java .io .IOException ;
36
36
import java .io .Reader ;
37
37
import java .io .StringReader ;
38
+ import java .util .HashSet ;
39
+ import java .util .Set ;
38
40
39
41
import org .json .JSONArray ;
40
42
import org .json .JSONException ;
@@ -903,7 +905,172 @@ public void testConfig() {
903
905
Util .compareActualVsExpectedJsonArrays (jsonArray , expectedJsonArray );
904
906
905
907
}
906
-
908
+
909
+ /**
910
+ * Test forceList parameter
911
+ */
912
+ @ Test
913
+ public void testSimpleForceList () {
914
+ String xmlStr =
915
+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
916
+ "<addresses>\n " +
917
+ " <address>\n " +
918
+ " <name>Sherlock Holmes</name>\n " +
919
+ " </address>\n " +
920
+ "</addresses>" ;
921
+
922
+ String expectedStr =
923
+ "{\" addresses\" :[{\" address\" :{\" name\" :\" Sherlock Holmes\" }}]}" ;
924
+
925
+ Set <String > forceList = new HashSet <String >();
926
+ forceList .add ("addresses" );
927
+
928
+ XMLParserConfiguration config =
929
+ new XMLParserConfiguration ()
930
+ .withForceList (forceList );
931
+ JSONObject jsonObject = XML .toJSONObject (xmlStr , config );
932
+ JSONObject expetedJsonObject = new JSONObject (expectedStr );
933
+
934
+ Util .compareActualVsExpectedJsonObjects (jsonObject , expetedJsonObject );
935
+ }
936
+ @ Test
937
+ public void testLongForceList () {
938
+ String xmlStr =
939
+ "<servers>" +
940
+ "<server>" +
941
+ "<name>host1</name>" +
942
+ "<os>Linux</os>" +
943
+ "<interfaces>" +
944
+ "<interface>" +
945
+ "<name>em0</name>" +
946
+ "<ip_address>10.0.0.1</ip_address>" +
947
+ "</interface>" +
948
+ "</interfaces>" +
949
+ "</server>" +
950
+ "</servers>" ;
951
+
952
+ String expectedStr =
953
+ "{" +
954
+ "\" servers\" : [" +
955
+ "{" +
956
+ "\" server\" : {" +
957
+ "\" name\" : \" host1\" ," +
958
+ "\" os\" : \" Linux\" ," +
959
+ "\" interfaces\" : [" +
960
+ "{" +
961
+ "\" interface\" : {" +
962
+ "\" name\" : \" em0\" ," +
963
+ "\" ip_address\" : \" 10.0.0.1\" " +
964
+ "}}]}}]}" ;
965
+
966
+ Set <String > forceList = new HashSet <String >();
967
+ forceList .add ("servers" );
968
+ forceList .add ("interfaces" );
969
+
970
+ XMLParserConfiguration config =
971
+ new XMLParserConfiguration ()
972
+ .withForceList (forceList );
973
+ JSONObject jsonObject = XML .toJSONObject (xmlStr , config );
974
+ JSONObject expetedJsonObject = new JSONObject (expectedStr );
975
+
976
+ Util .compareActualVsExpectedJsonObjects (jsonObject , expetedJsonObject );
977
+ }
978
+ @ Test
979
+ public void testMultipleTagForceList () {
980
+ String xmlStr =
981
+ "<addresses>\n " +
982
+ " <address>\n " +
983
+ " <name>Sherlock Holmes</name>\n " +
984
+ " <name>John H. Watson</name>\n " +
985
+ " </address>\n " +
986
+ "</addresses>" ;
987
+
988
+ String expectedStr =
989
+ "{" +
990
+ "\" addresses\" :[" +
991
+ "{" +
992
+ "\" address\" :[" +
993
+ "{" +
994
+ "\" name\" :[" +
995
+ "\" Sherlock Holmes\" ," +
996
+ "\" John H. Watson\" " +
997
+ "]" +
998
+ "}" +
999
+ "]" +
1000
+ "}" +
1001
+ "]" +
1002
+ "}" ;
1003
+
1004
+ Set <String > forceList = new HashSet <String >();
1005
+ forceList .add ("addresses" );
1006
+ forceList .add ("address" );
1007
+ forceList .add ("name" );
1008
+
1009
+ XMLParserConfiguration config =
1010
+ new XMLParserConfiguration ()
1011
+ .withForceList (forceList );
1012
+ JSONObject jsonObject = XML .toJSONObject (xmlStr , config );
1013
+ JSONObject expetedJsonObject = new JSONObject (expectedStr );
1014
+
1015
+ Util .compareActualVsExpectedJsonObjects (jsonObject , expetedJsonObject );
1016
+ }
1017
+ @ Test
1018
+ public void testEmptyForceList () {
1019
+ String xmlStr =
1020
+ "<addresses></addresses>" ;
1021
+
1022
+ String expectedStr =
1023
+ "{\" addresses\" :[]}" ;
1024
+
1025
+ Set <String > forceList = new HashSet <String >();
1026
+ forceList .add ("addresses" );
1027
+
1028
+ XMLParserConfiguration config =
1029
+ new XMLParserConfiguration ()
1030
+ .withForceList (forceList );
1031
+ JSONObject jsonObject = XML .toJSONObject (xmlStr , config );
1032
+ JSONObject expetedJsonObject = new JSONObject (expectedStr );
1033
+
1034
+ Util .compareActualVsExpectedJsonObjects (jsonObject , expetedJsonObject );
1035
+ }
1036
+ @ Test
1037
+ public void testContentForceList () {
1038
+ String xmlStr =
1039
+ "<addresses>Baker Street</addresses>" ;
1040
+
1041
+ String expectedStr =
1042
+ "{\" addresses\" :[\" Baker Street\" ]}" ;
1043
+
1044
+ Set <String > forceList = new HashSet <String >();
1045
+ forceList .add ("addresses" );
1046
+
1047
+ XMLParserConfiguration config =
1048
+ new XMLParserConfiguration ()
1049
+ .withForceList (forceList );
1050
+ JSONObject jsonObject = XML .toJSONObject (xmlStr , config );
1051
+ JSONObject expetedJsonObject = new JSONObject (expectedStr );
1052
+
1053
+ Util .compareActualVsExpectedJsonObjects (jsonObject , expetedJsonObject );
1054
+ }
1055
+ @ Test
1056
+ public void testEmptyTagForceList () {
1057
+ String xmlStr =
1058
+ "<addresses />" ;
1059
+
1060
+ String expectedStr =
1061
+ "{\" addresses\" :[]}" ;
1062
+
1063
+ Set <String > forceList = new HashSet <String >();
1064
+ forceList .add ("addresses" );
1065
+
1066
+ XMLParserConfiguration config =
1067
+ new XMLParserConfiguration ()
1068
+ .withForceList (forceList );
1069
+ JSONObject jsonObject = XML .toJSONObject (xmlStr , config );
1070
+ JSONObject expetedJsonObject = new JSONObject (expectedStr );
1071
+
1072
+ Util .compareActualVsExpectedJsonObjects (jsonObject , expetedJsonObject );
1073
+ }
907
1074
908
1075
/**
909
1076
* Convenience method, given an input string and expected result,
0 commit comments