KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > util > DefaultLifecycleEventMgrTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.terracotta.session.util;
6
7 import com.terracotta.session.Session;
8
9 import javax.servlet.http.HttpSessionAttributeListener JavaDoc;
10 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
11 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
12 import javax.servlet.http.HttpSessionEvent JavaDoc;
13 import javax.servlet.http.HttpSessionListener JavaDoc;
14 import javax.servlet.http.MockSessionAttributeListener JavaDoc;
15 import javax.servlet.http.MockSessionBindingListener JavaDoc;
16 import javax.servlet.http.MockSessionListener JavaDoc;
17
18 import junit.framework.TestCase;
19
20 public class DefaultLifecycleEventMgrTest extends TestCase {
21
22   private DefaultLifecycleEventMgr eventMgr;
23   private MockSessionAttributeListener JavaDoc attributeListener;
24   private HttpSessionAttributeListener JavaDoc[] attributeListeners;
25   private MockSessionListener JavaDoc sessionListener;
26   private HttpSessionListener JavaDoc[] sessionListeners;
27   private Session sess;
28
29   protected void setUp() throws Exception JavaDoc {
30     sess = new MockSession();
31     attributeListener = new MockSessionAttributeListener JavaDoc();
32     attributeListeners = new HttpSessionAttributeListener JavaDoc[] { attributeListener };
33     sessionListener = new MockSessionListener JavaDoc();
34     sessionListeners = new HttpSessionListener JavaDoc[] { sessionListener };
35     eventMgr = new DefaultLifecycleEventMgr(attributeListeners, sessionListeners);
36   }
37
38   public void testFireSessionCreatedEvent() {
39     eventMgr.fireSessionCreatedEvent(sess);
40     assertEquals("sessionCreated", sessionListener.getLastMethod());
41     assertSame(sess, sessionListener.getLastEvent().getSession());
42   }
43
44   public void testInvalidate() {
45     eventMgr.fireSessionDestroyedEvent(sess);
46     assertEquals("sessionDestroyed", sessionListener.getLastMethod());
47     assertSame(sess, sessionListener.getLastEvent().getSession());
48   }
49
50   public void testBindingEvents() {
51     final String JavaDoc name = "SomeAttributeName";
52     final MockSessionBindingListener JavaDoc val = new MockSessionBindingListener JavaDoc();
53
54     eventMgr.bindAttribute(sess, name, val);
55     checkBindingEvent("valueBound", name, val, val);
56     val.clear();
57
58     eventMgr.unbindAttribute(sess, name, val);
59     checkBindingEvent("valueUnbound", name, val, val);
60     val.clear();
61     attributeListener.clear();
62   }
63
64   public void testAttributeEvents() {
65     final String JavaDoc name = "SomeAttrName";
66     final Object JavaDoc val = name;
67
68     eventMgr.setAttribute(sess, name, val);
69     checkAttributeEvent("attributeAdded", name, val);
70
71     eventMgr.removeAttribute(sess, name, val);
72     checkAttributeEvent("attributeRemoved", name, val);
73
74     eventMgr.replaceAttribute(sess, name, val, val);
75     checkAttributeEvent("attributeReplaced", name, val);
76   }
77
78   public void testExceptionDelivery() {
79     final String JavaDoc name = "someName";
80     HttpSessionBindingListener JavaDoc bl = new BindingListenerWithException();
81     HttpSessionAttributeListener JavaDoc al = new AttributeListenerWithException();
82     HttpSessionListener JavaDoc sl = new SessionListenerWithException();
83     LifecycleEventMgr mgr = new DefaultLifecycleEventMgr(new HttpSessionAttributeListener JavaDoc[] { al }, new HttpSessionListener JavaDoc[] {sl});
84     try {
85       mgr.bindAttribute(sess, name, bl);
86       fail("expected exception");
87     } catch (Throwable JavaDoc e) {
88       // ok
89
}
90     try {
91       mgr.fireSessionCreatedEvent(sess);
92       fail("expected exception");
93     } catch (Throwable JavaDoc e) {
94       // ok
95
}
96     try {
97       mgr.fireSessionDestroyedEvent(sess);
98       fail("expected exception");
99     } catch (Throwable JavaDoc e) {
100       // ok
101
}
102     try {
103       mgr.removeAttribute(sess, name, bl);
104       fail("expected exception");
105     } catch (Throwable JavaDoc e) {
106       // ok
107
}
108     try {
109       mgr.replaceAttribute(sess, name, bl, bl);
110       fail("expected exception");
111     } catch (Throwable JavaDoc e) {
112       // ok
113
}
114     try {
115       mgr.setAttribute(sess, name, bl);
116       fail("expected exception");
117     } catch (Throwable JavaDoc e) {
118       // ok
119
}
120     try {
121       mgr.unbindAttribute(sess, name, bl);
122       fail("expected exception");
123     } catch (Throwable JavaDoc e) {
124       // ok
125
}
126   }
127
128   private void checkAttributeEvent(final String JavaDoc eventName, final String JavaDoc attrName, final Object JavaDoc attrVal) {
129     assertEquals(eventName, attributeListener.getLastMethod());
130     HttpSessionBindingEvent JavaDoc e = attributeListener.getLastEvent();
131     assertNotNull(e);
132     assertEquals(attrName, e.getName());
133     assertSame(attrVal, e.getValue());
134   }
135
136   private void checkBindingEvent(final String JavaDoc bindEventName, final String JavaDoc attrName,
137                                  final MockSessionBindingListener JavaDoc listener, final Object JavaDoc val) {
138     assertEquals(bindEventName, listener.getLastEventName());
139     HttpSessionBindingEvent JavaDoc e = listener.getLastEvent();
140     assertNotNull(e);
141     assertSame(sess, e.getSession());
142     assertSame(attrName, e.getName());
143     assertSame(val, e.getValue());
144   }
145
146   static class BindingListenerWithException implements HttpSessionBindingListener JavaDoc {
147     public void valueBound(HttpSessionBindingEvent JavaDoc arg0) {
148       throw new RuntimeException JavaDoc("Catch Me!");
149     }
150
151     public void valueUnbound(HttpSessionBindingEvent JavaDoc arg0) {
152       throw new RuntimeException JavaDoc("Catch Me!");
153     }
154   }
155
156   static class AttributeListenerWithException implements HttpSessionAttributeListener JavaDoc {
157     public void attributeAdded(HttpSessionBindingEvent JavaDoc arg0) {
158       throw new RuntimeException JavaDoc("Catch Me!");
159     }
160
161     public void attributeRemoved(HttpSessionBindingEvent JavaDoc arg0) {
162       throw new RuntimeException JavaDoc("Catch Me!");
163     }
164
165     public void attributeReplaced(HttpSessionBindingEvent JavaDoc arg0) {
166       throw new RuntimeException JavaDoc("Catch Me!");
167     }
168   }
169
170   static class SessionListenerWithException implements HttpSessionListener JavaDoc {
171     public void sessionCreated(HttpSessionEvent JavaDoc arg0) {
172       throw new RuntimeException JavaDoc("Catch Me!");
173     }
174
175     public void sessionDestroyed(HttpSessionEvent JavaDoc arg0) {
176       throw new RuntimeException JavaDoc("Catch Me!");
177     }
178   }
179 }
180
Popular Tags