Search in sources :

Example 26 with MockActionProxy

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&param2=value+2&param3=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&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
    control.verify();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ActionProxy(com.opensymphony.xwork2.ActionProxy) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) IMocksControl(org.easymock.IMocksControl) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 27 with MockActionProxy

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;
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) Mock(com.mockobjects.dynamic.Mock) EasyMock.createMock(org.easymock.EasyMock.createMock) EasyMock.createNiceMock(org.easymock.EasyMock.createNiceMock) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest)

Example 28 with MockActionProxy

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();
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) Mock(com.mockobjects.dynamic.Mock)

Example 29 with MockActionProxy

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);
}
Also used : Counter(net.bull.javamelody.internal.model.Counter) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy) Test(org.junit.Test)

Example 30 with MockActionProxy

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);
}
Also used : OgnlUtil(com.opensymphony.xwork2.ognl.OgnlUtil) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Aggregations

MockActionProxy (com.opensymphony.xwork2.mock.MockActionProxy)43 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)34 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)29 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)19 ActionProxy (com.opensymphony.xwork2.ActionProxy)17 HashMap (java.util.HashMap)15 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)13 ActionContext (com.opensymphony.xwork2.ActionContext)12 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)11 SimpleFooAction (com.opensymphony.xwork2.SimpleFooAction)10 ServletActionContext (org.apache.struts2.ServletActionContext)10 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)10 OgnlUtil (com.opensymphony.xwork2.ognl.OgnlUtil)8 ActionMapper (org.apache.struts2.dispatcher.mapper.ActionMapper)7 IMocksControl (org.easymock.IMocksControl)7 ValueStack (com.opensymphony.xwork2.util.ValueStack)6 DefaultUrlHelper (org.apache.struts2.views.util.DefaultUrlHelper)6 Mock (com.mockobjects.dynamic.Mock)5 Action (com.opensymphony.xwork2.Action)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5