WW-3603 Prevents creating new map entries

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1532974 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2013-10-17 06:11:32 +00:00
parent 488bfc0e1d
commit ed19d82efa
2 changed files with 55 additions and 1 deletions
@@ -95,7 +95,7 @@ public class XWorkMapPropertyAccessor extends MapPropertyAccessor {
result = map.get(key);
if (result == null &&
context.get(ReflectionContextState.CREATE_NULL_OBJECTS) != null
Boolean.TRUE.equals(context.get(ReflectionContextState.CREATE_NULL_OBJECTS))
&& objectTypeDeterminer.shouldCreateIfNew(lastClass,lastProperty,target,null,false)) {
Class valueClass = objectTypeDeterminer.getElementClass(lastClass, lastProperty, key);
@@ -0,0 +1,54 @@
package com.opensymphony.xwork2.ognl.accessor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.XWorkTestCase;
import com.opensymphony.xwork2.util.Element;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
import java.util.Collections;
import java.util.Map;
public class XWorkMapPropertyAccessorTest extends XWorkTestCase {
public void testCreateNullObjectsIsFalseByDefault() {
ValueStack vs = ActionContext.getContext().getValueStack();
vs.push(new MapHolder(Collections.emptyMap()));
assertNull(vs.findValue("map[key]"));
}
public void testMapContentsAreReturned() {
ValueStack vs = ActionContext.getContext().getValueStack();
vs.push(new MapHolder(Collections.singletonMap("key", "value")));
assertEquals("value", vs.findValue("map['key']"));
}
public void testNullIsNotReturnedWhenCreateNullObjectsIsSpecified() {
ValueStack vs = ActionContext.getContext().getValueStack();
vs.push(new MapHolder(Collections.emptyMap()));
ReflectionContextState.setCreatingNullObjects(vs.getContext(), true);
Object value = vs.findValue("map['key']");
assertNotNull(value);
assertSame(Object.class, value.getClass());
}
public void testNullIsReturnedWhenCreateNullObjectsIsSpecifiedAsFalse() {
ValueStack vs = ActionContext.getContext().getValueStack();
vs.push(new MapHolder(Collections.emptyMap()));
ReflectionContextState.setCreatingNullObjects(vs.getContext(), false);
assertNull(vs.findValue("map['key']"));
}
private static class MapHolder {
private final Map map;
public MapHolder(Map m) {
this.map = m;
}
@Element(value = Object.class)
public Map getMap() {
return map;
}
}
}