Skip to content

Commit fd35038

Browse files
acogoluegnesansd
authored andcommitted
Add helpers for JMS tests
1 parent 9062476 commit fd35038

File tree

6 files changed

+185
-40
lines changed

6 files changed

+185
-40
lines changed

deps/rabbit/test/amqp_jms_SUITE_data/pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<properties>
1111
<junit.jupiter.version>5.10.2</junit.jupiter.version>
1212
<qpid-jms-client.version>2.6.1</qpid-jms-client.version>
13+
<amqp-client.version>[0.5.0-SNAPSHOT,)</amqp-client.version>
1314
<logback.version>1.2.13</logback.version>
1415
<spotless.version>2.43.0</spotless.version>
1516
<google-java-format.version>1.25.2</google-java-format.version>
@@ -30,13 +31,18 @@
3031
<version>${qpid-jms-client.version}</version>
3132
<scope>test</scope>
3233
</dependency>
33-
3434
<dependency>
3535
<groupId>ch.qos.logback</groupId>
3636
<artifactId>logback-classic</artifactId>
3737
<version>${logback.version}</version>
3838
<scope>test</scope>
3939
</dependency>
40+
<dependency>
41+
<groupId>com.rabbitmq.client</groupId>
42+
<artifactId>amqp-client</artifactId>
43+
<version>${amqp-client.version}</version>
44+
<scope>test</scope>
45+
</dependency>
4046

4147
</dependencies>
4248
<build>
@@ -81,4 +87,16 @@
8187

8288
</plugins>
8389
</build>
90+
91+
<repositories>
92+
93+
<repository>
94+
<id>ossrh</id>
95+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
96+
<snapshots><enabled>true</enabled></snapshots>
97+
<releases><enabled>false</enabled></releases>
98+
</repository>
99+
100+
</repositories>
101+
84102
</project>

deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsConnectionTest.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
// The Original Code is RabbitMQ.
1212
//
1313
// The Initial Developer of the Original Code is Pivotal Software, Inc.
14-
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
14+
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc.
15+
// and/or its subsidiaries. All rights reserved.
1516
//
1617

1718
package com.rabbitmq.amqp.tests.jms;
@@ -31,28 +32,29 @@
3132
import org.apache.qpid.jms.JmsConnectionFactory;
3233
import org.junit.jupiter.api.Disabled;
3334
import org.junit.jupiter.api.Test;
34-
import org.junit.jupiter.api.TestInfo;
3535
import org.junit.jupiter.api.Timeout;
3636

