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();
}
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;
}
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);
}
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);
}
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&hello=earth&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);
}
Aggregations