/* * ****************************************************************************** * * Copyright (C) 2002-2015 by Pentaho : http://www.pentaho.com * * ****************************************************************************** * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.pentaho.platform.web.http.context; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.pentaho.platform.web.hsqldb.HsqlDatabaseStarterBean; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import static org.junit.Assert.*; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** * Created by rfellows on 10/28/15. */ @RunWith( MockitoJUnitRunner.class ) public class HsqldbStartupListenerTest { HsqldbStartupListener startupListener; @Mock ServletContextEvent contextEvent; @Mock ServletContext context; @Mock HsqlDatabaseStarterBean starterBean; @Before public void setUp() throws Exception { startupListener = new HsqldbStartupListener(); when( contextEvent.getServletContext() ).thenReturn( context ); } @Test public void testContextDestroyed_nullStarterBean() throws Exception { when( context.getAttribute( "hsqldb-starter-bean" ) ).thenReturn( null ); startupListener.contextDestroyed( contextEvent ); } @Test public void testContextDestroyed() throws Exception { when( context.getAttribute( "hsqldb-starter-bean" ) ).thenReturn( starterBean ); startupListener.contextDestroyed( contextEvent ); verify( starterBean ).stop(); } @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 ); } }