You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by mr...@apache.org on 2007/09/25 13:23:10 UTC

svn commit: r579205 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/components/ActionComponent.java test/java/org/apache/struts2/components/ActionComponentTest.java

Author: mrdon
Date: Tue Sep 25 04:23:05 2007
New Revision: 579205

URL: http://svn.apache.org/viewvc?rev=579205&view=rev
Log:
Ensuring parameters in the parameter map will be String arrays
WW-1960

Added:
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/ActionComponentTest.java
Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionComponent.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionComponent.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionComponent.java?rev=579205&r1=579204&r2=579205&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionComponent.java (original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionComponent.java Tue Sep 25 04:23:05 2007
@@ -22,7 +22,9 @@
 
 import java.io.IOException;
 import java.io.Writer;
+import java.lang.reflect.Array;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 
 import javax.servlet.ServletContext;
@@ -59,8 +61,8 @@
  *      <li>id (String) - the id (if specified) to put the action under stack's context.
  *      <li>name* (String) - name of the action to be executed (without the extension suffix eg. .action)</li>
  *      <li>namespace (String) - default to the namespace where this action tag is invoked</li>
- *      <li>executeResult (Boolean) -  default is false. Decides wheather the result of this action is to be executed or not</li>
- *      <li>ignoreContextParams (Boolean) - default to false. Decides wheather the request parameters are to be included when the action is invoked</li>
+ *      <li>executeResult (Boolean) -  default is false. Decides whether the result of this action is to be executed or not</li>
+ *      <li>ignoreContextParams (Boolean) - default to false. Decides whether the request parameters are to be included when the action is invoked</li>
  * </ul>
  * <!-- END SNIPPET: params -->
  *
@@ -170,18 +172,8 @@
         return end;
     }
 
-    private Map createExtraContext() {
-        Map parentParams = null;
-
-        if (!ignoreContextParams) {
-            parentParams = new ActionContext(getStack().getContext()).getParameters();
-        }
-
-        Map newParams = (parentParams != null) ? new HashMap(parentParams) : new HashMap();
-
-        if (parameters != null) {
-            newParams.putAll(parameters);
-        }
+    protected Map createExtraContext() {
+        Map newParams = createParametersForContext();
 
         ActionContext ctx = new ActionContext(stack.getContext());
         ServletContext servletContext = (ServletContext) ctx.get(ServletActionContext.SERVLET_CONTEXT);
@@ -205,6 +197,40 @@
         extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
 
         return extraContext;
+    }
+
+    /**
+     * Creates parameters map using parameters from the value stack and component parameters.  Any non-String array
+     * values will be converted into a single-value String array.
+     * 
+     * @return A map of String[] parameters
+     */
+    protected Map<String,String[]> createParametersForContext() {
+        Map parentParams = null;
+
+        if (!ignoreContextParams) {
+            parentParams = new ActionContext(getStack().getContext()).getParameters();
+        }
+
+        Map<String,String[]> newParams = (parentParams != null) 
+            ? new HashMap<String,String[]>(parentParams) 
+            : new HashMap<String,String[]>();
+
+        if (parameters != null) {
+            Map<String,String[]> params = new HashMap<String,String[]>();
+            for (Iterator i = parameters.entrySet().iterator(); i.hasNext(); ) {
+                Map.Entry entry = (Map.Entry) i.next();
+                String key = (String) entry.getKey();
+                Object val = entry.getValue();
+                if (val.getClass().isArray() && String.class == val.getClass().getComponentType()) {
+                    params.put(key, (String[])val);
+                } else {
+                    params.put(key, new String[]{val.toString()});
+                }
+            }
+            newParams.putAll(params);
+        }
+        return newParams;
     }
 
     public ActionProxy getProxy() {

Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/ActionComponentTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/ActionComponentTest.java?rev=579205&view=auto
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/ActionComponentTest.java (added)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/ActionComponentTest.java Tue Sep 25 04:23:05 2007
@@ -0,0 +1,54 @@
+/*
+ * $Id: ComponentTest.java 471756 2006-11-06 15:01:43Z husted $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.components;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.struts2.StrutsTestCase;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+import com.mockobjects.dynamic.Mock;
+import com.opensymphony.xwork2.util.ValueStack;
+
+public class ActionComponentTest extends StrutsTestCase {
+
+    public void testCreateParametersForContext() throws Exception {
+        MockHttpServletRequest req = new MockHttpServletRequest();
+        MockHttpServletResponse res = new MockHttpServletResponse();
+        Mock mockValueStack = new Mock(ValueStack.class);
+        HashMap ctx = new HashMap();
+        mockValueStack.expectAndReturn("getContext", ctx);
+        mockValueStack.expectAndReturn("getContext", ctx);
+        mockValueStack.expectAndReturn("getContext", ctx);
+        
+        ActionComponent comp = new ActionComponent((ValueStack) mockValueStack.proxy(), req, res);
+        comp.addParameter("foo", "bar");
+        comp.addParameter("baz", new String[]{"jim", "sarah"});
+        Map params = comp.createParametersForContext();
+        assertNotNull(params);
+        assertEquals(2, params.size());
+        assertEquals("bar", ((String[])params.get("foo"))[0]);
+        assertEquals(2, ((String[])params.get("baz")).length);
+        mockValueStack.verify();
+    }
+}