AwtAppContextClassloaderListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { try { final ClassLoader active = Thread.currentThread().getContextClassLoader(); try { ClassLoader root = active; while (root.getParent() != null) { root = root.getParent(); } Thread.currentThread().setContextClassLoader(root); ImageIO.getCacheDirectory(); } finally { Thread.currentThread().setContextClassLoader(active); } logger.info("AwtAppContextClassloaderListener Initialized"); } catch (Exception e) { logger.error("Failed to address PermGen leak.", e); } }  @Override void contextInitialized(ServletContextEvent sce); @Override void contextDestroyed(ServletContextEvent sce);  }
@Test public void contextInitializedTest() { listener.contextInitialized(event); new Verifications() { { ImageIO.getCacheDirectory(); times = 1; } }; }
@Test public void contextInitializedErrorTest() { new Expectations() { { ImageIO.getCacheDirectory(); result = new Exception(); } }; listener.contextInitialized(event); }