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