HsqldbStartupListener implements ServletContextListener { public void contextInitialized( ServletContextEvent sce ) { ServletContext ctx = sce.getServletContext(); logger.debug( "Starting HSQLDB Embedded Listener" ); HsqlDatabaseStarterBean starterBean = new HsqlDatabaseStarterBean(); String port = ctx.getInitParameter( "hsqldb-port" ); int portNum = -1; if ( port != null ) { logger.debug( String.format( "Port override specified: %s", port ) ); try { portNum = Integer.parseInt( port ); starterBean.setPort( portNum ); } catch ( NumberFormatException ex ) { logger.error( Messages.getErrorString( "HsqldbStartupListener.ERROR_0004_INVALID_PORT", "9001" ) ); port = null; } } starterBean.setDatabases( getDatabases( ctx ) ); String sampleDataAllowPortFailover = ctx.getInitParameter( "hsqldb-allow-port-failover" ); if ( ( sampleDataAllowPortFailover != null ) && ( sampleDataAllowPortFailover.equalsIgnoreCase( "true" ) ) ) { logger.debug( String.format( "Allow Port Failover specified" ) ); starterBean.setAllowPortFailover( true ); } if ( starterBean.start() ) { ctx.setAttribute( "hsqldb-starter-bean", starterBean ); } }  void contextDestroyed( ServletContextEvent sce ); void contextInitialized( ServletContextEvent sce );  }
@Test public void testContextInitialized_nullPort() throws Exception { when( context.getInitParameter( "hsqldb-port" ) ).thenReturn( null ); startupListener.contextInitialized( contextEvent ); verify( context, never() ).setAttribute( eq( "hsqldb-starter-bean" ), any() ); }
@Test public void testContextInitialized_gracefullyHandleInvalidPort() throws Exception { when( context.getInitParameter( "hsqldb-port" ) ).thenReturn( "eighty-eighty" ); startupListener.contextInitialized( contextEvent ); verify( context, never() ).setAttribute( eq( "hsqldb-starter-bean" ), any() ); }
@Test public void testContextInitialized_nullAllowPortFailover() throws Exception { when( context.getInitParameter( "hsqldb-port" ) ).thenReturn( "9999" ); when( context.getInitParameter( "hsqldb-allow-port-failover" ) ).thenReturn( null ); startupListener.contextInitialized( contextEvent ); verify( context, never() ).setAttribute( eq( "hsqldb-starter-bean" ), any() ); }
@Test public void testContextInitialized() throws Exception { when( context.getInitParameter( "hsqldb-port" ) ).thenReturn( "9999" ); when( context.getInitParameter( "hsqldb-allow-port-failover" ) ).thenReturn( "true" ); when( context.getInitParameter( "hsqldb-databases" ) ).thenReturn( "test@localhost" ); startupListener.contextInitialized( contextEvent ); verify( context ).setAttribute( eq( "hsqldb-starter-bean" ), any() ); startupListener.contextDestroyed( contextEvent ); }