|
|
|
@ -16,6 +16,8 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.r2dbc.connection.lookup; |
|
|
|
package org.springframework.r2dbc.connection.lookup; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
import io.r2dbc.spi.ConnectionFactory; |
|
|
|
import io.r2dbc.spi.ConnectionFactory; |
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
@ -26,7 +28,7 @@ import reactor.core.publisher.Mono; |
|
|
|
import reactor.test.StepVerifier; |
|
|
|
import reactor.test.StepVerifier; |
|
|
|
import reactor.util.context.Context; |
|
|
|
import reactor.util.context.Context; |
|
|
|
|
|
|
|
|
|
|
|
import static java.util.Collections.singletonMap; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -36,30 +38,28 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
|
|
* @author Jens Schauder |
|
|
|
* @author Jens Schauder |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ExtendWith(MockitoExtension.class) |
|
|
|
@ExtendWith(MockitoExtension.class) |
|
|
|
public class AbstractRoutingConnectionFactoryUnitTests { |
|
|
|
class AbstractRoutingConnectionFactoryUnitTests { |
|
|
|
|
|
|
|
|
|
|
|
private static final String ROUTING_KEY = "routingKey"; |
|
|
|
private static final String ROUTING_KEY = "routingKey"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final DummyRoutingConnectionFactory connectionFactory = new DummyRoutingConnectionFactory(); |
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
@Mock |
|
|
|
ConnectionFactory defaultConnectionFactory; |
|
|
|
ConnectionFactory defaultConnectionFactory; |
|
|
|
|
|
|
|
|
|
|
|
@Mock |
|
|
|
@Mock |
|
|
|
ConnectionFactory routedConnectionFactory; |
|
|
|
ConnectionFactory routedConnectionFactory; |
|
|
|
|
|
|
|
|
|
|
|
DummyRoutingConnectionFactory connectionFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
@BeforeEach |
|
|
|
public void before() { |
|
|
|
void before() { |
|
|
|
connectionFactory = new DummyRoutingConnectionFactory(); |
|
|
|
|
|
|
|
connectionFactory.setDefaultTargetConnectionFactory(defaultConnectionFactory); |
|
|
|
connectionFactory.setDefaultTargetConnectionFactory(defaultConnectionFactory); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldDetermineRoutedFactory() { |
|
|
|
void shouldDetermineRoutedFactory() { |
|
|
|
connectionFactory.setTargetConnectionFactories( |
|
|
|
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory)); |
|
|
|
singletonMap("key", routedConnectionFactory)); |
|
|
|
|
|
|
|
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup()); |
|
|
|
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup()); |
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
|
|
|
|
|
|
|
|
@ -71,9 +71,8 @@ public class AbstractRoutingConnectionFactoryUnitTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldFallbackToDefaultConnectionFactory() { |
|
|
|
void shouldFallbackToDefaultConnectionFactory() { |
|
|
|
connectionFactory.setTargetConnectionFactories( |
|
|
|
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory)); |
|
|
|
singletonMap("key", routedConnectionFactory)); |
|
|
|
|
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
|
|
|
|
|
|
|
|
connectionFactory.determineTargetConnectionFactory() |
|
|
|
connectionFactory.determineTargetConnectionFactory() |
|
|
|
@ -83,29 +82,27 @@ public class AbstractRoutingConnectionFactoryUnitTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void initializationShouldFailUnsupportedLookupKey() { |
|
|
|
void initializationShouldFailUnsupportedLookupKey() { |
|
|
|
connectionFactory.setTargetConnectionFactories(singletonMap("key", new Object())); |
|
|
|
connectionFactory.setTargetConnectionFactories(Map.of("key", new Object())); |
|
|
|
|
|
|
|
|
|
|
|
assertThatThrownBy(() -> connectionFactory.afterPropertiesSet()) |
|
|
|
assertThatIllegalArgumentException().isThrownBy(connectionFactory::initialize); |
|
|
|
.isInstanceOf(IllegalArgumentException.class); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void initializationShouldFailUnresolvableKey() { |
|
|
|
void initializationShouldFailUnresolvableKey() { |
|
|
|
connectionFactory.setTargetConnectionFactories(singletonMap("key", "value")); |
|
|
|
connectionFactory.setTargetConnectionFactories(Map.of("key", "value")); |
|
|
|
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup()); |
|
|
|
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup()); |
|
|
|
|
|
|
|
|
|
|
|
assertThatThrownBy(() -> connectionFactory.afterPropertiesSet()) |
|
|
|
assertThatThrownBy(connectionFactory::initialize) |
|
|
|
.isInstanceOf(ConnectionFactoryLookupFailureException.class) |
|
|
|
.isInstanceOf(ConnectionFactoryLookupFailureException.class) |
|
|
|
.hasMessageContaining("No ConnectionFactory with name 'value' registered"); |
|
|
|
.hasMessageContaining("No ConnectionFactory with name 'value' registered"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void unresolvableConnectionFactoryRetrievalShouldFail() { |
|
|
|
void unresolvableConnectionFactoryRetrievalShouldFail() { |
|
|
|
connectionFactory.setLenientFallback(false); |
|
|
|
connectionFactory.setLenientFallback(false); |
|
|
|
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup()); |
|
|
|
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup()); |
|
|
|
connectionFactory.setTargetConnectionFactories( |
|
|
|
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory)); |
|
|
|
singletonMap("key", routedConnectionFactory)); |
|
|
|
|
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
|
|
|
|
|
|
|
|
connectionFactory.determineTargetConnectionFactory() |
|
|
|
connectionFactory.determineTargetConnectionFactory() |
|
|
|
@ -115,9 +112,8 @@ public class AbstractRoutingConnectionFactoryUnitTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void connectionFactoryRetrievalWithUnknownLookupKeyShouldReturnDefaultConnectionFactory() { |
|
|
|
void connectionFactoryRetrievalWithUnknownLookupKeyShouldReturnDefaultConnectionFactory() { |
|
|
|
connectionFactory.setTargetConnectionFactories( |
|
|
|
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory)); |
|
|
|
singletonMap("key", routedConnectionFactory)); |
|
|
|
|
|
|
|
connectionFactory.setDefaultTargetConnectionFactory(defaultConnectionFactory); |
|
|
|
connectionFactory.setDefaultTargetConnectionFactory(defaultConnectionFactory); |
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
|
|
|
|
|
|
|
|
@ -129,9 +125,8 @@ public class AbstractRoutingConnectionFactoryUnitTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void connectionFactoryRetrievalWithoutLookupKeyShouldReturnDefaultConnectionFactory() { |
|
|
|
void connectionFactoryRetrievalWithoutLookupKeyShouldReturnDefaultConnectionFactory() { |
|
|
|
connectionFactory.setTargetConnectionFactories( |
|
|
|
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory)); |
|
|
|
singletonMap("key", routedConnectionFactory)); |
|
|
|
|
|
|
|
connectionFactory.setDefaultTargetConnectionFactory(defaultConnectionFactory); |
|
|
|
connectionFactory.setDefaultTargetConnectionFactory(defaultConnectionFactory); |
|
|
|
connectionFactory.setLenientFallback(false); |
|
|
|
connectionFactory.setLenientFallback(false); |
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
@ -143,12 +138,12 @@ public class AbstractRoutingConnectionFactoryUnitTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldLookupFromMap() { |
|
|
|
void shouldLookupFromMap() { |
|
|
|
MapConnectionFactoryLookup lookup = |
|
|
|
MapConnectionFactoryLookup lookup = |
|
|
|
new MapConnectionFactoryLookup("lookup-key", routedConnectionFactory); |
|
|
|
new MapConnectionFactoryLookup("lookup-key", routedConnectionFactory); |
|
|
|
|
|
|
|
|
|
|
|
connectionFactory.setConnectionFactoryLookup(lookup); |
|
|
|
connectionFactory.setConnectionFactoryLookup(lookup); |
|
|
|
connectionFactory.setTargetConnectionFactories(singletonMap("my-key", "lookup-key")); |
|
|
|
connectionFactory.setTargetConnectionFactories(Map.of("my-key", "lookup-key")); |
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
connectionFactory.afterPropertiesSet(); |
|
|
|
|
|
|
|
|
|
|
|
connectionFactory.determineTargetConnectionFactory() |
|
|
|
connectionFactory.determineTargetConnectionFactory() |
|
|
|
@ -159,7 +154,7 @@ public class AbstractRoutingConnectionFactoryUnitTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldAllowModificationsAfterInitialization() { |
|
|
|
void shouldAllowModificationsAfterInitialization() { |
|
|
|
MapConnectionFactoryLookup lookup = new MapConnectionFactoryLookup(); |
|
|
|
MapConnectionFactoryLookup lookup = new MapConnectionFactoryLookup(); |
|
|
|
|
|
|
|
|
|
|
|
connectionFactory.setConnectionFactoryLookup(lookup); |
|
|
|
connectionFactory.setConnectionFactoryLookup(lookup); |
|
|
|
@ -183,9 +178,8 @@ public class AbstractRoutingConnectionFactoryUnitTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void testInitialize_shouldDetermineRoutedFactory() { |
|
|
|
void initializeShouldDetermineRoutedFactory() { |
|
|
|
connectionFactory.setTargetConnectionFactories( |
|
|
|
connectionFactory.setTargetConnectionFactories(Map.of("key", routedConnectionFactory)); |
|
|
|
singletonMap("key", routedConnectionFactory)); |
|
|
|
|
|
|
|
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup()); |
|
|
|
connectionFactory.setConnectionFactoryLookup(new MapConnectionFactoryLookup()); |
|
|
|
connectionFactory.initialize(); |
|
|
|
connectionFactory.initialize(); |
|
|
|
|
|
|
|
|
|
|
|
|