1 5 package com.terracotta.session.util; 6 7 import com.terracotta.session.Session; 8 9 import javax.servlet.http.HttpSessionAttributeListener ; 10 import javax.servlet.http.HttpSessionBindingEvent ; 11 import javax.servlet.http.HttpSessionBindingListener ; 12 import javax.servlet.http.HttpSessionEvent ; 13 import javax.servlet.http.HttpSessionListener ; 14 import javax.servlet.http.MockSessionAttributeListener ; 15 import javax.servlet.http.MockSessionBindingListener ; 16 import javax.servlet.http.MockSessionListener ; 17 18 import junit.framework.TestCase; 19 20 public class DefaultLifecycleEventMgrTest extends TestCase { 21 22 private DefaultLifecycleEventMgr eventMgr; 23 private MockSessionAttributeListener attributeListener; 24 private HttpSessionAttributeListener [] attributeListeners; 25 private MockSessionListener sessionListener; 26 private HttpSessionListener [] sessionListeners; 27 private Session sess; 28 29 protected void setUp() throws Exception { 30 sess = new MockSession(); 31 attributeListener = new MockSessionAttributeListener (); 32 attributeListeners = new HttpSessionAttributeListener [] { attributeListener }; 33 sessionListener = new MockSessionListener (); 34 sessionListeners = new HttpSessionListener [] { 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 name = "SomeAttributeName"; 52 final MockSessionBindingListener val = new MockSessionBindingListener (); 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 name = "SomeAttrName"; 66 final Object 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 name = "someName"; 80 HttpSessionBindingListener bl = new BindingListenerWithException(); 81 HttpSessionAttributeListener al = new AttributeListenerWithException(); 82 HttpSessionListener sl = new SessionListenerWithException(); 83 LifecycleEventMgr mgr = new DefaultLifecycleEventMgr(new HttpSessionAttributeListener [] { al }, new HttpSessionListener [] {sl}); 84 try { 85 mgr.bindAttribute(sess, name, bl); 86 fail("expected exception"); 87 } catch (Throwable e) { 88 } 90 try { 91 mgr.fireSessionCreatedEvent(sess); 92 fail("expected exception"); 93 } catch (Throwable e) { 94 } 96 try { 97 mgr.fireSessionDestroyedEvent(sess); 98 fail("expected exception"); 99 } catch (Throwable e) { 100 } 102 try { 103 mgr.removeAttribute(sess, name, bl); 104 fail("expected exception"); 105 } catch (Throwable e) { 106 } 108 try { 109 mgr.replaceAttribute(sess, name, bl, bl); 110 fail("expected exception"); 111 } catch (Throwable e) { 112 } 114 try { 115 mgr.setAttribute(sess, name, bl); 116 fail("expected exception"); 117 } catch (Throwable e) { 118 } 120 try { 121 mgr.unbindAttribute(sess, name, bl); 122 fail("expected exception"); 123 } catch (Throwable e) { 124 } 126 } 127 128 private void checkAttributeEvent(final String eventName, final String attrName, final Object attrVal) { 129 assertEquals(eventName, attributeListener.getLastMethod()); 130 HttpSessionBindingEvent e = attributeListener.getLastEvent(); 131 assertNotNull(e); 132 assertEquals(attrName, e.getName()); 133 assertSame(attrVal, e.getValue()); 134 } 135 136 private void checkBindingEvent(final String bindEventName, final String attrName, 137 final MockSessionBindingListener listener, final Object val) { 138 assertEquals(bindEventName, listener.getLastEventName()); 139 HttpSessionBindingEvent 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 { 147 public void valueBound(HttpSessionBindingEvent arg0) { 148 throw new RuntimeException ("Catch Me!"); 149 } 150 151 public void valueUnbound(HttpSessionBindingEvent arg0) { 152 throw new RuntimeException ("Catch Me!"); 153 } 154 } 155 156 static class AttributeListenerWithException implements HttpSessionAttributeListener { 157 public void attributeAdded(HttpSessionBindingEvent arg0) { 158 throw new RuntimeException ("Catch Me!"); 159 } 160 161 public void attributeRemoved(HttpSessionBindingEvent arg0) { 162 throw new RuntimeException ("Catch Me!"); 163 } 164 165 public void attributeReplaced(HttpSessionBindingEvent arg0) { 166 throw new RuntimeException ("Catch Me!"); 167 } 168 } 169 170 static class SessionListenerWithException implements HttpSessionListener { 171 public void sessionCreated(HttpSessionEvent arg0) { 172 throw new RuntimeException ("Catch Me!"); 173 } 174 175 public void sessionDestroyed(HttpSessionEvent arg0) { 176 throw new RuntimeException ("Catch Me!"); 177 } 178 } 179 } 180 | Popular Tags |