-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
RabbitPropertiesTests.java
334 lines (280 loc) · 11.4 KB
/
RabbitPropertiesTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.amqp;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link RabbitProperties}.
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
class RabbitPropertiesTests {
private final RabbitProperties properties = new RabbitProperties();
@Test
void hostDefaultsToLocalhost() {
assertThat(this.properties.getHost()).isEqualTo("localhost");
}
@Test
void customHost() {
this.properties.setHost("rabbit.example.com");
assertThat(this.properties.getHost()).isEqualTo("rabbit.example.com");
}
@Test
void hostIsDeterminedFromFirstAddress() {
this.properties.setAddresses("rabbit1.example.com:1234,rabbit2.example.com:2345");
assertThat(this.properties.determineHost()).isEqualTo("rabbit1.example.com");
}
@Test
void determineHostReturnsHostPropertyWhenNoAddresses() {
this.properties.setHost("rabbit.example.com");
assertThat(this.properties.determineHost()).isEqualTo("rabbit.example.com");
}
@Test
void portDefaultsToNull() {
assertThat(this.properties.getPort()).isNull();
}
@Test
void customPort() {
this.properties.setPort(1234);
assertThat(this.properties.getPort()).isEqualTo(1234);
}
@Test
void determinePortReturnsPortOfFirstAddress() {
this.properties.setAddresses("rabbit1.example.com:1234,rabbit2.example.com:2345");
assertThat(this.properties.determinePort()).isEqualTo(1234);
}
@Test
void determinePortReturnsDefaultPortWhenNoAddresses() {
assertThat(this.properties.determinePort()).isEqualTo(5672);
}
@Test
void determinePortWithSslReturnsDefaultSslPortWhenNoAddresses() {
this.properties.getSsl().setEnabled(true);
assertThat(this.properties.determinePort()).isEqualTo(5671);
}
@Test
void determinePortReturnsPortPropertyWhenNoAddresses() {
this.properties.setPort(1234);
assertThat(this.properties.determinePort()).isEqualTo(1234);
}
@Test
void determinePortReturnsDefaultAmqpPortWhenFirstAddressHasNoExplicitPort() {
this.properties.setPort(1234);
this.properties.setAddresses("rabbit1.example.com,rabbit2.example.com:2345");
assertThat(this.properties.determinePort()).isEqualTo(5672);
}
@Test
void determinePortUsingAmqpReturnsPortOfFirstAddress() {
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
assertThat(this.properties.determinePort()).isEqualTo(5672);
}
@Test
void determinePortUsingAmqpsReturnsPortOfFirstAddress() {
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
assertThat(this.properties.determinePort()).isEqualTo(5671);
}
@Test
void determinePortReturnsDefaultAmqpsPortWhenFirstAddressHasNoExplicitPortButSslEnabled() {
this.properties.getSsl().setEnabled(true);
this.properties.setPort(1234);
this.properties.setAddresses("rabbit1.example.com,rabbit2.example.com:2345");
assertThat(this.properties.determinePort()).isEqualTo(5671);
}
@Test
void virtualHostDefaultsToNull() {
assertThat(this.properties.getVirtualHost()).isNull();
}
@Test
void customVirtualHost() {
this.properties.setVirtualHost("alpha");
assertThat(this.properties.getVirtualHost()).isEqualTo("alpha");
}
@Test
void virtualHostRetainsALeadingSlash() {
this.properties.setVirtualHost("/alpha");
assertThat(this.properties.getVirtualHost()).isEqualTo("/alpha");
}
@Test
void determineVirtualHostReturnsVirtualHostOfFirstAddress() {
this.properties.setAddresses("rabbit1.example.com:1234/alpha,rabbit2.example.com:2345/bravo");
assertThat(this.properties.determineVirtualHost()).isEqualTo("alpha");
}
@Test
void determineVirtualHostReturnsPropertyWhenNoAddresses() {
this.properties.setVirtualHost("alpha");
assertThat(this.properties.determineVirtualHost()).isEqualTo("alpha");
}
@Test
void determineVirtualHostReturnsPropertyWhenFirstAddressHasNoVirtualHost() {
this.properties.setVirtualHost("alpha");
this.properties.setAddresses("rabbit1.example.com:1234,rabbit2.example.com:2345/bravo");
assertThat(this.properties.determineVirtualHost()).isEqualTo("alpha");
}
@Test
void determineVirtualHostIsSlashWhenAddressHasTrailingSlash() {
this.properties.setAddresses("amqp://root:password@otherhost:1111/");
assertThat(this.properties.determineVirtualHost()).isEqualTo("/");
}
@Test
void emptyVirtualHostIsCoercedToASlash() {
this.properties.setVirtualHost("");
assertThat(this.properties.getVirtualHost()).isEqualTo("/");
}
@Test
void usernameDefaultsToGuest() {
assertThat(this.properties.getUsername()).isEqualTo("guest");
}
@Test
void customUsername() {
this.properties.setUsername("user");
assertThat(this.properties.getUsername()).isEqualTo("user");
}
@Test
void determineUsernameReturnsUsernameOfFirstAddress() {
this.properties.setAddresses("user:secret@rabbit1.example.com:1234/alpha,rabbit2.example.com:2345/bravo");
assertThat(this.properties.determineUsername()).isEqualTo("user");
}
@Test
void determineUsernameReturnsPropertyWhenNoAddresses() {
this.properties.setUsername("alice");
assertThat(this.properties.determineUsername()).isEqualTo("alice");
}
@Test
void determineUsernameReturnsPropertyWhenFirstAddressHasNoUsername() {
this.properties.setUsername("alice");
this.properties.setAddresses("rabbit1.example.com:1234/alpha,user:secret@rabbit2.example.com:2345/bravo");
assertThat(this.properties.determineUsername()).isEqualTo("alice");
}
@Test
void passwordDefaultsToGuest() {
assertThat(this.properties.getPassword()).isEqualTo("guest");
}
@Test
void customPassword() {
this.properties.setPassword("secret");
assertThat(this.properties.getPassword()).isEqualTo("secret");
}
@Test
void determinePasswordReturnsPasswordOfFirstAddress() {
this.properties.setAddresses("user:secret@rabbit1.example.com:1234/alpha,rabbit2.example.com:2345/bravo");
assertThat(this.properties.determinePassword()).isEqualTo("secret");
}
@Test
void determinePasswordReturnsPropertyWhenNoAddresses() {
this.properties.setPassword("secret");
assertThat(this.properties.determinePassword()).isEqualTo("secret");
}
@Test
void determinePasswordReturnsPropertyWhenFirstAddressHasNoPassword() {
this.properties.setPassword("12345678");
this.properties.setAddresses("rabbit1.example.com:1234/alpha,user:secret@rabbit2.example.com:2345/bravo");
assertThat(this.properties.determinePassword()).isEqualTo("12345678");
}
@Test
void addressesDefaultsToNull() {
assertThat(this.properties.getAddresses()).isNull();
}
@Test
void customAddresses() {
this.properties.setAddresses("user:secret@rabbit1.example.com:1234/alpha,rabbit2.example.com");
assertThat(this.properties.getAddresses())
.isEqualTo("user:secret@rabbit1.example.com:1234/alpha,rabbit2.example.com");
}
@Test
void ipv6Address() {
this.properties.setAddresses("amqp://foo:bar@[aaaa:bbbb:cccc::d]:1234");
assertThat(this.properties.determineHost()).isEqualTo("[aaaa:bbbb:cccc::d]");
assertThat(this.properties.determinePort()).isEqualTo(1234);
}
@Test
void ipv6AddressDefaultPort() {
this.properties.setAddresses("amqp://foo:bar@[aaaa:bbbb:cccc::d]");
assertThat(this.properties.determineHost()).isEqualTo("[aaaa:bbbb:cccc::d]");
assertThat(this.properties.determinePort()).isEqualTo(5672);
}
@Test
void determineAddressesReturnsAddressesWithJustHostAndPort() {
this.properties.setAddresses("user:secret@rabbit1.example.com:1234/alpha,rabbit2.example.com");
assertThat(this.properties.determineAddresses()).isEqualTo("rabbit1.example.com:1234,rabbit2.example.com:5672");
}
@Test
void determineAddressesUsesDefaultWhenNoAddressesSet() {
assertThat(this.properties.determineAddresses()).isEqualTo("localhost:5672");
}
@Test
void determineAddressesWithSslUsesDefaultWhenNoAddressesSet() {
this.properties.getSsl().setEnabled(true);
assertThat(this.properties.determineAddresses()).isEqualTo("localhost:5671");
}
@Test
void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSet() {
this.properties.setHost("rabbit.example.com");
this.properties.setPort(1234);
assertThat(this.properties.determineAddresses()).isEqualTo("rabbit.example.com:1234");
}
@Test
void determineSslUsingAmqpsReturnsStateOfFirstAddress() {
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
}
@Test
void sslDetermineEnabledIsTrueWhenAddressHasNoProtocolAndSslIsEnabled() {
this.properties.getSsl().setEnabled(true);
this.properties.setAddresses("root:password@otherhost");
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
}
@Test
void sslDetermineEnabledIsFalseWhenAddressHasNoProtocolAndSslIsDisabled() {
this.properties.getSsl().setEnabled(false);
this.properties.setAddresses("root:password@otherhost");
assertThat(this.properties.getSsl().determineEnabled()).isFalse();
}
@Test
void determineSslUsingAmqpReturnsStateOfFirstAddress() {
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
assertThat(this.properties.getSsl().determineEnabled()).isFalse();
}
@Test
void determineSslReturnFlagPropertyWhenNoAddresses() {
this.properties.getSsl().setEnabled(true);
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
}
@Test
void simpleContainerUseConsistentDefaultValues() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
SimpleMessageListenerContainer container = factory.createListenerContainer();
RabbitProperties.SimpleContainer simple = this.properties.getListener().getSimple();
assertThat(simple.isAutoStartup()).isEqualTo(container.isAutoStartup());
assertThat(container).hasFieldOrPropertyWithValue("missingQueuesFatal", simple.isMissingQueuesFatal());
assertThat(container).hasFieldOrPropertyWithValue("deBatchingEnabled", simple.isDeBatchingEnabled());
assertThat(container).hasFieldOrPropertyWithValue("consumerBatchEnabled", simple.isConsumerBatchEnabled());
}
@Test
void directContainerUseConsistentDefaultValues() {
DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory();
DirectMessageListenerContainer container = factory.createListenerContainer();
RabbitProperties.DirectContainer direct = this.properties.getListener().getDirect();
assertThat(direct.isAutoStartup()).isEqualTo(container.isAutoStartup());
assertThat(container).hasFieldOrPropertyWithValue("missingQueuesFatal", direct.isMissingQueuesFatal());
assertThat(container).hasFieldOrPropertyWithValue("deBatchingEnabled", direct.isDeBatchingEnabled());
}
}