RESOLVED - issue SWF-343: MapAccessor does not correctly handle null values

http://opensource.atlassian.com/projects/spring/browse/SWF-343
This commit is contained in:
Keith Donald
2007-08-15 15:36:06 +00:00
parent 2fbd74fb61
commit f8265e4fbe
3 changed files with 64 additions and 4 deletions

View File

@@ -48,6 +48,15 @@ public class MapAccessor implements MapAdaptable {
return map;
}
/**
* Returns a value in the map, returning null if the attribute is not present.
* @param key the key
* @return the value
*/
public Object get(Object key) {
return map.get(key);
}
/**
* Returns a value in the map, returning the defaultValue if no value was found.
* @param key the key
@@ -390,7 +399,7 @@ public class MapAccessor implements MapAdaptable {
}
/**
* Assert that value of the mak key is of the required type.
* Assert that value of the map key, if non-null, is of the required type.
* @param key the attribute name
* @param requiredType the required attribute value type
* @return the attribute value
@@ -400,7 +409,7 @@ public class MapAccessor implements MapAdaptable {
}
/**
* Assert that the key value is an instance of the required type.
* Assert that the key value, if non null, is an instance of the required type.
* @param key the key
* @param value the value
* @param requiredType the required type
@@ -408,10 +417,10 @@ public class MapAccessor implements MapAdaptable {
*/
public Object assertKeyValueInstanceOf(Object key, Object value, Class requiredType) {
Assert.notNull(requiredType, "The required type to assert is required");
if (!requiredType.isInstance(value)) {
if (value != null && !requiredType.isInstance(value)) {
throw new IllegalArgumentException("Map key '" + key + "' has value [" + value
+ "] that is not of expected type [" + requiredType + "], instead it is of type ["
+ (value != null ? value.getClass().getName() : "null") + "]");
+ value.getClass().getName() + "]");
}
return value;
}

View File

@@ -0,0 +1,48 @@
package org.springframework.binding.collection;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
public class MapAccessorTests extends TestCase {
private MapAccessor accessor;
protected void setUp() throws Exception {
Map map = new HashMap();
map.put("string", "hello");
map.put("integer", new Integer(9));
map.put("null", null);
this.accessor = new MapAccessor(map);
}
public void testAccessNullAttribute() {
assertEquals(null, accessor.get("null"));
assertEquals(null, accessor.get("null", "something else"));
assertEquals(null, accessor.getRequired("null"));
assertEquals(null, accessor.getString("null"));
assertEquals(null, accessor.getRequiredString("null"));
assertEquals(null, accessor.getInteger("null"));
assertEquals(null, accessor.getRequiredInteger("null"));
assertEquals(null, accessor.getCollection("null"));
assertEquals(null, accessor.getRequiredCollection("null"));
}
public void testGetString() {
assertEquals("hello", accessor.getString("string"));
assertEquals("hello", accessor.getRequiredString("string"));
}
public void testGetInteger() {
assertEquals(new Integer(9), accessor.getInteger("integer"));
assertEquals(new Integer(9), accessor.getRequiredInteger("integer"));
}
public void testGetRequiredMissingKey() {
try {
accessor.getRequired("bogus");
} catch (IllegalArgumentException e) {
}
}
}

View File

@@ -5,6 +5,9 @@ http://www.springframework.org/webflow
Changes in version 1.0.5 (21.08.2007)
-------------------------------------
Package org.springframework.webflow.binding
* Fixed bug in MapAccessor where accessing a map entry with a null value would result in an exception (SWF-343).
Package org.springframework.webflow.engine
* Ensured the RequestContext attribute map can never be null (SWF-347).