Propagate getFactoryMethod() when using InstanceSupplier.andThen()

Update `InstanceSupplier.andThen` to propagate the result of
`getFactoryMethod()`.

See gh-28748
This commit is contained in:
Phillip Webb
2022-07-20 18:56:57 +01:00
parent 3ae1b9ba57
commit 069d6d3280
2 changed files with 56 additions and 4 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.beans.factory.support;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import org.springframework.util.function.ThrowingBiFunction;
@@ -55,7 +57,7 @@ class InstanceSupplierTests {
}
@Test
void andThenWithBiFunctionWhenFunctionIsNullThrowsException() {
void andThenWhenFunctionIsNullThrowsException() {
InstanceSupplier<String> supplier = registeredBean -> "test";
ThrowingBiFunction<RegisteredBean, String, String> after = null;
assertThatIllegalArgumentException().isThrownBy(() -> supplier.andThen(after))
@@ -63,13 +65,23 @@ class InstanceSupplierTests {
}
@Test
void andThenWithBiFunctionAppliesFunctionToObtainResult() throws Exception {
void andThenAppliesFunctionToObtainResult() throws Exception {
InstanceSupplier<String> supplier = registeredBean -> "bean";
supplier = supplier.andThen(
(registeredBean, string) -> registeredBean.getBeanName() + "-" + string);
assertThat(supplier.get(this.registeredBean)).isEqualTo("test-bean");
}
@Test
void andThenWhenInstanceSupplierHasFactoryMethod() throws Exception {
Method factoryMethod = getClass().getDeclaredMethod("andThenWhenInstanceSupplierHasFactoryMethod");
InstanceSupplier<String> supplier = InstanceSupplier.using(factoryMethod, () -> "bean");
supplier = supplier.andThen(
(registeredBean, string) -> registeredBean.getBeanName() + "-" + string);
assertThat(supplier.get(this.registeredBean)).isEqualTo("test-bean");
assertThat(supplier.getFactoryMethod()).isSameAs(factoryMethod);
}
@Test
void ofSupplierWhenInstanceSupplierReturnsSameInstance() {
InstanceSupplier<String> supplier = registeredBean -> "test";