Skip to content

Commit dc2dbd9

Browse files
committed
Polishing
1 parent 6b67972 commit dc2dbd9

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/SpelMessage.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,8 @@
2020

2121
/**
2222
* Contains all the messages that can be produced by the Spring Expression Language.
23-
* Each message has a kind (info, warn, error) and a code number. Tests can be written to
23+
*
24+
* <p>Each message has a kind (info, warn, error) and a code number. Tests can be written to
2425
* expect particular code numbers rather than particular text, enabling the message text
2526
* to more easily be modified and the tests to run successfully in different locales.
2627
*
@@ -100,7 +101,7 @@ public enum SpelMessage {
100101
"A problem occurred whilst attempting to access the property ''{0}'': ''{1}''"),
101102

102103
FUNCTION_REFERENCE_CANNOT_BE_INVOKED(Kind.ERROR, 1022,
103-
"The function ''{0}'' mapped to an object of type ''{1}'' which cannot be invoked"),
104+
"The function ''{0}'' mapped to an object of type ''{1}'' cannot be invoked"),
104105

105106
EXCEPTION_DURING_FUNCTION_CALL(Kind.ERROR, 1023,
106107
"A problem occurred whilst attempting to invoke the function ''{0}'': ''{1}''"),

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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,55 @@
2323

2424
import static org.assertj.core.api.Assertions.assertThat;
2525
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;
2628

2729
/**
28-
* Tests the evaluation of expressions that access variables and functions (lambda/java).
30+
* Tests the evaluation of expressions that access variables and functions.
2931
*
3032
* @author Andy Clement
3133
* @author Sam Brannen
3234
*/
3335
class VariableAndFunctionTests extends AbstractExpressionTests {
3436

3537
@Test
36-
void testVariableAccess01() {
38+
void variableAccess() {
3739
evaluate("#answer", "42", Integer.class, SHOULD_BE_WRITABLE);
3840
evaluate("#answer / 2", 21, Integer.class, SHOULD_NOT_BE_WRITABLE);
3941
}
4042

4143
@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);
4547
}
4648

4749
@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() {
4963
evaluate("#reverseInt(1,2,3)", "int[3]{3,2,1}", int[].class);
5064
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);
5265
}
5366

5467
@Test
55-
void testFunctionAccess02() {
68+
void functionInvocationWithStringArgument() {
5669
evaluate("#reverseString('hello')", "olleh", String.class);
5770
evaluate("#reverseString(37)", "73", String.class); // requires type conversion of 37 to '37'
5871
}
5972

6073
@Test
61-
void testCallVarargsFunction() {
74+
void functionWithVarargs() {
6275
evaluate("#varargsFunction()", "[]", String.class);
6376
evaluate("#varargsFunction(new String[0])", "[]", String.class);
6477
evaluate("#varargsFunction('a')", "[a]", String.class);
@@ -89,13 +102,13 @@ void testCallVarargsFunction() {
89102
}
90103

91104
@Test
92-
void testCallingIllegalFunctions() throws Exception {
105+
void functionMethodMustBeStatic() throws Exception {
93106
SpelExpressionParser parser = new SpelExpressionParser();
94107
StandardEvaluationContext ctx = new StandardEvaluationContext();
95108
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));
99112
}
100113

101114

0 commit comments

Comments
 (0)