use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class DefaultActionInvocationTester method testInvokeWithAsyncManager.
public void testInvokeWithAsyncManager() throws Exception {
DefaultActionInvocation dai = new DefaultActionInvocation(new HashMap<String, Object>(), false);
dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
final Semaphore lock = new Semaphore(1);
lock.acquire();
dai.setAsyncManager(new AsyncManager() {
Object asyncActionResult;
@Override
public boolean hasAsyncActionResult() {
return asyncActionResult != null;
}
@Override
public Object getAsyncActionResult() {
return asyncActionResult;
}
@Override
public void invokeAsyncAction(Callable asyncAction) {
try {
asyncActionResult = asyncAction.call();
} catch (Exception e) {
asyncActionResult = e;
}
lock.release();
}
});
dai.action = new Callable<Callable<String>>() {
@Override
public Callable<String> call() throws Exception {
return new Callable<String>() {
@Override
public String call() throws Exception {
return "success";
}
};
}
};
MockActionProxy actionProxy = new MockActionProxy();
actionProxy.setMethod("call");
dai.proxy = actionProxy;
final boolean[] preResultExecuted = new boolean[1];
dai.addPreResultListener(new PreResultListener() {
@Override
public void beforeResult(ActionInvocation invocation, String resultCode) {
preResultExecuted[0] = true;
}
});
List<InterceptorMapping> interceptorMappings = new ArrayList<>();
MockInterceptor mockInterceptor1 = new MockInterceptor();
mockInterceptor1.setFoo("test1");
mockInterceptor1.setExpectedFoo("test1");
interceptorMappings.add(new InterceptorMapping("test1", mockInterceptor1));
dai.interceptors = interceptorMappings.iterator();
dai.ognlUtil = new OgnlUtil();
dai.invoke();
assertTrue("interceptor1 should be executed", mockInterceptor1.isExecuted());
assertFalse("preResultListener should no be executed", preResultExecuted[0]);
assertNotNull("an async action should be saved", dai.asyncAction);
assertFalse("invocation should not be executed", dai.executed);
assertNull("a null result should be passed to upper and wait for the async result", dai.resultCode);
if (lock.tryAcquire(1500L, TimeUnit.MILLISECONDS)) {
try {
dai.invoke();
assertTrue("preResultListener should be executed", preResultExecuted[0]);
assertNull("async action should be cleared", dai.asyncAction);
assertTrue("invocation should be executed", dai.executed);
assertEquals("success", dai.resultCode);
} finally {
lock.release();
}
} else {
lock.release();
fail("async result did not received on timeout!");
}
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class RestWorkflowInterceptorTest method testCustomValidationFailureStatusCode.
public void testCustomValidationFailureStatusCode() throws Exception {
RestWorkflowInterceptor wf = new RestWorkflowInterceptor();
ActionSupport action = new ActionSupport();
action.addActionError("some error");
wf.setValidationFailureStatusCode("666");
Mock mockActionInvocation = new Mock(ActionInvocation.class);
Mock mockActionProxy = new Mock(ActionProxy.class);
mockActionProxy.expectAndReturn("getConfig", null);
mockActionInvocation.expectAndReturn("getAction", action);
Mock mockContentTypeHandlerManager = new Mock(ContentTypeHandlerManager.class);
mockContentTypeHandlerManager.expectAndReturn("handleResult", new AnyConstraintMatcher() {
public boolean matches(Object[] args) {
DefaultHttpHeaders headers = (DefaultHttpHeaders) args[1];
return 666 == headers.getStatus();
}
}, null);
wf.setContentTypeHandlerManager((ContentTypeHandlerManager) mockContentTypeHandlerManager.proxy());
ActionContext.of(new HashMap<>()).withActionMapping(new ActionMapping()).bind();
wf.doIntercept((ActionInvocation) mockActionInvocation.proxy());
mockContentTypeHandlerManager.verify();
mockActionInvocation.verify();
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class AnnotationValidationInterceptorTest method setUp.
public void setUp() throws Exception {
super.setUp();
test = new TestAction();
interceptor = new AnnotationValidationInterceptor();
container.inject(interceptor);
config = new ActionConfig.Builder("", "foo", "").build();
mockActionInvocation = new Mock(ActionInvocation.class);
mockActionProxy = new Mock(ActionProxy.class);
mockActionInvocation.matchAndReturn("getProxy", (ActionProxy) mockActionProxy.proxy());
mockActionInvocation.matchAndReturn("getAction", test);
mockActionInvocation.expect("invoke");
ActionContext.getContext().withActionInvocation((ActionInvocation) mockActionInvocation.proxy());
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class I18nInterceptorTest method setUp.
public void setUp() throws Exception {
interceptor = new I18nInterceptor();
interceptor.setLocaleProviderFactory(new DefaultLocaleProviderFactory());
interceptor.init();
session = new HashMap<>();
ac = ActionContext.of(new HashMap<>()).bind().withSession(session).withParameters(HttpParameters.create().build());
request = new MockHttpServletRequest();
request.setSession(new MockHttpSession());
ServletActionContext.setRequest(request);
Action action = () -> Action.SUCCESS;
MockActionProxy proxy = new MockActionProxy();
proxy.setAction(action);
proxy.setNamespace("i18n");
proxy.setActionName("anAction");
mai = new MockActionInvocation();
((MockActionInvocation) mai).setAction(action);
((MockActionInvocation) mai).setInvocationContext(ac);
((MockActionInvocation) mai).setProxy(proxy);
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class DispatcherTest method testServiceActionResumePreviousProxy.
public void testServiceActionResumePreviousProxy() throws Exception {
Dispatcher du = initDispatcher(Collections.<String, String>emptyMap());
MockActionInvocation mai = new MockActionInvocation();
ActionContext.getContext().withActionInvocation(mai);
MockActionProxy actionProxy = new MockActionProxy();
actionProxy.setInvocation(mai);
mai.setProxy(actionProxy);
mai.setStack(new StubValueStack());
HttpServletRequest req = new MockHttpServletRequest();
req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, mai.getStack());
assertFalse(actionProxy.isExecutedCalled());
du.setDevMode("false");
du.setHandleException("false");
du.serviceAction(req, null, new ActionMapping());
assertTrue("should execute previous proxy", actionProxy.isExecutedCalled());
}
Aggregations