From e4bcda258a9d29e57fe81772741284a59c7708a7 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 18 Oct 2024 13:35:25 -0700 Subject: [PATCH] Polish "Add spring.data.redis.lettuce.read-from property" See gh-42588 Co-authored-by: Stephane Nicoll --- .../redis/LettuceConnectionConfiguration.java | 16 +++++++++++---- .../redis/RedisAutoConfigurationTests.java | 20 +++++++++---------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java index c47bf7cf72..5d2bf8b833 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java @@ -174,17 +174,25 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration { } } - private static ReadFrom getReadFrom(String readFrom) { + private ReadFrom getReadFrom(String readFrom) { int index = readFrom.indexOf(':'); if (index == -1) { - String name = readFrom.replaceAll("-", ""); - return ReadFrom.valueOf(name); + return ReadFrom.valueOf(getCanonicalReadFromName(readFrom)); } - String name = readFrom.substring(0, index).replaceAll("-", ""); + String name = getCanonicalReadFromName(readFrom.substring(0, index)); String value = readFrom.substring(index + 1); return ReadFrom.valueOf(name + ":" + value); } + private String getCanonicalReadFromName(String name) { + StringBuilder canonicalName = new StringBuilder(name.length()); + name.chars() + .filter(Character::isLetterOrDigit) + .map(Character::toLowerCase) + .forEach((c) -> canonicalName.append((char) c)); + return canonicalName.toString(); + } + private ClientOptions createClientOptions( ObjectProvider clientConfigurationBuilderCustomizers) { ClientOptions.Builder builder = initializeClientOptionsBuilder(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java index d27392b2f2..ad26b55f9a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 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. @@ -122,7 +122,7 @@ class RedisAutoConfigurationTests { }); } - @ParameterizedTest + @ParameterizedTest(name = "{0}") @MethodSource void shouldConfigureLettuceReadFromProperty(String type, ReadFrom readFrom) { this.contextRunner.withPropertyValues("spring.data.redis.lettuce.read-from:" + type).run((context) -> { @@ -132,6 +132,14 @@ class RedisAutoConfigurationTests { }); } + static Stream shouldConfigureLettuceReadFromProperty() { + return Stream.of(Arguments.of("any", ReadFrom.ANY), Arguments.of("any-replica", ReadFrom.ANY_REPLICA), + Arguments.of("lowest-latency", ReadFrom.LOWEST_LATENCY), Arguments.of("replica", ReadFrom.REPLICA), + Arguments.of("replica-preferred", ReadFrom.REPLICA_PREFERRED), + Arguments.of("upstream", ReadFrom.UPSTREAM), + Arguments.of("upstream-preferred", ReadFrom.UPSTREAM_PREFERRED)); + } + @Test void shouldConfigureLettuceRegexReadFromProperty() { RedisClusterNode node1 = createRedisNode("redis-node-1.region-1.example.com"); @@ -688,14 +696,6 @@ class RedisAutoConfigurationTests { return ReflectionTestUtils.invokeMethod(factory, "getRedisUsername"); } - static Stream shouldConfigureLettuceReadFromProperty() { - return Stream.of(Arguments.of("any", ReadFrom.ANY), Arguments.of("any-replica", ReadFrom.ANY_REPLICA), - Arguments.of("lowest-latency", ReadFrom.LOWEST_LATENCY), Arguments.of("replica", ReadFrom.REPLICA), - Arguments.of("replica-preferred", ReadFrom.REPLICA_PREFERRED), - Arguments.of("upstream", ReadFrom.UPSTREAM), - Arguments.of("upstream-preferred", ReadFrom.UPSTREAM_PREFERRED)); - } - private RedisClusterNode createRedisNode(String host) { RedisClusterNode node = new RedisClusterNode(); node.setUri(RedisURI.Builder.redis(host).build());