From 5ff9e6955ce372ccca13a93dd615348197f4249f Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 27 Sep 2023 17:16:54 +0200 Subject: [PATCH] Test status quo for void function references in SpEL --- .../spel/SpelCompilationCoverageTests.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java index 01cd0e446b7..f1c247400a4 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java @@ -29,6 +29,8 @@ import java.util.Set; import java.util.StringTokenizer; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.springframework.asm.MethodVisitor; import org.springframework.expression.AccessException; @@ -55,6 +57,7 @@ import static org.assertj.core.api.InstanceOfAssertFactories.BOOLEAN; * Checks SpelCompiler behavior. This should cover compilation all compiled node types. * * @author Andy Clement + * @author Sam Brannen * @since 4.1 */ public class SpelCompilationCoverageTests extends AbstractExpressionTests { @@ -997,6 +1000,60 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests { assertThat(expression.getValue(ctx).toString()).isEqualTo("4.0"); } + @ParameterizedTest + @ValueSource(strings = {"voidMethod", "voidWrapperMethod"}) + public void voidFunctionReference(String method) throws Exception { + assertVoidFunctionReferenceBehavior(method); + } + + private void assertVoidFunctionReferenceBehavior(String methodName) throws Exception { + Method method = getClass().getDeclaredMethod(methodName, String.class); + + EvaluationContext ctx = new StandardEvaluationContext(); + ctx.setVariable("voidMethod", method); + + expression = parser.parseExpression("#voidMethod('a')"); + + voidMethodInvokedWith = null; + expression.getValue(ctx); + assertThat(voidMethodInvokedWith).isEqualTo("a"); + assertCanCompile(expression); + + voidMethodInvokedWith = null; + expression.getValue(ctx); + assertThat(voidMethodInvokedWith).isEqualTo("a"); + assertCanCompile(expression); + + voidMethodInvokedWith = null; + expression.getValue(ctx); + assertThat(voidMethodInvokedWith).isEqualTo("a"); + assertCanCompile(expression); + + expression = parser.parseExpression("#voidMethod(#a)"); + ctx.setVariable("a", "foo"); + + voidMethodInvokedWith = null; + expression.getValue(ctx); + assertThat(voidMethodInvokedWith).isEqualTo("foo"); + assertCanCompile(expression); + + voidMethodInvokedWith = null; + expression.getValue(ctx); + assertThat(voidMethodInvokedWith).isEqualTo("foo"); + assertCanCompile(expression); + } + + private static String voidMethodInvokedWith; + + public static Void voidWrapperMethod(String str) { + voidMethodInvokedWith = str; + return null; + } + + public static void voidMethod(String str) { + voidMethodInvokedWith = str; + } + @Test public void functionReferenceVisibility_SPR12359() throws Exception { // Confirms visibility of what is being called.