use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class ServletActionRedirectResultTest method testExpressionParameterInResultWithConditionParseOn.
public void testExpressionParameterInResultWithConditionParseOn() throws Exception {
ResultConfig resultConfig = new ResultConfig.Builder("", "").addParam("actionName", "someActionName").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("statusCode", "333").addParam("param1", "${#value1}").addParam("param2", "${#value2}").addParam("param3", "${#value3}").addParam("anchor", "${#fragment}").build();
ActionContext context = ActionContext.getContext();
ValueStack stack = context.getValueStack();
context.getContextMap().put("value1", "value 1");
context.getContextMap().put("value2", "value 2");
context.getContextMap().put("value3", "value 3");
context.getContextMap().put("namespaceName", "${1-1}");
context.getContextMap().put("actionName", "${1-1}");
context.getContextMap().put("methodName", "${1-1}");
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
context.put(ServletActionContext.HTTP_REQUEST, req);
context.put(ServletActionContext.HTTP_RESPONSE, res);
Map<String, ResultConfig> results = new HashMap<>();
results.put("myResult", resultConfig);
ActionConfig actionConfig = new ActionConfig.Builder("", "", "").addResultConfigs(results).build();
ServletActionRedirectResult result = new ServletActionRedirectResult();
result.setNamespace("/myNamespace${#namespaceName}");
result.setActionName("myAction${#actionName}");
result.setMethod("myMethod${#methodName}");
result.setParse(true);
result.setEncode(false);
result.setPrependServletContext(false);
result.setAnchor("fragment");
result.setUrlHelper(new DefaultUrlHelper());
IMocksControl control = createControl();
ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
expect(mockInvocation.getProxy()).andReturn(mockActionProxy).anyTimes();
expect(mockInvocation.getResultCode()).andReturn("myResult").anyTimes();
expect(mockActionProxy.getConfig()).andReturn(actionConfig).anyTimes();
expect(mockInvocation.getInvocationContext()).andReturn(context).anyTimes();
expect(mockInvocation.getStack()).andReturn(stack).anyTimes();
control.replay();
result.setActionMapper(container.getInstance(ActionMapper.class));
result.execute(mockInvocation);
assertEquals("/myNamespace${1-1}/myAction${1-1}!myMethod${1-1}.action?param1=value+1¶m2=value+2¶m3=value+3#fragment", res.getRedirectedUrl());
req = new MockHttpServletRequest();
res = new MockHttpServletResponse();
context.put(ServletActionContext.HTTP_REQUEST, req);
context.put(ServletActionContext.HTTP_RESPONSE, res);
result.execute(mockInvocation);
assertEquals("/myNamespace0/myAction0!myMethod0.action?param1=value+1¶m2=value+2¶m3=value+3#fragment", res.getRedirectedUrl());
control.verify();
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class ServletRedirectResultTest method setUp.
protected void setUp() throws Exception {
super.setUp();
configurationManager.getConfiguration().addPackageConfig("foo", new PackageConfig.Builder("foo").namespace("/namespace").build());
view = new ServletRedirectResult();
container.inject(view);
responseMock = new Mock(HttpServletResponse.class);
requestMock = new Mock(HttpServletRequest.class);
requestMock.matchAndReturn("getContextPath", "/context");
ResultConfig resultConfig = new ResultConfig.Builder("", "").build();
Map<String, ResultConfig> results = new HashMap<>();
results.put("myResult", resultConfig);
ActionConfig actionConfig = new ActionConfig.Builder("", "", "").addResultConfigs(results).build();
ActionContext ac = ActionContext.getContext();
ac.withServletRequest((HttpServletRequest) requestMock.proxy());
ac.withServletResponse((HttpServletResponse) responseMock.proxy());
MockActionInvocation ai = new MockActionInvocation();
ai.setInvocationContext(ac);
ai.setResultCode("myResult");
ActionProxy mockActionProxy = createNiceMock(ActionProxy.class);
ai.setProxy(mockActionProxy);
expect(mockActionProxy.getConfig()).andReturn(actionConfig).anyTimes();
replay(mockActionProxy);
ai.setStack(ActionContext.getContext().getValueStack());
this.ai = ai;
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class VelocityResultTest method setUp.
protected void setUp() throws Exception {
super.setUp();
namespace = "/html";
result = new VelocityResult();
stack = ActionContext.getContext().getValueStack();
velocity = new TestVelocityEngine();
mockActionProxy = new Mock(ActionProxy.class);
mockActionProxy.expectAndReturn("getNamespace", "/html");
Mock mockActionInvocation = new Mock(ActionInvocation.class);
mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy());
mockActionInvocation.expectAndReturn("getStack", stack);
actionInvocation = (ActionInvocation) mockActionInvocation.proxy();
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project javamelody by javamelody.
the class TestStrutsInterceptor method testStruts.
/**
* Test.
* @throws Exception e
*/
@Test
public void testStruts() throws Exception {
final Counter strutsCounter = MonitoringProxy.getStrutsCounter();
strutsCounter.clear();
final StrutsInterceptor strutsInterceptor = new StrutsInterceptor();
final ActionInvocation invocation = createNiceMock(ActionInvocation.class);
final MockActionProxy proxy = new MockActionProxy();
proxy.setInvocation(invocation);
proxy.setActionName("test.action");
proxy.setMethod(null);
proxy.setNamespace("testnamespace");
expect(invocation.getProxy()).andReturn(proxy).anyTimes();
replay(invocation);
strutsCounter.setDisplayed(false);
strutsInterceptor.intercept(invocation);
final String requestsCount = "requestsCount";
assertSame(requestsCount, 0, strutsCounter.getRequestsCount());
strutsCounter.setDisplayed(true);
strutsInterceptor.intercept(invocation);
assertSame(requestsCount, 1, strutsCounter.getRequestsCount());
verify(invocation);
final ActionInvocation invocation2 = createNiceMock(ActionInvocation.class);
final MockActionProxy proxy2 = new MockActionProxy();
proxy2.setInvocation(invocation2);
proxy2.setActionName("test2.action");
proxy2.setMethod("execute");
proxy2.setNamespace("testnamespace");
expect(invocation2.getProxy()).andReturn(proxy2).anyTimes();
replay(invocation2);
strutsInterceptor.intercept(invocation2);
assertSame(requestsCount, 2, strutsCounter.getRequestsCount());
verify(invocation2);
final ActionInvocation invocation3 = createNiceMock(ActionInvocation.class);
final MockActionProxy proxy3 = new MockActionProxy();
proxy3.setInvocation(invocation3);
proxy3.setActionName("test3.action");
proxy3.setMethod("testmethod");
proxy3.setNamespace("testnamespace");
expect(invocation3.getProxy()).andReturn(proxy3).anyTimes();
expect(invocation3.invoke()).andThrow(new UnknownError("test d'erreur")).anyTimes();
replay(invocation3);
try {
strutsInterceptor.intercept(invocation3);
} catch (final UnknownError e) {
assertNotNull("ok", e);
}
assertSame(requestsCount, 3, strutsCounter.getRequestsCount());
verify(invocation3);
}
use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.
the class DefaultActionInvocationTester method testInvokingMissingMethod.
public void testInvokingMissingMethod() 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 ERROR;
}
};
MockActionProxy proxy = new MockActionProxy();
proxy.setMethod("notExists");
UnknownHandlerManager uhm = new DefaultUnknownHandlerManager() {
@Override
public boolean hasUnknownHandlers() {
return false;
}
};
dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
dai.proxy = proxy;
dai.ognlUtil = new OgnlUtil();
dai.unknownHandlerManager = uhm;
// when
Throwable actual = null;
try {
dai.invokeAction(action, null);
} catch (Exception e) {
actual = e;
}
// then
assertNotNull(actual);
assertTrue(actual instanceof NoSuchMethodException);
}
Aggregations