001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.interceptor;
006:
007: import java.util.LinkedHashMap;
008:
009: import org.easymock.MockControl;
010: import org.springframework.mock.web.MockHttpSession;
011:
012: import com.mockobjects.servlet.MockHttpServletRequest;
013: import com.opensymphony.webwork.dispatcher.SessionMap;
014: import com.opensymphony.xwork.ActionContext;
015: import com.opensymphony.xwork.ActionInvocation;
016:
017: import junit.framework.TestCase;
018:
019: /**
020: * @author tmjee
021: * @version $Date: 2006-12-04 15:41:44 +0100 (Mon, 04 Dec 2006) $ $Id: SessionInvalidationInterceptorTest.java 2751 2006-12-04 14:41:44Z tmjee $
022: */
023: public class SessionInvalidationInterceptorTest extends TestCase {
024:
025: public void testInvocationNow() throws Exception {
026: MockHttpSession session = new MockHttpSession();
027: final MockHttpServletRequest request = new MockHttpServletRequest();
028: request.setSession(session);
029:
030: ActionContext actionContext = new ActionContext(
031: new LinkedHashMap() {
032: private static final long serialVersionUID = 1L;
033: {
034: put(ActionContext.SESSION, new SessionMap(
035: request));
036: }
037: });
038:
039: MockControl control = MockControl
040: .createControl(ActionInvocation.class);
041: ActionInvocation invocation = (ActionInvocation) control
042: .getMock();
043: control.expectAndDefaultReturn(invocation
044: .getInvocationContext(), actionContext);
045: control.expectAndDefaultReturn(invocation.invoke(), "success");
046: control.replay();
047:
048: assertFalse(session.isInvalid());
049: assertNull(session.getAttribute("___invalidateSession"));
050:
051: SessionInvalidationInterceptor interceptor = new SessionInvalidationInterceptor();
052: interceptor.setType(SessionInvalidationInterceptor.NOW);
053: interceptor.intercept(invocation);
054:
055: assertNull(session.getAttribute("___invalidateSession"));
056: assertTrue(session.isInvalid());
057: control.verify();
058: }
059:
060: public void testInvocationNextRequest() throws Exception {
061: MockHttpSession session = new MockHttpSession();
062: final MockHttpServletRequest request = new MockHttpServletRequest();
063: request.setSession(session);
064:
065: ActionContext actionContext = new ActionContext(
066: new LinkedHashMap() {
067: private static final long serialVersionUID = 1L;
068: {
069: put(ActionContext.SESSION, new SessionMap(
070: request));
071: }
072: });
073:
074: MockControl control = MockControl
075: .createControl(ActionInvocation.class);
076: ActionInvocation invocation = (ActionInvocation) control
077: .getMock();
078: control.expectAndDefaultReturn(invocation
079: .getInvocationContext(), actionContext);
080: control.expectAndDefaultReturn(invocation.invoke(), "success");
081: control.replay();
082:
083: assertFalse(session.isInvalid());
084: assertNull(session.getAttribute("___invalidateSession"));
085:
086: SessionInvalidationInterceptor interceptor = new SessionInvalidationInterceptor();
087: interceptor
088: .setType(SessionInvalidationInterceptor.NEXT_REQUEST);
089: interceptor.intercept(invocation);
090:
091: assertNotNull(session.getAttribute("___invalidateSession"));
092: assertEquals(session.getAttribute("___invalidateSession"),
093: "true");
094: assertFalse(session.isInvalid());
095:
096: interceptor.intercept(invocation);
097:
098: assertTrue(session.isInvalid());
099: control.verify();
100: }
101:
102: }
|