Skip to content

Commit 047e63e

Browse files
LeeHyungGeolmp911de
authored andcommitted
RedisNode creation from bare hostname assigns default port.
Closes #2928 Original pull request: #3002
1 parent 6e85141 commit 047e63e

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/main/java/org/springframework/data/redis/connection/RedisNode.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
* @author Christoph Strobl
2525
* @author Thomas Darimont
2626
* @author Mark Paluch
27+
* @author LeeHyungGeol
2728
* @since 1.4
2829
*/
2930
public class RedisNode implements NamedNode {
3031

32+
private static final int DEFAULT_PORT = 6379;
33+
3134
@Nullable String id;
3235
@Nullable String name;
3336
@Nullable String host;
@@ -94,7 +97,17 @@ public static RedisNode fromString(String hostPortString) {
9497
portString = hostPortString.substring(colonPos + 1);
9598
} else {
9699
// 0 or 2+ colons. Bare hostname or IPv6 literal.
97-
host = hostPortString;
100+
int lastColonIndex = hostPortString.lastIndexOf(':');
101+
102+
// IPv6 literal
103+
if (lastColonIndex > hostPortString.indexOf(']')) {
104+
host = hostPortString.substring(0, lastColonIndex);
105+
portString = hostPortString.substring(lastColonIndex + 1);
106+
} else {
107+
// bare hostname
108+
host = hostPortString;
109+
portString = Integer.toString(DEFAULT_PORT);
110+
}
98111
}
99112
}
100113

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2014-2024 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+
package org.springframework.data.redis.connection;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.assertj.core.api.Assertions.*;
21+
22+
/**
23+
* Unit tests for {@link RedisNode}.
24+
* referred to the test code in ConversionUnitTests.java
25+
*
26+
* @author LeeHyungGeol
27+
*/
28+
public class RedisNodeUnitTests {
29+
private static final int DEFAULT_PORT = 6379;
30+
31+
@Test // GH-2928
32+
void shouldParseIPv4AddressWithPort() {
33+
RedisNode node = RedisNode.fromString("127.0.0.1:6379");
34+
assertThat(node.getHost()).isEqualTo("127.0.0.1");
35+
assertThat(node.getPort()).isEqualTo(6379);
36+
}
37+
38+
@Test // GH-2928
39+
void shouldParseIPv6AddressWithPort() {
40+
RedisNode node = RedisNode.fromString("[aaaa:bbbb::dddd:eeee]:6379");
41+
assertThat(node.getHost()).isEqualTo("aaaa:bbbb::dddd:eeee");
42+
assertThat(node.getPort()).isEqualTo(6379);
43+
}
44+
45+
@Test // GH-2928
46+
void shouldParseBareHostnameWithPort() {
47+
RedisNode node = RedisNode.fromString("my.redis.server:6379");
48+
assertThat(node.getHost()).isEqualTo("my.redis.server");
49+
assertThat(node.getPort()).isEqualTo(6379);
50+
}
51+
52+
@Test // GH-2928
53+
void shouldThrowExceptionForInvalidPort() {
54+
assertThatIllegalArgumentException()
55+
.isThrownBy(() -> RedisNode.fromString("127.0.0.1:invalidPort"));
56+
}
57+
58+
@Test // GH-2928
59+
void shouldParseBareIPv6WithoutPort() {
60+
RedisNode node = RedisNode.fromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
61+
assertThat(node.getHost()).isEqualTo("2001:0db8:85a3:0000:0000:8a2e:0370");
62+
assertThat(node.getPort()).isEqualTo(7334);
63+
}
64+
65+
@Test // GH-2928
66+
void shouldParseBareHostnameWithoutPort() {
67+
RedisNode node = RedisNode.fromString("my.redis.server");
68+
assertThat(node.getHost()).isEqualTo("my.redis.server");
69+
assertThat(node.getPort()).isEqualTo(DEFAULT_PORT);
70+
}
71+
}
72+

0 commit comments

Comments
 (0)