diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java index 190b05ffb31..8091c9c68e6 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/util/TestPropertyValues.java @@ -39,7 +39,6 @@ import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.SystemEnvironmentPropertySource; -import org.springframework.lang.Contract; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -327,7 +326,6 @@ public final class TestPropertyValues { private final @Nullable String value; private Pair(String name, @Nullable String value) { - Assert.hasLength(name, "'name' must not be empty"); this.name = name; this.value = value; } @@ -336,7 +334,7 @@ public final class TestPropertyValues { properties.put(this.name, this.value); } - public static @Nullable Pair parse(String pair) { + public static Pair parse(String pair) { int index = getSeparatorIndex(pair); String name = (index > 0) ? pair.substring(0, index) : pair; String value = (index > 0) ? pair.substring(index + 1) : ""; @@ -361,23 +359,19 @@ public final class TestPropertyValues { * @return the {@link Pair} instance or {@code null} * @since 2.4.0 */ - @Contract("!null -> !null") - public static @Nullable Pair fromMapEntry(Map.@Nullable Entry entry) { - return (entry != null) ? of(entry.getKey(), entry.getValue()) : null; + public static Pair fromMapEntry(Map.Entry entry) { + return of(entry.getKey(), entry.getValue()); } /** * Factory method to create a {@link Pair} from a name and value. * @param name the name * @param value the value - * @return the {@link Pair} instance or {@code null} + * @return the {@link Pair} * @since 2.4.0 */ - public static @Nullable Pair of(@Nullable String name, @Nullable String value) { - if (StringUtils.hasLength(name)) { - return new Pair(name, value); - } - return null; + public static Pair of(String name, @Nullable String value) { + return new Pair(name, value); } } @@ -387,14 +381,14 @@ public final class TestPropertyValues { */ private class SystemPropertiesHandler implements Closeable { - private final Map previous; + private final Map previous; SystemPropertiesHandler() { this.previous = apply(TestPropertyValues.this.properties); } - private Map apply(Map properties) { - Map previous = new LinkedHashMap<>(); + private Map apply(Map properties) { + Map previous = new LinkedHashMap<>(); properties.forEach((name, value) -> previous.put(name, setOrClear(name, (String) value))); return previous; } @@ -404,7 +398,7 @@ public final class TestPropertyValues { this.previous.forEach(this::setOrClear); } - private String setOrClear(String name, String value) { + private @Nullable String setOrClear(String name, @Nullable String value) { Assert.notNull(name, "'name' must not be null"); if (!StringUtils.hasLength(value)) { return (String) System.getProperties().remove(name); diff --git a/core/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java b/core/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java index c065c3346d2..d2d49a2a52a 100644 --- a/core/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java +++ b/core/spring-boot-test/src/test/java/org/springframework/boot/test/util/TestPropertyValuesTests.java @@ -200,8 +200,8 @@ class TestPropertyValuesTests { } @Test - void pairOfWhenNameAndValueAreEmptyReturnsNull() { - assertThat(Pair.of("", "")).isNull(); + void pairOfWhenNameAndValueAreEmptyReturnsPair() { + assertThat(Pair.of("", "")).isNotNull(); } @Test @@ -213,4 +213,13 @@ class TestPropertyValuesTests { assertThat(map).containsOnly(entry("spring", "boot")); } + @Test + void pairFromMapEntryWithEmptyKeyCreatesPair() { + Map map = new LinkedHashMap<>(); + Pair pair = Pair.fromMapEntry(entry("", "empty-key")); + assertThat(pair).isNotNull(); + pair.addTo(map); + assertThat(map).containsOnly(entry("", "empty-key")); + } + }