Skip to content

Commit 1396344

Browse files
committed
#75 - Polishing.
Unify connection creation by providing JDBC URL. Obtain R2DBC ConnectionFactory using connection factory discovery.
1 parent 9698094 commit 1396344

File tree

5 files changed

+165
-81
lines changed

5 files changed

+165
-81
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.r2dbc.testing;
17+
18+
import static io.r2dbc.spi.ConnectionFactoryOptions.*;
19+
20+
import io.r2dbc.spi.ConnectionFactories;
21+
import io.r2dbc.spi.ConnectionFactory;
22+
import io.r2dbc.spi.ConnectionFactoryOptions;
23+
24+
import javax.sql.DataSource;
25+
26+
/**
27+
* Utility methods to configure {@link DataSource}/{@link ConnectionFactoryOptions}.
28+
*
29+
* @author Mark Paluch
30+
*/
31+
abstract class ConnectionUtils {
32+
33+
/**
34+
* Obtain a {@link ConnectionFactory} given {@link ExternalDatabase} and {@code driver}.
35+
*
36+
* @param driver
37+
* @param configuration
38+
* @return
39+
*/
40+
static ConnectionFactory getConnectionFactory(String driver, ExternalDatabase configuration) {
41+
return ConnectionFactories.get(createOptions(driver, configuration));
42+
}
43+
44+
/**
45+
* Create {@link ConnectionFactoryOptions} from {@link ExternalDatabase} and {@code driver}.
46+
*
47+
* @param driver
48+
* @param configuration
49+
* @return
50+
*/
51+
private static ConnectionFactoryOptions createOptions(String driver, ExternalDatabase configuration) {
52+
53+
return ConnectionFactoryOptions.builder().option(DRIVER, driver) //
54+
.option(USER, configuration.getUsername()) //
55+
.option(PASSWORD, configuration.getPassword()) //
56+
.option(DATABASE, configuration.getDatabase()) //
57+
.option(HOST, configuration.getHostname()) //
58+
.option(PORT, configuration.getPort()) //
59+
.build();
60+
}
61+
62+
private ConnectionUtils() {
63+
// utility constructor.
64+
}
65+
}

src/test/java/org/springframework/data/r2dbc/testing/ExternalDatabase.java

+84-28
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.rules.ExternalResource;
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
29+
import org.testcontainers.containers.JdbcDatabaseContainer;
2930

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

51+
/**
52+
* @return hostname on which the database service runs.
53+
*/
54+
public abstract String getHostname();
55+
5056
/**
5157
* @return the post of the database service.
5258
*/
5359
public abstract int getPort();
5460

5561
/**
56-
* @return hostname on which the database service runs.
62+
* @return database user name.
5763
*/
58-
public abstract String getHostname();
64+
public abstract String getUsername();
65+
66+
/**
67+
* @return password for the database user.
68+
*/
69+
public abstract String getPassword();
5970

6071
/**
6172
* @return name of the database.
6273
*/
6374
public abstract String getDatabase();
6475

6576
/**
66-
* @return database user name.
77+
* @return JDBC URL for the endpoint.
6778
*/
68-
public abstract String getUsername();
79+
public abstract String getJdbcUrl();
6980

