mirror of
https://github.com/apache/struts.git
synced 2026-08-06 23:27:07 +00:00
WW-3826 adds new StrutsPortletTestCase to simplify testing actions to be used in portlet environment
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1344572 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -26,7 +26,6 @@ import com.opensymphony.xwork2.ActionProxy;
|
||||
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 extends XWorkTestCase {
|
||||
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 extends XWorkTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
|
||||
+11
-7
@@ -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>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user