18
18
19
19
import java .awt .Color ;
20
20
import java .util .Arrays ;
21
- import java .util .HashMap ;
22
21
import java .util .List ;
23
22
import java .util .Map ;
24
23
25
24
import org .junit .jupiter .api .Test ;
26
25
27
26
import org .springframework .expression .EvaluationContext ;
28
- import org .springframework .expression .EvaluationException ;
29
27
import org .springframework .expression .Expression ;
30
- import org .springframework .expression .ParseException ;
31
28
import org .springframework .expression .PropertyAccessor ;
32
29
import org .springframework .expression .TypedValue ;
33
30
import org .springframework .expression .spel .standard .SpelExpressionParser ;
36
33
import static org .assertj .core .api .Assertions .assertThat ;
37
34
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
38
35
39
- ///CLOVER:OFF
40
-
41
36
/**
42
- * Testcases showing the common scenarios/use-cases for picking up the expression language support.
37
+ * Test cases showing the common scenarios/use-cases for picking up the expression language support.
43
38
* The first test shows very basic usage, just drop it in and go. By 'standard infrastructure', it means:<br>
44
39
* <ul>
45
40
* <li>The context classloader is used (so, the default classpath)
@@ -66,22 +61,17 @@ class ExpressionLanguageScenarioTests extends AbstractExpressionTests {
66
61
*/
67
62
@ Test
68
63
void testScenario_UsingStandardInfrastructure () {
69
- try {
70
- // Create a parser
71
- SpelExpressionParser parser = new SpelExpressionParser ();
72
- // Parse an expression
73
- Expression expr = parser .parseRaw ("new String('hello world')" );
74
- // Evaluate it using a 'standard' context
75
- Object value = expr .getValue ();
76
- // They are reusable
77
- value = expr .getValue ();
78
-
79
- assertThat (value ).isEqualTo ("hello world" );
80
- assertThat (value .getClass ()).isEqualTo (String .class );
81
- }
82
- catch (EvaluationException | ParseException ex ) {
83
- throw new AssertionError (ex .getMessage (), ex );
84
- }
64
+ // Create a parser
65
+ SpelExpressionParser parser = new SpelExpressionParser ();
66
+ // Parse an expression
67
+ Expression expr = parser .parseRaw ("new String('hello world')" );
68
+ // Evaluate it using a 'standard' context
69
+ Object value = expr .getValue ();
70
+ // They are reusable
71
+ value = expr .getValue ();
72
+
73
+ assertThat (value ).isEqualTo ("hello world" );
74
+ assertThat (value .getClass ()).isEqualTo (String .class );
85
75
}
86
76
87
77
/**
@@ -95,7 +85,7 @@ void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() {
95
85
StandardEvaluationContext ctx = new StandardEvaluationContext ();
96
86
ctx .setVariable ("favouriteColour" ,"blue" );
97
87
List <Integer > primes = Arrays .asList (2 , 3 , 5 , 7 , 11 , 13 , 17 );
98
- ctx .setVariable ("primes" ,primes );
88
+ ctx .setVariable ("primes" , primes );
99
89
100
90
Expression expr = parser .parseRaw ("#favouriteColour" );
101
91
Object value = expr .getValue (ctx );
@@ -111,14 +101,6 @@ void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() {
111
101
assertThat (value .toString ()).isEqualTo ("[11, 13, 17]" );
112
102
}
113
103
114
-
115
- static class TestClass {
116
- public String str ;
117
- private int property ;
118
- public int getProperty () { return property ; }
119
- public void setProperty (int i ) { property = i ; }
120
- }
121
-
122
104
/**
123
105
* Scenario: using your own root context object
124
106
*/
@@ -169,61 +151,50 @@ void testScenario_UsingADifferentRootContextObject() {
169
151
* Scenario: using your own java methods and calling them from the expression
170
152
*/
171
153
@ Test
172
- void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem () throws SecurityException , NoSuchMethodException {
173
- try {
174
- // Create a parser
175
- SpelExpressionParser parser = new SpelExpressionParser ();
176
- // Use the standard evaluation context
177
- StandardEvaluationContext ctx = new StandardEvaluationContext ();
178
- ctx .registerFunction ("repeat" ,ExpressionLanguageScenarioTests .class .getDeclaredMethod ("repeat" ,String .class ));
179
-
180
- Expression expr = parser .parseRaw ("#repeat('hello')" );
181
- Object value = expr .getValue (ctx );
182
- assertThat (value ).isEqualTo ("hellohello" );
154
+ void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem () throws Exception {
155
+ // Create a parser
156
+ SpelExpressionParser parser = new SpelExpressionParser ();
157
+ // Use the standard evaluation context
158
+ StandardEvaluationContext ctx = new StandardEvaluationContext ();
159
+ ctx .registerFunction ("repeat" , ExpressionLanguageScenarioTests .class .getDeclaredMethod ("repeat" , String .class ));
183
160
184
- }
185
- catch (EvaluationException | ParseException ex ) {
186
- throw new AssertionError (ex .getMessage (), ex );
187
- }
161
+ Expression expr = parser .parseRaw ("#repeat('hello')" );
162
+ Object value = expr .getValue (ctx );
163
+ assertThat (value ).isEqualTo ("hellohello" );
188
164
}
189
165
190
166
/**
191
167
* Scenario: looking up your own MethodHandles and calling them from the expression
192
168
*/
193
169
@ Test
194
- void testScenario_RegisteringJavaMethodsAsMethodHandlesAndCallingThem () throws SecurityException {
195
- try {
196
- // Create a parser
197
- SpelExpressionParser parser = new SpelExpressionParser ();
198
- //this.context is already populated with all relevant MethodHandle examples
199
-
200
- Expression expr = parser .parseRaw ("#message('Message with %s words: <%s>', 2, 'Hello World', 'ignored')" );
201
- Object value = expr .getValue (this .context );
202
- assertThat (value ).isEqualTo ("Message with 2 words: <Hello World>" );
203
-
204
- expr = parser .parseRaw ("#messageTemplate('bound', 2, 'Hello World', 'ignored')" );
205
- value = expr .getValue (this .context );
206
- assertThat (value ).isEqualTo ("This is a bound message with 2 words: <Hello World>" );
207
-
208
- expr = parser .parseRaw ("#messageBound()" );
209
- value = expr .getValue (this .context );
210
- assertThat (value ).isEqualTo ("This is a prerecorded message with 3 words: <Oh Hello World>" );
211
-
212
- Expression staticExpr = parser .parseRaw ("#messageStatic('Message with %s words: <%s>', 2, 'Hello World', 'ignored')" );
213
- Object staticValue = staticExpr .getValue (this .context );
214
- assertThat (staticValue ).isEqualTo ("Message with 2 words: <Hello World>" );
215
-
216
- staticExpr = parser .parseRaw ("#messageStaticTemplate('bound', 2, 'Hello World', 'ignored')" );
217
- staticValue = staticExpr .getValue (this .context );
218
- assertThat (staticValue ).isEqualTo ("This is a bound message with 2 words: <Hello World>" );
219
-
220
- staticExpr = parser .parseRaw ("#messageStaticBound()" );
221
- staticValue = staticExpr .getValue (this .context );
222
- assertThat (staticValue ).isEqualTo ("This is a prerecorded message with 3 words: <Oh Hello World>" );
223
- }
224
- catch (EvaluationException | ParseException ex ) {
225
- throw new AssertionError (ex .getMessage (), ex );
226
- }
170
+ void testScenario_RegisteringJavaMethodsAsMethodHandlesAndCallingThem () throws Exception {
171
+ // Create a parser
172
+ SpelExpressionParser parser = new SpelExpressionParser ();
173
+ //this.context is already populated with all relevant MethodHandle examples
174
+
175
+ Expression expr = parser .parseRaw ("#message('Message with %s words: <%s>', 2, 'Hello World', 'ignored')" );
176
+ Object value = expr .getValue (this .context );
177
+ assertThat (value ).isEqualTo ("Message with 2 words: <Hello World>" );
178
+
179
+ expr = parser .parseRaw ("#messageTemplate('bound', 2, 'Hello World', 'ignored')" );
180
+ value = expr .getValue (this .context );
181
+ assertThat (value ).isEqualTo ("This is a bound message with 2 words: <Hello World>" );
182
+
183
+ expr = parser .parseRaw ("#messageBound()" );
184
+ value = expr .getValue (this .context );
185
+ assertThat (value ).isEqualTo ("This is a prerecorded message with 3 words: <Oh Hello World>" );
186
+
187
+ Expression staticExpr = parser .parseRaw ("#messageStatic('Message with %s words: <%s>', 2, 'Hello World', 'ignored')" );
188
+ Object staticValue = staticExpr .getValue (this .context );
189
+ assertThat (staticValue ).isEqualTo ("Message with 2 words: <Hello World>" );
190
+
191
+ staticExpr = parser .parseRaw ("#messageStaticTemplate('bound', 2, 'Hello World', 'ignored')" );
192
+ staticValue = staticExpr .getValue (this .context );
193
+ assertThat (staticValue ).isEqualTo ("This is a bound message with 2 words: <Hello World>" );
194
+
195
+ staticExpr = parser .parseRaw ("#messageStaticBound()" );
196
+ staticValue = staticExpr .getValue (this .context );
197
+ assertThat (staticValue ).isEqualTo ("This is a prerecorded message with 3 words: <Oh Hello World>" );
227
198
}
228
199
229
200
/**
@@ -240,8 +211,8 @@ void testScenario_AddingYourOwnPropertyResolvers_1() {
240
211
Expression expr = parser .parseRaw ("orange" );
241
212
Object value = expr .getValue (ctx );
242
213
assertThat (value ).isEqualTo (Color .orange );
243
- assertThatExceptionOfType (SpelEvaluationException .class ). isThrownBy (() ->
244
- expr .setValue (ctx , Color .blue ))
214
+ assertThatExceptionOfType (SpelEvaluationException .class )
215
+ . isThrownBy (() -> expr .setValue (ctx , Color .blue ))
245
216
.satisfies (ex -> assertThat (ex .getMessageCode ()).isEqualTo (SpelMessage .PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL ));
246
217
}
247
218
@@ -257,25 +228,31 @@ void testScenario_AddingYourOwnPropertyResolvers_2() {
257
228
Object value = expr .getValue (ctx );
258
229
assertThat (value ).isEqualTo (Color .green );
259
230
260
- assertThatExceptionOfType (SpelEvaluationException .class ). isThrownBy (() ->
261
- expr .setValue (ctx , Color .blue ))
231
+ assertThatExceptionOfType (SpelEvaluationException .class )
232
+ . isThrownBy (() -> expr .setValue (ctx , Color .blue ))
262
233
.satisfies (ex -> assertThat (ex .getMessageCode ()).isEqualTo (SpelMessage .PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL ));
263
234
}
264
235
265
236
237
+ static class TestClass {
238
+ public String str ;
239
+ private int property ;
240
+ public int getProperty () { return property ; }
241
+ public void setProperty (int i ) { property = i ; }
242
+ }
243
+
244
+
266
245
/**
267
246
* Regardless of the current context object, or root context object, this resolver can tell you what colour a fruit is !
268
247
* It only supports property reading, not writing. To support writing it would need to override canWrite() and write()
269
248
*/
270
249
private static class FruitColourAccessor implements PropertyAccessor {
271
250
272
- private static Map <String ,Color > propertyMap = new HashMap <>();
273
-
274
- static {
275
- propertyMap .put ("banana" ,Color .yellow );
276
- propertyMap .put ("apple" ,Color .red );
277
- propertyMap .put ("orange" ,Color .orange );
278
- }
251
+ private static final Map <String ,Color > propertyMap = Map .of (
252
+ "banana" , Color .yellow ,
253
+ "apple" , Color .red ,
254
+ "orange" , Color .orange
255
+ );
279
256
280
257
/**
281
258
* Null means you might be able to read any property, if an earlier property resolver hasn't beaten you to it
@@ -303,7 +280,6 @@ public boolean canWrite(EvaluationContext context, Object target, String name) {
303
280
@ Override
304
281
public void write (EvaluationContext context , Object target , String name , Object newValue ) {
305
282
}
306
-
307
283
}
308
284
309
285
@@ -313,12 +289,10 @@ public void write(EvaluationContext context, Object target, String name, Object
313
289
*/
314
290
private static class VegetableColourAccessor implements PropertyAccessor {
315
291
316
- private static Map <String ,Color > propertyMap = new HashMap <>();
317
-
318
- static {
319
- propertyMap .put ("carrot" ,Color .orange );
320
- propertyMap .put ("pea" ,Color .green );
321
- }
292
+ private static Map <String ,Color > propertyMap = Map .of (
293
+ "carrot" , Color .orange ,
294
+ "pea" , Color .green
295
+ );
322
296
323
297
/**
324
298
* Null means you might be able to read any property, if an earlier property resolver hasn't beaten you to it
@@ -346,7 +320,6 @@ public boolean canWrite(EvaluationContext context, Object target, String name) {
346
320
@ Override
347
321
public void write (EvaluationContext context , Object target , String name , Object newValue ) {
348
322
}
349
-
350
323
}
351
324
352
325
}
0 commit comments