ResourceInitializer implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { try { logger.info("ResourceInitializer::contextInitialized started"); ServletContext sc = sce.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sc); initService(wac); initDataCollection(wac); IResourceInitializerService resInitializerSvc = wac.getBean(IResourceInitializerService.class); if (!resInitializerSvc.isResourceInitialized()) { boolean initResource = Configuration.getBoolean("init_resource"); logger.info("need initializing resource? " + initResource); if (initResource) { resInitializerSvc.initResource(); } resInitializerSvc.updateOrInsertServerInfo(); } logger.info("ResourceInitializer completed"); if ( ConfigInfo.isJustRestored() ) { logger.info("handle post restore for user management"); PostUpgradeHandler postUpgradeHandler = wac.getBean("postUpgradeHandler", PostUpgradeHandler.class); postUpgradeHandler.handlePostUpgradeForUsrMgmt(); logger.info("handle post restore for other functions"); PostRestoreHandler postRestoreHandler = wac.getBean("postRestoreHandler", PostRestoreHandler.class); postRestoreHandler.handlePostRestore(); ConfigInfo.setJustRestored(false); ConfigInfo.save(); } if ( ConfigInfo.isJustUpgraded() ) { logger.info("handle post upgrade for the first time start after upgrade"); PostUpgradeHandler postUpgradeHandler = wac.getBean("postUpgradeHandler", PostUpgradeHandler.class); postUpgradeHandler.handlePostUpgrade(); ConfigInfo.setJustUpgraded(false); ConfigInfo.save(); } } catch (Throwable t) { logger.error("Resource initialization failed.", t); } }  @Override void contextInitialized(ServletContextEvent sce); @Override void contextDestroyed(ServletContextEvent sce);  }
@Test public void testContextInitialized() { resourceInitializer.contextInitialized(sce); verify(sce, times(1)).getServletContext(); verify(clusteringService, times(1)).init(); verify(collectionInitializerService, times(1)).setDeployTime(Mockito.any(Date.class)); verify(collectionInitializerService, times(1)).generateInstanceId(); verify(resourceInitializerService, times(1)).isResourceInitialized(); verify(resourceInitializerService, times(1)).initResource(); verify(resourceInitializerService, times(1)).updateOrInsertServerInfo(); when(resourceInitializerService.isResourceInitialized()).thenReturn(true); resourceInitializer.contextInitialized(sce); verify(sce, times(2)).getServletContext(); verify(clusteringService, times(2)).init(); verify(collectionInitializerService, times(2)).setDeployTime(Mockito.any(Date.class)); verify(collectionInitializerService, times(2)).generateInstanceId(); verify(resourceInitializerService, times(2)).isResourceInitialized(); verify(resourceInitializerService, times(1)).initResource(); verify(resourceInitializerService, times(1)).updateOrInsertServerInfo(); booleanFlag = false; when(resourceInitializerService.isResourceInitialized()).thenReturn(false); resourceInitializer.contextInitialized(sce); verify(sce, times(3)).getServletContext(); verify(clusteringService, times(3)).init(); verify(collectionInitializerService, times(3)).setDeployTime(Mockito.any(Date.class)); verify(collectionInitializerService, times(3)).generateInstanceId(); verify(resourceInitializerService, times(3)).isResourceInitialized(); verify(resourceInitializerService, times(1)).initResource(); verify(resourceInitializerService, times(2)).updateOrInsertServerInfo(); }