Skip to content

Commit d9dfb03

Browse files
eddumelendezsnicoll
authored andcommitted
Add container support for ClickHouse
See gh-42837
1 parent c6619dd commit d9dfb03

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc

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

8686
| `R2dbcConnectionDetails`
87-
| Containers of type `MariaDBContainer`, `MSSQLServerContainer`, `MySQLContainer`, `OracleContainer`, or `PostgreSQLContainer`
87+
| Containers of type `ClickHouseContainer`, `MariaDBContainer`, `MSSQLServerContainer`, `MySQLContainer`, `OracleContainer`, or `PostgreSQLContainer`
8888

8989
| `RabbitConnectionDetails`
9090
| Containers of type `RabbitMQContainer`

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ dependencies {
6464
optional("org.springframework.data:spring-data-neo4j")
6565
optional("org.testcontainers:activemq")
6666
optional("org.testcontainers:cassandra")
67+
optional("org.testcontainers:clickhouse")
6768
optional("org.testcontainers:couchbase")
6869
optional("org.testcontainers:elasticsearch")
6970
optional("org.testcontainers:grafana")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2012-2024 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.clickhouse.ClickHouseContainer;
21+
import org.testcontainers.clickhouse.ClickHouseR2DBCDatabaseContainer;
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 ClickHouseContainer}.
31+
*
32+
* @author Eddú Meléndez
33+
*/
34+
class ClickHouseR2dbcContainerConnectionDetailsFactory
35+
extends ContainerConnectionDetailsFactory<ClickHouseContainer, R2dbcConnectionDetails> {
36+
37+
ClickHouseR2dbcContainerConnectionDetailsFactory() {
38+
super(ANY_CONNECTION_NAME, "io.r2dbc.spi.ConnectionFactoryOptions");
39+
}
40+
41+
@Override
42+
public R2dbcConnectionDetails getContainerConnectionDetails(ContainerConnectionSource<ClickHouseContainer> source) {
43+
return new ClickHouseR2dbcDatabaseContainerConnectionDetails(source);
44+
}
45+
46+
/**
47+
* {@link R2dbcConnectionDetails} backed by a {@link ContainerConnectionSource}.
48+
*/
49+
private static final class ClickHouseR2dbcDatabaseContainerConnectionDetails
50+
extends ContainerConnectionDetails<ClickHouseContainer> implements R2dbcConnectionDetails {
51+
52+
private ClickHouseR2dbcDatabaseContainerConnectionDetails(
53+
ContainerConnectionSource<ClickHouseContainer> source) {
54+
super(source);
55+
}
56+
57+
@Override
58+
public ConnectionFactoryOptions getConnectionFactoryOptions() {
59+
return ClickHouseR2DBCDatabaseContainer.getOptions(getContainer());
60+
}
61+
62+
}
63+
64+
}

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
@@ -33,6 +33,7 @@ org.springframework.boot.testcontainers.service.connection.otlp.OpenTelemetryLog
3333
org.springframework.boot.testcontainers.service.connection.otlp.OpenTelemetryMetricsContainerConnectionDetailsFactory,\
3434
org.springframework.boot.testcontainers.service.connection.otlp.OpenTelemetryTracingContainerConnectionDetailsFactory,\
3535
org.springframework.boot.testcontainers.service.connection.pulsar.PulsarContainerConnectionDetailsFactory,\
36+
org.springframework.boot.testcontainers.service.connection.r2dbc.ClickHouseR2dbcContainerConnectionDetailsFactory,\
3637
org.springframework.boot.testcontainers.service.connection.r2dbc.MariaDbR2dbcContainerConnectionDetailsFactory,\
3738
org.springframework.boot.testcontainers.service.connection.r2dbc.MySqlR2dbcContainerConnectionDetailsFactory,\
3839
org.springframework.boot.testcontainers.service.connection.r2dbc.OracleFreeR2dbcContainerConnectionDetailsFactory,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2024 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.junit.jupiter.api.Test;
21+
22+
import org.springframework.aot.hint.RuntimeHints;
23+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
24+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactoryHints;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link ClickHouseR2dbcContainerConnectionDetailsFactory}.
30+
*
31+
* @author Eddú Meléndez
32+
*/
33+
class ClickHouseR2dbcContainerConnectionDetailsFactoryTests {
34+
35+
@Test
36+
void shouldRegisterHints() {
37+
RuntimeHints hints = ContainerConnectionDetailsFactoryHints.getRegisteredHints(getClass().getClassLoader());
38+
assertThat(RuntimeHintsPredicates.reflection().onType(ConnectionFactoryOptions.class)).accepts(hints);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)