|
35 | 35 | import org.springframework.context.annotation.Scope; |
36 | 36 | import org.springframework.context.annotation.ScopedProxyMode; |
37 | 37 | import org.springframework.context.support.GenericApplicationContext; |
| 38 | +import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; |
38 | 39 |
|
39 | 40 | import javax.sql.DataSource; |
40 | 41 | import java.io.PrintWriter; |
41 | 42 | import java.sql.Connection; |
42 | 43 | import java.util.List; |
| 44 | +import java.util.HashMap; |
| 45 | +import java.util.Map; |
43 | 46 | import java.util.concurrent.ThreadLocalRandom; |
44 | 47 | import java.util.logging.Logger; |
45 | 48 |
|
@@ -226,6 +229,30 @@ void testDecorateDynamicallyRegisteredBeans() { |
226 | 229 | assertThat(dataSource2).isInstanceOf(DecoratedDataSource.class); |
227 | 230 | }); |
228 | 231 | } |
| 232 | + |
| 233 | + @Test |
| 234 | + void testRoutingDataSourceIsDecorated() { |
| 235 | + ApplicationContextRunner contextRunner = this.contextRunner.withUserConfiguration(TestAbstractRoutingDataSourceConfiguration.class); |
| 236 | + |
| 237 | + contextRunner.run(context -> { |
| 238 | + DataSource dataSource = context.getBean(DataSource.class); |
| 239 | + assertThat(dataSource).isInstanceOf(DecoratedDataSource.class); |
| 240 | + DataSource realDataSource = ((DecoratedDataSource) dataSource).getRealDataSource(); |
| 241 | + assertThat(realDataSource).isInstanceOf(AbstractRoutingDataSource.class); |
| 242 | + }); |
| 243 | + } |
| 244 | + |
| 245 | + @Test |
| 246 | + void testRoutingDataSourceIsNotDecorated() { |
| 247 | + ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("decorator.datasource.ignore-routing-data-sources=true") |
| 248 | + .withUserConfiguration(TestAbstractRoutingDataSourceConfiguration.class); |
| 249 | + |
| 250 | + contextRunner.run(context -> { |
| 251 | + DataSource dataSource = context.getBean(DataSource.class); |
| 252 | + assertThat(dataSource).isNotInstanceOf(DecoratedDataSource.class); |
| 253 | + assertThat(dataSource).isInstanceOf(AbstractRoutingDataSource.class); |
| 254 | + }); |
| 255 | + } |
229 | 256 |
|
230 | 257 | private AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> assertThatDataSourceDecoratingChain(DataSource dataSource) { |
231 | 258 | return assertThat(((DecoratedDataSource) dataSource).getDecoratingChain()).extracting("dataSource").extracting("class"); |
@@ -289,6 +316,29 @@ public DataSource dataSource() { |
289 | 316 | return pool; |
290 | 317 | } |
291 | 318 | } |
| 319 | + |
| 320 | + @Configuration(proxyBeanMethods = false) |
| 321 | + static class TestAbstractRoutingDataSourceConfiguration { |
| 322 | + |
| 323 | + @Bean |
| 324 | + public DataSource dataSource() { |
| 325 | + AbstractRoutingDataSource routingDs = new AbstractRoutingDataSource() { |
| 326 | + @Override |
| 327 | + protected Object determineCurrentLookupKey() { |
| 328 | + return "ds1"; |
| 329 | + } |
| 330 | + }; |
| 331 | + BasicDataSource pool = new BasicDataSource(); |
| 332 | + pool.setDriverClassName("org.hsqldb.jdbcDriver"); |
| 333 | + pool.setUrl("jdbc:hsqldb:target/routingds"); |
| 334 | + pool.setUsername("sa"); |
| 335 | + Map<Object, Object> targetDataSources = new HashMap<>(); |
| 336 | + targetDataSources.put("ds1", pool); |
| 337 | + routingDs.setTargetDataSources(targetDataSources); |
| 338 | + routingDs.setDefaultTargetDataSource(pool); |
| 339 | + return routingDs; |
| 340 | + } |
| 341 | + } |
292 | 342 |
|
293 | 343 | /** |
294 | 344 | * Custom proxy data source for tests. |
|
0 commit comments