mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
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:
+1
-1
@@ -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);
|
||||
|
||||
|
||||
+54
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user