Search in sources :

Example 6 with ConnectionFactoryOptions

use of io.r2dbc.spi.ConnectionFactoryOptions in project spring-boot by spring-projects.

the class ConnectionFactoryOptionsInitializer method initializeRegularOptions.

private ConnectionFactoryOptions.Builder initializeRegularOptions(R2dbcProperties properties) {
    ConnectionFactoryOptions urlOptions = ConnectionFactoryOptions.parse(properties.getUrl());
    Builder optionsBuilder = urlOptions.mutate();
    configureIf(optionsBuilder, urlOptions, ConnectionFactoryOptions.USER, properties::getUsername, StringUtils::hasText);
    configureIf(optionsBuilder, urlOptions, ConnectionFactoryOptions.PASSWORD, properties::getPassword, StringUtils::hasText);
    configureIf(optionsBuilder, urlOptions, ConnectionFactoryOptions.DATABASE, () -> determineDatabaseName(properties), StringUtils::hasText);
    if (properties.getProperties() != null) {
        properties.getProperties().forEach((key, value) -> optionsBuilder.option(Option.valueOf(key), value));
    }
    return optionsBuilder;
}
Also used : ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) StringUtils(org.springframework.util.StringUtils) Builder(io.r2dbc.spi.ConnectionFactoryOptions.Builder)

Example 7 with ConnectionFactoryOptions

use of io.r2dbc.spi.ConnectionFactoryOptions in project spring-boot by spring-projects.

the class ConnectionFactoryBuilderTests method stringlyTypedOptionIsMappedWhenCreatingPoolConfiguration.

@ParameterizedTest
@SuppressWarnings({ "rawtypes", "unchecked" })
@MethodSource("primitivePoolingConnectionProviderOptions")
void stringlyTypedOptionIsMappedWhenCreatingPoolConfiguration(Option option) {
    String url = "r2dbc:pool:h2:mem:///" + UUID.randomUUID();
    ExpectedOption expectedOption = ExpectedOption.get(option);
    ConnectionFactoryOptions options = ConnectionFactoryBuilder.withUrl(url).configure((builder) -> builder.option(PoolingConnectionFactoryProvider.POOL_NAME, "defaultName").option(option, expectedOption.value.toString())).buildOptions();
    ConnectionPoolConfiguration configuration = new PoolingAwareOptionsCapableWrapper().connectionPoolConfiguration(options, mock(ConnectionFactory.class));
    assertThat(configuration).extracting(expectedOption.property).isEqualTo(expectedOption.value);
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) PoolingAwareOptionsCapableWrapper(org.springframework.boot.r2dbc.ConnectionFactoryBuilder.PoolingAwareOptionsCapableWrapper) H2ConnectionFactoryMetadata(io.r2dbc.h2.H2ConnectionFactoryMetadata) PoolingConnectionFactoryProvider(io.r2dbc.pool.PoolingConnectionFactoryProvider) ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) FieldFilter(org.springframework.util.ReflectionUtils.FieldFilter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConnectionPoolConfiguration(io.r2dbc.pool.ConnectionPoolConfiguration) UUID(java.util.UUID) ConnectionPool(io.r2dbc.pool.ConnectionPool) Arguments(org.junit.jupiter.params.provider.Arguments) Function(java.util.function.Function) Option(io.r2dbc.spi.Option) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ReflectionUtils(org.springframework.util.ReflectionUtils) Duration(java.time.Duration) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ResolvableType(org.springframework.core.ResolvableType) ValidationDepth(io.r2dbc.spi.ValidationDepth) MethodSource(org.junit.jupiter.params.provider.MethodSource) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) PoolingAwareOptionsCapableWrapper(org.springframework.boot.r2dbc.ConnectionFactoryBuilder.PoolingAwareOptionsCapableWrapper) ConnectionPoolConfiguration(io.r2dbc.pool.ConnectionPoolConfiguration) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with ConnectionFactoryOptions

use of io.r2dbc.spi.ConnectionFactoryOptions in project spring-boot by spring-projects.

the class ConnectionFactoryBuilderTests method buildOptionsWithBasicUrlShouldExposeOptions.

@Test
void buildOptionsWithBasicUrlShouldExposeOptions() {
    ConnectionFactoryOptions options = ConnectionFactoryBuilder.withUrl("r2dbc:simple://:pool:").buildOptions();
    assertThat(options.hasOption(ConnectionFactoryOptions.USER)).isFalse();
    assertThat(options.hasOption(ConnectionFactoryOptions.PASSWORD)).isFalse();
    assertThat(options.getRequiredValue(ConnectionFactoryOptions.DRIVER)).isEqualTo("simple");
}
Also used : ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with ConnectionFactoryOptions

