[ 
https://issues.apache.org/jira/browse/WW-4921?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16378405#comment-16378405
 ] 

ASF GitHub Bot commented on WW-4921:
------------------------------------

lukaszlenart closed pull request #214: WW-4921 initialize session like WW-3442
URL: https://github.com/apache/struts/pull/214
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/plugins/junit/src/main/java/org/apache/struts2/StrutsJUnit4TestCase.java 
b/plugins/junit/src/main/java/org/apache/struts2/StrutsJUnit4TestCase.java
index b39349d60..9a917e3cf 100644
--- a/plugins/junit/src/main/java/org/apache/struts2/StrutsJUnit4TestCase.java
+++ b/plugins/junit/src/main/java/org/apache/struts2/StrutsJUnit4TestCase.java
@@ -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;
 
@@ -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)
@@ -170,6 +168,20 @@ protected ActionProxy getActionProxy(String uri) {
         return proxy;
     }
 
+    protected 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);
+    }
+
+    protected 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
      */
diff --git 
a/plugins/junit/src/test/java/org/apache/struts2/StrutsJUnit4TestCaseTest.java 
b/plugins/junit/src/test/java/org/apache/struts2/StrutsJUnit4TestCaseTest.java
index d3e0d15d3..8bc691522 100644
--- 
a/plugins/junit/src/test/java/org/apache/struts2/StrutsJUnit4TestCaseTest.java
+++ 
b/plugins/junit/src/test/java/org/apache/struts2/StrutsJUnit4TestCaseTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.struts2;
 
+import com.opensymphony.xwork2.ActionProxy;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -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";


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> NPE in I18nInterceptor$SessionLocaleHandler.read
> ------------------------------------------------
>
>                 Key: WW-4921
>                 URL: https://issues.apache.org/jira/browse/WW-4921
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Core Interceptors
>            Reporter: Michael Hintenaus
>            Assignee: Yasser Zamani
>            Priority: Major
>              Labels: test
>             Fix For: 2.5.16
>
>
> Calling
> {code:java}
> getActionProxy("/index.action").getInvocation().invoke(){code}
> in a TestCase which extends from StrutsJUnit4TestCase will lead to a 
> NullPointerException.
> Overriding getActionProxy like this will help:
> {code:java}
>  @Override
>     protected ActionProxy getActionProxy(final String uri) {
>         final ActionProxy proxy = super.getActionProxy(uri);
>         final ActionContext context = 
> proxy.getInvocation().getInvocationContext();
>         if (context.getSession() == null) {
>             context.setSession(new SessionMap<>(request));            
>         }
>         return proxy;
>     }{code}
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to