EverrestInitializedListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent sce) { makeFileCollectorDestroyer().stopFileCollector(); ServletContext servletContext = sce.getServletContext(); EverrestProcessor processor = (EverrestProcessor)servletContext.getAttribute(EverrestProcessor.class.getName()); if (processor != null) { processor.stop(); } servletContext.removeAttribute(EverrestProcessor.class.getName()); servletContext.removeAttribute(EverrestConfiguration.class.getName()); servletContext.removeAttribute(Application.class.getName()); servletContext.removeAttribute(DependencySupplier.class.getName()); servletContext.removeAttribute(ResourceBinder.class.getName()); servletContext.removeAttribute(ApplicationProviderBinder.class.getName()); }  @Override void contextDestroyed(ServletContextEvent sce); @Override void contextInitialized(ServletContextEvent sce);  }
@Test public void removesEverrestComponentsFromServletContextAttributeWhenServletContextDestroyed() { EverrestInitializedListener everrestInitializedListener = new EverrestInitializedListener(); ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext); everrestInitializedListener.contextDestroyed(servletContextEvent); verify(servletContext).removeAttribute(eq(EverrestProcessor.class.getName())); verify(servletContext).removeAttribute(eq(EverrestConfiguration.class.getName())); verify(servletContext).removeAttribute(eq(Application.class.getName())); verify(servletContext).removeAttribute(eq(ApplicationProviderBinder.class.getName())); verify(servletContext).removeAttribute(eq(ResourceBinder.class.getName())); verify(servletContext).removeAttribute(eq(DependencySupplier.class.getName())); }
@Test public void stopsEverrestProcessorWhenServletContextDestroyed() throws Exception { EverrestProcessor everrestProcessor = mock(EverrestProcessor.class); when(servletContext.getAttribute(EverrestProcessor.class.getName())).thenReturn(everrestProcessor); EverrestInitializedListener everrestInitializedListener = new EverrestInitializedListener(); ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext); everrestInitializedListener.contextDestroyed(servletContextEvent); verify(everrestProcessor).stop(); }