From 0f8b50970d5fe0be1cde6c11da2bdf18505673c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Cogolu=C3=A8gnes?= Date: Thu, 13 Feb 2025 08:40:50 +0100 Subject: [PATCH 1/4] Set up JMS destination in JUnit extension (cherry picked from commit cb59ad877cd3e9c55df57af980ff81d63167130d) --- deps/rabbit/test/amqp_jms_SUITE.erl | 12 +- deps/rabbit/test/amqp_jms_SUITE_data/pom.xml | 20 + .../amqp/tests/jms/JmsConnectionTest.java | 39 +- .../amqp/tests/jms/JmsTemporaryQueueTest.java | 7 +- .../com/rabbitmq/amqp/tests/jms/JmsTest.java | 348 +++++++++--------- .../jms/JmsTestInfrastructureExtension.java | 103 ++++-- .../rabbitmq/amqp/tests/jms/TestUtils.java | 38 +- 7 files changed, 309 insertions(+), 258 deletions(-) diff --git a/deps/rabbit/test/amqp_jms_SUITE.erl b/deps/rabbit/test/amqp_jms_SUITE.erl index 7a5462eda3b0..d0fcfc9904c6 100644 --- a/deps/rabbit/test/amqp_jms_SUITE.erl +++ b/deps/rabbit/test/amqp_jms_SUITE.erl @@ -122,10 +122,8 @@ jms_temporary_queue(Config) -> %% Send different message types from JMS client to JMS client. message_types_jms_to_jms(Config) -> - TestName = QName = atom_to_binary(?FUNCTION_NAME), - ok = declare_queue(QName, <<"quorum">>, Config), - ok = run_jms_test(TestName, [{"-Dqueue=~ts", [rabbitmq_amqp_address:queue(QName)]}], Config), - ok = delete_queue(QName, Config). + TestName = atom_to_binary(?FUNCTION_NAME), + ok = run_jms_test(TestName, [], Config). %% Send different message types from JMS client to Erlang AMQP 1.0 client. message_types_jms_to_amqp(Config) -> @@ -133,10 +131,8 @@ message_types_jms_to_amqp(Config) -> ok = run_jms_test(TestName, [], Config). temporary_queue_rpc(Config) -> - TestName = QName = atom_to_binary(?FUNCTION_NAME), - ok = declare_queue(QName, <<"classic">>, Config), - ok = run_jms_test(TestName, [{"-Dqueue=~ts", [rabbitmq_amqp_address:queue(QName)]}], Config), - ok = delete_queue(QName, Config). + TestName = atom_to_binary(?FUNCTION_NAME), + ok = run_jms_test(TestName, [], Config). temporary_queue_delete(Config) -> TestName = atom_to_binary(?FUNCTION_NAME), diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/pom.xml b/deps/rabbit/test/amqp_jms_SUITE_data/pom.xml index 4d3219578bfa..c18e63ce1b5a 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/pom.xml +++ b/deps/rabbit/test/amqp_jms_SUITE_data/pom.xml @@ -89,6 +89,26 @@ + origin/main + + // 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) $YEAR Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. + // and/or its subsidiaries. All rights reserved. + // + + diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsConnectionTest.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsConnectionTest.java index d526cbbee4ff..e784e5455c9a 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsConnectionTest.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsConnectionTest.java @@ -14,7 +14,6 @@ // 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.Cli.startBroker; @@ -41,12 +40,12 @@ @JmsTestInfrastructure public class JmsConnectionTest { - String destination; + ConnectionFactory factory; @Test @Timeout(30) public void testCreateConnection() throws Exception { - try (Connection connection = connection()) { + try (Connection connection = factory.createConnection()) { assertNotNull(connection); } } @@ -54,7 +53,7 @@ public void testCreateConnection() throws Exception { @Test @Timeout(30) public void testCreateConnectionAndStart() throws Exception { - try (Connection connection = connection()) { + try (Connection connection = factory.createConnection()) { assertNotNull(connection); connection.start(); } @@ -65,7 +64,6 @@ public void testCreateConnectionAndStart() throws Exception { // Currently not supported by RabbitMQ. @Disabled public void testCreateWithDuplicateClientIdFails() throws Exception { - JmsConnectionFactory factory = (JmsConnectionFactory) connectionFactory(); JmsConnection connection1 = (JmsConnection) factory.createConnection(); connection1.setClientID("Test"); assertNotNull(connection1); @@ -89,7 +87,7 @@ public void testSetClientIdAfterStartedFails() { assertThrows( JMSException.class, () -> { - try (Connection connection = connection()) { + try (Connection connection = factory.createConnection()) { connection.setClientID("Test"); connection.start(); connection.setClientID("NewTest"); @@ -100,9 +98,10 @@ public void testSetClientIdAfterStartedFails() { @Test @Timeout(30) public void testCreateConnectionAsSystemAdmin() throws Exception { - JmsConnectionFactory factory = (JmsConnectionFactory) connectionFactory(); - factory.setUsername(adminUsername()); - factory.setPassword(adminPassword()); + JmsConnectionFactory f = (JmsConnectionFactory) factory; + + f.setUsername(adminUsername()); + f.setPassword(adminPassword()); try (Connection connection = factory.createConnection()) { assertNotNull(connection); connection.start(); @@ -112,8 +111,7 @@ public void testCreateConnectionAsSystemAdmin() throws Exception { @Test @Timeout(30) public void testCreateConnectionCallSystemAdmin() throws Exception { - try (Connection connection = - connectionFactory().createConnection(adminUsername(), adminPassword())) { + try (Connection connection = factory.createConnection(adminUsername(), adminPassword())) { assertNotNull(connection); connection.start(); } @@ -121,13 +119,13 @@ public void testCreateConnectionCallSystemAdmin() throws Exception { @Test @Timeout(30) - public void testCreateConnectionAsUnknwonUser() { + public void testCreateConnectionAsUnknownUser() { assertThrows( JMSSecurityException.class, () -> { - JmsConnectionFactory factory = (JmsConnectionFactory) connectionFactory(); - factory.setUsername("unknown"); - factory.setPassword("unknown"); + JmsConnectionFactory f = (JmsConnectionFactory) factory; + f.setUsername("unknown"); + f.setPassword("unknown"); try (Connection connection = factory.createConnection()) { assertNotNull(connection); connection.start(); @@ -137,11 +135,11 @@ public void testCreateConnectionAsUnknwonUser() { @Test @Timeout(30) - public void testCreateConnectionCallUnknwonUser() { + public void testCreateConnectionCallUnknownUser() { assertThrows( JMSSecurityException.class, () -> { - try (Connection connection = connectionFactory().createConnection("unknown", "unknown")) { + try (Connection connection = factory.createConnection("unknown", "unknown")) { assertNotNull(connection); connection.start(); } @@ -150,11 +148,10 @@ public void testCreateConnectionCallUnknwonUser() { @Test @Timeout(30) - public void testBrokerStopWontHangConnectionClose() throws Exception { - Connection connection = connection(); + public void testBrokerStopWontHangConnectionClose(Queue queue) throws Exception { + Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - Queue queue = queue(destination); connection.start(); MessageProducer producer = session.createProducer(queue); @@ -179,7 +176,7 @@ public void testBrokerStopWontHangConnectionClose() throws Exception { @Timeout(60) public void testConnectionExceptionBrokerStop() throws Exception { final CountDownLatch latch = new CountDownLatch(1); - try (Connection connection = connection()) { + try (Connection connection = factory.createConnection()) { connection.setExceptionListener(exception -> latch.countDown()); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTemporaryQueueTest.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTemporaryQueueTest.java index ae60fa4b8a31..dd2665dbbaac 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTemporaryQueueTest.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTemporaryQueueTest.java @@ -14,11 +14,9 @@ // 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; @@ -35,13 +33,16 @@ * Based on * https://github.com/apache/qpid-jms/tree/main/qpid-jms-interop-tests/qpid-jms-activemq-tests. */ +@JmsTestInfrastructure public class JmsTemporaryQueueTest { + ConnectionFactory factory; + Connection connection; @BeforeEach void init() throws JMSException { - connection = connection(); + connection = factory.createConnection(); } @AfterEach diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java index 71e736a4e016..eaa0e7a9c3d3 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java @@ -1,3 +1,19 @@ +// 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 static com.rabbitmq.amqp.tests.jms.TestUtils.protonClient; @@ -5,209 +21,175 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; -import jakarta.jms.*; -import java.util.*; -import java.util.concurrent.TimeUnit; -import javax.naming.Context; - import com.rabbitmq.qpid.protonj2.client.Client; import com.rabbitmq.qpid.protonj2.client.Delivery; import com.rabbitmq.qpid.protonj2.client.Receiver; +import jakarta.jms.*; import jakarta.jms.Queue; +import java.util.*; +import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; @JmsTestInfrastructure public class JmsTest { - private javax.naming.Context getContext() throws Exception{ - // Configure a JNDI initial context, see - // https://github.com/apache/qpid-jms/blob/main/qpid-jms-docs/Configuration.md#configuring-a-jndi-initialcontext - Hashtable env = new Hashtable<>(); - env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory"); - - String uri = System.getProperty("rmq_broker_uri", "amqp://localhost:5672"); - // For a list of options, see - // https://github.com/apache/qpid-jms/blob/main/qpid-jms-docs/Configuration.md#jms-configuration-options - uri = uri + "?jms.clientID=my-client-id"; - env.put("connectionfactory.myConnection", uri); - - String queueName = System.getProperty("queue"); - if (queueName != null) { - env.put("queue.myQueue", queueName); - } - - javax.naming.Context context = new javax.naming.InitialContext(env); - return context; + ConnectionFactory factory; + + // https://jakarta.ee/specifications/messaging/3.1/jakarta-messaging-spec-3.1#jakarta-messaging-message-types + @Test + public void message_types_jms_to_jms(Queue queue) throws Exception { + try (Connection connection = factory.createConnection()) { + Session session = connection.createSession(); + MessageProducer producer = session.createProducer(queue); + MessageConsumer consumer = session.createConsumer(queue); + connection.start(); + + // TextMessage + String msg1 = "msg1"; + TextMessage textMessage = session.createTextMessage(msg1); + producer.send(textMessage); + TextMessage receivedTextMessage = (TextMessage) consumer.receive(5000); + assertEquals(msg1, receivedTextMessage.getText()); + + // BytesMessage + String msg2 = "msg2"; + BytesMessage bytesMessage = session.createBytesMessage(); + bytesMessage.writeUTF(msg2); + producer.send(bytesMessage); + BytesMessage receivedBytesMessage = (BytesMessage) consumer.receive(5000); + assertEquals(msg2, receivedBytesMessage.readUTF()); + + // MapMessage + MapMessage mapMessage = session.createMapMessage(); + mapMessage.setString("key1", "value"); + mapMessage.setBoolean("key2", true); + mapMessage.setDouble("key3", 1.0); + mapMessage.setLong("key4", 1L); + producer.send(mapMessage); + MapMessage receivedMapMessage = (MapMessage) consumer.receive(5000); + assertEquals("value", receivedMapMessage.getString("key1")); + assertEquals(true, receivedMapMessage.getBoolean("key2")); + assertEquals(1.0, receivedMapMessage.getDouble("key3")); + assertEquals(1L, receivedMapMessage.getLong("key4")); + + // StreamMessage + StreamMessage streamMessage = session.createStreamMessage(); + streamMessage.writeString("value"); + streamMessage.writeBoolean(true); + streamMessage.writeDouble(1.0); + streamMessage.writeLong(1L); + producer.send(streamMessage); + StreamMessage receivedStreamMessage = (StreamMessage) consumer.receive(5000); + assertEquals("value", receivedStreamMessage.readString()); + assertEquals(true, receivedStreamMessage.readBoolean()); + assertEquals(1.0, receivedStreamMessage.readDouble()); + assertEquals(1L, receivedStreamMessage.readLong()); + + // ObjectMessage + ObjectMessage objectMessage = session.createObjectMessage(); + ArrayList list = new ArrayList<>(Arrays.asList(1, 2, 3)); + objectMessage.setObject(list); + producer.send(objectMessage); + ObjectMessage receivedObjectMessage = (ObjectMessage) consumer.receive(5000); + assertEquals(list, receivedObjectMessage.getObject()); } + } - // https://jakarta.ee/specifications/messaging/3.1/jakarta-messaging-spec-3.1#jakarta-messaging-message-types - @Test - public void message_types_jms_to_jms() throws Exception { - Context context = getContext(); - ConnectionFactory factory = (ConnectionFactory) context.lookup("myConnection"); - - try (Connection connection = factory.createConnection()) { - Session session = connection.createSession(); - Destination queue = (Destination) context.lookup("myQueue"); - MessageProducer producer = session.createProducer(queue); - MessageConsumer consumer = session.createConsumer(queue); - connection.start(); - - // TextMessage - String msg1 = "msg1"; - TextMessage textMessage = session.createTextMessage(msg1); - producer.send(textMessage); - TextMessage receivedTextMessage = (TextMessage) consumer.receive(5000); - assertEquals(msg1, receivedTextMessage.getText()); - - // BytesMessage - String msg2 = "msg2"; - BytesMessage bytesMessage = session.createBytesMessage(); - bytesMessage.writeUTF(msg2); - producer.send(bytesMessage); - BytesMessage receivedBytesMessage = (BytesMessage) consumer.receive(5000); - assertEquals(msg2, receivedBytesMessage.readUTF()); - - // MapMessage - MapMessage mapMessage = session.createMapMessage(); - mapMessage.setString("key1", "value"); - mapMessage.setBoolean("key2", true); - mapMessage.setDouble("key3", 1.0); - mapMessage.setLong("key4", 1L); - producer.send(mapMessage); - MapMessage receivedMapMessage = (MapMessage) consumer.receive(5000); - assertEquals("value", receivedMapMessage.getString("key1")); - assertEquals(true, receivedMapMessage.getBoolean("key2")); - assertEquals(1.0, receivedMapMessage.getDouble("key3")); - assertEquals(1L, receivedMapMessage.getLong("key4")); - - // StreamMessage - StreamMessage streamMessage = session.createStreamMessage(); - streamMessage.writeString("value"); - streamMessage.writeBoolean(true); - streamMessage.writeDouble(1.0); - streamMessage.writeLong(1L); - producer.send(streamMessage); - StreamMessage receivedStreamMessage = (StreamMessage) consumer.receive(5000); - assertEquals("value", receivedStreamMessage.readString()); - assertEquals(true, receivedStreamMessage.readBoolean()); - assertEquals(1.0, receivedStreamMessage.readDouble()); - assertEquals(1L, receivedStreamMessage.readLong()); - - // ObjectMessage - ObjectMessage objectMessage = session.createObjectMessage(); - ArrayList list = new ArrayList<>(Arrays.asList(1, 2, 3)); - objectMessage.setObject(list); - producer.send(objectMessage); - ObjectMessage receivedObjectMessage = (ObjectMessage) consumer.receive(5000); - assertEquals(list, receivedObjectMessage.getObject()); - } + @Test + public void message_types_jms_to_amqp(Queue queue) throws Exception { + String msg1 = "msg1🥕"; + try (Connection connection = factory.createConnection()) { + Session session = connection.createSession(); + MessageProducer producer = session.createProducer(queue); + + // TextMessage + TextMessage textMessage = session.createTextMessage(msg1); + producer.send(textMessage); + + // MapMessage + MapMessage mapMessage = session.createMapMessage(); + mapMessage.setString("key1", "value"); + mapMessage.setBoolean("key2", true); + mapMessage.setDouble("key3", -1.1); + mapMessage.setLong("key4", -1L); + producer.send(mapMessage); + + // StreamMessage + StreamMessage streamMessage = session.createStreamMessage(); + streamMessage.writeString("value"); + streamMessage.writeBoolean(true); + streamMessage.writeDouble(-1.1); + streamMessage.writeLong(-1L); + producer.send(streamMessage); } - String destination; - - @Test - public void message_types_jms_to_amqp() throws Exception { - Context context = getContext(); - ConnectionFactory factory = (ConnectionFactory) context.lookup("myConnection"); - - Queue queue = TestUtils.queue(destination); - String msg1 = "msg1🥕"; - try (Connection connection = factory.createConnection()) { - Session session = connection.createSession(); - MessageProducer producer = session.createProducer(queue); - - // TextMessage - TextMessage textMessage = session.createTextMessage(msg1); - producer.send(textMessage); - - // MapMessage - MapMessage mapMessage = session.createMapMessage(); - mapMessage.setString("key1", "value"); - mapMessage.setBoolean("key2", true); - mapMessage.setDouble("key3", -1.1); - mapMessage.setLong("key4", -1L); - producer.send(mapMessage); - - // StreamMessage - StreamMessage streamMessage = session.createStreamMessage(); - streamMessage.writeString("value"); - streamMessage.writeBoolean(true); - streamMessage.writeDouble(-1.1); - streamMessage.writeLong(-1L); - producer.send(streamMessage); - } - - try (Client client = protonClient(); - com.rabbitmq.qpid.protonj2.client.Connection amqpConnection = protonConnection(client)) { - Receiver receiver = amqpConnection.openReceiver(queue.getQueueName()); - Delivery delivery = receiver.receive(10, TimeUnit.SECONDS); - assertNotNull(delivery); - assertEquals(msg1, delivery.message().body()); - - delivery = receiver.receive(10, TimeUnit.SECONDS); - assertNotNull(delivery); - com.rabbitmq.qpid.protonj2.client.Message> mapMessage = delivery.message(); - assertThat(mapMessage.body()).containsEntry("key1", "value") - .containsEntry("key2", true) - .containsEntry("key3", -1.1) - .containsEntry("key4", -1L); - - delivery = receiver.receive(10, TimeUnit.SECONDS); - assertNotNull(delivery); - com.rabbitmq.qpid.protonj2.client.Message> listMessage = delivery.message(); - assertThat(listMessage.body()).containsExactly("value", true, -1.1, -1L); + try (Client client = protonClient(); + com.rabbitmq.qpid.protonj2.client.Connection amqpConnection = protonConnection(client)) { + Receiver receiver = amqpConnection.openReceiver(queue.getQueueName()); + Delivery delivery = receiver.receive(10, TimeUnit.SECONDS); + assertNotNull(delivery); + assertEquals(msg1, delivery.message().body()); + + delivery = receiver.receive(10, TimeUnit.SECONDS); + assertNotNull(delivery); + com.rabbitmq.qpid.protonj2.client.Message> mapMessage = + delivery.message(); + assertThat(mapMessage.body()) + .containsEntry("key1", "value") + .containsEntry("key2", true) + .containsEntry("key3", -1.1) + .containsEntry("key4", -1L); + + delivery = receiver.receive(10, TimeUnit.SECONDS); + assertNotNull(delivery); + com.rabbitmq.qpid.protonj2.client.Message> listMessage = delivery.message(); + assertThat(listMessage.body()).containsExactly("value", true, -1.1, -1L); } } // Test that Request/reply pattern using a TemporaryQueue works. // https://jakarta.ee/specifications/messaging/3.1/jakarta-messaging-spec-3.1#requestreply-pattern-using-a-temporaryqueue-jakarta-ee @Test - public void temporary_queue_rpc() throws Exception { - Context context = getContext(); - ConnectionFactory factory = (ConnectionFactory) context.lookup("myConnection"); - - try (JMSContext clientContext = factory.createContext()) { - Destination responseQueue = clientContext.createTemporaryQueue(); - JMSConsumer clientConsumer = clientContext.createConsumer(responseQueue); - - Destination requestQueue = (Destination) context.lookup("myQueue"); - TextMessage clientRequestMessage = clientContext.createTextMessage("hello"); - clientContext.createProducer(). - setJMSReplyTo(responseQueue). - send(requestQueue, clientRequestMessage); - - // Let's open a new connection to simulate the RPC server. - try (JMSContext serverContext = factory.createContext()) { - JMSConsumer serverConsumer = serverContext.createConsumer(requestQueue); - TextMessage serverRequestMessage = (TextMessage) serverConsumer.receive(5000); - - TextMessage serverResponseMessage = serverContext.createTextMessage( - serverRequestMessage.getText().toUpperCase()); - serverContext.createProducer(). - send(serverRequestMessage.getJMSReplyTo(), serverResponseMessage); - } - - TextMessage clientResponseMessage = (TextMessage) clientConsumer.receive(5000); - assertEquals("HELLO", clientResponseMessage.getText()); - } + public void temporary_queue_rpc(Queue requestQueue) throws Exception { + try (JMSContext clientContext = factory.createContext()) { + Destination responseQueue = clientContext.createTemporaryQueue(); + JMSConsumer clientConsumer = clientContext.createConsumer(responseQueue); + + TextMessage clientRequestMessage = clientContext.createTextMessage("hello"); + clientContext + .createProducer() + .setJMSReplyTo(responseQueue) + .send(requestQueue, clientRequestMessage); + + // Let's open a new connection to simulate the RPC server. + try (JMSContext serverContext = factory.createContext()) { + JMSConsumer serverConsumer = serverContext.createConsumer(requestQueue); + TextMessage serverRequestMessage = (TextMessage) serverConsumer.receive(5000); + + TextMessage serverResponseMessage = + serverContext.createTextMessage(serverRequestMessage.getText().toUpperCase()); + serverContext + .createProducer() + .send(serverRequestMessage.getJMSReplyTo(), serverResponseMessage); + } + + TextMessage clientResponseMessage = (TextMessage) clientConsumer.receive(5000); + assertEquals("HELLO", clientResponseMessage.getText()); } + } - // Test that a temporary queue can be deleted. - @Test - public void temporary_queue_delete() throws Exception { - Context context = getContext(); - ConnectionFactory factory = (ConnectionFactory) context.lookup("myConnection"); - - try (JMSContext clientContext = factory.createContext()) { - TemporaryQueue queue = clientContext.createTemporaryQueue(); - queue.delete(); - try { - clientContext.createProducer().send(queue, "hello"); - fail("should not be able to create producer for deleted temporary queue"); - } catch (IllegalStateRuntimeException expectedException) { - assertEquals("Temporary destination has been deleted", expectedException.getMessage()); - } - } + // Test that a temporary queue can be deleted. + @Test + public void temporary_queue_delete() throws Exception { + try (JMSContext clientContext = factory.createContext()) { + TemporaryQueue queue = clientContext.createTemporaryQueue(); + queue.delete(); + try { + clientContext.createProducer().send(queue, "hello"); + fail("should not be able to create producer for deleted temporary queue"); + } catch (IllegalStateRuntimeException expectedException) { + assertEquals("Temporary destination has been deleted", expectedException.getMessage()); + } } + } } diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTestInfrastructureExtension.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTestInfrastructureExtension.java index 2254b00ab278..dbe497a30b62 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTestInfrastructureExtension.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTestInfrastructureExtension.java @@ -11,19 +11,29 @@ // 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.util.Collections.singletonMap; import com.rabbitmq.client.amqp.Connection; import com.rabbitmq.client.amqp.Environment; import com.rabbitmq.client.amqp.impl.AmqpEnvironmentBuilder; +import jakarta.jms.ConnectionFactory; +import jakarta.jms.Queue; import java.lang.reflect.Field; +import java.lang.reflect.Parameter; +import java.util.Collections; +import java.util.Optional; +import java.util.function.Predicate; +import javax.naming.Context; +import javax.naming.NamingException; import org.junit.jupiter.api.extension.*; final class JmsTestInfrastructureExtension - implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback { + implements BeforeEachCallback, AfterEachCallback, ParameterResolver { private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(JmsTestInfrastructureExtension.class); @@ -32,52 +42,87 @@ 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(); + private static Optional field(Class cls, Predicate predicate) { + for (Field field : cls.getDeclaredFields()) { + if (predicate.test(field)) { + return Optional.of(field); } } - return field; + return Optional.empty(); } - @Override - public void beforeAll(ExtensionContext context) { - + private static boolean isQueue(Parameter parameter) { + return Queue.class.isAssignableFrom(parameter.getType()); } @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(); + if (context.getTestMethod().isPresent()) { + String queueName; + for (Parameter parameter : context.getTestMethod().get().getParameters()) { + if (isQueue(parameter)) { + queueName = TestUtils.name(context); + String queueAddress = TestUtils.queueAddress(queueName); + try (Environment environment = new AmqpEnvironmentBuilder().build(); + Connection connection = + environment.connectionBuilder().uri(TestUtils.brokerUri()).build()) { + connection.management().queue(queueName).declare(); + } + store(context).put("queueName", queueName); + Context jndiContext = TestUtils.context(singletonMap("queue." + queueName, queueAddress)); + store(context).put("jndiContext", jndiContext); + } + } + + if (context.getTestInstance().isPresent()) { + Optional connectionFactoryField = + field( + context.getTestInstance().get().getClass(), + field -> ConnectionFactory.class.isAssignableFrom(field.getType())); + if (connectionFactoryField.isPresent()) { + connectionFactoryField.get().setAccessible(true); + Context jndiContext = + store(context) + .getOrComputeIfAbsent( + "jndiContext", k -> TestUtils.context(Collections.emptyMap()), Context.class); + ConnectionFactory connectionFactory = + (ConnectionFactory) jndiContext.lookup("testConnectionFactory"); + connectionFactoryField.get().set(context.getTestInstance().get(), connectionFactory); + } } } } @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()); + public void afterEach(ExtensionContext context) { + String queueName = store(context).remove("queueName", String.class); + if (queueName != null) { try (Environment environment = new AmqpEnvironmentBuilder().build(); - Connection connection = environment.connectionBuilder().uri(TestUtils.brokerUri()).build()) { - connection.management().queueDelete(destination); + Connection connection = + environment.connectionBuilder().uri(TestUtils.brokerUri()).build()) { + connection.management().queueDelete(queueName); } } + store(context).remove("jndiContext", Context.class); } @Override - public void afterAll(ExtensionContext context) { + public boolean supportsParameter( + ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return isQueue(parameterContext.getParameter()); + } + @Override + public Object resolveParameter( + ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + String queueName = store(extensionContext).get("queueName", String.class); + Context jndiContext = store(extensionContext).get("jndiContext", Context.class); + try { + return jndiContext.lookup(queueName); + } catch (NamingException e) { + throw new RuntimeException(e); + } } } diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/TestUtils.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/TestUtils.java index 8cb972cbbbe2..7d79e269532e 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/TestUtils.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/TestUtils.java @@ -14,7 +14,6 @@ // 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; @@ -22,16 +21,14 @@ import com.rabbitmq.qpid.protonj2.client.Client; import com.rabbitmq.qpid.protonj2.client.ConnectionOptions; import com.rabbitmq.qpid.protonj2.client.exceptions.ClientException; -import jakarta.jms.Connection; -import jakarta.jms.ConnectionFactory; -import jakarta.jms.JMSException; -import jakarta.jms.Queue; import java.lang.reflect.Method; import java.net.URI; import java.net.URISyntaxException; +import java.util.Hashtable; +import java.util.Map; import java.util.UUID; -import org.apache.qpid.jms.JmsConnectionFactory; -import org.apache.qpid.jms.JmsQueue; +import javax.naming.Context; +import javax.naming.NamingException; import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.extension.ExtensionContext; @@ -72,17 +69,30 @@ static String adminPassword() { return "guest"; } - static ConnectionFactory connectionFactory() { - return new JmsConnectionFactory(brokerUri()); - } + static Context context(Map extraEnv) { + // Configure a JNDI initial context, see + // https://github.com/apache/qpid-jms/blob/main/qpid-jms-docs/Configuration.md#configuring-a-jndi-initialcontext + Hashtable env = new Hashtable<>(); + env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory"); + + String uri = brokerUri(); + // For a list of options, see + // https://github.com/apache/qpid-jms/blob/main/qpid-jms-docs/Configuration.md#jms-configuration-options + uri = uri + "?jms.clientID=my-client-id"; + env.put("connectionfactory.testConnectionFactory", uri); + + env.putAll(extraEnv); - static Connection connection() throws JMSException { - return connectionFactory().createConnection(); + try { + return new javax.naming.InitialContext(env); + } catch (NamingException e) { + throw new RuntimeException(e); + } } - static Queue queue(String name) { + static String queueAddress(String name) { // no path encoding, use names with e.g. ASCII characters only - return new JmsQueue("/queues/" + name); + return "/queues/" + name; } static Client protonClient() { From 5703a17e8af5529b231e47277cdc90aee59faeeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Cogolu=C3=A8gnes?= Date: Thu, 13 Feb 2025 10:32:38 +0100 Subject: [PATCH 2/4] Use AssertJ instead of JUnit assertions in JMS tests (cherry picked from commit d574e66dccceaf3b59ffd4d72b7be4a12c84c1c6) --- deps/rabbit/test/amqp_jms_SUITE_data/pom.xml | 7 ++ .../java/com/rabbitmq/amqp/tests/jms/Cli.java | 3 +- .../amqp/tests/jms/JmsConnectionTest.java | 78 +++++++++---------- .../amqp/tests/jms/JmsTemporaryQueueTest.java | 7 +- .../com/rabbitmq/amqp/tests/jms/JmsTest.java | 36 ++++----- .../src/test/resources/logback-test.xml | 2 +- 6 files changed, 68 insertions(+), 65 deletions(-) diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/pom.xml b/deps/rabbit/test/amqp_jms_SUITE_data/pom.xml index c18e63ce1b5a..5583dc92a31a 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/pom.xml +++ b/deps/rabbit/test/amqp_jms_SUITE_data/pom.xml @@ -50,6 +50,13 @@ ${assertj.version} test + + + com.google.googlejavaformat + google-java-format + ${google-java-format.version} + test + diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/Cli.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/Cli.java index 2dc08413eae4..2dea6c481f11 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/Cli.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/Cli.java @@ -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; diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsConnectionTest.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsConnectionTest.java index e784e5455c9a..a02e6b6b54bd 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsConnectionTest.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsConnectionTest.java @@ -19,10 +19,7 @@ import static com.rabbitmq.amqp.tests.jms.Cli.startBroker; import static com.rabbitmq.amqp.tests.jms.Cli.stopBroker; import static com.rabbitmq.amqp.tests.jms.TestUtils.*; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.assertj.core.api.Assertions.*; import jakarta.jms.*; import java.util.concurrent.CountDownLatch; @@ -46,7 +43,7 @@ public class JmsConnectionTest { @Timeout(30) public void testCreateConnection() throws Exception { try (Connection connection = factory.createConnection()) { - assertNotNull(connection); + assertThat(connection).isNotNull(); } } @@ -54,19 +51,18 @@ public void testCreateConnection() throws Exception { @Timeout(30) public void testCreateConnectionAndStart() throws Exception { try (Connection connection = factory.createConnection()) { - assertNotNull(connection); + assertThat(connection).isNotNull(); connection.start(); } } @Test @Timeout(30) - // Currently not supported by RabbitMQ. - @Disabled + @Disabled("Client ID conflict detection is not supported by RabbitMQ") public void testCreateWithDuplicateClientIdFails() throws Exception { JmsConnection connection1 = (JmsConnection) factory.createConnection(); connection1.setClientID("Test"); - assertNotNull(connection1); + assertThat(connection1).isNotNull(); connection1.start(); JmsConnection connection2 = (JmsConnection) factory.createConnection(); try { @@ -84,15 +80,15 @@ public void testCreateWithDuplicateClientIdFails() throws Exception { @Test public void testSetClientIdAfterStartedFails() { - assertThrows( - JMSException.class, - () -> { - try (Connection connection = factory.createConnection()) { - connection.setClientID("Test"); - connection.start(); - connection.setClientID("NewTest"); - } - }); + assertThatThrownBy( + () -> { + try (Connection connection = factory.createConnection()) { + connection.setClientID("Test"); + connection.start(); + connection.setClientID("NewTest"); + } + }) + .isInstanceOf(JMSException.class); } @Test @@ -103,7 +99,7 @@ public void testCreateConnectionAsSystemAdmin() throws Exception { f.setUsername(adminUsername()); f.setPassword(adminPassword()); try (Connection connection = factory.createConnection()) { - assertNotNull(connection); + assertThat(connection).isNotNull(); connection.start(); } } @@ -112,7 +108,7 @@ public void testCreateConnectionAsSystemAdmin() throws Exception { @Timeout(30) public void testCreateConnectionCallSystemAdmin() throws Exception { try (Connection connection = factory.createConnection(adminUsername(), adminPassword())) { - assertNotNull(connection); + assertThat(connection).isNotNull(); connection.start(); } } @@ -120,30 +116,30 @@ public void testCreateConnectionCallSystemAdmin() throws Exception { @Test @Timeout(30) public void testCreateConnectionAsUnknownUser() { - assertThrows( - JMSSecurityException.class, - () -> { - JmsConnectionFactory f = (JmsConnectionFactory) factory; - f.setUsername("unknown"); - f.setPassword("unknown"); - try (Connection connection = factory.createConnection()) { - assertNotNull(connection); - connection.start(); - } - }); + assertThatThrownBy( + () -> { + JmsConnectionFactory f = (JmsConnectionFactory) factory; + f.setUsername("unknown"); + f.setPassword("unknown"); + try (Connection connection = factory.createConnection()) { + assertThat(connection).isNotNull(); + connection.start(); + } + }) + .isInstanceOf(JMSSecurityException.class); } @Test @Timeout(30) public void testCreateConnectionCallUnknownUser() { - assertThrows( - JMSSecurityException.class, - () -> { - try (Connection connection = factory.createConnection("unknown", "unknown")) { - assertNotNull(connection); - connection.start(); - } - }); + assertThatThrownBy( + () -> { + try (Connection connection = factory.createConnection("unknown", "unknown")) { + assertThat(connection).isNotNull(); + connection.start(); + } + }) + .isInstanceOf(JMSSecurityException.class); } @Test @@ -180,11 +176,11 @@ public void testConnectionExceptionBrokerStop() throws Exception { connection.setExceptionListener(exception -> latch.countDown()); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - assertNotNull(session); + assertThat(session).isNotNull(); try { stopBroker(); - assertTrue(latch.await(10, TimeUnit.SECONDS)); + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); } finally { startBroker(); } diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTemporaryQueueTest.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTemporaryQueueTest.java index dd2665dbbaac..63a257ff86cb 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTemporaryQueueTest.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTemporaryQueueTest.java @@ -17,8 +17,7 @@ package com.rabbitmq.amqp.tests.jms; import static com.rabbitmq.amqp.tests.jms.TestUtils.brokerUri; -import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.fail; +import static org.assertj.core.api.Assertions.*; import jakarta.jms.*; import jakarta.jms.IllegalStateException; @@ -56,14 +55,14 @@ public void testCreatePublishConsumeTemporaryQueue() throws Exception { connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - assertNotNull(session); + assertThat(session).isNotNull(); TemporaryQueue queue = session.createTemporaryQueue(); MessageConsumer consumer = session.createConsumer(queue); MessageProducer producer = session.createProducer(queue); String body = UUID.randomUUID().toString(); producer.send(session.createTextMessage(body)); - assertEquals(body, consumer.receive(60_000).getBody(String.class)); + assertThat(consumer.receive(60_000).getBody(String.class)).isEqualTo(body); } @Test diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java index eaa0e7a9c3d3..e56f8edbea2b 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java @@ -19,7 +19,7 @@ import static com.rabbitmq.amqp.tests.jms.TestUtils.protonClient; import static com.rabbitmq.amqp.tests.jms.TestUtils.protonConnection; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.fail; import com.rabbitmq.qpid.protonj2.client.Client; import com.rabbitmq.qpid.protonj2.client.Delivery; @@ -49,7 +49,7 @@ public void message_types_jms_to_jms(Queue queue) throws Exception { TextMessage textMessage = session.createTextMessage(msg1); producer.send(textMessage); TextMessage receivedTextMessage = (TextMessage) consumer.receive(5000); - assertEquals(msg1, receivedTextMessage.getText()); + assertThat(receivedTextMessage.getText()).isEqualTo(msg1); // BytesMessage String msg2 = "msg2"; @@ -57,7 +57,7 @@ public void message_types_jms_to_jms(Queue queue) throws Exception { bytesMessage.writeUTF(msg2); producer.send(bytesMessage); BytesMessage receivedBytesMessage = (BytesMessage) consumer.receive(5000); - assertEquals(msg2, receivedBytesMessage.readUTF()); + assertThat(receivedBytesMessage.readUTF()).isEqualTo(msg2); // MapMessage MapMessage mapMessage = session.createMapMessage(); @@ -67,10 +67,10 @@ public void message_types_jms_to_jms(Queue queue) throws Exception { mapMessage.setLong("key4", 1L); producer.send(mapMessage); MapMessage receivedMapMessage = (MapMessage) consumer.receive(5000); - assertEquals("value", receivedMapMessage.getString("key1")); - assertEquals(true, receivedMapMessage.getBoolean("key2")); - assertEquals(1.0, receivedMapMessage.getDouble("key3")); - assertEquals(1L, receivedMapMessage.getLong("key4")); + assertThat(receivedMapMessage.getString("key1")).isEqualTo("value"); + assertThat(receivedMapMessage.getBoolean("key2")).isTrue(); + assertThat(receivedMapMessage.getDouble("key3")).isEqualTo(1.0); + assertThat(receivedMapMessage.getLong("key4")).isEqualTo(1L); // StreamMessage StreamMessage streamMessage = session.createStreamMessage(); @@ -80,10 +80,10 @@ public void message_types_jms_to_jms(Queue queue) throws Exception { streamMessage.writeLong(1L); producer.send(streamMessage); StreamMessage receivedStreamMessage = (StreamMessage) consumer.receive(5000); - assertEquals("value", receivedStreamMessage.readString()); - assertEquals(true, receivedStreamMessage.readBoolean()); - assertEquals(1.0, receivedStreamMessage.readDouble()); - assertEquals(1L, receivedStreamMessage.readLong()); + assertThat(receivedStreamMessage.readString()).isEqualTo("value"); + assertThat(receivedStreamMessage.readBoolean()).isTrue(); + assertThat(receivedStreamMessage.readDouble()).isEqualTo(1.0); + assertThat(receivedStreamMessage.readLong()).isEqualTo(1L); // ObjectMessage ObjectMessage objectMessage = session.createObjectMessage(); @@ -91,7 +91,7 @@ public void message_types_jms_to_jms(Queue queue) throws Exception { objectMessage.setObject(list); producer.send(objectMessage); ObjectMessage receivedObjectMessage = (ObjectMessage) consumer.receive(5000); - assertEquals(list, receivedObjectMessage.getObject()); + assertThat(receivedObjectMessage.getObject()).isEqualTo(list); } } @@ -127,11 +127,11 @@ public void message_types_jms_to_amqp(Queue queue) throws Exception { com.rabbitmq.qpid.protonj2.client.Connection amqpConnection = protonConnection(client)) { Receiver receiver = amqpConnection.openReceiver(queue.getQueueName()); Delivery delivery = receiver.receive(10, TimeUnit.SECONDS); - assertNotNull(delivery); - assertEquals(msg1, delivery.message().body()); + assertThat(delivery).isNotNull(); + assertThat(delivery.message().body()).isEqualTo(msg1); delivery = receiver.receive(10, TimeUnit.SECONDS); - assertNotNull(delivery); + assertThat(delivery).isNotNull(); com.rabbitmq.qpid.protonj2.client.Message> mapMessage = delivery.message(); assertThat(mapMessage.body()) @@ -141,7 +141,7 @@ public void message_types_jms_to_amqp(Queue queue) throws Exception { .containsEntry("key4", -1L); delivery = receiver.receive(10, TimeUnit.SECONDS); - assertNotNull(delivery); + assertThat(delivery).isNotNull(); com.rabbitmq.qpid.protonj2.client.Message> listMessage = delivery.message(); assertThat(listMessage.body()).containsExactly("value", true, -1.1, -1L); } @@ -174,7 +174,7 @@ public void temporary_queue_rpc(Queue requestQueue) throws Exception { } TextMessage clientResponseMessage = (TextMessage) clientConsumer.receive(5000); - assertEquals("HELLO", clientResponseMessage.getText()); + assertThat(clientResponseMessage.getText()).isEqualTo("HELLO"); } } @@ -188,7 +188,7 @@ public void temporary_queue_delete() throws Exception { clientContext.createProducer().send(queue, "hello"); fail("should not be able to create producer for deleted temporary queue"); } catch (IllegalStateRuntimeException expectedException) { - assertEquals("Temporary destination has been deleted", expectedException.getMessage()); + assertThat(expectedException).hasMessage("Temporary destination has been deleted"); } } } diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/resources/logback-test.xml b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/resources/logback-test.xml index d53d9bf65754..db74e8d9c1bf 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/resources/logback-test.xml +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/resources/logback-test.xml @@ -5,7 +5,7 @@ - + From 3af376745f0a79070710972bc3198ac55e7c5adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Cogolu=C3=A8gnes?= Date: Thu, 13 Feb 2025 10:44:09 +0100 Subject: [PATCH 3/4] Control queue type with annotation in JMS tests (cherry picked from commit 7d8f83c9194407f11942b2502c934d6d287569bc) --- .../test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java | 3 ++- .../amqp/tests/jms/JmsTestInfrastructureExtension.java | 10 +++++++++- .../java/com/rabbitmq/amqp/tests/jms/TestUtils.java | 6 ++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java index e56f8edbea2b..58b1f6a8a00c 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTest.java @@ -21,6 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; +import com.rabbitmq.amqp.tests.jms.TestUtils.Classic; import com.rabbitmq.qpid.protonj2.client.Client; import com.rabbitmq.qpid.protonj2.client.Delivery; import com.rabbitmq.qpid.protonj2.client.Receiver; @@ -150,7 +151,7 @@ public void message_types_jms_to_amqp(Queue queue) throws Exception { // Test that Request/reply pattern using a TemporaryQueue works. // https://jakarta.ee/specifications/messaging/3.1/jakarta-messaging-spec-3.1#requestreply-pattern-using-a-temporaryqueue-jakarta-ee @Test - public void temporary_queue_rpc(Queue requestQueue) throws Exception { + public void temporary_queue_rpc(@Classic Queue requestQueue) throws Exception { try (JMSContext clientContext = factory.createContext()) { Destination responseQueue = clientContext.createTemporaryQueue(); JMSConsumer clientConsumer = clientContext.createConsumer(responseQueue); diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTestInfrastructureExtension.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTestInfrastructureExtension.java index dbe497a30b62..090c39322f7d 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTestInfrastructureExtension.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/JmsTestInfrastructureExtension.java @@ -20,6 +20,7 @@ import com.rabbitmq.client.amqp.Connection; import com.rabbitmq.client.amqp.Environment; +import com.rabbitmq.client.amqp.Management; import com.rabbitmq.client.amqp.impl.AmqpEnvironmentBuilder; import jakarta.jms.ConnectionFactory; import jakarta.jms.Queue; @@ -55,6 +56,12 @@ private static boolean isQueue(Parameter parameter) { return Queue.class.isAssignableFrom(parameter.getType()); } + private static Management.QueueType queueType(Parameter parameter) { + return parameter.isAnnotationPresent(TestUtils.Classic.class) + ? Management.QueueType.CLASSIC + : Management.QueueType.QUORUM; + } + @Override public void beforeEach(ExtensionContext context) throws Exception { if (context.getTestMethod().isPresent()) { @@ -66,7 +73,8 @@ public void beforeEach(ExtensionContext context) throws Exception { try (Environment environment = new AmqpEnvironmentBuilder().build(); Connection connection = environment.connectionBuilder().uri(TestUtils.brokerUri()).build()) { - connection.management().queue(queueName).declare(); + Management.QueueType type = queueType(parameter); + connection.management().queue(queueName).type(type).declare(); } store(context).put("queueName", queueName); Context jndiContext = TestUtils.context(singletonMap("queue." + queueName, queueAddress)); diff --git a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/TestUtils.java b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/TestUtils.java index 7d79e269532e..97ed41781c7a 100644 --- a/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/TestUtils.java +++ b/deps/rabbit/test/amqp_jms_SUITE_data/src/test/java/com/rabbitmq/amqp/tests/jms/TestUtils.java @@ -21,6 +21,7 @@ import com.rabbitmq.qpid.protonj2.client.Client; import com.rabbitmq.qpid.protonj2.client.ConnectionOptions; import com.rabbitmq.qpid.protonj2.client.exceptions.ClientException; +import java.lang.annotation.*; import java.lang.reflect.Method; import java.net.URI; import java.net.URISyntaxException; @@ -126,4 +127,9 @@ private static String name(Class testClass, String testMethod) { return format( "%s_%s%s", testClass.getSimpleName(), testMethod, uuid.substring(uuid.length() / 2)); } + + @Target(ElementType.PARAMETER) + @Retention(RetentionPolicy.RUNTIME) + @Documented + @interface Classic {} } From 5e4942a27fb8316e7734e948a15875b0943cab87 Mon Sep 17 00:00:00 2001 From: David Ansari Date: Thu, 13 Feb 2025 12:46:09 +0100 Subject: [PATCH 4/4] Simplify (cherry picked from commit 6366eafa3b5188fe51ac4f1075b30f304a64b5ea) --- deps/rabbit/test/amqp_jms_SUITE.erl | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/deps/rabbit/test/amqp_jms_SUITE.erl b/deps/rabbit/test/amqp_jms_SUITE.erl index d0fcfc9904c6..8a00be3d11dd 100644 --- a/deps/rabbit/test/amqp_jms_SUITE.erl +++ b/deps/rabbit/test/amqp_jms_SUITE.erl @@ -122,28 +122,24 @@ jms_temporary_queue(Config) -> %% Send different message types from JMS client to JMS client. message_types_jms_to_jms(Config) -> - TestName = atom_to_binary(?FUNCTION_NAME), - ok = run_jms_test(TestName, [], Config). + ok = run_jms_test(?FUNCTION_NAME, Config). %% Send different message types from JMS client to Erlang AMQP 1.0 client. message_types_jms_to_amqp(Config) -> - TestName = atom_to_binary(?FUNCTION_NAME), - ok = run_jms_test(TestName, [], Config). + ok = run_jms_test(?FUNCTION_NAME, Config). temporary_queue_rpc(Config) -> - TestName = atom_to_binary(?FUNCTION_NAME), - ok = run_jms_test(TestName, [], Config). + ok = run_jms_test(?FUNCTION_NAME, Config). temporary_queue_delete(Config) -> - TestName = atom_to_binary(?FUNCTION_NAME), - ok = run_jms_test(TestName, [], Config). + ok = run_jms_test(?FUNCTION_NAME, Config). %% ------------------------------------------------------------------- %% Helpers %% ------------------------------------------------------------------- -run_jms_test(TestName, JavaProps, Config) -> - run(TestName, [{"-Dtest=JmsTest#~ts", [TestName]} | JavaProps], Config). +run_jms_test(TestName, Config) -> + run(TestName, [{"-Dtest=JmsTest#~ts", [TestName]}], Config). run(TestName, JavaProps, Config) -> TestProjectDir = ?config(data_dir, Config),