-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
RabbitPropertiesTests.java
295 lines (247 loc) · 10.3 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
/*
* Copyright 2012-2019 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.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
*/
public class RabbitPropertiesTests {
private final RabbitProperties properties = new RabbitProperties();
@Test
public void hostDefaultsToLocalhost() {
assertThat(this.properties.getHost()).isEqualTo("localhost");
}
@Test
public void customHost() {
this.properties.setHost("rabbit.example.com");
assertThat(this.properties.getHost()).isEqualTo("rabbit.example.com");
}
@Test
public void hostIsDeterminedFromFirstAddress() {
this.properties.setAddresses("rabbit1.example.com:1234,rabbit2.example.com:2345");
assertThat(this.properties.determineHost()).isEqualTo("rabbit1.example.com");
}
@Test
public void determineHostReturnsHostPropertyWhenNoAddresses() {
this.properties.setHost("rabbit.example.com");
assertThat(this.properties.determineHost()).isEqualTo("rabbit.example.com");
}
@Test
public void portDefaultsTo5672() {
assertThat(this.properties.getPort()).isEqualTo(5672);
}
@Test
public void customPort() {
this.properties.setPort(1234);
assertThat(this.properties.getPort()).isEqualTo(1234);
}
@Test
public void determinePortReturnsPortOfFirstAddress() {
this.properties.setAddresses("rabbit1.example.com:1234,rabbit2.example.com:2345");
assertThat(this.properties.determinePort()).isEqualTo(1234);
}
@Test
public void determinePortReturnsPortPropertyWhenNoAddresses() {
this.properties.setPort(1234);
assertThat(this.properties.determinePort()).isEqualTo(1234);
}
@Test
public void determinePortReturnsDefaultAmqpPortWhenFirstAddressHasNoExplicitPort() {
this.properties.setPort(1234);
this.properties.setAddresses("rabbit1.example.com,rabbit2.example.com:2345");
assertThat(this.properties.determinePort()).isEqualTo(5672);
}
@Test
public void determinePortUsingAmqpReturnsPortOfFirstAddress() {
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
assertThat(this.properties.determinePort()).isEqualTo(5672);
}
@Test
public void determinePortUsingAmqpsReturnsPortOfFirstAddress() {
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
assertThat(this.properties.determinePort()).isEqualTo(5671);
}
@Test
public 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
public void virtualHostDefaultsToNull() {
assertThat(this.properties.getVirtualHost()).isNull();
}
@Test
public void customVirtualHost() {
this.properties.setVirtualHost("alpha");
assertThat(this.properties.getVirtualHost()).isEqualTo("alpha");
}
@Test
public void virtualHostRetainsALeadingSlash() {
this.properties.setVirtualHost("/alpha");
assertThat(this.properties.getVirtualHost()).isEqualTo("/alpha");
}
@Test
public void determineVirtualHostReturnsVirtualHostOfFirstAddress() {
this.properties.setAddresses("rabbit1.example.com:1234/alpha,rabbit2.example.com:2345/bravo");
assertThat(this.properties.determineVirtualHost()).isEqualTo("alpha");
}
@Test
public void determineVirtualHostReturnsPropertyWhenNoAddresses() {
this.properties.setVirtualHost("alpha");
assertThat(this.properties.determineVirtualHost()).isEqualTo("alpha");
}
@Test
public 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
public void determineVirtualHostIsSlashWhenAddressHasTrailingSlash() {
this.properties.setAddresses("amqp://root:password@otherhost:1111/");
assertThat(this.properties.determineVirtualHost()).isEqualTo("/");
}
@Test
public void emptyVirtualHostIsCoercedToASlash() {
this.properties.setVirtualHost("");
assertThat(this.properties.getVirtualHost()).isEqualTo("/");
}
@Test
public void usernameDefaultsToGuest() {
assertThat(this.properties.getUsername()).isEqualTo("guest");
}
@Test
public void customUsername() {
this.properties.setUsername("user");
assertThat(this.properties.getUsername()).isEqualTo("user");
}
@Test
public void determineUsernameReturnsUsernameOfFirstAddress() {
this.properties.setAddresses("user:secret@rabbit1.example.com:1234/alpha," + "rabbit2.example.com:2345/bravo");
assertThat(this.properties.determineUsername()).isEqualTo("user");
}
@Test
public void determineUsernameReturnsPropertyWhenNoAddresses() {
this.properties.setUsername("alice");
assertThat(this.properties.determineUsername()).isEqualTo("alice");
}
@Test
public 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
public void passwordDefaultsToGuest() {
assertThat(this.properties.getPassword()).isEqualTo("guest");
}
@Test
public void customPassword() {
this.properties.setPassword("secret");
assertThat(this.properties.getPassword()).isEqualTo("secret");
}
@Test
public void determinePasswordReturnsPasswordOfFirstAddress() {
this.properties.setAddresses("user:secret@rabbit1.example.com:1234/alpha," + "rabbit2.example.com:2345/bravo");
assertThat(this.properties.determinePassword()).isEqualTo("secret");
}
@Test
public void determinePasswordReturnsPropertyWhenNoAddresses() {
this.properties.setPassword("secret");
assertThat(this.properties.determinePassword()).isEqualTo("secret");
}
@Test
public 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
public void addressesDefaultsToNull() {
assertThat(this.properties.getAddresses()).isNull();
}
@Test
public 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
public 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
public void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSet() {
this.properties.setHost("rabbit.example.com");
this.properties.setPort(1234);
assertThat(this.properties.determineAddresses()).isEqualTo("rabbit.example.com:1234");
}
@Test
public void determineSslUsingAmqpsReturnsStateOfFirstAddress() {
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
}
@Test
public void sslDetermineEnabledIsTrueWhenAddressHasNoProtocolAndSslIsEnabled() {
this.properties.getSsl().setEnabled(true);
this.properties.setAddresses("root:password@otherhost");
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
}
@Test
public void sslDetermineEnabledIsFalseWhenAddressHasNoProtocolAndSslIsDisabled() {
this.properties.getSsl().setEnabled(false);
this.properties.setAddresses("root:password@otherhost");
assertThat(this.properties.getSsl().determineEnabled()).isFalse();
}
@Test
public void determineSslUsingAmqpReturnsStateOfFirstAddress() {
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
assertThat(this.properties.getSsl().determineEnabled()).isFalse();
}
@Test
public void determineSslReturnFlagPropertyWhenNoAddresses() {
this.properties.getSsl().setEnabled(true);
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
}
@Test
public 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());
}
@Test
public 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());
}
}