You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by ni...@apache.org on 2008/06/24 14:33:59 UTC

svn commit: r671156 - in /struts/struts2/trunk/plugins/portlet/src: main/java/org/apache/struts2/portlet/ main/java/org/apache/struts2/portlet/context/ main/java/org/apache/struts2/portlet/dispatcher/ main/java/org/apache/struts2/portlet/servlet/ test/...

Author: nilsga
Date: Tue Jun 24 05:33:59 2008
New Revision: 671156

URL: http://svn.apache.org/viewvc?rev=671156&view=rev
Log:
WW-2622,WW-2537 Remove hard coding of .action extension and added use of generics.

Added:
    struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/servlet/
    struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/servlet/PortletServletRequestTest.java
Modified:
    struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/PortletActionConstants.java
    struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/context/PortletActionContext.java
    struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java
    struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequest.java
    struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/dispatcher/Jsr168DispatcherTest.java

Modified: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/PortletActionConstants.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/PortletActionConstants.java?rev=671156&r1=671155&r2=671156&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/PortletActionConstants.java (original)
+++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/PortletActionConstants.java Tue Jun 24 05:33:59 2008
@@ -21,6 +21,7 @@
 
 package org.apache.struts2.portlet;
 
+import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.portlet.dispatcher.DispatcherServlet;
 
 /**
@@ -131,4 +132,9 @@
 	 * Default name of dispatcher servlet in web.xml
 	 */
 	String DEFAULT_DISPATCHER_SERVLET_NAME = "Struts2PortletDispatcherServlet";
+	
+	/**
+	 * Key for the action mapping in the context
+    */
+	String ACTION_MAPPING = ServletActionContext.ACTION_MAPPING;
 }

Modified: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/context/PortletActionContext.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/context/PortletActionContext.java?rev=671156&r1=671155&r2=671156&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/context/PortletActionContext.java (original)
+++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/context/PortletActionContext.java Tue Jun 24 05:33:59 2008
@@ -204,5 +204,14 @@
     public static PortletContext getPortletContext() {
     	return (PortletContext)getContext().get(StrutsStatics.STRUTS_PORTLET_CONTEXT);
     }
+    
+    /**
+     * Gets the action mapping for this context
+     *
+     * @return The action mapping
+     */
+    public static ActionMapping getActionMapping() {
+        return (ActionMapping) getContext().get(ACTION_MAPPING);
+    }
 
 }

Modified: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java?rev=671156&r1=671155&r2=671156&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java (original)
+++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher.java Tue Jun 24 05:33:59 2008
@@ -180,6 +180,8 @@
     private Dispatcher dispatcherUtils;
     
     private ActionMapper actionMapper;
+    
+    private Container container;
 
     /**
      * Initialize the portlet with the init parameters from <tt>portlet.xml</tt>
@@ -226,7 +228,7 @@
         LocalizedTextUtil
                 .addDefaultResourceBundle("org/apache/struts2/struts-messages");
 
-        Container container = dispatcherUtils.getContainer();
+        container = dispatcherUtils.getContainer();
         //check for configuration reloading
         if ("true".equalsIgnoreCase(container.getInstance(String.class, StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD))) {
             FileManager.setReloadingConfigs(true);
@@ -351,13 +353,14 @@
      *        {@link PortletActionConstants})
      * @return a HashMap representing the <tt>Action</tt> context.
      */
