Skip to content

Commit 347c96a

Browse files
committed
Improve some code in the AbstractDeclarable
* Add `Declarable.setAdminsThatShouldDeclare(@nullable)` * Improve `RabbitAdminDeclarationTests` * Fix typo in the `AbstractMessageListenerContainer` JavaDoc
1 parent ef23aa9 commit 347c96a

File tree

4 files changed

+51
-58
lines changed

4 files changed

+51
-58
lines changed

spring-amqp/src/main/java/org/springframework/amqp/core/AbstractDeclarable.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.concurrent.locks.Lock;
2626
import java.util.concurrent.locks.ReentrantLock;
2727

28-
2928
import org.springframework.lang.Nullable;
3029
import org.springframework.util.Assert;
3130

@@ -40,15 +39,15 @@
4039
*/
4140
public abstract class AbstractDeclarable implements Declarable {
4241

43-
private final Lock lock = new ReentrantLock();
42+
private final Lock lock = new ReentrantLock();
4443

45-
private boolean shouldDeclare = true;
44+
private final Map<String, Object> arguments;
4645

47-
private Collection<Object> declaringAdmins = new ArrayList<>();
46+
private boolean shouldDeclare = true;
4847

4948
private boolean ignoreDeclarationExceptions;
5049

51-
private final Map<String, Object> arguments;
50+
private Collection<Object> declaringAdmins = new ArrayList<>();
5251

5352
public AbstractDeclarable() {
5453
this(null);
@@ -74,7 +73,7 @@ public boolean shouldDeclare() {
7473
}
7574

7675
/**
77-
* Whether or not this object should be automatically declared
76+
* Whether this object should be automatically declared
7877
* by any {@code AmqpAdmin}. Default is {@code true}.
7978
* @param shouldDeclare true or false.
8079
*/
@@ -102,14 +101,14 @@ public void setIgnoreDeclarationExceptions(boolean ignoreDeclarationExceptions)
102101
}
103102

104103
@Override
105-
public void setAdminsThatShouldDeclare(Object... adminArgs) {
104+
public void setAdminsThatShouldDeclare(@Nullable Object... adminArgs) {
106105
Collection<Object> admins = new ArrayList<>();
107106
if (adminArgs != null) {
108107
if (adminArgs.length > 1) {
109108
Assert.noNullElements(adminArgs, "'admins' cannot contain null elements");
110109
}
111110
if (adminArgs.length > 0 && !(adminArgs.length == 1 && adminArgs[0] == null)) {
112-
admins.addAll(Arrays.asList(adminArgs));
111+
admins = Arrays.asList(adminArgs);
113112
}
114113
}
115114
this.declaringAdmins = admins;

spring-amqp/src/main/java/org/springframework/amqp/core/Declarable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,7 +61,7 @@ public interface Declarable {
6161
* the behavior such that all admins will declare the object.
6262
* @param adminArgs The admins.
6363
*/
64-
void setAdminsThatShouldDeclare(Object... adminArgs);
64+
void setAdminsThatShouldDeclare(@Nullable Object... adminArgs);
6565

6666
/**
6767
* Add an argument to the declarable.

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/AbstractMessageListenerContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ protected void invokeErrorHandler(Throwable ex) {
14901490
// -------------------------------------------------------------------------
14911491

14921492
/**
1493-
* Execute the specified listener, committing or rolling back the transaction afterwards (if necessary).
1493+
* Execute the specified listener, committing or rolling back the transaction afterward (if necessary).
14941494
* @param channel the Rabbit Channel to operate on
14951495
* @param data the received Rabbit Message
14961496
* @see #invokeListener

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminDeclarationTests.java

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
1717
package org.springframework.amqp.rabbit.core;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.fail;
20+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2121
import static org.mockito.ArgumentMatchers.any;
2222
import static org.mockito.ArgumentMatchers.anyBoolean;
2323
import static org.mockito.ArgumentMatchers.anyMap;
@@ -79,8 +79,8 @@ public void testUnconditional() throws Exception {
7979
given(cf.createConnection()).willReturn(conn);
8080
given(conn.createChannel(false)).willReturn(channel);
8181
given(channel.queueDeclare("foo", true, false, false, new HashMap<>()))
82-
.willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0));
83-
final AtomicReference<ConnectionListener> listener = new AtomicReference<ConnectionListener>();
82+
.willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0));
83+
AtomicReference<ConnectionListener> listener = new AtomicReference<>();
8484
willAnswer(invocation -> {
8585
listener.set((ConnectionListener) invocation.getArguments()[0]);
8686
return null;
@@ -100,15 +100,15 @@ public void testUnconditional() throws Exception {
100100
listener.get().onCreate(conn);
101101

102102
verify(channel).queueDeclare("foo", true, false, false, new HashMap<>());
103-
verify(channel).exchangeDeclare("bar", "direct", true, false, false, new HashMap<String, Object>());
103+
verify(channel).exchangeDeclare("bar", "direct", true, false, false, new HashMap<>());
104104
verify(channel).queueBind("foo", "bar", "foo", new HashMap<>());
105105
}
106106

107107
@Test
108108
public void testNoDeclareWithCachedConnections() throws Exception {
109109
com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class);
110110

111-
final List<Channel> mockChannels = new ArrayList<Channel>();
111+
List<Channel> mockChannels = new ArrayList<>();
112112

113113
AtomicInteger connectionNumber = new AtomicInteger();
114114
willAnswer(invocation -> {
@@ -153,8 +153,8 @@ public void testUnconditionalWithExplicitFactory() throws Exception {
153153
given(cf.createConnection()).willReturn(conn);
154154
given(conn.createChannel(false)).willReturn(channel);
155155
given(channel.queueDeclare("foo", true, false, false, new HashMap<>()))
156-
.willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0));
157-
final AtomicReference<ConnectionListener> listener = new AtomicReference<ConnectionListener>();
156+
.willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0));
157+
AtomicReference<ConnectionListener> listener = new AtomicReference<>();
158158
willAnswer(invocation -> {
159159
listener.set(invocation.getArgument(0));
160160
return null;
@@ -177,7 +177,7 @@ public void testUnconditionalWithExplicitFactory() throws Exception {
177177
listener.get().onCreate(conn);
178178

179179
verify(channel).queueDeclare("foo", true, false, false, new HashMap<>());
180-
verify(channel).exchangeDeclare("bar", "direct", true, false, false, new HashMap<String, Object>());
180+
verify(channel).exchangeDeclare("bar", "direct", true, false, false, new HashMap<>());
181181
verify(channel).queueBind("foo", "bar", "foo", new HashMap<>());
182182
}
183183

@@ -189,8 +189,9 @@ public void testSkipBecauseDifferentFactory() throws Exception {
189189
Channel channel = mock(Channel.class);
190190
given(cf.createConnection()).willReturn(conn);
191191
given(conn.createChannel(false)).willReturn(channel);
192-
given(channel.queueDeclare("foo", true, false, false, null)).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0));
193-
final AtomicReference<ConnectionListener> listener = new AtomicReference<ConnectionListener>();
192+
given(channel.queueDeclare("foo", true, false, false, null))
193+
.willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0));
194+
AtomicReference<ConnectionListener> listener = new AtomicReference<>();
194195
willAnswer(invocation -> {
195196
listener.set(invocation.getArgument(0));
196197
return null;
@@ -215,20 +216,21 @@ public void testSkipBecauseDifferentFactory() throws Exception {
215216

216217
verify(channel, never()).queueDeclare(eq("foo"), anyBoolean(), anyBoolean(), anyBoolean(), any(Map.class));
217218
verify(channel, never())
218-
.exchangeDeclare(eq("bar"), eq("direct"), anyBoolean(), anyBoolean(), anyBoolean(), any(Map.class));
219+
.exchangeDeclare(eq("bar"), eq("direct"), anyBoolean(), anyBoolean(), anyBoolean(), any(Map.class));
219220
verify(channel, never()).queueBind(eq("foo"), eq("bar"), eq("foo"), any(Map.class));
220221
}
221222

222223
@SuppressWarnings("unchecked")
223224
@Test
224-
public void testSkipBecauseShouldntDeclare() throws Exception {
225+
public void testSkipBecauseShouldNotDeclare() throws Exception {
225226
ConnectionFactory cf = mock(ConnectionFactory.class);
226227
Connection conn = mock(Connection.class);
227228
Channel channel = mock(Channel.class);
228229
given(cf.createConnection()).willReturn(conn);
229230
given(conn.createChannel(false)).willReturn(channel);
230-
given(channel.queueDeclare("foo", true, false, false, null)).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0));
231-
final AtomicReference<ConnectionListener> listener = new AtomicReference<ConnectionListener>();
231+
given(channel.queueDeclare("foo", true, false, false, null))
232+
.willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0));
233+
AtomicReference<ConnectionListener> listener = new AtomicReference<>();
232234
willAnswer(invocation -> {
233235
listener.set(invocation.getArgument(0));
234236
return null;
@@ -252,7 +254,7 @@ public void testSkipBecauseShouldntDeclare() throws Exception {
252254

253255
verify(channel, never()).queueDeclare(eq("foo"), anyBoolean(), anyBoolean(), anyBoolean(), any(Map.class));
254256
verify(channel, never())
255-
.exchangeDeclare(eq("bar"), eq("direct"), anyBoolean(), anyBoolean(), anyBoolean(), any(Map.class));
257+
.exchangeDeclare(eq("bar"), eq("direct"), anyBoolean(), anyBoolean(), anyBoolean(), any(Map.class));
256258
verify(channel, never()).queueBind(eq("foo"), eq("bar"), eq("foo"), any(Map.class));
257259
}
258260

@@ -263,19 +265,17 @@ public void testJavaConfig() throws Exception {
263265
verify(Config.channel1).queueDeclare("foo", true, false, false, new HashMap<>());
264266
verify(Config.channel1, never()).queueDeclare("baz", true, false, false, new HashMap<>());
265267
verify(Config.channel1).queueDeclare("qux", true, false, false, new HashMap<>());
266-
verify(Config.channel1).exchangeDeclare("bar", "direct", true, false, true, new HashMap<String, Object>());
268+
verify(Config.channel1).exchangeDeclare("bar", "direct", true, false, true, new HashMap<>());
267269
verify(Config.channel1).queueBind("foo", "bar", "foo", new HashMap<>());
268-
269270
Config.listener2.onCreate(Config.conn2);
270271
verify(Config.channel2, never())
271272
.queueDeclare(eq("foo"), anyBoolean(), anyBoolean(), anyBoolean(), isNull());
272273
verify(Config.channel1, never()).queueDeclare("baz", true, false, false, new HashMap<>());
273274
verify(Config.channel2).queueDeclare("qux", true, false, false, new HashMap<>());
274275
verify(Config.channel2, never())
275276
.exchangeDeclare(eq("bar"), eq("direct"), anyBoolean(), anyBoolean(),
276-
anyBoolean(), anyMap());
277+
anyBoolean(), anyMap());
277278
verify(Config.channel2, never()).queueBind(eq("foo"), eq("bar"), eq("foo"), anyMap());
278-
279279
Config.listener3.onCreate(Config.conn3);
280280
verify(Config.channel3, never())
281281
.queueDeclare(eq("foo"), anyBoolean(), anyBoolean(), anyBoolean(), isNull());
@@ -286,7 +286,7 @@ public void testJavaConfig() throws Exception {
286286
verify(Config.channel3, never()).queueDeclare("qux", true, false, false, new HashMap<>());
287287
verify(Config.channel3, never())
288288
.exchangeDeclare(eq("bar"), eq("direct"), anyBoolean(), anyBoolean(),
289-
anyBoolean(), anyMap());
289+
anyBoolean(), anyMap());
290290
verify(Config.channel3, never()).queueBind(eq("foo"), eq("bar"), eq("foo"), anyMap());
291291

292292
context.close();
@@ -316,13 +316,9 @@ public void testAddRemove() {
316316
assertThat(queue.getDeclaringAdmins()).hasSize(2);
317317
queue.setAdminsThatShouldDeclare((Object[]) null);
318318
assertThat(queue.getDeclaringAdmins()).hasSize(0);
319-
try {
320-
queue.setAdminsThatShouldDeclare(null, admin1);
321-
fail("Expected Exception");
322-
}
323-
catch (IllegalArgumentException e) {
324-
assertThat(e.getMessage()).contains("'admins' cannot contain null elements");
325-
}
319+
assertThatIllegalArgumentException()
320+
.isThrownBy(() -> queue.setAdminsThatShouldDeclare(null, admin1))
321+
.withMessageContaining("'admins' cannot contain null elements");
326322
}
327323

328324
@Test
@@ -348,17 +344,17 @@ public void testNoOpWhenNothingToDeclare() throws Exception {
348344
@Configuration
349345
public static class Config {
350346

351-
private static Connection conn1 = mock(Connection.class);
347+
private static final Connection conn1 = mock();
352348

353-
private static Connection conn2 = mock(Connection.class);
349+
private static final Connection conn2 = mock();
354350

355-
private static Connection conn3 = mock(Connection.class);
351+
private static final Connection conn3 = mock();
356352

357-
private static Channel channel1 = mock(Channel.class);
353+
private static final Channel channel1 = mock();
358354

359-
private static Channel channel2 = mock(Channel.class);
355+
private static final Channel channel2 = mock();
360356

361-
private static Channel channel3 = mock(Channel.class);
357+
private static final Channel channel3 = mock();
362358

363359
private static ConnectionListener listener1;
364360

@@ -371,9 +367,9 @@ public ConnectionFactory cf1() throws IOException {
371367
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
372368
given(connectionFactory.createConnection()).willReturn(conn1);
373369
given(conn1.createChannel(false)).willReturn(channel1);
374-
willAnswer(inv -> {
375-
return new AMQImpl.Queue.DeclareOk(inv.getArgument(0), 0, 0);
376-
}).given(channel1).queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any());
370+
willAnswer(inv -> new AMQImpl.Queue.DeclareOk(inv.getArgument(0), 0, 0))
371+
.given(channel1)
372+
.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any());
377373
willAnswer(invocation -> {
378374
listener1 = invocation.getArgument(0);
379375
return null;
@@ -386,9 +382,9 @@ public ConnectionFactory cf2() throws IOException {
386382
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
387383
given(connectionFactory.createConnection()).willReturn(conn2);
388384
given(conn2.createChannel(false)).willReturn(channel2);
389-
willAnswer(inv -> {
390-
return new AMQImpl.Queue.DeclareOk(inv.getArgument(0), 0, 0);
391-
}).given(channel2).queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any());
385+
willAnswer(inv -> new AMQImpl.Queue.DeclareOk(inv.getArgument(0), 0, 0))
386+
.given(channel2)
387+
.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any());
392388
willAnswer(invocation -> {
393389
listener2 = invocation.getArgument(0);
394390
return null;
@@ -401,9 +397,9 @@ public ConnectionFactory cf3() throws IOException {
401397
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
402398
given(connectionFactory.createConnection()).willReturn(conn3);
403399
given(conn3.createChannel(false)).willReturn(channel3);
404-
willAnswer(inv -> {
405-
return new AMQImpl.Queue.DeclareOk(inv.getArgument(0), 0, 0);
406-
}).given(channel3).queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any());
400+
willAnswer(inv -> new AMQImpl.Queue.DeclareOk(inv.getArgument(0), 0, 0))
401+
.given(channel3)
402+
.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any());
407403
willAnswer(invocation -> {
408404
listener3 = invocation.getArgument(0);
409405
return null;
@@ -413,14 +409,12 @@ public ConnectionFactory cf3() throws IOException {
413409

414410
@Bean
415411
public RabbitAdmin admin1() throws IOException {
416-
RabbitAdmin rabbitAdmin = new RabbitAdmin(cf1());
417-
return rabbitAdmin;
412+
return new RabbitAdmin(cf1());
418413
}
419414

420415
@Bean
421416
public RabbitAdmin admin2() throws IOException {
422-
RabbitAdmin rabbitAdmin = new RabbitAdmin(cf2());
423-
return rabbitAdmin;
417+
return new RabbitAdmin(cf2());
424418
}
425419

426420
@Bean

0 commit comments

Comments
 (0)