Skip to content

Commit d1ae453

Browse files
committed
Restore support of deprecated containers
This commit restore the service connection support for Testcontainers implementations that were deprecated as part of TC 2.0. Previously, only the new location was taken into account. Closes gh-47796
1 parent f72da4c commit d1ae453

File tree

18 files changed

+914
-0
lines changed

18 files changed

+914
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2012-present 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.amqp.testcontainers;
18+
19+
import java.time.Duration;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import org.awaitility.Awaitility;
24+
import org.junit.jupiter.api.Test;
25+
import org.testcontainers.containers.RabbitMQContainer;
26+
import org.testcontainers.junit.jupiter.Container;
27+
import org.testcontainers.junit.jupiter.Testcontainers;
28+
29+
import org.springframework.amqp.rabbit.annotation.Queue;
30+
import org.springframework.amqp.rabbit.annotation.RabbitListener;
31+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
32+
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.boot.amqp.autoconfigure.RabbitAutoConfiguration;
34+
import org.springframework.boot.amqp.autoconfigure.RabbitConnectionDetails;
35+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
36+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
37+
import org.springframework.boot.testsupport.container.TestImage;
38+
import org.springframework.context.annotation.Bean;
39+
import org.springframework.context.annotation.Configuration;
40+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
41+
42+
import static org.assertj.core.api.Assertions.assertThat;
43+
44+
/**
45+
* Tests for {@link DeprecatedRabbitContainerConnectionDetailsFactory}.
46+
*
47+
* @author Moritz Halbritter
48+
* @author Andy Wilkinson
49+
* @author Phillip Webb
50+
* @deprecated since 4.0.0 for removal in 4.2.0 in favor of
51+
* {@link RabbitContainerConnectionDetailsFactoryIntegrationTests}.
52+
*/
53+
@SpringJUnitConfig
54+
@Testcontainers(disabledWithoutDocker = true)
55+
@Deprecated(since = "4.0.0", forRemoval = true)
56+
class DeprecatedRabbitContainerConnectionDetailsFactoryIntegrationTests {
57+
58+
@Container
59+
@ServiceConnection
60+
static final RabbitMQContainer rabbit = TestImage.container(RabbitMQContainer.class);
61+
62+
@Autowired(required = false)
63+
private RabbitConnectionDetails connectionDetails;
64+
65+
@Autowired
66+
private RabbitTemplate rabbitTemplate;
67+
68+
@Autowired
69+
private TestListener listener;
70+
71+
@Test
72+
void connectionCanBeMadeToRabbitContainer() {
73+
assertThat(this.connectionDetails).isNotNull();
74+
this.rabbitTemplate.convertAndSend("test", "message");
75+
Awaitility.waitAtMost(Duration.ofMinutes(4))
76+
.untilAsserted(() -> assertThat(this.listener.messages).containsExactly("message"));
77+
78+
}
79+
80+
@Configuration(proxyBeanMethods = false)
81+
@ImportAutoConfiguration(RabbitAutoConfiguration.class)
82+
static class TestConfiguration {
83+
84+
@Bean
85+
TestListener testListener() {
86+
return new TestListener();
87+
}
88+
89+
}
90+
91+
static class TestListener {
92+
93+
private final List<String> messages = new ArrayList<>();
94+
95+
@RabbitListener(queuesToDeclare = @Queue("test"))
96+
void processMessage(String message) {
97+
this.messages.add(message);
98+
}
99+
100+
}
101+
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2012-present 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.amqp.testcontainers;
18+
19+
import java.net.URI;
20+
import java.util.List;
21+
22+
import org.jspecify.annotations.Nullable;
23+
import org.testcontainers.containers.RabbitMQContainer;
24+
25+
import org.springframework.boot.amqp.autoconfigure.RabbitConnectionDetails;
26+
import org.springframework.boot.ssl.SslBundle;
27+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
28+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
29+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
30+
31+
/**
32+
* {@link ContainerConnectionDetailsFactory} to create {@link RabbitConnectionDetails}
33+
* from a {@link ServiceConnection @ServiceConnection}-annotated
34+
* {@link RabbitMQContainer}.
35+
*
36+
* @author Moritz Halbritter
37+
* @author Andy Wilkinson
38+
* @author Phillip Webb
39+
* @deprecated since 4.0.0 for removal in 4.2.0 in favor of
40+
* {@link RabbitContainerConnectionDetailsFactory}.
41+
*/
42+
@Deprecated(since = "4.0.0", forRemoval = true)
43+
class DeprecatedRabbitContainerConnectionDetailsFactory
44+
extends ContainerConnectionDetailsFactory<RabbitMQContainer, RabbitConnectionDetails> {
45+
46+
@Override
47+
protected RabbitConnectionDetails getContainerConnectionDetails(
48+
ContainerConnectionSource<RabbitMQContainer> source) {
49+
return new RabbitMqContainerConnectionDetails(source);
50+
}
51+
52+
/**
53+
* {@link RabbitConnectionDetails} backed by a {@link ContainerConnectionSource}.
54+
*/
55+
private static final class RabbitMqContainerConnectionDetails extends ContainerConnectionDetails<RabbitMQContainer>
56+
implements RabbitConnectionDetails {
57+
58+
private RabbitMqContainerConnectionDetails(ContainerConnectionSource<RabbitMQContainer> source) {
59+
super(source);
60+
}
61+
62+
@Override
63+
public String getUsername() {
64+
return getContainer().getAdminUsername();
65+
}
66+
67+
@Override
68+
public String getPassword() {
69+
return getContainer().getAdminPassword();
70+
}
71+
72+
@Override
73+
public List<Address> getAddresses() {
74+
URI uri = URI.create((getSslBundle() != null) ? getContainer().getAmqpsUrl() : getContainer().getAmqpUrl());
75+
return List.of(new Address(uri.getHost(), uri.getPort()));
76+
}
77+
78+
@Override
79+
public @Nullable SslBundle getSslBundle() {
80+
return super.getSslBundle();
81+
}
82+
83+
}
84+
85+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Connection Details Factories
22
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
33
org.springframework.boot.amqp.docker.compose.RabbitDockerComposeConnectionDetailsFactory,\
4+
org.springframework.boot.amqp.testcontainers.DeprecatedRabbitContainerConnectionDetailsFactory,\
45
org.springframework.boot.amqp.testcontainers.RabbitContainerConnectionDetailsFactory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-present 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.mongodb.testcontainers;
18+
19+
import com.mongodb.client.MongoClient;
20+
import com.mongodb.client.MongoClients;
21+
import org.junit.jupiter.api.Test;
22+
import org.testcontainers.containers.MongoDBContainer;
23+
import org.testcontainers.junit.jupiter.Container;
24+
import org.testcontainers.junit.jupiter.Testcontainers;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.mongodb.autoconfigure.MongoConnectionDetails;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.boot.testsupport.container.TestImage;
30+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Integration tests for {@link DeprecatedMongoDbContainerConnectionDetailsFactory}.
36+
*
37+
* @author Andy Wilkinson
38+
* @deprecated since 4.0.0 for removal in 4.2.0 in favor of
39+
* {@link MongoDbContainerConnectionDetailsFactory}.
40+
*/
41+
@SpringJUnitConfig
42+
@Testcontainers(disabledWithoutDocker = true)
43+
@Deprecated(since = "4.0.0", forRemoval = true)
44+
class DeprecatedMongoDbContainerConnectionDetailsFactoryTests {
45+
46+
@Container
47+
@ServiceConnection
48+
static final MongoDBContainer mongoDb = TestImage.container(MongoDBContainer.class);
49+
50+
@Autowired(required = false)
51+
private MongoConnectionDetails connectionDetails;
52+
53+
@Test
54+
void connectionCanBeMadeToContainer() {
55+
assertThat(this.connectionDetails).isNotNull();
56+
MongoClient client = MongoClients.create(this.connectionDetails.getConnectionString());
57+
assertThat(client.listDatabaseNames()).containsExactly("admin", "config", "local");
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-present 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.mongodb.testcontainers;
18+
19+
import org.testcontainers.containers.MongoDBContainer;
20+
21+
import org.springframework.boot.mongodb.autoconfigure.MongoConnectionDetails;
22+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
23+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
24+
25+
/**
26+
* {@link ContainerConnectionDetailsFactory} to create {@link MongoConnectionDetails} from
27+
* a {@link ServiceConnection @ServiceConnection}-annotated {@link MongoDBContainer}.
28+
*
29+
* @author Moritz Halbritter
30+
* @author Andy Wilkinson
31+
* @author Phillip Webb
32+
* @deprecated since 4.0.0 for removal in 4.2.0 in favor of
33+
* {@link MongoDbContainerConnectionDetailsFactory}.
34+
*/
35+
@Deprecated(since = "4.0.0", forRemoval = true)
36+
class DeprecatedMongoDbContainerConnectionDetailsFactory
37+
extends AbstractMongoContainerConnectionDetailsFactory<MongoDBContainer> {
38+
39+
DeprecatedMongoDbContainerConnectionDetailsFactory() {
40+
super(MongoDBContainer::getReplicaSetUrl);
41+
}
42+
43+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Connection Details Factories
22
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
33
org.springframework.boot.mongodb.docker.compose.MongoDockerComposeConnectionDetailsFactory,\
4+
org.springframework.boot.mongodb.testcontainers.DeprecatedMongoDbContainerConnectionDetailsFactory,\
45
org.springframework.boot.mongodb.testcontainers.MongoDbAtlasLocalContainerConnectionDetailsFactory,\
56
org.springframework.boot.mongodb.testcontainers.MongoDbContainerConnectionDetailsFactory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-present 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.neo4j.testcontainers;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.neo4j.driver.Driver;
21+
import org.neo4j.driver.GraphDatabase;
22+
import org.testcontainers.containers.Neo4jContainer;
23+
import org.testcontainers.junit.jupiter.Container;
24+
import org.testcontainers.junit.jupiter.Testcontainers;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.neo4j.autoconfigure.Neo4jConnectionDetails;
28+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
29+
import org.springframework.boot.testsupport.container.TestImage;
30+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Integration tests for {@link DeprecatedNeo4jContainerConnectionDetailsFactory}.
36+
*
37+
* @author Stephane Nicoll
38+
* @deprecated since 4.0.0 for removal in 4.2.0 in favor of
39+
* {@link Neo4jContainerConnectionDetailsFactory}.
40+
*/
41+
@SpringJUnitConfig
42+
@Testcontainers(disabledWithoutDocker = true)
43+
@Deprecated(since = "4.0.0", forRemoval = true)
44+
class DeprecatedNeo4jContainerConnectionDetailsFactoryIntegrationTests {
45+
46+
@Container
47+
@ServiceConnection
48+
static final Neo4jContainer<?> container = TestImage.container(Neo4jContainer.class);
49+
50+
@Autowired(required = false)
51+
private Neo4jConnectionDetails connectionDetails;
52+
53+
@Test
54+
void connectionCanBeMadeToContainer() {
55+
assertThat(this.connectionDetails).isNotNull();
56+
try (Driver driver = GraphDatabase.driver(this.connectionDetails.getUri(),
57+
this.connectionDetails.getAuthToken())) {
58+
driver.verifyConnectivity();
59+
}
60+
}
61+
62+
}

0 commit comments

Comments
 (0)