Author: lukaszlenart Date: Sun Jan 1 17:29:25 2012 New Revision: 1226265 URL: http://svn.apache.org/viewvc?rev=1226265&view=rev Log: WW-3733 - adds missing PortletContext to ActionContext to avoid NPE during PortletResult initialisation
Modified: struts/struts2/trunk/plugins/junit/pom.xml struts/struts2/trunk/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsTestCaseTest.java struts/struts2/trunk/plugins/portlet/pom.xml struts/struts2/trunk/pom.xml Modified: struts/struts2/trunk/plugins/junit/pom.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/pom.xml?rev=1226265&r1=1226264&r2=1226265&view=diff ============================================================================== --- struts/struts2/trunk/plugins/junit/pom.xml (original) +++ struts/struts2/trunk/plugins/junit/pom.xml Sun Jan 1 17:29:25 2012 @@ -55,7 +55,6 @@ <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> - <version>${project.version}</version> <optional>true</optional> </dependency> <dependency> @@ -67,5 +66,11 @@ <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> + <!-- Portlet --> + <dependency> + <groupId>javax.portlet</groupId> + <artifactId>portlet-api</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> 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=1226265&r1=1226264&r2=1226265&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 Sun Jan 1 17:29:25 2012 @@ -37,6 +37,7 @@ import org.springframework.mock.web.Mock import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockPageContext; import org.springframework.mock.web.MockServletContext; +import org.springframework.mock.web.portlet.MockPortletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -46,7 +47,11 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; -import java.util.logging.*; +import java.util.logging.ConsoleHandler; +import java.util.logging.Formatter; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; /** * Base test case for JUnit testing Struts. @@ -57,7 +62,7 @@ public abstract class StrutsTestCase ext protected MockPageContext pageContext; protected MockServletContext servletContext; protected Map<String, String> dispatcherInitParams; - + protected DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); static { @@ -109,10 +114,9 @@ public abstract class StrutsTestCase ext assertNotNull(mapping); Dispatcher.getInstance().serviceAction(request, response, servletContext, mapping); - if (response.getStatus() != HttpServletResponse.SC_OK) - throw new ServletException("Error code [" + response.getStatus() + "], Error: [" - + response.getErrorMessage() + "]"); - + if (response.getStatus() != HttpServletResponse.SC_OK) { + throw new ServletException("Error code [" + response.getStatus() + "], Error: [" + response.getErrorMessage() + "]"); + } return response.getContentAsString(); } @@ -131,12 +135,9 @@ public abstract class StrutsTestCase ext ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy( namespace, name, method, new HashMap<String, Object>(), true, false); - ActionContext invocationContext = proxy.getInvocation().getInvocationContext(); - invocationContext.setParameters(new HashMap(request.getParameterMap())); - // set the action context to the one used by the proxy - ActionContext.setContext(invocationContext); + initActionContext(proxy.getInvocation().getInvocationContext()); - // this is normaly done in onSetUp(), but we are using Struts internal + // this is normally done in onSetUp(), but we are using Struts internal // objects (proxy and action invocation) // so we have to hack around so it works ServletActionContext.setServletContext(servletContext); @@ -146,6 +147,29 @@ public abstract class StrutsTestCase ext return proxy; } + private void initActionContext(ActionContext actionContext) { + actionContext.setParameters(new HashMap(request.getParameterMap())); + + initMockPortletContext(actionContext); + applyAdditionalParams(actionContext); + + // set the action context to the one used by the proxy + ActionContext.setContext(actionContext); + } + + private void initMockPortletContext(ActionContext actionContext) { + actionContext.put(StrutsStatics.STRUTS_PORTLET_CONTEXT, new MockPortletContext()); + } + + /** + * Can be overwritten in subclass to provide additional context's params and settings used during action invocation + * + * @param context current {@link ActionContext} + */ + protected void applyAdditionalParams(ActionContext context) { + // empty be default + } + /** * Finds an ActionMapping for a given request */ @@ -179,10 +203,16 @@ public abstract class StrutsTestCase ext super.setUp(); initServletMockObjects(); setupBeforeInitDispatcher(); - initDispatcher(dispatcherInitParams); + Dispatcher dispatcher = initDispatcher(dispatcherInitParams); + setupAfterInitDispatcher(dispatcher); } protected void setupBeforeInitDispatcher() throws Exception { + // empty by default + } + + protected void setupAfterInitDispatcher(Dispatcher dispatcher) { + // empty by default } protected void initServletMockObjects() { Modified: struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsTestCaseTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsTestCaseTest.java?rev=1226265&r1=1226264&r2=1226265&view=diff ============================================================================== --- struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsTestCaseTest.java (original) +++ struts/struts2/trunk/plugins/junit/src/test/java/org/apache/struts2/StrutsTestCaseTest.java Sun Jan 1 17:29:25 2012 @@ -21,13 +21,17 @@ package org.apache.struts2; import com.opensymphony.xwork2.Action; +import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionProxy; import org.apache.struts2.dispatcher.mapper.ActionMapping; +import org.junit.Test; +import javax.portlet.PortletContext; import javax.servlet.ServletException; import java.io.UnsupportedEncodingException; public class StrutsTestCaseTest extends StrutsSpringTestCase { + public void testGetActionMapping() { ActionMapping mapping = getActionMapping("/test/testAction.action"); assertNotNull(mapping); @@ -61,4 +65,38 @@ public class StrutsTestCaseTest extends String name = (String) findValueAfterExecute("name"); assertEquals("FD", name); } + + @Test + public void shouldPortletContextBeAvailable() throws Exception { + // given + assertNull(ActionContext.getContext().get(StrutsStatics.STRUTS_PORTLET_CONTEXT)); + + // when + String output = executeAction("/test/testAction.action"); + assertEquals("Hello", output); + + // then + Object portletContext = ActionContext.getContext().get(StrutsStatics.STRUTS_PORTLET_CONTEXT); + assertNotNull(portletContext); + assertTrue(portletContext instanceof PortletContext); + } + + @Test + public void shouldAdditionalContextParamsBeAvailable() throws Exception { + // given + String key = "my-param"; + assertNull(ActionContext.getContext().get(key)); + + // when + String output = executeAction("/test/testAction.action"); + assertEquals("Hello", output); + + // then + assertNotNull(ActionContext.getContext().get(key)); + } + + @Override + protected void applyAdditionalParams(ActionContext context) { + context.put("my-param", new Object()); + } } Modified: struts/struts2/trunk/plugins/portlet/pom.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/pom.xml?rev=1226265&r1=1226264&r2=1226265&view=diff ============================================================================== --- struts/struts2/trunk/plugins/portlet/pom.xml (original) +++ struts/struts2/trunk/plugins/portlet/pom.xml Sun Jan 1 17:29:25 2012 @@ -18,9 +18,8 @@ <dependencies> <dependency> - <groupId>${project.groupId}</groupId> + <groupId>org.apache.struts</groupId> <artifactId>struts2-junit-plugin</artifactId> - <version>${project.version}</version> <scope>test</scope> </dependency> Modified: struts/struts2/trunk/pom.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/pom.xml?rev=1226265&r1=1226264&r2=1226265&view=diff ============================================================================== --- struts/struts2/trunk/pom.xml (original) +++ struts/struts2/trunk/pom.xml Sun Jan 1 17:29:25 2012 @@ -352,6 +352,11 @@ <artifactId>struts2-dwr-plugin</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.struts</groupId> + <artifactId>struts2-junit-plugin</artifactId> + <version>${project.version}</version> + </dependency> <dependency> <groupId>org.freemarker</groupId>