-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ActiveMQ and Artemis (#7400)
The following implementations have been added: * `ActiveMQContainer` supports [ActiveMQ](https://hub.docker.com/r/apache/activemq-classic) * `ArtemisContainer` supports [Artemis](https://hub.docker.com/r/apache/activemq-artemis)
- Loading branch information
1 parent
9de26f1
commit afc94f1
Showing
14 changed files
with
441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# ActiveMQ | ||
|
||
Testcontainers module for [ActiveMQ](https://hub.docker.com/r/apache/activemq-classic) and | ||
[Artemis](https://hub.docker.com/r/apache/activemq-artemis). | ||
|
||
## ActiveMQContainer's usage examples | ||
|
||
You can start an ActiveMQ Classic container instance from any Java application by using: | ||
|
||
<!--codeinclude--> | ||
[Default ActiveMQ container](../../modules/activemq/src/test/java/org/testcontainers/activemq/ActiveMQContainerTest.java) inside_block:container | ||
<!--/codeinclude--> | ||
|
||
With custom credentials: | ||
|
||
<!--codeinclude--> | ||
[Setting custom credentials](../../modules/activemq/src/test/java/org/testcontainers/activemq/ActiveMQContainerTest.java) inside_block:settingCredentials | ||
<!--/codeinclude--> | ||
|
||
## ArtemisContainer's usage examples | ||
|
||
You can start an ActiveMQ Artemis container instance from any Java application by using: | ||
|
||
<!--codeinclude--> | ||
[Default Artemis container](../../modules/activemq/src/test/java/org/testcontainers/activemq/ArtemisContainerTest.java) inside_block:container | ||
<!--/codeinclude--> | ||
|
||
With custom credentials: | ||
|
||
<!--codeinclude--> | ||
[Setting custom credentials](../../modules/activemq/src/test/java/org/testcontainers/activemq/ArtemisContainerTest.java) inside_block:settingCredentials | ||
<!--/codeinclude--> | ||
|
||
With anonymous login: | ||
|
||
<!--codeinclude--> | ||
[Allow anonymous login](../../modules/activemq/src/test/java/org/testcontainers/activemq/ArtemisContainerTest.java) inside_block:enableAnonymousLogin | ||
<!--/codeinclude--> | ||
|
||
## Adding this module to your project dependencies | ||
|
||
Add the following dependency to your `pom.xml`/`build.gradle` file: | ||
|
||
=== "Gradle" | ||
```groovy | ||
testImplementation "org.testcontainers:activemq:{{latest_version}}" | ||
``` | ||
|
||
=== "Maven" | ||
```xml | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>activemq</artifactId> | ||
<version>{{latest_version}}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
description = "Testcontainers :: ActiveMQ" | ||
|
||
dependencies { | ||
api project(':testcontainers') | ||
|
||
testImplementation 'org.assertj:assertj-core:3.24.2' | ||
testImplementation "org.apache.activemq:activemq-client-jakarta:5.18.2" | ||
testImplementation "org.apache.activemq:artemis-jakarta-client:2.29.0" | ||
} | ||
|
||
test { | ||
javaLauncher = javaToolchains.launcherFor { | ||
languageVersion = JavaLanguageVersion.of(11) | ||
} | ||
} | ||
|
||
compileTestJava { | ||
javaCompiler = javaToolchains.compilerFor { | ||
languageVersion = JavaLanguageVersion.of(11) | ||
} | ||
options.release.set(11) | ||
} |
85 changes: 85 additions & 0 deletions
85
modules/activemq/src/main/java/org/testcontainers/activemq/ActiveMQContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.testcontainers.activemq; | ||
|
||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* Testcontainers implementation for Apache ActiveMQ. | ||
* <p> | ||
* Exposed ports: | ||
* <ul> | ||
* <li>Console: 8161</li> | ||
* <li>TCP: 61616</li> | ||
* <li>AMQP: 5672</li> | ||
* <li>STOMP: 61613</li> | ||
* <li>MQTT: 1883</li> | ||
* <li>WS: 61614</li> | ||
* </ul> | ||
*/ | ||
public class ActiveMQContainer extends GenericContainer<ActiveMQContainer> { | ||
|
||
private static final DockerImageName DEFAULT_IMAGE = DockerImageName.parse("apache/activemq-classic"); | ||
|
||
private static final int WEB_CONSOLE_PORT = 8161; | ||
|
||
private static final int TCP_PORT = 61616; | ||
|
||
private static final int AMQP_PORT = 5672; | ||
|
||
private static final int STOMP_PORT = 61613; | ||
|
||
private static final int MQTT_PORT = 1883; | ||
|
||
private static final int WS_PORT = 61614; | ||
|
||
private String username; | ||
|
||
private String password; | ||
|
||
public ActiveMQContainer(String image) { | ||
this(DockerImageName.parse(image)); | ||
} | ||
|
||
public ActiveMQContainer(DockerImageName dockerImageName) { | ||
super(dockerImageName); | ||
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE); | ||
|
||
withExposedPorts(WEB_CONSOLE_PORT, TCP_PORT, AMQP_PORT, STOMP_PORT, MQTT_PORT, WS_PORT); | ||
waitingFor(Wait.forLogMessage(".*Apache ActiveMQ.*started.*", 1).withStartupTimeout(Duration.ofMinutes(1))); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
if (this.username != null) { | ||
addEnv("ACTIVEMQ_CONNECTION_USER", this.username); | ||
} | ||
if (this.password != null) { | ||
addEnv("ACTIVEMQ_CONNECTION_PASSWORD", this.password); | ||
} | ||
} | ||
|
||
public ActiveMQContainer withUser(String username) { | ||
this.username = username; | ||
return this; | ||
} | ||
|
||
public ActiveMQContainer withPassword(String password) { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
public String getBrokerUrl() { | ||
return String.format("tcp://%s:%s", getHost(), getMappedPort(TCP_PORT)); | ||
} | ||
|
||
public String getUser() { | ||
return this.username; | ||
} | ||
|
||
public String getPassword() { | ||
return this.password; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
modules/activemq/src/main/java/org/testcontainers/activemq/ArtemisContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.testcontainers.activemq; | ||
|
||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* Testcontainers implementation for Apache ActiveMQ Artemis. | ||
* <p> | ||
* Exposed ports: | ||
* <ul> | ||
* <li>Console: 8161</li> | ||
* <li>TCP: 61616</li> | ||
* <li>HORNETQ: 5445</li> | ||
* <li>AMQP: 5672</li> | ||
* <li>STOMP: 61613</li> | ||
* <li>MQTT: 1883</li> | ||
* <li>WS: 61614</li> | ||
* </ul> | ||
*/ | ||
public class ArtemisContainer extends GenericContainer<ArtemisContainer> { | ||
|
||
private static final DockerImageName DEFAULT_IMAGE = DockerImageName.parse("apache/activemq-artemis"); | ||
|
||
private static final int WEB_CONSOLE_PORT = 8161; | ||
|
||
// CORE,MQTT,AMQP,HORNETQ,STOMP,OPENWIRE | ||
private static final int TCP_PORT = 61616; | ||
|
||
private static final int HORNETQ_STOMP_PORT = 5445; | ||
|
||
private static final int AMQP_PORT = 5672; | ||
|
||
private static final int STOMP_PORT = 61613; | ||
|
||
private static final int MQTT_PORT = 1883; | ||
|
||
private static final int WS_PORT = 61614; | ||
|
||
private String username = "artemis"; | ||
|
||
private String password = "artemis"; | ||
|
||
public ArtemisContainer(String image) { | ||
this(DockerImageName.parse(image)); | ||
} | ||
|
||
public ArtemisContainer(DockerImageName dockerImageName) { | ||
super(dockerImageName); | ||
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE); | ||
|
||
withExposedPorts(WEB_CONSOLE_PORT, TCP_PORT, HORNETQ_STOMP_PORT, AMQP_PORT, STOMP_PORT, MQTT_PORT, WS_PORT); | ||
waitingFor(Wait.forLogMessage(".*HTTP Server started.*", 1).withStartupTimeout(Duration.ofMinutes(1))); | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
withEnv("ARTEMIS_USER", this.username); | ||
withEnv("ARTEMIS_PASSWORD", this.password); | ||
} | ||
|
||
public ArtemisContainer withUser(String username) { | ||
this.username = username; | ||
return this; | ||
} | ||
|
||
public ArtemisContainer withPassword(String password) { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
public String getBrokerUrl() { | ||
return String.format("tcp://%s:%s", getHost(), getMappedPort(TCP_PORT)); | ||
} | ||
|
||
public String getUser() { | ||
return getEnvMap().get("ARTEMIS_USER"); | ||
} | ||
|
||
public String getPassword() { | ||
return getEnvMap().get("ARTEMIS_PASSWORD"); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
modules/activemq/src/test/java/org/testcontainers/activemq/ActiveMQContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.testcontainers.activemq; | ||
|
||
import jakarta.jms.Connection; | ||
import jakarta.jms.ConnectionFactory; | ||
import jakarta.jms.Destination; | ||
import jakarta.jms.JMSException; | ||
import jakarta.jms.MessageConsumer; | ||
import jakarta.jms.MessageProducer; | ||
import jakarta.jms.Session; | ||
import jakarta.jms.TextMessage; | ||
import lombok.SneakyThrows; | ||
import org.apache.activemq.ActiveMQConnectionFactory; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ActiveMQContainerTest { | ||
|
||
@Test | ||
public void test() throws JMSException { | ||
try ( // container { | ||
ActiveMQContainer activemq = new ActiveMQContainer("apache/activemq-classic:5.18.3") | ||
// } | ||
) { | ||
activemq.start(); | ||
|
||
assertThat(activemq.getUser()).isNull(); | ||
assertThat(activemq.getPassword()).isNull(); | ||
assertFunctionality(activemq, false); | ||
} | ||
} | ||
|
||
@Test | ||
public void customCredentials() { | ||
try ( | ||
// settingCredentials { | ||
ActiveMQContainer activemq = new ActiveMQContainer("apache/activemq-classic:5.18.3") | ||
.withUser("testcontainers") | ||
.withPassword("testcontainers") | ||
// } | ||
) { | ||
activemq.start(); | ||
|
||
assertThat(activemq.getUser()).isEqualTo("testcontainers"); | ||
assertThat(activemq.getPassword()).isEqualTo("testcontainers"); | ||
assertFunctionality(activemq, true); | ||
} | ||
} | ||
|
||
@SneakyThrows | ||
private void assertFunctionality(ActiveMQContainer activemq, boolean useCredentials) { | ||
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(activemq.getBrokerUrl()); | ||
Connection connection; | ||
if (useCredentials) { | ||
connection = connectionFactory.createConnection(activemq.getUser(), activemq.getPassword()); | ||
} else { | ||
connection = connectionFactory.createConnection(); | ||
} | ||
connection.start(); | ||
|
||
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
|
||
Destination destination = session.createQueue("test-queue"); | ||
MessageProducer producer = session.createProducer(destination); | ||
|
||
String contentMessage = "Testcontainers"; | ||
TextMessage message = session.createTextMessage(contentMessage); | ||
producer.send(message); | ||
|
||
MessageConsumer consumer = session.createConsumer(destination); | ||
TextMessage messageReceived = (TextMessage) consumer.receive(); | ||
assertThat(messageReceived.getText()).isEqualTo(contentMessage); | ||
} | ||
} |
Oops, something went wrong.