7081
/**
7182
* Throws an {@link AssumptionViolatedException} if the database cannot be reached.
@@ -98,45 +109,63 @@ boolean checkValidity() {
98109
return false;
99110
}
100111

101-
/**
102-
* @return password for the database user.
103-
*/
104-
public abstract String getPassword();
105-
106112
/**
107113
* Provided (unmanaged resource) database connection coordinates.
108114
*/
109115
@Builder
110116
public static class ProvidedDatabase extends ExternalDatabase {
111117

112-
private final int port;
113118
private final String hostname;
114-
private final String database;
119+
private final int port;
115120
private final String username;
116121
private final String password;
122+
private final String database;
123+
private final String jdbcUrl;
117124

118-
/* (non-Javadoc)
119-
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPort()
125+
public static ProvidedDatabaseBuilder builder() {
126+
return new ProvidedDatabaseBuilder();
127+
}
128+
129+
/**
130+
* Create a {@link ProvidedDatabaseBuilder} initialized with {@link JdbcDatabaseContainer}.
131+
*
132+
* @param container
133+
* @return
120134
*/
121-
@Override
122-
public int getPort() {
123-
return port;
135+
public static ProvidedDatabaseBuilder builder(JdbcDatabaseContainer container) {
136+
137+
return builder().hostname(container.getContainerIpAddress()) //
138+
.port(container.getFirstMappedPort()) //
139+
.username(container.getUsername()) //
140+
.password(container.getPassword()) //
141+
.database(container.getDatabaseName()) //
142+
.jdbcUrl(container.getJdbcUrl());
124143
}
125144

126-
/* (non-Javadoc)
127-
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getHostname()
145+
/**
146+
* Create a {@link ProvidedDatabase} from {@link JdbcDatabaseContainer}.
147+
*
148+
* @param container
149+
* @return
128150
*/
151+
public static ProvidedDatabase from(JdbcDatabaseContainer container) {
152+
return builder(container).build();
153+
}
154+
155+
/* (non-Javadoc)
156+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getHostname()
157+
*/
129158
@Override
130159
public String getHostname() {
131160
return hostname;
132161
}
133162

134163
/* (non-Javadoc)
135-
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
164+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPort()
136165
*/
137166
@Override
138-
public String getDatabase() {
139-
return database;
167+
public int getPort() {
168+
return port;
140169
}
141170

142171
/* (non-Javadoc)
@@ -154,6 +183,22 @@ public String getUsername() {
154183
public String getPassword() {
155184
return password;
156185
}
186+
187+
/* (non-Javadoc)
188+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
189+
*/
190+
@Override
191+
public String getDatabase() {
192+
return database;
193+
}
194+
195+
/* (non-Javadoc)
196+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getJdbcUrl()
197+
*/
198+
@Override
199+
public String getJdbcUrl() {
200+
return jdbcUrl;
201+
}
157202
}
158203

159204
/**
@@ -173,11 +218,6 @@ boolean checkValidity() {
173218
return false;
174219
}
175220

176-
@Override
177-
public int getPort() {
178-
throw new UnsupportedOperationException(getClass().getSimpleName());
179-
}
180-
181221
/* (non-Javadoc)
182222
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getHostname()
183223
*/
@@ -187,10 +227,10 @@ public String getHostname() {
187227
}
188228

189229
/* (non-Javadoc)
190-
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
230+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getPort()
191231
*/
192232
@Override
193-
public String getDatabase() {
233+
public int getPort() {
194234
throw new UnsupportedOperationException(getClass().getSimpleName());
195235
}
196236

@@ -209,5 +249,21 @@ public String getUsername() {
209249
public String getPassword() {
210250
throw new UnsupportedOperationException(getClass().getSimpleName());
211251
}
252+
253+
/* (non-Javadoc)
254+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getDatabase()
255+
*/
256+
@Override
257+
public String getDatabase() {
258+
throw new UnsupportedOperationException(getClass().getSimpleName());
259+
}
260+
261+
/* (non-Javadoc)
262+
* @see org.springframework.data.jdbc.core.function.ExternalDatabase#getJdbcUrl()
263+
*/
264+
@Override
265+
public String getJdbcUrl() {
266+
throw new UnsupportedOperationException(getClass().getSimpleName());
267+
}
212268
}
213269
}

src/test/java/org/springframework/data/r2dbc/testing/MySqlTestSupport.java

+6-18
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626

2727
import org.testcontainers.containers.MySQLContainer;
2828

29-
import com.github.jasync.r2dbc.mysql.JasyncConnectionFactory;
30-
import com.github.jasync.sql.db.Configuration;
31-
import com.github.jasync.sql.db.mysql.pool.MySQLConnectionFactory;
3229
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
3330

3431
/**
@@ -104,15 +101,12 @@ private static ExternalDatabase testContainer() {
104101
if (testContainerDatabase == null) {
105102

106103
try {
107-
MySQLContainer mySQLContainer = new MySQLContainer("mysql:5.6.43");
108-
mySQLContainer.start();
104+
MySQLContainer container = new MySQLContainer("mysql:5.6.43");
105+
container.start();
109106

110-
testContainerDatabase = ProvidedDatabase.builder() //
111-
.hostname("localhost") //
112-
.port(mySQLContainer.getFirstMappedPort()) //
113-
.database(mySQLContainer.getDatabaseName()) //
107+
testContainerDatabase = ProvidedDatabase.builder(container) //
114108
.username("root") //
115-
.password(mySQLContainer.getPassword()).build();
109+
.build();
116110
} catch (IllegalStateException ise) {
117111
// docker not available.
118112
testContainerDatabase = ExternalDatabase.unavailable();
@@ -126,10 +120,7 @@ private static ExternalDatabase testContainer() {
126120
* Creates a new {@link ConnectionFactory} configured from the {@link ExternalDatabase}..
127121
*/
128122
public static ConnectionFactory createConnectionFactory(ExternalDatabase database) {
129-
130-
MySQLConnectionFactory jasync = new MySQLConnectionFactory(new Configuration(database.getUsername(),
131-
database.getHostname(), database.getPort(), database.getPassword(), database.getDatabase()));
132-
return new JasyncConnectionFactory(jasync);
123+
return ConnectionUtils.getConnectionFactory("mysql", database);
133124
}
134125

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

142133
dataSource.setUser(database.getUsername());
143134
dataSource.setPassword(database.getPassword());
144-
dataSource.setDatabaseName(database.getDatabase());
145-
dataSource.setServerName(database.getHostname());
146-
dataSource.setPortNumber(database.getPort());
135+
dataSource.setURL(database.getJdbcUrl());
147136

148137
return dataSource;
149138
}
150-
151139
}

