Skip to content

Commit 44f7721

Browse files
committed
Reinstate integration tests for R2DBC MySQL.
Closes #1475
1 parent 21428af commit 44f7721

File tree

4 files changed

+278
-1
lines changed

4 files changed

+278
-1
lines changed

spring-data-r2dbc/pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<r2dbc-h2.version>1.0.0.RELEASE</r2dbc-h2.version>
3131
<r2dbc-mariadb.version>1.1.3</r2dbc-mariadb.version>
3232
<r2dbc-mssql.version>1.0.0.RELEASE</r2dbc-mssql.version>
33+
<r2dbc-mysql.version>1.0.0</r2dbc-mysql.version>
3334
<oracle-r2dbc.version>1.0.0</oracle-r2dbc.version>
3435
<r2dbc-spi.version>1.0.0.RELEASE</r2dbc-spi.version>
3536
<reactive-streams.version>1.0.4</reactive-streams.version>
@@ -204,6 +205,13 @@
204205
<scope>test</scope>
205206
</dependency>
206207

208+
<dependency>
209+
<groupId>mysql</groupId>
210+
<artifactId>mysql-connector-java</artifactId>
211+
<version>${mysql-connector-java.version}</version>
212+
<scope>test</scope>
213+
</dependency>
214+
207215
<dependency>
208216
<groupId>com.oracle.database.jdbc</groupId>
209217
<artifactId>ojdbc11</artifactId>
@@ -241,6 +249,13 @@
241249
<scope>test</scope>
242250
</dependency>
243251

252+
<dependency>
253+
<groupId>io.asyncer</groupId>
254+
<artifactId>r2dbc-mysql</artifactId>
255+
<version>${r2dbc-mysql.version}</version>
256+
<scope>test</scope>
257+
</dependency>
258+
244259
<dependency>
245260
<groupId>com.oracle.database.r2dbc</groupId>
246261
<artifactId>oracle-r2dbc</artifactId>

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/dialect/DialectResolverUnitTests.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static org.assertj.core.api.Assertions.*;
44

5+
import io.asyncer.r2dbc.mysql.MySqlConnectionConfiguration;
6+
import io.asyncer.r2dbc.mysql.MySqlConnectionFactory;
57
import io.r2dbc.h2.H2ConnectionConfiguration;
68
import io.r2dbc.h2.H2ConnectionFactory;
79
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
@@ -30,18 +32,21 @@
3032
*/
3133
public class DialectResolverUnitTests {
3234

33-
@Test // gh-20, gh-104
35+
@Test // GH-20, GH-104, GH-1475
3436
void shouldResolveDatabaseType() {
3537

3638
PostgresqlConnectionFactory postgres = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
3739
.host("localhost").database("foo").username("bar").password("password").build());
3840
H2ConnectionFactory h2 = new H2ConnectionFactory(H2ConnectionConfiguration.builder().inMemory("mem").build());
3941
MariadbConnectionFactory mariadb = new MariadbConnectionFactory(
4042
MariadbConnectionConfiguration.builder().socket("/foo").username("bar").build());
43+
MySqlConnectionFactory mysql = MySqlConnectionFactory
44+
.from(MySqlConnectionConfiguration.builder().host("foo").username("bar").build());
4145

4246
assertThat(DialectResolver.getDialect(postgres)).isEqualTo(PostgresDialect.INSTANCE);
4347
assertThat(DialectResolver.getDialect(h2)).isEqualTo(H2Dialect.INSTANCE);
4448
assertThat(DialectResolver.getDialect(mariadb)).isEqualTo(MySqlDialect.INSTANCE);
49+
assertThat(DialectResolver.getDialect(mysql)).isEqualTo(MySqlDialect.INSTANCE);
4550
}
4651

