Author: lukaszlenart Date: Thu May 31 05:59:06 2012 New Revision: 1344572 URL: http://svn.apache.org/viewvc?rev=1344572&view=rev Log: WW-3826 adds new StrutsPortletTestCase to simplify testing actions to be used in portlet environment
Added: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/StrutsPortletTestCase.java Modified: struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java struts/struts2/trunk/plugins/portlet/pom.xml Modified: struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java?rev=1344572&r1=1344571&r2=1344572&view=diff ============================================================================== --- struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java (original) +++ struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java Thu May 31 05:59:06 2012 @@ -26,7 +26,6 @@ import com.opensymphony.xwork2.ActionPro import com.opensymphony.xwork2.ActionProxyFactory; import com.opensymphony.xwork2.XWorkTestCase; import com.opensymphony.xwork2.config.Configuration; -import com.opensymphony.xwork2.util.ClassLoaderUtil; import com.opensymphony.xwork2.util.logging.LoggerFactory; import com.opensymphony.xwork2.util.logging.jdk.JdkLoggerFactory; import org.apache.struts2.dispatcher.Dispatcher; @@ -153,7 +152,6 @@ public abstract class StrutsTestCase ext protected void initActionContext(ActionContext actionContext) { actionContext.setParameters(new HashMap<String, Object>(request.getParameterMap())); initSession(actionContext); - initPortletContext(actionContext); applyAdditionalParams(actionContext); // set the action context to the one used by the proxy ActionContext.setContext(actionContext); @@ -166,20 +164,6 @@ public abstract class StrutsTestCase ext } } - protected void initPortletContext(ActionContext actionContext) { - try { - ClassLoaderUtil.loadClass("javax.portlet.PortletContext", getClass()); - Class mockClazz = ClassLoaderUtil.loadClass("org.springframework.mock.web.portlet.MockPortletContext", getClass()); - actionContext.put(StrutsStatics.STRUTS_PORTLET_CONTEXT, mockClazz.newInstance()); - } catch (ClassNotFoundException e) { - LOG.debug("Cannot initialize portlet Mocks (javax.portlet.PortletContext class not found), you're missing dependencies (please add them) or not a portlet environment (so you can ignore this)!"); - } catch (InstantiationException e) { - LOG.warn("Cannot initialize portlet mocks!", e); - } catch (IllegalAccessException e) { - LOG.warn("Cannot initialize portlet mocks!", e); - } - } - /** * Can be overwritten in subclass to provide additional context's params and settings used during action invocation * Modified: struts/struts2/trunk/plugins/portlet/pom.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/pom.xml?rev=1344572&r1=1344571&r2=1344572&view=diff ============================================================================== --- struts/struts2/trunk/plugins/portlet/pom.xml (original) +++ struts/struts2/trunk/plugins/portlet/pom.xml Thu May 31 05:59:06 2012 @@ -20,7 +20,13 @@ <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-junit-plugin</artifactId> - <scope>test</scope> + <optional>true</optional> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <optional>true</optional> </dependency> <dependency> @@ -92,15 +98,13 @@ <!-- Mocks for unit testing (by Spring) --> <dependency> <groupId>org.springframework</groupId> - <artifactId>spring-mock</artifactId> - <version>2.0.7</version> - <scope>test</scope> + <artifactId>spring-test</artifactId> + <optional>true</optional> </dependency> <dependency> <groupId>org.springframework</groupId> - <artifactId>spring-portlet</artifactId> - <version>2.0.8</version> - <scope>test</scope> + <artifactId>spring-webmvc-portlet</artifactId> + <optional>true</optional> </dependency> <dependency> <groupId>org.springframework</groupId> Added: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/StrutsPortletTestCase.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/StrutsPortletTestCase.java?rev=1344572&view=auto ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/StrutsPortletTestCase.java (added) +++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/StrutsPortletTestCase.java Thu May 31 05:59:06 2012 @@ -0,0 +1,90 @@ +package org.apache.struts2; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts2.portlet.PortletConstants; +import org.apache.struts2.portlet.PortletPhase; +import org.springframework.mock.web.portlet.MockPortletContext; +import org.springframework.mock.web.portlet.MockPortletRequest; +import org.springframework.mock.web.portlet.MockPortletResponse; +import org.springframework.mock.web.portlet.MockPortletSession; +import org.springframework.mock.web.portlet.MockStateAwareResponse; + +import javax.portlet.PortletMode; +import java.util.HashMap; +import java.util.Map; + +/** + * Base class used to test action in portlet environment + */ +public abstract class StrutsPortletTestCase extends StrutsTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(StrutsPortletTestCase.class); + + protected MockPortletSession portletSession; + protected MockPortletRequest portletRequest; + protected MockPortletResponse portletResponse; + protected MockContext portletContext; + + @Override + protected void initActionContext(ActionContext actionContext) { + super.initActionContext(actionContext); + initPortletContext(actionContext); + } + + protected void initPortletContext(ActionContext actionContext) { + LOG.debug("Initializing mock portlet environment"); + portletContext = new MockContext(); + portletContext.setMajorVersion(getMajorVersion()); + actionContext.put(StrutsStatics.STRUTS_PORTLET_CONTEXT, portletContext); + + portletRequest = new MockPortletRequest(portletContext); + portletResponse = new MockStateAwareResponse(); + portletSession = new MockPortletSession(); + portletRequest.setSession(portletSession); + actionContext.setSession(createSession()); + actionContext.put(PortletConstants.REQUEST, portletRequest); + actionContext.put(PortletConstants.RESPONSE, portletResponse); + actionContext.put(PortletConstants.MODE_NAMESPACE_MAP, new HashMap<PortletMode, String>()); + actionContext.put(PortletConstants.PHASE, PortletPhase.EVENT_PHASE); + } + + /** + * Override to define version of your portlet environment + * + * @return portlet version + */ + protected int getMajorVersion() { + return 2; + } + + /** + * Override to create your own session + * + * @return Map with session parameters + */ + private Map<String, Object> createSession() { + return new HashMap<String, Object>(portletRequest.getPortletSession().getAttributeMap()); + } + +} + +/** + * Simple workaround to define Portlet version + */ +class MockContext extends MockPortletContext { + + private int majorVersion; + + @Override + public int getMajorVersion() { + return majorVersion; + } + + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + +} +