Add helpers for JMS tests

This commit is contained in:
Arnaud Cogoluègnes 2025-02-10 11:45:58 +01:00 committed by David Ansari
parent 9062476a18
commit fd350386a9
6 changed files with 185 additions and 40 deletions

View File

@ -10,6 +10,7 @@
<properties>
<junit.jupiter.version>5.10.2</junit.jupiter.version>
<qpid-jms-client.version>2.6.1</qpid-jms-client.version>
<amqp-client.version>[0.5.0-SNAPSHOT,)</amqp-client.version>
<logback.version>1.2.13</logback.version>
<spotless.version>2.43.0</spotless.version>
<google-java-format.version>1.25.2</google-java-format.version>
@ -30,13 +31,18 @@
<version>${qpid-jms-client.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.rabbitmq.client</groupId>
<artifactId>amqp-client</artifactId>
<version>${amqp-client.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -81,4 +87,16 @@
</plugins>
</build>
<repositories>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>
</project>

View File

@ -11,7 +11,8 @@
// The Original Code is RabbitMQ.
//
// The Initial Developer of the Original Code is Pivotal Software, Inc.
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc.
// and/or its subsidiaries. All rights reserved.
//
package com.rabbitmq.amqp.tests.jms;
@ -31,19 +32,21 @@ import org.apache.qpid.jms.JmsConnection;
import org.apache.qpid.jms.JmsConnectionFactory;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.Timeout;
/**
* Based on https://github.com/apache/qpid-jms/tree/main/qpid-jms-interop-tests/qpid-jms-activemq-tests.
* Based on
* https://github.com/apache/qpid-jms/tree/main/qpid-jms-interop-tests/qpid-jms-activemq-tests.
*/
@JmsTestInfrastructure
public class JmsConnectionTest {
String destination;
@Test
@Timeout(30)
public void testCreateConnection() throws Exception {
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
try (Connection connection = factory.createConnection()) {
try (Connection connection = connection()) {
assertNotNull(connection);
}
}
@ -51,8 +54,7 @@ public class JmsConnectionTest {
@Test
@Timeout(30)
public void testCreateConnectionAndStart() throws Exception {
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
try (Connection connection = factory.createConnection()) {
try (Connection connection = connection()) {
assertNotNull(connection);
connection.start();
}
@ -63,7 +65,7 @@ public class JmsConnectionTest {
// Currently not supported by RabbitMQ.
@Disabled
public void testCreateWithDuplicateClientIdFails() throws Exception {
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
JmsConnectionFactory factory = (JmsConnectionFactory) connectionFactory();
JmsConnection connection1 = (JmsConnection) factory.createConnection();
connection1.setClientID("Test");
assertNotNull(connection1);
@ -87,8 +89,7 @@ public class JmsConnectionTest {
assertThrows(
JMSException.class,
() -> {
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
try (Connection connection = factory.createConnection()) {
try (Connection connection = connection()) {
connection.setClientID("Test");
connection.start();
connection.setClientID("NewTest");
@ -99,7 +100,7 @@ public class JmsConnectionTest {
@Test
@Timeout(30)
public void testCreateConnectionAsSystemAdmin() throws Exception {
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
JmsConnectionFactory factory = (JmsConnectionFactory) connectionFactory();
factory.setUsername(adminUsername());
factory.setPassword(adminPassword());
try (Connection connection = factory.createConnection()) {
@ -111,8 +112,8 @@ public class JmsConnectionTest {
@Test
@Timeout(30)
public void testCreateConnectionCallSystemAdmin() throws Exception {
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
try (Connection connection = factory.createConnection(adminUsername(), adminPassword())) {
try (Connection connection =
connectionFactory().createConnection(adminUsername(), adminPassword())) {
assertNotNull(connection);
connection.start();
}
@ -124,7 +125,7 @@ public class JmsConnectionTest {
assertThrows(
JMSSecurityException.class,
() -> {
JmsConnectionFactory factory = new JmsConnectionFactory(TestUtils.brokerUri());
JmsConnectionFactory factory = (JmsConnectionFactory) connectionFactory();
factory.setUsername("unknown");
factory.setPassword("unknown");
try (Connection connection = factory.createConnection()) {
@ -140,8 +141,7 @@ public class JmsConnectionTest {
assertThrows(
JMSSecurityException.class,
() -> {
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
try (Connection connection = factory.createConnection("unknown", "unknown")) {
try (Connection connection = connectionFactory().createConnection("unknown", "unknown")) {
assertNotNull(connection);
connection.start();
}
@ -150,14 +150,11 @@ public class JmsConnectionTest {
@Test
@Timeout(30)
public void testBrokerStopWontHangConnectionClose(TestInfo info) throws Exception {
Connection connection = new JmsConnectionFactory(brokerUri()).createConnection();
public void testBrokerStopWontHangConnectionClose() throws Exception {
Connection connection = connection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// TODO use a "regular" queue
TemporaryQueue queue = session.createTemporaryQueue();
// String destinationName = name(info);
// Queue queue = session.createQueue("/queues/" + destinationName);
Queue queue = queue(destination);
connection.start();
MessageProducer producer = session.createProducer(queue);
@ -182,7 +179,7 @@ public class JmsConnectionTest {
@Timeout(60)
public void testConnectionExceptionBrokerStop() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
try (Connection connection = new JmsConnectionFactory(brokerUri()).createConnection()) {
try (Connection connection = connection()) {
connection.setExceptionListener(exception -> latch.countDown());
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

View File

@ -11,12 +11,14 @@
// The Original Code is RabbitMQ.
//
// The Initial Developer of the Original Code is Pivotal Software, Inc.
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc.
// and/or its subsidiaries. All rights reserved.
//
package com.rabbitmq.amqp.tests.jms;
import static com.rabbitmq.amqp.tests.jms.TestUtils.brokerUri;
import static com.rabbitmq.amqp.tests.jms.TestUtils.connection;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.fail;
@ -25,16 +27,23 @@ import jakarta.jms.IllegalStateException;
import java.util.UUID;
import org.apache.qpid.jms.JmsConnectionFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
/**
* Based on https://github.com/apache/qpid-jms/tree/main/qpid-jms-interop-tests/qpid-jms-activemq-tests.
* Based on
* https://github.com/apache/qpid-jms/tree/main/qpid-jms-interop-tests/qpid-jms-activemq-tests.
*/
public class JmsTemporaryQueueTest {
Connection connection;
@BeforeEach
void init() throws JMSException {
connection = connection();
}
@AfterEach
void tearDown() throws JMSException {
connection.close();
@ -43,7 +52,6 @@ public class JmsTemporaryQueueTest {
@Test
@Timeout(60)
public void testCreatePublishConsumeTemporaryQueue() throws Exception {
connection = new JmsConnectionFactory(brokerUri()).createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
@ -60,7 +68,6 @@ public class JmsTemporaryQueueTest {
@Test
@Timeout(60)
public void testCantConsumeFromTemporaryQueueCreatedOnAnotherConnection() throws Exception {
connection = new JmsConnectionFactory(brokerUri()).createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
@ -84,7 +91,6 @@ public class JmsTemporaryQueueTest {
@Test
@Timeout(60)
public void testCantSendToTemporaryQueueFromClosedConnection() throws Exception {
connection = new JmsConnectionFactory(brokerUri()).createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
@ -113,7 +119,6 @@ public class JmsTemporaryQueueTest {
@Test
@Timeout(60)
public void testCantDeleteTemporaryQueueWithConsumers() throws Exception {
connection = new JmsConnectionFactory(brokerUri()).createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

View File

@ -0,0 +1,26 @@
// The contents of this file are subject to the Mozilla Public 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.mozilla.org/en-US/MPL/2.0/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
// the License for the specific language governing rights and
// limitations under the License.
//
// The Original Code is RabbitMQ.
//
// The Initial Developer of the Original Code is Pivotal Software, Inc.
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc.
// and/or its subsidiaries. All rights reserved.
//
package com.rabbitmq.amqp.tests.jms;
import java.lang.annotation.*;
import org.junit.jupiter.api.extension.ExtendWith;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(JmsTestInfrastructureExtension.class)
public @interface JmsTestInfrastructure {}

View File

@ -0,0 +1,83 @@
// The contents of this file are subject to the Mozilla Public 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.mozilla.org/en-US/MPL/2.0/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
// the License for the specific language governing rights and
// limitations under the License.
//
// The Original Code is RabbitMQ.
//
// The Initial Developer of the Original Code is Pivotal Software, Inc.
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
//
package com.rabbitmq.amqp.tests.jms;
import com.rabbitmq.client.amqp.Connection;
import com.rabbitmq.client.amqp.Environment;
import com.rabbitmq.client.amqp.impl.AmqpEnvironmentBuilder;
import java.lang.reflect.Field;
import org.junit.jupiter.api.extension.*;
final class JmsTestInfrastructureExtension
implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
private static final ExtensionContext.Namespace NAMESPACE =
ExtensionContext.Namespace.create(JmsTestInfrastructureExtension.class);
private static ExtensionContext.Store store(ExtensionContext extensionContext) {
return extensionContext.getRoot().getStore(NAMESPACE);
}
private static Field field(Class<?> cls, String name) {
Field field = null;
while (field == null && cls != null) {
try {
field = cls.getDeclaredField(name);
} catch (NoSuchFieldException e) {
cls = cls.getSuperclass();
}
}
return field;
}
@Override
public void beforeAll(ExtensionContext context) {
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
Field field = field(context.getTestInstance().get().getClass(), "destination");
if (field != null) {
field.setAccessible(true);
String destination = TestUtils.name(context);
field.set(context.getTestInstance().get(), destination);
try (Environment environment = new AmqpEnvironmentBuilder().build();
Connection connection = environment.connectionBuilder().uri(TestUtils.brokerUri()).build()) {
connection.management().queue(destination).declare();
}
}
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
Field field = field(context.getTestInstance().get().getClass(), "destination");
if (field != null) {
field.setAccessible(true);
String destination = (String) field.get(context.getTestInstance().get());
try (Environment environment = new AmqpEnvironmentBuilder().build();
Connection connection = environment.connectionBuilder().uri(TestUtils.brokerUri()).build()) {
connection.management().queueDelete(destination);
}
}
}
@Override
public void afterAll(ExtensionContext context) {
}
}

View File

@ -11,29 +11,30 @@
// The Original Code is RabbitMQ.
//
// The Initial Developer of the Original Code is Pivotal Software, Inc.
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc.
// and/or its subsidiaries. All rights reserved.
//
package com.rabbitmq.amqp.tests.jms;
import static java.lang.String.format;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.JMSException;
import jakarta.jms.Queue;
import java.lang.reflect.Method;
import java.util.UUID;
import org.junit.jupiter.api.Tag;
import org.apache.qpid.jms.JmsConnectionFactory;
import org.apache.qpid.jms.JmsQueue;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.extension.ExtensionContext;
final class TestUtils {
private static final String DEFAULT_BROKER_URI = "amqp://localhost:5672";
private TestUtils() { }
private TestUtils() {}
static String brokerUri() {
String uri = System.getProperty("rmq_broker_uri", "amqp://localhost:5672");
@ -48,10 +49,26 @@ final class TestUtils {
return "guest";
}
static ConnectionFactory connectionFactory() {
return new JmsConnectionFactory(brokerUri());
}
static Connection connection() throws JMSException {
return connectionFactory().createConnection();
}
static Queue queue(String name) {
// no path encoding, use names with e.g. ASCII characters only
return new JmsQueue("/queues/" + name);
}
static String name(TestInfo info) {
return name(info.getTestClass().get(), info.getTestMethod().get());
}
static String name(ExtensionContext context) {
return name(context.getTestInstance().get().getClass(), context.getTestMethod().get());
}
private static String name(Class<?> testClass, Method testMethod) {
return name(testClass, testMethod.getName());
@ -62,5 +79,4 @@ final class TestUtils {
return format(
"%s_%s%s", testClass.getSimpleName(), testMethod, uuid.substring(uuid.length() / 2));
}
}