Search in sources :

Example 1 with Mock

use of com.mockobjects.dynamic.Mock in project struts by apache.

the class ActionTagTest method testExecuteButResetReturnSameInvocation.

public void testExecuteButResetReturnSameInvocation() throws Exception {
    Mock mockActionInv = new Mock(ActionInvocation.class);
    ActionTag tag = new ActionTag();
    tag.setPageContext(pageContext);
    tag.setNamespace("");
    tag.setName("testActionTagAction");
    tag.setExecuteResult(true);
    ActionContext.getContext().withActionInvocation((ActionInvocation) mockActionInv.proxy());
    ActionInvocation oldInvocation = ActionContext.getContext().getActionInvocation();
    assertNotNull(oldInvocation);
    tag.doStartTag();
    // tag clear components on doEndTag
    ActionComponent component = (ActionComponent) tag.getComponent();
    tag.doEndTag();
    assertSame(oldInvocation, ActionContext.getContext().getActionInvocation());
    // Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
    ActionTag freshTag = new ActionTag();
    freshTag.setPageContext(pageContext);
    assertFalse("Tag state after doEndTag() under default tag clear state is equal to new Tag with pageContext/parent set.  " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
}
Also used : ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionComponent(org.apache.struts2.components.ActionComponent) Mock(com.mockobjects.dynamic.Mock)

Example 2 with Mock

use of com.mockobjects.dynamic.Mock 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 3 with Mock

use of com.mockobjects.dynamic.Mock 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 4 with Mock

use of com.mockobjects.dynamic.Mock in project struts by apache.

the class AbstractTagTest method createMocks.

protected void createMocks() {
    action = this.getAction();
    container.inject(action);
    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();
    JspWriter jspWriter = new StrutsMockJspWriter(writer);
    servletContext.setRealPath(new File("nosuchfile.properties").getAbsolutePath());
    servletContext.setServletInfo("Resin");
    pageContext = new StrutsMockPageContext();
    pageContext.setRequest(request);
    pageContext.setResponse(response);
    pageContext.setJspWriter(jspWriter);
    pageContext.setServletContext(servletContext);
    mockContainer = new Mock(Container.class);
    MockDispatcher du = new MockDispatcher(pageContext.getServletContext(), new HashMap<>(), configurationManager);
    du.init();
    Dispatcher.setInstance(du);
    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.of(context).withServletRequest(request).withServletResponse(response).withServletContext(servletContext).bind();
}
Also used : ApplicationMap(org.apache.struts2.dispatcher.ApplicationMap) MockDispatcher(org.apache.struts2.dispatcher.MockDispatcher) RequestMap(org.apache.struts2.dispatcher.RequestMap) JspWriter(javax.servlet.jsp.JspWriter) Mock(com.mockobjects.dynamic.Mock) Container(com.opensymphony.xwork2.inject.Container) StringWriter(java.io.StringWriter) File(java.io.File)

Example 5 with Mock

use of com.mockobjects.dynamic.Mock in project struts by apache.

the class JspTemplateTest method testCheckBox_clearTagStateSet.

public void testCheckBox_clearTagStateSet() throws Exception {
    TestAction testAction = (TestAction) action;
    testAction.setFoo("true");
    CheckboxTag tag = new CheckboxTag();
    Mock rdMock = new Mock(RequestDispatcher.class);
    rdMock.expect("include", C.args(C.isA(HttpServletRequest.class), C.isA(HttpServletResponse.class)));
    RequestDispatcher dispatcher = (RequestDispatcher) rdMock.proxy();
    request.setupGetRequestDispatcher(dispatcher);
    // Explicitly request tag state clearing.
    tag.setPerformClearTagStateForTagPoolingServers(true);
    tag.setPageContext(pageContext);
    tag.setTemplate("/test/checkbox.jsp");
    tag.doStartTag();
    // Ensure component tag state clearing is set true (to match tag).
    setComponentTagClearTagState(tag, true);
    tag.doEndTag();
    rdMock.verify();
    // Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
    CheckboxTag freshTag = new CheckboxTag();
    freshTag.setPerformClearTagStateForTagPoolingServers(true);
    freshTag.setPageContext(pageContext);
    assertTrue("Tag state after doEndTag() inequal to new Tag with pageContext/parent set.  " + "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.", strutsBodyTagsAreReflectionEqual(tag, freshTag));
}
Also used : Mock(com.mockobjects.dynamic.Mock) RequestDispatcher(javax.servlet.RequestDispatcher) TestAction(org.apache.struts2.TestAction)

Aggregations

Mock (com.mockobjects.dynamic.Mock)91 HashMap (java.util.HashMap)14 ValidationException (com.opensymphony.xwork2.validator.ValidationException)12 StrutsException (org.apache.struts2.StrutsException)12 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)10 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 ActionContext (com.opensymphony.xwork2.ActionContext)8 TreeMap (java.util.TreeMap)8 ActionProxy (com.opensymphony.xwork2.ActionProxy)6 HttpParameters (org.apache.struts2.dispatcher.HttpParameters)6 MockHttpSession (org.springframework.mock.web.MockHttpSession)6 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)4 Container (com.opensymphony.xwork2.inject.Container)4 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)4 ModelDrivenAction2 (com.opensymphony.xwork2.test.ModelDrivenAction2)4 LinkedHashMap (java.util.LinkedHashMap)4 Action (com.opensymphony.xwork2.Action)3