Merge branch '3.1.x' into 3.2.x

Closes gh-40834
This commit is contained in:
Andy Wilkinson 2024-05-20 13:12:03 +01:00
commit f440f2c3ac
10 changed files with 533 additions and 0 deletions

View File

@ -39,6 +39,11 @@ dependencies {
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
testImplementation(project(":spring-boot-project:spring-boot-test"))
testImplementation("ch.qos.logback:logback-classic")
testImplementation("co.elastic.clients:elasticsearch-java") {
exclude group: "commons-logging", module: "commons-logging"
}
testImplementation("com.couchbase.client:java-client")
testImplementation("com.datastax.oss:java-driver-core")
testImplementation("io.micrometer:micrometer-registry-otlp")
testImplementation("io.rest-assured:rest-assured") {
exclude group: "commons-logging", module: "commons-logging"
@ -46,19 +51,28 @@ dependencies {
testImplementation("org.apache.activemq:activemq-client-jakarta")
testImplementation("org.assertj:assertj-core")
testImplementation("org.awaitility:awaitility")
testImplementation("org.flywaydb:flyway-core")
testImplementation("org.influxdb:influxdb-java")
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.junit.platform:junit-platform-engine")
testImplementation("org.junit.platform:junit-platform-launcher")
testImplementation("org.liquibase:liquibase-core") {
exclude(group: "javax.xml.bind", module: "jaxb-api")
}
testImplementation("org.mockito:mockito-core")
testImplementation("org.mockito:mockito-junit-jupiter")
testImplementation("org.springframework:spring-core-test")
testImplementation("org.springframework:spring-jdbc")
testImplementation("org.springframework:spring-jms")
testImplementation("org.springframework:spring-r2dbc")
testImplementation("org.springframework.amqp:spring-rabbit")
testImplementation("org.springframework.data:spring-data-redis")
testImplementation("org.springframework.kafka:spring-kafka")
testImplementation("org.springframework.pulsar:spring-pulsar")
testImplementation("org.testcontainers:junit-jupiter")
testRuntimeOnly("com.oracle.database.r2dbc:oracle-r2dbc")
testRuntimeOnly("com.zaxxer:HikariCP")
testRuntimeOnly("io.lettuce:lettuce-core")
testRuntimeOnly("org.postgresql:postgresql")
}

View File

@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
import org.springframework.boot.testcontainers.beans.TestcontainerBeanDefinition;
import org.springframework.boot.testcontainers.lifecycle.TestcontainersLifecycleApplicationContextInitializer;
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable;
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -81,6 +82,7 @@ class ServiceConnectionAutoConfigurationTests {
}
@Test
@ClassPathExclusions("lettuce-core-*.jar")
void whenHasUserConfigurationDoesNotRegisterReplacement() {
try (AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext()) {
applicationContext.register(UserConfiguration.class, WithRedisAutoConfiguration.class,

View File

@ -0,0 +1,66 @@
/*
* Copyright 2012-2024 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.boot.testcontainers.service.connection.cassandra;
import com.datastax.oss.driver.api.core.CqlSession;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.cassandra.CassandraConnectionDetails;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.CassandraContainer;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link CassandraContainerConnectionDetailsFactory}.
*
* @author Andy Wilkinson
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class CassandraContainerConnectionDetailsFactoryTests {
@Container
@ServiceConnection
static final CassandraContainer cassandra = new CassandraContainer();
@Autowired(required = false)
private CassandraConnectionDetails connectionDetails;
@Autowired
private CqlSession cqlSession;
@Test
void connectionCanBeMadeToCassandraContainer() {
assertThat(this.connectionDetails).isNotNull();
assertThat(this.cqlSession.getMetadata().getNodes()).hasSize(1);
}
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(CassandraAutoConfiguration.class)
static class TestConfiguration {
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright 2012-2024 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.boot.testcontainers.service.connection.couchbase;
import java.time.Duration;
import com.couchbase.client.java.Cluster;
import org.junit.jupiter.api.Test;
import org.testcontainers.couchbase.BucketDefinition;
import org.testcontainers.couchbase.CouchbaseContainer;
import org.testcontainers.couchbase.CouchbaseService;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseConnectionDetails;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link CouchbaseContainerConnectionDetailsFactory}.
*
* @author Andy Wilkinson
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class CouchbaseContainerConnectionDetailsFactoryTests {
@Container
@ServiceConnection
static final CouchbaseContainer couchbase = new CouchbaseContainer(DockerImageNames.couchbase())
.withEnabledServices(CouchbaseService.KV, CouchbaseService.INDEX, CouchbaseService.QUERY)
.withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(10))
.withBucket(new BucketDefinition("cbbucket"));
@Autowired(required = false)
private CouchbaseConnectionDetails connectionDetails;
@Autowired
private Cluster cluster;
@Test
void connectionCanBeMadeToCouchbaseContainer() {
assertThat(this.connectionDetails).isNotNull();
assertThat(this.cluster.diagnostics().endpoints()).hasSize(1);
}
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(CouchbaseAutoConfiguration.class)
static class TestConfiguration {
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright 2012-2024 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.boot.testcontainers.service.connection.elasticsearch;
import java.io.IOException;
import java.time.Duration;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import org.junit.jupiter.api.Test;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientAutoConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchConnectionDetails;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ElasticsearchContainerConnectionDetailsFactory}.
*
* @author Andy Wilkinson
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class ElasticsearchContainerConnectionDetailsFactoryTests {
@Container
@ServiceConnection
static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(DockerImageNames.elasticsearch())
.withEnv("ES_JAVA_OPTS", "-Xms32m -Xmx512m")
.withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(10));
@Autowired(required = false)
private ElasticsearchConnectionDetails connectionDetails;
@Autowired
private ElasticsearchClient client;
@Test
void connectionCanBeMadeToElasticsearchContainer() throws IOException {
assertThat(this.connectionDetails).isNotNull();
assertThat(this.client.cluster().health().numberOfNodes()).isEqualTo(1);
}
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration({ ElasticsearchClientAutoConfiguration.class,
ElasticsearchRestClientAutoConfiguration.class })
static class TestConfiguration {
}
}

View File

@ -0,0 +1,70 @@
/*
* Copyright 2012-2024 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.boot.testcontainers.service.connection.flyway;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Tests for {@link FlywayContainerConnectionDetailsFactory}.
*
* @author Andy Wilkinson
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class FlywayContainerConnectionDetailsFactoryTests {
@Container
@ServiceConnection
static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(DockerImageNames.postgresql());
@Autowired(required = false)
private JdbcConnectionDetails connectionDetails;
@Autowired
private Flyway flyway;
@Test
void connectionCanBeMadeToJdbcContainer() {
assertThat(this.connectionDetails).isNotNull();
JdbcTemplate jdbc = new JdbcTemplate(this.flyway.getConfiguration().getDataSource());
assertThatNoException().isThrownBy(() -> jdbc.execute("SELECT * from public.flyway_schema_history"));
}
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(FlywayAutoConfiguration.class)
static class TestConfiguration {
}
}

View File

@ -0,0 +1,72 @@
/*
* Copyright 2012-2024 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.boot.testcontainers.service.connection.jdbc;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Tests for {@link JdbcContainerConnectionDetailsFactory}.
*
* @author Andy Wilkinson
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class JdbcContainerConnectionDetailsFactoryTests {
@Container
@ServiceConnection
static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(DockerImageNames.postgresql());
@Autowired(required = false)
private JdbcConnectionDetails connectionDetails;
@Autowired
private DataSource dataSource;
@Test
void connectionCanBeMadeToJdbcContainer() {
assertThat(this.connectionDetails).isNotNull();
JdbcTemplate jdbc = new JdbcTemplate(this.dataSource);
assertThatNoException().isThrownBy(() -> jdbc.execute(DatabaseDriver.POSTGRESQL.getValidationQuery()));
}
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(DataSourceAutoConfiguration.class)
static class TestConfiguration {
}
}

View File

@ -0,0 +1,70 @@
/*
* Copyright 2012-2024 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.boot.testcontainers.service.connection.liquibase;
import liquibase.integration.spring.SpringLiquibase;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Tests for {@link LiquibaseContainerConnectionDetailsFactory}.
*
* @author Andy Wilkinson
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class LiquibaseContainerConnectionDetailsFactoryTests {
@Container
@ServiceConnection
static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(DockerImageNames.postgresql());
@Autowired(required = false)
private JdbcConnectionDetails connectionDetails;
@Autowired
private SpringLiquibase liquibase;
@Test
void connectionCanBeMadeToJdbcContainer() {
assertThat(this.connectionDetails).isNotNull();
JdbcTemplate jdbc = new JdbcTemplate(this.liquibase.getDataSource());
assertThatNoException().isThrownBy(() -> jdbc.execute("SELECT * from example"));
}
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(LiquibaseAutoConfiguration.class)
static class TestConfiguration {
}
}

View File

@ -0,0 +1,69 @@
/*
* Copyright 2012-2024 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.boot.testcontainers.service.connection.redis;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link RedisContainerConnectionDetailsFactory}.
*
* @author Andy Wilkinson
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class RedisContainerConnectionDetailsFactoryTests {
@Container
@ServiceConnection
static final RedisContainer redis = new RedisContainer();
@Autowired(required = false)
private RedisConnectionDetails connectionDetails;
@Autowired
private RedisConnectionFactory connectionFactory;
@Test
void connectionCanBeMadeToRedisContainer() {
assertThat(this.connectionDetails).isNotNull();
try (RedisConnection connection = this.connectionFactory.getConnection()) {
assertThat(connection.commands().echo("Hello, World".getBytes())).isEqualTo("Hello, World".getBytes());
}
}
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration(RedisAutoConfiguration.class)
static class TestConfiguration {
}
}

View File

@ -0,0 +1,20 @@
databaseChangeLog:
- changeSet:
id: 1
author: wilkinsona
changes:
- createTable:
tableName: example
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: name
type: varchar(50)
constraints:
nullable: false