You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2020/04/05 08:00:05 UTC
[struts] branch action-context-boost updated: WW-4789 WW-3788
Introduces helper methods to allow build fluent API
This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a commit to branch action-context-boost
in repository https://gitbox.apache.org/repos/asf/struts.git
The following commit(s) were added to refs/heads/action-context-boost by this push:
new f22eac7 WW-4789 WW-3788 Introduces helper methods to allow build fluent API
f22eac7 is described below
commit f22eac7f0ab03f6da974e4d0d2427938390a1902
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Sun Apr 5 09:59:55 2020 +0200
WW-4789 WW-3788 Introduces helper methods to allow build fluent API
---
.../com/opensymphony/xwork2/ActionContext.java | 64 +++++++++++++++++++++-
.../xwork2/DefaultActionInvocation.java | 4 +-
.../org/apache/struts2/ServletActionContext.java | 6 +-
.../interceptor/CreateSessionInterceptor.java | 2 +-
.../struts2/interceptor/ScopeInterceptor.java | 56 +++++++++----------
.../struts2/util/InvocationSessionStore.java | 28 +++++-----
.../com/opensymphony/xwork2/ActionContextTest.java | 21 ++++---
.../validator/VisitorFieldValidatorTest.java | 37 ++++++++-----
.../struts2/result/ServletRedirectResultTest.java | 4 +-
.../views/freemarker/FreeMarkerResultTest.java | 9 +--
.../freemarker/FreemarkerResultMockedTest.java | 9 +--
.../apache/struts2/views/jsp/AbstractTagTest.java | 9 +--
.../org/apache/struts2/views/jsp/URLTagTest.java | 10 ++--
.../org/apache/struts2/EmbeddedJSPResultTest.java | 11 ++--
.../struts2/views/java/simple/AbstractTest.java | 2 +-
.../apache/struts2/json/JSONInterceptorTest.java | 13 ++---
.../org/apache/struts2/StrutsJUnit4TestCase.java | 2 +-
.../org/apache/struts2/StrutsPortletTestCase.java | 2 +-
.../java/org/apache/struts2/StrutsTestCase.java | 2 +-
.../rest/ContentTypeHandlerManagerTest.java | 4 +-
20 files changed, 182 insertions(+), 113 deletions(-)
diff --git a/core/src/main/java/com/opensymphony/xwork2/ActionContext.java b/core/src/main/java/com/opensymphony/xwork2/ActionContext.java
index 95baaab..3a0ff0b 100644
--- a/core/src/main/java/com/opensymphony/xwork2/ActionContext.java
+++ b/core/src/main/java/com/opensymphony/xwork2/ActionContext.java
@@ -62,7 +62,10 @@ public class ActionContext implements Serializable {
/**
* Constant for the name of the action being executed.
+ *
+ * @deprecated used helper methods instead
*/
+ @Deprecated
public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name";
/**
@@ -275,7 +278,9 @@ public class ActionContext implements Serializable {
* Sets the name of the current Action in the ActionContext.
*
* @param name the name of the current action.
+ * @deprecated use {@link #withActionName(String)} instead
*/
+ @Deprecated
public void setName(String name) {
put(ACTION_NAME, name);
}
@@ -290,6 +295,15 @@ public class ActionContext implements Serializable {
}
/**
+ * Gets the name of the current Action.
+ *
+ * @return the name of the current action.
+ */
+ public String getActionName() {
+ return (String) get(ACTION_NAME);
+ }
+
+ /**
* Sets the action parameters.
*
* @param parameters the parameters for the current action.
@@ -313,7 +327,9 @@ public class ActionContext implements Serializable {
* Sets a map of action session values.
*
* @param session the session values.
+ * @deprecated use {@link #withSession(Map)} instead
*/
+ @Deprecated
public void setSession(Map<String, Object> session) {
put(SESSION, session);
}
@@ -404,16 +420,19 @@ public class ActionContext implements Serializable {
return (HttpServletResponse) get(StrutsStatics.HTTP_RESPONSE);
}
- public void setServletContext(ServletContext servletContext) {
+ public ActionContext withServletContext(ServletContext servletContext) {
put(StrutsStatics.SERVLET_CONTEXT, servletContext);
+ return this;
}
- public void setServletRequest(HttpServletRequest request) {
+ public ActionContext withServletRequest(HttpServletRequest request) {
put(StrutsStatics.HTTP_REQUEST, request);
+ return this;
}
- public void setServletResponse(HttpServletResponse response) {
+ public ActionContext withServletResponse(HttpServletResponse response) {
put(StrutsStatics.HTTP_RESPONSE, response);
+ return this;
}
public PageContext getPageContext() {
@@ -431,4 +450,43 @@ public class ActionContext implements Serializable {
public void setActionMapping(ActionMapping actionMapping) {
put(StrutsStatics.ACTION_MAPPING, actionMapping);
}
+
+ public ActionContext withApplication(Map<String, Object> application) {
+ put(APPLICATION, application);
+ return this;
+ }
+
+ public ActionContext withSession(Map<String, Object> session) {
+ put(SESSION, session);
+ return this;
+ }
+
+ public ActionContext withParameters(HttpParameters parameters) {
+ put(PARAMETERS, parameters);
+ return this;
+ }
+
+ public ActionContext withActionName(String actionName) {
+ put(ACTION_NAME, actionName);
+ return this;
+ }
+
+ public ActionContext withValueStack(ValueStack valueStack) {
+ put(VALUE_STACK, valueStack);
+ return this;
+ }
+
+ public ActionContext withPageContext(PageContext pageContext) {
+ put(StrutsStatics.PAGE_CONTEXT, pageContext);
+ return this;
+ }
+
+ public ActionContext withPageContextOrClear(ActionContext actionContext) {
+ if (actionContext == null) {
+ put(StrutsStatics.PAGE_CONTEXT, null);
+ } else {
+ put(StrutsStatics.PAGE_CONTEXT, actionContext.getPageContext());
+ }
+ return this;
+ }
}
diff --git a/core/src/main/java/com/opensymphony/xwork2/DefaultActionInvocation.java b/core/src/main/java/com/opensymphony/xwork2/DefaultActionInvocation.java
index b38d8d0..c256ad5 100644
--- a/core/src/main/java/com/opensymphony/xwork2/DefaultActionInvocation.java
+++ b/core/src/main/java/com/opensymphony/xwork2/DefaultActionInvocation.java
@@ -395,8 +395,8 @@ public class DefaultActionInvocation implements ActionInvocation {
contextMap.put("action", action);
}
- invocationContext = ActionContext.of(contextMap);
- invocationContext.setName(proxy.getActionName());
+ invocationContext = ActionContext.of(contextMap)
+ .withActionName(proxy.getActionName());
createInterceptors(proxy);
diff --git a/core/src/main/java/org/apache/struts2/ServletActionContext.java b/core/src/main/java/org/apache/struts2/ServletActionContext.java
index c9840dc..24a4422 100644
--- a/core/src/main/java/org/apache/struts2/ServletActionContext.java
+++ b/core/src/main/java/org/apache/struts2/ServletActionContext.java
@@ -102,7 +102,7 @@ public class ServletActionContext implements StrutsStatics {
* @param request the HTTP servlet request object.
*/
public static void setRequest(HttpServletRequest request) {
- ActionContext.getContext().setServletRequest(request);
+ ActionContext.getContext().withServletRequest(request);
}
/**
@@ -120,7 +120,7 @@ public class ServletActionContext implements StrutsStatics {
* @param response the HTTP servlet response object.
*/
public static void setResponse(HttpServletResponse response) {
- ActionContext.getContext().setServletResponse(response);
+ ActionContext.getContext().withServletResponse(response);
}
/**
@@ -147,6 +147,6 @@ public class ServletActionContext implements StrutsStatics {
* @param servletContext The servlet context to use
*/
public static void setServletContext(ServletContext servletContext) {
- ActionContext.getContext().setServletContext(servletContext);
+ ActionContext.getContext().withServletContext(servletContext);
}
}
diff --git a/core/src/main/java/org/apache/struts2/interceptor/CreateSessionInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/CreateSessionInterceptor.java
index 9091220..b927c13 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/CreateSessionInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/CreateSessionInterceptor.java
@@ -93,7 +93,7 @@ public class CreateSessionInterceptor extends AbstractInterceptor {
if (httpSession == null) {
LOG.debug("Creating new HttpSession and new SessionMap in ServletActionContext");
servletRequest.getSession(true);
- invocation.getInvocationContext().setSession(new SessionMap<>(servletRequest));
+ invocation.getInvocationContext().withSession(new SessionMap<>(servletRequest));
}
return invocation.invoke();
}
diff --git a/core/src/main/java/org/apache/struts2/interceptor/ScopeInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ScopeInterceptor.java
index b83c7e9..08afd6e 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/ScopeInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/ScopeInterceptor.java
@@ -232,7 +232,7 @@ public class ScopeInterceptor extends AbstractInterceptor implements PreResultLi
return o;
}
- private static Map locks = new IdentityHashMap();
+ private static Map<Object, Object> locks = new IdentityHashMap<>();
static void lock(Object o, ActionInvocation invocation) throws Exception {
synchronized (o) {
@@ -262,7 +262,7 @@ public class ScopeInterceptor extends AbstractInterceptor implements PreResultLi
}
protected void after(ActionInvocation invocation, String result) throws Exception {
- Map ses = ActionContext.getContext().getSession();
+ Map<String, Object> ses = ActionContext.getContext().getSession();
if ( ses != null) {
unlock(ses);
}
@@ -271,18 +271,18 @@ public class ScopeInterceptor extends AbstractInterceptor implements PreResultLi
protected void before(ActionInvocation invocation) throws Exception {
invocation.addPreResultListener(this);
- Map ses = ActionContext.getContext().getSession();
- if (ses == null && autoCreateSession) {
- ses = new SessionMap(ServletActionContext.getRequest());
- ActionContext.getContext().setSession(ses);
+ Map<String, Object> session = ActionContext.getContext().getSession();
+ if (session == null && autoCreateSession) {
+ session = new SessionMap<>(ServletActionContext.getRequest());
+ ActionContext.getContext().withSession(session);
}
- if ( ses != null) {
- lock(ses, invocation);
+ if ( session != null) {
+ lock(session, invocation);
}
String key = getKey(invocation);
- Map app = ActionContext.getContext().getApplication();
+ Map<String, Object> app = ActionContext.getContext().getApplication();
final ValueStack stack = ActionContext.getContext().getValueStack();
LOG.debug("scope interceptor before");
@@ -304,14 +304,14 @@ public class ScopeInterceptor extends AbstractInterceptor implements PreResultLi
return;
}
- if (ses == null) {
+ if (session == null) {
LOG.debug("No HttpSession created... Cannot set session scoped variables");
return;
}
- if (session != null && (!"start".equals(type))) {
- for (String string : session) {
- Object attribute = ses.get(key + string);
+ if (this.session != null && (!"start".equals(type))) {
+ for (String string : this.session) {
+ Object attribute = session.get(key + string);
if (attribute != null) {
LOG.debug("Session scoped variable set {} = {}", string, String.valueOf(attribute));
stack.setValue(string, nullConvert(attribute));
@@ -329,38 +329,38 @@ public class ScopeInterceptor extends AbstractInterceptor implements PreResultLi
*/
public void beforeResult(ActionInvocation invocation, String resultCode) {
String key = getKey(invocation);
- Map app = ActionContext.getContext().getApplication();
+ Map<String, Object> application = ActionContext.getContext().getApplication();
final ValueStack stack = ActionContext.getContext().getValueStack();
- if (application != null)
- for (String string : application) {
+ if (this.application != null)
+ for (String string : this.application) {
Object value = stack.findValue(string);
LOG.debug("Application scoped variable saved {} = {}", string, String.valueOf(value));
//if( value != null)
- app.put(key + string, nullConvert(value));
+ application.put(key + string, nullConvert(value));
}
boolean ends = "end".equals(type);
- Map ses = ActionContext.getContext().getSession();
- if (ses != null) {
+ Map<String, Object> session = ActionContext.getContext().getSession();
+ if (session != null) {
- if (session != null) {
- for (String string : session) {
+ if (this.session != null) {
+ for (String string : this.session) {
if (ends) {
- ses.remove(key + string);
+ session.remove(key + string);
} else {
Object value = stack.findValue(string);
LOG.debug("Session scoped variable saved {} = {}", string, String.valueOf(value));
// Null value should be scoped too
//if( value != null)
- ses.put(key + string, nullConvert(value));
+ session.put(key + string, nullConvert(value));
}
}
}
- unlock(ses);
+ unlock(session);
} else {
LOG.debug("No HttpSession created... Cannot save session scoped variables.");
}
@@ -406,15 +406,15 @@ public class ScopeInterceptor extends AbstractInterceptor implements PreResultLi
* @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
*/
public String intercept(ActionInvocation invocation) throws Exception {
- String result = null;
- Map ses = ActionContext.getContext().getSession();
+ String result;
+ Map<String, Object> session = ActionContext.getContext().getSession();
before(invocation);
try {
result = invocation.invoke();
after(invocation, result);
} finally {
- if (ses != null) {
- unlock(ses);
+ if (session != null) {
+ unlock(session);
}
}
diff --git a/core/src/main/java/org/apache/struts2/util/InvocationSessionStore.java b/core/src/main/java/org/apache/struts2/util/InvocationSessionStore.java
index 2696599..795022c 100644
--- a/core/src/main/java/org/apache/struts2/util/InvocationSessionStore.java
+++ b/core/src/main/java/org/apache/struts2/util/InvocationSessionStore.java
@@ -60,15 +60,13 @@ public class InvocationSessionStore {
// WW-5026 - Preserve the previous PageContext (even if null) and restore it to the
// ActionContext after loading the savedInvocation context. The saved context's PageContext
// would already be closed at this point (causing failures if used for output).
- final ActionContext savedActionContext = savedInvocation.getInvocationContext();
final ActionContext previousActionContext = ActionContext.getContext();
- ActionContext.bind(savedActionContext);
- savedActionContext.setValueStack(savedInvocation.getStack());
- if (previousActionContext != null) {
- savedActionContext.setPageContext(previousActionContext.getPageContext());
- } else {
- savedActionContext.setPageContext(null);
- }
+
+ savedInvocation
+ .getInvocationContext()
+ .withPageContextOrClear(previousActionContext)
+ .withValueStack(savedInvocation.getStack())
+ .bind();
}
return savedInvocation;
@@ -84,13 +82,13 @@ public class InvocationSessionStore {
*/
public static void storeInvocation(String key, String token, ActionInvocation invocation) {
InvocationContext invocationContext = new InvocationContext(invocation, token);
- Map invocationMap = getInvocationMap();
+ Map<String, Object> invocationMap = getInvocationMap();
invocationMap.put(key, invocationContext);
setInvocationMap(invocationMap);
}
- static void setInvocationMap(Map invocationMap) {
- Map session = ActionContext.getContext().getSession();
+ static void setInvocationMap(Map<String, Object> invocationMap) {
+ Map<String, Object> session = ActionContext.getContext().getSession();
if (session == null) {
throw new IllegalStateException("Unable to access the session.");
@@ -99,17 +97,17 @@ public class InvocationSessionStore {
session.put(INVOCATION_MAP_KEY, invocationMap);
}
- static Map getInvocationMap() {
- Map session = ActionContext.getContext().getSession();
+ static Map<String, Object> getInvocationMap() {
+ Map<String, Object> session = ActionContext.getContext().getSession();
if (session == null) {
throw new IllegalStateException("Unable to access the session.");
}
- Map invocationMap = (Map) session.get(INVOCATION_MAP_KEY);
+ Map<String, Object> invocationMap = (Map<String, Object>) session.get(INVOCATION_MAP_KEY);
if (invocationMap == null) {
- invocationMap = new HashMap();
+ invocationMap = new HashMap<>();
setInvocationMap(invocationMap);
}
diff --git a/core/src/test/java/com/opensymphony/xwork2/ActionContextTest.java b/core/src/test/java/com/opensymphony/xwork2/ActionContextTest.java
index 8651b95..db5326d 100644
--- a/core/src/test/java/com/opensymphony/xwork2/ActionContextTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/ActionContextTest.java
@@ -34,10 +34,10 @@ import java.util.Map;
*/
public class ActionContextTest extends XWorkTestCase {
- private static final String APPLICATION_KEY = "com.opensymphony.xwork2.ActionContextTest.application";
- private static final String SESSION_KEY = "com.opensymphony.xwork2.ActionContextTest.session";
- private static final String PARAMETERS_KEY = "com.opensymphony.xwork2.ActionContextTest.params";
- private static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContextTest.actionName";
+ private static final String APPLICATION_KEY = ActionContextTest.class.getName() + ".application";
+ private static final String SESSION_KEY = ActionContextTest.class.getName() + ".session";
+ private static final String PARAMETERS_KEY = ActionContextTest.class.getName() + ".params";
+ private static final String ACTION_NAME = ActionContextTest.class.getName() + ".actionName";
private ActionContext context;
@@ -45,6 +45,7 @@ public class ActionContextTest extends XWorkTestCase {
super.setUp();
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
Map<String, Object> extraContext = valueStack.getContext();
+
Map<String, Object> application = new HashMap<>();
application.put(APPLICATION_KEY, APPLICATION_KEY);
@@ -53,11 +54,13 @@ public class ActionContextTest extends XWorkTestCase {
Map<String, Object> params = new HashMap<>();
params.put(PARAMETERS_KEY, PARAMETERS_KEY);
- extraContext.put(ActionContext.APPLICATION, application);
- extraContext.put(ActionContext.SESSION, session);
- extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
- extraContext.put(ActionContext.ACTION_NAME, ACTION_NAME);
- context = ActionContext.of(extraContext).bind();
+
+ context = ActionContext.of(extraContext)
+ .withApplication(application)
+ .withSession(session)
+ .withParameters(HttpParameters.create(params).build())
+ .withActionName(ACTION_NAME)
+ .bind();
}
public void testContextParams() {
diff --git a/core/src/test/java/com/opensymphony/xwork2/validator/VisitorFieldValidatorTest.java b/core/src/test/java/com/opensymphony/xwork2/validator/VisitorFieldValidatorTest.java
index 7116753..e2642bb 100644
--- a/core/src/test/java/com/opensymphony/xwork2/validator/VisitorFieldValidatorTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/validator/VisitorFieldValidatorTest.java
@@ -18,19 +18,27 @@
*/
package com.opensymphony.xwork2.validator;
-import com.opensymphony.xwork2.*;
+import com.opensymphony.xwork2.Action;
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.ActionProxy;
+import com.opensymphony.xwork2.TestBean;
+import com.opensymphony.xwork2.XWorkTestCase;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.conversion.impl.ConversionData;
-
import org.easymock.EasyMock;
-import java.util.*;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
/**
* VisitorFieldValidatorTest
*
* @author Jason Carreira
- * Created Aug 4, 2003 1:26:01 AM
+ * Created Aug 4, 2003 1:26:01 AM
*/
public class VisitorFieldValidatorTest extends XWorkTestCase {
@@ -43,7 +51,7 @@ public class VisitorFieldValidatorTest extends XWorkTestCase {
action = container.inject(VisitorValidatorTestAction.class);
TestBean bean = action.getBean();
- Calendar cal = new GregorianCalendar(1900, 1, 1);
+ Calendar cal = new GregorianCalendar(1900, Calendar.FEBRUARY, 1);
bean.setBirth(cal.getTime());
bean.setCount(-1);
@@ -56,7 +64,7 @@ public class VisitorFieldValidatorTest extends XWorkTestCase {
EasyMock.expect(invocation.invoke()).andReturn(Action.SUCCESS).anyTimes();
EasyMock.expect(proxy.getMethod()).andReturn("execute").anyTimes();
EasyMock.expect(proxy.getConfig()).andReturn(config).anyTimes();
-
+
EasyMock.replay(invocation);
EasyMock.replay(proxy);
@@ -106,8 +114,8 @@ public class VisitorFieldValidatorTest extends XWorkTestCase {
}
public void testCollectionValidation() throws Exception {
- List testBeanList = action.getTestBeanList();
- TestBean testBean = (TestBean) testBeanList.get(0);
+ List<TestBean> testBeanList = action.getTestBeanList();
+ TestBean testBean = testBeanList.get(0);
testBean.setName("foo");
validate("validateList");
@@ -140,7 +148,7 @@ public class VisitorFieldValidatorTest extends XWorkTestCase {
assertEquals(3, fieldErrors.size());
assertTrue(fieldErrors.containsKey("bean.count"));
assertTrue(fieldErrors.containsKey("bean.name"));
- assertTrue(!fieldErrors.containsKey("bean.birth"));
+ assertFalse(fieldErrors.containsKey("bean.birth"));
//the error from the action should be there too
assertTrue(fieldErrors.containsKey("context"));
@@ -152,7 +160,7 @@ public class VisitorFieldValidatorTest extends XWorkTestCase {
Map<String, List<String>> fieldErrors = action.getFieldErrors();
assertEquals(3, fieldErrors.size());
- assertTrue(!fieldErrors.containsKey("bean.count"));
+ assertFalse(fieldErrors.containsKey("bean.count"));
assertTrue(fieldErrors.containsKey("bean.name"));
assertTrue(fieldErrors.containsKey("bean.birth"));
@@ -166,13 +174,13 @@ public class VisitorFieldValidatorTest extends XWorkTestCase {
Map<String, List<String>> fieldErrors = action.getFieldErrors();
assertEquals(5, fieldErrors.size());
- assertTrue(!fieldErrors.containsKey("bean.count"));
+ assertFalse(fieldErrors.containsKey("bean.count"));
assertTrue(fieldErrors.containsKey("bean.name"));
assertTrue(fieldErrors.containsKey("bean.birth"));
assertTrue(fieldErrors.containsKey("bean.child.name"));
assertTrue(fieldErrors.containsKey("bean.child.birth"));
-
+
//the error from the action should be there too
assertTrue(fieldErrors.containsKey("context"));
}
@@ -188,7 +196,7 @@ public class VisitorFieldValidatorTest extends XWorkTestCase {
Map<String, List<String>> fieldErrors = action.getFieldErrors();
assertEquals(6, fieldErrors.size());
- assertTrue(!fieldErrors.containsKey("bean.count"));
+ assertFalse(fieldErrors.containsKey("bean.count"));
assertTrue(fieldErrors.containsKey("bean.name"));
assertTrue(fieldErrors.containsKey("bean.birth"));
@@ -209,8 +217,7 @@ public class VisitorFieldValidatorTest extends XWorkTestCase {
}
private void validate(String context) throws ValidationException {
- ActionContext actionContext = ActionContext.getContext();
- actionContext.setName(context);
+ ActionContext.getContext().withActionName(context);
container.getInstance(ActionValidatorManager.class).validate(action, context);
}
}
diff --git a/core/src/test/java/org/apache/struts2/result/ServletRedirectResultTest.java b/core/src/test/java/org/apache/struts2/result/ServletRedirectResultTest.java
index 67cd45d..a1c046a 100644
--- a/core/src/test/java/org/apache/struts2/result/ServletRedirectResultTest.java
+++ b/core/src/test/java/org/apache/struts2/result/ServletRedirectResultTest.java
@@ -455,8 +455,8 @@ public class ServletRedirectResultTest extends StrutsInternalTestCase implements
.addResultConfigs(results).build();
ActionContext ac = ActionContext.getContext();
- ac.setServletRequest((HttpServletRequest) requestMock.proxy());
- ac.setServletResponse((HttpServletResponse) responseMock.proxy());
+ ac.withServletRequest((HttpServletRequest) requestMock.proxy());
+ ac.withServletResponse((HttpServletResponse) responseMock.proxy());
MockActionInvocation ai = new MockActionInvocation();
ai.setInvocationContext(ac);
diff --git a/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java b/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java
index 0e386cb..935d2ae 100644
--- a/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java
+++ b/core/src/test/java/org/apache/struts2/views/freemarker/FreeMarkerResultTest.java
@@ -134,10 +134,11 @@ public class FreeMarkerResultTest extends StrutsInternalTestCase {
servletContext = new StrutsMockServletContext();
stack = ActionContext.getContext().getValueStack();
- context = ActionContext.of(stack.getContext()).bind();
- context.setServletResponse(response);
- context.setServletRequest(request);
- context.setServletContext(servletContext);
+ context = ActionContext.of(stack.getContext())
+ .withServletResponse(response)
+ .withServletRequest(request)
+ .withServletContext(servletContext)
+ .bind();
servletContext.setAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY, null);
diff --git a/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java b/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java
index cfc3e67..cd3ce51 100644
--- a/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java
+++ b/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java
@@ -246,10 +246,11 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase {
request = new MockHttpServletRequest();
stack = ActionContext.getContext().getValueStack();
- context = ActionContext.of(stack.getContext()).bind();
- context.setServletResponse(response);
- context.setServletRequest(request);
- context.setServletContext(servletContext);
+ context = ActionContext.of(stack.getContext())
+ .withServletResponse(response)
+ .withServletRequest(request)
+ .withServletContext(servletContext)
+ .bind();
servletContext.setAttribute(FreemarkerManager.CONFIG_SERVLET_CONTEXT_KEY, null);
diff --git a/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java
index eb410ac..75fb008 100644
--- a/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java
+++ b/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java
@@ -121,10 +121,11 @@ public abstract class AbstractTagTest extends StrutsInternalTestCase {
extraContext.remove(ActionContext.LOCALE);
stack.getContext().putAll(extraContext);
- ActionContext actionContext = ActionContext.of(context).bind();
- actionContext.setServletRequest(request);
- actionContext.setServletResponse(response);
- actionContext.setServletContext(servletContext);
+ ActionContext actionContext = ActionContext.of(context)
+ .withServletRequest(request)
+ .withServletResponse(response)
+ .withServletContext(servletContext)
+ .bind();
}
protected void tearDown() throws Exception {
diff --git a/core/src/test/java/org/apache/struts2/views/jsp/URLTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/URLTagTest.java
index 4fb3b0d..50f7811 100644
--- a/core/src/test/java/org/apache/struts2/views/jsp/URLTagTest.java
+++ b/core/src/test/java/org/apache/struts2/views/jsp/URLTagTest.java
@@ -556,11 +556,11 @@ public class URLTagTest extends AbstractUITagTest {
extraContext.remove(ActionContext.LOCALE);
stack.getContext().putAll(extraContext);
- ActionContext actionContext = ActionContext.of(context).bind();
- actionContext.setServletRequest(request);
- actionContext.setServletResponse(response);
- actionContext.setServletContext(servletContext);
-
+ ActionContext actionContext = ActionContext.of(context)
+ .withServletRequest(request)
+ .withServletResponse(response)
+ .withServletContext(servletContext)
+ .bind();
// Make sure we have an action invocation available
ActionContext.getContext().setActionInvocation(new DefaultActionInvocation(null, true));
diff --git a/plugins/embeddedjsp/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java b/plugins/embeddedjsp/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java
index f0e42fe..4fc21e2 100644
--- a/plugins/embeddedjsp/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java
+++ b/plugins/embeddedjsp/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java
@@ -329,11 +329,12 @@ public class EmbeddedJSPResultTest extends TestCase {
EasyMock.replay(request);
- ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
- actionContext.setParameters(HttpParameters.create(params).build());
- actionContext.setServletRequest(request);
- actionContext.setServletResponse(response);
- actionContext.setServletContext(context);
+ ActionContext actionContext = ActionContext.of(new HashMap<>())
+ .withParameters(HttpParameters.create(params).build())
+ .withServletRequest(request)
+ .withServletResponse(response)
+ .withServletContext(context)
+ .bind();
//mock value stack
Map<String, Object> stackContext = new HashMap<>();
diff --git a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java
index 322f1de..aa14c67 100644
--- a/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java
+++ b/plugins/javatemplates/src/test/java/org/apache/struts2/views/java/simple/AbstractTest.java
@@ -122,7 +122,7 @@ public abstract class AbstractTest extends TestCase {
replay(container);
ActionContext actionContext = ActionContext.of(stackContext).bind();
- actionContext.setServletRequest(request);
+ actionContext.withServletRequest(request);
}
protected static String s(String input) {
diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
index 134c6fb..48cfcb5 100644
--- a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
+++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java
@@ -22,7 +22,6 @@ import java.util.Calendar;
import java.util.List;
import java.util.Map;
-import org.apache.struts2.StrutsStatics;
import org.apache.struts2.StrutsTestCase;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -517,15 +516,15 @@ public class JSONInterceptorTest extends StrutsTestCase {
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
- ActionContext context = ActionContext.getContext();
- ValueStack stack = context.getValueStack();
+ MockServletContext servletContext = new MockServletContext();
- context.setServletRequest(request);
- context.setServletResponse(response);
+ ActionContext context = ActionContext.getContext()
+ .withServletRequest(request)
+ .withServletResponse(response)
+ .withServletContext(servletContext);
- MockServletContext servletContext = new MockServletContext();
+ ValueStack stack = context.getValueStack();
- context.setServletContext(servletContext);
this.invocation = new MockActionInvocationEx();
this.invocation.setInvocationContext(context);
this.invocation.setStack(stack);
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 75ce279..634cc0a 100644
--- a/plugins/junit/src/main/java/org/apache/struts2/StrutsJUnit4TestCase.java
+++ b/plugins/junit/src/main/java/org/apache/struts2/StrutsJUnit4TestCase.java
@@ -143,7 +143,7 @@ public abstract class StrutsJUnit4TestCase<T> extends XWorkJUnit4TestCase {
protected void initSession(ActionContext actionContext) {
if (actionContext.getSession() == null) {
- actionContext.setSession(new HashMap<String, Object>());
+ actionContext.withSession(new HashMap<>());
request.setSession(new MockHttpSession(servletContext));
}
}
diff --git a/plugins/junit/src/main/java/org/apache/struts2/StrutsPortletTestCase.java b/plugins/junit/src/main/java/org/apache/struts2/StrutsPortletTestCase.java
index 624a186..b5b4309 100644
--- a/plugins/junit/src/main/java/org/apache/struts2/StrutsPortletTestCase.java
+++ b/plugins/junit/src/main/java/org/apache/struts2/StrutsPortletTestCase.java
@@ -71,7 +71,7 @@ public abstract class StrutsPortletTestCase extends StrutsTestCase {
portletResponse = new MockStateAwareResponse();
portletSession = new MockPortletSession();
portletRequest.setSession(portletSession);
- actionContext.setSession(createSession());
+ actionContext.withSession(createSession());
actionContext.put(PortletConstants.REQUEST, portletRequest);
actionContext.put(PortletConstants.RESPONSE, portletResponse);
actionContext.put(PortletConstants.MODE_NAMESPACE_MAP, new HashMap<PortletMode, String>());
diff --git a/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java b/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java
index 808ef63..8451515 100644
--- a/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java
+++ b/plugins/junit/src/main/java/org/apache/struts2/StrutsTestCase.java
@@ -127,7 +127,7 @@ public abstract class StrutsTestCase extends XWorkTestCase {
protected void initSession(ActionContext actionContext) {
if (actionContext.getSession() == null) {
- actionContext.setSession(new HashMap<String, Object>());
+ actionContext.withSession(new HashMap<>());
request.setSession(new MockHttpSession(servletContext));
}
}
diff --git a/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java b/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java
index a55d0d2..3e2a41e 100644
--- a/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java
+++ b/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java
@@ -59,8 +59,8 @@ public class ContentTypeHandlerManagerTest extends TestCase {
mockRequest = new MockHttpServletRequest();
mockRequest.setMethod("GET");
ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
- actionContext.setServletRequest(mockRequest);
- actionContext.setServletResponse(mockResponse);
+ actionContext.withServletRequest(mockRequest);
+ actionContext.withServletResponse(mockResponse);
invocation = new MockActionInvocation();
invocation.setProxy(new MockActionProxy());