Search in sources :

Example 36 with Mock

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

the class ServletDispatcherResultTest method testWithParameter.

public void testWithParameter() {
    ServletDispatcherResult view = container.inject(ServletDispatcherResult.class);
    view.setLocation("foo.jsp?bar=1");
    Mock dispatcherMock = new Mock(RequestDispatcher.class);
    dispatcherMock.expect("forward", C.ANY_ARGS);
    Mock requestMock = new Mock(HttpServletRequest.class);
    requestMock.expectAndReturn("getAttribute", "struts.actiontag.invocation", null);
    requestMock.expectAndReturn("getAttribute", "javax.servlet.include.servlet_path", null);
    requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp?bar=1")), dispatcherMock.proxy());
    // this is a bad mock, but it works
    requestMock.expect("setAttribute", C.ANY_ARGS);
    // this is a bad mock, but it works
    requestMock.expect("setAttribute", C.ANY_ARGS);
    requestMock.matchAndReturn("getRequestURI", "foo.jsp");
    Mock responseMock = new Mock(HttpServletResponse.class);
    responseMock.expectAndReturn("isCommitted", Boolean.FALSE);
    ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy());
    ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
    MockActionInvocation mockActionInvocation = new MockActionInvocation();
    mockActionInvocation.setInvocationContext(ActionContext.getContext());
    mockActionInvocation.setStack(container.getInstance(ValueStackFactory.class).createValueStack());
    try {
        view.execute(mockActionInvocation);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    assertTrue(mockActionInvocation.getInvocationContext().getParameters().contains("bar"));
    assertEquals("1", mockActionInvocation.getInvocationContext().getParameters().get("bar").getValue());
    assertEquals("1", ((HttpParameters) mockActionInvocation.getInvocationContext().getContextMap().get("parameters")).get("bar").getValue());
    dispatcherMock.verify();
    requestMock.verify();
    dispatcherMock.verify();
}
Also used : ServletDispatcherResult(org.apache.struts2.result.ServletDispatcherResult) HttpParameters(org.apache.struts2.dispatcher.HttpParameters) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) Mock(com.mockobjects.dynamic.Mock)

Example 37 with Mock

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

the class ServletRedirectResultTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    configurationManager.getConfiguration().addPackageConfig("foo", new PackageConfig.Builder("foo").namespace("/namespace").build());
    view = new ServletRedirectResult();
    container.inject(view);
    responseMock = new Mock(HttpServletResponse.class);
    requestMock = new Mock(HttpServletRequest.class);
    requestMock.matchAndReturn("getContextPath", "/context");
    ResultConfig resultConfig = new ResultConfig.Builder("", "").build();
    Map<String, ResultConfig> results = new HashMap<>();
    results.put("myResult", resultConfig);
    ActionConfig actionConfig = new ActionConfig.Builder("", "", "").addResultConfigs(results).build();
    ActionContext ac = ActionContext.getContext();
    ac.withServletRequest((HttpServletRequest) requestMock.proxy());
    ac.withServletResponse((HttpServletResponse) responseMock.proxy());
    MockActionInvocation ai = new MockActionInvocation();
    ai.setInvocationContext(ac);
    ai.setResultCode("myResult");
    ActionProxy mockActionProxy = createNiceMock(ActionProxy.class);
    ai.setProxy(mockActionProxy);
    expect(mockActionProxy.getConfig()).andReturn(actionConfig).anyTimes();
    replay(mockActionProxy);
    ai.setStack(ActionContext.getContext().getValueStack());
    this.ai = ai;
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) Mock(com.mockobjects.dynamic.Mock) EasyMock.createMock(org.easymock.EasyMock.createMock) EasyMock.createNiceMock(org.easymock.EasyMock.createNiceMock) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest)

Example 38 with Mock

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

the class InvocationSessionStoreTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    stack = ActionContext.getContext().getValueStack();
    session = new HashMap<>();
    ActionContext actionContext = ActionContext.of(stack.getContext()).withSession(session).withValueStack(stack).bind();
    invocationMock = new Mock(ActionInvocation.class);
    invocation = (ActionInvocation) invocationMock.proxy();
    invocationMock.matchAndReturn("getInvocationContext", actionContext);
    invocationMock.matchAndReturn("getStack", stack);
    Mock proxyMock = new Mock(ActionProxy.class);
    proxyMock.matchAndReturn("getInvocation", invocation);
    ActionProxy proxy = (ActionProxy) proxyMock.proxy();
    invocationMock.matchAndReturn("getProxy", proxy);
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionContext(com.opensymphony.xwork2.ActionContext) Mock(com.mockobjects.dynamic.Mock)

Example 39 with Mock

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

the class DefaultUrlHelperTest method testForwardedRequest.

/**
 * 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. When the request has been forwarded
 * in a Servlet 2.4 container, the UrlHelper should use the javax.servlet.forward.request_uri request attribute
 * instead of a call to HttpServletRequest#getRequestURI().
 */
public void testForwardedRequest() {
    String expectedString = "https://www.example.com/mywebapp/product/widget/promo.html";
    Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    mockHttpServletRequest.expectAndReturn("getServerName", "www.example.com");
    mockHttpServletRequest.expectAndReturn("getScheme", "http");
    mockHttpServletRequest.expectAndReturn("getServerPort", 80);
    mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
    mockHttpServletRequest.expectAndReturn("getAttribute", "javax.servlet.forward.request_uri", "/mywebapp/product/widget/");
    mockHttpServletRequest.expectAndReturn("getRequestURI", "/mywebapp/");
    Mock mockHttpServletResponse = new Mock(HttpServletResponse.class);
    mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
    String actionName = "promo.html";
    Map params = new TreeMap();
    String urlString = urlHelper.buildUrl(actionName, (HttpServletRequest) mockHttpServletRequest.proxy(), (HttpServletResponse) mockHttpServletResponse.proxy(), params, "https", true, true);
    assertEquals(expectedString, urlString);
}
Also used : TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Mock(com.mockobjects.dynamic.Mock)

Example 40 with Mock

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

the class DefaultUrlHelperTest method testSwitchToHttpNonDefaultPort.

/**
 * This test is similar to {@link #testSwitchToHttpScheme()} with the HTTP port equal to 7001 and the HTTPS port
 * equal to port 7002.
 */
public void testSwitchToHttpNonDefaultPort() {
    String expectedString = "http://www.mydomain.com:7001/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
    urlHelper.setHttpPort("7001");
    urlHelper.setHttpsPort("7002");
    Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
    mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
    mockHttpServletRequest.expectAndReturn("getScheme", "https");
    mockHttpServletRequest.expectAndReturn("getServerPort", 7002);
    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, "http", true, true);
    assertEquals(expectedString, urlString);
}
Also used : TreeMap(java.util.TreeMap) 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