Polish contribution
See gh-31248
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -61,7 +61,7 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple
|
||||
|
||||
/**
|
||||
* Specify the map of target DataSources, with the lookup key as key.
|
||||
* The mapped value can either be a corresponding {@link javax.sql.DataSource}
|
||||
* <p>The mapped value can either be a corresponding {@link javax.sql.DataSource}
|
||||
* instance or a data source name String (to be resolved via a
|
||||
* {@link #setDataSourceLookup DataSourceLookup}).
|
||||
* <p>The key can be of arbitrary type; this class implements the
|
||||
@@ -114,15 +114,23 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delegates to {@link #initialize()}.
|
||||
*/
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronizes targetDataSources to resolvedDataSources
|
||||
* and defaultTargetDataSource to resolvedDefaultDataSource.
|
||||
* @throws IllegalArgumentException in case of targetDataSources is null
|
||||
* Initialize the internal state of this {@code AbstractRoutingDataSource}
|
||||
* by resolving the configured target DataSources.
|
||||
* @throws IllegalArgumentException if the target DataSources have not been configured
|
||||
* @since 6.1
|
||||
* @see #setTargetDataSources(Map)
|
||||
* @see #setDefaultTargetDataSource(Object)
|
||||
* @see #getResolvedDataSources()
|
||||
* @see #getResolvedDefaultDataSource()
|
||||
*/
|
||||
public void initialize() {
|
||||
if (this.targetDataSources == null) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.jdbc.datasource.lookup;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@@ -28,7 +27,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for {@link AbstractRoutingDataSource}.
|
||||
*
|
||||
@@ -38,7 +36,7 @@ class AbstractRoutingDataSourceTests {
|
||||
|
||||
@Test
|
||||
void setTargetDataSources() {
|
||||
final ThreadLocal<String> lookupKey = new ThreadLocal<>();
|
||||
ThreadLocal<String> lookupKey = new ThreadLocal<>();
|
||||
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
@@ -50,14 +48,11 @@ class AbstractRoutingDataSourceTests {
|
||||
|
||||
MapDataSourceLookup dataSourceLookup = new MapDataSourceLookup();
|
||||
dataSourceLookup.addDataSource("dataSource2", ds2);
|
||||
|
||||
routingDataSource.setDataSourceLookup(dataSourceLookup);
|
||||
|
||||
Map<Object, Object> targetDataSources = new HashMap<>();
|
||||
targetDataSources.put("ds1", ds1);
|
||||
targetDataSources.put("ds2", "dataSource2");
|
||||
routingDataSource.setTargetDataSources(targetDataSources);
|
||||
|
||||
routingDataSource.setTargetDataSources(Map.of("ds1", ds1, "ds2", "dataSource2"));
|
||||
routingDataSource.afterPropertiesSet();
|
||||
|
||||
lookupKey.set("ds1");
|
||||
assertThat(routingDataSource.determineTargetDataSource()).isSameAs(ds1);
|
||||
lookupKey.set("ds2");
|
||||
@@ -84,9 +79,7 @@ class AbstractRoutingDataSourceTests {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
Map<Object, Object> targetDataSources = new HashMap<>();
|
||||
targetDataSources.put("ds1", 1);
|
||||
routingDataSource.setTargetDataSources(targetDataSources);
|
||||
routingDataSource.setTargetDataSources(Map.of("ds1", 1));
|
||||
assertThatIllegalArgumentException().isThrownBy(routingDataSource::afterPropertiesSet)
|
||||
.withMessage("Illegal data source value - only [javax.sql.DataSource] and String supported: 1");
|
||||
}
|
||||
@@ -94,7 +87,7 @@ class AbstractRoutingDataSourceTests {
|
||||
|
||||
@Test
|
||||
void setDefaultTargetDataSource() {
|
||||
final ThreadLocal<String> lookupKey = new ThreadLocal<>();
|
||||
ThreadLocal<String> lookupKey = new ThreadLocal<>();
|
||||
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
@@ -102,7 +95,7 @@ class AbstractRoutingDataSourceTests {
|
||||
}
|
||||
};
|
||||
DataSource ds = new StubDataSource();
|
||||
routingDataSource.setTargetDataSources(new HashMap<>());
|
||||
routingDataSource.setTargetDataSources(Map.of());
|
||||
routingDataSource.setDefaultTargetDataSource(ds);
|
||||
routingDataSource.afterPropertiesSet();
|
||||
lookupKey.set("foo");
|
||||
@@ -111,7 +104,7 @@ class AbstractRoutingDataSourceTests {
|
||||
|
||||
@Test
|
||||
void setDefaultTargetDataSourceFallbackIsFalse() {
|
||||
final ThreadLocal<String> lookupKey = new ThreadLocal<>();
|
||||
ThreadLocal<String> lookupKey = new ThreadLocal<>();
|
||||
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
@@ -119,7 +112,7 @@ class AbstractRoutingDataSourceTests {
|
||||
}
|
||||
};
|
||||
DataSource ds = new StubDataSource();
|
||||
routingDataSource.setTargetDataSources(new HashMap<>());
|
||||
routingDataSource.setTargetDataSources(Map.of());
|
||||
routingDataSource.setDefaultTargetDataSource(ds);
|
||||
routingDataSource.setLenientFallback(false);
|
||||
routingDataSource.afterPropertiesSet();
|
||||
@@ -130,7 +123,7 @@ class AbstractRoutingDataSourceTests {
|
||||
|
||||
@Test
|
||||
void setDefaultTargetDataSourceLookupKeyIsNullWhenFallbackIsFalse() {
|
||||
final ThreadLocal<String> lookupKey = new ThreadLocal<>();
|
||||
ThreadLocal<String> lookupKey = new ThreadLocal<>();
|
||||
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
@@ -138,7 +131,7 @@ class AbstractRoutingDataSourceTests {
|
||||
}
|
||||
};
|
||||
DataSource ds = new StubDataSource();
|
||||
routingDataSource.setTargetDataSources(new HashMap<>());
|
||||
routingDataSource.setTargetDataSources(Map.of());
|
||||
routingDataSource.setDefaultTargetDataSource(ds);
|
||||
routingDataSource.setLenientFallback(false);
|
||||
routingDataSource.afterPropertiesSet();
|
||||
@@ -147,7 +140,7 @@ class AbstractRoutingDataSourceTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInitialize_synchronizeTargetDataSourcesToResolvedDataSources() {
|
||||
void initializeSynchronizesTargetDataSourcesToResolvedDataSources() {
|
||||
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
@@ -158,11 +151,7 @@ class AbstractRoutingDataSourceTests {
|
||||
DataSource ds1 = new StubDataSource();
|
||||
DataSource ds2 = new StubDataSource();
|
||||
|
||||
Map<Object, Object> targetDataSources = new HashMap<>();
|
||||
targetDataSources.put("ds1", ds1);
|
||||
targetDataSources.put("ds2", ds2);
|
||||
routingDataSource.setTargetDataSources(targetDataSources);
|
||||
|
||||
routingDataSource.setTargetDataSources(Map.of("ds1", ds1, "ds2", ds2));
|
||||
routingDataSource.initialize();
|
||||
|
||||
Map<Object, DataSource> resolvedDataSources = routingDataSource.getResolvedDataSources();
|
||||
|
||||
Reference in New Issue
Block a user