Skip to content

Commit d50ec68

Browse files
committed
Polish contribution
See gh-31248
1 parent 6d2d8a3 commit d50ec68

File tree

4 files changed

+69
-70
lines changed

4 files changed

+69
-70
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,7 +61,7 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple
6161

6262
/**
6363
* Specify the map of target DataSources, with the lookup key as key.
64-
* The mapped value can either be a corresponding {@link javax.sql.DataSource}
64+
* <p>The mapped value can either be a corresponding {@link javax.sql.DataSource}
6565
* instance or a data source name String (to be resolved via a
6666
* {@link #setDataSourceLookup DataSourceLookup}).
6767
* <p>The key can be of arbitrary type; this class implements the
@@ -114,15 +114,23 @@ public void setDataSourceLookup(@Nullable DataSourceLookup dataSourceLookup) {
114114
}
115115

116116

117+
/**
118+
* Delegates to {@link #initialize()}.
119+
*/
117120
@Override
118121
public void afterPropertiesSet() {
119122
initialize();
120123
}
121124

122125
/**
123-
* Synchronizes targetDataSources to resolvedDataSources
124-
* and defaultTargetDataSource to resolvedDefaultDataSource.
125-
* @throws IllegalArgumentException in case of targetDataSources is null
126+
* Initialize the internal state of this {@code AbstractRoutingDataSource}
127+
* by resolving the configured target DataSources.
128+
* @throws IllegalArgumentException if the target DataSources have not been configured
129+
* @since 6.1
130+
* @see #setTargetDataSources(Map)
131+
* @see #setDefaultTargetDataSource(Object)
132+
* @see #getResolvedDataSources()
133+
* @see #getResolvedDefaultDataSource()
126134
*/
127135
public void initialize() {
128136
if (this.targetDataSources == null) {

spring-jdbc/src/test/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSourceTests.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.jdbc.datasource.lookup;
1818

1919

20-
import java.util.HashMap;
2120
import java.util.Map;
2221

2322
import javax.sql.DataSource;
@@ -28,7 +27,6 @@
2827
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2928
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3029

31-
3230
/**
3331
* Tests for {@link AbstractRoutingDataSource}.
3432
*
@@ -38,7 +36,7 @@ class AbstractRoutingDataSourceTests {
3836

3937
@Test
4038
void setTargetDataSources() {
41-
final ThreadLocal<String> lookupKey = new ThreadLocal<>();
39+
ThreadLocal<String> lookupKey = new ThreadLocal<>();
4240
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
4341
@Override
4442
protected Object determineCurrentLookupKey() {
@@ -50,14 +48,11 @@ protected Object determineCurrentLookupKey() {
5048

5149
MapDataSourceLookup dataSourceLookup = new MapDataSourceLookup();
5250
dataSourceLookup.addDataSource("dataSource2", ds2);
53-
routingDataSource.setDataSourceLookup(dataSourceLookup);
54-
55-
Map<Object, Object> targetDataSources = new HashMap<>();
56-
targetDataSources.put("ds1", ds1);
57-
targetDataSources.put("ds2", "dataSource2");
58-
routingDataSource.setTargetDataSources(targetDataSources);
5951

52+
routingDataSource.setDataSourceLookup(dataSourceLookup);
53+
routingDataSource.setTargetDataSources(Map.of("ds1", ds1, "ds2", "dataSource2"));
6054
routingDataSource.afterPropertiesSet();
55+
6156
lookupKey.set("ds1");
6257
assertThat(routingDataSource.determineTargetDataSource()).isSameAs(ds1);
6358
lookupKey.set("ds2");
@@ -84,25 +79,23 @@ protected Object determineCurrentLookupKey() {
8479
return null;
8580
}
8681
};
87-
Map<Object, Object> targetDataSources = new HashMap<>();
88-
targetDataSources.put("ds1", 1);
89-
routingDataSource.setTargetDataSources(targetDataSources);
82+
routingDataSource.setTargetDataSources(Map.of("ds1", 1));
9083
assertThatIllegalArgumentException().isThrownBy(routingDataSource::afterPropertiesSet)
9184
.withMessage("Illegal data source value - only [javax.sql.DataSource] and String supported: 1");
9285
}
9386

9487

9588
@Test
9689
void setDefaultTargetDataSource() {
97-
final ThreadLocal<String> lookupKey = new ThreadLocal<>();
90+
ThreadLocal<String> lookupKey = new ThreadLocal<>();
9891
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
9992
@Override
10093
protected Object determineCurrentLookupKey() {
10194
return lookupKey.get();
10295
}
10396
};
10497
DataSource ds = new StubDataSource();
105-
routingDataSource.setTargetDataSources(new HashMap<>());
98+
routingDataSource.setTargetDataSources(Map.of());
10699
routingDataSource.setDefaultTargetDataSource(ds);
107100
routingDataSource.afterPropertiesSet();
108101
lookupKey.set("foo");
@@ -111,15 +104,15 @@ protected Object determineCurrentLookupKey() {
111104

112105
@Test
113106
void setDefaultTargetDataSourceFallbackIsFalse() {
114-
final ThreadLocal<String> lookupKey = new ThreadLocal<>();
107+
ThreadLocal<String> lookupKey = new ThreadLocal<>();
115108
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
116109
@Override
117110
protected Object determineCurrentLookupKey() {
118111
return lookupKey.get();
119112
}
120113
};
121114
DataSource ds = new StubDataSource();
122-
routingDataSource.setTargetDataSources(new HashMap<>());
115+
routingDataSource.setTargetDataSources(Map.of());
123116
routingDataSource.setDefaultTargetDataSource(ds);
124117
routingDataSource.setLenientFallback(false);
125118
routingDataSource.afterPropertiesSet();
@@ -130,15 +123,15 @@ protected Object determineCurrentLookupKey() {
130123

131124
@Test
132125
void setDefaultTargetDataSourceLookupKeyIsNullWhenFallbackIsFalse() {
133-
final ThreadLocal<String> lookupKey = new ThreadLocal<>();
126+
ThreadLocal<String> lookupKey = new ThreadLocal<>();
134127
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
135128
@Override
136129
protected Object determineCurrentLookupKey() {
137130
return lookupKey.get();
138131
}
139132
};
140133
DataSource ds = new StubDataSource();
141-
routingDataSource.setTargetDataSources(new HashMap<>());
134+
routingDataSource.setTargetDataSources(Map.of());
142135
routingDataSource.setDefaultTargetDataSource(ds);
143136
routingDataSource.setLenientFallback(false);
144137
routingDataSource.afterPropertiesSet();
@@ -147,7 +140,7 @@ protected Object determineCurrentLookupKey() {
147140
}
148141

149142
@Test
150-
void testInitialize_synchronizeTargetDataSourcesToResolvedDataSources() {
143+
void initializeSynchronizesTargetDataSourcesToResolvedDataSources() {
151144
AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() {
152145
@Override
153146
protected Object determineCurrentLookupKey() {
@@ -158,11 +151,7 @@ protected Object determineCurrentLookupKey() {
158151
DataSource ds1 = new StubDataSource();
159152
DataSource ds2 = new StubDataSource();
160153

161-
Map<Object, Object> targetDataSources = new HashMap<>();
162-
targetDataSources.put("ds1", ds1);
163-
targetDataSources.put("ds2", ds2);
164-
routingDataSource.setTargetDataSources(targetDataSources);
165-
154+
routingDataSource.setTargetDataSources(Map.of("ds1", ds1, "ds2", ds2));
166155
routingDataSource.initialize();
167156

168157
Map<Object, DataSource> resolvedDataSources = routingDataSource.getResolvedDataSources();

spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/AbstractRoutingConnectionFactory.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
/**
3232
* Abstract {@link ConnectionFactory} implementation that routes
3333
* {@link #create()} calls to one of various target
34-
* {@link ConnectionFactory factories} based on a lookup key.
34+
* {@linkplain ConnectionFactory factories} based on a lookup key.
3535
* The latter is typically (but not necessarily) determined from some
3636
* subscriber context.
3737
*
38-
* <p> Allows to configure a {@link #setDefaultTargetConnectionFactory(Object)
39-
* default ConnectionFactory} as fallback.
38+
* <p>Allows to configure a default target {@link #setDefaultTargetConnectionFactory(Object)
39+
* ConnectionFactory} as a fallback.
4040
*
41-
* <p> Calls to {@link #getMetadata()} are routed to the
41+
* <p>Calls to {@link #getMetadata()} are routed to the
4242
* {@link #setDefaultTargetConnectionFactory(Object) default ConnectionFactory}
4343
* if configured.
4444
*
@@ -125,14 +125,22 @@ public void setConnectionFactoryLookup(ConnectionFactoryLookup connectionFactory
125125
}
126126

127127

128+
/**
129+
* Delegates to {@link #initialize()}.
130+
*/
128131
@Override
129132
public void afterPropertiesSet() {
130133
initialize();
131134
}
132135

133136
/**
134-
* Synchronizes targetConnectionFactories to resolvedConnectionFactories
135-
* and defaultTargetConnectionFactory to resolvedDefaultConnectionFactory.
137+
* Initialize the internal state of this {@code AbstractRoutingConnectionFactory}
138+
* by resolving the configured target ConnectionFactories.
139+
* @throws IllegalArgumentException if the target ConnectionFactories have not
140+
* been configured
141+
* @since 6.1
142+
* @see #setTargetConnectionFactories(Map)
143+
* @see #setDefaultTargetConnectionFactory(Object)
136144
*/
137145
public void initialize() {
138146
Assert.notNull(this.targetConnectionFactories, "Property 'targetConnectionFactories' must not be null");

spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/lookup/AbstractRoutingConnectionFactoryUnitTests.java

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.r2dbc.connection.lookup;
1818

19+
import java.util.Map;
20+
1921
import io.r2dbc.spi.ConnectionFactory;
2022
import org.junit.jupiter.api.BeforeEach;
2123
import org.junit.jupiter.api.Test;
@@ -26,7 +28,7 @@
2628
import reactor.test.StepVerifier;
2729
import reactor.util.context.Context;
2830

29-
import static java.util.Collections.singletonMap;
31+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3032
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3133

3234
/**
@@ -36,30 +38,28 @@
3638
* @author Jens Schauder
3739
*/
3840
@ExtendWith(MockitoExtension.class)
39-
public class AbstractRoutingConnectionFactoryUnitTests {
41+
class AbstractRoutingConnectionFactoryUnitTests {
4042

4143
private static final String ROUTING_KEY = "routingKey";
4244

45+
final DummyRoutingConnectionFactory connectionFactory = new DummyRoutingConnectionFactory();
46+
4347
@Mock
4448
ConnectionFactory defaultConnectionFactory;
4549

4650
@Mock
4751
ConnectionFactory routedConnectionFactory;
4852

49-
DummyRoutingConnectionFactory connectionFactory;
50-
5153

5254
@BeforeEach
53-
public void before() {
54-
connectionFactory = new DummyRoutingConnectionFactory();
55+
void before() {
5556
connectionFactory.setDefaultTargetConnectionFactory(defaultConnectionFactory);
5657
}
5758

5859

5960
@Test
60-
public void shouldDetermineRoutedFactory() {
61-
connectionFactory.setTargetConnectionFactories(
62-
singletonMap("key", routedConnectionFactory));
61+
void shouldDetermineRoutedFactory() {
62+
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory));
6363
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup());
6464
connectionFactory.afterPropertiesSet();
6565

@@ -71,9 +71,8 @@ public void shouldDetermineRoutedFactory() {
7171
}
7272

7373
@Test
74-
public void shouldFallbackToDefaultConnectionFactory() {
75-
connectionFactory.setTargetConnectionFactories(
76-
singletonMap("key", routedConnectionFactory));
74+
void shouldFallbackToDefaultConnectionFactory() {
75+
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory));
7776
connectionFactory.afterPropertiesSet();
7877

7978
connectionFactory.determineTargetConnectionFactory()
@@ -83,29 +82,27 @@ public void shouldFallbackToDefaultConnectionFactory() {
8382
}
8483

8584
@Test
86-
public void initializationShouldFailUnsupportedLookupKey() {
87-
connectionFactory.setTargetConnectionFactories(singletonMap("key", new Object()));
85+
void initializationShouldFailUnsupportedLookupKey() {
86+
connectionFactory.setTargetConnectionFactories(Map.of("key", new Object()));
8887

89-
assertThatThrownBy(() -> connectionFactory.afterPropertiesSet())
90-
.isInstanceOf(IllegalArgumentException.class);
88+
assertThatIllegalArgumentException().isThrownBy(connectionFactory::initialize);
9189
}
9290

9391
@Test
94-
public void initializationShouldFailUnresolvableKey() {
95-
connectionFactory.setTargetConnectionFactories(singletonMap("key", "value"));
92+
void initializationShouldFailUnresolvableKey() {
93+
connectionFactory.setTargetConnectionFactories(Map.of("key", "value"));
9694
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup());
9795

98-
assertThatThrownBy(() -> connectionFactory.afterPropertiesSet())
96+
assertThatThrownBy(connectionFactory::initialize)
9997
.isInstanceOf(ConnectionFactoryLookupFailureException.class)
10098
.hasMessageContaining("No ConnectionFactory with name 'value' registered");
10199
}
102100

103101
@Test
104-
public void unresolvableConnectionFactoryRetrievalShouldFail() {
102+
void unresolvableConnectionFactoryRetrievalShouldFail() {
105103
connectionFactory.setLenientFallback(false);
106104
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup());
107-
connectionFactory.setTargetConnectionFactories(
108-
singletonMap("key", routedConnectionFactory));
105+
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory));
109106
connectionFactory.afterPropertiesSet();
110107

111108
connectionFactory.determineTargetConnectionFactory()
@@ -115,9 +112,8 @@ public void unresolvableConnectionFactoryRetrievalShouldFail() {
115112
}
116113

117114
@Test
118-
public void connectionFactoryRetrievalWithUnknownLookupKeyShouldReturnDefaultConnectionFactory() {
119-
connectionFactory.setTargetConnectionFactories(
120-
singletonMap("key", routedConnectionFactory));
115+
void connectionFactoryRetrievalWithUnknownLookupKeyShouldReturnDefaultConnectionFactory() {
116+
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory));
121117
connectionFactory.setDefaultTargetConnectionFactory(defaultConnectionFactory);
122118
connectionFactory.afterPropertiesSet();
123119

@@ -129,9 +125,8 @@ public void connectionFactoryRetrievalWithUnknownLookupKeyShouldReturnDefaultCon
129125
}
130126

131127
@Test
132-
public void connectionFactoryRetrievalWithoutLookupKeyShouldReturnDefaultConnectionFactory() {
133-
connectionFactory.setTargetConnectionFactories(
134-
singletonMap("key", routedConnectionFactory));
128+
void connectionFactoryRetrievalWithoutLookupKeyShouldReturnDefaultConnectionFactory() {
129+
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory));
135130
connectionFactory.setDefaultTargetConnectionFactory(defaultConnectionFactory);
136131
connectionFactory.setLenientFallback(false);
137132
connectionFactory.afterPropertiesSet();
@@ -143,12 +138,12 @@ public void connectionFactoryRetrievalWithoutLookupKeyShouldReturnDefaultConnect
143138
}
144139

145140
@Test
146-
public void shouldLookupFromMap() {
141+
void shouldLookupFromMap() {
147142
MapConnectionFactoryLookup lookup =
148143
new MapConnectionFactoryLookup("lookup-key", routedConnectionFactory);
149144

150145
connectionFactory.setConnectionFactoryLookup(lookup);
151-
connectionFactory.setTargetConnectionFactories(singletonMap("my-key", "lookup-key"));
146+
connectionFactory.setTargetConnectionFactories(Map.of("my-key", "lookup-key"));
152147
connectionFactory.afterPropertiesSet();
153148

154149
connectionFactory.determineTargetConnectionFactory()
@@ -159,7 +154,7 @@ public void shouldLookupFromMap() {
159154
}
160155

161156
@Test
162-
public void shouldAllowModificationsAfterInitialization() {
157+
void shouldAllowModificationsAfterInitialization() {
163158
MapConnectionFactoryLookup lookup = new MapConnectionFactoryLookup();
164159

165160
connectionFactory.setConnectionFactoryLookup(lookup);
@@ -183,9 +178,8 @@ public void shouldAllowModificationsAfterInitialization() {
183178
}
184179

185180
@Test
186-
void testInitialize_shouldDetermineRoutedFactory() {
187-
connectionFactory.setTargetConnectionFactories(
188-
singletonMap("key", routedConnectionFactory));
181+
void initializeShouldDetermineRoutedFactory() {
182+
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory));
189183
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup());
190184
connectionFactory.initialize();
191185

0 commit comments

Comments
 (0)