use of io.r2dbc.spi.ConnectionFactoryOptions in project spring-boot by spring-projects.

the class ConnectionFactoryBuilderTests method buildOptionsWithDriverPropertiesShouldExposeOptions.

@Test
void buildOptionsWithDriverPropertiesShouldExposeOptions() {
    ConnectionFactoryOptions options = ConnectionFactoryBuilder.withUrl("r2dbc:simple://user:password@myhost").configure((o) -> o.option(Option.valueOf("simpleOne"), "one").option(Option.valueOf("simpleTwo"), "two")).buildOptions();
    assertThat(options.getRequiredValue(Option.<String>valueOf("simpleOne"))).isEqualTo("one");
    assertThat(options.getRequiredValue(Option.<String>valueOf("simpleTwo"))).isEqualTo("two");
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) PoolingAwareOptionsCapableWrapper(org.springframework.boot.r2dbc.ConnectionFactoryBuilder.PoolingAwareOptionsCapableWrapper) H2ConnectionFactoryMetadata(io.r2dbc.h2.H2ConnectionFactoryMetadata) PoolingConnectionFactoryProvider(io.r2dbc.pool.PoolingConnectionFactoryProvider) ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) FieldFilter(org.springframework.util.ReflectionUtils.FieldFilter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConnectionPoolConfiguration(io.r2dbc.pool.ConnectionPoolConfiguration) UUID(java.util.UUID) ConnectionPool(io.r2dbc.pool.ConnectionPool) Arguments(org.junit.jupiter.params.provider.Arguments) Function(java.util.function.Function) Option(io.r2dbc.spi.Option) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ReflectionUtils(org.springframework.util.ReflectionUtils) Duration(java.time.Duration) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ResolvableType(org.springframework.core.ResolvableType) ValidationDepth(io.r2dbc.spi.ValidationDepth) MethodSource(org.junit.jupiter.params.provider.MethodSource) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with ConnectionFactoryOptions

use of io.r2dbc.spi.ConnectionFactoryOptions in project spring-boot by spring-projects.

the class ConnectionFactoryBuilderTests method buildWhenDerivedWithNewCredentialsReturnsNewConnectionFactory.

@Test
void buildWhenDerivedWithNewCredentialsReturnsNewConnectionFactory() {
    ConnectionFactory connectionFactory = ConnectionFactoryBuilder.withUrl(EmbeddedDatabaseConnection.H2.getUrl(UUID.randomUUID().toString())).build();
    ConnectionFactoryOptions initialOptions = ((OptionsCapableConnectionFactory) connectionFactory).getOptions();
    ConnectionFactory derived = ConnectionFactoryBuilder.derivedFrom(connectionFactory).username("admin").password("secret").build();
    ConnectionFactoryOptions derivedOptions = ((OptionsCapableConnectionFactory) derived).getOptions();
    assertThat(derivedOptions.getRequiredValue(ConnectionFactoryOptions.USER)).isEqualTo("admin");
    assertThat(derivedOptions.getRequiredValue(ConnectionFactoryOptions.PASSWORD)).isEqualTo("secret");
    assertMatchingOptions(derivedOptions, initialOptions, ConnectionFactoryOptions.CONNECT_TIMEOUT, ConnectionFactoryOptions.DATABASE, ConnectionFactoryOptions.DRIVER, ConnectionFactoryOptions.HOST, ConnectionFactoryOptions.PORT, ConnectionFactoryOptions.PROTOCOL, ConnectionFactoryOptions.SSL);
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ConnectionFactoryOptions (io.r2dbc.spi.ConnectionFactoryOptions)11 Test (org.junit.jupiter.api.Test)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 ConnectionFactory (io.r2dbc.spi.ConnectionFactory)6 ConnectionPool (io.r2dbc.pool.ConnectionPool)4 ConnectionPoolConfiguration (io.r2dbc.pool.ConnectionPoolConfiguration)4 H2ConnectionFactoryMetadata (io.r2dbc.h2.H2ConnectionFactoryMetadata)3 PoolingConnectionFactoryProvider (io.r2dbc.pool.PoolingConnectionFactoryProvider)3 Option (io.r2dbc.spi.Option)3 ValidationDepth (io.r2dbc.spi.ValidationDepth)3 Duration (java.time.Duration)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 UUID (java.util.UUID)3 Function (java.util.function.Function)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 Arguments (org.junit.jupiter.params.provider.Arguments)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 Mockito.mock (org.mockito.Mockito.mock)3