Skip to content

Commit

Permalink
initialize session
Browse files Browse the repository at this point in the history
See also WW-4921
  • Loading branch information
yasserzamani committed Feb 27, 2018
1 parent b4ca6ce commit b806c11
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockPageContext;
import org.springframework.mock.web.MockServletContext;

Expand Down Expand Up @@ -155,10 +156,7 @@ protected ActionProxy getActionProxy(String uri) {
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, new HashMap<String, Object>(), true, false);

ActionContext invocationContext = proxy.getInvocation().getInvocationContext();
invocationContext.setParameters(HttpParameters.create(request.getParameterMap()).build());
// set the action context to the one used by the proxy
ActionContext.setContext(invocationContext);
initActionContext(proxy.getInvocation().getInvocationContext());

// this is normally done in onSetUp(), but we are using Struts internal
// objects (proxy and action invocation)
Expand All @@ -170,6 +168,20 @@ protected ActionProxy getActionProxy(String uri) {
return proxy;
}

private void initActionContext(ActionContext actionContext) {
actionContext.setParameters(HttpParameters.create(request.getParameterMap()).build());
initSession(actionContext);
// set the action context to the one used by the proxy
ActionContext.setContext(actionContext);
}

private void initSession(ActionContext actionContext) {
if (actionContext.getSession() == null) {
actionContext.setSession(new HashMap<String, Object>());
request.setSession(new MockHttpSession(servletContext));
}
}

/**
* Finds an ActionMapping for a given request
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.struts2;

import com.opensymphony.xwork2.ActionProxy;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -33,6 +34,13 @@ public void testExecuteActionAgainstCustomStrutsConfigFile() throws Exception {
Assert.assertEquals("Test-2", output);
}

@Test
public void testSessionInitialized() throws Exception {
ActionProxy proxy = getActionProxy("/test/testAction-2.action");
Assert.assertNotNull("invocation session should being initialized",
proxy.getInvocation().getInvocationContext().getSession());
}

@Override
protected String getConfigPath() {
return "struts-test.xml";
Expand Down

0 comments on commit b806c11

Please sign in to comment.