Skip to content

Commit 73a63a1

Browse files
committed
Merge pull request #34852 from eddumelendez
* gh-34852: Polish "Add support for Oracle R2DBC Service Connection" Add support for Oracle R2DBC Service Connection Closes gh-34852
2 parents 4ee1bb0 + 0da209d commit 73a63a1

File tree

6 files changed

+147
-1
lines changed

6 files changed

+147
-1
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ The following service connection factories are provided in the `spring-boot-test
971971
| Containers of type `Neo4jContainer`
972972

973973
| `R2dbcConnectionDetails`
974-
| Containers of type `MariaDBContainer`, `MSSQLServerContainer`, `MySQLContainer` or `PostgreSQLContainer`
974+
| Containers of type `MariaDBContainer`, `MSSQLServerContainer`, `MySQLContainer`, `OracleContainer`, or `PostgreSQLContainer`
975975

976976
| `RabbitConnectionDetails`
977977
| Containers of type `RabbitMQContainer`

spring-boot-project/spring-boot-testcontainers/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies {
2525
optional("org.testcontainers:mssqlserver")
2626
optional("org.testcontainers:mysql")
2727
optional("org.testcontainers:neo4j")
28+
optional("org.testcontainers:oracle-xe")
2829
optional("org.testcontainers:postgresql")
2930
optional("org.testcontainers:rabbitmq")
3031
optional("org.testcontainers:redpanda")
@@ -41,8 +42,11 @@ dependencies {
4142
testImplementation("org.mockito:mockito-core")
4243
testImplementation("org.mockito:mockito-junit-jupiter")
4344
testImplementation("org.springframework:spring-core-test")
45+
testImplementation("org.springframework:spring-r2dbc")
4446
testImplementation("org.springframework.amqp:spring-rabbit")
4547
testImplementation("org.springframework.kafka:spring-kafka")
4648
testImplementation("org.testcontainers:junit-jupiter")
49+
50+
testRuntimeOnly("com.oracle.database.r2dbc:oracle-r2dbc")
4751
}
4852

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-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+
17+
package org.springframework.boot.testcontainers.service.connection.r2dbc;
18+
19+
import io.r2dbc.spi.ConnectionFactoryOptions;
20+
import org.testcontainers.containers.OracleContainer;
21+
import org.testcontainers.containers.OracleR2DBCDatabaseContainer;
22+
23+
import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
24+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
25+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
26+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
27+
28+
/**
29+
* {@link ContainerConnectionDetailsFactory} to create {@link R2dbcConnectionDetails} from
30+
* a {@link ServiceConnection @ServiceConnection}-annotated {@link OracleContainer}.
31+
*
32+
* @author Eddú Meléndez
33+
*/
34+
class OracleR2dbcContainerConnectionDetailsFactory
35+
extends ContainerConnectionDetailsFactory<R2dbcConnectionDetails, OracleContainer> {
36+
37+
OracleR2dbcContainerConnectionDetailsFactory() {
38+
super(ANY_CONNECTION_NAME, "io.r2dbc.spi.ConnectionFactoryOptions");
39+
}
40+
41+
@Override
42+
public R2dbcConnectionDetails getContainerConnectionDetails(ContainerConnectionSource<OracleContainer> source) {
43+
return new R2dbcDatabaseContainerConnectionDetails(source);
44+
}
45+
46+
/**
47+
* {@link R2dbcConnectionDetails} backed by a {@link ContainerConnectionSource}.
48+
*/
49+
private static final class R2dbcDatabaseContainerConnectionDetails extends ContainerConnectionDetails
50+
implements R2dbcConnectionDetails {
51+
52+
private final OracleContainer container;
53+
54+
private R2dbcDatabaseContainerConnectionDetails(ContainerConnectionSource<OracleContainer> source) {
55+
super(source);
56+
this.container = source.getContainer();
57+
}
58+
59+
@Override
60+
public ConnectionFactoryOptions getConnectionFactoryOptions() {
61+
return OracleR2DBCDatabaseContainer.getOptions(this.container);
62+
}
63+
64+
}
65+
66+
}

spring-boot-project/spring-boot-testcontainers/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ org.springframework.boot.testcontainers.service.connection.neo4j.Neo4jContainerC
2222
org.springframework.boot.testcontainers.service.connection.r2dbc.MariaDbR2dbcContainerConnectionDetailsFactory,\
2323
org.springframework.boot.testcontainers.service.connection.r2dbc.MsSqlServerR2dbcContainerConnectionDetailsFactory,\
2424
org.springframework.boot.testcontainers.service.connection.r2dbc.MySqlR2dbcContainerConnectionDetailsFactory,\
25+
org.springframework.boot.testcontainers.service.connection.r2dbc.OracleR2dbcContainerConnectionDetailsFactory,\
2526
org.springframework.boot.testcontainers.service.connection.r2dbc.PostgresR2dbcContainerConnectionDetailsFactory,\
2627
org.springframework.boot.testcontainers.service.connection.redis.RedisContainerConnectionDetailsFactory,\
2728
org.springframework.boot.testcontainers.service.connection.redpanda.RedpandaContainerConnectionDetailsFactory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2012-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+
17+
package org.springframework.boot.testcontainers.service.connection.r2dbc;
18+
19+
import io.r2dbc.spi.ConnectionFactory;
20+
import org.junit.jupiter.api.Test;
21+
import org.testcontainers.containers.OracleContainer;
22+
import org.testcontainers.junit.jupiter.Container;
23+
import org.testcontainers.junit.jupiter.Testcontainers;
24+
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration;
28+
import org.springframework.boot.jdbc.DatabaseDriver;
29+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
30+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.r2dbc.core.DatabaseClient;
33+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
/**
38+
* Tests for {@link OracleR2dbcContainerConnectionDetailsFactory}.
39+
*
40+
* @author Andy Wilkinson
41+
*/
42+
@SpringJUnitConfig
43+
@Testcontainers(disabledWithoutDocker = true)
44+
class OracleR2dbcContainerConnectionDetailsFactoryTests {
45+
46+
@Container
47+
@ServiceConnection
48+
static final OracleContainer oracle = new OracleContainer(DockerImageNames.oracleXe());
49+
50+
@Autowired
51+
ConnectionFactory connectionFactory;
52+
53+
@Test
54+
void connectionCanBeMadeToOracleContainer() {
55+
Object result = DatabaseClient.create(this.connectionFactory)
56+
.sql(DatabaseDriver.ORACLE.getValidationQuery())
57+
.map((row, metadata) -> row.get(0))
58+
.first()
59+
.block();
60+
assertThat(result).isEqualTo("Hello");
61+
}
62+
63+
@Configuration(proxyBeanMethods = false)
64+
@ImportAutoConfiguration(R2dbcAutoConfiguration.class)
65+
static class TestConfiguration {
66+
67+
}
68+
69+
}

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/testcontainers/DockerImageNames.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public final class DockerImageNames {
4141

4242
private static final String NEO4J_VERSION = "4.4.11";
4343

44+
private static final String ORACLE_XE_VERSION = "18.4.0-slim";
45+
4446
private static final String POSTGRESQL_VERSION = "14.0";
4547

4648
private static final String RABBIT_VERSION = "3.11-alpine";
@@ -110,6 +112,10 @@ public static DockerImageName neo4j() {
110112
return DockerImageName.parse("neo4j").withTag(NEO4J_VERSION);
111113
}
112114

115+
public static DockerImageName oracleXe() {
116+
return DockerImageName.parse("gvenzl/oracle-xe").withTag(ORACLE_XE_VERSION);
117+
}
118+
113119
/**
114120
* Return a {@link DockerImageName} suitable for running PostgreSQL.
115121
* @return a docker image name for running postgresql

0 commit comments

Comments
 (0)