SpEL selection/projection works with Iterable as well

Issue: SPR-13231
This commit is contained in:
Juergen Hoeller
2015-07-15 11:22:52 +02:00
parent ea2a1d33d9
commit 0783a1c667
3 changed files with 108 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.expression.spel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@@ -37,6 +38,7 @@ import static org.junit.Assert.*;
/**
* @author Mark Fisher
* @author Sam Brannen
* @author Juergen Hoeller
*/
public class SelectionAndProjectionTests {
@@ -106,6 +108,21 @@ public class SelectionAndProjectionTests {
assertEquals(4, value);
}
@Test
public void selectionWithIterable() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new IterableTestBean());
Object value = expression.getValue(context);
assertTrue(value instanceof List);
List<?> list = (List<?>) value;
assertEquals(5, list.size());
assertEquals(0, list.get(0));
assertEquals(1, list.get(1));
assertEquals(2, list.get(2));
assertEquals(3, list.get(3));
assertEquals(4, list.get(4));
}
@Test
public void selectionWithArray() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
@@ -242,6 +259,20 @@ public class SelectionAndProjectionTests {
assertEquals(7, list.get(2));
}
@Test
public void projectionWithIterable() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("testList", IntegerTestBean.createIterable());
Object value = expression.getValue(context);
assertTrue(value instanceof List);
List<?> list = (List<?>) value;
assertEquals(3, list.size());
assertEquals(5, list.get(0));
assertEquals(6, list.get(1));
assertEquals(7, list.get(2));
}
@Test
public void projectionWithArray() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]");
@@ -258,23 +289,6 @@ public class SelectionAndProjectionTests {
assertEquals(new Integer(7), array[2]);
}
static class MapTestBean {
private final Map<String, String> colors = new TreeMap<String, String>();
MapTestBean() {
// colors.put("black", "schwarz");
colors.put("red", "rot");
colors.put("brown", "braun");
colors.put("blue", "blau");
colors.put("yellow", "gelb");
colors.put("beige", "beige");
}
public Map<String, String> getColors() {
return colors;
}
}
static class ListTestBean {
@@ -291,6 +305,7 @@ public class SelectionAndProjectionTests {
}
}
static class SetTestBean {
private final Set<Integer> integers = new LinkedHashSet<Integer>();
@@ -306,6 +321,28 @@ public class SelectionAndProjectionTests {
}
}
static class IterableTestBean {
private final Set<Integer> integers = new LinkedHashSet<Integer>();
IterableTestBean() {
for (int i = 0; i < 10; i++) {
integers.add(i);
}
}
public Iterable<Integer> getIntegers() {
return new Iterable<Integer>() {
@Override
public Iterator<Integer> iterator() {
return integers.iterator();
}
};
}
}
static class ArrayTestBean {
private final int[] ints = new int[10];
@@ -328,6 +365,26 @@ public class SelectionAndProjectionTests {
}
}
static class MapTestBean {
private final Map<String, String> colors = new TreeMap<String, String>();
MapTestBean() {
// colors.put("black", "schwarz");
colors.put("red", "rot");
colors.put("brown", "braun");
colors.put("blue", "blau");
colors.put("yellow", "gelb");
colors.put("beige", "beige");
}
public Map<String, String> getColors() {
return colors;
}
}
static class IntegerTestBean {
private final IntegerWrapper wrapper;
@@ -356,6 +413,16 @@ public class SelectionAndProjectionTests {
return set;
}
static Iterable<IntegerTestBean> createIterable() {
final Set<IntegerTestBean> set = createSet();
return new Iterable<IntegerTestBean>() {
@Override
public Iterator<IntegerTestBean> iterator() {
return set.iterator();
}
};
}
static IntegerTestBean[] createArray() {
IntegerTestBean[] array = new IntegerTestBean[3];
for (int i = 0; i < 3; i++) {
@@ -369,6 +436,7 @@ public class SelectionAndProjectionTests {
}
}
static class IntegerWrapper {
private final Number value;