30
30
import com .zaxxer .hikari .HikariDataSource ;
31
31
import org .apache .commons .dbcp2 .BasicDataSource ;
32
32
import org .junit .After ;
33
- import org .junit .Before ;
33
+ import org .junit .Rule ;
34
34
import org .junit .Test ;
35
+ import org .junit .rules .ExpectedException ;
35
36
36
37
import org .springframework .beans .factory .BeanCreationException ;
37
- import org .springframework .boot .autoconfigure .context .PropertyPlaceholderAutoConfiguration ;
38
38
import org .springframework .boot .jdbc .DatabaseDriver ;
39
39
import org .springframework .boot .test .util .TestPropertyValues ;
40
+ import org .springframework .context .ConfigurableApplicationContext ;
40
41
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
41
42
import org .springframework .context .annotation .Bean ;
42
43
import org .springframework .context .annotation .Configuration ;
43
44
import org .springframework .jdbc .datasource .SimpleDriverDataSource ;
44
45
45
46
import static org .assertj .core .api .Assertions .assertThat ;
47
+ import static org .mockito .ArgumentMatchers .contains ;
46
48
import static org .mockito .Mockito .mock ;
47
49
48
50
/**
53
55
*/
54
56
public class DataSourceAutoConfigurationTests {
55
57
56
- private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
58
+ @ Rule
59
+ public final ExpectedException thrown = ExpectedException .none ();
57
60
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 ;
65
62
66
63
@ 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
+ }
70
68
}
71
69
72
70
@ Test
73
71
public void testDefaultDataSourceExists () throws Exception {
74
- this .context .register (DataSourceAutoConfiguration .class ,
75
- PropertyPlaceholderAutoConfiguration .class );
76
- this .context .refresh ();
72
+ load ();
77
73
assertThat (this .context .getBean (DataSource .class )).isNotNull ();
78
74
}
79
75
80
76
@ Test
81
77
public void testDataSourceHasEmbeddedDefault () throws Exception {
82
- this .context .register (DataSourceAutoConfiguration .class ,
83
- PropertyPlaceholderAutoConfiguration .class );
84
- this .context .refresh ();
78
+ load ();
85
79
HikariDataSource dataSource = this .context .getBean (HikariDataSource .class );
86
80
assertThat (dataSource .getJdbcUrl ()).isNotNull ();
87
81
assertThat (dataSource .getDriverClassName ()).isNotNull ();
88
82
}
89
83
90
- @ Test ( expected = BeanCreationException . class )
84
+ @ Test
91
85
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
+ }
99
94
}
100
95
101
- @ Test ( expected = BeanCreationException . class )
96
+ @ Test
102
97
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" );
112
101
}
113
102
114
103
@ Test
@@ -123,7 +112,7 @@ public void hikariValidatesConnectionByDefault() throws Exception {
123
112
public void tomcatIsFallback () throws Exception {
124
113
org .apache .tomcat .jdbc .pool .DataSource dataSource = autoConfigureDataSource (
125
114
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" );
127
116
}
128
117
129
118
@ Test
@@ -139,7 +128,7 @@ public void tomcatValidatesConnectionByDefault() {
139
128
public void commonsDbcp2IsFallback () throws Exception {
140
129
BasicDataSource dataSource = autoConfigureDataSource (BasicDataSource .class ,
141
130
"com.zaxxer.hikari" , "org.apache.tomcat" );
142
- assertThat (dataSource .getUrl ()).isEqualTo ("jdbc:hsqldb:mem:testdb" );
131
+ assertThat (dataSource .getUrl ()).startsWith ("jdbc:hsqldb:mem:testdb" );
143
132
}
144
133
145
134
@ Test
@@ -153,13 +142,8 @@ public void commonsDbcp2ValidatesConnectionByDefault() throws Exception {
153
142
154
143
@ Test
155
144
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" );
163
147
DataSource bean = this .context .getBean (DataSource .class );
164
148
assertThat (bean ).isNotNull ();
165
149
@ SuppressWarnings ("resource" )
@@ -174,33 +158,25 @@ public void testEmbeddedTypeDefaultsUsername() throws Exception {
174
158
*/
175
159
@ Test
176
160
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 ());
186
167
testExplicitType ();
187
168
}
188
169
189
170
@ Test
190
171
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 ());
197
176
testExplicitType ();
198
177
}
199
178
200
179
private void testExplicitType () {
201
- this .context .register (DataSourceAutoConfiguration .class ,
202
- PropertyPlaceholderAutoConfiguration .class );
203
- this .context .refresh ();
204
180
assertThat (this .context .getBeansOfType (DataSource .class )).hasSize (1 );
205
181
DataSource bean = this .context .getBean (DataSource .class );
206
182
assertThat (bean ).isNotNull ();
@@ -209,12 +185,8 @@ private void testExplicitType() {
209
185
210
186
@ Test
211
187
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" );
218
190
DataSource dataSource = this .context .getBean (DataSource .class );
219
191
assertThat (dataSource ).isNotNull ();
220
192
assertThat (((HikariDataSource ) dataSource ).getDriverClassName ())
@@ -224,30 +196,46 @@ public void testExplicitDriverClassClearsUsername() throws Exception {
224
196
225
197
@ Test
226
198
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 );
231
200
DataSource dataSource = this .context .getBean (DataSource .class );
232
201
assertThat (dataSource ).isInstanceOf (BasicDataSource .class );
233
202
}
234
203
235
204
@ SuppressWarnings ("unchecked" )
236
205
private <T extends DataSource > T autoConfigureDataSource (Class <T > expectedType ,
237
206
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 ));
246
208
DataSource bean = this .context .getBean (DataSource .class );
247
209
assertThat (bean ).isInstanceOf (expectedType );
248
210
return (T ) bean ;
249
211
}
250
212
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
+
251
239
@ Configuration
252
240
static class TestDataSourceConfiguration {
253
241
0 commit comments