1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 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 ;
20
21
import java .util .LinkedHashSet ;
21
22
import java .util .List ;
22
23
import java .util .Map ;
37
38
/**
38
39
* @author Mark Fisher
39
40
* @author Sam Brannen
41
+ * @author Juergen Hoeller
40
42
*/
41
43
public class SelectionAndProjectionTests {
42
44
@@ -106,6 +108,21 @@ public void selectLastItemInSet() throws Exception {
106
108
assertEquals (4 , value );
107
109
}
108
110
111
+ @ Test
112
+ public void selectionWithIterable () throws Exception {
113
+ Expression expression = new SpelExpressionParser ().parseRaw ("integers.?[#this<5]" );
114
+ EvaluationContext context = new StandardEvaluationContext (new IterableTestBean ());
115
+ Object value = expression .getValue (context );
116
+ assertTrue (value instanceof List );
117
+ List <?> list = (List <?>) value ;
118
+ assertEquals (5 , list .size ());
119
+ assertEquals (0 , list .get (0 ));
120
+ assertEquals (1 , list .get (1 ));
121
+ assertEquals (2 , list .get (2 ));
122
+ assertEquals (3 , list .get (3 ));
123
+ assertEquals (4 , list .get (4 ));
124
+ }
125
+
109
126
@ Test
110
127
public void selectionWithArray () throws Exception {
111
128
Expression expression = new SpelExpressionParser ().parseRaw ("integers.?[#this<5]" );
@@ -242,6 +259,20 @@ public void projectionWithSet() throws Exception {
242
259
assertEquals (7 , list .get (2 ));
243
260
}
244
261
262
+ @ Test
263
+ public void projectionWithIterable () throws Exception {
264
+ Expression expression = new SpelExpressionParser ().parseRaw ("#testList.![wrapper.value]" );
265
+ EvaluationContext context = new StandardEvaluationContext ();
266
+ context .setVariable ("testList" , IntegerTestBean .createIterable ());
267
+ Object value = expression .getValue (context );
268
+ assertTrue (value instanceof List );
269
+ List <?> list = (List <?>) value ;
270
+ assertEquals (3 , list .size ());
271
+ assertEquals (5 , list .get (0 ));
272
+ assertEquals (6 , list .get (1 ));
273
+ assertEquals (7 , list .get (2 ));
274
+ }
275
+
245
276
@ Test
246
277
public void projectionWithArray () throws Exception {
247
278
Expression expression = new SpelExpressionParser ().parseRaw ("#testArray.![wrapper.value]" );
@@ -258,23 +289,6 @@ public void projectionWithArray() throws Exception {
258
289
assertEquals (new Integer (7 ), array [2 ]);
259
290
}
260
291
261
- static class MapTestBean {
262
-
263
- private final Map <String , String > colors = new TreeMap <String , String >();
264
-
265
- MapTestBean () {
266
- // colors.put("black", "schwarz");
267
- colors .put ("red" , "rot" );
268
- colors .put ("brown" , "braun" );
269
- colors .put ("blue" , "blau" );
270
- colors .put ("yellow" , "gelb" );
271
- colors .put ("beige" , "beige" );
272
- }
273
-
274
- public Map <String , String > getColors () {
275
- return colors ;
276
- }
277
- }
278
292
279
293
static class ListTestBean {
280
294
@@ -291,6 +305,7 @@ public List<Integer> getIntegers() {
291
305
}
292
306
}
293
307
308
+
294
309
static class SetTestBean {
295
310
296
311
private final Set <Integer > integers = new LinkedHashSet <Integer >();
@@ -306,6 +321,28 @@ public Set<Integer> getIntegers() {
306
321
}
307
322
}
308
323
324
+
325
+ static class IterableTestBean {
326
+
327
+ private final Set <Integer > integers = new LinkedHashSet <Integer >();
328
+
329
+ IterableTestBean () {
330
+ for (int i = 0 ; i < 10 ; i ++) {
331
+ integers .add (i );
332
+ }
333
+ }
334
+
335
+ public Iterable <Integer > getIntegers () {
336
+ return new Iterable <Integer >() {
337
+ @ Override
338
+ public Iterator <Integer > iterator () {
339
+ return integers .iterator ();
340
+ }
341
+ };
342
+ }
343
+ }
344
+
345
+
309
346
static class ArrayTestBean {
310
347
311
348
private final int [] ints = new int [10 ];
@@ -328,6 +365,26 @@ public Integer[] getIntegers() {
328
365
}
329
366
}
330
367
368
+
369
+ static class MapTestBean {
370
+
371
+ private final Map <String , String > colors = new TreeMap <String , String >();
372
+
373
+ MapTestBean () {
374
+ // colors.put("black", "schwarz");
375
+ colors .put ("red" , "rot" );
376
+ colors .put ("brown" , "braun" );
377
+ colors .put ("blue" , "blau" );
378
+ colors .put ("yellow" , "gelb" );
379
+ colors .put ("beige" , "beige" );
380
+ }
381
+
382
+ public Map <String , String > getColors () {
383
+ return colors ;
384
+ }
385
+ }
386
+
387
+
331
388
static class IntegerTestBean {
332
389
333
390
private final IntegerWrapper wrapper ;
@@ -356,6 +413,16 @@ static Set<IntegerTestBean> createSet() {
356
413
return set ;
357
414
}
358
415
416
+ static Iterable <IntegerTestBean > createIterable () {
417
+ final Set <IntegerTestBean > set = createSet ();
418
+ return new Iterable <IntegerTestBean >() {
419
+ @ Override
420
+ public Iterator <IntegerTestBean > iterator () {
421
+ return set .iterator ();
422
+ }
423
+ };
424
+ }
425
+
359
426
static IntegerTestBean [] createArray () {
360
427
IntegerTestBean [] array = new IntegerTestBean [3 ];
361
428
for (int i = 0 ; i < 3 ; i ++) {
@@ -369,6 +436,7 @@ static IntegerTestBean[] createArray() {
369
436
}
370
437
}
371
438
439
+
372
440
static class IntegerWrapper {
373
441
374
442
private final Number value ;
0 commit comments