Skip to content

Commit 8d0d103

Browse files
acogoluegnesansd
authored andcommitted
Use custom assertions in JMS tests
It makes assertions shorter and easier to read.
1 parent 26eb53a commit 8d0d103

File tree

3 files changed

+247
-132
lines changed

3 files changed

+247
-132
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 jakarta.jms.JMSException;
20+
import jakarta.jms.Message;
21+
import jakarta.jms.TextMessage;
22+
import org.assertj.core.api.AbstractObjectAssert;
23+
24+
import java.util.Arrays;
25+
26+
abstract class Assertions {
27+
28+
private Assertions() { }
29+
30+
static JmsMessageAssert assertThat(Message message) {
31+
return new JmsMessageAssert(message);
32+
}
33+
34+
static class JmsMessageAssert extends AbstractObjectAssert<JmsMessageAssert, Message> {
35+
36+
private static final String JMS_X_DELIVERY_COUNT = "JMSXDeliveryCount";
37+
38+
public JmsMessageAssert(Message message) {
39+
super(message, JmsMessageAssert.class);
40+
}
41+
42+
public static JmsMessageAssert assertThat(Message message) {
43+
return new JmsMessageAssert(message);
44+
}
45+
46+
public JmsMessageAssert isRedelivered() throws JMSException {
47+
isNotNull();
48+
if (!actual.getJMSRedelivered()) {
49+
failWithMessage("Message is expected to be redelivered");
50+
}
51+
return this;
52+
}
53+
54+
public JmsMessageAssert isNotRedelivered() throws JMSException {
55+
isNotNull();
56+
if (actual.getJMSRedelivered()) {
57+
failWithMessage("Message is not expected to be redelivered");
58+
}
59+
return this;
60+
}
61+
62+
public JmsMessageAssert hasDeliveryCount(int expectedDeliveryCount) throws JMSException {
63+
isNotNull();
64+
int actualDeliveryCount = this.actual.getIntProperty(JMS_X_DELIVERY_COUNT);
65+
if (actualDeliveryCount != expectedDeliveryCount) {
66+
failWithMessage("Delivery count is expected to be %d but is %d", expectedDeliveryCount,
67+
actualDeliveryCount);
68+
}
69+
return this;
70+
}
71+
72+
public JmsMessageAssert isTextMessage() {
73+
isNotNull();
74+
if (!(this.actual instanceof TextMessage)) {
75+
failWithMessage("Expected a text message, but is %s", this.actual.getClass().getName());
76+
}
77+
return this;
78+
}
79+
80+
public JmsMessageAssert hasText(String expected) throws JMSException {
81+
isNotNull();
82+
isTextMessage();
83+
if (!expected.equals(this.actual.getBody(String.class))) {
84+
failWithMessage("Expected %s but got %s", expected, this.actual.getBody(String.class));
85+
}
86+
return this;
87+
}
88+
89+
public JmsMessageAssert hasType(String expected) throws JMSException {
90+
isNotNull();
91+
if (!expected.equals(this.actual.getJMSType())) {
92+
failWithMessage("Expected %s JMSType but got %s", expected, this.actual.getJMSType());
93+
}
94+
return this;
95+
}
96+
97+
public JmsMessageAssert hasId(String expected) throws JMSException {
98+
isNotNull();
99+
if (!expected.equals(this.actual.getJMSMessageID())) {
100+
failWithMessage("Expected %s JMSMessageID but got %s", expected, this.actual.getJMSMessageID());
101+
}
102+
return this;
103+
}
104+
105+
public JmsMessageAssert hasCorrelationId(String expected) throws JMSException {
106+
isNotNull();
107+
if (!expected.equals(this.actual.getJMSCorrelationID())) {
108+
failWithMessage("Expected %s JMSCorrelationID but got %s", expected, this.actual.getJMSCorrelationID());
109+
}
110+
return this;
111+
}
112+
113+
public JmsMessageAssert hasCorrelationId(byte[] expected) throws JMSException {
114+
isNotNull();
115+
if (!Arrays.equals(expected, this.actual.getJMSCorrelationIDAsBytes())) {
116+
failWithMessage("Expected %s JMSCorrelationID but got %s", expected, this.actual.getJMSCorrelationIDAsBytes());
117+
}
118+
return this;
119+
}
120+
121+
public JmsMessageAssert hasPriority(int expected) throws JMSException {
122+
isNotNull();
123+
if (expected != this.actual.getJMSPriority()) {
124+
failWithMessage("Expected %d JMSPriority but got %d", expected, this.actual.getJMSPriority());
125+
}
126+
return this;
127+
}
128+
129+
public JmsMessageAssert hasTimestamp(long expected) throws JMSException {
130+
isNotNull();
131+
if (expected != this.actual.getJMSTimestamp()) {
132+
failWithMessage("Expected %d JMSTimestamp but got %d", expected, this.actual.getJMSTimestamp());
133+
}
134+
return this;
135+
}
136+
137+
public JmsMessageAssert hasProperty(String key, Object expectedValue) throws JMSException {
138+
isNotNull();
139+
Object value = actual.getObjectProperty(key);
140+
if (value == null) {
141+
failWithMessage("Expected %s property but is not present", key);
142+
}
143+
if (!expectedValue.equals(value)) {
144+
failWithMessage("Expected %s for property %s value but got %s", expectedValue, key, value);
145+
}
146+
return this;
147+
}
148+
149+
150+
}
151+
152+
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//
1717
package com.rabbitmq.amqp.tests.jms;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
19+
import static com.rabbitmq.amqp.tests.jms.Assertions.assertThat;
2020

2121
import jakarta.jms.Connection;
2222
import jakarta.jms.ConnectionFactory;
@@ -68,9 +68,8 @@ void testSelectors(
6868

6969
MessageConsumer consumer = session.createConsumer(queue, "JMSPriority > 8");
7070
Message msg = consumer.receive(5000);
71-
assertThat(msg).isNotNull().isInstanceOf(TextMessage.class);
72-
assertThat(((TextMessage) msg).getText()).isEqualTo("hello + 9");
73-
assertThat(consumer.receive(1000)).isNull();
71+
assertThat(msg).isNotNull().hasText("hello + 9");
72+
org.assertj.core.api.Assertions.assertThat(consumer.receive(1000)).isNull();
7473
}
7574
}
7675

@@ -110,9 +109,7 @@ void testSelectorsWithJMSType(@TestUtils.QueueArgs(
110109

111110
MessageConsumer consumer = session.createConsumer(queue, "JMSType = '" + type + "'");
112111
Message msg = consumer.receive(5000);
113-
assertThat(msg).isNotNull().isInstanceOf(TextMessage.class);
114-
assertThat(msg.getJMSType()).isEqualTo(type);
115-
assertThat(((TextMessage) msg).getText()).isEqualTo("text + type");
112+
assertThat(msg).isNotNull().hasType(type).hasText("text + type");
116113
}
117114
}
118115
}

0 commit comments

Comments
 (0)