use of com.mockobjects.dynamic.Mock in project commons-configuration by apache.
the class MockInitialContextFactory method getInitialContext.
/**
* Creates a {@code Context} object that is backed by a mock object. The mock context can be queried for the values of
* certain test properties. It also supports listing the contained (sub) properties.
*
* @param env the environment
* @return the context mock
*/
@Override
public Context getInitialContext(@SuppressWarnings("rawtypes") final Hashtable env) throws NamingException {
final boolean useCycles = env.containsKey(PROP_CYCLES);
final Mock mockTopCtx = createCtxMock(PREFIX);
final Mock mockCycleCtx = createCtxMock("");
final Mock mockPrfxCtx = createCtxMock("");
final Mock mockBaseCtx = new Mock(Context.class);
mockBaseCtx.matchAndReturn(METHOD_LOOKUP, C.eq(""), mockTopCtx.proxy());
mockBaseCtx.matchAndReturn(METHOD_LOOKUP, C.eq("test"), mockPrfxCtx.proxy());
mockTopCtx.matchAndReturn(METHOD_LOOKUP, C.eq("test"), mockPrfxCtx.proxy());
mockPrfxCtx.matchAndReturn(METHOD_LIST, C.eq(""), createEnumMock(mockPrfxCtx, PROP_NAMES, PROP_VALUES).proxy());
if (useCycles) {
mockTopCtx.matchAndReturn(METHOD_LOOKUP, C.eq("cycle"), mockCycleCtx.proxy());
mockTopCtx.matchAndReturn(METHOD_LIST, C.eq(""), createEnumMock(mockTopCtx, new String[] { "test", "cycle" }, new Object[] { mockPrfxCtx.proxy(), mockCycleCtx.proxy() }).proxy());
final Mock mockEnum = createEnumMock(mockCycleCtx, PROP_NAMES, PROP_VALUES, false);
addEnumPair(mockEnum, "cycleCtx", mockCycleCtx.proxy());
closeEnum(mockEnum);
mockCycleCtx.matchAndReturn(METHOD_LIST, C.eq(""), mockEnum.proxy());
mockCycleCtx.matchAndReturn(METHOD_LOOKUP, C.eq("cycleCtx"), mockCycleCtx.proxy());
} else {
mockTopCtx.matchAndReturn(METHOD_LIST, C.eq(""), createEnumMock(mockTopCtx, new String[] { "test" }, new Object[] { mockPrfxCtx.proxy() }).proxy());
}
return (Context) mockBaseCtx.proxy();
}
use of com.mockobjects.dynamic.Mock in project struts by apache.
the class DefaultUrlHelperTest method testDoNotForceAddSchemeHostAndPort.
public void testDoNotForceAddSchemeHostAndPort() throws Exception {
String expectedUrl = "/contextPath/path1/path2/myAction.action";
Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
mockHttpServletRequest.expectAndReturn("getScheme", "http");
mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
mockHttpServletRequest.expectAndReturn("getContextPath", "/contextPath");
Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl, expectedUrl);
String result = urlHelper.buildUrl("/path1/path2/myAction.action", (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), null, "http", true, true, false);
assertEquals(expectedUrl, result);
}
use of com.mockobjects.dynamic.Mock in project struts by apache.
the class DefaultUrlHelperTest method testSwitchToHttpsScheme.
/**
* The UrlHelper should build a URL that starts with "https" followed by the server name when the scheme of the
* current request is "http" and the port for the "https" scheme is 443.
*/
public void testSwitchToHttpsScheme() {
String expectedString = "https://www.mydomain.com/mywebapp/MyAction.action?foo=bar&hello=earth&hello=mars";
Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
mockHttpServletRequest.expectAndReturn("getScheme", "http");
mockHttpServletRequest.expectAndReturn("getServerPort", 80);
mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
String actionName = "/MyAction.action";
TreeMap params = new TreeMap();
params.put("hello", new String[] { "earth", "mars" });
params.put("foo", "bar");
String urlString = urlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
assertEquals(expectedString, urlString);
}
use of com.mockobjects.dynamic.Mock in project struts by apache.
the class DefaultUrlHelperTest method testBuildWithRootContext.
public void testBuildWithRootContext() {
String expectedUrl = "/MyAction.action";
Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
mockHttpServletRequest.expectAndReturn("getContextPath", "/");
mockHttpServletRequest.expectAndReturn("getScheme", "http");
Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl, expectedUrl);
String actualUrl = urlHelper.buildUrl(expectedUrl, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), new HashMap());
assertEquals(expectedUrl, actualUrl);
}
use of com.mockobjects.dynamic.Mock in project struts by apache.
the class DefaultUrlHelperTest method testForceAddSchemeHostAndPortWithNonStandardPort.
public void testForceAddSchemeHostAndPortWithNonStandardPort() throws Exception {
String expectedUrl = "http://localhost:9090/contextPath/path1/path2/myAction.action";
Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
mockHttpServletRequest.expectAndReturn("getScheme", "http");
mockHttpServletRequest.expectAndReturn("getServerName", "localhost");
mockHttpServletRequest.expectAndReturn("getContextPath", "/contextPath");
mockHttpServletRequest.expectAndReturn("getServerPort", 9090);
Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
mockHttpServletResponse.expectAndReturn("encodeURL", expectedUrl, expectedUrl);
String result = urlHelper.buildUrl("/path1/path2/myAction.action", (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), null, "http", true, true, true);
assertEquals(expectedUrl, result);
mockHttpServletRequest.verify();
}
Aggregations