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();
}
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();
}
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();
}
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();
}
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());
}
Aggregations