Skip to content

RedisNode creation from bare hostname assigns default port #3002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
* @author Christoph Strobl
* @author Thomas Darimont
* @author Mark Paluch
* @author LeeHyungGeol
* @since 1.4
*/
public class RedisNode implements NamedNode {

private static final int DEFAULT_PORT = 6379;

@Nullable String id;
@Nullable String name;
@Nullable String host;
Expand Down Expand Up @@ -94,7 +97,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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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); }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what has happened here. In any case, this test is formatted to not be readable in any way.

Copy link
Contributor Author

@LeeHyungGeol LeeHyungGeol Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mp911de
I apologize for the issue with the code formatting. I have identified the problem and am currently making corrections. I suspect that the cause of the formatting issue is related to code lint. Therefore, I checked the Spring Data Code Formatting Settings on this page: https://github.com/spring-projects/spring-data-build/tree/main/etc/ide and I am applying those settings.

I am currently trying to apply eclipse-java-google-style.xml as the Eclipse code formatter. Do I need any other formatter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mp911de
I have fixed the issue with the code formatting. I would appreciate it if you could review the PR again.

Expand Down