From 59b7535ed5dc30699a4f0da504d432adce5c0d89 Mon Sep 17 00:00:00 2001 From: LeeHyungGeol Date: Sun, 22 Sep 2024 16:50:02 +0900 Subject: [PATCH 1/5] Update RedisNode creation from bare hostname should assign default port --- .../data/redis/connection/RedisNode.java | 14 +++++++++++++- .../data/redis/connection/RedisNodeUnitTests.java | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java diff --git a/src/main/java/org/springframework/data/redis/connection/RedisNode.java b/src/main/java/org/springframework/data/redis/connection/RedisNode.java index 2ea0e6dd18..ec63cfb0c6 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisNode.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisNode.java @@ -28,6 +28,8 @@ */ public class RedisNode implements NamedNode { + private static final int DEFAULT_PORT = 6379; + @Nullable String id; @Nullable String name; @Nullable String host; @@ -94,7 +96,17 @@ public static RedisNode fromString(String hostPortString) { portString = hostPortString.substring(colonPos + 1); } else { // 0 or 2+ colons. Bare hostname or IPv6 literal. - host = hostPortString; + int lastColonIndex = hostPortString.lastIndexOf(':'); + + // IPv6 literal + if (lastColonIndex > hostPortString.indexOf(']')) { + host = hostPortString.substring(0, lastColonIndex); + portString = hostPortString.substring(lastColonIndex + 1); + } else { + // bare hostname + host = hostPortString; + portString = Integer.toString(DEFAULT_PORT); + } } } diff --git a/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java b/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java new file mode 100644 index 0000000000..46bc7bf7ec --- /dev/null +++ b/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java @@ -0,0 +1 @@ +package org.springframework.data.redis.connection; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; /** * Unit tests for {@link RedisNode}. * I referenced the test code from ConversionUnitTests.java * * @author LeeHyungGeol */ public class RedisNodeUnitTests { private static final int DEFAULT_PORT = 6379; @Test // IPv4 Address with port void shouldParseIPv4AddressWithPort() { RedisNode node = RedisNode.fromString("127.0.0.1:6379"); assertThat(node.getHost()).isEqualTo("127.0.0.1"); assertThat(node.getPort()).isEqualTo(6379); } @Test // IPv6 Address with port void shouldParseIPv6AddressWithPort() { RedisNode node = RedisNode.fromString("[aaaa:bbbb::dddd:eeee]:6379"); assertThat(node.getHost()).isEqualTo("aaaa:bbbb::dddd:eeee"); assertThat(node.getPort()).isEqualTo(6379); } @Test // Bare hostname with port void shouldParseBareHostnameWithPort() { RedisNode node = RedisNode.fromString("my.redis.server:6379"); assertThat(node.getHost()).isEqualTo("my.redis.server"); assertThat(node.getPort()).isEqualTo(6379); } @Test // Invalid port parsing void shouldThrowExceptionForInvalidPort() { assertThrows(IllegalArgumentException.class, () -> { RedisNode node = RedisNode.fromString("127.0.0.1:invalidPort"); }); } @Test // Bare IPv6 address (no square brackets) void shouldParseBareIPv6WithoutPort() { RedisNode node = RedisNode.fromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); assertThat(node.getHost()).isEqualTo("2001:0db8:85a3:0000:0000:8a2e:0370"); assertThat(node.getPort()).isEqualTo(7334); } @Test // Bare hostname without port void shouldParseBareHostnameWithoutPort() { RedisNode node = RedisNode.fromString("my.redis.server"); assertThat(node.getHost()).isEqualTo("my.redis.server"); assertThat(node.getPort()).isEqualTo(DEFAULT_PORT); } } \ No newline at end of file From 8145291211c0d86994d2953e0cbd610b7227c129 Mon Sep 17 00:00:00 2001 From: LeeHyungGeol Date: Sun, 22 Sep 2024 17:18:33 +0900 Subject: [PATCH 2/5] Update RedisNode creation from bare hostname should assign default port --- .../org/springframework/data/redis/connection/RedisNode.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/springframework/data/redis/connection/RedisNode.java b/src/main/java/org/springframework/data/redis/connection/RedisNode.java index ec63cfb0c6..21202b76b0 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisNode.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisNode.java @@ -24,6 +24,7 @@ * @author Christoph Strobl * @author Thomas Darimont * @author Mark Paluch + * @author LeeHyungGeol * @since 1.4 */ public class RedisNode implements NamedNode { From 2889a56f8eb82f45b2152c5265a12e1408dfb57d Mon Sep 17 00:00:00 2001 From: LeeHyungGeol Date: Mon, 23 Sep 2024 19:33:31 +0900 Subject: [PATCH 3/5] Update RedisNodeUnitTests code lint to Spring Data Code Formatting Settings. --- .../data/redis/connection/RedisNodeUnitTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java b/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java index 46bc7bf7ec..9436f3df6d 100644 --- a/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java @@ -1 +1 @@ -package org.springframework.data.redis.connection; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; /** * Unit tests for {@link RedisNode}. * I referenced the test code from ConversionUnitTests.java * * @author LeeHyungGeol */ public class RedisNodeUnitTests { private static final int DEFAULT_PORT = 6379; @Test // IPv4 Address with port void shouldParseIPv4AddressWithPort() { RedisNode node = RedisNode.fromString("127.0.0.1:6379"); assertThat(node.getHost()).isEqualTo("127.0.0.1"); assertThat(node.getPort()).isEqualTo(6379); } @Test // IPv6 Address with port void shouldParseIPv6AddressWithPort() { RedisNode node = RedisNode.fromString("[aaaa:bbbb::dddd:eeee]:6379"); assertThat(node.getHost()).isEqualTo("aaaa:bbbb::dddd:eeee"); assertThat(node.getPort()).isEqualTo(6379); } @Test // Bare hostname with port void shouldParseBareHostnameWithPort() { RedisNode node = RedisNode.fromString("my.redis.server:6379"); assertThat(node.getHost()).isEqualTo("my.redis.server"); assertThat(node.getPort()).isEqualTo(6379); } @Test // Invalid port parsing void shouldThrowExceptionForInvalidPort() { assertThrows(IllegalArgumentException.class, () -> { RedisNode node = RedisNode.fromString("127.0.0.1:invalidPort"); }); } @Test // Bare IPv6 address (no square brackets) void shouldParseBareIPv6WithoutPort() { RedisNode node = RedisNode.fromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); assertThat(node.getHost()).isEqualTo("2001:0db8:85a3:0000:0000:8a2e:0370"); assertThat(node.getPort()).isEqualTo(7334); } @Test // Bare hostname without port void shouldParseBareHostnameWithoutPort() { RedisNode node = RedisNode.fromString("my.redis.server"); assertThat(node.getHost()).isEqualTo("my.redis.server"); assertThat(node.getPort()).isEqualTo(DEFAULT_PORT); } } \ No newline at end of file +package org.springframework.data.redis.connection; import static org.assertj.core.api.Assertions.*; import org.junit.jupiter.api.Test; public class RedisNodeUnitTests { private static final int DEFAULT_PORT = 6379; @Test // GH-2928 void shouldParseIPv4AddressWithPort() { RedisNode node = RedisNode.fromString("127.0.0.1:6379"); assertThat(node.getHost()).isEqualTo("127.0.0.1"); assertThat(node.getPort()).isEqualTo(6379); } @Test // GH-2928 void shouldParseIPv6AddressWithPort() { RedisNode node = RedisNode.fromString("[aaaa:bbbb::dddd:eeee]:6379"); assertThat(node.getHost()).isEqualTo("aaaa:bbbb::dddd:eeee"); assertThat(node.getPort()).isEqualTo(6379); } @Test // GH-2928 void shouldParseBareHostnameWithPort() { RedisNode node = RedisNode.fromString("my.redis.server:6379"); assertThat(node.getHost()).isEqualTo("my.redis.server"); assertThat(node.getPort()).isEqualTo(6379); } @Test // GH-2928 void shouldThrowExceptionForInvalidPort() { assertThatIllegalArgumentException() .isThrownBy(() -> RedisNode.fromString("127.0.0.1:invalidPort")); } @Test // GH-2928 void shouldParseBareIPv6WithoutPort() { RedisNode node = RedisNode.fromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); assertThat(node.getHost()).isEqualTo("2001:0db8:85a3:0000:0000:8a2e:0370"); assertThat(node.getPort()).isEqualTo(7334); } @Test // GH-2928 void shouldParseBareHostnameWithoutPort() { RedisNode node = RedisNode.fromString("my.redis.server"); assertThat(node.getHost()).isEqualTo("my.redis.server"); assertThat(node.getPort()).isEqualTo(DEFAULT_PORT); } } \ No newline at end of file From ec2b6c6c0fd28a6577a373276b4169d4f05e4eed Mon Sep 17 00:00:00 2001 From: LeeHyungGeol Date: Mon, 23 Sep 2024 21:13:49 +0900 Subject: [PATCH 4/5] Update RedisNodeUnitTests code lint to Intellij Default Settings. --- .../redis/connection/RedisNodeUnitTests.java | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java b/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java index 9436f3df6d..1c5693838e 100644 --- a/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java @@ -1 +1,51 @@ -package org.springframework.data.redis.connection; import static org.assertj.core.api.Assertions.*; import org.junit.jupiter.api.Test; public class RedisNodeUnitTests { private static final int DEFAULT_PORT = 6379; @Test // GH-2928 void shouldParseIPv4AddressWithPort() { RedisNode node = RedisNode.fromString("127.0.0.1:6379"); assertThat(node.getHost()).isEqualTo("127.0.0.1"); assertThat(node.getPort()).isEqualTo(6379); } @Test // GH-2928 void shouldParseIPv6AddressWithPort() { RedisNode node = RedisNode.fromString("[aaaa:bbbb::dddd:eeee]:6379"); assertThat(node.getHost()).isEqualTo("aaaa:bbbb::dddd:eeee"); assertThat(node.getPort()).isEqualTo(6379); } @Test // GH-2928 void shouldParseBareHostnameWithPort() { RedisNode node = RedisNode.fromString("my.redis.server:6379"); assertThat(node.getHost()).isEqualTo("my.redis.server"); assertThat(node.getPort()).isEqualTo(6379); } @Test // GH-2928 void shouldThrowExceptionForInvalidPort() { assertThatIllegalArgumentException() .isThrownBy(() -> RedisNode.fromString("127.0.0.1:invalidPort")); } @Test // GH-2928 void shouldParseBareIPv6WithoutPort() { RedisNode node = RedisNode.fromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); assertThat(node.getHost()).isEqualTo("2001:0db8:85a3:0000:0000:8a2e:0370"); assertThat(node.getPort()).isEqualTo(7334); } @Test // GH-2928 void shouldParseBareHostnameWithoutPort() { RedisNode node = RedisNode.fromString("my.redis.server"); assertThat(node.getHost()).isEqualTo("my.redis.server"); assertThat(node.getPort()).isEqualTo(DEFAULT_PORT); } } \ No newline at end of file +package org.springframework.data.redis.connection; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +public class RedisNodeUnitTests { + private static final int DEFAULT_PORT = 6379; + + @Test // GH-2928 + void shouldParseIPv4AddressWithPort() { + RedisNode node = RedisNode.fromString("127.0.0.1:6379"); + assertThat(node.getHost()).isEqualTo("127.0.0.1"); + assertThat(node.getPort()).isEqualTo(6379); + } + + @Test // GH-2928 + void shouldParseIPv6AddressWithPort() { + RedisNode node = RedisNode.fromString("[aaaa:bbbb::dddd:eeee]:6379"); + assertThat(node.getHost()).isEqualTo("aaaa:bbbb::dddd:eeee"); + assertThat(node.getPort()).isEqualTo(6379); + } + + @Test // GH-2928 + void shouldParseBareHostnameWithPort() { + RedisNode node = RedisNode.fromString("my.redis.server:6379"); + assertThat(node.getHost()).isEqualTo("my.redis.server"); + assertThat(node.getPort()).isEqualTo(6379); + } + + @Test // GH-2928 + void shouldThrowExceptionForInvalidPort() { + assertThatIllegalArgumentException() + .isThrownBy(() -> RedisNode.fromString("127.0.0.1:invalidPort")); + } + + @Test // GH-2928 + void shouldParseBareIPv6WithoutPort() { + RedisNode node = RedisNode.fromString("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); + assertThat(node.getHost()).isEqualTo("2001:0db8:85a3:0000:0000:8a2e:0370"); + assertThat(node.getPort()).isEqualTo(7334); + } + + @Test // GH-2928 + void shouldParseBareHostnameWithoutPort() { + RedisNode node = RedisNode.fromString("my.redis.server"); + assertThat(node.getHost()).isEqualTo("my.redis.server"); + assertThat(node.getPort()).isEqualTo(DEFAULT_PORT); + } +} + From 4a5214e6e827d6ed7d215e69c5d1b3769ea28c0e Mon Sep 17 00:00:00 2001 From: LeeHyungGeol Date: Mon, 23 Sep 2024 21:18:44 +0900 Subject: [PATCH 5/5] Add comment about Apache License & author in the headers of the class. --- .../redis/connection/RedisNodeUnitTests.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java b/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java index 1c5693838e..90ea456652 100644 --- a/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/RedisNodeUnitTests.java @@ -1,9 +1,30 @@ +/* + * Copyright 2014-2024 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.data.redis.connection; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.*; +/** + * Unit tests for {@link RedisNode}. + * referred to the test code in ConversionUnitTests.java + * + * @author LeeHyungGeol + */ public class RedisNodeUnitTests { private static final int DEFAULT_PORT = 6379;