Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Commit 545a6db

Browse files
committed
Proper QueueSpecification for null queue name
1 parent 1a506e0 commit 545a6db

File tree

2 files changed

+193
-6
lines changed

2 files changed

+193
-6
lines changed

src/main/java/reactor/rabbitmq/QueueSpecification.java

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,70 @@
2323
*/
2424
public class QueueSpecification {
2525

26-
private String name;
27-
private boolean durable = false;
28-
private boolean exclusive = false;
29-
private boolean autoDelete = false;
30-
private Map<String, Object> arguments;
26+
static class NullNameQueueSpecification extends QueueSpecification {
27+
28+
NullNameQueueSpecification() {
29+
this.name = null;
30+
this.durable = false;
31+
this.exclusive = true;
32+
this.autoDelete = true;
33+
}
34+
35+
@Override
36+
public QueueSpecification name(String name) {
37+
if (name == null) {
38+
return this;
39+
}
40+
return QueueSpecification.queue(name)
41+
.durable(durable)
42+
.exclusive(exclusive)
43+
.autoDelete(autoDelete);
44+
}
45+
46+
@Override
47+
public QueueSpecification durable(boolean durable) {
48+
if (this.durable != durable) {
49+
throw new IllegalArgumentException("once a queue has null name, durable is always false");
50+
}
51+
return this;
52+
}
53+
54+
@Override
55+
public QueueSpecification exclusive(boolean exclusive) {
56+
if (this.exclusive != exclusive) {
57+
throw new IllegalArgumentException("once a queue has null name, exclusive is always true");
58+
}
59+
return this;
60+
}
61+
62+
@Override
63+
public QueueSpecification autoDelete(boolean autoDelete) {
64+
if (this.autoDelete != autoDelete) {
65+
throw new IllegalArgumentException("once a queue has null name, autoDelete is always true");
66+
}
67+
return this;
68+
}
69+
}
70+
71+
String name;
72+
boolean durable = false;
73+
boolean exclusive = false;
74+
boolean autoDelete = false;
75+
Map<String, Object> arguments;
3176

3277
public static QueueSpecification queue() {
33-
return new QueueSpecification();
78+
return new NullNameQueueSpecification();
3479
}
3580

3681
public static QueueSpecification queue(String name) {
3782
return new QueueSpecification().name(name);
3883
}
3984

4085
public QueueSpecification name(String queue) {
86+
if (queue == null) {
87+
return new NullNameQueueSpecification();
88+
}
89+
4190
this.name = queue;
4291
return this;
4392
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright (c) 2018-2019 Pivotal Software Inc, All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package reactor.rabbitmq;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Nested;
21+
import org.junit.jupiter.api.Test;
22+
23+
class QueueSpecificationTest {
24+
25+
@Nested
26+
class NullName {
27+
@Test
28+
void nullNameShouldReturnANonDurableQueue() {
29+
Assertions.assertFalse(
30+
QueueSpecification.queue().isDurable());
31+
}
32+
33+
@Test
34+
void passingNullNameShouldReturnANonDurableQueue() {
35+
Assertions.assertFalse(
36+
QueueSpecification.queue(null).isDurable());
37+
}
38+
39+
@Test
40+
void nullNameShouldNotAbleToConfigureDurableToTrue() {
41+
Assertions.assertThrows(
42+
IllegalArgumentException.class,
43+
() -> QueueSpecification.queue().durable(true),
44+
"once a queue has null name, durable is always false");
45+
}
46+
47+
@Test
48+
void nullNameShouldKeepDurableWhenConfigureDurableToFalse() {
49+
Assertions.assertFalse(
50+
QueueSpecification.queue().durable(false).isDurable());
51+
}
52+
53+
@Test
54+
void passingNullNameShouldReturnAExclusiveQueue() {
55+
Assertions.assertTrue(
56+
QueueSpecification.queue().isExclusive());
57+
}
58+
59+
@Test
60+
void nullNameShouldNotAbleToConfigureExclusiveToFalse() {
61+
Assertions.assertThrows(
62+
IllegalArgumentException.class,
63+
() -> QueueSpecification.queue().exclusive(false),
64+
"once a queue has null name, exclusive is always true");
65+
}
66+
67+
@Test
68+
void nullNameShouldKeepDurableWhenConfigureExclusiveToTrue() {
69+
Assertions.assertTrue(
70+
QueueSpecification.queue().exclusive(true).isExclusive());
71+
}
72+
73+
@Test
74+
void passingNullNameShouldReturnAnAutoDeleteQueue() {
75+
Assertions.assertTrue(
76+
QueueSpecification.queue().isAutoDelete());
77+
}
78+
79+
@Test
80+
void nullNameShouldNotAbleToConfigureAutoDeleteToFalse() {
81+
Assertions.assertThrows(
82+
IllegalArgumentException.class,
83+
() -> QueueSpecification.queue().autoDelete(false),
84+
"once a queue has null name, autoDelete is always true");
85+
}
86+
87+
@Test
88+
void nullNameShouldKeepAutoDeleteWhenConfigureAutoDeleteToTrue() {
89+
Assertions.assertTrue(
90+
QueueSpecification.queue().autoDelete(true).isAutoDelete());
91+
}
92+
93+
@Test
94+
void passingANonNullNameAfterShouldReturnAConfigurableQueueSpecification() {
95+
QueueSpecification queueSpecification = QueueSpecification.queue()
96+
.name("not-null-anymore")
97+
.durable(true)
98+
.exclusive(false)
99+
.autoDelete(false);
100+
101+
Assertions.assertEquals(queueSpecification.getName(), "not-null-anymore");
102+
Assertions.assertTrue(queueSpecification.isDurable());
103+
Assertions.assertFalse(queueSpecification.isAutoDelete());
104+
Assertions.assertFalse(queueSpecification.isExclusive());
105+
}
106+
}
107+
108+
@Nested
109+
class NotNullName {
110+
@Test
111+
void queueSpecificationShouldReturnCorrespondingProperties() {
112+
QueueSpecification queueSpecification = QueueSpecification.queue("my-queue")
113+
.durable(false)
114+
.autoDelete(false)
115+
.exclusive(false);
116+
117+
Assertions.assertEquals(queueSpecification.getName(), "my-queue");
118+
Assertions.assertFalse(queueSpecification.isDurable());
119+
Assertions.assertFalse(queueSpecification.isAutoDelete());
120+
Assertions.assertFalse(queueSpecification.isExclusive());
121+
Assertions.assertNull(queueSpecification.getArguments());
122+
}
123+
124+
@Test
125+
void queueSpecificationShouldReturnCorrespondingPropertiesWhenEmptyName() {
126+
QueueSpecification queueSpecification = QueueSpecification.queue("")
127+
.durable(false)
128+
.autoDelete(false)
129+
.exclusive(false);
130+
131+
Assertions.assertEquals(queueSpecification.getName(), "");
132+
Assertions.assertFalse(queueSpecification.isDurable());
133+
Assertions.assertFalse(queueSpecification.isAutoDelete());
134+
Assertions.assertFalse(queueSpecification.isExclusive());
135+
Assertions.assertNull(queueSpecification.getArguments());
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)