Search in sources :

Example 56 with Mock

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

the class ConfigurationTest method testMultipleContainerProviders.

public void testMultipleContainerProviders() throws Exception {
    // to start from scratch
    configurationManager.destroyConfiguration();
    // to build basic configuration
    configurationManager.getConfiguration();
    Mock mockContainerProvider = new Mock(ContainerProvider.class);
    mockContainerProvider.expect("init", C.ANY_ARGS);
    mockContainerProvider.expect("register", C.ANY_ARGS);
    mockContainerProvider.matchAndReturn("equals", C.ANY_ARGS, false);
    mockContainerProvider.matchAndReturn("toString", "foo");
    mockContainerProvider.matchAndReturn("destroy", null);
    mockContainerProvider.expectAndReturn("needsReload", true);
    // the order of providers must be changed as just first is checked if reload is needed
    configurationManager.addContainerProvider((ContainerProvider) mockContainerProvider.proxy());
    XmlConfigurationProvider provider = new StrutsXmlConfigurationProvider("xwork-sample.xml");
    container.inject(provider);
    configurationManager.addContainerProvider(provider);
    Configuration config = null;
    try {
        config = configurationManager.getConfiguration();
    } catch (ConfigurationException e) {
        e.printStackTrace();
        fail();
    }
    RuntimeConfiguration configuration = config.getRuntimeConfiguration();
    // check that it has configuration from xml
    assertNotNull(configuration.getActionConfig("/foo/bar", "Bar"));
    mockContainerProvider.verify();
}
Also used : StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) Mock(com.mockobjects.dynamic.Mock)

Example 57 with Mock

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

the class ContentTypeHandlerManagerTest method testHandleResultNotModified.

public void testHandleResultNotModified() throws IOException {
    Mock mockHandlerXml = new Mock(ContentTypeHandler.class);
    mockHandlerXml.matchAndReturn("getExtension", "xml");
    mgr.handlersByExtension.put("xml", (ContentTypeHandler) mockHandlerXml.proxy());
    mgr.handleResult(invocation, new DefaultHttpHeaders().withStatus(SC_NOT_MODIFIED), new Object());
    assertEquals(0, mockResponse.getContentLength());
}
Also used : Mock(com.mockobjects.dynamic.Mock)

Example 58 with Mock

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

the class TemplateEngineManagerTest method setUp.

public void setUp() throws Exception {
    mgr = new TemplateEngineManager();
    mockContainer = new Mock(Container.class);
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(TemplateEngine.class), C.eq("jsp")), new JspTemplateEngine());
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(TemplateEngine.class), C.eq("ftl")), new FreemarkerTemplateEngine());
    mockContainer.matchAndReturn("getInstanceNames", C.args(C.eq(TemplateEngine.class)), new HashSet<String>() {

        {
            add("jsp");
            add("vm");
            add("ftl");
        }
    });
    mgr.setContainer((Container) mockContainer.proxy());
    mgr.setDefaultTemplateType("jsp");
}
Also used : Container(com.opensymphony.xwork2.inject.Container) TemplateEngineManager(org.apache.struts2.components.template.TemplateEngineManager) Mock(com.mockobjects.dynamic.Mock) JspTemplateEngine(org.apache.struts2.components.template.JspTemplateEngine) FreemarkerTemplateEngine(org.apache.struts2.components.template.FreemarkerTemplateEngine)

Example 59 with Mock

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

the class ServletDispatcherResultTest method testSimple.

public void testSimple() {
    ServletDispatcherResult view = new ServletDispatcherResult();
    view.setLocation("foo.jsp");
    Mock dispatcherMock = new Mock(RequestDispatcher.class);
    dispatcherMock.expect("forward", C.ANY_ARGS);
    Mock requestMock = new Mock(HttpServletRequest.class);
    requestMock.expectAndReturn("getAttribute", "struts.actiontag.invocation", null);
    requestMock.expectAndReturn("getAttribute", "javax.servlet.include.servlet_path", null);
    requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy());
    // this is a bad mock, but it works
    requestMock.expect("setAttribute", C.ANY_ARGS);
    // this is a bad mock, but it works
    requestMock.expect("setAttribute", C.ANY_ARGS);
    requestMock.matchAndReturn("getRequestURI", "foo.jsp");
    Mock responseMock = new Mock(HttpServletResponse.class);
    responseMock.expectAndReturn("isCommitted", Boolean.FALSE);
    ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy());
    ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
    try {
        view.execute(null);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    dispatcherMock.verify();
    requestMock.verify();
    dispatcherMock.verify();
}
Also used : ServletDispatcherResult(org.apache.struts2.result.ServletDispatcherResult) Mock(com.mockobjects.dynamic.Mock)

Example 60 with Mock

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

the class ServletDispatcherResultTest method testInclude.

public void testInclude() {
    ServletDispatcherResult view = new ServletDispatcherResult();
    view.setLocation("foo.jsp");
    Mock dispatcherMock = new Mock(RequestDispatcher.class);
    dispatcherMock.expect("include", C.ANY_ARGS);
    Mock requestMock = new Mock(HttpServletRequest.class);
    requestMock.expectAndReturn("getAttribute", "struts.actiontag.invocation", null);
    requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy());
    Mock responseMock = new Mock(HttpServletResponse.class);
    responseMock.expectAndReturn("isCommitted", Boolean.TRUE);
    ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy());
    ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
    try {
        view.execute(null);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    dispatcherMock.verify();
    requestMock.verify();
    dispatcherMock.verify();
}
Also used : ServletDispatcherResult(org.apache.struts2.result.ServletDispatcherResult) Mock(com.mockobjects.dynamic.Mock)

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