Added shallow copy for config map

This commit is contained in:
Rahul Kumar 2020-09-06 11:17:10 +05:30
parent ed9658d5cb
commit 56d4130a86
2 changed files with 38 additions and 1 deletions

View File

@ -24,6 +24,7 @@ SOFTWARE.
*/
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -278,7 +279,8 @@ public class XMLParserConfiguration {
*/
public XMLParserConfiguration withXsiTypeMap(final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap) {
XMLParserConfiguration newConfig = this.clone();
newConfig.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
Map<String, XMLXsiTypeConverter<?>> cloneXsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>(xsiTypeMap);
newConfig.xsiTypeMap = Collections.unmodifiableMap(cloneXsiTypeMap);
return newConfig;
}
}

View File

@ -1033,4 +1033,39 @@ public class XMLTest {
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
}
@Test
public void testToJsonWithXSITypeWhenTypeConversionNotEnabledOnOne() {
String originalXml = "<root><asString xsi:type=\"string\">12345</asString><asInt>54321</asInt></root>";
String expectedJsonString = "{\"root\":{\"asString\":\"12345\",\"asInt\":54321}}";
JSONObject expectedJson = new JSONObject(expectedJsonString);
Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
@Override public String convert(final String value) {
return value;
}
});
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap));
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
}
@Test
public void testXSITypeMapNotModifiable() {
Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
XMLParserConfiguration config = new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap);
xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
@Override public String convert(final String value) {
return value;
}
});
assertEquals("Config Conversion Map size is expected to be 0", 0, config.getXsiTypeMap().size());
try {
config.getXsiTypeMap().put("boolean", new XMLXsiTypeConverter<Boolean>() {
@Override public Boolean convert(final String value) {
return Boolean.valueOf(value);
}
});
fail("Expected to be unable to modify the config");
} catch (Exception ignored) { }
}
}