Search in sources :

Example 16 with DefaultActionInvocation

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

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

the class ActionChainResult method execute.

/**
 * @param invocation the DefaultActionInvocation calling the action call stack
 */
public void execute(ActionInvocation invocation) throws Exception {
    if (invocation == null) {
        throw new IllegalArgumentException("Invocation cannot be null!");
    }
    ValueStack stack = invocation.getInvocationContext().getValueStack();
    String finalNamespace = this.namespace != null ? TextParseUtil.translateVariables(namespace, stack) : invocation.getProxy().getNamespace();
    String finalActionName = TextParseUtil.translateVariables(actionName, stack);
    String finalMethodName = this.methodName != null ? TextParseUtil.translateVariables(this.methodName, stack) : null;
    if (isInChainHistory(finalNamespace, finalActionName, finalMethodName)) {
        addToHistory(finalNamespace, finalActionName, finalMethodName);
        throw new StrutsException("Infinite recursion detected: " + ActionChainResult.getChainHistory().toString());
    }
    if (ActionChainResult.getChainHistory().isEmpty() && invocation.getProxy() != null) {
        addToHistory(finalNamespace, invocation.getProxy().getActionName(), invocation.getProxy().getMethod());
    }
    addToHistory(finalNamespace, finalActionName, finalMethodName);
    Map<String, Object> extraContext = ActionContext.of(new HashMap<>()).withValueStack(invocation.getInvocationContext().getValueStack()).withParameters(invocation.getInvocationContext().getParameters()).with(CHAIN_HISTORY, ActionChainResult.getChainHistory()).getContextMap();
    LOG.debug("Chaining to action {}", finalActionName);
    proxy = actionProxyFactory.createActionProxy(finalNamespace, finalActionName, finalMethodName, extraContext);
    proxy.execute();
}
Also used : StrutsException(org.apache.struts2.StrutsException) ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 18 with DefaultActionInvocation

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

the class AnnotationValidationConfigurationBuilderTest method createValidationManager.

private AnnotationActionValidatorManager createValidationManager(final Class<? extends ActionSupport> actionClass, Locale locale) throws Exception {
    loadConfigurationProviders(new ConfigurationProvider() {

        public void destroy() {
        }

        public void init(Configuration configuration) throws ConfigurationException {
            configuration.addPackageConfig("default", new PackageConfig.Builder("default").addActionConfig("annotation", new ActionConfig.Builder("", "annotation", actionClass.getName()).build()).build());
        }

        public boolean needsReload() {
            return false;
        }

        public void loadPackages() throws ConfigurationException {
        }

        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.constant(StrutsConstants.STRUTS_DEVMODE, true);
        }
    });
    // ActionContext is destroyed during rebuilding configuration
    ActionContext.getContext().withLocale(locale);
    ActionInvocation invocation = new DefaultActionInvocation(ActionContext.getContext().getContextMap(), true);
    container.inject(invocation);
    invocation.init(actionProxyFactory.createActionProxy("", "annotation", null, ActionContext.getContext().getContextMap()));
    AnnotationActionValidatorManager manager = new AnnotationActionValidatorManager();
    container.inject(manager);
    ValidatorFactory vf = container.getInstance(ValidatorFactory.class);
    vf.registerValidator("myValidator", MyValidator.class.getName());
    return manager;
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) Configuration(com.opensymphony.xwork2.config.Configuration) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) DefaultActionInvocation(com.opensymphony.xwork2.DefaultActionInvocation) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) DefaultActionInvocation(com.opensymphony.xwork2.DefaultActionInvocation) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties)

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