FileDocCategorySizeDatePackage
CookieInterceptorTest.javaAPI DocExample16658Mon Jul 23 13:26:18 BST 2007org.apache.struts2.interceptor

CookieInterceptorTest

public class CookieInterceptorTest extends org.apache.struts2.StrutsTestCase

Fields Summary
Constructors Summary
Methods Summary
public voidtestIntercepDefault()

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setCookies(new Cookie[] {
                new Cookie("cookie1", "cookie1value"),
                new Cookie("cookie2", "cookie2value"),
                new Cookie("cookie3", "cookie3value")
            });
        ServletActionContext.setRequest(request);

        MockActionWithCookieAware action = new MockActionWithCookieAware();

        ActionContext.getContext().getValueStack().push(action);

        MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
        ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.getAction(), action);
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.invoke(), Action.SUCCESS);

        actionInvocationControl.replay();

        // by default the interceptor doesn't accept any cookies
        CookieInterceptor interceptor = new CookieInterceptor();
        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"));
    
public voidtestInterceptAll1()

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setCookies(new Cookie[] {
                new Cookie("cookie1", "cookie1value"),
                new Cookie("cookie2", "cookie2value"),
                new Cookie("cookie3", "cookie3value")
            });
        ServletActionContext.setRequest(request);

        MockActionWithCookieAware action = new MockActionWithCookieAware();

        ActionContext.getContext().getValueStack().push(action);

        MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
        ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.getAction(), action);
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.invoke(), Action.SUCCESS);

        actionInvocationControl.replay();

        CookieInterceptor interceptor = new CookieInterceptor();
        interceptor.setCookiesName("*");
        interceptor.setCookiesValue("*");
        interceptor.intercept(invocation);

        assertFalse(action.getCookiesMap().isEmpty());
        assertEquals(action.getCookiesMap().size(), 3);
        assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
        assertEquals(action.getCookiesMap().get("cookie2"), "cookie2value");
        assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
        assertEquals(action.getCookie1(), "cookie1value");
        assertEquals(action.getCookie2(), "cookie2value");
        assertEquals(action.getCookie3(), "cookie3value");
        assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
        assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), "cookie2value");
        assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
    
public voidtestInterceptAll2()

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setCookies(new Cookie[] {
                new Cookie("cookie1", "cookie1value"),
                new Cookie("cookie2", "cookie2value"),
                new Cookie("cookie3", "cookie3value")
            });
        ServletActionContext.setRequest(request);

        MockActionWithCookieAware action = new MockActionWithCookieAware();

        ActionContext.getContext().getValueStack().push(action);

        MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
        ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.getAction(), action);
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.invoke(), Action.SUCCESS);

        actionInvocationControl.replay();

        CookieInterceptor interceptor = new CookieInterceptor();
        interceptor.setCookiesName("cookie1, cookie2, cookie3");
        interceptor.setCookiesValue("cookie1value, cookie2value, cookie3value");
        interceptor.intercept(invocation);

        assertFalse(action.getCookiesMap().isEmpty());
        assertEquals(action.getCookiesMap().size(), 3);
        assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
        assertEquals(action.getCookiesMap().get("cookie2"), "cookie2value");
        assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
        assertEquals(action.getCookie1(), "cookie1value");
        assertEquals(action.getCookie2(), "cookie2value");
        assertEquals(action.getCookie3(), "cookie3value");
        assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
        assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), "cookie2value");
        assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
    
public voidtestInterceptSelectedCookiesNameAndValue()

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setCookies(new Cookie[] {
                new Cookie("cookie1", "cookie1value"),
                new Cookie("cookie2", "cookie2value"),
                new Cookie("cookie3", "cookie3value")
            });
        ServletActionContext.setRequest(request);

        MockActionWithCookieAware action = new MockActionWithCookieAware();

        ActionContext.getContext().getValueStack().push(action);

        MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
        ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.getAction(), action);
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.invoke(), Action.SUCCESS);

        actionInvocationControl.replay();

        CookieInterceptor interceptor = new CookieInterceptor();
        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);
    
public voidtestInterceptSelectedCookiesNameOnly1()

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setCookies(new Cookie[] {
                new Cookie("cookie1", "cookie1value"),
                new Cookie("cookie2", "cookie2value"),
                new Cookie("cookie3", "cookie3value")
            });
        ServletActionContext.setRequest(request);

        MockActionWithCookieAware action = new MockActionWithCookieAware();

        ActionContext.getContext().getValueStack().push(action);

        MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
        ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.getAction(), action);
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.invoke(), Action.SUCCESS);

        actionInvocationControl.replay();

        CookieInterceptor interceptor = new CookieInterceptor();
        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");
    
public voidtestInterceptSelectedCookiesNameOnly2()

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setCookies(new Cookie[] {
                new Cookie("cookie1", "cookie1value"),
                new Cookie("cookie2", "cookie2value"),
                new Cookie("cookie3", "cookie3value")
            });
        ServletActionContext.setRequest(request);

        MockActionWithCookieAware action = new MockActionWithCookieAware();

        ActionContext.getContext().getValueStack().push(action);

        MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
        ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.getAction(), action);
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.invoke(), Action.SUCCESS);

        actionInvocationControl.replay();

        CookieInterceptor interceptor = new CookieInterceptor();
        interceptor.setCookiesName("cookie1, cookie3");
        interceptor.setCookiesValue("*");
        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");
    
public voidtestInterceptSelectedCookiesNameOnly3()

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setCookies(new Cookie[] {
                new Cookie("cookie1", "cookie1value"),
                new Cookie("cookie2", "cookie2value"),
                new Cookie("cookie3", "cookie3value")
            });
        ServletActionContext.setRequest(request);

        MockActionWithCookieAware action = new MockActionWithCookieAware();

        ActionContext.getContext().getValueStack().push(action);

        MockControl actionInvocationControl = MockControl.createControl(ActionInvocation.class);
        ActionInvocation invocation = (ActionInvocation) actionInvocationControl.getMock();
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.getAction(), action);
        actionInvocationControl.expectAndDefaultReturn(
                                                       invocation.invoke(), Action.SUCCESS);

        actionInvocationControl.replay();

        CookieInterceptor interceptor = new CookieInterceptor();
        interceptor.setCookiesName("cookie1, cookie3");
        interceptor.setCookiesValue("");
        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");