SessionHelpers { public void onAddListener(ServletContext servletContext, Object listener) { String contextPath = servletContext.getContextPath(); ServletContextDescriptor scd = getDescriptor(servletContext); logger.debug("Registering listener {} for context {}", listener, contextPath); if (listener instanceof HttpSessionListener) { scd.addHttpSessionListener((HttpSessionListener)listener); } if (listener instanceof HttpSessionAttributeListener) { scd.addHttpSessionAttributeListener((HttpSessionAttributeListener)listener); } if (ServletLevel.isServlet31) { if (listener instanceof HttpSessionIdListener) { scd.addHttpSessionIdListener((HttpSessionIdListener)listener); } } }  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 testOnAddListener() { ServletContextDescriptor scd = new ServletContextDescriptor(servletContext); when(servletContext.getAttribute(Attributes.SERVLET_CONTEXT_DESCRIPTOR)).thenReturn(scd); sessionHelpers.onAddListener(servletContext, "Dummy"); assertTrue(scd.getHttpSessionListeners().isEmpty()); assertTrue(scd.getHttpSessionIdListeners().isEmpty()); assertTrue(scd.getHttpSessionAttributeListeners().isEmpty()); HttpSessionListener listener = mock(HttpSessionListener.class); HttpSessionIdListener idListener = mock(HttpSessionIdListener.class); HttpSessionAttributeListener attributeListener = mock(HttpSessionAttributeListener.class); HttpSessionListener multiListener = mock(HttpSessionListener.class, withSettings().extraInterfaces(HttpSessionAttributeListener.class)); HttpSessionAttributeListener attributeMultiListener = (HttpSessionAttributeListener)multiListener; sessionHelpers.onAddListener(servletContext, listener); assertThat(scd.getHttpSessionListeners(), hasItem(listener)); assertTrue(scd.getHttpSessionIdListeners().isEmpty()); assertTrue(scd.getHttpSessionAttributeListeners().isEmpty()); sessionHelpers.onAddListener(servletContext, idListener); assertThat(scd.getHttpSessionListeners(), hasItem(listener)); assertThat(scd.getHttpSessionIdListeners(), hasItem(idListener)); assertTrue(scd.getHttpSessionAttributeListeners().isEmpty()); sessionHelpers.onAddListener(servletContext, attributeListener); assertThat(scd.getHttpSessionListeners(), hasItem(listener)); assertThat(scd.getHttpSessionIdListeners(), hasItem(idListener)); assertThat(scd.getHttpSessionAttributeListeners(), hasItem(attributeListener)); sessionHelpers.onAddListener(servletContext, multiListener); assertThat(scd.getHttpSessionListeners(), hasItem(listener)); assertThat(scd.getHttpSessionListeners(), hasItem(multiListener)); assertThat(scd.getHttpSessionIdListeners(), hasItem(idListener)); assertThat(scd.getHttpSessionAttributeListeners(), hasItem(attributeListener)); assertThat(scd.getHttpSessionAttributeListeners(), hasItem(attributeMultiListener)); }