Search in sources :

Example 31 with Mock

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

the class DispatcherTest method testGetLocale_With_NullDefaultLocale_And_RuntimeException.

public void testGetLocale_With_NullDefaultLocale_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.CANADA_FRENCH);
    // 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 some 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");
        // Attempting to set StrutsConstants.STRUTS_LOCALE to null via parameters causes an NPE.
        }
    });
    // Force a null Struts default locale, otherwise we receive the default "de_DE" from the test configuration.
    testDispatcher.setDefaultLocale(null);
    // When
    testDispatcher.prepare(request, response);
    Map<String, Object> contextMap = createTestContextMap(testDispatcher, request, response);
    // Then
    // Expect the system default value when Mock request access fails.
    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 32 with Mock

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

the class DispatcherTest method testGetLocale_With_NullDefaultLocale.

public void testGetLocale_With_NullDefaultLocale() 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.CANADA_FRENCH);
    // 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.CANADA_FRENCH);
    HttpServletRequest request = (HttpServletRequest) mock.proxy();
    HttpServletResponse response = new MockHttpServletResponse();
    Dispatcher testDispatcher = initDispatcher(new HashMap<String, String>() {

        {
            put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
        // Attempting to set StrutsConstants.STRUTS_LOCALE to null here via parameters causes an NPE.
        }
    });
    // Force a null Struts default locale, otherwise we receive the default "de_DE" from the test configuration.
    testDispatcher.setDefaultLocale(null);
    // When
    testDispatcher.prepare(request, response);
    Map<String, Object> contextMap = createTestContextMap(testDispatcher, request, response);
    // Then
    // Expect the request set value from Mock.
    assertEquals(Locale.CANADA_FRENCH, 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 33 with Mock

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

the class DispatcherTest method testGetLocale_With_DefaultLocale_fr_CA.

public void testGetLocale_With_DefaultLocale_fr_CA() 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("getParameterMap", new HashMap<String, Object>());
    // From Dispatcher prepare().
    mock.expectAndReturn("getSession", false, mockHttpSession);
    // From createTestContextMap().
    mock.expectAndReturn("getSession", true, mockHttpSession);
    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 fr_CA.
            put(StrutsConstants.STRUTS_LOCALE, Locale.CANADA_FRENCH.toString());
        }
    });
    // When
    testDispatcher.prepare(request, response);
    Map<String, Object> contextMap = createTestContextMap(testDispatcher, request, response);
    // Then
    // Expect the Dispatcher defaultLocale value.
    assertEquals(Locale.CANADA_FRENCH, 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 34 with Mock

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

the class DispatcherTest method testInterceptorDestroy.

public void testInterceptorDestroy() throws Exception {
    Mock mockInterceptor = new Mock(Interceptor.class);
    mockInterceptor.matchAndReturn("hashCode", 0);
    mockInterceptor.expect("destroy");
    InterceptorMapping interceptorMapping = new InterceptorMapping("test", (Interceptor) mockInterceptor.proxy());
    InterceptorStackConfig isc = new InterceptorStackConfig.Builder("test").addInterceptor(interceptorMapping).build();
    PackageConfig packageConfig = new PackageConfig.Builder("test").addInterceptorStackConfig(isc).build();
    Map<String, PackageConfig> packageConfigs = new HashMap<String, PackageConfig>();
    packageConfigs.put("test", packageConfig);
    Mock mockContainer = new Mock(Container.class);
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory());
    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);
    Mock mockConfiguration = new Mock(Configuration.class);
    mockConfiguration.matchAndReturn("getPackageConfigs", packageConfigs);
    mockConfiguration.matchAndReturn("getContainer", mockContainer.proxy());
    mockConfiguration.expect("destroy");
    ConfigurationManager configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
    configurationManager.setConfiguration((Configuration) mockConfiguration.proxy());
    Dispatcher dispatcher = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), configurationManager);
    dispatcher.init();
    dispatcher.cleanup();
    mockInterceptor.verify();
    mockContainer.verify();
    mockConfiguration.verify();
}
Also used : HashMap(java.util.HashMap) Mock(com.mockobjects.dynamic.Mock) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) MockServletContext(org.springframework.mock.web.MockServletContext) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) ConfigurationManager(com.opensymphony.xwork2.config.ConfigurationManager)

Example 35 with Mock

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

the class SessionMapTest method setUp.

protected void setUp() throws Exception {
    sessionMock = new Mock(HttpSession.class);
    sessionMock.matchAndReturn("getId", "1");
    requestMock = new Mock(HttpServletRequest.class);
    requestMock.matchAndReturn("getSession", new Constraint[] { new IsEqual(Boolean.FALSE) }, sessionMock.proxy());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) Mock(com.mockobjects.dynamic.Mock) IsEqual(com.mockobjects.constraint.IsEqual)

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