Search in sources :

Example 1 with OgnlUtil

use of com.opensymphony.xwork2.ognl.OgnlUtil in project struts by apache.

the class OgnlUtilTest method reloadTestContainerConfiguration.

private void reloadTestContainerConfiguration(boolean allowStaticField) {
    loadConfigurationProviders(new StubConfigurationProvider() {

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            props.setProperty(StrutsConstants.STRUTS_ALLOW_STATIC_FIELD_ACCESS, "" + allowStaticField);
        }
    });
    ognlUtil = container.getInstance(OgnlUtil.class);
}
Also used : ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 2 with OgnlUtil

use of com.opensymphony.xwork2.ognl.OgnlUtil in project struts by apache.

the class DefaultActionInvocationTester method testUnknownHandlerManagerThatThrowsException.

public void testUnknownHandlerManagerThatThrowsException() 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 {
            throw new NoSuchMethodException();
        }
    };
    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
    // 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 3 with OgnlUtil

use of com.opensymphony.xwork2.ognl.OgnlUtil in project struts by apache.

the class DefaultActionInvocationTester method testInvokingExistingMethodThatThrowsException.

public void testInvokingExistingMethodThatThrowsException() throws Exception {
    // given
    DefaultActionInvocation dai = new DefaultActionInvocation(ActionContext.getContext().getContextMap(), false);
    container.inject(dai);
    SimpleAction action = new SimpleAction() {

        @Override
        public String execute() throws Exception {
            throw new IllegalArgumentException();
        }
    };
    MockActionProxy proxy = new MockActionProxy();
    proxy.setMethod("execute");
    dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
    dai.proxy = proxy;
    dai.ognlUtil = new OgnlUtil();
    // when
    Throwable actual = null;
    try {
        dai.invokeAction(action, null);
    } catch (Exception e) {
        actual = e;
    }
    // then
    assertNotNull(actual);
    assertTrue(actual instanceof IllegalArgumentException);
}
Also used : OgnlUtil(com.opensymphony.xwork2.ognl.OgnlUtil) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 4 with OgnlUtil

use of com.opensymphony.xwork2.ognl.OgnlUtil 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 5 with OgnlUtil

use of com.opensymphony.xwork2.ognl.OgnlUtil 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)

Aggregations

OgnlUtil (com.opensymphony.xwork2.ognl.OgnlUtil)9 MockActionProxy (com.opensymphony.xwork2.mock.MockActionProxy)8 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)3 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)2 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)2 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)2 ActionContext (com.opensymphony.xwork2.ActionContext)1 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)1 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)1 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)1 PreResultListener (com.opensymphony.xwork2.interceptor.PreResultListener)1 MockInterceptor (com.opensymphony.xwork2.mock.MockInterceptor)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 Semaphore (java.util.concurrent.Semaphore)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 OgnlException (ognl.OgnlException)1 ServletActionContext (org.apache.struts2.ServletActionContext)1 EvaluationException (org.apache.tiles.evaluator.EvaluationException)1