Search in sources :

Example 1 with DefaultActionInvocation

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

the class URLTagTest method testEmptyActionCustomMapper.

public void testEmptyActionCustomMapper() throws Exception {
    Map<String, String> props = new HashMap<>();
    props.put("config", "struts-default.xml,struts-plugin.xml,struts.xml,org/apache/struts2/views/jsp/WW3090-struts.xml");
    this.tearDown();
    Dispatcher du = this.initDispatcher(props);
    /**
     * create our standard mock objects
     */
    action = this.getAction();
    stack = ActionContext.getContext().getValueStack();
    context = stack.getContext();
    stack.push(action);
    request = new StrutsMockHttpServletRequest();
    request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
    response = new StrutsMockHttpServletResponse();
    request.setSession(new StrutsMockHttpSession());
    request.setupGetServletPath("/");
    writer = new StringWriter();
    servletContext = new StrutsMockServletContext();
    servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
    servletContext.setServletInfo("Resin");
    pageContext = new StrutsMockPageContext();
    pageContext.setRequest(request);
    pageContext.setResponse(response);
    pageContext.setServletContext(servletContext);
    mockContainer = new Mock(Container.class);
    session = new SessionMap(request);
    Map<String, Object> extraContext = du.createContextMap(new RequestMap(request), HttpParameters.create(request.getParameterMap()).build(), session, new ApplicationMap(pageContext.getServletContext()), request, response);
    // let's not set the locale -- there is a test that checks if Dispatcher actually picks this up...
    // ... but generally we want to just use no locale (let it stay system default)
    extraContext.remove(ActionContext.LOCALE);
    stack.getContext().putAll(extraContext);
    context.put(ServletActionContext.HTTP_REQUEST, request);
    context.put(ServletActionContext.HTTP_RESPONSE, response);
    context.put(ServletActionContext.SERVLET_CONTEXT, servletContext);
    ActionContext actionContext = ActionContext.of(context).withServletRequest(request).withServletResponse(response).withServletContext(servletContext).bind();
    // Make sure we have an action invocation available
    ActionContext.getContext().withActionInvocation(new DefaultActionInvocation(null, true));
    DefaultActionProxyFactory apFactory = new DefaultActionProxyFactory();
    apFactory.setContainer(container);
    ActionProxy ap = apFactory.createActionProxy("/", "hello", null, null);
    ActionContext.getContext().getActionInvocation().init(ap);
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(80);
    tag = new URLTag();
    tag.setPageContext(pageContext);
    JspWriter jspWriter = new StrutsMockJspWriter(writer);
    pageContext.setJspWriter(jspWriter);
    request.setRequestURI("/context/someAction.action");
    tag.setAction(null);
    tag.setValue(null);
    tag.doStartTag();
    tag.doEndTag();
    assertEquals("/hello.action-red", writer.toString());
    // Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
    URLTag freshTag = new URLTag();
    freshTag.setPageContext(pageContext);
    // URLTag clears component in doEndTag and has no additional state set here, so it compares as equal with the default tag clear state as well.
    assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set.  " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
    writer = new StringWriter();
    jspWriter = new StrutsMockJspWriter(writer);
    pageContext.setJspWriter(jspWriter);
    tag.doStartTag();
    tag.doEndTag();
    assertEquals("/hello.action-blue", writer.toString());
    // Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
    // URLTag clears component in doEndTag and has no additional state set here, so it compares as equal with the default tag clear state as well.
    assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set.  " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
    writer = new StringWriter();
    jspWriter = new StrutsMockJspWriter(writer);
    pageContext.setJspWriter(jspWriter);
    tag.doStartTag();
    tag.doEndTag();
    assertEquals("/hello.action-red", writer.toString());
    // Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
    // URLTag clears component in doEndTag and has no additional state set here, so it compares as equal with the default tag clear state as well.
    assertTrue("Tag state after doEndTag() under default tag clear state is inequal to new Tag with pageContext/parent set.  " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
}
Also used : ApplicationMap(org.apache.struts2.dispatcher.ApplicationMap) ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Dispatcher(org.apache.struts2.dispatcher.Dispatcher) Mock(com.mockobjects.dynamic.Mock) DefaultActionInvocation(com.opensymphony.xwork2.DefaultActionInvocation) Container(com.opensymphony.xwork2.inject.Container) StringWriter(java.io.StringWriter) SessionMap(org.apache.struts2.dispatcher.SessionMap) RequestMap(org.apache.struts2.dispatcher.RequestMap) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) JspWriter(javax.servlet.jsp.JspWriter) DefaultActionProxyFactory(com.opensymphony.xwork2.DefaultActionProxyFactory) File(java.io.File)

Example 2 with DefaultActionInvocation

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

the class URLTagTest method testEmptyActionCustomMapper_clearTagStateSet.

