@ -42,6 +42,7 @@ import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext ;
import org.springframework.context.annotation.AnnotationConfigApplicationContext ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.jdbc.datasource.SimpleDriverDataSource ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.mockito.Mockito.mock ;
import static org.mockito.Mockito.mock ;
@ -184,18 +185,39 @@ public class DataSourceAutoConfigurationTests {
assertThat ( pool . getUsername ( ) ) . isEqualTo ( "sa" ) ;
assertThat ( pool . getUsername ( ) ) . isEqualTo ( "sa" ) ;
}
}
/ * *
* This test makes sure that if no supported data source is present , a datasource
* is still created if "spring.datasource.type" is present .
* /
@Test
@Test
public void explicitType ( ) {
public void explicitType NoSupportedDataSource ( ) {
EnvironmentTestUtils . addEnvironment ( this . context ,
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver" ,
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver" ,
"spring.datasource.url:jdbc:hsqldb:mem:testdb" ,
"spring.datasource.url:jdbc:hsqldb:mem:testdb" ,
"spring.datasource.type:" + HikariDataSource . class . getName ( ) ) ;
"spring.datasource.type:" + SimpleDriverDataSource . class . getName ( ) ) ;
this . context . setClassLoader ( new HidePackagesClassLoader (
"org.apache.tomcat" , "com.zaxxer.hikari" , "org.apache.commons.dbcp" ,
"org.apache.commons.dbcp2" ) ) ;
testExplicitType ( ) ;
}
@Test
public void explicitTypeSupportedDataSource ( ) {
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver" ,
"spring.datasource.url:jdbc:hsqldb:mem:testdb" ,
"spring.datasource.type:" + SimpleDriverDataSource . class . getName ( ) ) ;
testExplicitType ( ) ;
}
private void testExplicitType ( ) {
this . context . register ( DataSourceAutoConfiguration . class ,
this . context . register ( DataSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
this . context . refresh ( ) ;
assertThat ( this . context . getBeansOfType ( DataSource . class ) ) . hasSize ( 1 ) ;
DataSource bean = this . context . getBean ( DataSource . class ) ;
DataSource bean = this . context . getBean ( DataSource . class ) ;
assertThat ( bean ) . isNotNull ( ) ;
assertThat ( bean ) . isNotNull ( ) ;
assertThat ( bean . getClass ( ) ) . isEqualTo ( HikariDataSource . class ) ;
assertThat ( bean . getClass ( ) ) . isEqualTo ( SimpleDriver DataSource. class ) ;
}
}
@Test
@Test
@ -232,21 +254,7 @@ public class DataSourceAutoConfigurationTests {
EnvironmentTestUtils . addEnvironment ( this . context ,
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver" ,
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver" ,
"spring.datasource.url:jdbc:hsqldb:mem:testdb" ) ;
"spring.datasource.url:jdbc:hsqldb:mem:testdb" ) ;
this . context . setClassLoader (
this . context . setClassLoader ( new HidePackagesClassLoader ( hiddenPackages ) ) ;
new URLClassLoader ( new URL [ 0 ] , getClass ( ) . getClassLoader ( ) ) {
@Override
protected Class < ? > loadClass ( String name , boolean resolve )
throws ClassNotFoundException {
for ( String hiddenPackage : hiddenPackages ) {
if ( name . startsWith ( hiddenPackage ) ) {
throw new ClassNotFoundException ( ) ;
}
}
return super . loadClass ( name , resolve ) ;
}
} ) ;
this . context . register ( DataSourceAutoConfiguration . class ,
this . context . register ( DataSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
this . context . refresh ( ) ;
@ -312,4 +320,27 @@ public class DataSourceAutoConfigurationTests {
}
}
private static final class HidePackagesClassLoader extends URLClassLoader {
private final String [ ] hiddenPackages ;
private HidePackagesClassLoader ( String . . . hiddenPackages ) {
super ( new URL [ 0 ] , DataSourceAutoConfigurationTests . class . getClassLoader ( ) ) ;
this . hiddenPackages = hiddenPackages ;
}
@Override
protected Class < ? > loadClass ( String name , boolean resolve )
throws ClassNotFoundException {
for ( String hiddenPackage : this . hiddenPackages ) {
if ( name . startsWith ( hiddenPackage ) ) {
throw new ClassNotFoundException ( ) ;
}
}
return super . loadClass ( name , resolve ) ;
}
}
}
}