Polish
This commit is contained in:
parent
acaef9c95a
commit
1ba0df99b2
|
|
@ -30,19 +30,21 @@ import javax.sql.DataSource;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import org.apache.commons.dbcp2.BasicDataSource;
|
import org.apache.commons.dbcp2.BasicDataSource;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
|
||||||
import org.springframework.boot.jdbc.DatabaseDriver;
|
import org.springframework.boot.jdbc.DatabaseDriver;
|
||||||
import org.springframework.boot.test.util.TestPropertyValues;
|
import org.springframework.boot.test.util.TestPropertyValues;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
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 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.ArgumentMatchers.contains;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -53,62 +55,49 @@ import static org.mockito.Mockito.mock;
|
||||||
*/
|
*/
|
||||||
public class DataSourceAutoConfigurationTests {
|
public class DataSourceAutoConfigurationTests {
|
||||||
|
|
||||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
@Rule
|
||||||
|
public final ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
@Before
|
private ConfigurableApplicationContext context;
|
||||||
public void init() {
|
|
||||||
EmbeddedDatabaseConnection.override = null;
|
|
||||||
TestPropertyValues.of("spring.datasource.initialize:false",
|
|
||||||
"spring.datasource.url:jdbc:hsqldb:mem:testdb-" + new Random().nextInt())
|
|
||||||
.applyTo(this.context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void restore() {
|
public void close() {
|
||||||
EmbeddedDatabaseConnection.override = null;
|
if (this.context != null) {
|
||||||
this.context.close();
|
this.context.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultDataSourceExists() throws Exception {
|
public void testDefaultDataSourceExists() throws Exception {
|
||||||
this.context.register(DataSourceAutoConfiguration.class,
|
load();
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getBean(DataSource.class)).isNotNull();
|
assertThat(this.context.getBean(DataSource.class)).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDataSourceHasEmbeddedDefault() throws Exception {
|
public void testDataSourceHasEmbeddedDefault() throws Exception {
|
||||||
this.context.register(DataSourceAutoConfiguration.class,
|
load();
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
HikariDataSource dataSource = this.context.getBean(HikariDataSource.class);
|
HikariDataSource dataSource = this.context.getBean(HikariDataSource.class);
|
||||||
assertThat(dataSource.getJdbcUrl()).isNotNull();
|
assertThat(dataSource.getJdbcUrl()).isNotNull();
|
||||||
assertThat(dataSource.getDriverClassName()).isNotNull();
|
assertThat(dataSource.getDriverClassName()).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = BeanCreationException.class)
|
@Test
|
||||||
public void testBadUrl() throws Exception {
|
public void testBadUrl() throws Exception {
|
||||||
TestPropertyValues.of("spring.datasource.url:jdbc:not-going-to-work")
|
try {
|
||||||
.applyTo(this.context);
|
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
|
||||||
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
|
this.thrown.expect(BeanCreationException.class);
|
||||||
this.context.register(DataSourceAutoConfiguration.class,
|
load("spring.datasource.url:jdbc:not-going-to-work");
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
}
|
||||||
this.context.refresh();
|
finally {
|
||||||
assertThat(this.context.getBean(DataSource.class)).isNotNull();
|
EmbeddedDatabaseConnection.override = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = BeanCreationException.class)
|
@Test
|
||||||
public void testBadDriverClass() throws Exception {
|
public void testBadDriverClass() throws Exception {
|
||||||
TestPropertyValues
|
this.thrown.expect(BeanCreationException.class);
|
||||||
.of("spring.datasource.driverClassName:org.none.jdbcDriver",
|
this.thrown.expectMessage(contains("org.none.jdbcDriver"));
|
||||||
"spring.datasource.url:jdbc:hsqldb:mem:testdb")
|
load("spring.datasource.driverClassName:org.none.jdbcDriver");
|
||||||
.applyTo(this.context);
|
|
||||||
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
|
|
||||||
this.context.register(DataSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getBean(DataSource.class)).isNotNull();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -123,7 +112,7 @@ public class DataSourceAutoConfigurationTests {
|
||||||
public void tomcatIsFallback() throws Exception {
|
public void tomcatIsFallback() throws Exception {
|
||||||
org.apache.tomcat.jdbc.pool.DataSource dataSource = autoConfigureDataSource(
|
org.apache.tomcat.jdbc.pool.DataSource dataSource = autoConfigureDataSource(
|
||||||
org.apache.tomcat.jdbc.pool.DataSource.class, "com.zaxxer.hikari");
|
org.apache.tomcat.jdbc.pool.DataSource.class, "com.zaxxer.hikari");
|
||||||
assertThat(dataSource.getUrl()).isEqualTo("jdbc:hsqldb:mem:testdb");
|
assertThat(dataSource.getUrl()).startsWith("jdbc:hsqldb:mem:testdb");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -139,7 +128,7 @@ public class DataSourceAutoConfigurationTests {
|
||||||
public void commonsDbcp2IsFallback() throws Exception {
|
public void commonsDbcp2IsFallback() throws Exception {
|
||||||
BasicDataSource dataSource = autoConfigureDataSource(BasicDataSource.class,
|
BasicDataSource dataSource = autoConfigureDataSource(BasicDataSource.class,
|
||||||
"com.zaxxer.hikari", "org.apache.tomcat");
|
"com.zaxxer.hikari", "org.apache.tomcat");
|
||||||
assertThat(dataSource.getUrl()).isEqualTo("jdbc:hsqldb:mem:testdb");
|
assertThat(dataSource.getUrl()).startsWith("jdbc:hsqldb:mem:testdb");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -153,13 +142,8 @@ public class DataSourceAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmbeddedTypeDefaultsUsername() throws Exception {
|
public void testEmbeddedTypeDefaultsUsername() throws Exception {
|
||||||
TestPropertyValues
|
load("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
|
||||||
.of("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
|
"spring.datasource.url:jdbc:hsqldb:mem:testdb");
|
||||||
"spring.datasource.url:jdbc:hsqldb:mem:testdb")
|
|
||||||
.applyTo(this.context);
|
|
||||||
this.context.register(DataSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
DataSource bean = this.context.getBean(DataSource.class);
|
DataSource bean = this.context.getBean(DataSource.class);
|
||||||
assertThat(bean).isNotNull();
|
assertThat(bean).isNotNull();
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
|
|
@ -174,33 +158,25 @@ public class DataSourceAutoConfigurationTests {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void explicitTypeNoSupportedDataSource() {
|
public void explicitTypeNoSupportedDataSource() {
|
||||||
TestPropertyValues
|
load(null, new HidePackagesClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
|
||||||
.of("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
|
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"),
|
||||||
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
|
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
|
||||||
"spring.datasource.type:"
|
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
|
||||||
+ SimpleDriverDataSource.class.getName())
|
"spring.datasource.type:"
|
||||||
.applyTo(this.context);
|
+ SimpleDriverDataSource.class.getName());
|
||||||
this.context.setClassLoader(
|
|
||||||
new HidePackagesClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
|
|
||||||
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"));
|
|
||||||
testExplicitType();
|
testExplicitType();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void explicitTypeSupportedDataSource() {
|
public void explicitTypeSupportedDataSource() {
|
||||||
TestPropertyValues
|
load("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
|
||||||
.of("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
|
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
|
||||||
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
|
"spring.datasource.type:"
|
||||||
"spring.datasource.type:"
|
+ SimpleDriverDataSource.class.getName());
|
||||||
+ SimpleDriverDataSource.class.getName())
|
|
||||||
.applyTo(this.context);
|
|
||||||
testExplicitType();
|
testExplicitType();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testExplicitType() {
|
private void testExplicitType() {
|
||||||
this.context.register(DataSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getBeansOfType(DataSource.class)).hasSize(1);
|
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();
|
||||||
|
|
@ -209,12 +185,8 @@ public class DataSourceAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExplicitDriverClassClearsUsername() throws Exception {
|
public void testExplicitDriverClassClearsUsername() throws Exception {
|
||||||
TestPropertyValues.of(
|
load("spring.datasource.driverClassName:" + DatabaseTestDriver.class.getName(),
|
||||||
"spring.datasource.driverClassName:" + DatabaseTestDriver.class.getName(),
|
"spring.datasource.url:jdbc:foo://localhost");
|
||||||
"spring.datasource.url:jdbc:foo://localhost").applyTo(this.context);
|
|
||||||
this.context.register(DataSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
DataSource dataSource = this.context.getBean(DataSource.class);
|
DataSource dataSource = this.context.getBean(DataSource.class);
|
||||||
assertThat(dataSource).isNotNull();
|
assertThat(dataSource).isNotNull();
|
||||||
assertThat(((HikariDataSource) dataSource).getDriverClassName())
|
assertThat(((HikariDataSource) dataSource).getDriverClassName())
|
||||||
|
|
@ -224,10 +196,7 @@ public class DataSourceAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultDataSourceCanBeOverridden() throws Exception {
|
public void testDefaultDataSourceCanBeOverridden() throws Exception {
|
||||||
this.context.register(TestDataSourceConfiguration.class,
|
load(TestDataSourceConfiguration.class);
|
||||||
DataSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
DataSource dataSource = this.context.getBean(DataSource.class);
|
DataSource dataSource = this.context.getBean(DataSource.class);
|
||||||
assertThat(dataSource).isInstanceOf(BasicDataSource.class);
|
assertThat(dataSource).isInstanceOf(BasicDataSource.class);
|
||||||
}
|
}
|
||||||
|
|
@ -235,19 +204,38 @@ public class DataSourceAutoConfigurationTests {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private <T extends DataSource> T autoConfigureDataSource(Class<T> expectedType,
|
private <T extends DataSource> T autoConfigureDataSource(Class<T> expectedType,
|
||||||
final String... hiddenPackages) {
|
final String... hiddenPackages) {
|
||||||
TestPropertyValues
|
load(null, new HidePackagesClassLoader(hiddenPackages));
|
||||||
.of("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
|
|
||||||
"spring.datasource.url:jdbc:hsqldb:mem:testdb")
|
|
||||||
.applyTo(this.context);
|
|
||||||
this.context.setClassLoader(new HidePackagesClassLoader(hiddenPackages));
|
|
||||||
this.context.register(DataSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
DataSource bean = this.context.getBean(DataSource.class);
|
DataSource bean = this.context.getBean(DataSource.class);
|
||||||
assertThat(bean).isInstanceOf(expectedType);
|
assertThat(bean).isInstanceOf(expectedType);
|
||||||
return (T) bean;
|
return (T) bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void load(String... environment) {
|
||||||
|
load(null, environment);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load(Class<?> config, String... environment) {
|
||||||
|
load(config, null, environment);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load(Class<?> config, ClassLoader classLoader, String... environment) {
|
||||||
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
|
if (classLoader != null) {
|
||||||
|
ctx.setClassLoader(classLoader);
|
||||||
|
}
|
||||||
|
TestPropertyValues
|
||||||
|
.of("spring.datasource.initialize=false",
|
||||||
|
"spring.datasource.url:jdbc:hsqldb:mem:testdb-" + new Random().nextInt())
|
||||||
|
.applyTo(ctx);
|
||||||
|
TestPropertyValues.of(environment).applyTo(ctx);
|
||||||
|
if (config != null) {
|
||||||
|
ctx.register(config);
|
||||||
|
}
|
||||||
|
ctx.register(DataSourceAutoConfiguration.class);
|
||||||
|
ctx.refresh();
|
||||||
|
this.context = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
static class TestDataSourceConfiguration {
|
static class TestDataSourceConfiguration {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue