Skip to content

Commit f3e7325

Browse files
eddumelendezmhalbritter
authored andcommitted
Add service connection for Docker Compose and Testcontainers ActiveMQ
See gh-39363
1 parent f936356 commit f3e7325

File tree

14 files changed

+416
-6
lines changed

14 files changed

+416
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.docker.compose.service.connection.activemq;
18+
19+
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQConnectionDetails;
20+
import org.springframework.boot.docker.compose.core.RunningService;
21+
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
22+
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
23+
24+
/**
25+
* {@link DockerComposeConnectionDetailsFactory} to create
26+
* {@link ActiveMQConnectionDetails} for an {@code activemq} service.
27+
*
28+
* @author Stephane Nicoll
29+
* @author Eddú Meléndez
30+
*/
31+
class ActiveMQClassicDockerComposeConnectionDetailsFactory
32+
extends DockerComposeConnectionDetailsFactory<ActiveMQConnectionDetails> {
33+
34+
private static final int ACTIVEMQ_PORT = 61616;
35+
36+
protected ActiveMQClassicDockerComposeConnectionDetailsFactory() {
37+
super("apache/activemq-classic");
38+
}
39+
40+
@Override
41+
protected ActiveMQConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
42+
return new ActiveMQDockerComposeConnectionDetails(source.getRunningService());
43+
}
44+
45+
/**
46+
* {@link ActiveMQConnectionDetails} backed by a {@code activemq}
47+
* {@link RunningService}.
48+
*/
49+
static class ActiveMQDockerComposeConnectionDetails extends DockerComposeConnectionDetails
50+
implements ActiveMQConnectionDetails {
51+
52+
private final ActiveMQClassicEnvironment environment;
53+
54+
private final String brokerUrl;
55+
56+
protected ActiveMQDockerComposeConnectionDetails(RunningService service) {
57+
super(service);
58+
this.environment = new ActiveMQClassicEnvironment(service.env());
59+
this.brokerUrl = "tcp://" + service.host() + ":" + service.ports().get(ACTIVEMQ_PORT);
60+
}
61+
62+
@Override
63+
public String getBrokerUrl() {
64+
return this.brokerUrl;
65+
}
66+
67+
@Override
68+
public String getUser() {
69+
return this.environment.getUser();
70+
}
71+
72+
@Override
73+
public String getPassword() {
74+
return this.environment.getPassword();
75+
}
76+
77+
}
78+
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.docker.compose.service.connection.activemq;
18+
19+
import java.util.Map;
20+
21+
/**
22+
* ActiveMQ environment details.
23+
*
24+
* @author Stephane Nicoll
25+
* @author Eddú Meléndez
26+
*/
27+
class ActiveMQClassicEnvironment {
28+
29+
private final String user;
30+
31+
private final String password;
32+
33+
ActiveMQClassicEnvironment(Map<String, String> env) {
34+
this.user = env.get("ACTIVEMQ_CONNECTION_USER");
35+
this.password = env.get("ACTIVEMQ_CONNECTION_PASSWORD");
36+
}
37+
38+
String getUser() {
39+
return this.user;
40+
}
41+
42+
String getPassword() {
43+
return this.password;
44+
}
45+
46+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ org.springframework.boot.docker.compose.service.connection.DockerComposeServiceC
55

66
# Connection Details Factories
77
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
8+
org.springframework.boot.docker.compose.service.connection.activemq.ActiveMQClassicDockerComposeConnectionDetailsFactory,\
89
org.springframework.boot.docker.compose.service.connection.activemq.ActiveMQDockerComposeConnectionDetailsFactory,\
910
org.springframework.boot.docker.compose.service.connection.activemq.ArtemisDockerComposeConnectionDetailsFactory,\
1011
org.springframework.boot.docker.compose.service.connection.cassandra.CassandraDockerComposeConnectionDetailsFactory,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.docker.compose.service.connection.activemq;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQConnectionDetails;
22+
import org.springframework.boot.docker.compose.service.connection.test.AbstractDockerComposeIntegrationTests;
23+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Integration tests for {@link ActiveMQClassicDockerComposeConnectionDetailsFactory}.
29+
*
30+
* @author Stephane Nicoll
31+
* @author Eddú Meléndez
32+
*/
33+
class ActiveMQClassicDockerComposeConnectionDetailsFactoryIntegrationTests
34+
extends AbstractDockerComposeIntegrationTests {
35+
36+
ActiveMQClassicDockerComposeConnectionDetailsFactoryIntegrationTests() {
37+
super("activemq-classic-compose.yaml", DockerImageNames.activeMqClassic());
38+
}
39+
40+
@Test
41+
void runCreatesConnectionDetails() {
42+
ActiveMQConnectionDetails connectionDetails = run(ActiveMQConnectionDetails.class);
43+
assertThat(connectionDetails.getBrokerUrl()).isNotNull().startsWith("tcp://");
44+
assertThat(connectionDetails.getUser()).isEqualTo("root");
45+
assertThat(connectionDetails.getPassword()).isEqualTo("secret");
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.docker.compose.service.connection.activemq;
18+
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link ActiveMQClassicEnvironment}.
28+
*
29+
* @author Stephane Nicoll
30+
* @author Eddú Meléndez
31+
*/
32+
class ActiveMQClassicEnvironmentTests {
33+
34+
@Test
35+
void getUserWhenHasNoActiveMqUser() {
36+
ActiveMQClassicEnvironment environment = new ActiveMQClassicEnvironment(Collections.emptyMap());
37+
assertThat(environment.getUser()).isNull();
38+
}
39+
40+
@Test
41+
void getUserWhenHasActiveMqUser() {
42+
ActiveMQClassicEnvironment environment = new ActiveMQClassicEnvironment(
43+
Map.of("ACTIVEMQ_CONNECTION_USER", "me"));
44+
assertThat(environment.getUser()).isEqualTo("me");
45+
}
46+
47+
@Test
48+
void getPasswordWhenHasNoActiveMqPassword() {
49+
ActiveMQClassicEnvironment environment = new ActiveMQClassicEnvironment(Collections.emptyMap());
50+
assertThat(environment.getPassword()).isNull();
51+
}
52+
53+
@Test
54+
void getPasswordWhenHasActiveMqPassword() {
55+
ActiveMQClassicEnvironment environment = new ActiveMQClassicEnvironment(
56+
Map.of("ACTIVEMQ_CONNECTION_PASSWORD", "secret"));
57+
assertThat(environment.getPassword()).isEqualTo("secret");
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
activemq:
3+
image: '{imageName}'
4+
ports:
5+
- '61616'
6+
environment:
7+
ACTIVEMQ_CONNECTION_USER: 'root'
8+
ACTIVEMQ_CONNECTION_PASSWORD: 'secret'

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/docker-compose.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The following service connections are currently supported:
7070
| Connection Details | Matched on
7171

7272
| `ActiveMQConnectionDetails`
73-
| Containers named "symptoma/activemq"
73+
| Containers named "symptoma/activemq", "apache/activemq-classic"
7474

7575
| `ArtemisConnectionDetails`
7676
| Containers named "apache/activemq-artemis"

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
@@ -981,7 +981,7 @@ The following service connection factories are provided in the `spring-boot-test
981981
| Connection Details | Matched on
982982

983983
| `ActiveMQConnectionDetails`
984-
| Containers named "symptoma/activemq"
984+
| Containers named "symptoma/activemq" or `ActiveMQContainer`
985985

986986
| `ArtemisConnectionDetails`
987987
| Containers of type `ArtemisContainer`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.activemq;
18+
19+
import org.testcontainers.activemq.ActiveMQContainer;
20+
21+
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQConnectionDetails;
22+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
23+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
24+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
25+
26+
/**
27+
* {@link ContainerConnectionDetailsFactory} to create {@link ActiveMQConnectionDetails} *
28+
* from a {@link ServiceConnection @ServiceConnection}-annotated
29+
* {@link ActiveMQContainer}.
30+
*
31+
* @author Eddú Meléndez
32+
*/
33+
class ActiveMQClassicContainerConnectionDetailsFactory
34+
extends ContainerConnectionDetailsFactory<ActiveMQContainer, ActiveMQConnectionDetails> {
35+
36+
@Override
37+
protected ActiveMQConnectionDetails getContainerConnectionDetails(
38+
ContainerConnectionSource<ActiveMQContainer> source) {
39+
return new ActiveMQContainerConnectionDetails(source);
40+
}
41+
42+
private static final class ActiveMQContainerConnectionDetails extends ContainerConnectionDetails<ActiveMQContainer>
43+
implements ActiveMQConnectionDetails {
44+
45+
private ActiveMQContainerConnectionDetails(ContainerConnectionSource<ActiveMQContainer> source) {
46+
super(source);
47+
}
48+
49+
@Override
50+
public String getBrokerUrl() {
51+
return getContainer().getBrokerUrl();
52+
}
53+
54+
@Override
55+
public String getUser() {
56+
return getContainer().getUser();
57+
}
58+
59+
@Override
60+
public String getPassword() {
61+
return getContainer().getPassword();
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
@@ -8,6 +8,7 @@ org.springframework.boot.testcontainers.service.connection.ServiceConnectionCont
88

99
# Connection Details Factories
1010
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
11+
org.springframework.boot.testcontainers.service.connection.activemq.ActiveMQClassicContainerConnectionDetailsFactory,\
1112
org.springframework.boot.testcontainers.service.connection.activemq.ActiveMQContainerConnectionDetailsFactory,\
1213
org.springframework.boot.testcontainers.service.connection.activemq.ArtemisContainerConnectionDetailsFactory,\
1314
org.springframework.boot.testcontainers.service.connection.amqp.RabbitContainerConnectionDetailsFactory,\

0 commit comments

Comments
 (0)