From f8265e4fbe0c43d7de0a42b42c13b266a6002d6a Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Wed, 15 Aug 2007 15:36:06 +0000 Subject: [PATCH] RESOLVED - issue SWF-343: MapAccessor does not correctly handle null values http://opensource.atlassian.com/projects/spring/browse/SWF-343 --- .../binding/collection/MapAccessor.java | 17 +++++-- .../binding/collection/MapAccessorTests.java | 48 +++++++++++++++++++ spring-webflow/changelog.txt | 3 ++ 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 spring-binding/src/test/java/org/springframework/binding/collection/MapAccessorTests.java diff --git a/spring-binding/src/main/java/org/springframework/binding/collection/MapAccessor.java b/spring-binding/src/main/java/org/springframework/binding/collection/MapAccessor.java index 419712c3..b13092e1 100644 --- a/spring-binding/src/main/java/org/springframework/binding/collection/MapAccessor.java +++ b/spring-binding/src/main/java/org/springframework/binding/collection/MapAccessor.java @@ -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; } diff --git a/spring-binding/src/test/java/org/springframework/binding/collection/MapAccessorTests.java b/spring-binding/src/test/java/org/springframework/binding/collection/MapAccessorTests.java new file mode 100644 index 00000000..8d3a1df1 --- /dev/null +++ b/spring-binding/src/test/java/org/springframework/binding/collection/MapAccessorTests.java @@ -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) { + } + } + +} diff --git a/spring-webflow/changelog.txt b/spring-webflow/changelog.txt index 5a402c63..dd4e75b9 100644 --- a/spring-webflow/changelog.txt +++ b/spring-webflow/changelog.txt @@ -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).