|
23 | 23 |
|
24 | 24 | import static org.assertj.core.api.Assertions.assertThat;
|
25 | 25 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 26 | +import static org.springframework.expression.spel.SpelMessage.FUNCTION_MUST_BE_STATIC; |
| 27 | +import static org.springframework.expression.spel.SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION; |
26 | 28 |
|
27 | 29 | /**
|
28 |
| - * Tests the evaluation of expressions that access variables and functions (lambda/java). |
| 30 | + * Tests the evaluation of expressions that access variables and functions. |
29 | 31 | *
|
30 | 32 | * @author Andy Clement
|
31 | 33 | * @author Sam Brannen
|
32 | 34 | */
|
33 | 35 | class VariableAndFunctionTests extends AbstractExpressionTests {
|
34 | 36 |
|
35 | 37 | @Test
|
36 |
| - void testVariableAccess01() { |
| 38 | + void variableAccess() { |
37 | 39 | evaluate("#answer", "42", Integer.class, SHOULD_BE_WRITABLE);
|
38 | 40 | evaluate("#answer / 2", 21, Integer.class, SHOULD_NOT_BE_WRITABLE);
|
39 | 41 | }
|
40 | 42 |
|
41 | 43 | @Test
|
42 |
| - void testVariableAccess_WellKnownVariables() { |
43 |
| - evaluate("#this.getName()","Nikola Tesla",String.class); |
44 |
| - evaluate("#root.getName()","Nikola Tesla",String.class); |
| 44 | + void variableAccessWithWellKnownVariables() { |
| 45 | + evaluate("#this.getName()", "Nikola Tesla", String.class); |
| 46 | + evaluate("#root.getName()", "Nikola Tesla", String.class); |
45 | 47 | }
|
46 | 48 |
|
47 | 49 | @Test
|
48 |
| - void testFunctionAccess01() { |
| 50 | + void functionInvocationWithIncorrectNumberOfArguments() { |
| 51 | + // Method: reverseInt() expects 3 ints |
| 52 | + evaluateAndCheckError("#reverseInt()", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 0, 3); |
| 53 | + evaluateAndCheckError("#reverseInt(1,2)", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 2, 3); |
| 54 | + evaluateAndCheckError("#reverseInt(1,2,3,4)", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 4, 3); |
| 55 | + |
| 56 | + // MethodHandle: message() maps to java.lang.String.format(String, Object...) |
| 57 | + evaluateAndCheckError("#message()", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 0, 2); |
| 58 | + evaluateAndCheckError("#message('%s')", INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 1, 2); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void functionInvocationWithPrimitiveArguments() { |
49 | 63 | evaluate("#reverseInt(1,2,3)", "int[3]{3,2,1}", int[].class);
|
50 | 64 | evaluate("#reverseInt('1',2,3)", "int[3]{3,2,1}", int[].class); // requires type conversion of '1' to 1
|
51 |
| - evaluateAndCheckError("#reverseInt(1)", SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 0, 1, 3); |
52 | 65 | }
|
53 | 66 |
|
54 | 67 | @Test
|
55 |
| - void testFunctionAccess02() { |
| 68 | + void functionInvocationWithStringArgument() { |
56 | 69 | evaluate("#reverseString('hello')", "olleh", String.class);
|
57 | 70 | evaluate("#reverseString(37)", "73", String.class); // requires type conversion of 37 to '37'
|
58 | 71 | }
|
59 | 72 |
|
60 | 73 | @Test
|
61 |
| - void testCallVarargsFunction() { |
| 74 | + void functionWithVarargs() { |
62 | 75 | evaluate("#varargsFunction()", "[]", String.class);
|
63 | 76 | evaluate("#varargsFunction(new String[0])", "[]", String.class);
|
64 | 77 | evaluate("#varargsFunction('a')", "[a]", String.class);
|
@@ -89,13 +102,13 @@ void testCallVarargsFunction() {
|
89 | 102 | }
|
90 | 103 |
|
91 | 104 | @Test
|
92 |
| - void testCallingIllegalFunctions() throws Exception { |
| 105 | + void functionMethodMustBeStatic() throws Exception { |
93 | 106 | SpelExpressionParser parser = new SpelExpressionParser();
|
94 | 107 | StandardEvaluationContext ctx = new StandardEvaluationContext();
|
95 | 108 | ctx.setVariable("notStatic", this.getClass().getMethod("nonStatic"));
|
96 |
| - assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> |
97 |
| - parser.parseRaw("#notStatic()").getValue(ctx)). |
98 |
| - satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.FUNCTION_MUST_BE_STATIC)); |
| 109 | + assertThatExceptionOfType(SpelEvaluationException.class) |
| 110 | + .isThrownBy(() -> parser.parseRaw("#notStatic()").getValue(ctx)) |
| 111 | + .satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(FUNCTION_MUST_BE_STATIC)); |
99 | 112 | }
|
100 | 113 |
|
101 | 114 |
|
|
0 commit comments