use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.
the class DefaultActionInvocationTester method testNullResultPossible.
public void testNullResultPossible() throws Exception {
ActionProxy actionProxy = actionProxyFactory.createActionProxy("", "NullFoo", "nullMethod", new HashMap<String, Object>());
DefaultActionInvocation defaultActionInvocation = (DefaultActionInvocation) actionProxy.getInvocation();
defaultActionInvocation.init(actionProxy);
String result = defaultActionInvocation.invoke();
assertNull(result);
}
use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.
the class DefaultActionInvocationTester method testUnknownHandlerManagerThatReturnsSuccess.
public void testUnknownHandlerManagerThatReturnsSuccess() 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 "success";
}
};
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
String result = dai.invokeAction(new SimpleAction(), null);
// then
assertNotNull(result);
assertEquals("success", result);
}
use of com.opensymphony.xwork2.DefaultActionInvocation 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.DefaultActionInvocation in project struts by apache.
the class InvocationSessionStore method loadInvocation.
/**
* Checks the Map in the Session for the key and the token. If the
* ActionInvocation is saved in the Session, the ValueStack from the
* ActionProxy associated with the ActionInvocation is set into the
* ActionContext and the ActionInvocation is returned.
*
* @param key the name the DefaultActionInvocation and ActionContext were saved as
* @param token token for check
* @return the DefaultActionInvocation saved using the key, or null if none was found
*/
public static ActionInvocation loadInvocation(String key, String token) {
InvocationContext invocationContext = (InvocationContext) getInvocationMap().get(key);
if ((invocationContext == null) || !invocationContext.token.equals(token)) {
return null;
}
final ActionInvocation savedInvocation = invocationContext.invocation;
if (savedInvocation != null) {
// 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 previousActionContext = ActionContext.getContext();
savedInvocation.getInvocationContext().withPageContext(previousActionContext.getPageContext()).withValueStack(savedInvocation.getStack()).bind();
}
return savedInvocation;
}
use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.
the class DefaultActionInvocationTester method testInvoke.
/**
* Tests interceptor chain invoke.
*
* @throws Exception when action throws exception
*/
public void testInvoke() throws Exception {
List<InterceptorMapping> interceptorMappings = new ArrayList<>();
MockInterceptor mockInterceptor1 = new MockInterceptor();
mockInterceptor1.setFoo("test1");
mockInterceptor1.setExpectedFoo("test1");
interceptorMappings.add(new InterceptorMapping("test1", mockInterceptor1));
MockInterceptor mockInterceptor2 = new MockInterceptor();
interceptorMappings.add(new InterceptorMapping("test2", mockInterceptor2));
mockInterceptor2.setFoo("test2");
mockInterceptor2.setExpectedFoo("test2");
MockInterceptor mockInterceptor3 = new MockInterceptor();
interceptorMappings.add(new InterceptorMapping("test3", mockInterceptor3));
mockInterceptor3.setFoo("test3");
mockInterceptor3.setExpectedFoo("test3");
DefaultActionInvocation defaultActionInvocation = new DefaultActionInvocationTester(interceptorMappings);
container.inject(defaultActionInvocation);
defaultActionInvocation.stack = container.getInstance(ValueStackFactory.class).createValueStack();
// is possible when result is not executed already
defaultActionInvocation.setResultCode("");
defaultActionInvocation.invoke();
assertTrue(mockInterceptor1.isExecuted());
assertTrue(mockInterceptor2.isExecuted());
assertTrue(mockInterceptor3.isExecuted());
assertTrue(defaultActionInvocation.isExecuted());
try {
defaultActionInvocation.setResultCode("");
fail("should not possible when result already executed");
} catch (Exception ignored) {
}
try {
defaultActionInvocation.invoke();
fail("should not possible when result already executed");
} catch (Exception ignored) {
}
}
Aggregations