Skip to content

Commit 2280416

Browse files
Merge branch '3.1.x'
Closes gh-37622
2 parents bebca55 + 667797f commit 2280416

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/PropertiesRabbitConnectionDetails.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ public String getVirtualHost() {
5353
public List<Address> getAddresses() {
5454
List<Address> addresses = new ArrayList<>();
5555
for (String address : this.properties.determineAddresses().split(",")) {
56-
String[] components = address.split(":");
57-
addresses.add(new Address(components[0], Integer.parseInt(components[1])));
56+
int portSeparatorIndex = address.lastIndexOf(':');
57+
String host = address.substring(0, portSeparatorIndex);
58+
String port = address.substring(portSeparatorIndex + 1);
59+
addresses.add(new Address(host, Integer.parseInt(port)));
5860
}
5961
return addresses;
6062
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
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+
* https://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 org.springframework.boot.autoconfigure.amqp;
18+
19+
import java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.boot.autoconfigure.amqp.RabbitConnectionDetails.Address;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Tests for {@link PropertiesRabbitConnectionDetails}.
29+
*
30+
* @author Jonas Fügedi
31+
*/
32+
class PropertiesRabbitConnectionDetailsTests {
33+
34+
private static final int DEFAULT_PORT = 5672;
35+
36+
@Test
37+
void getAddresses() {
38+
RabbitProperties properties = new RabbitProperties();
39+
properties.setAddresses("localhost,localhost:1234,[::1],[::1]:32863");
40+
PropertiesRabbitConnectionDetails propertiesRabbitConnectionDetails = new PropertiesRabbitConnectionDetails(
41+
properties);
42+
List<Address> addresses = propertiesRabbitConnectionDetails.getAddresses();
43+
assertThat(addresses.size()).isEqualTo(4);
44+
assertThat(addresses.get(0).host()).isEqualTo("localhost");
45+
assertThat(addresses.get(0).port()).isEqualTo(DEFAULT_PORT);
46+
assertThat(addresses.get(1).host()).isEqualTo("localhost");
47+
assertThat(addresses.get(1).port()).isEqualTo(1234);
48+
assertThat(addresses.get(2).host()).isEqualTo("[::1]");
49+
assertThat(addresses.get(2).port()).isEqualTo(DEFAULT_PORT);
50+
assertThat(addresses.get(3).host()).isEqualTo("[::1]");
51+
assertThat(addresses.get(3).port()).isEqualTo(32863);
52+
}
53+
54+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSet() {
282282
assertThat(this.properties.determineAddresses()).isEqualTo("rabbit.example.com:1234");
283283
}
284284

285+
@Test
286+
void determineAddressesUsesIpv6HostAndPortPropertiesWhenNoAddressesSet() {
287+
this.properties.setHost("[::1]");
288+
this.properties.setPort(32863);
289+
assertThat(this.properties.determineAddresses()).isEqualTo("[::1]:32863");
290+
}
291+
285292
@Test
286293
void determineSslUsingAmqpsReturnsStateOfFirstAddress() {
287294
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");

0 commit comments

Comments
 (0)