Merge branch '2.0.x'

pull/13707/merge
Stephane Nicoll 6 years ago
commit 8194dc4aea

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,16 +16,12 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import java.util.Random;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
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.context.annotation.Primary; import org.springframework.context.annotation.Primary;
@ -46,129 +42,121 @@ import static org.mockito.Mockito.mock;
*/ */
public class JdbcTemplateAutoConfigurationTests { public class JdbcTemplateAutoConfigurationTests {
private ConfigurableApplicationContext context; private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.datasource.initialization-mode=never",
@After "spring.datasource.generate-unique-name=true")
public void restore() { .withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
if (this.context != null) { JdbcTemplateAutoConfiguration.class));
this.context.close();
}
}
@Test @Test
public void testJdbcTemplateExists() { public void testJdbcTemplateExists() {
load(); this.contextRunner.run((context) -> {
assertThat(this.context.getBeansOfType(JdbcOperations.class)).hasSize(1); assertThat(context).hasSingleBean(JdbcOperations.class);
JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class); JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
assertThat(jdbcTemplate.getDataSource()) assertThat(jdbcTemplate.getDataSource())
.isEqualTo(this.context.getBean(DataSource.class)); .isEqualTo(context.getBean(DataSource.class));
assertThat(jdbcTemplate.getFetchSize()).isEqualTo(-1); assertThat(jdbcTemplate.getFetchSize()).isEqualTo(-1);
assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(-1); assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(-1);
assertThat(jdbcTemplate.getMaxRows()).isEqualTo(-1); assertThat(jdbcTemplate.getMaxRows()).isEqualTo(-1);
});
} }
@Test @Test
public void testJdbcTemplateWithCustomProperties() { public void testJdbcTemplateWithCustomProperties() {
load("spring.jdbc.template.fetch-size:100", this.contextRunner.withPropertyValues("spring.jdbc.template.fetch-size:100",
"spring.jdbc.template.query-timeout:60", "spring.jdbc.template.query-timeout:60",
"spring.jdbc.template.max-rows:1000"); "spring.jdbc.template.max-rows:1000").run((context) -> {
JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class); assertThat(context).hasSingleBean(JdbcOperations.class);
assertThat(jdbcTemplate).isNotNull(); JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
assertThat(jdbcTemplate.getDataSource()).isNotNull(); assertThat(jdbcTemplate.getDataSource()).isNotNull();
assertThat(jdbcTemplate.getFetchSize()).isEqualTo(100); assertThat(jdbcTemplate.getFetchSize()).isEqualTo(100);
assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(60); assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(60);
assertThat(jdbcTemplate.getMaxRows()).isEqualTo(1000); assertThat(jdbcTemplate.getMaxRows()).isEqualTo(1000);
});
} }
@Test @Test
public void testJdbcTemplateExistsWithCustomDataSource() { public void testJdbcTemplateExistsWithCustomDataSource() {
load(TestDataSourceConfiguration.class); this.contextRunner.withUserConfiguration(TestDataSourceConfiguration.class)
assertThat(this.context.getBeansOfType(JdbcOperations.class)).hasSize(1); .run((context) -> {
JdbcTemplate jdbcTemplate = this.context.getBean(JdbcTemplate.class); assertThat(context).hasSingleBean(JdbcOperations.class);
assertThat(jdbcTemplate).isNotNull(); JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
assertThat(jdbcTemplate.getDataSource()) assertThat(jdbcTemplate.getDataSource())
.isEqualTo(this.context.getBean("customDataSource")); .isEqualTo(context.getBean("customDataSource"));
});
} }
@Test @Test
public void testNamedParameterJdbcTemplateExists() { public void testNamedParameterJdbcTemplateExists() {
load(); this.contextRunner.run((context) -> {
assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) assertThat(context).hasSingleBean(NamedParameterJdbcOperations.class);
.hasSize(1); NamedParameterJdbcTemplate namedParameterJdbcTemplate = context
NamedParameterJdbcTemplate namedParameterJdbcTemplate = this.context
.getBean(NamedParameterJdbcTemplate.class); .getBean(NamedParameterJdbcTemplate.class);
assertThat(namedParameterJdbcTemplate.getJdbcOperations()) assertThat(namedParameterJdbcTemplate.getJdbcOperations())
.isEqualTo(this.context.getBean(JdbcOperations.class)); .isEqualTo(context.getBean(JdbcOperations.class));
});
} }
@Test @Test
public void testMultiDataSource() { public void testMultiDataSource() {
load(MultiDataSourceConfiguration.class); this.contextRunner.withUserConfiguration(MultiDataSourceConfiguration.class)
assertThat(this.context.getBeansOfType(JdbcOperations.class)).isEmpty(); .run((context) -> {
assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) assertThat(context).doesNotHaveBean(JdbcOperations.class);
.isEmpty(); assertThat(context)
.doesNotHaveBean(NamedParameterJdbcOperations.class);
});
} }
@Test @Test
public void testMultiJdbcTemplate() { public void testMultiJdbcTemplate() {
load(MultiJdbcTemplateConfiguration.class); this.contextRunner.withUserConfiguration(MultiJdbcTemplateConfiguration.class)
assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) .run((context) -> assertThat(context)
.isEmpty(); .doesNotHaveBean(NamedParameterJdbcOperations.class));
} }
@Test @Test
public void testMultiDataSourceUsingPrimary() { public void testMultiDataSourceUsingPrimary() {
load(MultiDataSourceUsingPrimaryConfiguration.class); this.contextRunner
assertThat(this.context.getBeansOfType(JdbcOperations.class)).hasSize(1); .withUserConfiguration(MultiDataSourceUsingPrimaryConfiguration.class)
assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) .run((context) -> {
.hasSize(1); assertThat(context).hasSingleBean(JdbcOperations.class);
assertThat(this.context.getBean(JdbcTemplate.class).getDataSource()) assertThat(context).hasSingleBean(NamedParameterJdbcOperations.class);
.isEqualTo(this.context.getBean("test1DataSource")); assertThat(context.getBean(JdbcTemplate.class).getDataSource())
.isEqualTo(context.getBean("test1DataSource"));
});
} }
@Test @Test
public void testMultiJdbcTemplateUsingPrimary() { public void testMultiJdbcTemplateUsingPrimary() {
load(MultiJdbcTemplateUsingPrimaryConfiguration.class); this.contextRunner
assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) .withUserConfiguration(MultiJdbcTemplateUsingPrimaryConfiguration.class)
.hasSize(1); .run((context) -> {
assertThat(this.context.getBean(NamedParameterJdbcTemplate.class) assertThat(context).hasSingleBean(NamedParameterJdbcOperations.class);
.getJdbcOperations()).isEqualTo(this.context.getBean("test1Template")); assertThat(context.getBean(NamedParameterJdbcTemplate.class)
.getJdbcOperations())
.isEqualTo(context.getBean("test1Template"));
});
} }
@Test @Test
public void testExistingCustomJdbcTemplate() { public void testExistingCustomJdbcTemplate() {
load(CustomConfiguration.class); this.contextRunner.withUserConfiguration(CustomConfiguration.class)
assertThat(this.context.getBeansOfType(JdbcOperations.class)).hasSize(1); .run((context) -> {
assertThat(this.context.getBean(JdbcOperations.class)) assertThat(context).hasSingleBean(JdbcOperations.class);
.isEqualTo(this.context.getBean("customJdbcOperations")); assertThat(context.getBean(JdbcOperations.class))
.isEqualTo(context.getBean("customJdbcOperations"));
});
} }
@Test @Test
public void testExistingCustomNamedParameterJdbcTemplate() { public void testExistingCustomNamedParameterJdbcTemplate() {
load(CustomConfiguration.class); this.contextRunner.withUserConfiguration(CustomConfiguration.class)
assertThat(this.context.getBeansOfType(NamedParameterJdbcOperations.class)) .run((context) -> {
.hasSize(1); assertThat(context).hasSingleBean(NamedParameterJdbcOperations.class);
assertThat(this.context.getBean(NamedParameterJdbcOperations.class)) assertThat(context.getBean(NamedParameterJdbcOperations.class))
.isEqualTo(this.context.getBean("customNamedParameterJdbcOperations")); .isEqualTo(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.initialization-mode:never",
"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

Loading…
Cancel
Save