3737
/**
38-
* Based on https://github.com/apache/qpid-jms/tree/main/qpid-jms-interop-tests/qpid-jms-activemq-tests.
38+
* Based on
39+
* https://github.com/apache/qpid-jms/tree/main/qpid-jms-interop-tests/qpid-jms-activemq-tests.
3940
*/
41+
@JmsTestInfrastructure
4042
public class JmsConnectionTest {
4143

44+
String destination;
45+
4246
@Test
4347
@Timeout(30)
4448
public void testCreateConnection() throws Exception {
45-
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
46-
try (Connection connection = factory.createConnection()) {
49+
try (Connection connection = connection()) {
4750
assertNotNull(connection);
4851
}
4952
}
5053

5154
@Test
5255
@Timeout(30)
5356
public void testCreateConnectionAndStart() throws Exception {
54-
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
55-
try (Connection connection = factory.createConnection()) {
57+
try (Connection connection = connection()) {
5658
assertNotNull(connection);
5759
connection.start();
5860
}
@@ -63,7 +65,7 @@ public void testCreateConnectionAndStart() throws Exception {
6365
// Currently not supported by RabbitMQ.
6466
@Disabled
6567
public void testCreateWithDuplicateClientIdFails() throws Exception {
66-
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
68+
JmsConnectionFactory factory = (JmsConnectionFactory) connectionFactory();
6769
JmsConnection connection1 = (JmsConnection) factory.createConnection();
6870
connection1.setClientID("Test");
6971
assertNotNull(connection1);
@@ -87,8 +89,7 @@ public void testSetClientIdAfterStartedFails() {
8789
assertThrows(
8890
JMSException.class,
8991
() -> {
90-
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
91-
try (Connection connection = factory.createConnection()) {
92+
try (Connection connection = connection()) {
9293
connection.setClientID("Test");
9394
connection.start();
9495
connection.setClientID("NewTest");
@@ -99,7 +100,7 @@ public void testSetClientIdAfterStartedFails() {
99100
@Test
100101
@Timeout(30)
101102
public void testCreateConnectionAsSystemAdmin() throws Exception {
102-
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
103+
JmsConnectionFactory factory = (JmsConnectionFactory) connectionFactory();
103104
factory.setUsername(adminUsername());
104105
factory.setPassword(adminPassword());
105106
try (Connection connection = factory.createConnection()) {
@@ -111,8 +112,8 @@ public void testCreateConnectionAsSystemAdmin() throws Exception {
111112
@Test
112113
@Timeout(30)
113114
public void testCreateConnectionCallSystemAdmin() throws Exception {
114-
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
115-
try (Connection connection = factory.createConnection(adminUsername(), adminPassword())) {
115+
try (Connection connection =
116+
connectionFactory().createConnection(adminUsername(), adminPassword())) {
116117
assertNotNull(connection);
117118
connection.start();
118119
}
@@ -124,7 +125,7 @@ public void testCreateConnectionAsUnknwonUser() {
124125
assertThrows(
125126
JMSSecurityException.class,
126127
() -> {
127-
JmsConnectionFactory factory = new JmsConnectionFactory(TestUtils.brokerUri());
128+
JmsConnectionFactory factory = (JmsConnectionFactory) connectionFactory();
128129
factory.setUsername("unknown");
129130
factory.setPassword("unknown");
130131
try (Connection connection = factory.createConnection()) {
@@ -140,8 +141,7 @@ public void testCreateConnectionCallUnknwonUser() {
140141
assertThrows(
141142
JMSSecurityException.class,
142143
() -> {
143-
JmsConnectionFactory factory = new JmsConnectionFactory(brokerUri());
144-
try (Connection connection = factory.createConnection("unknown", "unknown")) {
144+
try (Connection connection = connectionFactory().createConnection("unknown", "unknown")) {
145145
assertNotNull(connection);
146146
connection.start();
147147
}
@@ -150,14 +150,11 @@ public void testCreateConnectionCallUnknwonUser() {
150150

151151
@Test
152152
@Timeout(30)
153-
public void testBrokerStopWontHangConnectionClose(TestInfo info) throws Exception {
154-
Connection connection = new JmsConnectionFactory(brokerUri()).createConnection();
153+
public void testBrokerStopWontHangConnectionClose() throws Exception {
154+
Connection connection = connection();
155155
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
156156

157-
// TODO use a "regular" queue
158-
TemporaryQueue queue = session.createTemporaryQueue();
159-
// String destinationName = name(info);
160-
// Queue queue = session.createQueue("/queues/" + destinationName);
157+
Queue queue = queue(destination);
161158
connection.start();
162159

163160
MessageProducer producer = session.createProducer(queue);
@@ -182,7 +179,7 @@ public void testBrokerStopWontHangConnectionClose(TestInfo info) throws Exceptio
182179
@Timeout(60)
183180
public void testConnectionExceptionBrokerStop() throws Exception {
184181
final CountDownLatch latch = new CountDownLatch(1);
185-
try (Connection connection = new JmsConnectionFactory(brokerUri()).createConnection()) {
182+
try (Connection connection = connection()) {
186183
connection.setExceptionListener(exception -> latch.countDown());
187184
connection.start();
188185
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTemporaryQueueTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
// The Original Code is RabbitMQ.
1212
//
1313
// The Initial Developer of the Original Code is Pivotal Software, Inc.
14-
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
14+
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc.
15+
// and/or its subsidiaries. All rights reserved.
1516
//
1617

1718
package com.rabbitmq.amqp.tests.jms;
1819

1920
import static com.rabbitmq.amqp.tests.jms.TestUtils.brokerUri;
21+
import static com.rabbitmq.amqp.tests.jms.TestUtils.connection;
2022
import static org.junit.jupiter.api.Assertions.*;
2123
import static org.junit.jupiter.api.Assertions.fail;
2224

@@ -25,16 +27,23 @@
2527
import java.util.UUID;
2628
import org.apache.qpid.jms.JmsConnectionFactory;
2729
import org.junit.jupiter.api.AfterEach;
30+
import org.junit.jupiter.api.BeforeEach;
2831
import org.junit.jupiter.api.Test;
2932
import org.junit.jupiter.api.Timeout;
3033

3134
/**
32-
* Based on https://github.com/apache/qpid-jms/tree/main/qpid-jms-interop-tests/qpid-jms-activemq-tests.
35+
* Based on
36+
* https://github.com/apache/qpid-jms/tree/main/qpid-jms-interop-tests/qpid-jms-activemq-tests.
3337
*/
3438
public class JmsTemporaryQueueTest {
3539

3640
Connection connection;
3741

42+
@BeforeEach
43+
void init() throws JMSException {
44+
connection = connection();
45+
}
46+
3847
@AfterEach
3948
void tearDown() throws JMSException {
4049
connection.close();
@@ -43,7 +52,6 @@ void tearDown() throws JMSException {
4352
@Test
4453
@Timeout(60)
4554
public void testCreatePublishConsumeTemporaryQueue() throws Exception {
46-
connection = new JmsConnectionFactory(brokerUri()).createConnection();
4755
connection.start();
4856

4957
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
@@ -60,7 +68,6 @@ public void testCreatePublishConsumeTemporaryQueue() throws Exception {
6068
@Test
6169
@Timeout(60)
6270
public void testCantConsumeFromTemporaryQueueCreatedOnAnotherConnection() throws Exception {
63-
connection = new JmsConnectionFactory(brokerUri()).createConnection();
6471
connection.start();
6572

6673
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
@@ -84,7 +91,6 @@ public void testCantConsumeFromTemporaryQueueCreatedOnAnotherConnection() throws
8491
@Test
8592
@Timeout(60)
8693
public void testCantSendToTemporaryQueueFromClosedConnection() throws Exception {
87-
connection = new JmsConnectionFactory(brokerUri()).createConnection();
8894
connection.start();
8995

9096
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
@@ -113,7 +119,6 @@ public void testCantSendToTemporaryQueueFromClosedConnection() throws Exception
113119
@Test
114120
@Timeout(60)
115121
public void testCantDeleteTemporaryQueueWithConsumers() throws Exception {
116-
connection = new JmsConnectionFactory(brokerUri()).createConnection();
117122
connection.start();
118123

119124
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// The contents of this file are subject to the Mozilla Public License
2+
// Version 2.0 (the "License"); you may not use this file except in
3+
// compliance with the License. You may obtain a copy of the License
4+
// at https://www.mozilla.org/en-US/MPL/2.0/
5+
//
6+
// Software distributed under the License is distributed on an "AS IS"
7+
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
8+
// the License for the specific language governing rights and
9+
// limitations under the License.
10+
//
11+
// The Original Code is RabbitMQ.
12+
//
13+
// The Initial Developer of the Original Code is Pivotal Software, Inc.
14+
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc.
15+
// and/or its subsidiaries. All rights reserved.
16+
//
17+
package com.rabbitmq.amqp.tests.jms;
18+
19+
import java.lang.annotation.*;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
22+
@Target(ElementType.TYPE)
23+
@Retention(RetentionPolicy.RUNTIME)
24+
@Documented
25+
@ExtendWith(JmsTestInfrastructureExtension.class)
26+
public @interface JmsTestInfrastructure {}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// The contents of this file are subject to the Mozilla Public License
2+
// Version 2.0 (the "License"); you may not use this file except in
3+
// compliance with the License. You may obtain a copy of the License
4+
// at https://www.mozilla.org/en-US/MPL/2.0/
5+
//
6+
// Software distributed under the License is distributed on an "AS IS"
7+
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
8+
// the License for the specific language governing rights and
9+
// limitations under the License.
10+
//
11+
// The Original Code is RabbitMQ.
12+
//
13+
// The Initial Developer of the Original Code is Pivotal Software, Inc.
14+
// Copyright (c) 2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
15+
//
16+
package com.rabbitmq.amqp.tests.jms;
17+
18+
19+
import com.rabbitmq.client.amqp.Connection;
20+
import com.rabbitmq.client.amqp.Environment;
21+
import com.rabbitmq.client.amqp.impl.AmqpEnvironmentBuilder;
22+
import java.lang.reflect.Field;
23+
import org.junit.jupiter.api.extension.*;
24+
25+
final class JmsTestInfrastructureExtension
26+
implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
27+
28+
private static final ExtensionContext.Namespace NAMESPACE =
29+
ExtensionContext.Namespace.create(JmsTestInfrastructureExtension.class);
30+
31+
private static ExtensionContext.Store store(ExtensionContext extensionContext) {
32+
return extensionContext.getRoot().getStore(NAMESPACE);
33+
}
34+
35+
private static Field field(Class<?> cls, String name) {
36+
Field field = null;
37+
while (field == null && cls != null) {
38+
try {
39+
field = cls.getDeclaredField(name);
40+
} catch (NoSuchFieldException e) {
41+
cls = cls.getSuperclass();
42+
}
43+
}
44+
return field;
45+
}
46+
47+
@Override
48+
public void beforeAll(ExtensionContext context) {
49+
50+
}
51+
52+
@Override
53+
public void beforeEach(ExtensionContext context) throws Exception {
54+
Field field = field(context.getTestInstance().get().getClass(), "destination");
55+
if (field != null) {
56+
field.setAccessible(true);
57+
String destination = TestUtils.name(context);
58+
field.set(context.getTestInstance().get(), destination);
59+
try (Environment environment = new AmqpEnvironmentBuilder().build();
60+
Connection connection = environment.connectionBuilder().uri(TestUtils.brokerUri()).build()) {
61+
connection.management().queue(destination).declare();
62+
}
63+
}
64+
}
65+
66+
@Override
67+
public void afterEach(ExtensionContext context) throws Exception {
68+
Field field = field(context.getTestInstance().get().getClass(), "destination");
69+
if (field != null) {
70+
field.setAccessible(true);
71+
String destination = (String) field.get(context.getTestInstance().get());
72+
try (Environment environment = new AmqpEnvironmentBuilder().build();
73+
Connection connection = environment.connectionBuilder().uri(TestUtils.brokerUri()).build()) {
74+
connection.management().queueDelete(destination);
75+
}
76+
}
77+
}
78+
79+
@Override
80+
public void afterAll(ExtensionContext context) {
81+
82+
}
83+
}

0 commit comments

Comments
 (0)