Search in sources :

Example 76 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class MessageStorePreResultListenerTest method testStoreMessage.

public void testStoreMessage() {
    MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
    interceptor.setAllowRequestParameterSwitch(true);
    interceptor.setOperationMode(MessageStoreInterceptor.STORE_MODE);
    MessageStorePreResultListener listener = new MessageStorePreResultListener();
    listener.init(interceptor);
    Map<String, Object> sessionMap = new LinkedHashMap<>();
    ActionSupport action = new ActionSupport();
    action.addActionError("some action error 1");
    action.addActionError("some action error 2");
    action.addActionMessage("some action message 1");
    action.addActionMessage("some action message 2");
    action.addFieldError("field1", "some field error 1");
    action.addFieldError("field2", "some field error 2");
    ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
    actionContext.setParameters(HttpParameters.create().build());
    actionContext.setSession(sessionMap);
    HttpSession mockedSession = EasyMock.createControl().createMock(HttpSession.class);
    HttpServletRequest mockedRequest = EasyMock.createControl().createMock(HttpServletRequest.class);
    mockedRequest.getSession(false);
    EasyMock.expectLastCall().andReturn(mockedSession);
    EasyMock.expectLastCall().once();
    ServletActionContext.setRequest(mockedRequest);
    EasyMock.replay(mockedRequest);
    HttpServletResponse mockedResponse = EasyMock.createControl().createMock(HttpServletResponse.class);
    mockedResponse.isCommitted();
    EasyMock.expectLastCall().andReturn(false);
    EasyMock.expectLastCall().once();
    ServletActionContext.setResponse(mockedResponse);
    EasyMock.replay(mockedResponse);
    // Mock (ActionInvocation)
    ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
    mockActionInvocation.getInvocationContext();
    EasyMock.expectLastCall().andReturn(actionContext);
    EasyMock.expectLastCall().anyTimes();
    mockActionInvocation.getAction();
    EasyMock.expectLastCall().andReturn(action);
    mockActionInvocation.getProxy();
    MockActionProxy actionProxy = new MockActionProxy();
    ResultConfig resultConfig = new ResultConfig.Builder(Action.SUCCESS, ServletRedirectResult.class.getName()).build();
    ActionConfig actionConfig = new ActionConfig.Builder("", "test", action.getClass().getName()).addResultConfig(resultConfig).build();
    actionProxy.setConfig(actionConfig);
    EasyMock.expectLastCall().andReturn(actionProxy);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(mockActionInvocation);
    interceptor.init();
    listener.beforeResult(mockActionInvocation, Action.SUCCESS);
    assertEquals(sessionMap.size(), 3);
    assertTrue(sessionMap.containsKey(MessageStoreInterceptor.actionErrorsSessionKey));
    assertTrue(sessionMap.containsKey(MessageStoreInterceptor.actionMessagesSessionKey));
    assertTrue(sessionMap.containsKey(MessageStoreInterceptor.fieldErrorsSessionKey));
    List actionErrors = (List) sessionMap.get(MessageStoreInterceptor.actionErrorsSessionKey);
    List actionMessages = (List) sessionMap.get(MessageStoreInterceptor.actionMessagesSessionKey);
    Map fieldErrors = (Map) sessionMap.get(MessageStoreInterceptor.fieldErrorsSessionKey);
    assertEquals(actionErrors.size(), 2);
    assertEquals(actionMessages.size(), 2);
    assertEquals(fieldErrors.size(), 2);
    assertTrue(actionErrors.contains("some action error 1"));
    assertTrue(actionErrors.contains("some action error 2"));
    assertTrue(actionMessages.contains("some action message 1"));
    assertTrue(actionMessages.contains("some action message 2"));
    assertTrue(fieldErrors.containsKey("field1"));
    assertTrue(fieldErrors.containsKey("field2"));
    assertEquals(((List) fieldErrors.get("field1")).size(), 1);
    assertEquals(((List) fieldErrors.get("field2")).size(), 1);
    assertEquals(((List) fieldErrors.get("field1")).get(0), "some field error 1");
    assertEquals(((List) fieldErrors.get("field2")).get(0), "some field error 2");
    EasyMock.verify(mockActionInvocation);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpSession(javax.servlet.http.HttpSession) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionSupport(com.opensymphony.xwork2.ActionSupport) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 77 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class StrutsConversionErrorInterceptorTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    interceptor = new StrutsConversionErrorInterceptor();
    mockInvocation = new Mock(ActionInvocation.class);
    invocation = (ActionInvocation) mockInvocation.proxy();
    stack = ActionContext.getContext().getValueStack();
    conversionErrors = new HashMap<>();
    context = ActionContext.of(stack.getContext()).withConversionErrors(conversionErrors).bind();
    mockInvocation.matchAndReturn("getInvocationContext", context);
    mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
    mockInvocation.expectAndReturn("getStack", stack);
    mockInvocation.expect("addPreResultListener", C.ANY_ARGS);
}
Also used : ActionInvocation(com.opensymphony.xwork2.ActionInvocation) Mock(com.mockobjects.dynamic.Mock)

