Skip to content

Commit 7c3a184

Browse files
committed
Polish SpEL SelectionAndProjectionTests
1 parent 01e50fb commit 7c3a184

File tree

1 file changed

+60
-119
lines changed

1 file changed

+60
-119
lines changed

spring-expression/src/test/java/org/springframework/expression/spel/SelectionAndProjectionTests.java

Lines changed: 60 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.expression.spel;
1818

1919
import java.util.ArrayList;
20-
import java.util.Iterator;
2120
import java.util.LinkedHashSet;
2221
import java.util.List;
2322
import java.util.Map;
@@ -40,187 +39,151 @@
4039
* @author Sam Brannen
4140
* @author Juergen Hoeller
4241
*/
43-
public class SelectionAndProjectionTests {
42+
class SelectionAndProjectionTests {
4443

4544
@Test
46-
public void selectionWithList() throws Exception {
45+
@SuppressWarnings("unchecked")
46+
void selectionWithList() throws Exception {
4747
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
4848
EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
4949
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);
5953
}
6054

6155
@Test
62-
public void selectFirstItemInList() throws Exception {
56+
void selectFirstItemInList() throws Exception {
6357
Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
6458
EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
6559
Object value = expression.getValue(context);
66-
boolean condition = value instanceof Integer;
67-
assertThat(condition).isTrue();
60+
assertThat(value).isInstanceOf(Integer.class);
6861
assertThat(value).isEqualTo(0);
6962
}
7063

7164
@Test
72-
public void selectLastItemInList() throws Exception {
65+
void selectLastItemInList() throws Exception {
7366
Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]");
7467
EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
7568
Object value = expression.getValue(context);
76-
boolean condition = value instanceof Integer;
77-
assertThat(condition).isTrue();
69+
assertThat(value).isInstanceOf(Integer.class);
7870
assertThat(value).isEqualTo(4);
7971
}
8072

8173
@Test
82-
public void selectionWithSet() throws Exception {
74+
@SuppressWarnings("unchecked")
75+
void selectionWithSet() throws Exception {
8376
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
8477
EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
8578
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);
9582
}
9683

9784
@Test
98-
public void selectFirstItemInSet() throws Exception {
85+
void selectFirstItemInSet() throws Exception {
9986
Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
10087
EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
10188
Object value = expression.getValue(context);
102-
boolean condition = value instanceof Integer;
103-
assertThat(condition).isTrue();
89+
assertThat(value).isInstanceOf(Integer.class);
10490
assertThat(value).isEqualTo(0);
10591
}
10692

10793
@Test
108-
public void selectLastItemInSet() throws Exception {
94+
void selectLastItemInSet() throws Exception {
10995
Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]");
11096
EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
11197
Object value = expression.getValue(context);
112-
boolean condition = value instanceof Integer;
113-
assertThat(condition).isTrue();
98+
assertThat(value).isInstanceOf(Integer.class);
11499
assertThat(value).isEqualTo(4);
115100
}
116101

117102
@Test
118-
public void selectionWithIterable() throws Exception {
103+
@SuppressWarnings("unchecked")
104+
void selectionWithIterable() throws Exception {
119105
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
120106
EvaluationContext context = new StandardEvaluationContext(new IterableTestBean());
121107
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);
131111
}
132112

133113
@Test
134-
public void selectionWithArray() throws Exception {
114+
void selectionWithArray() throws Exception {
135115
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
136116
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
137117
Object value = expression.getValue(context);
138118
assertThat(value.getClass().isArray()).isTrue();
139119
TypedValue typedValue = new TypedValue(value);
140120
assertThat(typedValue.getTypeDescriptor().getElementTypeDescriptor().getType()).isEqualTo(Integer.class);
141121
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);
148123
}
149124

150125
@Test
151-
public void selectFirstItemInArray() throws Exception {
126+
void selectFirstItemInArray() throws Exception {
152127
Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
153128
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
154129
Object value = expression.getValue(context);
155-
boolean condition = value instanceof Integer;
156-
assertThat(condition).isTrue();
130+
assertThat(value).isInstanceOf(Integer.class);
157131
assertThat(value).isEqualTo(0);
158132
}
159133

160134
@Test
161-
public void selectLastItemInArray() throws Exception {
135+
void selectLastItemInArray() throws Exception {
162136
Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]");
163137
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
164138
Object value = expression.getValue(context);
165-
boolean condition = value instanceof Integer;
166-
assertThat(condition).isTrue();
139+
assertThat(value).isInstanceOf(Integer.class);
167140
assertThat(value).isEqualTo(4);
168141
}
169142

170143
@Test
171-
public void selectionWithPrimitiveArray() throws Exception {
144+
void selectionWithPrimitiveArray() throws Exception {
172145
Expression expression = new SpelExpressionParser().parseRaw("ints.?[#this<5]");
173146
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
174147
Object value = expression.getValue(context);
175148
assertThat(value.getClass().isArray()).isTrue();
176149
TypedValue typedValue = new TypedValue(value);
177150
assertThat(typedValue.getTypeDescriptor().getElementTypeDescriptor().getType()).isEqualTo(Integer.class);
178151
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);
185153
}
186154

187155
@Test
188-
public void selectFirstItemInPrimitiveArray() throws Exception {
156+
void selectFirstItemInPrimitiveArray() throws Exception {
189157
Expression expression = new SpelExpressionParser().parseRaw("ints.^[#this<5]");
190158
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
191159
Object value = expression.getValue(context);
192-
boolean condition = value instanceof Integer;
193-
assertThat(condition).isTrue();
160+
assertThat(value).isInstanceOf(Integer.class);
194161
assertThat(value).isEqualTo(0);
195162
}
196163

197164
@Test
198-
public void selectLastItemInPrimitiveArray() throws Exception {
165+
void selectLastItemInPrimitiveArray() throws Exception {
199166
Expression expression = new SpelExpressionParser().parseRaw("ints.$[#this<5]");
200167
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
201168
Object value = expression.getValue(context);
202-
boolean condition = value instanceof Integer;
203-
assertThat(condition).isTrue();
169+
assertThat(value).isInstanceOf(Integer.class);
204170
assertThat(value).isEqualTo(4);
205171
}
206172

207173
@Test
208174
@SuppressWarnings("unchecked")
209-
public void selectionWithMap() {
175+
void selectionWithMap() {
210176
EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
211177
ExpressionParser parser = new SpelExpressionParser();
212178
Expression exp = parser.parseExpression("colors.?[key.startsWith('b')]");
213179

214180
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");
219182
}
220183

221184
@Test
222185
@SuppressWarnings("unchecked")
223-
public void selectFirstItemInMap() {
186+
void selectFirstItemInMap() {
224187
EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
225188
ExpressionParser parser = new SpelExpressionParser();
226189

@@ -232,7 +195,7 @@ public void selectFirstItemInMap() {
232195

233196
@Test
234197
@SuppressWarnings("unchecked")
235-
public void selectLastItemInMap() {
198+
void selectLastItemInMap() {
236199
EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
237200
ExpressionParser parser = new SpelExpressionParser();
238201

@@ -243,52 +206,43 @@ public void selectLastItemInMap() {
243206
}
244207

245208
@Test
246-
public void projectionWithList() throws Exception {
209+
@SuppressWarnings("unchecked")
210+
void projectionWithList() throws Exception {
247211
Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
248212
EvaluationContext context = new StandardEvaluationContext();
249213
context.setVariable("testList", IntegerTestBean.createList());
250214
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);
258218
}
259219

260220
@Test
261-
public void projectionWithSet() throws Exception {
221+
@SuppressWarnings("unchecked")
222+
void projectionWithSet() throws Exception {
262223
Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
263224
EvaluationContext context = new StandardEvaluationContext();
264225
context.setVariable("testList", IntegerTestBean.createSet());
265226
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);
273230
}
274231

275232
@Test
276-
public void projectionWithIterable() throws Exception {
233+
@SuppressWarnings("unchecked")
234+
void projectionWithIterable() throws Exception {
277235
Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
278236
EvaluationContext context = new StandardEvaluationContext();
279237
context.setVariable("testList", IntegerTestBean.createIterable());
280238
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);
288242
}
289243

290244
@Test
291-
public void projectionWithArray() throws Exception {
245+
void projectionWithArray() throws Exception {
292246
Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]");
293247
EvaluationContext context = new StandardEvaluationContext();
294248
context.setVariable("testArray", IntegerTestBean.createArray());
@@ -297,10 +251,7 @@ public void projectionWithArray() throws Exception {
297251
TypedValue typedValue = new TypedValue(value);
298252
assertThat(typedValue.getTypeDescriptor().getElementTypeDescriptor().getType()).isEqualTo(Number.class);
299253
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);
304255
}
305256

306257

@@ -347,12 +298,7 @@ static class IterableTestBean {
347298
}
348299

349300
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;
356302
}
357303
}
358304

@@ -429,12 +375,7 @@ static Set<IntegerTestBean> createSet() {
429375

430376
static Iterable<IntegerTestBean> createIterable() {
431377
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;
438379
}
439380

440381
static IntegerTestBean[] createArray() {

0 commit comments

Comments
 (0)