Search in sources :

Example 76 with Mock

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

the class ActionComponentTest method testCreateParametersForContext.

public void testCreateParametersForContext() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    Mock mockValueStack = new Mock(ValueStack.class);
    HashMap<String, Object> ctx = new HashMap<>();
    mockValueStack.expectAndReturn("getContext", ctx);
    mockValueStack.expectAndReturn("getContext", ctx);
    mockValueStack.expectAndReturn("getActionContext", ActionContext.getContext());
    ActionComponent comp = new ActionComponent((ValueStack) mockValueStack.proxy(), req, res);
    comp.addParameter("foo", "bar");
    comp.addParameter("baz", new String[] { "jim", "sarah" });
    HttpParameters params = comp.createParametersForContext();
    assertNotNull(params);
    assertEquals(2, params.keySet().size());
    assertEquals("bar", params.get("foo").getValue());
    assertEquals(2, params.get("baz").getMultipleValues().length);
    mockValueStack.verify();
}
Also used : HttpParameters(org.apache.struts2.dispatcher.HttpParameters) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Mock(com.mockobjects.dynamic.Mock)

Example 77 with Mock

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

the class StaticParametersInterceptorTest method testParameterizable.

public void testParameterizable() throws Exception {
    Mock mock = new Mock(Parameterizable.class);
    MockActionInvocation mai = new MockActionInvocation();
    MockActionProxy map = new MockActionProxy();
    ActionConfig ac = new ActionConfig.Builder("", "", "").build();
    Map params = ac.getParams();
    map.setConfig(ac);
    mai.setProxy(map);
    mai.setAction(mock.proxy());
    mock.expect("setParams", params);
    interceptor.intercept(mai);
    mock.verify();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) Map(java.util.Map) HashMap(java.util.HashMap) Mock(com.mockobjects.dynamic.Mock) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 78 with Mock

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

the class AnnotationParameterFilterInterceptorTest method testAllowingByDefaultWithModel.

/**
 * "name" should be removed from the map, as it is blocked.
 * All other parameters should remain
 */
public void testAllowingByDefaultWithModel() throws Exception {
    Map<String, Object> parameterMap = new HashMap<>();
    parameterMap.put("job", "Baker");
    parameterMap.put("name", "Martin");
    parameterMap.put("m1", "s1");
    parameterMap.put("m2", "s2");
    ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
    actionContext.setParameters(HttpParameters.create(parameterMap).build());
    stack.push(new AllowingByDefaultModel());
    Mock mockInvocation = new Mock(ActionInvocation.class);
    mockInvocation.expectAndReturn("getInvocationContext", actionContext);
    mockInvocation.matchAndReturn("getAction", new AllowingByDefaultAction());
    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 two entries", 2, parameters.keySet().size());
    assertTrue(parameters.get("job").isDefined());
    assertFalse(parameters.get("name").isDefined());
    assertFalse(parameters.get("m1").isDefined());
    assertTrue(parameters.get("m2").isDefined());
}
Also used : 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 79 with Mock

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

the class AnnotationParameterFilterInterceptorTest method testBlockingByDefaultWithModel.

/**
 * Only "name" should remain in the parameter map.  All others
 * should be removed
 */
public void testBlockingByDefaultWithModel() throws Exception {
    Map<String, Object> parameterMap = new HashMap<>();
    parameterMap.put("job", "Baker");
    parameterMap.put("name", "Martin");
    parameterMap.put("m1", "s1");
    parameterMap.put("m2", "s2");
    ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
    actionContext.setParameters(HttpParameters.create(parameterMap).build());
    stack.push(new BlockingByDefaultModel());
    Mock mockInvocation = new Mock(ActionInvocation.class);
    mockInvocation.expectAndReturn("getInvocationContext", actionContext);
    mockInvocation.matchAndReturn("getAction", new BlockingByDefaultAction());
    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 two entries", 2, parameters.keySet().size());
    assertFalse(parameters.get("job").isDefined());
    assertTrue(parameters.get("name").isDefined());
    assertTrue(parameters.get("m1").isDefined());
    assertFalse(parameters.get("m2").isDefined());
}
Also used : 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 80 with Mock

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

the class AnnotationParameterFilterInterceptorTest method testAllowingByDefault.

/**
 * "name" should be removed from the map, as it is blocked.
 * All other parameters should remain
 */
public void testAllowingByDefault() 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 AllowingByDefaultAction();
    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("Paramwter map should contain one entry", 1, parameters.keySet().size());
    assertTrue(parameters.get("job").isDefined());
    assertFalse(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)

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