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