Search in sources :

Example 21 with MockActionProxy

use of com.opensymphony.xwork2.mock.MockActionProxy 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!");
    }
}
Also used : ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) PreResultListener(com.opensymphony.xwork2.interceptor.PreResultListener) Callable(java.util.concurrent.Callable) MockInterceptor(com.opensymphony.xwork2.mock.MockInterceptor) OgnlUtil(com.opensymphony.xwork2.ognl.OgnlUtil) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 22 with MockActionProxy

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

the class RestWorkflowInterceptorTest method testCustomValidationFailureStatusCode.

public void testCustomValidationFailureStatusCode() throws Exception {
    RestWorkflowInterceptor wf = new RestWorkflowInterceptor();
    ActionSupport action = new ActionSupport();
    action.addActionError("some error");
    wf.setValidationFailureStatusCode("666");
    Mock mockActionInvocation = new Mock(ActionInvocation.class);
    Mock mockActionProxy = new Mock(ActionProxy.class);
    mockActionProxy.expectAndReturn("getConfig", null);
    mockActionInvocation.expectAndReturn("getAction", action);
    Mock mockContentTypeHandlerManager = new Mock(ContentTypeHandlerManager.class);
    mockContentTypeHandlerManager.expectAndReturn("handleResult", new AnyConstraintMatcher() {

        public boolean matches(Object[] args) {
            DefaultHttpHeaders headers = (DefaultHttpHeaders) args[1];
            return 666 == headers.getStatus();
        }
    }, null);
    wf.setContentTypeHandlerManager((ContentTypeHandlerManager) mockContentTypeHandlerManager.proxy());
    ActionContext.of(new HashMap<>()).withActionMapping(new ActionMapping()).bind();
    wf.doIntercept((ActionInvocation) mockActionInvocation.proxy());
    mockContentTypeHandlerManager.verify();
    mockActionInvocation.verify();
}
Also used : ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) ActionSupport(com.opensymphony.xwork2.ActionSupport) Mock(com.mockobjects.dynamic.Mock) AnyConstraintMatcher(com.mockobjects.dynamic.AnyConstraintMatcher)

Example 23 with MockActionProxy

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

the class AnnotationValidationInterceptorTest method setUp.

public void setUp() throws Exception {
    super.setUp();
    test = new TestAction();
    interceptor = new AnnotationValidationInterceptor();
    container.inject(interceptor);
    config = new ActionConfig.Builder("", "foo", "").build();
    mockActionInvocation = new Mock(ActionInvocation.class);
    mockActionProxy = new Mock(ActionProxy.class);
    mockActionInvocation.matchAndReturn("getProxy", (ActionProxy) mockActionProxy.proxy());
    mockActionInvocation.matchAndReturn("getAction", test);
    mockActionInvocation.expect("invoke");
    ActionContext.getContext().withActionInvocation((ActionInvocation) mockActionInvocation.proxy());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) Mock(com.mockobjects.dynamic.Mock)

Example 24 with MockActionProxy

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

the class I18nInterceptorTest method setUp.

public void setUp() throws Exception {
    interceptor = new I18nInterceptor();
    interceptor.setLocaleProviderFactory(new DefaultLocaleProviderFactory());
    interceptor.init();
    session = new HashMap<>();
    ac = ActionContext.of(new HashMap<>()).bind().withSession(session).withParameters(HttpParameters.create().build());
    request = new MockHttpServletRequest();
    request.setSession(new MockHttpSession());
    ServletActionContext.setRequest(request);
    Action action = () -> Action.SUCCESS;
    MockActionProxy proxy = new MockActionProxy();
    proxy.setAction(action);
    proxy.setNamespace("i18n");
    proxy.setActionName("anAction");
    mai = new MockActionInvocation();
    ((MockActionInvocation) mai).setAction(action);
    ((MockActionInvocation) mai).setInvocationContext(ac);
    ((MockActionInvocation) mai).setProxy(proxy);
}
Also used : Action(com.opensymphony.xwork2.Action) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) MockHttpSession(org.springframework.mock.web.MockHttpSession) DefaultLocaleProviderFactory(com.opensymphony.xwork2.DefaultLocaleProviderFactory) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 25 with MockActionProxy

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

the class DispatcherTest method testServiceActionResumePreviousProxy.

public void testServiceActionResumePreviousProxy() throws Exception {
    Dispatcher du = initDispatcher(Collections.<String, String>emptyMap());
    MockActionInvocation mai = new MockActionInvocation();
    ActionContext.getContext().withActionInvocation(mai);
    MockActionProxy actionProxy = new MockActionProxy();
    actionProxy.setInvocation(mai);
    mai.setProxy(actionProxy);
    mai.setStack(new StubValueStack());
    HttpServletRequest req = new MockHttpServletRequest();
    req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, mai.getStack());
    assertFalse(actionProxy.isExecutedCalled());
    du.setDevMode("false");
    du.setHandleException("false");
    du.serviceAction(req, null, new ActionMapping());
    assertTrue("should execute previous proxy", actionProxy.isExecutedCalled());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) StubValueStack(com.opensymphony.xwork2.StubValueStack) ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) 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