This commit is contained in:
Stephane Nicoll 2017-05-31 17:25:38 +02:00
parent 7524e62537
commit 6f25131a1d
1 changed files with 59 additions and 61 deletions

View File

@ -20,13 +20,11 @@ import java.util.Random;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
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;
@ -43,98 +41,98 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer * @author Dave Syer
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Kazuki Shimizu * @author Kazuki Shimizu
* @since 1.4.0
*/ */
public class JdbcTemplateAutoConfigurationTests { public class JdbcTemplateAutoConfigurationTests {
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private ConfigurableApplicationContext context;
@Before
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 restore() {
EmbeddedDatabaseConnection.override = null; if (this.context != null) {
this.context.close(); this.context.close();
}
} }
@Test @Test
public void testJdbcTemplateExists() throws Exception { public void testJdbcTemplateExists() {
this.context.register(DataSourceAutoConfiguration.class, load();
JdbcTemplateAutoConfiguration.class, assertThat(this.context.getBeansOfType(JdbcTemplate.class)).hasSize(1);
PropertyPlaceholderAutoConfiguration.class); JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class);
this.context.refresh(); assertThat(jdbcTemplate.getDataSource()).isEqualTo(
this.context.getBean(DataSource.class));
}
@Test
public void testJdbcTemplateExistsWithCustomDataSource() {
load(TestDataSourceConfiguration.class);
assertThat(this.context.getBeansOfType(JdbcTemplate.class)).hasSize(1);
JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class); JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class);
assertThat(jdbcTemplate).isNotNull(); assertThat(jdbcTemplate).isNotNull();
assertThat(jdbcTemplate.getDataSource()).isNotNull(); assertThat(jdbcTemplate.getDataSource()).isEqualTo(
this.context.getBean("customDataSource"));
} }
@Test @Test
public void testJdbcTemplateExistsWithCustomDataSource() throws Exception { public void testNamedParameterJdbcTemplateExists() {
this.context.register(TestDataSourceConfiguration.class, load();
DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class))
PropertyPlaceholderAutoConfiguration.class); .hasSize(1);
this.context.refresh();
JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class);
assertThat(jdbcTemplate).isNotNull();
assertThat(jdbcTemplate.getDataSource() instanceof BasicDataSource).isTrue();
} }
@Test @Test
public void testNamedParameterJdbcTemplateExists() throws Exception { public void testMultiDataSource() {
this.context.register(DataSourceAutoConfiguration.class, load(MultiDataSourceConfiguration.class);
JdbcTemplateAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(NamedParameterJdbcOperations.class)).isNotNull();
}
@Test
public void testMultiDataSource() throws Exception {
this.context.register(MultiDataSourceConfiguration.class,
DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeansOfType(JdbcOperations.class)).isEmpty(); assertThat(this.context.getBeansOfType(JdbcOperations.class)).isEmpty();
assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class))
.isEmpty(); .isEmpty();
} }
@Test @Test
public void testMultiDataSourceUsingPrimary() throws Exception { public void testMultiDataSourceUsingPrimary() {
this.context.register(MultiDataSourceUsingPrimaryConfiguration.class, load(MultiDataSourceUsingPrimaryConfiguration.class);
DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, assertThat(this.context.getBeansOfType(JdbcOperations.class)).hasSize(1);
PropertyPlaceholderAutoConfiguration.class); assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class))
this.context.refresh(); .hasSize(1);
assertThat(this.context.getBean(JdbcOperations.class)).isNotNull(); assertThat(this.context.getBean(JdbcTemplate.class).getDataSource())
assertThat(this.context.getBean(NamedParameterJdbcOperations.class)).isNotNull(); .isEqualTo(this.context.getBean("test1DataSource"));
} }
@Test @Test
public void testExistingCustomJdbcTemplate() throws Exception { public void testExistingCustomJdbcTemplate() {
this.context.register(CustomConfiguration.class, load(CustomConfiguration.class);
DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, assertThat(this.context.getBeansOfType(JdbcOperations.class)).hasSize(1);
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(JdbcOperations.class)) assertThat(this.context.getBean(JdbcOperations.class))
.isEqualTo(this.context.getBean("customJdbcOperations")); .isEqualTo(this.context.getBean("customJdbcOperations"));
} }
@Test @Test
public void testExistingCustomNamedParameterJdbcTemplate() throws Exception { public void testExistingCustomNamedParameterJdbcTemplate() {
this.context.register(CustomConfiguration.class, load(CustomConfiguration.class);
DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class))
PropertyPlaceholderAutoConfiguration.class); .hasSize(1);
this.context.refresh();
assertThat(this.context.getBean(NamedParameterJdbcOperations.class)) assertThat(this.context.getBean(NamedParameterJdbcOperations.class))
.isEqualTo(this.context.getBean("customNamedParameterJdbcOperations")); .isEqualTo(this.context.getBean("customNamedParameterJdbcOperations"));
} }
public void load(String... environment) {
load(null, environment);
}
public void load(Class<?> config, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
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,
JdbcTemplateAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
@Configuration @Configuration
static class CustomConfiguration { static class CustomConfiguration {
@ -155,7 +153,7 @@ public class JdbcTemplateAutoConfigurationTests {
static class TestDataSourceConfiguration { static class TestDataSourceConfiguration {
@Bean @Bean
public DataSource dataSource() { public DataSource customDataSource() {
return new TestDataSource("overridedb"); return new TestDataSource("overridedb");
} }