@@ -1052,4 +1052,61 @@ public void testValidXMLToJSONObjectStillWorks() {
10521052 }
10531053 }
10541054
1055+ /**
1056+ * Tests that the childNodes attribute is reserved in object-form parsing.
1057+ */
1058+ @ Test
1059+ public void testToJSONObjectRejectsReservedChildNodesAttribute () {
1060+ String xmlStr = "<p childNodes=\" metadata\" >text</p>" ;
1061+ for (boolean keepStrings : new boolean [] {false , true }) {
1062+ JSONException exception = assertThrows (JSONException .class ,
1063+ () -> JSONML .toJSONObject (xmlStr , keepStrings ));
1064+ assertTrue (exception .getMessage ().startsWith ("Reserved attribute." ));
1065+ }
1066+ }
1067+
1068+ /**
1069+ * Tests that the ordinary childNode attribute is preserved in object-form parsing.
1070+ */
1071+ @ Test
1072+ public void testToJSONObjectPreservesChildNodeAttribute () {
1073+ String xmlStr = "<p childNode=\" metadata\" >text</p>" ;
1074+ for (boolean keepStrings : new boolean [] {false , true }) {
1075+ JSONObject jsonObject = JSONML .toJSONObject (xmlStr , keepStrings );
1076+ assertEquals ("p" , jsonObject .getString ("tagName" ));
1077+ assertEquals ("metadata" , jsonObject .getString ("childNode" ));
1078+ assertEquals ("text" , jsonObject .getJSONArray ("childNodes" ).getString (0 ));
1079+ }
1080+ }
1081+
1082+ /**
1083+ * Tests that the tagName attribute remains reserved in object-form parsing.
1084+ */
1085+ @ Test
1086+ public void testToJSONObjectRejectsReservedTagNameAttribute () {
1087+ String xmlStr = "<p tagName=\" metadata\" >text</p>" ;
1088+ for (boolean keepStrings : new boolean [] {false , true }) {
1089+ JSONException exception = assertThrows (JSONException .class ,
1090+ () -> JSONML .toJSONObject (xmlStr , keepStrings ));
1091+ assertTrue (exception .getMessage ().startsWith ("Reserved attribute." ));
1092+ }
1093+ }
1094+
1095+ /**
1096+ * Tests that childNode and childNodes attributes are preserved in array-form parsing.
1097+ */
1098+ @ Test
1099+ public void testToJSONArrayPreservesChildNodeAttributes () {
1100+ String xmlStr = "<p childNode=\" singular\" childNodes=\" plural\" >text</p>" ;
1101+ for (boolean keepStrings : new boolean [] {false , true }) {
1102+ JSONArray jsonArray = JSONML .toJSONArray (xmlStr , keepStrings );
1103+ assertEquals (3 , jsonArray .length ());
1104+ assertEquals ("p" , jsonArray .getString (0 ));
1105+ JSONObject attributes = jsonArray .getJSONObject (1 );
1106+ assertEquals ("singular" , attributes .getString ("childNode" ));
1107+ assertEquals ("plural" , attributes .getString ("childNodes" ));
1108+ assertEquals ("text" , jsonArray .getString (2 ));
1109+ }
1110+ }
1111+
10551112}
0 commit comments