Pass test case for empty tag with forceList

This commit is contained in:
Zach 2021-11-19 21:05:05 -06:00
parent 3f9b53fee4
commit e638955034
2 changed files with 34 additions and 15 deletions

View File

@ -415,7 +415,7 @@ public class XML {
if (parse(x, jsonObject, tagName, config)) { if (parse(x, jsonObject, tagName, config)) {
if (config.getForceList().contains(tagName)) { if (config.getForceList().contains(tagName)) {
if (jsonObject.length() == 0) { if (jsonObject.length() == 0) {
context.append(tagName, ""); context.put(tagName, new JSONArray());
} else if (jsonObject.length() == 1 } else if (jsonObject.length() == 1
&& jsonObject.opt(config.getcDataTagName()) != null) { && jsonObject.opt(config.getcDataTagName()) != null) {
context.append(tagName, jsonObject.opt(config.getcDataTagName())); context.append(tagName, jsonObject.opt(config.getcDataTagName()));

View File

@ -919,7 +919,8 @@ public class XMLConfigurationTest {
" </address>\n"+ " </address>\n"+
"</addresses>"; "</addresses>";
String expectedStr = "{\"addresses\":[{\"address\":{\"name\":\"Sherlock Holmes\"}}]}"; String expectedStr =
"{\"addresses\":[{\"address\":{\"name\":\"Sherlock Holmes\"}}]}";
Set<String> forceList = new HashSet<String>(); Set<String> forceList = new HashSet<String>();
forceList.add("addresses"); forceList.add("addresses");
@ -949,18 +950,18 @@ public class XMLConfigurationTest {
"</servers>"; "</servers>";
String expectedStr = String expectedStr =
"{"+ "{"+
"\"servers\": ["+ "\"servers\": ["+
"{"+ "{"+
"\"server\": {"+ "\"server\": {"+
"\"name\": \"host1\","+ "\"name\": \"host1\","+
"\"os\": \"Linux\","+ "\"os\": \"Linux\","+
"\"interfaces\": ["+ "\"interfaces\": ["+
"{"+ "{"+
"\"interface\": {"+ "\"interface\": {"+
"\"name\": \"em0\","+ "\"name\": \"em0\","+
"\"ip_address\": \"10.0.0.1\""+ "\"ip_address\": \"10.0.0.1\""+
"}}]}}]}"; "}}]}}]}";
Set<String> forceList = new HashSet<String>(); Set<String> forceList = new HashSet<String>();
forceList.add("servers"); forceList.add("servers");
@ -974,7 +975,25 @@ public class XMLConfigurationTest {
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject); Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
} }
@Test
public void testEmptyForceList() {
String xmlStr =
"<addresses></addresses>";
String expectedStr =
"{\"addresses\":[]}";
Set<String> forceList = new HashSet<String>();
forceList.add("addresses");
XMLParserConfiguration config =
new XMLParserConfiguration()
.withForceList(forceList);
JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
JSONObject expetedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
}
/** /**
* Convenience method, given an input string and expected result, * Convenience method, given an input string and expected result,