Commits

unkyaku  committed 658e479

Support type conversion.

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@296e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 11811c5

Comments (0)

Files changed (3)

File src/java/com/opensymphony/xwork/util/XWorkMap.java

  */
 package com.opensymphony.xwork.util;
 
+import com.opensymphony.xwork.ActionContext;
 import com.opensymphony.xwork.ObjectFactory;
 
 import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
 
 
 /**
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
+    /**
+     * Returns the value to which the specified key is mapped in this identity hash map.  If there
+     * is no mapping for this key, create an appropriate object for this key, put it in the map, and
+     * return the new object.  Use {@link #containsKey(Object)} to check if there really is a
+     * mapping for a key or not.
+     *
+     * @param key the key whose associated value is to be returned.
+     * @return the value to which this map maps the specified key
+     */
     public Object get(Object key) {
         Object o = super.get(key);
 
 
         return o;
     }
+
+    /**
+     * Associates the specified value with the specified key in this map. If the map previously
+     * contained a mapping for this key, the old value is replaced.
+     *
+     * @param key   key with which the specified value is to be associated.
+     * @param value value to be associated with the specified key.
+     * @return previous value associated with specified key, or <tt>null</tt> if there was no
+     *         mapping for key.  A <tt>null</tt> return can also indicate that the HashMap
+     *         previously associated <tt>null</tt> with the specified key.
+     */
+    public Object put(Object key, Object value) {
+        if ((value != null) && !clazz.isAssignableFrom(value.getClass())) {
+            // convert to correct type
+            Map context = ActionContext.getContext().getContextMap();
+            value = XWorkConverter.getInstance().convertValue(context, null, null, null, value, clazz);
+        }
+
+        return super.put(key, value);
+    }
+
+    /**
+     * Copies all of the mappings from the specified map to this map These mappings will replace any
+     * mappings that this map had for any of the keys currently in the specified map.
+     *
+     * @param m mappings to be stored in this map.
+     * @throws NullPointerException if the specified map is null.
+     */
+    public void putAll(Map m) {
+        for (Iterator i = m.entrySet().iterator(); i.hasNext();) {
+            Map.Entry e = (Map.Entry) i.next();
+            put(e.getKey(), e.getValue());
+        }
+    }
 }

File src/test/com/opensymphony/xwork/test/User-conversion.properties

+Collection_list = java.lang.String
+Collection_map = java.lang.String

File src/test/com/opensymphony/xwork/util/XWorkMapTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.util;
+
+import com.opensymphony.xwork.test.User;
+
+import junit.framework.TestCase;
+
+import java.util.Map;
+
+
+/**
+ * @author Mark Woon
+ */
+public class XWorkMapTest extends TestCase {
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void testPutValue() {
+        OgnlValueStack stack = new OgnlValueStack();
+        Map stackContext = stack.getContext();
+        stackContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
+        stackContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.TRUE);
+        stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
+
+        User user = new User();
+        stack.push(user);
+
+        // indexed string
+        stack.setValue("map['foo']", "asdf");
+        assertNotNull(user.getMap());
+        assertEquals(1, user.getMap().size());
+        assertEquals(String.class, user.getMap().get("foo").getClass());
+        assertEquals("asdf", user.getMap().get("foo"));
+
+        // type conversion
+        stack.setValue("map['bar']", new String[] {"qwerty"});
+        assertNotNull(user.getMap());
+        assertEquals(2, user.getMap().size());
+        assertEquals(String.class, user.getMap().get("bar").getClass());
+        assertEquals("qwerty", user.getMap().get("bar"));
+    }
+
+    public void testTypeConversion() {
+        XWorkMap map = new XWorkMap(String.class);
+        String[] test = new String[] {"qwerty"};
+        map.put("foo", test);
+        assertEquals(1, map.size());
+        assertEquals("qwerty", map.get("foo"));
+    }
+}