32
32
import org .springframework .expression .spel .standard .SpelExpression ;
33
33
import org .springframework .expression .spel .standard .SpelExpressionParser ;
34
34
import org .springframework .expression .spel .support .StandardEvaluationContext ;
35
+ import org .springframework .expression .spel .support .StandardTypeLocator ;
36
+ import org .springframework .expression .spel .testresources .Inventor ;
35
37
import org .springframework .expression .spel .testresources .PlaceOfBirth ;
36
38
37
39
import static org .assertj .core .api .Assertions .assertThat ;
48
50
class MethodInvocationTests extends AbstractExpressionTests {
49
51
50
52
@ Test
51
- void testSimpleAccess01 () {
53
+ void simpleAccess () {
52
54
evaluate ("getPlaceOfBirth().getCity()" , "Smiljan" , String .class );
53
55
}
54
56
55
57
@ Test
56
- void testStringClass () {
58
+ void stringClass () {
57
59
evaluate ("new java.lang.String('hello').charAt(2)" , 'l' , Character .class );
58
60
evaluate ("new java.lang.String('hello').charAt(2).equals('l'.charAt(0))" , true , Boolean .class );
59
61
evaluate ("'HELLO'.toLowerCase()" , "hello" , String .class );
60
62
evaluate ("' abcba '.trim()" , "abcba" , String .class );
61
63
}
62
64
63
65
@ Test
64
- void testNonExistentMethods () {
66
+ void nonExistentMethods () {
65
67
// name is ok but madeup() does not exist
66
68
evaluateAndCheckError ("name.madeup()" , SpelMessage .METHOD_NOT_FOUND , 5 );
67
69
}
68
70
69
71
@ Test
70
- void testWidening01 () {
72
+ void widening () {
71
73
// widening of int 3 to double 3 is OK
72
74
evaluate ("new Double(3.0d).compareTo(8)" , -1 , Integer .class );
73
75
evaluate ("new Double(3.0d).compareTo(3)" , 0 , Integer .class );
74
76
evaluate ("new Double(3.0d).compareTo(2)" , 1 , Integer .class );
75
77
}
76
78
77
79
@ Test
78
- void testArgumentConversion01 () {
80
+ void argumentConversion () {
79
81
// Rely on Double>String conversion for calling startsWith()
80
82
evaluate ("new String('hello 2.0 to you').startsWith(7.0d)" , false , Boolean .class );
81
83
evaluate ("new String('7.0 foobar').startsWith(7.0d)" , true , Boolean .class );
82
84
}
83
85
84
- @ Test
85
- void testMethodThrowingException_SPR6760 () {
86
+ @ Test // SPR-6760
87
+ void methodThrowingException () {
86
88
// Test method on inventor: throwException()
87
- // On 1 it will throw an IllegalArgumentException
88
- // On 2 it will throw a RuntimeException
89
- // On 3 it will exit normally
89
+ // On 1 it will throw an IllegalArgumentException.
90
+ // On 2 it will throw a RuntimeException.
91
+ // On 4 it will throw a TestException.
92
+ // Otherwise, it will exit normally.
90
93
// In each case it increments the Inventor field 'counter' when invoked
91
94
92
95
SpelExpressionParser parser = new SpelExpressionParser ();
@@ -115,7 +118,6 @@ void testMethodThrowingException_SPR6760() {
115
118
assertThat (o ).isEqualTo (3 );
116
119
assertThat (parser .parseExpression ("counter" ).getValue (eContext )).isEqualTo (2 );
117
120
118
-
119
121
// Now cause it to throw an exception:
120
122
eContext .setVariable ("bar" , 1 );
121
123
assertThatException ()
@@ -135,12 +137,13 @@ void testMethodThrowingException_SPR6760() {
135
137
/**
136
138
* Check on first usage (when the cachedExecutor in MethodReference is null) that the exception is not wrapped.
137
139
*/
138
- @ Test
139
- void testMethodThrowingException_SPR6941 () {
140
+ @ Test // SPR-6941
141
+ void methodThrowingRuntimeException () {
140
142
// Test method on inventor: throwException()
141
- // On 1 it will throw an IllegalArgumentException
142
- // On 2 it will throw a RuntimeException
143
- // On 3 it will exit normally
143
+ // On 1 it will throw an IllegalArgumentException.
144
+ // On 2 it will throw a RuntimeException.
145
+ // On 4 it will throw a TestException.
146
+ // Otherwise, it will exit normally.
144
147
// In each case it increments the Inventor field 'counter' when invoked
145
148
146
149
SpelExpressionParser parser = new SpelExpressionParser ();
@@ -152,12 +155,13 @@ void testMethodThrowingException_SPR6941() {
152
155
.isNotInstanceOf (SpelEvaluationException .class );
153
156
}
154
157
155
- @ Test
156
- void testMethodThrowingException_SPR6941_2 () {
158
+ @ Test // SPR-6941
159
+ void methodThrowingCustomException () {
157
160
// Test method on inventor: throwException()
158
- // On 1 it will throw an IllegalArgumentException
159
- // On 2 it will throw a RuntimeException
160
- // On 3 it will exit normally
161
+ // On 1 it will throw an IllegalArgumentException.
162
+ // On 2 it will throw a RuntimeException.
163
+ // On 4 it will throw a TestException.
164
+ // Otherwise, it will exit normally.
161
165
// In each case it increments the Inventor field 'counter' when invoked
162
166
163
167
SpelExpressionParser parser = new SpelExpressionParser ();
@@ -166,12 +170,11 @@ void testMethodThrowingException_SPR6941_2() {
166
170
context .setVariable ("bar" , 4 );
167
171
assertThatExceptionOfType (ExpressionInvocationTargetException .class )
168
172
.isThrownBy (() -> expr .getValue (context ))
169
- .satisfies (ex -> assertThat (ex .getCause ().getClass ().getName ()).isEqualTo (
170
- "org.springframework.expression.spel.testresources.Inventor$TestException" ));
173
+ .withCauseExactlyInstanceOf (Inventor .TestException .class );
171
174
}
172
175
173
- @ Test
174
- void testMethodFiltering_SPR6764 () {
176
+ @ Test // SPR-6764
177
+ void methodFiltering () {
175
178
SpelExpressionParser parser = new SpelExpressionParser ();
176
179
StandardEvaluationContext context = new StandardEvaluationContext ();
177
180
context .setRootObject (new TestObject ());
@@ -211,7 +214,7 @@ void testMethodFiltering_SPR6764() {
211
214
}
212
215
213
216
@ Test
214
- void testAddingMethodResolvers () {
217
+ void addingMethodResolvers () {
215
218
StandardEvaluationContext ctx = new StandardEvaluationContext ();
216
219
217
220
// reflective method accessor is the only one by default
@@ -235,7 +238,7 @@ void testAddingMethodResolvers() {
235
238
}
236
239
237
240
@ Test
238
- void testVarargsInvocation01 () {
241
+ void varargsInvocation01 () {
239
242
// Calling 'public String aVarargsMethod(String... strings)'
240
243
evaluate ("aVarargsMethod('a','b','c')" , "[a, b, c]" , String .class );
241
244
evaluate ("aVarargsMethod('a')" , "[a]" , String .class );
@@ -252,7 +255,7 @@ void testVarargsInvocation01() {
252
255
}
253
256
254
257
@ Test
255
- void testVarargsInvocation02 () {
258
+ void varargsInvocation02 () {
256
259
// Calling 'public String aVarargsMethod2(int i, String... strings)'
257
260
evaluate ("aVarargsMethod2(5,'a','b','c')" , "5-[a, b, c]" , String .class );
258
261
evaluate ("aVarargsMethod2(2,'a')" , "2-[a]" , String .class );
@@ -267,7 +270,7 @@ void testVarargsInvocation02() {
267
270
}
268
271
269
272
@ Test
270
- void testVarargsInvocation03 () {
273
+ void varargsInvocation03 () {
271
274
// Calling 'public int aVarargsMethod3(String str1, String... strings)' - returns all strings concatenated with "-"
272
275
273
276
// No conversion necessary
@@ -295,7 +298,7 @@ void testVarargsInvocation03() {
295
298
}
296
299
297
300
@ Test // gh-33013
298
- void testVarargsWithObjectArrayType () {
301
+ void varargsWithObjectArrayType () {
299
302
// Calling 'public String formatObjectVarargs(String format, Object... args)' -> String.format(format, args)
300
303
301
304
// No var-args and no conversion necessary
@@ -336,7 +339,7 @@ void testVarargsWithObjectArrayType() {
336
339
}
337
340
338
341
@ Test
339
- void testVarargsWithPrimitiveArrayType () {
342
+ void varargsWithPrimitiveArrayType () {
340
343
// Calling 'public String formatPrimitiveVarargs(String format, int... nums)' -> effectively String.format(format, args)
341
344
342
345
// No var-args and no conversion necessary
@@ -357,13 +360,28 @@ void testVarargsWithPrimitiveArrayType() {
357
360
}
358
361
359
362
@ Test
360
- void testVarargsWithPrimitiveArrayToObjectArrayConversion () {
363
+ void varargsWithPrimitiveArrayToObjectArrayConversion () {
361
364
evaluate ("formatObjectVarargs('x -> %s %s %s', new short[]{1, 2, 3})" , "x -> 1 2 3" , String .class ); // short[] to Object[]
362
365
evaluate ("formatObjectVarargs('x -> %s %s %s', new int[]{1, 2, 3})" , "x -> 1 2 3" , String .class ); // int[] to Object[]
363
366
}
364
367
368
+ @ Test // gh-33315
369
+ void varargsWithListConvertedToVarargsArray () {
370
+ ((StandardTypeLocator ) context .getTypeLocator ()).registerImport ("java.util" );
371
+
372
+ // Calling 'public String aVarargsMethod(String... strings)' -> Arrays.toString(strings)
373
+ String expected = "[a, b, c]" ;
374
+ evaluate ("aVarargsMethod(T(List).of('a', 'b', 'c'))" , expected , String .class );
375
+ evaluate ("aVarargsMethod({'a', 'b', 'c'})" , expected , String .class );
376
+
377
+ // Calling 'public String formatObjectVarargs(String format, Object... args)' -> String.format(format, args)
378
+ expected = "x -> a b c" ;
379
+ evaluate ("formatObjectVarargs('x -> %s %s %s', T(List).of('a', 'b', 'c'))" , expected , String .class );
380
+ evaluate ("formatObjectVarargs('x -> %s %s %s', {'a', 'b', 'c'})" , expected , String .class );
381
+ }
382
+
365
383
@ Test
366
- void testVarargsOptionalInvocation () {
384
+ void varargsOptionalInvocation () {
367
385
// Calling 'public String optionalVarargsMethod(Optional<String>... values)'
368
386
evaluate ("optionalVarargsMethod()" , "[]" , String .class );
369
387
evaluate ("optionalVarargsMethod(new String[0])" , "[]" , String .class );
@@ -379,12 +397,12 @@ void testVarargsOptionalInvocation() {
379
397
}
380
398
381
399
@ Test
382
- void testInvocationOnNullContextObject () {
400
+ void invocationOnNullContextObject () {
383
401
evaluateAndCheckError ("null.toString()" ,SpelMessage .METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED );
384
402
}
385
403
386
404
@ Test
387
- void testMethodOfClass () {
405
+ void methodOfClass () {
388
406
Expression expression = parser .parseExpression ("getName()" );
389
407
Object value = expression .getValue (new StandardEvaluationContext (String .class ));
390
408
assertThat (value ).isEqualTo ("java.lang.String" );
0 commit comments