Search in sources :

Example 31 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.

the class DefaultActionInvocationTester method testInvokingExistingExecuteMethod.

public void testInvokingExistingExecuteMethod() 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 SUCCESS;
        }
    };
    MockActionProxy proxy = new MockActionProxy();
    proxy.setMethod("execute");
    dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
    dai.proxy = proxy;
    dai.ognlUtil = new OgnlUtil();
    // when
    String result = dai.invokeAction(action, null);
    // then
    assertEquals("success", result);
}
Also used : OgnlUtil(com.opensymphony.xwork2.ognl.OgnlUtil) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 32 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.

the class DefaultActionInvocationTester method testUnknownHandlerManagerThatReturnsNull.

public void testUnknownHandlerManagerThatReturnsNull() 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 null;
        }
    };
    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
    Throwable actual = null;
    try {
        dai.invokeAction(new SimpleAction(), 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)

Example 33 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.

the class ContentTypeHandlerManagerTest method testHandleResultOK.

public void testHandleResultOK() throws IOException {
    String obj = "mystring";
    ContentTypeHandler handler = new AbstractContentTypeHandler() {

        public void toObject(ActionInvocation invocation, Reader in, Object target) {
        }

        public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer stream) throws IOException {
            stream.write(obj.toString());
            return resultCode;
        }

        public String getContentType() {
            return "foo";
        }

        public String getExtension() {
            return "foo";
        }
    };
    mgr.handlersByExtension.put("xml", handler);
    mgr.setDefaultExtension("xml");
    ActionConfig actionConfig = new ActionConfig.Builder("", "", "").build();
    MockActionProxy proxy = new MockActionProxy();
    proxy.setConfig(actionConfig);
    invocation.setProxy(proxy);
    mgr.handleResult(invocation, new DefaultHttpHeaders().withStatus(SC_OK), obj);
    assertEquals(obj.getBytes().length, mockResponse.getContentLength());
}
Also used : AbstractContentTypeHandler(org.apache.struts2.rest.handler.AbstractContentTypeHandler) ContentTypeHandler(org.apache.struts2.rest.handler.ContentTypeHandler) ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) AbstractContentTypeHandler(org.apache.struts2.rest.handler.AbstractContentTypeHandler) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) Reader(java.io.Reader) Writer(java.io.Writer) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 34 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.

the class ContentTypeHandlerManagerTest method setUp.

@Override
public void setUp() {
    mgr = new DefaultContentTypeHandlerManager();
    mockResponse = new MockHttpServletResponse();
    mockRequest = new MockHttpServletRequest();
    mockRequest.setMethod("GET");
    ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
    actionContext.withServletRequest(mockRequest);
    actionContext.withServletResponse(mockResponse);
    invocation = new MockActionInvocation();
    invocation.setProxy(new MockActionProxy());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionContext(com.opensymphony.xwork2.ActionContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 35 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy in project struts by apache.

the class RestActionInvocationTest method testInvoke.

/**
 * Test the global execution
 * @throws Exception
 */
public void testInvoke() throws Exception {
    // Default index method return 'success'
    ((MockActionProxy) restActionInvocation.getProxy()).setMethod("index");
    // Define result 'success'
    ResultConfig resultConfig = new ResultConfig.Builder("success", "org.apache.struts2.result.HttpHeaderResult").addParam("status", "123").build();
    ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest", "RestAction", "org.apache.rest.RestAction").addResultConfig(resultConfig).build();
    ((MockActionProxy) restActionInvocation.getProxy()).setConfig(actionConfig);
    request.setMethod("GET");
    restActionInvocation.setOgnlUtil(new OgnlUtil());
    restActionInvocation.invoke();
    assertEquals(123, response.getStatus());
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) 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