SessionHelpers { @SuppressWarnings("unchecked") public void interceptHttpListener(EventListener caller, HttpSessionEvent event) { if (event.getSession() instanceof RepositoryBackedHttpSession) { return; } Object value = event.getSession().getServletContext().getAttribute(INTROSPECTING_LISTENERS); if (value != null && !((Set<?>)value).contains(caller)) { ((Set<Object>)value).add(caller); onAddListener(event.getSession().getServletContext(), caller); } }  ServletRequest prepareRequest(ServletRequest request, ServletResponse response,
      ServletContext originalServletContext); ServletResponse prepareResponse(ServletRequest request, ServletResponse response); MethodHandle[] initSessionManagement(ServletContext servletContext); MethodHandle[] initSessionManagement(ServletContext servletContext, boolean reset); void commitRequest(ServletRequest request, ServletRequest oldRequest); @SuppressWarnings("unchecked") void interceptHttpListener(EventListener caller, HttpSessionEvent event); void onAddListener(ServletContext servletContext, Object listener);  }
@Test public void testInterceptHttpListener() { EventListener caller = mock(EventListener.class); HttpSessionEvent event = mock(HttpSessionEvent.class); @SuppressWarnings("unchecked") Set<Object> listenerSet = mock(Set.class); HttpSession session = mock(HttpSession.class); when(session.getServletContext()).thenReturn(servletContext); when(servletContext.getAttribute(SessionHelpers.INTROSPECTING_LISTENERS)).thenReturn(listenerSet); when(event.getSession()).thenReturn(session); sessionHelpers.interceptHttpListener(caller, event); verify(listenerSet).add(caller); }
@Test public void testInterceptHttpListenerAlreadyWrapped() { EventListener caller = mock(EventListener.class); HttpSessionEvent event = mock(HttpSessionEvent.class); @SuppressWarnings("unchecked") Set<Object> listenerSet = mock(Set.class); HttpSession session = mock(RepositoryBackedHttpSession.class); when(session.getServletContext()).thenReturn(servletContext); when(servletContext.getAttribute(SessionHelpers.INTROSPECTING_LISTENERS)).thenReturn(listenerSet); when(event.getSession()).thenReturn(session); sessionHelpers.interceptHttpListener(caller, event); verify(listenerSet, never()).add(caller); }
@Test public void testInterceptHttpListenerAlreadyIntercepted() { EventListener caller = mock(EventListener.class); HttpSessionEvent event = mock(HttpSessionEvent.class); @SuppressWarnings("unchecked") Set<Object> listenerSet = mock(Set.class); listenerSet.add(caller); HttpSession session = mock(RepositoryBackedHttpSession.class); when(session.getServletContext()).thenReturn(servletContext); when(servletContext.getAttribute(SessionHelpers.INTROSPECTING_LISTENERS)).thenReturn(listenerSet); when(event.getSession()).thenReturn(session); sessionHelpers.interceptHttpListener(caller, event); verify(listenerSet, times(1)).add(caller); }