Ensuring parameters in the parameter map will be String arrays

WW-1960


git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@579205 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald J. Brown
2007-09-25 11:23:05 +00:00
parent 0be14bb564
commit a3e6c3c124
2 changed files with 94 additions and 14 deletions
@@ -22,7 +22,9 @@ package org.apache.struts2.components;
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 @@ import com.opensymphony.xwork2.util.ValueStackFactory;
* <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 @@ public class ActionComponent extends ContextBean {
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);
@@ -207,6 +199,40 @@ public class ActionComponent extends ContextBean {
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() {
return proxy;
}
@@ -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();
}
}