Search in sources :

Example 66 with Interceptor

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

the class MessageStoreInterceptorTest method testIgnoreMessageWithoutSession.

public void testIgnoreMessageWithoutSession() throws Exception {
    MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
    interceptor.setAllowRequestParameterSwitch(true);
    interceptor.setOperationMode(MessageStoreInterceptor.STORE_MODE);
    ActionSupport action = new ActionSupport();
    action.addActionError("some action error 1");
    action.addActionMessage("some action message 1");
    action.addFieldError("field2", "some field error 2");
    ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
    actionContext.setParameters(HttpParameters.create().build());
    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);
    // Mock (ActionInvocation)
    ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
    mockActionInvocation.getInvocationContext();
    EasyMock.expectLastCall().andReturn(actionContext);
    EasyMock.expectLastCall().anyTimes();
    mockActionInvocation.invoke();
    EasyMock.expectLastCall().andReturn(Action.SUCCESS);
    mockActionInvocation.addPreResultListener(EasyMock.anyObject());
    EasyMock.expectLastCall();
    EasyMock.replay(mockActionInvocation);
    interceptor.init();
    interceptor.intercept(mockActionInvocation);
    interceptor.destroy();
    EasyMock.verify(mockActionInvocation);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpSession(javax.servlet.http.HttpSession) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionSupport(com.opensymphony.xwork2.ActionSupport) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 67 with Interceptor

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

the class CheckboxInterceptorTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    param = new HashMap<>();
    interceptor = new CheckboxInterceptor();
    ai = new MockActionInvocation();
    ai.setInvocationContext(ActionContext.getContext());
}
Also used : MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation)

Example 68 with Interceptor

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

the class CookieInterceptorTest method testIntercepDefault.

public void testIntercepDefault() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(new Cookie("cookie1", "cookie1value"), new Cookie("cookie2", "cookie2value"), new Cookie("cookie3", "cookie3value"));
    ServletActionContext.setRequest(request);
    MockActionWithCookieAware action = new MockActionWithCookieAware();
    ActionContext.getContext().getValueStack().push(action);
    ActionInvocation invocation = (ActionInvocation) createMock(ActionInvocation.class);
    expect(invocation.getAction()).andReturn(action);
    expect(invocation.invoke()).andReturn(Action.SUCCESS);
    replay(invocation);
    // by default the interceptor doesn't accept any cookies
    CookieInterceptor interceptor = new CookieInterceptor();
    interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker());
    interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
    interceptor.intercept(invocation);
    assertTrue(action.getCookiesMap().isEmpty());
    assertNull(action.getCookie1(), null);
    assertNull(action.getCookie2(), null);
    assertNull(action.getCookie3(), null);
    assertNull(ActionContext.getContext().getValueStack().findValue("cookie1"));
    assertNull(ActionContext.getContext().getValueStack().findValue("cookie2"));
    assertNull(ActionContext.getContext().getValueStack().findValue("cookie3"));
    verify(invocation);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) DefaultAcceptedPatternsChecker(com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker) DefaultExcludedPatternsChecker(com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)

Example 69 with Interceptor

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

the class CookieInterceptorTest method testInterceptSelectedCookiesNameOnly1.

public void testInterceptSelectedCookiesNameOnly1() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(new Cookie("cookie1", "cookie1value"), new Cookie("cookie2", "cookie2value"), new Cookie("cookie3", "cookie3value"));
    ServletActionContext.setRequest(request);
    MockActionWithCookieAware action = new MockActionWithCookieAware();
    ActionContext.getContext().getValueStack().push(action);
    ActionInvocation invocation = (ActionInvocation) createMock(ActionInvocation.class);
    expect(invocation.getAction()).andReturn(action);
    expect(invocation.invoke()).andReturn(Action.SUCCESS);
    replay(invocation);
    CookieInterceptor interceptor = new CookieInterceptor();
    interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker());
    interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
    interceptor.setCookiesName("cookie1, cookie3");
    interceptor.setCookiesValue("cookie1value, cookie2value, cookie3value");
    interceptor.intercept(invocation);
    assertFalse(action.getCookiesMap().isEmpty());
    assertEquals(action.getCookiesMap().size(), 2);
    assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
    assertEquals(action.getCookiesMap().get("cookie2"), null);
    assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
    assertEquals(action.getCookie1(), "cookie1value");
    assertEquals(action.getCookie2(), null);
    assertEquals(action.getCookie3(), "cookie3value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), null);
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
    verify(invocation);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) DefaultAcceptedPatternsChecker(com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker) DefaultExcludedPatternsChecker(com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)

Example 70 with Interceptor

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

the class CookieInterceptorTest method testInterceptSelectedCookiesNameAndValue.

public void testInterceptSelectedCookiesNameAndValue() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(new Cookie("cookie1", "cookie1value"), new Cookie("cookie2", "cookie2value"), new Cookie("cookie3", "cookie3value"));
    ServletActionContext.setRequest(request);
    MockActionWithCookieAware action = new MockActionWithCookieAware();
    ActionContext.getContext().getValueStack().push(action);
    ActionInvocation invocation = (ActionInvocation) createMock(ActionInvocation.class);
    expect(invocation.getAction()).andReturn(action);
    expect(invocation.invoke()).andReturn(Action.SUCCESS);
    replay(invocation);
    CookieInterceptor interceptor = new CookieInterceptor();
    interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker());
    interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
    interceptor.setCookiesName("cookie1, cookie3");
    interceptor.setCookiesValue("cookie1value");
    interceptor.intercept(invocation);
    assertFalse(action.getCookiesMap().isEmpty());
    assertEquals(action.getCookiesMap().size(), 1);
    assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
    assertEquals(action.getCookiesMap().get("cookie2"), null);
    assertEquals(action.getCookiesMap().get("cookie3"), null);
    assertEquals(action.getCookie1(), "cookie1value");
    assertEquals(action.getCookie2(), null);
    assertEquals(action.getCookie3(), null);
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), null);
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), null);
    verify(invocation);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) DefaultAcceptedPatternsChecker(com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker) DefaultExcludedPatternsChecker(com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)

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