use of com.opensymphony.xwork2.ognl.OgnlUtil in project struts by apache.
the class OgnlUtilTest method reloadTestContainerConfiguration.
private void reloadTestContainerConfiguration(boolean allowStaticField) {
loadConfigurationProviders(new StubConfigurationProvider() {
@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
props.setProperty(StrutsConstants.STRUTS_ALLOW_STATIC_FIELD_ACCESS, "" + allowStaticField);
}
});
ognlUtil = container.getInstance(OgnlUtil.class);
}
use of com.opensymphony.xwork2.ognl.OgnlUtil in project struts by apache.
the class DefaultActionInvocationTester method testUnknownHandlerManagerThatThrowsException.
public void testUnknownHandlerManagerThatThrowsException() 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 {
throw new NoSuchMethodException();
}
};
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
// 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.ognl.OgnlUtil in project struts by apache.
the class DefaultActionInvocationTester method testInvokingExistingMethodThatThrowsException.
public void testInvokingExistingMethodThatThrowsException() throws Exception {
// given
DefaultActionInvocation dai = new DefaultActionInvocation(ActionContext.getContext().getContextMap(), false);
container.inject(dai);
SimpleAction action = new SimpleAction() {
@Override
public String execute() throws Exception {
throw new IllegalArgumentException();
}
};
MockActionProxy proxy = new MockActionProxy();
proxy.setMethod("execute");
dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
dai.proxy = proxy;
dai.ognlUtil = new OgnlUtil();
// when
Throwable actual = null;
try {
dai.invokeAction(action, null);
} catch (Exception e) {
actual = e;
}
// then
assertNotNull(actual);
assertTrue(actual instanceof IllegalArgumentException);
}
use of com.opensymphony.xwork2.ognl.OgnlUtil 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.ognl.OgnlUtil 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!");
}
}
Aggregations