use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class DefaultActionInvocationTester method testInvokingExistingExecuteMethod.
public void testInvokingExistingExecuteMethod() throws Exception {
// given
DefaultActionInvocation dai = new DefaultActionInvocation(ActionContext.getContext().getContextMap(), false);
container.inject(dai);
SimpleAction action = new SimpleAction() {
@Override
public String execute() throws Exception {
return SUCCESS;
}
};
MockActionProxy proxy = new MockActionProxy();
proxy.setMethod("execute");
dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
dai.proxy = proxy;
dai.ognlUtil = new OgnlUtil();
// when
String result = dai.invokeAction(action, null);
// then
assertEquals("success", result);
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class DefaultActionInvocationTester method testUnknownHandlerManagerThatReturnsNull.
public void testUnknownHandlerManagerThatReturnsNull() throws Exception {
// given
DefaultActionInvocation dai = new DefaultActionInvocation(ActionContext.getContext().getContextMap(), false);
container.inject(dai);
UnknownHandlerManager uhm = new DefaultUnknownHandlerManager() {
@Override
public boolean hasUnknownHandlers() {
return true;
}
@Override
public Object handleUnknownMethod(Object action, String methodName) throws NoSuchMethodException {
return null;
}
};
MockActionProxy proxy = new MockActionProxy();
proxy.setMethod("notExists");
dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
dai.proxy = proxy;
dai.ognlUtil = new OgnlUtil();
dai.unknownHandlerManager = uhm;
// when
Throwable actual = null;
try {
dai.invokeAction(new SimpleAction(), null);
} catch (Exception e) {
actual = e;
}
// then
assertNotNull(actual);
assertTrue(actual instanceof NoSuchMethodException);
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class ContentTypeHandlerManagerTest method testHandleResultOK.
public void testHandleResultOK() throws IOException {
String obj = "mystring";
ContentTypeHandler handler = new AbstractContentTypeHandler() {
public void toObject(ActionInvocation invocation, Reader in, Object target) {
}
public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer stream) throws IOException {
stream.write(obj.toString());
return resultCode;
}
public String getContentType() {
return "foo";
}
public String getExtension() {
return "foo";
}
};
mgr.handlersByExtension.put("xml", handler);
mgr.setDefaultExtension("xml");
ActionConfig actionConfig = new ActionConfig.Builder("", "", "").build();
MockActionProxy proxy = new MockActionProxy();
proxy.setConfig(actionConfig);
invocation.setProxy(proxy);
mgr.handleResult(invocation, new DefaultHttpHeaders().withStatus(SC_OK), obj);
assertEquals(obj.getBytes().length, mockResponse.getContentLength());
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class ContentTypeHandlerManagerTest method setUp.
@Override
public void setUp() {
mgr = new DefaultContentTypeHandlerManager();
mockResponse = new MockHttpServletResponse();
mockRequest = new MockHttpServletRequest();
mockRequest.setMethod("GET");
ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
actionContext.withServletRequest(mockRequest);
actionContext.withServletResponse(mockResponse);
invocation = new MockActionInvocation();
invocation.setProxy(new MockActionProxy());
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class RestActionInvocationTest method testInvoke.
/**
* Test the global execution
* @throws Exception
*/
public void testInvoke() throws Exception {
// 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);
request.setMethod("GET");
restActionInvocation.setOgnlUtil(new OgnlUtil());
restActionInvocation.invoke();
assertEquals(123, response.getStatus());
}
Aggregations