HttpSessionNotifier implements SessionNotifier { @Override public void sessionDestroyed(RepositoryBackedSession session, boolean shutdown) { if (session instanceof HttpSession) { for (String key : session.getAttributeNamesWithValues()) { HttpSessionBindingEvent event = new HttpSessionBindingEvent((HttpSession)session, key); Object value = session.getAttribute(key); if (value instanceof HttpSessionBindingListener) { ((HttpSessionBindingListener)value).valueUnbound(event); } for (HttpSessionAttributeListener listener : descriptor.getHttpSessionAttributeListeners()) { listener.attributeRemoved(event); } } HttpSessionEvent event = new HttpSessionEvent((HttpSession)session); if (shutdown) { List<HttpSessionListener> listeners = descriptor.getHttpSessionListeners(); for (int i = listeners.size() - 1; i >= 0; i--) { listeners.get(i).sessionDestroyed(event); } } else { for (HttpSessionListener listener : descriptor.getHttpSessionListeners()) { listener.sessionDestroyed(event); } } } } HttpSessionNotifier(ServletContextDescriptor descriptor); @Override void sessionCreated(RepositoryBackedSession session); @Override void attributeAdded(RepositoryBackedSession session, String key, Object value); @Override void attributeReplaced(RepositoryBackedSession session, String key, Object replacedValue); @Override void attributeRemoved(RepositoryBackedSession session, String key, Object removedValue); @Override void attributeBeingStored(RepositoryBackedSession session, String key, Object value); @Override void attributeHasBeenRestored(RepositoryBackedSession session, String key, Object value); @Override void sessionDestroyed(RepositoryBackedSession session, boolean shutdown); @Override void sessionIdChanged(RepositoryBackedSession session, String oldId);  }
@Test public void testSessionDestroyed() { HttpSessionBindingListener bindingListener = mock(HttpSessionBindingListener.class); String dummy = "dummy"; when(session.getAttribute("binding")).thenReturn(bindingListener); when(session.getAttribute("attribute")).thenReturn(dummy); when(session.getAttributeNamesWithValues()).thenReturn(Arrays.asList("binding", "attribute")); HttpSessionListener listener = mock(HttpSessionListener.class); descriptor.addHttpSessionListener(listener); notifier.sessionDestroyed(session, false); verify(bindingListener).valueUnbound(any(HttpSessionBindingEvent.class)); verify(listener).sessionDestroyed(any(HttpSessionEvent.class)); HttpSessionListener listener2 = mock(HttpSessionListener.class); HttpSessionBindingListener bindingListener2 = mock(HttpSessionBindingListener.class); when(session.getAttribute("binding2")).thenReturn(bindingListener2); when(session.getAttributeNamesWithValues()).thenReturn(Arrays.asList("binding", "attribute", "binding2")); descriptor.addHttpSessionListener(listener2); notifier.sessionDestroyed(session, false); verify(listener, times(2)).sessionDestroyed(any(HttpSessionEvent.class)); verify(bindingListener, times(2)).valueUnbound(any(HttpSessionBindingEvent.class)); verify(bindingListener2).valueUnbound(any(HttpSessionBindingEvent.class)); verify(listener2).sessionDestroyed(any(HttpSessionEvent.class)); }
@Test public void testShutdown() { HttpSessionListener listener = mock(HttpSessionListener.class); descriptor.addHttpSessionListener(listener); notifier.sessionDestroyed(session, true); verify(listener).sessionDestroyed(any(HttpSessionEvent.class)); HttpSessionListener listener2 = mock(HttpSessionListener.class); descriptor.addHttpSessionListener(listener2); notifier.sessionDestroyed(session, true); verify(listener, times(2)).sessionDestroyed(any(HttpSessionEvent.class)); verify(listener2).sessionDestroyed(any(HttpSessionEvent.class)); }