Search in sources :

Example 26 with Mock

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());
}
Also used : AbstractContentTypeHandler(org.apache.struts2.rest.handler.AbstractContentTypeHandler) ContentTypeHandler(org.apache.struts2.rest.handler.ContentTypeHandler) Mock(com.mockobjects.dynamic.Mock)

Example 27 with Mock

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();
}
Also used : ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) ActionSupport(com.opensymphony.xwork2.ActionSupport) Mock(com.mockobjects.dynamic.Mock) AnyConstraintMatcher(com.mockobjects.dynamic.AnyConstraintMatcher)

Example 28 with Mock

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

Example 29 with Mock

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);
}
Also used : ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ReflectionProvider(com.opensymphony.xwork2.util.reflection.ReflectionProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) Mock(com.mockobjects.dynamic.Mock)

Example 30 with Mock

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Mock(com.mockobjects.dynamic.Mock) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

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