Skip to content

Commit 1ba0df9

Browse files
committed
Polish
1 parent acaef9c commit 1ba0df9

File tree

1 file changed

+70
-82
lines changed

1 file changed

+70
-82
lines changed

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java

Lines changed: 70 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@
3030
import com.zaxxer.hikari.HikariDataSource;
3131
import org.apache.commons.dbcp2.BasicDataSource;
3232
import org.junit.After;
33-
import org.junit.Before;
33+
import org.junit.Rule;
3434
import org.junit.Test;
35+
import org.junit.rules.ExpectedException;
3536

3637
import org.springframework.beans.factory.BeanCreationException;
37-
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
3838
import org.springframework.boot.jdbc.DatabaseDriver;
3939
import org.springframework.boot.test.util.TestPropertyValues;
40+
import org.springframework.context.ConfigurableApplicationContext;
4041
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4142
import org.springframework.context.annotation.Bean;
4243
import org.springframework.context.annotation.Configuration;
4344
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
4445

4546
import static org.assertj.core.api.Assertions.assertThat;
47+
import static org.mockito.ArgumentMatchers.contains;
4648
import static org.mockito.Mockito.mock;
4749

4850
/**
@@ -53,62 +55,49 @@
5355
*/
5456
public class DataSourceAutoConfigurationTests {
5557

56-
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
58+
@Rule
59+
public final ExpectedException thrown = ExpectedException.none();
5760

58-
@Before
59-
public void init() {
60-
EmbeddedDatabaseConnection.override = null;
61-
TestPropertyValues.of("spring.datasource.initialize:false",
62-
"spring.datasource.url:jdbc:hsqldb:mem:testdb-" + new Random().nextInt())
63-
.applyTo(this.context);
64-
}
61+
private ConfigurableApplicationContext context;
6562

6663
@After
67-
public void restore() {
68-
EmbeddedDatabaseConnection.override = null;
69-
this.context.close();
64+
public void close() {
65+
if (this.context != null) {
66+
this.context.close();
67+
}
7068
}
7169

7270
@Test
7371
public void testDefaultDataSourceExists() throws Exception {
74-
this.context.register(DataSourceAutoConfiguration.class,
75-
PropertyPlaceholderAutoConfiguration.class);
76-
this.context.refresh();
72+
load();
7773
assertThat(this.context.getBean(DataSource.class)).isNotNull();
7874
}
7975

8076
@Test
8177
public void testDataSourceHasEmbeddedDefault() throws Exception {
82-
this.context.register(DataSourceAutoConfiguration.class,
83-
PropertyPlaceholderAutoConfiguration.class);
84-
this.context.refresh();
78+
load();
8579
HikariDataSource dataSource = this.context.getBean(HikariDataSource.class);
8680
assertThat(dataSource.getJdbcUrl()).isNotNull();
8781
assertThat(dataSource.getDriverClassName()).isNotNull();
8882
}
8983

90-
@Test(expected = BeanCreationException.class)
84+
@Test
9185
public void testBadUrl() throws Exception {
92-
TestPropertyValues.of("spring.datasource.url:jdbc:not-going-to-work")
93-
.applyTo(this.context);
94-
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
95-
this.context.register(DataSourceAutoConfiguration.class,
96-
PropertyPlaceholderAutoConfiguration.class);
97-
this.context.refresh();
98-
assertThat(this.context.getBean(DataSource.class)).isNotNull();
86+
try {
87+
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
88+
this.thrown.expect(BeanCreationException.class);
89+
load("spring.datasource.url:jdbc:not-going-to-work");
90+
}
91+
finally {
92+
EmbeddedDatabaseConnection.override = null;
93+
}
9994
}
10095

101-
@Test(expected = BeanCreationException.class)
96+
@Test
10297
public void testBadDriverClass() throws Exception {
103-
TestPropertyValues
104-
.of("spring.datasource.driverClassName:org.none.jdbcDriver",
105-
"spring.datasource.url:jdbc:hsqldb:mem:testdb")
106-
.applyTo(this.context);
107-
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
108-
this.context.register(DataSourceAutoConfiguration.class,
109-
PropertyPlaceholderAutoConfiguration.class);
110-
this.context.refresh();
111-
assertThat(this.context.getBean(DataSource.class)).isNotNull();
98+
this.thrown.expect(BeanCreationException.class);
99+
this.thrown.expectMessage(contains("org.none.jdbcDriver"));
100+
load("spring.datasource.driverClassName:org.none.jdbcDriver");
112101
}
113102

114103
@Test
@@ -123,7 +112,7 @@ public void hikariValidatesConnectionByDefault() throws Exception {
123112
public void tomcatIsFallback() throws Exception {
124113
org.apache.tomcat.jdbc.pool.DataSource dataSource = autoConfigureDataSource(
125114
org.apache.tomcat.jdbc.pool.DataSource.class, "com.zaxxer.hikari");
126-
assertThat(dataSource.getUrl()).isEqualTo("jdbc:hsqldb:mem:testdb");
115+
assertThat(dataSource.getUrl()).startsWith("jdbc:hsqldb:mem:testdb");
127116
}
128117

129118
@Test
@@ -139,7 +128,7 @@ public void tomcatValidatesConnectionByDefault() {
139128
public void commonsDbcp2IsFallback() throws Exception {
140129
BasicDataSource dataSource = autoConfigureDataSource(BasicDataSource.class,
141130
"com.zaxxer.hikari", "org.apache.tomcat");
142-
assertThat(dataSource.getUrl()).isEqualTo("jdbc:hsqldb:mem:testdb");
131+
assertThat(dataSource.getUrl()).startsWith("jdbc:hsqldb:mem:testdb");
143132
}
144133

145134
@Test
@@ -153,13 +142,8 @@ public void commonsDbcp2ValidatesConnectionByDefault() throws Exception {
153142

154143
@Test
155144
public void testEmbeddedTypeDefaultsUsername() throws Exception {
156-
TestPropertyValues
157-
.of("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
158-
"spring.datasource.url:jdbc:hsqldb:mem:testdb")
159-
.applyTo(this.context);
160-
this.context.register(DataSourceAutoConfiguration.class,
161-
PropertyPlaceholderAutoConfiguration.class);
162-
this.context.refresh();
145+
load("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
146+
"spring.datasource.url:jdbc:hsqldb:mem:testdb");
163147
DataSource bean = this.context.getBean(DataSource.class);
164148
assertThat(bean).isNotNull();
165149
@SuppressWarnings("resource")
@@ -174,33 +158,25 @@ public void testEmbeddedTypeDefaultsUsername() throws Exception {
174158
*/
175159
@Test
176160
public void explicitTypeNoSupportedDataSource() {
177-
TestPropertyValues
178-
.of("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
179-
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
180-
"spring.datasource.type:"
181-
+ SimpleDriverDataSource.class.getName())
182-
.applyTo(this.context);
183-
this.context.setClassLoader(
184-
new HidePackagesClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
185-
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"));
161+
load(null, new HidePackagesClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
162+
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"),
163+
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
164+
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
165+
"spring.datasource.type:"
166+
+ SimpleDriverDataSource.class.getName());
186167
testExplicitType();
187168
}
188169

189170
@Test
190171
public void explicitTypeSupportedDataSource() {
191-
TestPropertyValues
192-
.of("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
193-
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
194-
"spring.datasource.type:"
195-
+ SimpleDriverDataSource.class.getName())
196-
.applyTo(this.context);
172+
load("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
173+
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
174+
"spring.datasource.type:"
175+
+ SimpleDriverDataSource.class.getName());
197176
testExplicitType();
198177
}
199178

200179
private void testExplicitType() {
201-
this.context.register(DataSourceAutoConfiguration.class,
202-
PropertyPlaceholderAutoConfiguration.class);
203-
this.context.refresh();
204180
assertThat(this.context.getBeansOfType(DataSource.class)).hasSize(1);
205181
DataSource bean = this.context.getBean(DataSource.class);
206182
assertThat(bean).isNotNull();
@@ -209,12 +185,8 @@ private void testExplicitType() {
209185

210186
@Test
211187
public void testExplicitDriverClassClearsUsername() throws Exception {
212-
TestPropertyValues.of(
213-
"spring.datasource.driverClassName:" + DatabaseTestDriver.class.getName(),
214-
"spring.datasource.url:jdbc:foo://localhost").applyTo(this.context);
215-
this.context.register(DataSourceAutoConfiguration.class,
216-
PropertyPlaceholderAutoConfiguration.class);
217-
this.context.refresh();
188+
load("spring.datasource.driverClassName:" + DatabaseTestDriver.class.getName(),
189+
"spring.datasource.url:jdbc:foo://localhost");
218190
DataSource dataSource = this.context.getBean(DataSource.class);
219191
assertThat(dataSource).isNotNull();
220192
assertThat(((HikariDataSource) dataSource).getDriverClassName())
@@ -224,30 +196,46 @@ public void testExplicitDriverClassClearsUsername() throws Exception {
224196

225197
@Test
226198
public void testDefaultDataSourceCanBeOverridden() throws Exception {
227-
this.context.register(TestDataSourceConfiguration.class,
228-
DataSourceAutoConfiguration.class,
229-
PropertyPlaceholderAutoConfiguration.class);
230-
this.context.refresh();
199+
load(TestDataSourceConfiguration.class);
231200
DataSource dataSource = this.context.getBean(DataSource.class);
232201
assertThat(dataSource).isInstanceOf(BasicDataSource.class);
233202
}
234203

235204
@SuppressWarnings("unchecked")
236205
private <T extends DataSource> T autoConfigureDataSource(Class<T> expectedType,
237206
final String... hiddenPackages) {
238-
TestPropertyValues
239-
.of("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
240-
"spring.datasource.url:jdbc:hsqldb:mem:testdb")
241-
.applyTo(this.context);
242-
this.context.setClassLoader(new HidePackagesClassLoader(hiddenPackages));
243-
this.context.register(DataSourceAutoConfiguration.class,
244-
PropertyPlaceholderAutoConfiguration.class);
245-
this.context.refresh();
207+
load(null, new HidePackagesClassLoader(hiddenPackages));
246208
DataSource bean = this.context.getBean(DataSource.class);
247209
assertThat(bean).isInstanceOf(expectedType);
248210
return (T) bean;
249211
}
250212

213+
public void load(String... environment) {
214+
load(null, environment);
215+
}
216+
217+
public void load(Class<?> config, String... environment) {
218+
load(config, null, environment);
219+
}
220+
221+
public void load(Class<?> config, ClassLoader classLoader, String... environment) {
222+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
223+
if (classLoader != null) {
224+
ctx.setClassLoader(classLoader);
225+
}
226+
TestPropertyValues
227+
.of("spring.datasource.initialize=false",
228+
"spring.datasource.url:jdbc:hsqldb:mem:testdb-" + new Random().nextInt())
229+
.applyTo(ctx);
230+
TestPropertyValues.of(environment).applyTo(ctx);
231+
if (config != null) {
232+
ctx.register(config);
233+
}
234+
ctx.register(DataSourceAutoConfiguration.class);
235+
ctx.refresh();
236+
this.context = ctx;
237+
}
238+
251239
@Configuration
252240
static class TestDataSourceConfiguration {
253241

0 commit comments

Comments
 (0)