@ -20,9 +20,11 @@ import java.sql.Connection;
import java.sql.SQLException ;
import java.sql.Statement ;
import javax.persistence.EntityManagerFactory ;
import javax.sql.DataSource ;
import org.junit.Test ;
import org.mockito.InOrder ;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties ;
import org.springframework.boot.context.properties.EnableConfigurationProperties ;
@ -36,6 +38,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import static org.hamcrest.Matchers.is ;
import static org.junit.Assert.assertThat ;
import static org.mockito.BDDMockito.given ;
import static org.mockito.Mockito.inOrder ;
import static org.mockito.Mockito.mock ;
import static org.mockito.Mockito.times ;
import static org.mockito.Mockito.verify ;
@ -49,7 +52,7 @@ public class DevToolsDataSourceAutoConfigurationTests {
@Test
public void embeddedDatabaseIsNotShutDown ( ) throws SQLException {
ConfigurableApplicationContext context = createContext ( "org.h2.Driver" ,
ConfigurableApplicationContext context = createContext WithDriver ( "org.h2.Driver" ,
EmbeddedDatabaseConfiguration . class ) ;
DataSource dataSource = context . getBean ( DataSource . class ) ;
context . close ( ) ;
@ -58,8 +61,8 @@ public class DevToolsDataSourceAutoConfigurationTests {
@Test
public void externalDatabaseIsNotShutDown ( ) throws SQLException {
ConfigurableApplicationContext context = createContext ( "org.postgresql.Driver" ,
DataSourceConfiguration . class ) ;
ConfigurableApplicationContext context = createContext WithDriver(
"org.postgresql.Driver" , DataSourceConfiguration . class ) ;
DataSource dataSource = context . getBean ( DataSource . class ) ;
context . close ( ) ;
verify ( dataSource , times ( 0 ) ) . getConnection ( ) ;
@ -67,7 +70,35 @@ public class DevToolsDataSourceAutoConfigurationTests {
@Test
public void nonEmbeddedInMemoryDatabaseIsShutDown ( ) throws SQLException {
ConfigurableApplicationContext context = createContext ( "org.h2.Driver" ,
ConfigurableApplicationContext context = createContextWithDriver ( "org.h2.Driver" ,
DataSourceConfiguration . class ) ;
DataSource dataSource = context . getBean ( DataSource . class ) ;
Connection connection = mock ( Connection . class ) ;
given ( dataSource . getConnection ( ) ) . willReturn ( connection ) ;
Statement statement = mock ( Statement . class ) ;
given ( connection . createStatement ( ) ) . willReturn ( statement ) ;
context . close ( ) ;
verify ( statement ) . execute ( "SHUTDOWN" ) ;
}
@Test
public void nonEmbeddedInMemoryDatabaseConfiguredWithDriverIsShutDown ( )
throws SQLException {
ConfigurableApplicationContext context = createContextWithDriver ( "org.h2.Driver" ,
DataSourceConfiguration . class ) ;
DataSource dataSource = context . getBean ( DataSource . class ) ;
Connection connection = mock ( Connection . class ) ;
given ( dataSource . getConnection ( ) ) . willReturn ( connection ) ;
Statement statement = mock ( Statement . class ) ;
given ( connection . createStatement ( ) ) . willReturn ( statement ) ;
context . close ( ) ;
verify ( statement ) . execute ( "SHUTDOWN" ) ;
}
@Test
public void nonEmbeddedInMemoryDatabaseConfiguredWithUrlIsShutDown ( )
throws SQLException {
ConfigurableApplicationContext context = createContextWithUrl ( "jdbc:h2:mem:test" ,
DataSourceConfiguration . class ) ;
DataSource dataSource = context . getBean ( DataSource . class ) ;
Connection connection = mock ( Connection . class ) ;
@ -87,13 +118,40 @@ public class DevToolsDataSourceAutoConfigurationTests {
is ( 0 ) ) ;
}
private ConfigurableApplicationContext createContext ( String driver ,
@Test
public void entityManagerFactoryIsClosedBeforeDatabaseIsShutDown ( )
throws SQLException {
ConfigurableApplicationContext context = createContextWithUrl ( "jdbc:h2:mem:test" ,
DataSourceConfiguration . class , EntityManagerFactoryConfiguration . class ) ;
DataSource dataSource = context . getBean ( DataSource . class ) ;
Connection connection = mock ( Connection . class ) ;
given ( dataSource . getConnection ( ) ) . willReturn ( connection ) ;
Statement statement = mock ( Statement . class ) ;
given ( connection . createStatement ( ) ) . willReturn ( statement ) ;
EntityManagerFactory entityManagerFactory = context
. getBean ( EntityManagerFactory . class ) ;
context . close ( ) ;
InOrder inOrder = inOrder ( statement , entityManagerFactory ) ;
inOrder . verify ( statement ) . execute ( "SHUTDOWN" ) ;
inOrder . verify ( entityManagerFactory ) . close ( ) ;
}
private ConfigurableApplicationContext createContextWithDriver ( String driver ,
Class < ? > . . . classes ) {
return createContext ( "spring.datasource.driver-class-name:" + driver , classes ) ;
}
private ConfigurableApplicationContext createContextWithUrl ( String url ,
Class < ? > . . . classes ) {
return createContext ( "spring.datasource.url:" + url , classes ) ;
}
private ConfigurableApplicationContext createContext ( String property ,
Class < ? > . . . classes ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
context . register ( classes ) ;
context . register ( DevToolsDataSourceAutoConfiguration . class ) ;
EnvironmentTestUtils . addEnvironment ( context ,
"spring.datasource.driver-class-name:" + driver ) ;
EnvironmentTestUtils . addEnvironment ( context , property ) ;
context . refresh ( ) ;
return context ;
}
@ -113,7 +171,7 @@ public class DevToolsDataSourceAutoConfigurationTests {
static class DataSourceConfiguration {
@Bean
public DataSource in ( ) {
public DataSource dataSource ( ) {
return mock ( DataSource . class ) ;
}
@ -123,10 +181,19 @@ public class DevToolsDataSourceAutoConfigurationTests {
static class NoDataSourcePropertiesConfiguration {
@Bean
public DataSource in ( ) {
public DataSource dataSource ( ) {
return mock ( DataSource . class ) ;
}
}
@Configuration
static class EntityManagerFactoryConfiguration {
@Bean
public EntityManagerFactory entityManagerFactory ( ) {
return mock ( EntityManagerFactory . class ) ;
}
}
}