001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.terracotta.session.util;
006:
007: import com.terracotta.session.Session;
008:
009: import javax.servlet.http.HttpSessionAttributeListener;
010: import javax.servlet.http.HttpSessionBindingEvent;
011: import javax.servlet.http.HttpSessionBindingListener;
012: import javax.servlet.http.HttpSessionEvent;
013: import javax.servlet.http.HttpSessionListener;
014: import javax.servlet.http.MockSessionAttributeListener;
015: import javax.servlet.http.MockSessionBindingListener;
016: import javax.servlet.http.MockSessionListener;
017:
018: import junit.framework.TestCase;
019:
020: public class DefaultLifecycleEventMgrTest extends TestCase {
021:
022: private DefaultLifecycleEventMgr eventMgr;
023: private MockSessionAttributeListener attributeListener;
024: private HttpSessionAttributeListener[] attributeListeners;
025: private MockSessionListener sessionListener;
026: private HttpSessionListener[] sessionListeners;
027: private Session sess;
028:
029: protected void setUp() throws Exception {
030: sess = new MockSession();
031: attributeListener = new MockSessionAttributeListener();
032: attributeListeners = new HttpSessionAttributeListener[] { attributeListener };
033: sessionListener = new MockSessionListener();
034: sessionListeners = new HttpSessionListener[] { sessionListener };
035: eventMgr = new DefaultLifecycleEventMgr(attributeListeners,
036: sessionListeners);
037: }
038:
039: public void testFireSessionCreatedEvent() {
040: eventMgr.fireSessionCreatedEvent(sess);
041: assertEquals("sessionCreated", sessionListener.getLastMethod());
042: assertSame(sess, sessionListener.getLastEvent().getSession());
043: }
044:
045: public void testInvalidate() {
046: eventMgr.fireSessionDestroyedEvent(sess);
047: assertEquals("sessionDestroyed", sessionListener
048: .getLastMethod());
049: assertSame(sess, sessionListener.getLastEvent().getSession());
050: }
051:
052: public void testBindingEvents() {
053: final String name = "SomeAttributeName";
054: final MockSessionBindingListener val = new MockSessionBindingListener();
055:
056: eventMgr.bindAttribute(sess, name, val);
057: checkBindingEvent("valueBound", name, val, val);
058: val.clear();
059:
060: eventMgr.unbindAttribute(sess, name, val);
061: checkBindingEvent("valueUnbound", name, val, val);
062: val.clear();
063: attributeListener.clear();
064: }
065:
066: public void testAttributeEvents() {
067: final String name = "SomeAttrName";
068: final Object val = name;
069:
070: eventMgr.setAttribute(sess, name, val);
071: checkAttributeEvent("attributeAdded", name, val);
072:
073: eventMgr.removeAttribute(sess, name, val);
074: checkAttributeEvent("attributeRemoved", name, val);
075:
076: eventMgr.replaceAttribute(sess, name, val, val);
077: checkAttributeEvent("attributeReplaced", name, val);
078: }
079:
080: public void testExceptionDelivery() {
081: final String name = "someName";
082: HttpSessionBindingListener bl = new BindingListenerWithException();
083: HttpSessionAttributeListener al = new AttributeListenerWithException();
084: HttpSessionListener sl = new SessionListenerWithException();
085: LifecycleEventMgr mgr = new DefaultLifecycleEventMgr(
086: new HttpSessionAttributeListener[] { al },
087: new HttpSessionListener[] { sl });
088: try {
089: mgr.bindAttribute(sess, name, bl);
090: fail("expected exception");
091: } catch (Throwable e) {
092: // ok
093: }
094: try {
095: mgr.fireSessionCreatedEvent(sess);
096: fail("expected exception");
097: } catch (Throwable e) {
098: // ok
099: }
100: try {
101: mgr.fireSessionDestroyedEvent(sess);
102: fail("expected exception");
103: } catch (Throwable e) {
104: // ok
105: }
106: try {
107: mgr.removeAttribute(sess, name, bl);
108: fail("expected exception");
109: } catch (Throwable e) {
110: // ok
111: }
112: try {
113: mgr.replaceAttribute(sess, name, bl, bl);
114: fail("expected exception");
115: } catch (Throwable e) {
116: // ok
117: }
118: try {
119: mgr.setAttribute(sess, name, bl);
120: fail("expected exception");
121: } catch (Throwable e) {
122: // ok
123: }
124: try {
125: mgr.unbindAttribute(sess, name, bl);
126: fail("expected exception");
127: } catch (Throwable e) {
128: // ok
129: }
130: }
131:
132: private void checkAttributeEvent(final String eventName,
133: final String attrName, final Object attrVal) {
134: assertEquals(eventName, attributeListener.getLastMethod());
135: HttpSessionBindingEvent e = attributeListener.getLastEvent();
136: assertNotNull(e);
137: assertEquals(attrName, e.getName());
138: assertSame(attrVal, e.getValue());
139: }
140:
141: private void checkBindingEvent(final String bindEventName,
142: final String attrName,
143: final MockSessionBindingListener listener, final Object val) {
144: assertEquals(bindEventName, listener.getLastEventName());
145: HttpSessionBindingEvent e = listener.getLastEvent();
146: assertNotNull(e);
147: assertSame(sess, e.getSession());
148: assertSame(attrName, e.getName());
149: assertSame(val, e.getValue());
150: }
151:
152: static class BindingListenerWithException implements
153: HttpSessionBindingListener {
154: public void valueBound(HttpSessionBindingEvent arg0) {
155: throw new RuntimeException("Catch Me!");
156: }
157:
158: public void valueUnbound(HttpSessionBindingEvent arg0) {
159: throw new RuntimeException("Catch Me!");
160: }
161: }
162:
163: static class AttributeListenerWithException implements
164: HttpSessionAttributeListener {
165: public void attributeAdded(HttpSessionBindingEvent arg0) {
166: throw new RuntimeException("Catch Me!");
167: }
168:
169: public void attributeRemoved(HttpSessionBindingEvent arg0) {
170: throw new RuntimeException("Catch Me!");
171: }
172:
173: public void attributeReplaced(HttpSessionBindingEvent arg0) {
174: throw new RuntimeException("Catch Me!");
175: }
176: }
177:
178: static class SessionListenerWithException implements
179: HttpSessionListener {
180: public void sessionCreated(HttpSessionEvent arg0) {
181: throw new RuntimeException("Catch Me!");
182: }
183:
184: public void sessionDestroyed(HttpSessionEvent arg0) {
185: throw new RuntimeException("Catch Me!");
186: }
187: }
188: }
|