Search in sources :

Example 71 with Mock

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

the class StaticContentLoaderTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    this.contentLoader = new DefaultStaticContentLoader();
    this.contentLoader.setStaticContentPath("/static");
    MockServletContext servletContext = new MockServletContext();
    req = new MockHttpServletRequest(servletContext);
    res = new MockHttpServletResponse();
    Mock hostConfigMock = new Mock(HostConfig.class);
    hostConfigMock.expectAndReturn("getInitParameter", C.args(C.eq("packages")), null);
    hostConfigMock.expectAndReturn("getInitParameter", C.args(C.eq("loggerFactory")), null);
    contentLoader.setEncoding("utf-8");
    contentLoader.setHostConfig((HostConfig) hostConfigMock.proxy());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Mock(com.mockobjects.dynamic.Mock)

Example 72 with Mock

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

the class DispatcherTest method testSetEncodingIfDiffer.

public void testSetEncodingIfDiffer() throws Exception {
    // given
    Mock mock = new Mock(HttpServletRequest.class);
    mock.expectAndReturn("getCharacterEncoding", "utf-8");
    mock.expectAndReturn("getHeader", "X-Requested-With", "");
    mock.expectAndReturn("getCharacterEncoding", "utf-8");
    HttpServletRequest req = (HttpServletRequest) mock.proxy();
    HttpServletResponse res = new MockHttpServletResponse();
    Dispatcher du = initDispatcher(new HashMap<String, String>() {

        {
            put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
        }
    });
    // when
    du.prepare(req, res);
    // then
    assertEquals(req.getCharacterEncoding(), "utf-8");
    assertEquals(res.getCharacterEncoding(), "utf-8");
    mock.verify();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Mock(com.mockobjects.dynamic.Mock) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 73 with Mock

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

the class DispatcherTest method testGetLocale_With_BadDefaultLocale_And_RuntimeException.

public void testGetLocale_With_BadDefaultLocale_And_RuntimeException() throws Exception {
    // Given
    Mock mock = new Mock(HttpServletRequest.class);
    MockHttpSession mockHttpSession = new MockHttpSession();
    // From Dispatcher prepare().
    mock.expectAndReturn("getCharacterEncoding", "utf-8");
    // From Dispatcher prepare().
    mock.expectAndReturn("getHeader", "X-Requested-With", "");
    // From Dispatcher prepare().
    mock.expectAndReturn("getLocale", Locale.UK);
    // From Dispatcher prepare().
    mock.expectAndReturn("getParameterMap", new HashMap<String, Object>());
    // From Dispatcher prepare().
    mock.expectAndReturn("getSession", false, mockHttpSession);
    // From createTestContextMap().
    mock.expectAndReturn("getSession", true, mockHttpSession);
    // From createTestContextMap().
    mock.expectAndThrow("getLocale", new IllegalStateException("Test theoretical state preventing HTTP Request Locale access"));
    HttpServletRequest request = (HttpServletRequest) mock.proxy();
    HttpServletResponse response = new MockHttpServletResponse();
    Dispatcher testDispatcher = initDispatcher(new HashMap<String, String>() {

        {
            put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
            // Set the Dispatcher defaultLocale to an invalid value.
            put(StrutsConstants.STRUTS_LOCALE, "This_is_not_a_valid_Locale_string");
        }
    });
    // When
    testDispatcher.prepare(request, response);
    Map<String, Object> contextMap = createTestContextMap(testDispatcher, request, response);
    // Then
    // Expect the system default value, when BOTH Dispatcher default Locale AND request access fail.
    assertEquals(Locale.getDefault(), contextMap.get(ActionContext.LOCALE));
    mock.verify();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Mock(com.mockobjects.dynamic.Mock) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 74 with Mock

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

the class DispatcherTest method testObjectFactoryDestroy.

public void testObjectFactoryDestroy() throws Exception {
    ConfigurationManager cm = new ConfigurationManager(Container.DEFAULT_NAME);
    Dispatcher du = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), cm);
    Mock mockConfiguration = new Mock(Configuration.class);
    cm.setConfiguration((Configuration) mockConfiguration.proxy());
    Mock mockContainer = new Mock(Container.class);
    String reloadConfigs = container.getInstance(String.class, StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD);
    mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD)), reloadConfigs);
    final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory();
    destroyedObjectFactory.setContainer((Container) mockContainer.proxy());
    mockContainer.expectAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), destroyedObjectFactory);
    mockConfiguration.expectAndReturn("getContainer", mockContainer.proxy());
    mockConfiguration.expectAndReturn("getContainer", mockContainer.proxy());
    mockConfiguration.expect("destroy");
    mockConfiguration.matchAndReturn("getPackageConfigs", new HashMap<String, PackageConfig>());
    du.init();
    assertFalse(destroyedObjectFactory.destroyed);
    du.cleanup();
    assertTrue(destroyedObjectFactory.destroyed);
    mockConfiguration.verify();
    mockContainer.verify();
}
Also used : ConfigurationManager(com.opensymphony.xwork2.config.ConfigurationManager) MockServletContext(org.springframework.mock.web.MockServletContext) Mock(com.mockobjects.dynamic.Mock) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 75 with Mock

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

the class DispatcherTest method testGetLocale_With_BadDefaultLocale_RequestLocale_en_UK.

public void testGetLocale_With_BadDefaultLocale_RequestLocale_en_UK() throws Exception {
    // Given
    Mock mock = new Mock(HttpServletRequest.class);
    MockHttpSession mockHttpSession = new MockHttpSession();
    // From Dispatcher prepare().
    mock.expectAndReturn("getCharacterEncoding", "utf-8");
    // From Dispatcher prepare().
    mock.expectAndReturn("getHeader", "X-Requested-With", "");
    // From Dispatcher prepare().
    mock.expectAndReturn("getLocale", Locale.UK);
    // From Dispatcher prepare().
    mock.expectAndReturn("getParameterMap", new HashMap<String, Object>());
    // From Dispatcher prepare().
    mock.expectAndReturn("getSession", false, mockHttpSession);
    // From createTestContextMap().
    mock.expectAndReturn("getSession", true, mockHttpSession);
    // From createTestContextMap().
    mock.expectAndReturn("getLocale", Locale.UK);
    HttpServletRequest request = (HttpServletRequest) mock.proxy();
    HttpServletResponse response = new MockHttpServletResponse();
    Dispatcher testDispatcher = initDispatcher(new HashMap<String, String>() {

        {
            put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
            // Set Dispatcher defaultLocale to an invalid value.
            put(StrutsConstants.STRUTS_LOCALE, "This_is_not_a_valid_Locale_string");
        }
    });
    // When
    testDispatcher.prepare(request, response);
    Map<String, Object> contextMap = createTestContextMap(testDispatcher, request, response);
    // Then
    // Expect the request set value from Mock.
    assertEquals(Locale.UK, contextMap.get(ActionContext.LOCALE));
    mock.verify();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Mock(com.mockobjects.dynamic.Mock) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

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