diff --git a/spring-core/src/main/java/org/springframework/util/function/SupplierUtils.java b/spring-core/src/main/java/org/springframework/util/function/SupplierUtils.java index 22f08636f27..6772ec543ab 100644 --- a/spring-core/src/main/java/org/springframework/util/function/SupplierUtils.java +++ b/spring-core/src/main/java/org/springframework/util/function/SupplierUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-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. @@ -40,4 +40,16 @@ public abstract class SupplierUtils { return (supplier != null ? supplier.get() : null); } + /** + * Resolve a given {@code Supplier}, getting its result or immediately + * returning the given Object as-is if not a {@code Supplier}. + * @param candidate the candidate to resolve (potentially a {@code Supplier}) + * @return a supplier's result or the given Object as-is + * @since 6.1.4 + */ + @Nullable + public static Object resolve(@Nullable Object candidate) { + return (candidate instanceof Supplier supplier ? supplier.get() : null); + } + } diff --git a/spring-test/src/main/java/org/springframework/test/context/support/DynamicValuesPropertySource.java b/spring-test/src/main/java/org/springframework/test/context/support/DynamicValuesPropertySource.java index 51aee79bf11..66420036131 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/DynamicValuesPropertySource.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/DynamicValuesPropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-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. @@ -19,38 +19,27 @@ package org.springframework.test.context.support; import java.util.Map; import java.util.function.Supplier; -import org.springframework.core.env.EnumerablePropertySource; -import org.springframework.util.StringUtils; +import org.springframework.core.env.MapPropertySource; +import org.springframework.util.function.SupplierUtils; /** - * {@link EnumerablePropertySource} backed by a map with dynamically supplied - * values. + * {@link MapPropertySource} backed by a map with dynamically supplied values. * * @author Phillip Webb * @author Sam Brannen + * @author Juergen Hoeller * @since 5.2.5 */ -class DynamicValuesPropertySource extends EnumerablePropertySource>> { +class DynamicValuesPropertySource extends MapPropertySource { + @SuppressWarnings({"rawtypes", "unchecked"}) DynamicValuesPropertySource(String name, Map> valueSuppliers) { - super(name, valueSuppliers); + super(name, (Map) valueSuppliers); } - @Override public Object getProperty(String name) { - Supplier valueSupplier = this.source.get(name); - return (valueSupplier != null ? valueSupplier.get() : null); - } - - @Override - public boolean containsProperty(String name) { - return this.source.containsKey(name); - } - - @Override - public String[] getPropertyNames() { - return StringUtils.toStringArray(this.source.keySet()); + return SupplierUtils.resolve(super.getProperty(name)); } }