4752
@Test // gh-20, gh-104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2019-2023 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.repository;
17+
18+
import io.r2dbc.spi.ConnectionFactory;
19+
import reactor.core.publisher.Flux;
20+
import reactor.core.publisher.Mono;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.junit.jupiter.api.extension.RegisterExtension;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.ComponentScan.Filter;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.context.annotation.FilterType;
30+
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
31+
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
32+
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
33+
import org.springframework.data.r2dbc.testing.ExternalDatabase;
34+
import org.springframework.data.r2dbc.testing.MySqlDbTestSupport;
35+
import org.springframework.test.context.ContextConfiguration;
36+
import org.springframework.test.context.junit.jupiter.SpringExtension;
37+
38+
/**
39+
* Integration tests for {@link LegoSetRepository} using {@link R2dbcRepositoryFactory} against MySQL.
40+
*
41+
* @author Mark Paluch
42+
*/
43+
@ExtendWith(SpringExtension.class)
44+
@ContextConfiguration
45+
public class MySqlR2dbcRepositoryIntegrationTests extends AbstractR2dbcRepositoryIntegrationTests {
46+
47+
@RegisterExtension public static final ExternalDatabase database = MySqlDbTestSupport.database();
48+
49+
@Configuration
50+
@EnableR2dbcRepositories(considerNestedRepositories = true,
51+
includeFilters = @Filter(classes = MySqlLegoSetRepository.class, type = FilterType.ASSIGNABLE_TYPE))
52+
static class IntegrationTestConfiguration extends AbstractR2dbcConfiguration {
53+
54+
@Bean
55+
@Override
56+
public ConnectionFactory connectionFactory() {
57+
return MySqlDbTestSupport.createConnectionFactory(database);
58+
}
59+
}
60+
61+
@Override
62+
protected DataSource createDataSource() {
63+
return MySqlDbTestSupport.createDataSource(database);
64+
}
65+
66+
@Override
67+
protected ConnectionFactory createConnectionFactory() {
68+
return MySqlDbTestSupport.createConnectionFactory(database);
69+
}
70+
71+
@Override
72+
protected String getCreateTableStatement() {
73+
return MySqlDbTestSupport.CREATE_TABLE_LEGOSET_WITH_ID_GENERATION;
74+
}
75+
76+
@Override
77+
protected Class<? extends LegoSetRepository> getRepositoryInterfaceType() {
78+
return MySqlLegoSetRepository.class;
79+
}
80+
81+
interface MySqlLegoSetRepository extends LegoSetRepository {
82+
83+
@Override
84+
@Query("SELECT name FROM legoset")
85+
Flux<Named> findAsProjection();
86+
87+
@Override
88+
@Query("SELECT * FROM legoset WHERE manual = :manual")
89+
Mono<LegoSet> findByManual(int manual);
90+
91+
@Override
92+
@Query("SELECT id FROM legoset")
93+
Flux<Integer> findAllIds();
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright 2019-2023 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 io.asyncer.r2dbc.mysql.MySqlConnectionFactoryProvider;
19+
import io.r2dbc.spi.ConnectionFactory;
20+
import io.r2dbc.spi.ConnectionFactoryOptions;
21+
import lombok.SneakyThrows;
22+
23+
import java.util.function.Supplier;
24+
import java.util.stream.Stream;
25+
26+
import javax.sql.DataSource;
27+
28+
import org.springframework.data.r2dbc.testing.ExternalDatabase.ProvidedDatabase;
29+
import org.testcontainers.containers.MySQLContainer;
30+
31+
import com.mysql.cj.jdbc.MysqlDataSource;
32+
33+
/**
34+
* Utility class for testing against MySQL.
35+
*
36+
* @author Mark Paluch
37+
* @author Jens Schauder
38+
*/
39+
public class MySqlDbTestSupport {
40+
41+
private static ExternalDatabase testContainerDatabase;
42+
43+
public static final String CREATE_TABLE_LEGOSET = "CREATE TABLE legoset (\n" //
44+
+ " id integer PRIMARY KEY,\n" //
45+
+ " name varchar(255) NOT NULL,\n" //
46+
+ " manual integer NULL\n," //
47+
+ " cert varbinary(255) NULL\n" //
48+
+ ") ENGINE=InnoDB;";
49+
50+
public static final String CREATE_TABLE_LEGOSET_WITH_ID_GENERATION = "CREATE TABLE legoset (\n" //
51+
+ " id integer AUTO_INCREMENT PRIMARY KEY,\n" //
52+
+ " name varchar(255) NOT NULL,\n" //
53+
+ " flag boolean NOT NULL,\n" //
54+
+ " manual integer NULL\n" //
55+
+ ") ENGINE=InnoDB;";
56+
57+
public static final String CREATE_TABLE_LEGOSET_WITH_MIXED_CASE_NAMES = "CREATE TABLE `LegoSet` (\n" //
58+
+ " `Id` integer AUTO_INCREMENT PRIMARY KEY,\n" //
59+
+ " `Name` varchar(255) NOT NULL,\n" //
60+
+ " `Manual` integer NULL\n" //
61+
+ ") ENGINE=InnoDB;";
62+
63+
public static final String DROP_TABLE_LEGOSET_WITH_MIXED_CASE_NAMES = "DROP TABLE `LegoSet`";
64+
65+
/**
66+
* Returns a database either hosted locally at {@code localhost:3306/mysql} or running inside Docker.
67+
*
68+
* @return information about the database. Guaranteed to be not {@literal null}.
69+
*/
70+
public static ExternalDatabase database() {
71+
72+
if (Boolean.getBoolean("spring.data.r2dbc.test.preferLocalDatabase")) {
73+
74+
return getFirstWorkingDatabase( //
75+
MySqlDbTestSupport::local, //
76+
MySqlDbTestSupport::testContainer //
77+
);
78+
} else {
79+
80+
return getFirstWorkingDatabase( //
81+
MySqlDbTestSupport::testContainer, //
82+
MySqlDbTestSupport::local //
83+
);
84+
}
85+
}
86+
87+
@SafeVarargs
88+
private static ExternalDatabase getFirstWorkingDatabase(Supplier<ExternalDatabase>... suppliers) {
89+
90+
return Stream.of(suppliers).map(Supplier::get) //
91+
.filter(ExternalDatabase::checkValidity) //
92+
.findFirst() //
93+
.orElse(ExternalDatabase.unavailable());
94+
}
95+
96+
/**
97+
* Returns a locally provided database .
98+
*/
99+
private static ExternalDatabase local() {
100+
101+
return ProvidedDatabase.builder() //
102+
.hostname("localhost") //
103+
.port(3306) //
104+
.database("mysql") //
105+
.username("root") //
106+
.password("my-secret-pw") //
107+
.jdbcUrl("jdbc:mysql://localhost:3306/mysql") //
108+
.build();
109+
}
110+
111+
/**
112+
* Returns a database provided via Testcontainers.
113+
*/
114+
private static ExternalDatabase testContainer() {
115+
116+
if (testContainerDatabase == null) {
117+
118+
try {
119+
120+
var container = new MySQLContainer<>("mysql:8.0.32").withUsername("test").withPassword("test")
121+
.withConfigurationOverride("");
122+
123+
container.start();
124+
125+
testContainerDatabase = ProvidedDatabase.builder(container) //
126+
.username("root") //
127+
.database(container.getDatabaseName()) //
128+
.build();
129+
} catch (IllegalStateException ise) {
130+
// docker not available.
131+
testContainerDatabase = ExternalDatabase.unavailable();
132+
}
133+
}
134+
135+
return testContainerDatabase;
136+
}
137+
138+
/**
139+
* Creates a new R2DBC MySQL {@link ConnectionFactory} configured from the {@link ExternalDatabase}.
140+
*/
141+
public static ConnectionFactory createConnectionFactory(ExternalDatabase database) {
142+
143+
ConnectionFactoryOptions options = ConnectionUtils.createOptions("mysql", database);
144+
return new MySqlConnectionFactoryProvider().create(options);
145+
}
146+
147+
/**
148+
* Creates a new {@link DataSource} configured from the {@link ExternalDatabase}.
149+
*/
150+
@SneakyThrows
151+
public static DataSource createDataSource(ExternalDatabase database) {
152+
153+
MysqlDataSource dataSource = new MysqlDataSource();
154+
155+
dataSource.setUser(database.getUsername());
156+
dataSource.setPassword(database.getPassword());
157+
dataSource.setUrl(
158+
String.format("jdbc:mysql://%s:%d/%s?", database.getHostname(), database.getPort(), database.getDatabase()));
159+
160+
return dataSource;
161+
}
162+
}

0 commit comments

Comments
 (0)