Search in sources :

Example 46 with Mock

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();
}
Also used : Context(javax.naming.Context) Mock(com.mockobjects.dynamic.Mock)

Example 47 with Mock

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);
}
Also used : Mock(com.mockobjects.dynamic.Mock)

Example 48 with Mock

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);
}
Also used : TreeMap(java.util.TreeMap) Mock(com.mockobjects.dynamic.Mock)

Example 49 with Mock

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);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Mock(com.mockobjects.dynamic.Mock)

Example 50 with Mock

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();
}
Also used : Mock(com.mockobjects.dynamic.Mock)

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