Example 78 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class ValidateAction method testDMIMethodsAreIgnored.

public void testDMIMethodsAreIgnored() throws Exception {
    // given
    ParametersInterceptor interceptor = createParametersInterceptor();
    final Map<String, Object> actual = injectValueStackFactory(interceptor);
    ValueStack stack = injectValueStack(actual);
    final Map<String, Object> expected = new HashMap<String, Object>() {

        {
            put("ordinary.bean", "value");
        }
    };
    Map<String, Object> parameters = new HashMap<String, Object>() {

        {
            put("ordinary.bean", "value");
            put("action:", "myAction");
            put("method:", "doExecute");
        }
    };
    // when
    interceptor.setParameters(new NoParametersAction(), stack, HttpParameters.create(parameters).build());
    // then
    assertEquals(expected, actual);
}
Also used : OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 79 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class ValidateAction method testInternalParametersAreIgnored.

public void testInternalParametersAreIgnored() throws Exception {
    // given
    ParametersInterceptor interceptor = createParametersInterceptor();
    final Map<String, Object> actual = injectValueStackFactory(interceptor);
    ValueStack stack = injectValueStack(actual);
    final Map<String, Object> expected = new HashMap<String, Object>() {

        {
            put("ordinary.bean", "value");
        }
    };
    Map<String, Object> parameters = new HashMap<String, Object>() {

        {
            put("ordinary.bean", "value");
            put("#some.internal.object", "true");
            put("(bla)#some.internal.object", "true");
            put("#some.internal.object(bla)#some.internal.object", "true");
            put("#_some.internal.object", "true");
            put("\u0023_some.internal.object", "true");
            put("\u0023_some.internal.object,[dfd],bla(\u0023_some.internal.object)", "true");
            put("\\u0023_some.internal.object", "true");
        }
    };
    // when
    interceptor.setParameters(new NoParametersAction(), stack, HttpParameters.create(parameters).build());
    // then
    assertEquals(expected, actual);
}
Also used : OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 80 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class PrepareInterceptorTest method testPrefixInvocation2.

public void testPrefixInvocation2() throws Exception {
    ActionProxy mockActionProxy = (ActionProxy) createMock(ActionProxy.class);
    expect(mockActionProxy.getMethod()).andStubReturn("save");
    ActionInvocation mockActionInvocation = (ActionInvocation) createMock(ActionInvocation.class);
    expect(mockActionInvocation.getAction()).andStubReturn(mockAction);
    expect(mockActionInvocation.invoke()).andStubReturn("okok");
    expect(mockActionInvocation.getProxy()).andStubReturn(mockActionProxy);
    mockAction.prepare();
    expectLastCall().times(1);
    replay(mockAction, mockActionProxy, mockActionInvocation);
    PrepareInterceptor interceptor = new PrepareInterceptor();
    String result = interceptor.intercept(mockActionInvocation);
    assertEquals("okok", result);
    verify(mockAction, mockActionProxy, mockActionInvocation);
}
Also used : MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation)

Aggregations

ActionInvocation (com.opensymphony.xwork2.ActionInvocation)32 HashMap (java.util.HashMap)28 ActionContext (com.opensymphony.xwork2.ActionContext)20 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)19 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)19 ServletActionContext (org.apache.struts2.ServletActionContext)14 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)13 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)12 LinkedHashMap (java.util.LinkedHashMap)12 Map (java.util.Map)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)10 InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)10 DefaultAcceptedPatternsChecker (com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker)10 DefaultExcludedPatternsChecker (com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)10 Cookie (javax.servlet.http.Cookie)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Mock (com.mockobjects.dynamic.Mock)8 ActionSupport (com.opensymphony.xwork2.ActionSupport)8