use of com.opensymphony.xwork2.Result 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.Result in project struts by apache.
the class XmlConfigurationProviderActionsTest method testActions.
public void testActions() throws Exception {
final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml";
ConfigurationProvider provider = buildConfigurationProvider(filename);
// setup expectations
// bar action is very simple, just two params
params.put("foo", "17");
params.put("bar", "23");
params.put("testXW412", "foo.jspa?fooID=${fooID}&something=bar");
params.put("testXW412Again", "something");
ActionConfig barAction = new ActionConfig.Builder("", "Bar", SimpleAction.class.getName()).addParams(params).build();
// foo action is a little more complex, two params, a result and an interceptor stack
results = new HashMap<>();
params = new HashMap<>();
params.put("foo", "18");
params.put("bar", "24");
results.put("success", new ResultConfig.Builder("success", MockResult.class.getName()).build());
InterceptorConfig noopInterceptorConfig = new InterceptorConfig.Builder("noop", NoOpInterceptor.class.getName()).build();
interceptors.add(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptorConfig, new HashMap<String, String>())));
ActionConfig fooAction = new ActionConfig.Builder("", "Foo", SimpleAction.class.getName()).addParams(params).addResultConfigs(results).addInterceptors(interceptors).build();
// wildcard action is simple wildcard example
results = new HashMap<>();
results.put("*", new ResultConfig.Builder("*", MockResult.class.getName()).build());
ActionConfig wildcardAction = new ActionConfig.Builder("", "WildCard", SimpleAction.class.getName()).addResultConfigs(results).addInterceptors(interceptors).build();
// fooBar action is a little more complex, two params, a result and an interceptor stack
params = new HashMap<String, String>();
params.put("foo", "18");
params.put("bar", "24");
results = new HashMap<>();
results.put("success", new ResultConfig.Builder("success", MockResult.class.getName()).build());
ExceptionMappingConfig exceptionConfig = new ExceptionMappingConfig.Builder("runtime", "java.lang.RuntimeException", "exception").build();
exceptionMappings.add(exceptionConfig);
ActionConfig fooBarAction = new ActionConfig.Builder("", "FooBar", SimpleAction.class.getName()).addParams(params).addResultConfigs(results).addInterceptors(interceptors).addExceptionMappings(exceptionMappings).build();
// TestInterceptorParam action tests that an interceptor worked
HashMap<String, String> interceptorParams = new HashMap<>();
interceptorParams.put("expectedFoo", "expectedFooValue");
interceptorParams.put("foo", MockInterceptor.DEFAULT_FOO_VALUE);
InterceptorConfig mockInterceptorConfig = new InterceptorConfig.Builder("test", MockInterceptor.class.getName()).build();
interceptors = new ArrayList<>();
interceptors.add(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptorConfig, interceptorParams)));
ActionConfig intAction = new ActionConfig.Builder("", "TestInterceptorParam", SimpleAction.class.getName()).addInterceptors(interceptors).build();
// TestInterceptorParamOverride action tests that an interceptor with a param override worked
interceptorParams = new HashMap<>();
interceptorParams.put("expectedFoo", "expectedFooValue");
interceptorParams.put("foo", "foo123");
interceptors = new ArrayList<>();
interceptors.add(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptorConfig, interceptorParams)));
ActionConfig intOverAction = new ActionConfig.Builder("", "TestInterceptorParamOverride", SimpleAction.class.getName()).addInterceptors(interceptors).build();
// execute the configuration
provider.init(configuration);
provider.loadPackages();
PackageConfig pkg = configuration.getPackageConfig("default");
Map actionConfigs = pkg.getActionConfigs();
// assertions
assertEquals(7, actionConfigs.size());
assertEquals(barAction, actionConfigs.get("Bar"));
assertEquals(fooAction, actionConfigs.get("Foo"));
assertEquals(wildcardAction, actionConfigs.get("WildCard"));
assertEquals(fooBarAction, actionConfigs.get("FooBar"));
assertEquals(intAction, actionConfigs.get("TestInterceptorParam"));
assertEquals(intOverAction, actionConfigs.get("TestInterceptorParamOverride"));
}
use of com.opensymphony.xwork2.Result in project struts by apache.
the class ChainResultTest method testNamespaceAndActionExpressionEvaluation.
public void testNamespaceAndActionExpressionEvaluation() throws Exception {
ActionChainResult result = new ActionChainResult();
result.setActionName("${actionName}");
result.setNamespace("${namespace}");
String expectedActionName = "testActionName";
String expectedNamespace = "testNamespace";
Map<String, Object> values = new HashMap<>();
values.put("actionName", expectedActionName);
values.put("namespace", expectedNamespace);
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(values);
Mock actionProxyMock = new Mock(ActionProxy.class);
actionProxyMock.matchAndReturn("getActionName", expectedActionName);
actionProxyMock.matchAndReturn("getMethod", "execute");
actionProxyMock.expect("execute");
ActionProxyFactory testActionProxyFactory = new NamespaceActionNameTestActionProxyFactory(expectedNamespace, expectedActionName, (ActionProxy) actionProxyMock.proxy());
result.setActionProxyFactory(testActionProxyFactory);
ActionProxy actionProxy = (ActionProxy) actionProxyMock.proxy();
result.setActionProxyFactory(testActionProxyFactory);
Mock invocationMock = new Mock(ActionInvocation.class);
invocationMock.matchAndReturn("getProxy", actionProxy);
invocationMock.matchAndReturn("getInvocationContext", ActionContext.getContext());
result.execute((ActionInvocation) invocationMock.proxy());
actionProxyMock.verify();
}
use of com.opensymphony.xwork2.Result in project struts by apache.
the class ChainResultTest method testWithNoNamespace.
public void testWithNoNamespace() throws Exception {
ActionChainResult result = new ActionChainResult();
result.setActionName("${actionName}");
String expectedActionName = "testActionName";
String expectedNamespace = "${1-1}";
Map<String, Object> values = new HashMap<>();
values.put("actionName", expectedActionName);
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(values);
Mock actionProxyMock = new Mock(ActionProxy.class);
actionProxyMock.expect("execute");
actionProxyMock.expectAndReturn("getNamespace", expectedNamespace);
actionProxyMock.expectAndReturn("getActionName", expectedActionName);
actionProxyMock.expectAndReturn("getMethod", null);
ActionProxy actionProxy = (ActionProxy) actionProxyMock.proxy();
ActionProxyFactory testActionProxyFactory = new NamespaceActionNameTestActionProxyFactory(expectedNamespace, expectedActionName, actionProxy);
result.setActionProxyFactory(testActionProxyFactory);
Mock invocationMock = new Mock(ActionInvocation.class);
invocationMock.matchAndReturn("getProxy", actionProxy);
invocationMock.matchAndReturn("getInvocationContext", ActionContext.getContext());
try {
ActionContext.bind(stack.getActionContext());
result.execute((ActionInvocation) invocationMock.proxy());
actionProxyMock.verify();
} finally {
ActionContext.clear();
}
}
use of com.opensymphony.xwork2.Result in project struts by apache.
the class ServletUrlRenderer method renderFormUrl.
/**
* {@inheritDoc}
*/
@Override
public void renderFormUrl(Form formComponent) {
String namespace = formComponent.determineNamespace(formComponent.namespace, formComponent.getStack(), formComponent.request);
String action;
ValueStack vs = ActionContext.getContext().getValueStack();
String scheme = vs.findString("scheme");
if (formComponent.action != null) {
action = formComponent.findString(formComponent.action);
} else {
// no action supplied? ok, then default to the current request
// (action or general URL)
ActionInvocation ai = formComponent.getStack().getActionContext().getActionInvocation();
if (ai != null) {
action = ai.getProxy().getActionName();
namespace = ai.getProxy().getNamespace();
} else {
// hmm, ok, we need to just assume the current URL cut down
String uri = formComponent.request.getRequestURI();
action = uri.substring(uri.lastIndexOf('/'));
}
}
Map actionParams = null;
if (action != null && action.indexOf('?') > 0) {
String queryString = action.substring(action.indexOf('?') + 1);
actionParams = urlHelper.parseQueryString(queryString, false);
action = action.substring(0, action.indexOf('?'));
}
ActionMapping nameMapping = actionMapper.getMappingFromActionName(action);
String actionName = nameMapping.getName();
String actionMethod = nameMapping.getMethod();
final ActionConfig actionConfig = formComponent.configuration.getRuntimeConfiguration().getActionConfig(namespace, actionName);
if (actionConfig != null) {
ActionMapping mapping = new ActionMapping(actionName, namespace, actionMethod, formComponent.parameters);
String result = urlHelper.buildUrl(formComponent.actionMapper.getUriFromActionMapping(mapping), formComponent.request, formComponent.response, actionParams, scheme, formComponent.includeContext, true, false, false);
formComponent.addParameter("action", result);
// let's try to get the actual action class and name
// this can be used for getting the list of validators
formComponent.addParameter("actionName", actionName);
try {
Class clazz = formComponent.objectFactory.getClassInstance(actionConfig.getClassName());
formComponent.addParameter("actionClass", clazz);
} catch (ClassNotFoundException e) {
// this is OK, we'll just move on
}
formComponent.addParameter("namespace", namespace);
// if the name isn't specified, use the action name
if (formComponent.name == null) {
formComponent.addParameter("name", actionName);
}
// if the id isn't specified, use the action name
if (formComponent.getId() == null && actionName != null) {
String escapedId = formComponent.escape(actionName);
formComponent.addParameter("id", escapedId);
formComponent.addParameter("escapedId", escapedId);
}
} else if (action != null) {
// was not found in the configuration.
if (namespace != null && LOG.isWarnEnabled()) {
LOG.warn("No configuration found for the specified action: '{}' in namespace: '{}'. Form action defaulting to 'action' attribute's literal value.", actionName, namespace);
}
String result = urlHelper.buildUrl(action, formComponent.request, formComponent.response, actionParams, scheme, formComponent.includeContext, true);
formComponent.addParameter("action", result);
// namespace: cut out anything between the start and the last /
int slash = result.lastIndexOf('/');
if (slash != -1) {
formComponent.addParameter("namespace", result.substring(0, slash));
} else {
formComponent.addParameter("namespace", "");
}
// name/id: cut out anything between / and . should be the id and
// name
String id = formComponent.getId();
if (id == null) {
slash = result.lastIndexOf('/');
int dot = result.indexOf('.', slash);
if (dot != -1) {
id = result.substring(slash + 1, dot);
} else {
id = result.substring(slash + 1);
}
String escapedId = formComponent.escape(id);
formComponent.addParameter("id", escapedId);
formComponent.addParameter("escapedId", escapedId);
}
}
// WW-1284
// evaluate if client-side js is to be enabled. (if validation
// interceptor does allow validation eg. method is not filtered out)
formComponent.evaluateClientSideJsEnablement(actionName, namespace, actionMethod);
}
Aggregations