Search in sources :

Example 11 with Mock

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

the class AnnotationParameterFilterInterceptorTest method testBlockingByDefault.

/**
 * Only "name" should remain in the parameter map.  All others
 * should be removed
 */
public void testBlockingByDefault() throws Exception {
    Map<String, Object> parameterMap = new HashMap<>();
    parameterMap.put("job", "Baker");
    parameterMap.put("name", "Martin");
    ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
    actionContext.setParameters(HttpParameters.create(parameterMap).build());
    Action action = new BlockingByDefaultAction();
    stack.push(action);
    Mock mockInvocation = new Mock(ActionInvocation.class);
    mockInvocation.expectAndReturn("getInvocationContext", actionContext);
    mockInvocation.matchAndReturn("getAction", action);
    mockInvocation.matchAndReturn("getStack", stack);
    mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
    mockInvocation.expectAndReturn("getInvocationContext", actionContext);
    mockInvocation.expectAndReturn("getInvocationContext", actionContext);
    ActionInvocation invocation = (ActionInvocation) mockInvocation.proxy();
    AnnotationParameterFilterInterceptor interceptor = new AnnotationParameterFilterInterceptor();
    interceptor.intercept(invocation);
    HttpParameters parameters = invocation.getInvocationContext().getParameters();
    assertEquals("Parameter map should contain one entry", 1, parameters.keySet().size());
    assertFalse(parameters.get("job").isDefined());
    assertTrue(parameters.get("name").isDefined());
}
Also used : Action(com.opensymphony.xwork2.Action) HttpParameters(org.apache.struts2.dispatcher.HttpParameters) HashMap(java.util.HashMap) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionContext(com.opensymphony.xwork2.ActionContext) Mock(com.mockobjects.dynamic.Mock)

Example 12 with Mock

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

the class ExceptionMappingInterceptorTest method testThrownExceptionNoMatchLoggingCategoryLevelInfo.

public void testThrownExceptionNoMatchLoggingCategoryLevelInfo() {
    this.setupWithoutExceptionMappings();
    Mock action = new Mock(Action.class);
    Exception exception = new Exception("test");
    mockInvocation.expectAndThrow("invoke", exception);
    mockInvocation.matchAndReturn("getAction", action.proxy());
    try {
        interceptor.setLogEnabled(true);
        interceptor.setLogCategory("showcase.unhandled");
        interceptor.setLogLevel("info");
        interceptor.intercept(invocation);
        fail("Should not have reached this point.");
    } catch (Exception e) {
        assertEquals(e, exception);
    }
}
Also used : Mock(com.mockobjects.dynamic.Mock) StrutsException(org.apache.struts2.StrutsException) ValidationException(com.opensymphony.xwork2.validator.ValidationException)

Example 13 with Mock

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

the class ExceptionMappingInterceptorTest method setUpWithExceptionMappings.

private void setUpWithExceptionMappings() {
    ActionConfig actionConfig = new ActionConfig.Builder("", "", "").addExceptionMapping(new ExceptionMappingConfig.Builder("xwork", "org.apache.struts2.StrutsException", "spooky").build()).addExceptionMapping(new ExceptionMappingConfig.Builder("throwable", "java.lang.Throwable", "throwable").build()).build();
    Mock actionProxy = new Mock(ActionProxy.class);
    actionProxy.expectAndReturn("getConfig", actionConfig);
    mockInvocation.expectAndReturn("getProxy", actionProxy.proxy());
    invocation = (ActionInvocation) mockInvocation.proxy();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig) Mock(com.mockobjects.dynamic.Mock)

Example 14 with Mock

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

the class ExceptionMappingInterceptorTest method testThrownExceptionNoMatchLoggingCategory.

public void testThrownExceptionNoMatchLoggingCategory() {
    this.setupWithoutExceptionMappings();
    Mock action = new Mock(Action.class);
    Exception exception = new Exception("test");
    mockInvocation.expectAndThrow("invoke", exception);
    mockInvocation.matchAndReturn("getAction", action.proxy());
    try {
        interceptor.setLogEnabled(true);
        interceptor.setLogCategory("showcase.unhandled");
        interceptor.intercept(invocation);
        fail("Should not have reached this point.");
    } catch (Exception e) {
        assertEquals(e, exception);
    }
}
Also used : Mock(com.mockobjects.dynamic.Mock) StrutsException(org.apache.struts2.StrutsException) ValidationException(com.opensymphony.xwork2.validator.ValidationException)

Example 15 with Mock

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

the class ExceptionMappingInterceptorTest method testThrownExceptionMatching.

public void testThrownExceptionMatching() throws Exception {
    this.setUpWithExceptionMappings();
    Mock action = new Mock(Action.class);
    Exception exception = new StrutsException("test");
    mockInvocation.expectAndThrow("invoke", exception);
    mockInvocation.matchAndReturn("getAction", action.proxy());
    String result = interceptor.intercept(invocation);
    assertNotNull(stack.findValue("exception"));
    assertEquals(stack.findValue("exception"), exception);
    assertEquals(result, "spooky");
    // is on top of the root
    ExceptionHolder holder = (ExceptionHolder) stack.getRoot().get(0);
    // to invoke the method for unit test
    assertNotNull(holder.getExceptionStack());
}
Also used : StrutsException(org.apache.struts2.StrutsException) Mock(com.mockobjects.dynamic.Mock) StrutsException(org.apache.struts2.StrutsException) ValidationException(com.opensymphony.xwork2.validator.ValidationException)

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