public void testEmptyActionCustomMapper_clearTagStateSet() throws Exception {
    Map<String, String> props = new HashMap<String, String>();
    props.put("config", "struts-default.xml,struts-plugin.xml,struts.xml,org/apache/struts2/views/jsp/WW3090-struts.xml");
    this.tearDown();
    Dispatcher du = this.initDispatcher(props);
    action = this.getAction();
    stack = ActionContext.getContext().getValueStack();
    context = stack.getContext();
    stack.push(action);
    request = new StrutsMockHttpServletRequest();
    request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
    response = new StrutsMockHttpServletResponse();
    request.setSession(new StrutsMockHttpSession());
    request.setupGetServletPath("/");
    writer = new StringWriter();
    servletContext = new StrutsMockServletContext();
    servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
    servletContext.setServletInfo("Resin");
    pageContext = new StrutsMockPageContext();
    pageContext.setRequest(request);
    pageContext.setResponse(response);
    pageContext.setServletContext(servletContext);
    mockContainer = new Mock(Container.class);
    session = new SessionMap<>(request);
    Map<String, Object> extraContext = du.createContextMap(new RequestMap(request), HttpParameters.create(request.getParameterMap()).build(), session, new ApplicationMap(pageContext.getServletContext()), request, response);
    // let's not set the locale -- there is a test that checks if Dispatcher actually picks this up...
    // ... but generally we want to just use no locale (let it stay system default)
    extraContext = ActionContext.of(extraContext).withLocale(null).getContextMap();
    stack.getContext().putAll(extraContext);
    ActionContext actionContext = ActionContext.of(context).withServletRequest(request).withServletResponse(response).withServletContext(servletContext).bind();
    // Make sure we have an action invocation available
    ActionContext.getContext().withActionInvocation(new DefaultActionInvocation(null, true));
    DefaultActionProxyFactory apFactory = new DefaultActionProxyFactory();
    apFactory.setContainer(container);
    ActionProxy ap = apFactory.createActionProxy("/", "hello", null, null);
    ActionContext.getContext().getActionInvocation().init(ap);
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(80);
    tag = new URLTag();
    // Explicitly request tag state clearing.
    tag.setPerformClearTagStateForTagPoolingServers(true);
    tag.setPageContext(pageContext);
    JspWriter jspWriter = new StrutsMockJspWriter(writer);
    pageContext.setJspWriter(jspWriter);
    request.setRequestURI("/context/someAction.action");
    tag.setAction(null);
    tag.setValue(null);
    tag.doStartTag();
    // Ensure component tag state clearing is set true (to match tag).
    setComponentTagClearTagState(tag, true);
    tag.doEndTag();
    assertEquals("/hello.action-red", writer.toString());
    // Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
    URLTag freshTag = new URLTag();
    freshTag.setPerformClearTagStateForTagPoolingServers(true);
    freshTag.setPageContext(pageContext);
    assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set.  " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
    writer = new StringWriter();
    jspWriter = new StrutsMockJspWriter(writer);
    pageContext.setJspWriter(jspWriter);
    tag.doStartTag();
    // Ensure component tag state clearing is set true (to match tag).
    setComponentTagClearTagState(tag, true);
    tag.doEndTag();
    assertEquals("/hello.action-blue", writer.toString());
    // Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
    assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set.  " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
    writer = new StringWriter();
    jspWriter = new StrutsMockJspWriter(writer);
    pageContext.setJspWriter(jspWriter);
    tag.doStartTag();
    // Ensure component tag state clearing is set true (to match tag).
    setComponentTagClearTagState(tag, true);
    tag.doEndTag();
    assertEquals("/hello.action-red", writer.toString());
    // Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
    assertTrue("Tag state after doEndTag() and explicit tag state clearing is inequal to new Tag with pageContext/parent set.  " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
}
Also used : ApplicationMap(org.apache.struts2.dispatcher.ApplicationMap) ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Dispatcher(org.apache.struts2.dispatcher.Dispatcher) Mock(com.mockobjects.dynamic.Mock) DefaultActionInvocation(com.opensymphony.xwork2.DefaultActionInvocation) Container(com.opensymphony.xwork2.inject.Container) StringWriter(java.io.StringWriter) RequestMap(org.apache.struts2.dispatcher.RequestMap) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) JspWriter(javax.servlet.jsp.JspWriter) DefaultActionProxyFactory(com.opensymphony.xwork2.DefaultActionProxyFactory) File(java.io.File)

Example 3 with DefaultActionInvocation

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

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

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

the class DefaultActionInvocationTester method testInvokeWithLazyParams.

public void testInvokeWithLazyParams() throws Exception {
    HashMap<String, Object> params = new HashMap<>();
    params.put("blah", "this is blah");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    DefaultActionInvocation defaultActionInvocation = new DefaultActionInvocation(extraContext, true);
    container.inject(defaultActionInvocation);
    ActionProxy actionProxy = actionProxyFactory.createActionProxy("", "LazyFoo", null, extraContext);
    defaultActionInvocation.init(actionProxy);
    defaultActionInvocation.invoke();
    SimpleAction action = (SimpleAction) defaultActionInvocation.getAction();
    assertEquals("this is blah", action.getBlah());
    assertEquals("this is blah", action.getName());
}
Also used : MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy) HashMap(java.util.HashMap)

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