src/test/java/org/springframework/data/r2dbc/testing/PostgresTestSupport.java

+7-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.springframework.data.r2dbc.testing;
22

3-
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
4-
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
53
import io.r2dbc.spi.ConnectionFactory;
64

75
import java.util.function.Supplier;
@@ -10,7 +8,9 @@
108
import javax.sql.DataSource;
119

1210
import org.postgresql.ds.PGSimpleDataSource;
11+
1312
import org.springframework.data.r2dbc.testing.ExternalDatabase.ProvidedDatabase;
13+
1414
import org.testcontainers.containers.PostgreSQLContainer;
1515

1616
/**
@@ -87,15 +87,10 @@ private static ExternalDatabase testContainer() {
8787
if (testContainerDatabase == null) {
8888

8989
try {
90-
PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();
91-
postgreSQLContainer.start();
90+
PostgreSQLContainer container = new PostgreSQLContainer();
91+
container.start();
9292

93-
testContainerDatabase = ProvidedDatabase.builder() //
94-
.hostname("localhost") //
95-
.port(postgreSQLContainer.getFirstMappedPort()) //
96-
.database(postgreSQLContainer.getDatabaseName()) //
97-
.username(postgreSQLContainer.getUsername()) //
98-
.password(postgreSQLContainer.getPassword()).build();
93+
testContainerDatabase = ProvidedDatabase.from(container);
9994

10095
} catch (IllegalStateException ise) {
10196
// docker not available.
@@ -111,14 +106,7 @@ private static ExternalDatabase testContainer() {
111106
* Creates a new {@link ConnectionFactory} configured from the {@link ExternalDatabase}..
112107
*/
113108
public static ConnectionFactory createConnectionFactory(ExternalDatabase database) {
114-
115-
return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder() //
116-
.host(database.getHostname()) //
117-
.database(database.getDatabase()) //
118-
.port(database.getPort()) //
119-
.username(database.getUsername()) //
120-
.password(database.getPassword()) //
121-
.build());
109+
return ConnectionUtils.getConnectionFactory("postgresql", database);
122110
}
123111

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

131119
dataSource.setUser(database.getUsername());
132120
dataSource.setPassword(database.getPassword());
133-
dataSource.setDatabaseName(database.getDatabase());
134-
dataSource.setServerName(database.getHostname());
135-
dataSource.setPortNumber(database.getPort());
121+
dataSource.setURL(database.getJdbcUrl());
136122

137123
return dataSource;
138124
}
139-
140125
}

0 commit comments

Comments
 (0)