Search in sources :

Example 6 with OgnlUtil

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

the class OgnlValueStackTest method reloadTestContainerConfiguration.

private void reloadTestContainerConfiguration(Boolean allowStaticMethod, Boolean allowStaticField) throws Exception {
    loadConfigurationProviders(new StubConfigurationProvider() {

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            // undefined values then should be evaluated to false
            if (props.containsKey(StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS)) {
                props.remove(StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS);
            }
            if (props.containsKey(StrutsConstants.STRUTS_ALLOW_STATIC_FIELD_ACCESS)) {
                props.remove(StrutsConstants.STRUTS_ALLOW_STATIC_FIELD_ACCESS);
            }
            if (allowStaticMethod != null) {
                props.setProperty(StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS, "" + allowStaticMethod);
            }
            if (allowStaticField != null) {
                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 7 with OgnlUtil

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

the class StrutsAttributeEvaluator method evaluate.

@Override
public Object evaluate(String expression, Request request) {
    try {
        HttpServletRequest httpRequest = ServletUtil.getServletRequest(request).getRequest();
        ActionContext ctx = ServletActionContext.getActionContext(httpRequest);
        if (ctx == null) {
            LOG.error("Cannot obtain HttpServletRequest from [{}]", request.getClass().getName());
            throw new ConfigurationException("There is no ActionContext for current request!");
        }
        OgnlUtil ognlUtil = ctx.getContainer().getInstance(OgnlUtil.class);
        LOG.debug("Trying evaluate expression [{}] using OgnlUtil's getValue", expression);
        Object result = ognlUtil.getValue(expression, ctx.getContextMap(), ctx.getValueStack().getRoot());
        LOG.debug("Final result of evaluating expression [{}] is: {}", expression, result);
        return result;
    } catch (OgnlException e) {
        throw new EvaluationException(e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OgnlException(ognl.OgnlException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) OgnlUtil(com.opensymphony.xwork2.ognl.OgnlUtil) EvaluationException(org.apache.tiles.evaluator.EvaluationException) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext)

Example 8 with OgnlUtil

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

Example 9 with OgnlUtil

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

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

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