@ -24,11 +24,13 @@ import java.sql.SQLFeatureNotSupportedException;
import java.util.HashMap ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.Map ;
import java.util.Properties ;
import java.util.Properties ;
import java.util.Random ;
import java.util.logging.Logger ;
import java.util.logging.Logger ;
import javax.sql.DataSource ;
import javax.sql.DataSource ;
import org.apache.commons.dbcp.BasicDataSource ;
import org.apache.commons.dbcp.BasicDataSource ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.Test ;
import org.mockito.Mockito ;
import org.mockito.Mockito ;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration ;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration ;
@ -56,6 +58,13 @@ public class DataSourceAutoConfigurationTests {
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
@Before
public void init ( ) {
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.datasource.initialize:false" ,
"spring.datasource.url:jdbc:hsqldb:mem:testdb-" + new Random ( ) . nextInt ( ) ) ;
}
@Test
@Test
public void testDefaultDataSourceExists ( ) throws Exception {
public void testDefaultDataSourceExists ( ) throws Exception {
this . context . register ( DataSourceAutoConfiguration . class ,
this . context . register ( DataSourceAutoConfiguration . class ,
@ -85,8 +94,7 @@ public class DataSourceAutoConfigurationTests {
. addEnvironment (
. addEnvironment (
this . context ,
this . context ,
"spring.datasource.driverClassName:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationTests$DatabaseDriver" ,
"spring.datasource.driverClassName:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationTests$DatabaseDriver" ,
"spring.datasource.url:jdbc:foo://localhost" ,
"spring.datasource.url:jdbc:foo://localhost" ) ;
"spring.datasource.initialize:false" ) ;
this . context . register ( DataSourceAutoConfiguration . class ,
this . context . register ( DataSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
this . context . refresh ( ) ;
@ -141,6 +149,8 @@ public class DataSourceAutoConfigurationTests {
@Test
@Test
public void testDataSourceInitialized ( ) throws Exception {
public void testDataSourceInitialized ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.datasource.initialize:true" ) ;
this . context . register ( DataSourceAutoConfiguration . class ,
this . context . register ( DataSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
this . context . refresh ( ) ;
@ -172,16 +182,17 @@ public class DataSourceAutoConfigurationTests {
@Test
@Test
public void testDataSourceInitializedWithMultipleScripts ( ) throws Exception {
public void testDataSourceInitializedWithMultipleScripts ( ) throws Exception {
this . context . register ( DataSourceAutoConfiguration . class ,
EnvironmentTestUtils . addEnvironment (
PropertyPlaceholderAutoConfiguration . class ) ;
this . context ,
Map < String , Object > map = new HashMap < String , Object > ( ) ;
"spring.datasource.initialize:true" ,
map . put ( DataSourceAutoConfiguration . CONFIGURATION_PREFIX + ".schema" ,
"spring.datasource.schema:"
ClassUtils . addResourcePathToPackagePath ( getClass ( ) , "schema.sql" )
+ ClassUtils . addResourcePathToPackagePath ( getClass ( ) ,
"schema.sql" )
+ ","
+ ","
+ ClassUtils . addResourcePathToPackagePath ( getClass ( ) ,
+ ClassUtils . addResourcePathToPackagePath ( getClass ( ) ,
"another.sql" ) ) ;
"another.sql" ) ) ;
this . context . getEnvironment( ) . getPropertySources ( )
this . context . register( DataSourceAutoConfiguration . class ,
. addFirst ( new MapPropertySource ( "test" , map ) ) ;
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
this . context . refresh ( ) ;
DataSource dataSource = this . context . getBean ( DataSource . class ) ;
DataSource dataSource = this . context . getBean ( DataSource . class ) ;
assertTrue ( dataSource instanceof org . apache . tomcat . jdbc . pool . DataSource ) ;
assertTrue ( dataSource instanceof org . apache . tomcat . jdbc . pool . DataSource ) ;