StartupListener implements ServletContextListener { @SuppressWarnings("unchecked") public void contextInitialized(ServletContextEvent event) { log.debug("Initializing context..."); ServletContext context = event.getServletContext(); Map<String, Object> config = (HashMap<String, Object>) context.getAttribute(Constants.CONFIG); if (config == null) { config = new HashMap<>(); } ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context); PasswordEncoder passwordEncoder = null; try { ProviderManager provider = (ProviderManager) ctx.getBean("org.springframework.security.authentication.ProviderManager#0"); for (Object o : provider.getProviders()) { AuthenticationProvider p = (AuthenticationProvider) o; if (p instanceof RememberMeAuthenticationProvider) { config.put("rememberMeEnabled", Boolean.TRUE); } else if (ctx.getBean("passwordEncoder") != null) { passwordEncoder = (PasswordEncoder) ctx.getBean("passwordEncoder"); } } } catch (NoSuchBeanDefinitionException n) { log.debug("authenticationManager bean not found, assuming test and ignoring..."); } context.setAttribute(Constants.CONFIG, config); if (log.isDebugEnabled()) { log.debug("Remember Me Enabled? " + config.get("rememberMeEnabled")); if (passwordEncoder != null) { log.debug("Password Encoder: " + passwordEncoder.getClass().getSimpleName()); } log.debug("Populating drop-downs..."); } setupContext(context); String appVersion = null; try { InputStream is = context.getResourceAsStream("/META-INF/MANIFEST.MF"); if (is == null) { log.warn("META-INF/MANIFEST.MF not found."); } else { Manifest mf = new Manifest(); mf.read(is); Attributes atts = mf.getMainAttributes(); appVersion = atts.getValue("Implementation-Version"); } } catch (IOException e) { log.error("I/O Exception reading manifest: " + e.getMessage()); } if (appVersion == null || appVersion.contains("SNAPSHOT")) { appVersion = "" + new Random().nextInt(100000); } log.info("Application version set to: " + appVersion); context.setAttribute(Constants.ASSETS_VERSION, appVersion); }  @SuppressWarnings("unchecked") void contextInitialized(ServletContextEvent event); static void setupContext(ServletContext context); void contextDestroyed(ServletContextEvent servletContextEvent);  }
@Test public void testContextInitialized() { listener.contextInitialized(new ServletContextEvent(sc)); assertTrue(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null); assertTrue(sc.getAttribute(Constants.AVAILABLE_ROLES) != null); assertNotNull(sc.getAttribute(Constants.ASSETS_VERSION)); }