Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
#75 - Polishing.
Unify connection creation by providing JDBC URL. Obtain R2DBC ConnectionFactory using connection factory discovery.
  • Loading branch information
mp911de committed Apr 2, 2019
commit 01da6d9bc55d1eab7ca0eb12cb83613b5c80ff27
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.r2dbc.testing;

import static io.r2dbc.spi.ConnectionFactoryOptions.*;

import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryOptions;

import javax.sql.DataSource;

/**
* Utility methods to configure {@link DataSource}/{@link ConnectionFactoryOptions}.
*
* @author Mark Paluch
*/
abstract class ConnectionUtils {

/**
* Obtain a {@link ConnectionFactory} given {@link ExternalDatabase} and {@code driver}.
*
* @param driver
* @param configuration
* @return
*/
static ConnectionFactory getConnectionFactory(String driver, ExternalDatabase configuration) {
return ConnectionFactories.get(createOptions(driver, configuration));
}

/**
* Create {@link ConnectionFactoryOptions} from {@link ExternalDatabase} and {@code driver}.
*
* @param driver
* @param configuration
* @return
*/
private static ConnectionFactoryOptions createOptions(String driver, ExternalDatabase configuration) {

return ConnectionFactoryOptions.builder().option(DRIVER, driver) //
.option(USER, configuration.getUsername()) //
.option(PASSWORD, configuration.getPassword()) //
.option(DATABASE, configuration.getDatabase()) //
.option(HOST, configuration.getHostname()) //
.option(PORT, configuration.getPort()) //
.build();
}

private ConnectionUtils() {
// utility constructor.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.rules.ExternalResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.JdbcDatabaseContainer;

/**
* {@link ExternalResource} wrapper to encapsulate {@link ProvidedDatabase} and
Expand All @@ -47,25 +48,35 @@ public static ExternalDatabase unavailable() {
return NoAvailableDatabase.INSTANCE;
}

/**
* @return hostname on which the database service runs.
*/
public abstract String getHostname();

/**
* @return the post of the database service.
*/
public abstract int getPort();

/**
* @return hostname on which the database service runs.
* @return database user name.
*/
public abstract String getHostname();
public abstract String getUsername();

/**
* @return password for the database user.
*/
public abstract String getPassword();

/**
* @return name of the database.
*/
public abstract String getDatabase();

/**
* @return database user name.
* @return JDBC URL for the endpoint.
*/
public abstract String getUsername();
public abstract String getJdbcUrl();

/**
* Throws an {@link AssumptionViolatedException} if the database cannot be reached.
Expand Down Expand Up @@ -98,45 +109,63 @@ boolean checkValidity() {
return false;
}

/**
* @return password for the database user.
*/
public abstract String getPassword();

/**
* Provided (unmanaged resource) database connection coordinates.
*/
@Builder
public static class ProvidedDatabase extends ExternalDatabase {

private final int port;
private final String hostname;
private final String database;
private final int port;
private final String username;
private final String password;
private final String database;
private final String jdbcUrl;

/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPort()
public static ProvidedDatabaseBuilder builder() {
return new ProvidedDatabaseBuilder();
}

/**
* Create a {@link ProvidedDatabaseBuilder} initialized with {@link JdbcDatabaseContainer}.
*
* @param container
* @return
*/
@Override
public int getPort() {
return port;
public static ProvidedDatabaseBuilder builder(JdbcDatabaseContainer container) {

return builder().hostname(container.getContainerIpAddress()) //
.port(container.getFirstMappedPort()) //
.username(container.getUsername()) //
.password(container.getPassword()) //
.database(container.getDatabaseName()) //
.jdbcUrl(container.getJdbcUrl());
}

/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getHostname()
/**
* Create a {@link ProvidedDatabase} from {@link JdbcDatabaseContainer}.
*
* @param container
* @return
*/
public static ProvidedDatabase from(JdbcDatabaseContainer container) {
return builder(container).build();
}

/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getHostname()
*/
@Override
public String getHostname() {
return hostname;
}

/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPort()
*/
@Override
public String getDatabase() {
return database;
public int getPort() {
return port;
}

/* (non-Javadoc)
Expand All @@ -154,6 +183,22 @@ public String getUsername() {
public String getPassword() {
return password;
}

/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
*/
@Override
public String getDatabase() {
return database;
}

/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getJdbcUrl()
*/
@Override
public String getJdbcUrl() {
return jdbcUrl;
}
}

/**
Expand All @@ -173,11 +218,6 @@ boolean checkValidity() {
return false;
}

@Override
public int getPort() {
throw new UnsupportedOperationException(getClass().getSimpleName());
}

/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getHostname()
*/
Expand All @@ -187,10 +227,10 @@ public String getHostname() {
}

/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPort()
*/
@Override
public String getDatabase() {
public int getPort() {
throw new UnsupportedOperationException(getClass().getSimpleName());
}

Expand All @@ -209,5 +249,21 @@ public String getUsername() {
public String getPassword() {
throw new UnsupportedOperationException(getClass().getSimpleName());
}

/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
*/
@Override
public String getDatabase() {
throw new UnsupportedOperationException(getClass().getSimpleName());
}

/* (non-Javadoc)
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getJdbcUrl()
*/
@Override
public String getJdbcUrl() {
throw new UnsupportedOperationException(getClass().getSimpleName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@

import org.testcontainers.containers.MySQLContainer;

import com.github.jasync.r2dbc.mysql.JasyncConnectionFactory;
import com.github.jasync.sql.db.Configuration;
import com.github.jasync.sql.db.mysql.pool.MySQLConnectionFactory;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

/**
Expand Down Expand Up @@ -104,15 +101,12 @@ private static ExternalDatabase testContainer() {
if (testContainerDatabase == null) {

try {
MySQLContainer mySQLContainer = new MySQLContainer("mysql:5.6.43");
mySQLContainer.start();
MySQLContainer container = new MySQLContainer("mysql:5.6.43");
container.start();

testContainerDatabase = ProvidedDatabase.builder() //
.hostname("localhost") //
.port(mySQLContainer.getFirstMappedPort()) //
.database(mySQLContainer.getDatabaseName()) //
testContainerDatabase = ProvidedDatabase.builder(container) //
.username("root") //
.password(mySQLContainer.getPassword()).build();
.build();
} catch (IllegalStateException ise) {
// docker not available.
testContainerDatabase = ExternalDatabase.unavailable();
Expand All @@ -126,10 +120,7 @@ private static ExternalDatabase testContainer() {
* Creates a new {@link ConnectionFactory} configured from the {@link ExternalDatabase}..
*/
public static ConnectionFactory createConnectionFactory(ExternalDatabase database) {

MySQLConnectionFactory jasync = new MySQLConnectionFactory(new Configuration(database.getUsername(),
database.getHostname(), database.getPort(), database.getPassword(), database.getDatabase()));
return new JasyncConnectionFactory(jasync);
return ConnectionUtils.getConnectionFactory("mysql", database);
}

/**
Expand All @@ -141,11 +132,8 @@ public static DataSource createDataSource(ExternalDatabase database) {

dataSource.setUser(database.getUsername());
dataSource.setPassword(database.getPassword());
dataSource.setDatabaseName(database.getDatabase());
dataSource.setServerName(database.getHostname());
dataSource.setPortNumber(database.getPort());
dataSource.setURL(database.getJdbcUrl());

return dataSource;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.springframework.data.r2dbc.testing;

import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
import io.r2dbc.spi.ConnectionFactory;

import java.util.function.Supplier;
Expand All @@ -10,7 +8,9 @@
import javax.sql.DataSource;

import org.postgresql.ds.PGSimpleDataSource;

import org.springframework.data.r2dbc.testing.ExternalDatabase.ProvidedDatabase;

import org.testcontainers.containers.PostgreSQLContainer;

/**
Expand Down Expand Up @@ -87,15 +87,10 @@ private static ExternalDatabase testContainer() {
if (testContainerDatabase == null) {

try {
PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();
postgreSQLContainer.start();
PostgreSQLContainer container = new PostgreSQLContainer();
container.start();

testContainerDatabase = ProvidedDatabase.builder() //
.hostname("localhost") //
.port(postgreSQLContainer.getFirstMappedPort()) //
.database(postgreSQLContainer.getDatabaseName()) //
.username(postgreSQLContainer.getUsername()) //
.password(postgreSQLContainer.getPassword()).build();
testContainerDatabase = ProvidedDatabase.from(container);

} catch (IllegalStateException ise) {
// docker not available.
Expand All @@ -111,14 +106,7 @@ private static ExternalDatabase testContainer() {
* Creates a new {@link ConnectionFactory} configured from the {@link ExternalDatabase}..
*/
public static ConnectionFactory createConnectionFactory(ExternalDatabase database) {

return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder() //
.host(database.getHostname()) //
.database(database.getDatabase()) //
.port(database.getPort()) //
.username(database.getUsername()) //
.password(database.getPassword()) //
.build());
return ConnectionUtils.getConnectionFactory("postgresql", database);
}

/**
Expand All @@ -130,11 +118,8 @@ public static DataSource createDataSource(ExternalDatabase database) {

dataSource.setUser(database.getUsername());
dataSource.setPassword(database.getPassword());
dataSource.setDatabaseName(database.getDatabase());
dataSource.setServerName(database.getHostname());
dataSource.setPortNumber(database.getPort());
dataSource.setURL(database.getJdbcUrl());

return dataSource;
}

}
Loading