Skip to content

Commit 64fc9ee

Browse files
committed
Test function registration with SimpleEvaluationContext
1 parent ce43d1b commit 64fc9ee

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

spring-expression/src/test/java/org/springframework/expression/spel/SpelDocumentationTests.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.invoke.MethodHandle;
2020
import java.lang.invoke.MethodHandles;
2121
import java.lang.invoke.MethodType;
22+
import java.lang.reflect.Method;
2223
import java.util.ArrayList;
2324
import java.util.Date;
2425
import java.util.GregorianCalendar;
@@ -39,6 +40,7 @@
3940
import org.springframework.expression.spel.support.StandardEvaluationContext;
4041
import org.springframework.expression.spel.testresources.Inventor;
4142
import org.springframework.expression.spel.testresources.PlaceOfBirth;
43+
import org.springframework.util.ReflectionUtils;
4244

4345
import static org.assertj.core.api.Assertions.assertThat;
4446
import static org.assertj.core.api.Assertions.within;
@@ -554,45 +556,51 @@ void thisAndRootVariables() {
554556
@Nested
555557
class Functions {
556558

559+
private final ExpressionParser parser = new SpelExpressionParser();
560+
561+
private final StandardEvaluationContext context = new StandardEvaluationContext();
562+
563+
private final Method reverseStringMethod = ReflectionUtils.findMethod(StringUtils.class, "reverseString", String.class);
564+
557565
@Test
558-
void functions() throws Exception {
559-
ExpressionParser parser = new SpelExpressionParser();
560-
StandardEvaluationContext context = new StandardEvaluationContext();
561-
context.registerFunction("reverseString", StringUtils.class.getDeclaredMethod("reverseString", String.class));
566+
void registerFunctionViaMethodWithStandardEvaluationContext() {
567+
context.registerFunction("reverseString", reverseStringMethod);
562568

563-
String helloWorldReversed = parser.parseExpression("#reverseString('hello world')").getValue(context, String.class);
564-
assertThat(helloWorldReversed).isEqualTo("dlrow olleh");
569+
String result = parser.parseExpression("#reverseString('hello world')").getValue(context, String.class);
570+
assertThat(result).isEqualTo("dlrow olleh");
565571
}
566572

567573
@Test
568-
void methodHandlesNotBound() throws Exception {
569-
ExpressionParser parser = new SpelExpressionParser();
570-
StandardEvaluationContext context = new StandardEvaluationContext();
574+
void registerFunctionViaMethodWithSimpleEvaluationContext() {
575+
SimpleEvaluationContext simpleContext = SimpleEvaluationContext.forReadOnlyDataBinding().build();
576+
simpleContext.setVariable("reverseString", reverseStringMethod);
571577

572-
MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "formatted",
578+
String result = parser.parseExpression("#reverseString('hello world')").getValue(simpleContext, String.class);
579+
assertThat(result).isEqualTo("dlrow olleh");
580+
}
581+
582+
@Test
583+
void registerFunctionViaMethodHandleNotBound() throws Exception {
584+
MethodHandle methodHandle = MethodHandles.lookup().findVirtual(String.class, "formatted",
573585
MethodType.methodType(String.class, Object[].class));
574-
context.setVariable("message", mh);
586+
context.registerFunction("message", methodHandle);
575587

576588
String message = parser.parseExpression("#message('Simple message: <%s>', 'Hello World', 'ignored')")
577589
.getValue(context, String.class);
578590
assertThat(message).isEqualTo("Simple message: <Hello World>");
579591
}
580592

581593
@Test
582-
void methodHandlesFullyBound() throws Throwable {
583-
ExpressionParser parser = new SpelExpressionParser();
584-
StandardEvaluationContext context = new StandardEvaluationContext();
585-
594+
void registerFunctionViaMethodHandleFullyBound() throws Exception {
586595
String template = "This is a %s message with %s words: <%s>";
587596
Object varargs = new Object[] { "prerecorded", 3, "Oh Hello World!", "ignored" };
588-
MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "formatted",
597+
MethodHandle methodHandle = MethodHandles.lookup().findVirtual(String.class, "formatted",
589598
MethodType.methodType(String.class, Object[].class))
590599
.bindTo(template)
591-
.bindTo(varargs); //here we have to provide arguments in a single array binding
592-
context.setVariable("message", mh);
600+
.bindTo(varargs); // here we have to provide arguments in a single array binding
601+
context.registerFunction("message", methodHandle);
593602

594-
String message = parser.parseExpression("#message()")
595-
.getValue(context, String.class);
603+
String message = parser.parseExpression("#message()").getValue(context, String.class);
596604
assertThat(message).isEqualTo("This is a prerecorded message with 3 words: <Oh Hello World!>");
597605
}
598606
}

0 commit comments

Comments
 (0)