use of com.mockobjects.dynamic.Mock in project struts by apache.
the class ContentTypeHandlerManagerTest method testHandleRequestContentType.
/**
* Assert that the request content-type and differ from the response content type
*/
public void testHandleRequestContentType() {
Mock mockHandlerForm = new Mock(ContentTypeHandler.class);
mockHandlerForm.matchAndReturn("getExtension", null);
mockHandlerForm.matchAndReturn("getContentType", "application/x-www-form-urlencoded");
mockHandlerForm.matchAndReturn("toString", "x-www-form-urlencoded");
Mock mockHandlerJson = new Mock(ContentTypeHandler.class);
mockHandlerJson.matchAndReturn("getExtension", "json");
mockHandlerJson.matchAndReturn("getContentType", "application/javascript");
mockHandlerJson.matchAndReturn("toString", "json");
Mock mockContainer = new Mock(Container.class);
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("x-www-form-urlencoded")), mockHandlerForm.proxy());
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ContentTypeHandler.class), C.eq("json")), mockHandlerJson.proxy());
mockContainer.matchAndReturn("getInstance", C.args(C.eq(String.class), C.eq("struts.rest.handlerOverride.json")), null);
mockContainer.expectAndReturn("getInstanceNames", C.args(C.eq(ContentTypeHandler.class)), new HashSet<>(Arrays.asList("x-www-form-urlencoded", "json")));
mockRequest.setContentType(FormUrlEncodedHandler.CONTENT_TYPE);
mockRequest.setContent("a=1&b=2".getBytes(StandardCharsets.UTF_8));
mgr.setContainer((Container) mockContainer.proxy());
ContentTypeHandler handler = mgr.getHandlerForRequest(mockRequest);
assertEquals("application/x-www-form-urlencoded", handler.getContentType());
}
use of com.mockobjects.dynamic.Mock in project struts by apache.
the class RestWorkflowInterceptorTest method testCustomValidationFailureStatusCode.
public void testCustomValidationFailureStatusCode() throws Exception {
RestWorkflowInterceptor wf = new RestWorkflowInterceptor();
ActionSupport action = new ActionSupport();
action.addActionError("some error");
wf.setValidationFailureStatusCode("666");
Mock mockActionInvocation = new Mock(ActionInvocation.class);
Mock mockActionProxy = new Mock(ActionProxy.class);
mockActionProxy.expectAndReturn("getConfig", null);
mockActionInvocation.expectAndReturn("getAction", action);
Mock mockContentTypeHandlerManager = new Mock(ContentTypeHandlerManager.class);
mockContentTypeHandlerManager.expectAndReturn("handleResult", new AnyConstraintMatcher() {
public boolean matches(Object[] args) {
DefaultHttpHeaders headers = (DefaultHttpHeaders) args[1];
return 666 == headers.getStatus();
}
}, null);
wf.setContentTypeHandlerManager((ContentTypeHandlerManager) mockContentTypeHandlerManager.proxy());
ActionContext.of(new HashMap<>()).withActionMapping(new ActionMapping()).bind();
wf.doIntercept((ActionInvocation) mockActionInvocation.proxy());
mockContentTypeHandlerManager.verify();
mockActionInvocation.verify();
}
use of com.mockobjects.dynamic.Mock in project struts by apache.
the class AnnotationValidationInterceptorTest method setUp.
public void setUp() throws Exception {
super.setUp();
test = new TestAction();
interceptor = new AnnotationValidationInterceptor();
container.inject(interceptor);
config = new ActionConfig.Builder("", "foo", "").build();
mockActionInvocation = new Mock(ActionInvocation.class);
mockActionProxy = new Mock(ActionProxy.class);
mockActionInvocation.matchAndReturn("getProxy", (ActionProxy) mockActionProxy.proxy());
mockActionInvocation.matchAndReturn("getAction", test);
mockActionInvocation.expect("invoke");
ActionContext.getContext().withActionInvocation((ActionInvocation) mockActionInvocation.proxy());
}
use of com.mockobjects.dynamic.Mock in project struts by apache.
the class HttpHeaderResultTest method setUp.
protected void setUp() throws Exception {
super.setUp();
result = new HttpHeaderResult();
responseMock = new Mock(HttpServletResponse.class);
response = (HttpServletResponse) responseMock.proxy();
invocationMock = new Mock(ActionInvocation.class);
invocationMock.expectAndReturn("getInvocationContext", ActionContext.getContext());
invocationMock.expectAndReturn("getStack", ActionContext.getContext().getValueStack());
invocation = (ActionInvocation) invocationMock.proxy();
reflectionProvider = container.getInstance(ReflectionProvider.class);
ServletActionContext.setResponse(response);
}
use of com.mockobjects.dynamic.Mock in project struts by apache.
the class DispatcherTest method testGetLocale_With_DefaultLocale_FromConfiguration.
public void testGetLocale_With_DefaultLocale_FromConfiguration() 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("getParameterMap", new HashMap<String, Object>());
// From Dispatcher prepare().
mock.expectAndReturn("getSession", false, mockHttpSession);
// From createTestContextMap().
mock.expectAndReturn("getSession", true, mockHttpSession);
HttpServletRequest request = (HttpServletRequest) mock.proxy();
HttpServletResponse response = new MockHttpServletResponse();
Dispatcher testDispatcher = initDispatcher(new HashMap<String, String>() {
{
put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
// Not setting a Struts Locale here, so we should receive the default "de_DE" from the test configuration.
}
});
// When
testDispatcher.prepare(request, response);
Map<String, Object> contextMap = createTestContextMap(testDispatcher, request, response);
// Then
// Expect the Dispatcher defaultLocale value "de_DE" from the test configuration.
assertEquals(Locale.GERMANY, contextMap.get(ActionContext.LOCALE));
mock.verify();
}
Aggregations