From 6b8d5ca5d485e34d5dc12a4ccf86a200fd2aee98 Mon Sep 17 00:00:00 2001 From: erabii Date: Tue, 6 Jul 2021 16:40:53 -0400 Subject: [PATCH] fix (#815) --- .../commons/config/PropertySourceUtils.java | 37 ++++++++++-------- .../config/PropertySourceUtilsTest.java | 38 +++++++++++++++++++ 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtils.java index 86955ae6..87353ac2 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtils.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtils.java @@ -18,6 +18,7 @@ package org.springframework.cloud.kubernetes.commons.config; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.UncheckedIOException; import java.util.Map; import java.util.Properties; import java.util.function.BinaryOperator; @@ -44,6 +45,10 @@ import static org.springframework.cloud.kubernetes.commons.config.Constants.SPRI */ public final class PropertySourceUtils { + private PropertySourceUtils() { + throw new IllegalStateException("Can't instantiate a utility class"); + } + /** * Function to convert a String to Properties. */ @@ -54,7 +59,7 @@ public final class PropertySourceUtils { return properties; } catch (IOException e) { - throw new IllegalArgumentException(); + throw new UncheckedIOException(e); } }; @@ -62,12 +67,7 @@ public final class PropertySourceUtils { * Function to convert Properties to a Map. */ public static final Function> PROPERTIES_TO_MAP = p -> p.entrySet().stream() - .collect(Collectors.toMap(e -> String.valueOf(e.getKey()), Map.Entry::getValue, throwingMerger(), - java.util.LinkedHashMap::new)); - - private PropertySourceUtils() { - throw new IllegalStateException("Can't instantiate a utility class"); - } + .collect(Collectors.toMap(e -> e.getKey().toString(), Map.Entry::getValue)); /** * Function to convert String into Properties with an environment. @@ -80,12 +80,16 @@ public final class PropertySourceUtils { yamlFactory.setDocumentMatchers(properties -> { if (environment != null) { String profiles = null; - if (properties.containsKey(SPRING_CONFIG_ACTIVATE_ON_PROFILE)) { - profiles = properties.getProperty(SPRING_CONFIG_ACTIVATE_ON_PROFILE); + String activeOnProfile = properties.getProperty(SPRING_CONFIG_ACTIVATE_ON_PROFILE); + String springProfiles = properties.getProperty(SPRING_PROFILES); + + if (activeOnProfile != null) { + profiles = activeOnProfile; } - else if (properties.containsKey(SPRING_PROFILES)) { - profiles = properties.getProperty(SPRING_PROFILES); + else if (springProfiles != null) { + profiles = springProfiles; } + if (StringUtils.hasText(profiles)) { return environment.acceptsProfiles(Profiles.of(profiles)) ? FOUND : NOT_FOUND; } @@ -98,13 +102,14 @@ public final class PropertySourceUtils { } /** - * Throws IllegalStateException. - * @param Throwable. - * @return IllegalStateException. + * returns a {@link BinaryOperator} that unconditionally throws an + * {@link IllegalStateException}. + * @param type of the argument + * @return a {@link BinaryOperator} */ public static BinaryOperator throwingMerger() { - return (u, v) -> { - throw new IllegalStateException(String.format("Duplicate key %s", u)); + return (left, right) -> { + throw new IllegalStateException("Duplicate key " + left); }; } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtilsTest.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtilsTest.java index e2518f11..919543e3 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtilsTest.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtilsTest.java @@ -16,6 +16,7 @@ package org.springframework.cloud.kubernetes.commons.config; +import java.util.Map; import java.util.Properties; import java.util.function.Function; @@ -90,4 +91,41 @@ public class PropertySourceUtilsTest { assertThat(properties.getProperty("spring.config.activate.on-profile")).isNull(); } + @Test + void keyValueToProperties_noEntryPresent() { + Properties properties = PropertySourceUtils.KEY_VALUE_TO_PROPERTIES.apply(""); + assertThat(properties).isNotNull(); + } + + @Test + void keyValueToProperties_oneEntry() { + Properties properties = PropertySourceUtils.KEY_VALUE_TO_PROPERTIES.apply("a=b"); + assertThat(properties).isNotNull(); + assertThat(properties.getProperty("a")).isEqualTo("b"); + } + + @Test + void propertiesToMap_empty() { + Map result = PropertySourceUtils.PROPERTIES_TO_MAP.apply(new Properties()); + assertThat(result).isNotNull(); + assertThat(result).isEmpty(); + } + + @Test + void propertiesToMap_oneEntry() { + Properties properties = PropertySourceUtils.KEY_VALUE_TO_PROPERTIES.apply("a=b"); + Map result = PropertySourceUtils.PROPERTIES_TO_MAP.apply(properties); + assertThat(result).isNotNull(); + assertThat(result.get("a")).isEqualTo("b"); + } + + @Test + void propertiesToMap_sameKey() { + Properties properties = PropertySourceUtils.KEY_VALUE_TO_PROPERTIES.apply("a=b\na=c"); + Map result = PropertySourceUtils.PROPERTIES_TO_MAP.apply(properties); + assertThat(result).isNotNull(); + assertThat(result.get("a")).isEqualTo("c"); + + } + }