-    public HashMap createContextMap(Map requestMap, Map parameterMap,
-            Map sessionMap, Map applicationMap, PortletRequest request,
+    public HashMap<String, Object> createContextMap(Map<String, Object> requestMap, Map<String, String[]> parameterMap,
+            Map<String, Object> sessionMap, Map<String, Object> applicationMap, PortletRequest request,
             PortletResponse response, PortletConfig portletConfig, Integer phase) throws IOException {
 
         // TODO Must put http request/response objects into map for use with
     	HttpServletResponse dummyResponse = new PortletServletResponse(response);
     	HttpServletRequest dummyRequest = new PortletServletRequest(request, getPortletContext());
+    	container.inject(dummyRequest);
     	ServletContext dummyServletContext = new PortletServletContext(getPortletContext());
     	if(EVENT_PHASE.equals(phase)) {
     		dummyRequest = dispatcherUtils.wrapRequest(dummyRequest, dummyServletContext);
@@ -368,7 +371,7 @@
     		}
     	}
         // ServletActionContext
-        HashMap<String,Object> extraContext = new HashMap<String,Object>();
+        HashMap<String, Object> extraContext = new HashMap<String, Object>();
         // The dummy servlet objects. Eases reuse of existing interceptors that uses the servlet objects.
         extraContext.put(StrutsStatics.HTTP_REQUEST, dummyRequest);
         extraContext.put(StrutsStatics.HTTP_RESPONSE, dummyResponse);
@@ -426,17 +429,18 @@
      *        {@link PortletActionConstants})
      */
     public void serviceAction(PortletRequest request, PortletResponse response,
-            ActionMapping mapping, Map requestMap, Map parameterMap,
-            Map sessionMap, Map applicationMap, String portletNamespace,
+            ActionMapping mapping, Map<String, Object> requestMap, Map<String, String[]> parameterMap,
+            Map<String, Object> sessionMap, Map<String, Object> applicationMap, String portletNamespace,
             Integer phase) throws PortletException {
         LOG.debug("serviceAction");
         String actionName = mapping.getName();
         String namespace = mapping.getNamespace();
         Dispatcher.setInstance(dispatcherUtils);
         try {
-            HashMap extraContext = createContextMap(requestMap, parameterMap,
+            HashMap<String, Object> extraContext = createContextMap(requestMap, parameterMap,
                     sessionMap, applicationMap, request, response,
                     getPortletConfig(), phase);
+            extraContext.put(PortletActionConstants.ACTION_MAPPING, mapping);
             LOG.debug("Creating action proxy for name = " + actionName
                     + ", namespace = " + namespace);
             ActionProxy proxy = factory.createActionProxy(namespace,
@@ -539,8 +543,8 @@
      * @throws IOException if an exception occurs while retrieving the parameter
      *         map.
      */
-    protected Map getParameterMap(PortletRequest request) throws IOException {
-        return new HashMap(request.getParameterMap());
+    protected Map<String, String[]> getParameterMap(PortletRequest request) throws IOException {
+        return new HashMap<String, String[]>(request.getParameterMap());
     }
 
     /**

Modified: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequest.java?rev=671156&r1=671155&r2=671156&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequest.java (original)
+++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequest.java Tue Jun 24 05:33:59 2008
@@ -42,31 +42,44 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 
+import org.apache.struts2.StrutsConstants;
+import org.apache.struts2.dispatcher.mapper.ActionMapping;
 import org.apache.struts2.portlet.PortletActionConstants;
+import org.apache.struts2.portlet.context.PortletActionContext;
+
+import com.opensymphony.xwork2.inject.Inject;
 
 /**
- * Wrapper object exposing a {@link PortletRequest} as a {@link HttpServletRequest} instance.
- * Clients accessing this request object will in fact operate on the
- * {@link PortletRequest} object wrapped by this request object.
+ * Wrapper object exposing a {@link PortletRequest} as a
+ * {@link HttpServletRequest} instance. Clients accessing this request object
+ * will in fact operate on the {@link PortletRequest} object wrapped by this
+ * request object.
  */
 public class PortletServletRequest implements HttpServletRequest, PortletActionConstants {
 
 	private PortletRequest portletRequest;
+
 	private PortletContext portletContext;
-	
+
+	private String extension;
+
 	public PortletServletRequest(PortletRequest portletRequest, PortletContext portletContext) {
 		this.portletRequest = portletRequest;
 		this.portletContext = portletContext;
 	}
-	
-	/* (non-Javadoc)
+
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getAuthType()
 	 */
 	public String getAuthType() {
 		return portletRequest.getAuthType();
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getContextPath()
 	 */
 	public String getContextPath() {
@@ -74,27 +87,32 @@
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public Cookie[] getCookies() {
-		if(portletRequest instanceof HttpServletRequest) {
-			return ((HttpServletRequest)portletRequest).getCookies();
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getCookies();
 		}
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public long getDateHeader(String name) {
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
-	 * Gets a property from the {@link PortletRequest}. Note that a {@link PortletRequest} is not 
-	 * guaranteed to map properties to headers.
+	 * Gets a property from the {@link PortletRequest}. Note that a
+	 * {@link PortletRequest} is not guaranteed to map properties to headers.
+	 * 
 	 * @see PortletRequest#getProperty(String)
 	 * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
 	 */
@@ -103,8 +121,9 @@
 	}
 
 	/**
-	 * Gets the property names from the {@link PortletRequest}. Note that a {@link PortletRequest} is not 
-	 * guaranteed to map properties to headers.
+	 * Gets the property names from the {@link PortletRequest}. Note that a
+	 * {@link PortletRequest} is not guaranteed to map properties to headers.
+	 * 
 	 * @see PortletRequest#getPropertyNames()
 	 * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
 	 */
@@ -113,8 +132,10 @@
 	}
 
 	/**
-	 * Gets the values for the specified property from the {@link PortletRequest}. Note that a 
-	 * {@link PortletRequest} is not guaranteed to map properties to headers.
+	 * Gets the values for the specified property from the
+	 * {@link PortletRequest}. Note that a {@link PortletRequest} is not
+	 * guaranteed to map properties to headers.
+	 * 
 	 * @see PortletRequest#getProperties(String)
 	 * @see HttpServletRequest#getHeaders(String)
 	 */
@@ -123,42 +144,54 @@
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public int getIntHeader(String name) {
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getMethod()
 	 */
 	public String getMethod() {
 		return null;
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getPathInfo()
 	 */
 	public String getPathInfo() {
 		return null;
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
 	 */
 	public String getPathTranslated() {
 		return null;
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getQueryString()
 	 */
 	public String getQueryString() {
 		return null;
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
 	 */
 	public String getRemoteUser() {
@@ -166,22 +199,28 @@
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public String getRequestURI() {
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public StringBuffer getRequestURL() {
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
 	 */
 	public String getRequestedSessionId() {
@@ -189,20 +228,28 @@
 	}
 
 	/**
-	 * A {@link PortletRequest} has no servlet path. But for compatibility with Struts 2 components and
-	 * interceptors, the action parameter on the request is mapped to the servlet path.
+	 * A {@link PortletRequest} has no servlet path. But for compatibility with
+	 * Struts 2 components and interceptors, the action parameter on the request
+	 * is mapped to the servlet path.
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getServletPath()
 	 */
 	public String getServletPath() {
 		String actionPath = getParameter(ACTION_PARAM);
-		if(actionPath != null && !actionPath.endsWith(".action")) {
-			actionPath += ".action";
+		if (!hasExtension(actionPath)) {
+			actionPath += "." + extension;
 		}
 		return actionPath;
 	}
 
+	private boolean hasExtension(String actionPath) {
+		return extension == null || "".equals(extension)
+				|| (actionPath != null && actionPath.endsWith("." + extension));
+	}
+
 	/**
 	 * Get the {@link PortletSession} as a {@link PortletHttpSession} instance.
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getSession()
 	 */
 	public HttpSession getSession() {
@@ -211,13 +258,16 @@
 
 	/**
 	 * Get the {@link PortletSession} as a {@link PortletHttpSession} instance.
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
 	 */
 	public HttpSession getSession(boolean create) {
 		return new PortletHttpSession(portletRequest.getPortletSession(create));
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
 	 */
 	public Principal getUserPrincipal() {
@@ -225,37 +275,47 @@
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public boolean isRequestedSessionIdFromCookie() {
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public boolean isRequestedSessionIdFromURL() {
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public boolean isRequestedSessionIdFromUrl() {
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
 	 */
 	public boolean isRequestedSessionIdValid() {
 		return portletRequest.isRequestedSessionIdValid();
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.http.HttpServletRequest#isUserInRole(java.lang.String)
 	 */
 	public boolean isUserInRole(String role) {
@@ -263,21 +323,23 @@
 	}
 
 	/**
-	 * Gets an attribute value on the {@link PortletRequest}. If the attribute name is 
-	 * <tt>javax.servlet.include.servlet_path</tt>, it returns the same as
-	 * {@link PortletServletRequest#getServletPath()}
+	 * Gets an attribute value on the {@link PortletRequest}. If the attribute
+	 * name is <tt>javax.servlet.include.servlet_path</tt>, it returns the
+	 * same as {@link PortletServletRequest#getServletPath()}
+	 * 
 	 * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
 	 */
 	public Object getAttribute(String name) {
-		if("javax.servlet.include.servlet_path".equals(name)) {
+		if ("javax.servlet.include.servlet_path".equals(name)) {
 			return getServletPath();
-		}
-		else {
+		} else {
 			return portletRequest.getAttribute(name);
 		}
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#getAttributeNames()
 	 */
 	public Enumeration getAttributeNames() {
@@ -286,130 +348,153 @@
 
 	/**
 	 * Can only be invoked in the event phase.
+	 * 
 	 * @see ServletRequest#getCharacterEncoding()
-	 * @throws IllegalStateException If the portlet is not in the event phase.
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
 	 */
 	public String getCharacterEncoding() {
-		if(portletRequest instanceof ActionRequest) {
-			return ((ActionRequest)portletRequest).getCharacterEncoding();
-		}
-		else {
+		if (portletRequest instanceof ActionRequest) {
+			return ((ActionRequest) portletRequest).getCharacterEncoding();
+		} else {
 			throw new IllegalStateException("Not allowed in render phase");
 		}
 	}
 
 	/**
 	 * Can only be invoked in the event phase.
+	 * 
 	 * @see ServletRequest#getContentLength()
-	 * @throws IllegalStateException If the portlet is not in the event phase.
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
 	 */
 	public int getContentLength() {
-		if(portletRequest instanceof ActionRequest) {
-			return ((ActionRequest)portletRequest).getContentLength();
-		}
-		else {
+		if (portletRequest instanceof ActionRequest) {
+			return ((ActionRequest) portletRequest).getContentLength();
+		} else {
 			throw new IllegalStateException("Not allowed in render phase");
 		}
 	}
 
 	/**
 	 * Can only be invoked in the event phase.
+	 * 
 	 * @see ServletRequest#getContentType()
-	 * @throws IllegalStateException If the portlet is not in the event phase.
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
 	 */
 	public String getContentType() {
-		if(portletRequest instanceof ActionRequest) {
-			return ((ActionRequest)portletRequest).getContentType();
-		}
-		else {
+		if (portletRequest instanceof ActionRequest) {
+			return ((ActionRequest) portletRequest).getContentType();
+		} else {
 			throw new IllegalStateException("Not allowed in render phase");
 		}
 	}
 
 	/**
-	 * Can only be invoked in the event phase. When invoked in the event phase, it will wrap the
-	 * portlet's {@link InputStream} as a {@link PortletServletInputStream}.
+	 * Can only be invoked in the event phase. When invoked in the event phase,
+	 * it will wrap the portlet's {@link InputStream} as a
+	 * {@link PortletServletInputStream}.
+	 * 
 	 * @see ServletRequest#getInputStream()
-	 * @throws IllegalStateException If the portlet is not in the event phase.
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
 	 */
 	public ServletInputStream getInputStream() throws IOException {
-		if(portletRequest instanceof ActionRequest) {
-			return new PortletServletInputStream(((ActionRequest)portletRequest).getPortletInputStream());
-		}
-		else {
+		if (portletRequest instanceof ActionRequest) {
+			return new PortletServletInputStream(((ActionRequest) portletRequest).getPortletInputStream());
+		} else {
 			throw new IllegalStateException("Not allowed in render phase");
 		}
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public String getLocalAddr() {
-		if(portletRequest instanceof HttpServletRequest) {
-			return ((HttpServletRequest)portletRequest).getLocalAddr();
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getLocalAddr();
 		}
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public String getLocalName() {
-		if(portletRequest instanceof HttpServletRequest) {
-			return ((HttpServletRequest)portletRequest).getLocalName();
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getLocalName();
 		}
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public int getLocalPort() {
-		if(portletRequest instanceof HttpServletRequest) {
-			return ((HttpServletRequest)portletRequest).getLocalPort();
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getLocalPort();
 		}
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#getLocale()
 	 */
 	public Locale getLocale() {
 		return portletRequest.getLocale();
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#getLocales()
 	 */
 	public Enumeration getLocales() {
 		return portletRequest.getLocales();
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
 	 */
 	public String getParameter(String name) {
 		return portletRequest.getParameter(name);
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#getParameterMap()
 	 */
 	public Map getParameterMap() {
 		return portletRequest.getParameterMap();
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#getParameterNames()
 	 */
 	public Enumeration getParameterNames() {
 		return portletRequest.getParameterNames();
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
 	 */
 	public String[] getParameterValues(String name) {
@@ -417,31 +502,36 @@
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public String getProtocol() {
-		if(portletRequest instanceof HttpServletRequest) {
-			return ((HttpServletRequest)portletRequest).getProtocol();
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getProtocol();
 		}
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
 	 * Can only be invoked in the event phase.
+	 * 
 	 * @see ServletRequest#getReader()
-	 * @throws IllegalStateException If the portlet is not in the event phase.
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
 	 */
 	public BufferedReader getReader() throws IOException {
-		if(portletRequest instanceof ActionRequest) {
-			return ((ActionRequest)portletRequest).getReader();
-		}
-		else {
+		if (portletRequest instanceof ActionRequest) {
+			return ((ActionRequest) portletRequest).getReader();
+		} else {
 			throw new IllegalStateException("Not allowed in render phase");
 		}
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
 	 */
 	public String getRealPath(String path) {
@@ -449,54 +539,66 @@
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public String getRemoteAddr() {
-		if(portletRequest instanceof HttpServletRequest) {
-			return ((HttpServletRequest)portletRequest).getRemoteAddr();
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getRemoteAddr();
 		}
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public String getRemoteHost() {
-		if(portletRequest instanceof HttpServletRequest) {
-			return ((HttpServletRequest)portletRequest).getRemoteHost();
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getRemoteHost();
 		}
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public int getRemotePort() {
-		if(portletRequest instanceof HttpServletRequest) {
-			return ((HttpServletRequest)portletRequest).getRemotePort();
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getRemotePort();
 		}
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
 	/**
-	 * Get the {@link PortletRequestDispatcher} as a {@link PortletServletRequestDispatcher} instance.
+	 * Get the {@link PortletRequestDispatcher} as a
+	 * {@link PortletServletRequestDispatcher} instance.
+	 * 
 	 * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
 	 */
 	public RequestDispatcher getRequestDispatcher(String path) {
 		return new PortletServletRequestDispatcher(portletContext.getRequestDispatcher(path));
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#getScheme()
 	 */
 	public String getScheme() {
 		return portletRequest.getScheme();
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#getServerName()
 	 */
 	public String getServerName() {
@@ -504,32 +606,41 @@
 	}
 
 	/**
-	 * Not allowed in a portlet. 
-	 * @throws IllegalStateException Not allowed in a portlet.
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
 	 */
 	public int getServerPort() {
-		if(portletRequest instanceof HttpServletRequest) {
-			return ((HttpServletRequest)portletRequest).getServerPort();
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getServerPort();
 		}
 		throw new IllegalStateException("Not allowed in a portlet");
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#isSecure()
 	 */
 	public boolean isSecure() {
 		return portletRequest.isSecure();
 	}
 
-	/* (non-Javadoc)
+	/*
+	 * (non-Javadoc)
+	 * 
 	 * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
 	 */
 	public void removeAttribute(String name) {
 		portletRequest.removeAttribute(name);
 	}
 
-	/* (non-Javadoc)
-	 * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#setAttribute(java.lang.String,
+	 *      java.lang.Object)
 	 */
 	public void setAttribute(String name, Object o) {
 		portletRequest.setAttribute(name, o);
@@ -537,23 +648,32 @@
 
 	/**
 	 * Can only be invoked in the event phase.
+	 * 
 	 * @see ServletRequest#setCharacterEncoding(String)
-	 * @throws IllegalStateException If the portlet is not in the event phase.
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
 	 */
 	public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
-		if(portletRequest instanceof ActionRequest) {
-			((ActionRequest)portletRequest).setCharacterEncoding(env);
-		}
-		else {
+		if (portletRequest instanceof ActionRequest) {
+			((ActionRequest) portletRequest).setCharacterEncoding(env);
+		} else {
 			throw new IllegalStateException("Not allowed in render phase");
 		}
 	}
-	
+
 	/**
-	 * Get the wrapped {@link PortletRequest} instance. 
+	 * Get the wrapped {@link PortletRequest} instance.
+	 * 
 	 * @return The wrapped {@link PortletRequest} instance.
 	 */
 	public PortletRequest getPortletRequest() {
 		return portletRequest;
 	}
+
+	@Inject(StrutsConstants.STRUTS_ACTION_EXTENSION)
+	public void setExtension(String extension) {
+		if (extension != null) {
+			this.extension = extension.split(",")[0];
+		}
+	}
 }

Modified: struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/dispatcher/Jsr168DispatcherTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/dispatcher/Jsr168DispatcherTest.java?rev=671156&r1=671155&r2=671156&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/dispatcher/Jsr168DispatcherTest.java (original)
+++ struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/dispatcher/Jsr168DispatcherTest.java Tue Jun 24 05:33:59 2008
@@ -52,6 +52,7 @@
 import org.springframework.mock.web.portlet.MockPortletContext;
 
 import com.opensymphony.xwork2.Action;
+import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.ActionProxy;
 import com.opensymphony.xwork2.ActionProxyFactory;
@@ -59,272 +60,283 @@
 
 /**
  * Jsr168DispatcherTest. Insert description.
- *
+ * 
  */
 public class Jsr168DispatcherTest extends MockObjectTestCase implements PortletActionConstants {
 
-	private final String MULTIPART_REQUEST = "-----------------------------4827543632391\r\n" 
-		+ "Content-Disposition: form-data; name=\"upload\"; filename=\"test.txt\"\r\n"
-		+ "Content-Type: text/plain\r\n"
-		+ "\r\n"
-		+ "This is a test file\r\n"
-		+ "-----------------------------4827543632391\r\n"
-		+ "Content-Disposition: form-data; name=\"caption\"\r\n"
-		+ "\r\n"
-		+ "TestCaption\r\n"
-		+ "-----------------------------4827543632391--";
-	
-    Jsr168Dispatcher dispatcher = null;
-    Mock mockConfig = null;
-    Mock mockCtx = null;
-    Mock mockRequest = null;
-    Mock mockSession = null;
-    Mock mockActionFactory = null;
-    Mock mockActionProxy = null;
-    Mock mockAction = null;
-    Mock mockInvocation = null;
-
-    public void setUp() {
-        dispatcher = new Jsr168Dispatcher();
-    }
-
-    private void initPortletConfig(final Map initParams, final Map attributes) {
-        mockConfig = mock(PortletConfig.class);
-        mockCtx = mock(PortletContext.class);
-        mockConfig.stubs().method(ANYTHING);
-        mockCtx.stubs().method(ANYTHING);
-        setupStub(initParams, mockConfig, "getInitParameter");
-        mockCtx.stubs().method("getAttributeNames").will(returnValue(Collections.enumeration(attributes.keySet())));
-        setupStub(attributes, mockCtx, "getAttribute");
-        mockConfig.stubs().method("getPortletContext").will(returnValue(mockCtx.proxy()));
-        mockCtx.stubs().method("getInitParameterNames").will(returnValue(Collections.enumeration(initParams.keySet())));
-        setupStub(initParams, mockCtx, "getInitParameter");
-        mockConfig.stubs().method("getInitParameterNames").will(returnValue(Collections.enumeration(initParams.keySet())));
-        setupStub(initParams, mockConfig, "getInitParameter");
-        mockConfig.stubs().method("getResourceBundle").will(returnValue(new ListResourceBundle() {
-            protected Object[][] getContents() {
-                return new String[][]{{"javax.portlet.title", "MyTitle"}};
-            }
-        }));
-    }
-
-    private void setupActionFactory(String namespace, String actionName, String result, ValueStack stack) {
-        if(mockActionFactory == null) {
-            mockActionFactory = mock(ActionProxyFactory.class);
-        }
-        mockAction = mock(Action.class);
-        mockActionProxy = mock(ActionProxy.class);
-        mockInvocation = mock(ActionInvocation.class);
-
-        mockActionFactory.expects(once()).method("createActionProxy").with(new Constraint[]{eq(namespace), eq(actionName), NULL, isA(Map.class)}).will(returnValue(mockActionProxy.proxy()));
-        mockActionProxy.stubs().method("getAction").will(returnValue(mockAction.proxy()));
-        mockActionProxy.expects(once()).method("execute").will(returnValue(result));
-        mockActionProxy.expects(once()).method("getInvocation").will(returnValue(mockInvocation.proxy()));
-        mockInvocation.stubs().method("getStack").will(returnValue(stack));
-
-    }
-    
-    public void testParseConfigWithBang() {
-    	MockPortletContext portletContext = new MockPortletContext();
-    	MockPortletConfig portletConfig = new MockPortletConfig(portletContext);
-
-    	portletConfig.addInitParameter("viewNamespace", "/view");
-    	portletConfig.addInitParameter("defaultViewAction", "index!input");
-    	
-    	Map<PortletMode, ActionMapping> actionMap = new HashMap<PortletMode, ActionMapping>();
-    	
-    	dispatcher.parseModeConfig(actionMap, portletConfig, PortletMode.VIEW, "viewNamespace", "defaultViewAction");
-    	
-    	ActionMapping mapping = actionMap.get(PortletMode.VIEW);
-    	assertEquals("index", mapping.getName());
-    	assertEquals("/view", mapping.getNamespace());
-    	assertEquals("input", mapping.getMethod());
-    }
-
-    public void testRender_ok() {
-        final Mock mockResponse = mock(RenderResponse.class);
-        mockResponse.stubs().method(ANYTHING);
-
-        PortletMode mode = PortletMode.VIEW;
-
-        Map requestParams = new HashMap();
-        requestParams.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
-        requestParams.put(EVENT_ACTION, new String[]{"true"});
-        requestParams.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
-
-        Map sessionMap = new HashMap();
-
-
-
-        Map initParams = new HashMap();
-        initParams.put("viewNamespace", "/view");
-        initParams.put(StrutsConstants.STRUTS_ALWAYS_SELECT_FULL_NAMESPACE, "true");
-
-        initPortletConfig(initParams, new HashMap());
-        initRequest(requestParams, new HashMap(), sessionMap, new HashMap(), PortletMode.VIEW, WindowState.NORMAL, false, null);
-        setupActionFactory("/view", "testAction", "success", EasyMock.createNiceMock(ValueStack.class));
-
-        mockInvocation.expects(once()).method("getStack").will(
-                returnValue(null));
-        //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
-        try {
-            dispatcher
-                    .setActionProxyFactory((ActionProxyFactory) mockActionFactory
-                            .proxy());
-            dispatcher.init((PortletConfig) mockConfig.proxy());
-            dispatcher.render((RenderRequest) mockRequest.proxy(),
-                    (RenderResponse) mockResponse.proxy());
-        } catch (Exception e) {
-            e.printStackTrace();
-            fail("Error occured");
-        }
-    }
-
-    public void testProcessAction_ok() {
-        final Mock mockResponse = mock(ActionResponse.class);
-
-        PortletMode mode = PortletMode.VIEW;
-        Map initParams = new HashMap();
-        initParams.put("viewNamespace", "/view");
-
-        Map requestParams = new HashMap();
-        requestParams.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
-        requestParams.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
-
-        initParams.put(StrutsConstants.STRUTS_ALWAYS_SELECT_FULL_NAMESPACE, "true");
-        initPortletConfig(initParams, new HashMap());
-        initRequest(requestParams, new HashMap(), new HashMap(), new HashMap(), PortletMode.VIEW, WindowState.NORMAL, true, null);
-        setupActionFactory("/view", "testAction", "success", EasyMock.createNiceMock(ValueStack.class));
-        //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
-        try {
-            dispatcher
-                    .setActionProxyFactory((ActionProxyFactory) mockActionFactory
-                            .proxy());
-            dispatcher.init((PortletConfig) mockConfig.proxy());
-            dispatcher.processAction((ActionRequest) mockRequest.proxy(),
-                    (ActionResponse) mockResponse.proxy());
-        } catch (Exception e) {
-            e.printStackTrace();
-            fail("Error occured");
-        }
-    }
-
-    /**
-     * Initialize the mock request (and as a result, the mock session)
-     * @param requestParams The request parameters
-     * @param requestAttributes The request attributes
-     * @param sessionParams The session attributes
-     * @param renderParams The render parameters. Will only be set if <code>isEvent</code> is <code>true</code>
-     * @param mode The portlet mode
-     * @param state The portlet window state
-     * @param isEvent <code>true</code> when the request is an ActionRequest.
-     * @param locale The locale. If <code>null</code>, the request will return <code>Locale.getDefault()</code>
-     */
-    private void initRequest(Map requestParams, Map requestAttributes, Map sessionParams, Map renderParams, PortletMode mode, WindowState state, boolean isEvent, Locale locale) {
-        mockRequest = isEvent ? mock(ActionRequest.class) : mock(RenderRequest.class);
-        mockSession = mock(PortletSession.class);
-        mockSession.stubs().method(ANYTHING);
-        mockRequest.stubs().method(ANYTHING);
-        setupStub(sessionParams, mockSession, "getAttribute");
-        mockSession.stubs().method("getAttributeNames").will(returnValue(Collections.enumeration(sessionParams.keySet())));
-        setupParamStub(requestParams, mockRequest, "getParameter");
-        setupStub(requestAttributes, mockRequest, "getAttribute");
-        mockRequest.stubs().method("getAttributeNames").will(returnValue(Collections.enumeration(requestAttributes.keySet())));
-        mockRequest.stubs().method("getParameterMap").will(returnValue(requestParams));
-        mockRequest.stubs().method("getParameterNames").will(returnValue(Collections.enumeration(requestParams.keySet())));
-        mockRequest.stubs().method("getPortletSession").will(returnValue(mockSession.proxy()));
-        if(locale != null) {
-            mockRequest.stubs().method("getLocale").will(returnValue(locale));
-        }
-        else {
-            mockRequest.stubs().method("getLocale").will(returnValue(Locale.getDefault()));
-        }
-        mockRequest.stubs().method("getPortletMode").will(returnValue(mode));
-        mockRequest.stubs().method("getWindowState").will(returnValue(state));
-    }
-
-    private void setupParamStub(Map requestParams, Mock mockRequest, String method) {
-        Map newMap = new HashMap();
-        Iterator it = requestParams.keySet().iterator();
-        while(it.hasNext()) {
-            Object key = it.next();
-            String[] val = (String[])requestParams.get(key);
-            newMap.put(key, val[0]);
-        }
-        setupStub(newMap, mockRequest, method);
-
-    }
-
-    /**
-     * Set up stubs for the mock.
-     * @param map The map containing the <code>key</code> and <code>values</code>. The key is the
-     * expected parameter to <code>method</code>, and value is the value that should be returned from
-     * the stub.
-     * @param mock The mock to initialize.
-     * @param method The name of the method to stub.
-     */
-    private void setupStub(Map map, Mock mock, String method) {
-        Iterator it = map.keySet().iterator();
-        while(it.hasNext()) {
-            Object key = it.next();
-            Object val = map.get(key);
-            mock.stubs().method(method).with(eq(key)).will(returnValue(val));
-        }
-    }
-
-    public void testModeChangeUsingPortletWidgets() {
-        final Mock mockResponse = mock(RenderResponse.class);
-        mockResponse.stubs().method(ANYTHING);
-        PortletMode mode = PortletMode.EDIT;
-
-        Map requestParams = new HashMap();
-        requestParams.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
-        requestParams.put(EVENT_ACTION, new String[]{"false"});
-        requestParams.put(PortletActionConstants.MODE_PARAM, new String[]{PortletMode.VIEW.toString()});
-
-        Map sessionMap = new HashMap();
-
-        Map initParams = new HashMap();
-        initParams.put("viewNamespace", "/view");
-        initParams.put("editNamespace", "/edit");
-
-        initPortletConfig(initParams, new HashMap());
-        initRequest(requestParams, new HashMap(), sessionMap, new HashMap(), mode, WindowState.NORMAL, false, null);
-        setupActionFactory("/edit", "default", "success", EasyMock.createNiceMock(ValueStack.class));
-
-        mockInvocation.expects(once()).method("getStack").will(
-                returnValue(null));
-        //mockSession.expects(once()).method("setAttribute").with(new Constraint[]{eq(PortletActionConstants.LAST_MODE), eq(PortletMode.VIEW)});
-        try {
-            dispatcher
-                    .setActionProxyFactory((ActionProxyFactory) mockActionFactory
-                            .proxy());
-            dispatcher.init((PortletConfig) mockConfig.proxy());
-            dispatcher.render((RenderRequest) mockRequest.proxy(),
-                    (RenderResponse) mockResponse.proxy());
-        } catch (Exception e) {
-            e.printStackTrace();
-            fail("Error occured");
-        }
-    }
-    
-    public void testMultipartRequest_parametersAreCopiedToActionInvocation() throws Exception {
-    	MockPortletContext ctx = new MockPortletContext();
-    	ctx.setAttribute("javax.servlet.context.tempdir", new File("target").getAbsoluteFile());
-    	MockActionRequest request = new MockActionRequest(ctx);
-    	request.setContent(MULTIPART_REQUEST.getBytes("US-ASCII"));
-    	request.setContentType("multipart/form-data; boundary=---------------------------4827543632391");
-    	request.setProperty("Content-Length", "" + MULTIPART_REQUEST.length());
-    	MockActionResponse response = new MockActionResponse();
-    	Map<String, Object> requestMap = new HashMap<String, Object>();
-    	Map<String, String[]> paramMap = new HashMap<String, String[]>();
-    	Map<String, Object> sessionMap = new HashMap<String, Object>();
-    	Map<String, Object> applicationMap = new HashMap<String, Object>();
-    	initPortletConfig(new HashMap(), new HashMap());
-    	MockPortletConfig config = new MockPortletConfig(ctx);
-    	dispatcher.init(config);
-    	dispatcher.createContextMap(requestMap, paramMap, sessionMap, applicationMap, request, response, config, PortletActionConstants.EVENT_PHASE);
-    	assertNotNull("Caption was not found in parameter map!", paramMap.get("caption"));
-    	assertEquals("TestCaption", paramMap.get("caption")[0]);
-    }
+	private final String MULTIPART_REQUEST = "-----------------------------4827543632391\r\n"
+			+ "Content-Disposition: form-data; name=\"upload\"; filename=\"test.txt\"\r\n"
+			+ "Content-Type: text/plain\r\n" 
+			+ "\r\n" 
+			+ "This is a test file\r\n"
+			+ "-----------------------------4827543632391\r\n" 
+			+ "Content-Disposition: form-data; name=\"caption\"\r\n"
+			+ "\r\n" 
+			+ "TestCaption\r\n" + "-----------------------------4827543632391--";
+
+	Jsr168Dispatcher dispatcher = null;
+
+	Mock mockConfig = null;
+
+	Mock mockCtx = null;
+
+	Mock mockRequest = null;
+
+	Mock mockSession = null;
+
+	Mock mockActionFactory = null;
+
+	Mock mockActionProxy = null;
+
+	Mock mockAction = null;
+
+	Mock mockInvocation = null;
+
+	public void setUp() throws Exception {
+		super.setUp();
+		dispatcher = new Jsr168Dispatcher();
+	}
+
+	private void initPortletConfig(final Map<String, String> initParams, final Map<String, Object> attributes) {
+		mockConfig = mock(PortletConfig.class);
+		mockCtx = mock(PortletContext.class);
+		mockConfig.stubs().method(ANYTHING);
+		mockCtx.stubs().method(ANYTHING);
+		setupStub(initParams, mockConfig, "getInitParameter");
+		mockCtx.stubs().method("getAttributeNames").will(returnValue(Collections.enumeration(attributes.keySet())));
+		setupStub(attributes, mockCtx, "getAttribute");
+		mockConfig.stubs().method("getPortletContext").will(returnValue(mockCtx.proxy()));
+		mockCtx.stubs().method("getInitParameterNames").will(returnValue(Collections.enumeration(initParams.keySet())));
+		setupStub(initParams, mockCtx, "getInitParameter");
+		mockConfig.stubs().method("getInitParameterNames").will(
+				returnValue(Collections.enumeration(initParams.keySet())));
+		setupStub(initParams, mockConfig, "getInitParameter");
+		mockConfig.stubs().method("getResourceBundle").will(returnValue(new ListResourceBundle() {
+			protected Object[][] getContents() {
+				return new String[][] { { "javax.portlet.title", "MyTitle" } };
+			}
+		}));
+	}
+
+	private void setupActionFactory(String namespace, String actionName, String result, ValueStack stack) {
+		if (mockActionFactory == null) {
+			mockActionFactory = mock(ActionProxyFactory.class);
+		}
+		mockAction = mock(Action.class);
+		mockActionProxy = mock(ActionProxy.class);
+		mockInvocation = mock(ActionInvocation.class);
+
+		mockActionFactory.expects(once()).method("createActionProxy").with(
+				new Constraint[] { eq(namespace), eq(actionName), NULL, isA(Map.class) }).will(
+				returnValue(mockActionProxy.proxy()));
+		mockActionProxy.stubs().method("getAction").will(returnValue(mockAction.proxy()));
+		mockActionProxy.expects(once()).method("execute").will(returnValue(result));
+		mockActionProxy.expects(once()).method("getInvocation").will(returnValue(mockInvocation.proxy()));
+		mockInvocation.stubs().method("getStack").will(returnValue(stack));
+
+	}
+
+	public void testParseConfigWithBang() {
+		MockPortletContext portletContext = new MockPortletContext();
+		MockPortletConfig portletConfig = new MockPortletConfig(portletContext);
+
+		portletConfig.addInitParameter("viewNamespace", "/view");
+		portletConfig.addInitParameter("defaultViewAction", "index!input");
+
+		Map<PortletMode, ActionMapping> actionMap = new HashMap<PortletMode, ActionMapping>();
+
+		dispatcher.parseModeConfig(actionMap, portletConfig, PortletMode.VIEW, "viewNamespace", "defaultViewAction");
+
+		ActionMapping mapping = actionMap.get(PortletMode.VIEW);
+		assertEquals("index", mapping.getName());
+		assertEquals("/view", mapping.getNamespace());
+		assertEquals("input", mapping.getMethod());
+	}
+
+	public void testRender_ok() throws Exception {
+		final Mock mockResponse = mock(RenderResponse.class);
+		mockResponse.stubs().method(ANYTHING);
+
+		PortletMode mode = PortletMode.VIEW;
+
+		Map<String, String[]> requestParams = new HashMap<String, String[]>();
+		requestParams.put(PortletActionConstants.ACTION_PARAM, new String[] { "/view/testAction" });
+		requestParams.put(EVENT_ACTION, new String[] { "true" });
+		requestParams.put(PortletActionConstants.MODE_PARAM, new String[] { mode.toString() });
+
+		Map sessionMap = new HashMap();
+
+		Map<String, String> initParams = new HashMap<String, String>();
+		initParams.put("viewNamespace", "/view");
+		initParams.put(StrutsConstants.STRUTS_ALWAYS_SELECT_FULL_NAMESPACE, "true");
+
+		initPortletConfig(initParams, new HashMap<String, Object>());
+		initRequest(requestParams, new HashMap(), sessionMap, new HashMap(), PortletMode.VIEW, WindowState.NORMAL,
+				false, null);
+		setupActionFactory("/view", "testAction", "success", EasyMock.createNiceMock(ValueStack.class));
+
+		mockInvocation.expects(once()).method("getStack").will(returnValue(null));
+		// mockSession.expects(once()).method("setAttribute").with(new
+		// Constraint[]{eq(PortletActionConstants.LAST_MODE),
+		// eq(PortletMode.VIEW)});
+		dispatcher.setActionProxyFactory((ActionProxyFactory) mockActionFactory.proxy());
+		dispatcher.init((PortletConfig) mockConfig.proxy());
+		dispatcher.render((RenderRequest) mockRequest.proxy(), (RenderResponse) mockResponse.proxy());
+	}
+
+	public void testProcessAction_ok() throws Exception {
+		final Mock mockResponse = mock(ActionResponse.class);
+
+		PortletMode mode = PortletMode.VIEW;
+		Map<String, String> initParams = new HashMap<String, String>();
+		initParams.put("viewNamespace", "/view");
+
+		Map<String, String[]> requestParams = new HashMap<String, String[]>();
+		requestParams.put(PortletActionConstants.ACTION_PARAM, new String[] { "/view/testAction" });
+		requestParams.put(PortletActionConstants.MODE_PARAM, new String[] { mode.toString() });
+
+		initParams.put(StrutsConstants.STRUTS_ALWAYS_SELECT_FULL_NAMESPACE, "true");
+		initPortletConfig(initParams, new HashMap<String, Object>());
+		initRequest(requestParams, new HashMap<String, Object>(), new HashMap<String, Object>(), new HashMap<String, String[]>(), PortletMode.VIEW, WindowState.NORMAL,
+				true, null);
+		setupActionFactory("/view", "testAction", "success", EasyMock.createNiceMock(ValueStack.class));
+		// mockSession.expects(once()).method("setAttribute").with(new
+		// Constraint[]{eq(PortletActionConstants.LAST_MODE),
+		// eq(PortletMode.VIEW)});
+		dispatcher.setActionProxyFactory((ActionProxyFactory) mockActionFactory.proxy());
+		dispatcher.init((PortletConfig) mockConfig.proxy());
+		dispatcher.processAction((ActionRequest) mockRequest.proxy(), (ActionResponse) mockResponse.proxy());
+	}
+
+	/**
+	 * Initialize the mock request (and as a result, the mock session)
+	 * 
+	 * @param requestParams
+	 *            The request parameters
+	 * @param requestAttributes
+	 *            The request attributes
+	 * @param sessionParams
+	 *            The session attributes
+	 * @param renderParams
+	 *            The render parameters. Will only be set if
+	 *            <code>isEvent</code> is <code>true</code>
+	 * @param mode
+	 *            The portlet mode
+	 * @param state
+	 *            The portlet window state
+	 * @param isEvent
+	 *            <code>true</code> when the request is an ActionRequest.
+	 * @param locale
+	 *            The locale. If <code>null</code>, the request will return
+	 *            <code>Locale.getDefault()</code>
+	 */
+	private void initRequest(Map<String, String[]> requestParams, Map<String, Object> requestAttributes, Map<String, Object> sessionParams, Map<String, String[]> renderParams,
+			PortletMode mode, WindowState state, boolean isEvent, Locale locale) {
+		mockRequest = isEvent ? mock(ActionRequest.class) : mock(RenderRequest.class);
+		mockSession = mock(PortletSession.class);
+		mockSession.stubs().method(ANYTHING);
+		mockRequest.stubs().method(ANYTHING);
+		setupStub(sessionParams, mockSession, "getAttribute");
+		mockSession.stubs().method("getAttributeNames").will(
+				returnValue(Collections.enumeration(sessionParams.keySet())));
+		setupParamStub(requestParams, mockRequest, "getParameter");
+		setupStub(requestAttributes, mockRequest, "getAttribute");
+		mockRequest.stubs().method("getAttributeNames").will(
+				returnValue(Collections.enumeration(requestAttributes.keySet())));
+		mockRequest.stubs().method("getParameterMap").will(returnValue(requestParams));
+		mockRequest.stubs().method("getParameterNames").will(
+				returnValue(Collections.enumeration(requestParams.keySet())));
+		mockRequest.stubs().method("getPortletSession").will(returnValue(mockSession.proxy()));
+		if (locale != null) {
+			mockRequest.stubs().method("getLocale").will(returnValue(locale));
+		} else {
+			mockRequest.stubs().method("getLocale").will(returnValue(Locale.getDefault()));
+		}
+		mockRequest.stubs().method("getPortletMode").will(returnValue(mode));
+		mockRequest.stubs().method("getWindowState").will(returnValue(state));
+	}
+
+	private void setupParamStub(Map<String, String[]> requestParams, Mock mockRequest, String method) {
+		Map<String, String> newMap = new HashMap<String, String>();
+		Iterator<String> it = requestParams.keySet().iterator();
+		while (it.hasNext()) {
+			String key = it.next();
+			String[] val = (String[]) requestParams.get(key);
+			newMap.put(key, val[0]);
+		}
+		setupStub(newMap, mockRequest, method);
+
+	}
+
+	/**
+	 * Set up stubs for the mock.
+	 * 
+	 * @param map
+	 *            The map containing the <code>key</code> and
+	 *            <code>values</code>. The key is the expected parameter to
+	 *            <code>method</code>, and value is the value that should be
+	 *            returned from the stub.
+	 * @param mock
+	 *            The mock to initialize.
+	 * @param method
+	 *            The name of the method to stub.
+	 */
+	private void setupStub(Map map, Mock mock, String method) {
+		Iterator it = map.keySet().iterator();
+		while (it.hasNext()) {
+			Object key = it.next();
+			Object val = map.get(key);
+			mock.stubs().method(method).with(eq(key)).will(returnValue(val));
+		}
+	}
+
+	public void testModeChangeUsingPortletWidgets() throws Exception {
+		final Mock mockResponse = mock(RenderResponse.class);
+		mockResponse.stubs().method(ANYTHING);
+		PortletMode mode = PortletMode.EDIT;
+
+		Map<String, String[]> requestParams = new HashMap<String, String[]>();
+		requestParams.put(PortletActionConstants.ACTION_PARAM, new String[] { "/view/testAction" });
+		requestParams.put(EVENT_ACTION, new String[] { "false" });
+		requestParams.put(PortletActionConstants.MODE_PARAM, new String[] { PortletMode.VIEW.toString() });
+
+		Map<String, Object> sessionMap = new HashMap<String, Object>();
+
+		Map<String, String> initParams = new HashMap<String, String>();
+		initParams.put("viewNamespace", "/view");
+		initParams.put("editNamespace", "/edit");
+
+		initPortletConfig(initParams, new HashMap<String, Object>());
+		initRequest(requestParams, new HashMap<String, Object>(), sessionMap, new HashMap<String, String[]>(), mode, WindowState.NORMAL, false, null);
+		setupActionFactory("/edit", "default", "success", EasyMock.createNiceMock(ValueStack.class));
+
+		mockInvocation.expects(once()).method("getStack").will(returnValue(null));
+		// mockSession.expects(once()).method("setAttribute").with(new
+		// Constraint[]{eq(PortletActionConstants.LAST_MODE),
+		// eq(PortletMode.VIEW)});
+		dispatcher.setActionProxyFactory((ActionProxyFactory) mockActionFactory.proxy());
+		dispatcher.init((PortletConfig) mockConfig.proxy());
+		dispatcher.render((RenderRequest) mockRequest.proxy(), (RenderResponse) mockResponse.proxy());
+	}
+
+	public void testMultipartRequest_parametersAreCopiedToActionInvocation() throws Exception {
+		MockPortletContext ctx = new MockPortletContext();
+		ctx.setAttribute("javax.servlet.context.tempdir", new File("target").getAbsoluteFile());
+		MockActionRequest request = new MockActionRequest(ctx);
+		request.setContent(MULTIPART_REQUEST.getBytes("US-ASCII"));
+		request.setContentType("multipart/form-data; boundary=---------------------------4827543632391");
+		request.setProperty("Content-Length", "" + MULTIPART_REQUEST.length());
+		MockActionResponse response = new MockActionResponse();
+		Map<String, Object> requestMap = new HashMap<String, Object>();
+		Map<String, String[]> paramMap = new HashMap<String, String[]>();
+		Map<String, Object> sessionMap = new HashMap<String, Object>();
+		Map<String, Object> applicationMap = new HashMap<String, Object>();
+		initPortletConfig(new HashMap<String, String>(), new HashMap<String, Object>());
+		MockPortletConfig config = new MockPortletConfig(ctx);
+		dispatcher.init(config);
+		dispatcher.createContextMap(requestMap, paramMap, sessionMap, applicationMap, request, response, config,
+				PortletActionConstants.EVENT_PHASE);
+		assertNotNull("Caption was not found in parameter map!", paramMap.get("caption"));
+		assertEquals("TestCaption", paramMap.get("caption")[0]);
+	}
 }

Added: struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/servlet/PortletServletRequestTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/servlet/PortletServletRequestTest.java?rev=671156&view=auto
==============================================================================
--- struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/servlet/PortletServletRequestTest.java (added)
+++ struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/portlet/servlet/PortletServletRequestTest.java Tue Jun 24 05:33:59 2008
@@ -0,0 +1,49 @@
+package org.apache.struts2.portlet.servlet;
+
+import org.apache.struts2.ServletActionContext;
+import org.apache.struts2.StrutsTestCase;
+import org.apache.struts2.dispatcher.mapper.ActionMapping;
+import org.apache.struts2.portlet.PortletActionConstants;
+import org.apache.struts2.portlet.context.PortletActionContext;
+import org.springframework.mock.web.portlet.MockPortletContext;
+import org.springframework.mock.web.portlet.MockPortletRequest;
+
+import com.opensymphony.xwork2.ActionContext;
+
+public class PortletServletRequestTest extends StrutsTestCase {
+	
+	private MockPortletRequest portletRequest;
+	private MockPortletContext portletContext;
+	private PortletServletRequest request;
+	
+	protected void setUp() throws Exception {
+		super.setUp();
+		portletRequest = new MockPortletRequest();
+		portletContext = new MockPortletContext();
+		request = new PortletServletRequest(portletRequest, portletContext);
+	}
+	
+	public void testGetServletPathShouldHandleDefaultActionExtension() throws Exception {
+		portletRequest.setParameter(PortletActionConstants.ACTION_PARAM, "actionName");
+		request.setExtension("action");
+		assertEquals("actionName.action", request.getServletPath());
+	}
+	
+	public void testGetServletPathShouldHandleCustomActionExtension() throws Exception {
+		portletRequest.setParameter(PortletActionConstants.ACTION_PARAM, "actionName");
+		request.setExtension("custom");
+		assertEquals("actionName.custom", request.getServletPath());
+	}
+	
+	public void testGetServletPathShouldHandleNoExtension() throws Exception {
+		portletRequest.setParameter(PortletActionConstants.ACTION_PARAM, "actionName");
+		request.setExtension("");
+		assertEquals("actionName", request.getServletPath());
+	}
+	
+	public void testGetServletPathShouldHandleMultipleExtensionsByUsingTheFirst() throws Exception {
+		portletRequest.setParameter(PortletActionConstants.ACTION_PARAM, "actionName");
+		request.setExtension("action,,");
+		assertEquals("actionName.action", request.getServletPath());
+	}
+}