Merge pull request #1072 from apache/fix/WW-5468-modeldriven-2

WW-5468 Exempt ModelDriven Actions from @StrutsParameter requirement
This commit is contained in:
Kusal Kithul-Godage
2024-10-14 18:59:11 +11:00
parent 8566c14648
commit 7cdcd84b83
17 changed files with 252 additions and 219 deletions
@@ -21,7 +21,6 @@ package org.apache.struts.beanvalidation.actions;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts.beanvalidation.models.Person;
import org.apache.struts2.interceptor.parameter.StrutsParameter;
import javax.validation.Valid;
@@ -30,7 +29,6 @@ public class ModelDrivenAction extends ActionSupport implements ModelDriven<Pers
@Valid
private final Person model = new Person();
@StrutsParameter(depth = 2)
@Override
public Person getModel() {
return model;
@@ -22,7 +22,6 @@ import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts.beanvalidation.constraints.ValidationGroup;
import org.apache.struts.beanvalidation.models.Person;
import org.apache.struts2.interceptor.parameter.StrutsParameter;
import javax.validation.Valid;
@@ -31,7 +30,6 @@ public class ValidateGroupAction extends ActionSupport implements ModelDriven<Pe
@Valid
private final Person model = new Person();
@StrutsParameter(depth = 2)
@Override
public Person getModel() {
return model;
@@ -23,7 +23,6 @@ import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionProxyFactory;
import com.opensymphony.xwork2.config.Configuration;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.springframework.mock.web.MockHttpServletRequest;
@@ -75,7 +74,7 @@ public class StrutsRestTestCase<T> extends StrutsJUnit4TestCase<T> {
ActionMapping mapping = getActionMapping(request);
assertNotNull(mapping);
Dispatcher.getInstance().serviceAction(request, response, mapping);
dispatcher.serviceAction(request, response, mapping);
if (response.getStatus() != HttpServletResponse.SC_OK)
throw new ServletException("Error code [" + response.getStatus() + "], Error: ["
@@ -32,7 +32,6 @@ import com.opensymphony.xwork2.ognl.OgnlUtil;
import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
import junit.framework.TestCase;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.parameter.StrutsParameter;
import org.apache.struts2.result.HttpHeaderResult;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -47,97 +46,97 @@ import static javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
public class RestActionInvocationTest extends TestCase {
RestActionInvocation restActionInvocation;
MockHttpServletRequest request;
MockHttpServletResponse response;
RestActionInvocation restActionInvocation;
MockHttpServletRequest request;
MockHttpServletResponse response;
@Override
protected void setUp() throws Exception {
super.setUp();
@Override
protected void setUp() throws Exception {
super.setUp();
restActionInvocation = new RestActionInvocationTester();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
ServletActionContext.setRequest(request);
ServletActionContext.setResponse(response);
restActionInvocation = new RestActionInvocationTester();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
ServletActionContext.setRequest(request);
ServletActionContext.setResponse(response);
}
}
/**
* Test the correct action results: null, String, HttpHeaders, Result
* @throws Exception
*/
public void testSaveResult() throws Exception {
/**
* Test the correct action results: null, String, HttpHeaders, Result
* @throws Exception
*/
public void testSaveResult() throws Exception {
Object methodResult = "index";
ActionConfig actionConfig = restActionInvocation.getProxy().getConfig();
assertEquals("index", restActionInvocation.saveResult(actionConfig, methodResult));
Object methodResult = "index";
ActionConfig actionConfig = restActionInvocation.getProxy().getConfig();
assertEquals("index", restActionInvocation.saveResult(actionConfig, methodResult));
setUp();
methodResult = new DefaultHttpHeaders("show");
assertEquals("show", restActionInvocation.saveResult(actionConfig, methodResult));
assertEquals(methodResult, restActionInvocation.httpHeaders);
setUp();
methodResult = new DefaultHttpHeaders("show");
assertEquals("show", restActionInvocation.saveResult(actionConfig, methodResult));
assertEquals(methodResult, restActionInvocation.httpHeaders);
setUp();
methodResult = new HttpHeaderResult(HttpServletResponse.SC_ACCEPTED);
assertEquals(null, restActionInvocation.saveResult(actionConfig, methodResult));
assertEquals(methodResult, restActionInvocation.createResult());
setUp();
methodResult = new HttpHeaderResult(HttpServletResponse.SC_ACCEPTED);
assertEquals(null, restActionInvocation.saveResult(actionConfig, methodResult));
assertEquals(methodResult, restActionInvocation.createResult());
setUp();
try {
methodResult = new Object();
restActionInvocation.saveResult(actionConfig, methodResult);
setUp();
try {
methodResult = new Object();
restActionInvocation.saveResult(actionConfig, methodResult);
// ko
assertFalse(true);
// ko
assertFalse(true);
} catch (ConfigurationException c) {
// ok, object not allowed
}
}
} catch (ConfigurationException c) {
// ok, object not allowed
}
}
/**
* Test the target selection: exception, error messages, model and null
* @throws Exception
*/
public void testSelectTarget() throws Exception {
/**
* Test the target selection: exception, error messages, model and null
* @throws Exception
*/
public void testSelectTarget() throws Exception {
// Exception
Exception e = new Exception();
restActionInvocation.getStack().set("exception", e);
restActionInvocation.selectTarget();
assertEquals(e, restActionInvocation.target);
// Exception
Exception e = new Exception();
restActionInvocation.getStack().set("exception", e);
restActionInvocation.selectTarget();
assertEquals(e, restActionInvocation.target);
// Error messages
setUp();
String actionMessage = "Error!";
RestActionSupport action = (RestActionSupport)restActionInvocation.getAction();
action.addActionError(actionMessage);
Map<String, Object> errors = new HashMap<String, Object>();
List<String> list = new ArrayList<String>();
list.add(actionMessage);
errors.put("actionErrors", list);
restActionInvocation.selectTarget();
assertEquals(errors, restActionInvocation.target);
// Error messages
setUp();
String actionMessage = "Error!";
RestActionSupport action = (RestActionSupport)restActionInvocation.getAction();
action.addActionError(actionMessage);
Map<String, Object> errors = new HashMap<String, Object>();
List<String> list = new ArrayList<String>();
list.add(actionMessage);
errors.put("actionErrors", list);
restActionInvocation.selectTarget();
assertEquals(errors, restActionInvocation.target);
// Model with get and no content in post, put, delete
setUp();
RestAction restAction = (RestAction)restActionInvocation.getAction();
List<String> model = new ArrayList<String>();
model.add("Item");
restAction.model = model;
request.setMethod("GET");
restActionInvocation.selectTarget();
assertEquals(model, restActionInvocation.target);
request.setMethod("POST");
restActionInvocation.selectTarget();
assertEquals(null, restActionInvocation.target);
request.setMethod("PUT");
restActionInvocation.selectTarget();
assertEquals(null, restActionInvocation.target);
request.setMethod("DELETE");
restActionInvocation.selectTarget();
assertEquals(null, restActionInvocation.target);
// Model with get and no content in post, put, delete
setUp();
RestAction restAction = (RestAction)restActionInvocation.getAction();
List<String> model = new ArrayList<String>();
model.add("Item");
restAction.model = model;
request.setMethod("GET");
restActionInvocation.selectTarget();
assertEquals(model, restActionInvocation.target);
request.setMethod("POST");
restActionInvocation.selectTarget();
assertEquals(null, restActionInvocation.target);
request.setMethod("PUT");
restActionInvocation.selectTarget();
assertEquals(null, restActionInvocation.target);
request.setMethod("DELETE");
restActionInvocation.selectTarget();
assertEquals(null, restActionInvocation.target);
// disable content restriction to GET only
model = new ArrayList<String>();
@@ -151,102 +150,100 @@ public class RestActionInvocationTest extends TestCase {
assertEquals(model.get(0), "Item1");
}
/**
* Test the not modified status code.
* @throws Exception
*/
public void testResultNotModified() throws Exception {
/**
* Test the not modified status code.
* @throws Exception
*/
public void testResultNotModified() throws Exception {
request.addHeader("If-None-Match", "123");
request.setMethod("GET");
request.addHeader("If-None-Match", "123");
request.setMethod("GET");
RestAction restAction = (RestAction)restActionInvocation.getAction();
List<String> model = new ArrayList<String>() {
@Override
public int hashCode() {
return 123;
}
};
model.add("Item");
restAction.model = model;
RestAction restAction = (RestAction)restActionInvocation.getAction();
List<String> model = new ArrayList<String>() {
@Override
public int hashCode() {
return 123;
}
};
model.add("Item");
restAction.model = model;
restActionInvocation.processResult();
assertEquals(SC_NOT_MODIFIED, response.getStatus());
restActionInvocation.processResult();
assertEquals(SC_NOT_MODIFIED, response.getStatus());
}
/**
* Test the default error result.
* @throws Exception
*/
public void testDefaultErrorResult() throws Exception {
/**
* Test the default error result.
* @throws Exception
*/
public void testDefaultErrorResult() throws Exception {
// Exception
Exception e = new Exception();
restActionInvocation.getStack().set("exception", e);
request.setMethod("GET");
// Exception
Exception e = new Exception();
restActionInvocation.getStack().set("exception", e);
request.setMethod("GET");
RestAction restAction = (RestAction)restActionInvocation.getAction();
List<String> model = new ArrayList<String>();
model.add("Item");
restAction.model = model;
RestAction restAction = (RestAction)restActionInvocation.getAction();
List<String> model = new ArrayList<String>();
model.add("Item");
restAction.model = model;
restActionInvocation.setDefaultErrorResultName("default-error");
ResultConfig resultConfig = new ResultConfig.Builder("default-error",
"org.apache.struts2.result.HttpHeaderResult")
.addParam("status", "123").build();
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
"RestAction", "org.apache.rest.RestAction")
.addResultConfig(resultConfig)
.build();
((MockActionProxy)restActionInvocation.getProxy()).setConfig(actionConfig);
restActionInvocation.setDefaultErrorResultName("default-error");
ResultConfig resultConfig = new ResultConfig.Builder("default-error",
"org.apache.struts2.result.HttpHeaderResult")
.addParam("status", "123").build();
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
"RestAction", "org.apache.rest.RestAction")
.addResultConfig(resultConfig)
.build();
((MockActionProxy)restActionInvocation.getProxy()).setConfig(actionConfig);
restActionInvocation.processResult();
assertEquals(123, response.getStatus());
restActionInvocation.processResult();
assertEquals(123, response.getStatus());
}
}
public void testNoResult() throws Exception {
public void testNoResult() throws Exception {
RestAction restAction = (RestAction)restActionInvocation.getAction();
List<String> model = new ArrayList<String>();
model.add("Item");
restAction.model = model;
request.setMethod("GET");
restActionInvocation.setResultCode("index");
RestAction restAction = (RestAction)restActionInvocation.getAction();
List<String> model = new ArrayList<String>();
model.add("Item");
restAction.model = model;
request.setMethod("GET");
restActionInvocation.setResultCode("index");
try {
restActionInvocation.processResult();
try {
restActionInvocation.processResult();
// ko
assertFalse(true);
// ko
assertFalse(true);
} catch (ConfigurationException c) {
// ok, no result
}
} catch (ConfigurationException c) {
// ok, no result
}
}
}
/**
* Test the global execution
* @throws Exception
*/
public void testInvoke() throws Exception {
/**
* Test the global execution
* @throws Exception
*/
public void testInvoke() throws Exception {
// Default index method return 'success'
((MockActionProxy)restActionInvocation.getProxy()).setMethod("index");
// Default index method return 'success'
((MockActionProxy)restActionInvocation.getProxy()).setMethod("index");
// Define result 'success'
ResultConfig resultConfig = new ResultConfig.Builder("success",
"org.apache.struts2.result.HttpHeaderResult")
.addParam("status", "123").build();
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
"RestAction", "org.apache.rest.RestAction")
.addResultConfig(resultConfig)
.build();
((MockActionProxy)restActionInvocation.getProxy()).setConfig(actionConfig);
// Define result 'success'
ResultConfig resultConfig = new ResultConfig.Builder("success", "org.apache.struts2.result.HttpHeaderResult")
.addParam("status", "123").build();
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest", "RestAction", "org.apache.rest.RestAction")
.addResultConfig(resultConfig)
.build();
((MockActionProxy)restActionInvocation.getProxy()).setConfig(actionConfig);
request.setMethod("GET");
request.setMethod("GET");
restActionInvocation.setOgnlUtil(new OgnlUtil());
restActionInvocation.invoke();
@@ -256,31 +253,31 @@ public class RestActionInvocationTest extends TestCase {
class RestActionInvocationTester extends RestActionInvocation {
RestActionInvocationTester() {
super(new HashMap<String, Object>(), true);
List<InterceptorMapping> interceptorMappings = new ArrayList<InterceptorMapping>();
RestActionInvocationTester() {
super(new HashMap<>(), true);
List<InterceptorMapping> interceptorMappings = new ArrayList<>();
MockInterceptor mockInterceptor = new MockInterceptor();
mockInterceptor.setFoo("interceptor");
mockInterceptor.setExpectedFoo("interceptor");
interceptorMappings.add(new InterceptorMapping("interceptor", mockInterceptor));
interceptors = interceptorMappings.iterator();
MockActionProxy actionProxy = new MockActionProxy();
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
"RestAction", "org.apache.rest.RestAction").build();
ActionConfig actionConfig = new ActionConfig.Builder(
"org.apache.rest", "RestAction", "org.apache.rest.RestAction").build();
actionProxy.setConfig(actionConfig);
proxy = actionProxy;
action = new RestAction();
setMimeTypeHandlerSelector(new DefaultContentTypeHandlerManager());
unknownHandlerManager = new DefaultUnknownHandlerManager();
try {
XWorkTestCaseHelper.setUp();
} catch (Exception e) {
throw new RuntimeException(e);
}
invocationContext = ActionContext.getContext();
container = ActionContext.getContext().getContainer();
stack = ActionContext.getContext().getValueStack();
objectFactory = container.getInstance(ObjectFactory.class);
try {
XWorkTestCaseHelper.setUp();
} catch (Exception e) {
throw new RuntimeException(e);
}
invocationContext = ActionContext.getContext();
container = ActionContext.getContext().getContainer();
stack = ActionContext.getContext().getValueStack();
objectFactory = container.getInstance(ObjectFactory.class);
}
@@ -288,13 +285,12 @@ public class RestActionInvocationTest extends TestCase {
static class RestAction extends RestActionSupport implements ModelDriven<List<String>> {
List<String> model;
List<String> model;
@StrutsParameter(depth = 1)
@Override
public List<String> getModel() {
return model;
}
@Override
public List<String> getModel() {
return model;
}
}
}
@@ -44,9 +44,8 @@ public class ModelDrivenAction extends ActionSupport implements ModelDriven {
/**
* @return the model to be pushed onto the ValueStack after the Action itself
*/
@StrutsParameter(depth = 2)
@Override
public Object getModel() {
public TestBean getModel() {
return model;
}
}