Search in sources :

Example 6 with ConnectionFactory

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

the class R2dbcAutoConfigurationTests method configureWithPoolInvokeOptionCustomizer.

@Test
void configureWithPoolInvokeOptionCustomizer() {
    this.contextRunner.withPropertyValues("spring.r2dbc.url:r2dbc:simple://host/database").withUserConfiguration(CustomizerConfiguration.class).run((context) -> {
        assertThat(context).hasSingleBean(ConnectionFactory.class).hasSingleBean(ConnectionPool.class);
        ConnectionFactory pool = context.getBean(ConnectionFactory.class);
        ConnectionFactory connectionFactory = ((ConnectionPool) pool).unwrap();
        assertThat(connectionFactory).asInstanceOf(type(OptionsCapableConnectionFactory.class)).extracting(OptionsCapableConnectionFactory::getOptions).satisfies((options) -> assertThat(options.getRequiredValue(Option.valueOf("customized"))).isEqualTo(Boolean.TRUE));
    });
}
Also used : ConnectionPool(io.r2dbc.pool.ConnectionPool) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) SimpleTestConnectionFactory(org.springframework.boot.autoconfigure.r2dbc.SimpleConnectionFactoryProvider.SimpleTestConnectionFactory) OptionsCapableConnectionFactory(org.springframework.boot.r2dbc.OptionsCapableConnectionFactory) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) OptionsCapableConnectionFactory(org.springframework.boot.r2dbc.OptionsCapableConnectionFactory) Test(org.junit.jupiter.api.Test)

Example 7 with ConnectionFactory

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

the class ConnectionFactoryBuilderTests method buildWhenDerivedFromPoolReturnsNewNonPooledConnectionFactory.

@Test
void buildWhenDerivedFromPoolReturnsNewNonPooledConnectionFactory() {
    ConnectionFactory connectionFactory = ConnectionFactoryBuilder.withUrl(EmbeddedDatabaseConnection.H2.getUrl(UUID.randomUUID().toString())).build();
    ConnectionFactoryOptions initialOptions = ((OptionsCapableConnectionFactory) connectionFactory).getOptions();
    ConnectionPoolConfiguration poolConfiguration = ConnectionPoolConfiguration.builder(connectionFactory).build();
    ConnectionPool pool = new ConnectionPool(poolConfiguration);
    ConnectionFactory derived = ConnectionFactoryBuilder.derivedFrom(pool).username("admin").password("secret").build();
    assertThat(derived).isNotInstanceOf(ConnectionPool.class).isInstanceOf(OptionsCapableConnectionFactory.class);
    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 : ConnectionPool(io.r2dbc.pool.ConnectionPool) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) ConnectionPoolConfiguration(io.r2dbc.pool.ConnectionPoolConfiguration) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with ConnectionFactory

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

the class ConnectionFactoryBuilderTests method buildWhenDerivedWithNewDatabaseReturnsNewConnectionFactory.

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

Example 9 with ConnectionFactory

use of io.r2dbc.spi.ConnectionFactory in project spring-framework by spring-projects.

the class ConnectionFactoryUtils method getConnectionSynchronizationOrder.

/**
 * Determine the connection synchronization order to use for the given {@link ConnectionFactory}.
 * Decreased for every level of nesting that a {@link ConnectionFactory} has,
 * checked through the level of {@link DelegatingConnectionFactory} nesting.
 * @param connectionFactory the {@link ConnectionFactory} to check
 * @return the connection synchronization order to use
 * @see #CONNECTION_SYNCHRONIZATION_ORDER
 */
private static int getConnectionSynchronizationOrder(ConnectionFactory connectionFactory) {
    int order = CONNECTION_SYNCHRONIZATION_ORDER;
    ConnectionFactory current = connectionFactory;
    while (current instanceof DelegatingConnectionFactory) {
        order--;
        current = ((DelegatingConnectionFactory) current).getTargetConnectionFactory();
    }
    return order;
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory)

Example 10 with ConnectionFactory

use of io.r2dbc.spi.ConnectionFactory in project spring-framework by spring-projects.

the class BeanFactoryConnectionFactoryLookupUnitTests method shouldLookupConnectionFactory.

@Test
public void shouldLookupConnectionFactory() {
    DummyConnectionFactory expectedConnectionFactory = new DummyConnectionFactory();
    when(beanFactory.getBean(CONNECTION_FACTORY_BEAN_NAME, ConnectionFactory.class)).thenReturn(expectedConnectionFactory);
    BeanFactoryConnectionFactoryLookup lookup = new BeanFactoryConnectionFactoryLookup();
    lookup.setBeanFactory(beanFactory);
    ConnectionFactory connectionFactory = lookup.getConnectionFactory(CONNECTION_FACTORY_BEAN_NAME);
    assertThat(connectionFactory).isNotNull();
    assertThat(connectionFactory).isSameAs(expectedConnectionFactory);
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionFactory (io.r2dbc.spi.ConnectionFactory)25 Test (org.junit.jupiter.api.Test)17 H2ConnectionFactory (io.r2dbc.h2.H2ConnectionFactory)11 CloseableConnectionFactory (io.r2dbc.h2.CloseableConnectionFactory)5 H2ConnectionOption (io.r2dbc.h2.H2ConnectionOption)5 Connection (io.r2dbc.spi.Connection)5 Result (io.r2dbc.spi.Result)5 ValidationDepth (io.r2dbc.spi.ValidationDepth)5 Collections (java.util.Collections)5 UUID (java.util.UUID)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Assertions.entry (org.assertj.core.api.Assertions.entry)5 BDDMockito.given (org.mockito.BDDMockito.given)5 Mockito.mock (org.mockito.Mockito.mock)5 ReactiveHealthIndicator (org.springframework.boot.actuate.health.ReactiveHealthIndicator)5 Status (org.springframework.boot.actuate.health.Status)5 Flux (reactor.core.publisher.Flux)5 Mono (reactor.core.publisher.Mono)5 StepVerifier (reactor.test.StepVerifier)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4