Search in sources :

Example 6 with DefaultActionInvocation

use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.

the class DefaultActionInvocationTester method testNullResultPossible.

public void testNullResultPossible() throws Exception {
    ActionProxy actionProxy = actionProxyFactory.createActionProxy("", "NullFoo", "nullMethod", new HashMap<String, Object>());
    DefaultActionInvocation defaultActionInvocation = (DefaultActionInvocation) actionProxy.getInvocation();
    defaultActionInvocation.init(actionProxy);
    String result = defaultActionInvocation.invoke();
    assertNull(result);
}
Also used : MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 7 with DefaultActionInvocation

use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.

the class DefaultActionInvocationTester method testUnknownHandlerManagerThatReturnsSuccess.

public void testUnknownHandlerManagerThatReturnsSuccess() 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 "success";
        }
    };
    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
    String result = dai.invokeAction(new SimpleAction(), null);
    // then
    assertNotNull(result);
    assertEquals("success", result);
}
Also used : OgnlUtil(com.opensymphony.xwork2.ognl.OgnlUtil) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 8 with DefaultActionInvocation

use of com.opensymphony.xwork2.DefaultActionInvocation 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 9 with DefaultActionInvocation

use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.

the class InvocationSessionStore method loadInvocation.

/**
 * Checks the Map in the Session for the key and the token. If the
 * ActionInvocation is saved in the Session, the ValueStack from the
 * ActionProxy associated with the ActionInvocation is set into the
 * ActionContext and the ActionInvocation is returned.
 *
 * @param key the name the DefaultActionInvocation and ActionContext were saved as
 * @param token token for check
 * @return the DefaultActionInvocation saved using the key, or null if none was found
 */
public static ActionInvocation loadInvocation(String key, String token) {
    InvocationContext invocationContext = (InvocationContext) getInvocationMap().get(key);
    if ((invocationContext == null) || !invocationContext.token.equals(token)) {
        return null;
    }
    final ActionInvocation savedInvocation = invocationContext.invocation;
    if (savedInvocation != null) {
        // WW-5026 - Preserve the previous PageContext (even if null) and restore it to the
        // ActionContext after loading the savedInvocation context.  The saved context's PageContext
        // would already be closed at this point (causing failures if used for output).
        final ActionContext previousActionContext = ActionContext.getContext();
        savedInvocation.getInvocationContext().withPageContext(previousActionContext.getPageContext()).withValueStack(savedInvocation.getStack()).bind();
    }
    return savedInvocation;
}
Also used : ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 10 with DefaultActionInvocation

use of com.opensymphony.xwork2.DefaultActionInvocation in project struts by apache.

the class DefaultActionInvocationTester method testInvoke.

/**
 * Tests interceptor chain invoke.
 *
 * @throws Exception when action throws exception
 */
public void testInvoke() throws Exception {
    List<InterceptorMapping> interceptorMappings = new ArrayList<>();
    MockInterceptor mockInterceptor1 = new MockInterceptor();
    mockInterceptor1.setFoo("test1");
    mockInterceptor1.setExpectedFoo("test1");
    interceptorMappings.add(new InterceptorMapping("test1", mockInterceptor1));
    MockInterceptor mockInterceptor2 = new MockInterceptor();
    interceptorMappings.add(new InterceptorMapping("test2", mockInterceptor2));
    mockInterceptor2.setFoo("test2");
    mockInterceptor2.setExpectedFoo("test2");
    MockInterceptor mockInterceptor3 = new MockInterceptor();
    interceptorMappings.add(new InterceptorMapping("test3", mockInterceptor3));
    mockInterceptor3.setFoo("test3");
    mockInterceptor3.setExpectedFoo("test3");
    DefaultActionInvocation defaultActionInvocation = new DefaultActionInvocationTester(interceptorMappings);
    container.inject(defaultActionInvocation);
    defaultActionInvocation.stack = container.getInstance(ValueStackFactory.class).createValueStack();
    // is possible when result is not executed already
    defaultActionInvocation.setResultCode("");
    defaultActionInvocation.invoke();
    assertTrue(mockInterceptor1.isExecuted());
    assertTrue(mockInterceptor2.isExecuted());
    assertTrue(mockInterceptor3.isExecuted());
    assertTrue(defaultActionInvocation.isExecuted());
    try {
        defaultActionInvocation.setResultCode("");
        fail("should not possible when result already executed");
    } catch (Exception ignored) {
    }
    try {
        defaultActionInvocation.invoke();
        fail("should not possible when result already executed");
    } catch (Exception ignored) {
    }
}
Also used : MockInterceptor(com.opensymphony.xwork2.mock.MockInterceptor) ArrayList(java.util.ArrayList) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping)

Aggregations

MockActionProxy (com.opensymphony.xwork2.mock.MockActionProxy)12 OgnlUtil (com.opensymphony.xwork2.ognl.OgnlUtil)7 ActionContext (com.opensymphony.xwork2.ActionContext)3 DefaultActionInvocation (com.opensymphony.xwork2.DefaultActionInvocation)3 HashMap (java.util.HashMap)3 Mock (com.mockobjects.dynamic.Mock)2 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)2 ActionProxy (com.opensymphony.xwork2.ActionProxy)2 DefaultActionProxyFactory (com.opensymphony.xwork2.DefaultActionProxyFactory)2 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)2 Container (com.opensymphony.xwork2.inject.Container)2 MockInterceptor (com.opensymphony.xwork2.mock.MockInterceptor)2 File (java.io.File)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 JspWriter (javax.servlet.jsp.JspWriter)2 ServletActionContext (org.apache.struts2.ServletActionContext)2 ApplicationMap (org.apache.struts2.dispatcher.ApplicationMap)2 Dispatcher (org.apache.struts2.dispatcher.Dispatcher)2