1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .expression .spel ;
18
18
19
19
import java .util .ArrayList ;
20
- import java .util .Iterator ;
21
20
import java .util .LinkedHashSet ;
22
21
import java .util .List ;
23
22
import java .util .Map ;
40
39
* @author Sam Brannen
41
40
* @author Juergen Hoeller
42
41
*/
43
- public class SelectionAndProjectionTests {
42
+ class SelectionAndProjectionTests {
44
43
45
44
@ Test
46
- public void selectionWithList () throws Exception {
45
+ @ SuppressWarnings ("unchecked" )
46
+ void selectionWithList () throws Exception {
47
47
Expression expression = new SpelExpressionParser ().parseRaw ("integers.?[#this<5]" );
48
48
EvaluationContext context = new StandardEvaluationContext (new ListTestBean ());
49
49
Object value = expression .getValue (context );
50
- boolean condition = value instanceof List ;
51
- assertThat (condition ).isTrue ();
52
- List <?> list = (List <?>) value ;
53
- assertThat (list .size ()).isEqualTo (5 );
54
- assertThat (list .get (0 )).isEqualTo (0 );
55
- assertThat (list .get (1 )).isEqualTo (1 );
56
- assertThat (list .get (2 )).isEqualTo (2 );
57
- assertThat (list .get (3 )).isEqualTo (3 );
58
- assertThat (list .get (4 )).isEqualTo (4 );
50
+ assertThat (value ).isInstanceOf (List .class );
51
+ List <Integer > list = (List <Integer >) value ;
52
+ assertThat (list ).containsExactly (0 , 1 , 2 , 3 , 4 );
59
53
}
60
54
61
55
@ Test
62
- public void selectFirstItemInList () throws Exception {
56
+ void selectFirstItemInList () throws Exception {
63
57
Expression expression = new SpelExpressionParser ().parseRaw ("integers.^[#this<5]" );
64
58
EvaluationContext context = new StandardEvaluationContext (new ListTestBean ());
65
59
Object value = expression .getValue (context );
66
- boolean condition = value instanceof Integer ;
67
- assertThat (condition ).isTrue ();
60
+ assertThat (value ).isInstanceOf (Integer .class );
68
61
assertThat (value ).isEqualTo (0 );
69
62
}
70
63
71
64
@ Test
72
- public void selectLastItemInList () throws Exception {
65
+ void selectLastItemInList () throws Exception {
73
66
Expression expression = new SpelExpressionParser ().parseRaw ("integers.$[#this<5]" );
74
67
EvaluationContext context = new StandardEvaluationContext (new ListTestBean ());
75
68
Object value = expression .getValue (context );
76
- boolean condition = value instanceof Integer ;
77
- assertThat (condition ).isTrue ();
69
+ assertThat (value ).isInstanceOf (Integer .class );
78
70
assertThat (value ).isEqualTo (4 );
79
71
}
80
72
81
73
@ Test
82
- public void selectionWithSet () throws Exception {
74
+ @ SuppressWarnings ("unchecked" )
75
+ void selectionWithSet () throws Exception {
83
76
Expression expression = new SpelExpressionParser ().parseRaw ("integers.?[#this<5]" );
84
77
EvaluationContext context = new StandardEvaluationContext (new SetTestBean ());
85
78
Object value = expression .getValue (context );
86
- boolean condition = value instanceof List ;
87
- assertThat (condition ).isTrue ();
88
- List <?> list = (List <?>) value ;
89
- assertThat (list .size ()).isEqualTo (5 );
90
- assertThat (list .get (0 )).isEqualTo (0 );
91
- assertThat (list .get (1 )).isEqualTo (1 );
92
- assertThat (list .get (2 )).isEqualTo (2 );
93
- assertThat (list .get (3 )).isEqualTo (3 );
94
- assertThat (list .get (4 )).isEqualTo (4 );
79
+ assertThat (value ).isInstanceOf (List .class );
80
+ List <Integer > list = (List <Integer >) value ;
81
+ assertThat (list ).containsExactly (0 , 1 , 2 , 3 , 4 );
95
82
}
96
83
97
84
@ Test
98
- public void selectFirstItemInSet () throws Exception {
85
+ void selectFirstItemInSet () throws Exception {
99
86
Expression expression = new SpelExpressionParser ().parseRaw ("integers.^[#this<5]" );
100
87
EvaluationContext context = new StandardEvaluationContext (new SetTestBean ());
101
88
Object value = expression .getValue (context );
102
- boolean condition = value instanceof Integer ;
103
- assertThat (condition ).isTrue ();
89
+ assertThat (value ).isInstanceOf (Integer .class );
104
90
assertThat (value ).isEqualTo (0 );
105
91
}
106
92
107
93
@ Test
108
- public void selectLastItemInSet () throws Exception {
94
+ void selectLastItemInSet () throws Exception {
109
95
Expression expression = new SpelExpressionParser ().parseRaw ("integers.$[#this<5]" );
110
96
EvaluationContext context = new StandardEvaluationContext (new SetTestBean ());
111
97
Object value = expression .getValue (context );
112
- boolean condition = value instanceof Integer ;
113
- assertThat (condition ).isTrue ();
98
+ assertThat (value ).isInstanceOf (Integer .class );
114
99
assertThat (value ).isEqualTo (4 );
115
100
}
116
101
117
102
@ Test
118
- public void selectionWithIterable () throws Exception {
103
+ @ SuppressWarnings ("unchecked" )
104
+ void selectionWithIterable () throws Exception {
119
105
Expression expression = new SpelExpressionParser ().parseRaw ("integers.?[#this<5]" );
120
106
EvaluationContext context = new StandardEvaluationContext (new IterableTestBean ());
121
107
Object value = expression .getValue (context );
122
- boolean condition = value instanceof List ;
123
- assertThat (condition ).isTrue ();
124
- List <?> list = (List <?>) value ;
125
- assertThat (list .size ()).isEqualTo (5 );
126
- assertThat (list .get (0 )).isEqualTo (0 );
127
- assertThat (list .get (1 )).isEqualTo (1 );
128
- assertThat (list .get (2 )).isEqualTo (2 );
129
- assertThat (list .get (3 )).isEqualTo (3 );
130
- assertThat (list .get (4 )).isEqualTo (4 );
108
+ assertThat (value ).isInstanceOf (List .class );
109
+ List <Integer > list = (List <Integer >) value ;
110
+ assertThat (list ).containsExactly (0 , 1 , 2 , 3 , 4 );
131
111
}
132
112
133
113
@ Test
134
- public void selectionWithArray () throws Exception {
114
+ void selectionWithArray () throws Exception {
135
115
Expression expression = new SpelExpressionParser ().parseRaw ("integers.?[#this<5]" );
136
116
EvaluationContext context = new StandardEvaluationContext (new ArrayTestBean ());
137
117
Object value = expression .getValue (context );
138
118
assertThat (value .getClass ().isArray ()).isTrue ();
139
119
TypedValue typedValue = new TypedValue (value );
140
120
assertThat (typedValue .getTypeDescriptor ().getElementTypeDescriptor ().getType ()).isEqualTo (Integer .class );
141
121
Integer [] array = (Integer []) value ;
142
- assertThat (array .length ).isEqualTo (5 );
143
- assertThat (array [0 ]).isEqualTo (0 );
144
- assertThat (array [1 ]).isEqualTo (1 );
145
- assertThat (array [2 ]).isEqualTo (2 );
146
- assertThat (array [3 ]).isEqualTo (3 );
147
- assertThat (array [4 ]).isEqualTo (4 );
122
+ assertThat (array ).containsExactly (0 , 1 , 2 , 3 , 4 );
148
123
}
149
124
150
125
@ Test
151
- public void selectFirstItemInArray () throws Exception {
126
+ void selectFirstItemInArray () throws Exception {
152
127
Expression expression = new SpelExpressionParser ().parseRaw ("integers.^[#this<5]" );
153
128
EvaluationContext context = new StandardEvaluationContext (new ArrayTestBean ());
154
129
Object value = expression .getValue (context );
155
- boolean condition = value instanceof Integer ;
156
- assertThat (condition ).isTrue ();
130
+ assertThat (value ).isInstanceOf (Integer .class );
157
131
assertThat (value ).isEqualTo (0 );
158
132
}
159
133
160
134
@ Test
161
- public void selectLastItemInArray () throws Exception {
135
+ void selectLastItemInArray () throws Exception {
162
136
Expression expression = new SpelExpressionParser ().parseRaw ("integers.$[#this<5]" );
163
137
EvaluationContext context = new StandardEvaluationContext (new ArrayTestBean ());
164
138
Object value = expression .getValue (context );
165
- boolean condition = value instanceof Integer ;
166
- assertThat (condition ).isTrue ();
139
+ assertThat (value ).isInstanceOf (Integer .class );
167
140
assertThat (value ).isEqualTo (4 );
168
141
}
169
142
170
143
@ Test
171
- public void selectionWithPrimitiveArray () throws Exception {
144
+ void selectionWithPrimitiveArray () throws Exception {
172
145
Expression expression = new SpelExpressionParser ().parseRaw ("ints.?[#this<5]" );
173
146
EvaluationContext context = new StandardEvaluationContext (new ArrayTestBean ());
174
147
Object value = expression .getValue (context );
175
148
assertThat (value .getClass ().isArray ()).isTrue ();
176
149
TypedValue typedValue = new TypedValue (value );
177
150
assertThat (typedValue .getTypeDescriptor ().getElementTypeDescriptor ().getType ()).isEqualTo (Integer .class );
178
151
Integer [] array = (Integer []) value ;
179
- assertThat (array .length ).isEqualTo (5 );
180
- assertThat (array [0 ]).isEqualTo (0 );
181
- assertThat (array [1 ]).isEqualTo (1 );
182
- assertThat (array [2 ]).isEqualTo (2 );
183
- assertThat (array [3 ]).isEqualTo (3 );
184
- assertThat (array [4 ]).isEqualTo (4 );
152
+ assertThat (array ).containsExactly (0 , 1 , 2 , 3 , 4 );
185
153
}
186
154
187
155
@ Test
188
- public void selectFirstItemInPrimitiveArray () throws Exception {
156
+ void selectFirstItemInPrimitiveArray () throws Exception {
189
157
Expression expression = new SpelExpressionParser ().parseRaw ("ints.^[#this<5]" );
190
158
EvaluationContext context = new StandardEvaluationContext (new ArrayTestBean ());
191
159
Object value = expression .getValue (context );
192
- boolean condition = value instanceof Integer ;
193
- assertThat (condition ).isTrue ();
160
+ assertThat (value ).isInstanceOf (Integer .class );
194
161
assertThat (value ).isEqualTo (0 );
195
162
}
196
163
197
164
@ Test
198
- public void selectLastItemInPrimitiveArray () throws Exception {
165
+ void selectLastItemInPrimitiveArray () throws Exception {
199
166
Expression expression = new SpelExpressionParser ().parseRaw ("ints.$[#this<5]" );
200
167
EvaluationContext context = new StandardEvaluationContext (new ArrayTestBean ());
201
168
Object value = expression .getValue (context );
202
- boolean condition = value instanceof Integer ;
203
- assertThat (condition ).isTrue ();
169
+ assertThat (value ).isInstanceOf (Integer .class );
204
170
assertThat (value ).isEqualTo (4 );
205
171
}
206
172
207
173
@ Test
208
174
@ SuppressWarnings ("unchecked" )
209
- public void selectionWithMap () {
175
+ void selectionWithMap () {
210
176
EvaluationContext context = new StandardEvaluationContext (new MapTestBean ());
211
177
ExpressionParser parser = new SpelExpressionParser ();
212
178
Expression exp = parser .parseExpression ("colors.?[key.startsWith('b')]" );
213
179
214
180
Map <String , String > colorsMap = (Map <String , String >) exp .getValue (context );
215
- assertThat (colorsMap .size ()).isEqualTo (3 );
216
- assertThat (colorsMap .containsKey ("beige" )).isTrue ();
217
- assertThat (colorsMap .containsKey ("blue" )).isTrue ();
218
- assertThat (colorsMap .containsKey ("brown" )).isTrue ();
181
+ assertThat (colorsMap ).containsOnlyKeys ("beige" , "blue" , "brown" );
219
182
}
220
183
221
184
@ Test
222
185
@ SuppressWarnings ("unchecked" )
223
- public void selectFirstItemInMap () {
186
+ void selectFirstItemInMap () {
224
187
EvaluationContext context = new StandardEvaluationContext (new MapTestBean ());
225
188
ExpressionParser parser = new SpelExpressionParser ();
226
189
@@ -232,7 +195,7 @@ public void selectFirstItemInMap() {
232
195
233
196
@ Test
234
197
@ SuppressWarnings ("unchecked" )
235
- public void selectLastItemInMap () {
198
+ void selectLastItemInMap () {
236
199
EvaluationContext context = new StandardEvaluationContext (new MapTestBean ());
237
200
ExpressionParser parser = new SpelExpressionParser ();
238
201
@@ -243,52 +206,43 @@ public void selectLastItemInMap() {
243
206
}
244
207
245
208
@ Test
246
- public void projectionWithList () throws Exception {
209
+ @ SuppressWarnings ("unchecked" )
210
+ void projectionWithList () throws Exception {
247
211
Expression expression = new SpelExpressionParser ().parseRaw ("#testList.![wrapper.value]" );
248
212
EvaluationContext context = new StandardEvaluationContext ();
249
213
context .setVariable ("testList" , IntegerTestBean .createList ());
250
214
Object value = expression .getValue (context );
251
- boolean condition = value instanceof List ;
252
- assertThat (condition ).isTrue ();
253
- List <?> list = (List <?>) value ;
254
- assertThat (list .size ()).isEqualTo (3 );
255
- assertThat (list .get (0 )).isEqualTo (5 );
256
- assertThat (list .get (1 )).isEqualTo (6 );
257
- assertThat (list .get (2 )).isEqualTo (7 );
215
+ assertThat (value ).isInstanceOf (List .class );
216
+ List <Integer > list = (List <Integer >) value ;
217
+ assertThat (list ).containsExactly (5 , 6 , 7 );
258
218
}
259
219
260
220
@ Test
261
- public void projectionWithSet () throws Exception {
221
+ @ SuppressWarnings ("unchecked" )
222
+ void projectionWithSet () throws Exception {
262
223
Expression expression = new SpelExpressionParser ().parseRaw ("#testList.![wrapper.value]" );
263
224
EvaluationContext context = new StandardEvaluationContext ();
264
225
context .setVariable ("testList" , IntegerTestBean .createSet ());
265
226
Object value = expression .getValue (context );
266
- boolean condition = value instanceof List ;
267
- assertThat (condition ).isTrue ();
268
- List <?> list = (List <?>) value ;
269
- assertThat (list .size ()).isEqualTo (3 );
270
- assertThat (list .get (0 )).isEqualTo (5 );
271
- assertThat (list .get (1 )).isEqualTo (6 );
272
- assertThat (list .get (2 )).isEqualTo (7 );
227
+ assertThat (value ).isInstanceOf (List .class );
228
+ List <Integer > list = (List <Integer >) value ;
229
+ assertThat (list ).containsExactly (5 , 6 , 7 );
273
230
}
274
231
275
232
@ Test
276
- public void projectionWithIterable () throws Exception {
233
+ @ SuppressWarnings ("unchecked" )
234
+ void projectionWithIterable () throws Exception {
277
235
Expression expression = new SpelExpressionParser ().parseRaw ("#testList.![wrapper.value]" );
278
236
EvaluationContext context = new StandardEvaluationContext ();
279
237
context .setVariable ("testList" , IntegerTestBean .createIterable ());
280
238
Object value = expression .getValue (context );
281
- boolean condition = value instanceof List ;
282
- assertThat (condition ).isTrue ();
283
- List <?> list = (List <?>) value ;
284
- assertThat (list .size ()).isEqualTo (3 );
285
- assertThat (list .get (0 )).isEqualTo (5 );
286
- assertThat (list .get (1 )).isEqualTo (6 );
287
- assertThat (list .get (2 )).isEqualTo (7 );
239
+ assertThat (value ).isInstanceOf (List .class );
240
+ List <Integer > list = (List <Integer >) value ;
241
+ assertThat (list ).containsExactly (5 , 6 , 7 );
288
242
}
289
243
290
244
@ Test
291
- public void projectionWithArray () throws Exception {
245
+ void projectionWithArray () throws Exception {
292
246
Expression expression = new SpelExpressionParser ().parseRaw ("#testArray.![wrapper.value]" );
293
247
EvaluationContext context = new StandardEvaluationContext ();
294
248
context .setVariable ("testArray" , IntegerTestBean .createArray ());
@@ -297,10 +251,7 @@ public void projectionWithArray() throws Exception {
297
251
TypedValue typedValue = new TypedValue (value );
298
252
assertThat (typedValue .getTypeDescriptor ().getElementTypeDescriptor ().getType ()).isEqualTo (Number .class );
299
253
Number [] array = (Number []) value ;
300
- assertThat (array .length ).isEqualTo (3 );
301
- assertThat (array [0 ]).isEqualTo (5 );
302
- assertThat (array [1 ]).isEqualTo (5.9f );
303
- assertThat (array [2 ]).isEqualTo (7 );
254
+ assertThat (array ).containsExactly (5 , 5.9f , 7 );
304
255
}
305
256
306
257
@@ -347,12 +298,7 @@ static class IterableTestBean {
347
298
}
348
299
349
300
public Iterable <Integer > getIntegers () {
350
- return new Iterable <Integer >() {
351
- @ Override
352
- public Iterator <Integer > iterator () {
353
- return integers .iterator ();
354
- }
355
- };
301
+ return integers ::iterator ;
356
302
}
357
303
}
358
304
@@ -429,12 +375,7 @@ static Set<IntegerTestBean> createSet() {
429
375
430
376
static Iterable <IntegerTestBean > createIterable () {
431
377
final Set <IntegerTestBean > set = createSet ();
432
- return new Iterable <IntegerTestBean >() {
433
- @ Override
434
- public Iterator <IntegerTestBean > iterator () {
435
- return set .iterator ();
436
- }
437
- };
378
+ return set ::iterator ;
438
379
}
439
380
440
381
static IntegerTestBean [] createArray () {
0 commit comments