Skip to content

Commit b8c2780

Browse files
committed
Simplify SpEL ExpressionWithConversionTests
1 parent ac7c7ff commit b8c2780

File tree

1 file changed

+32
-65
lines changed

1 file changed

+32
-65
lines changed

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

+32-65
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -16,82 +16,68 @@
1616

1717
package org.springframework.expression.spel;
1818

19-
import java.util.ArrayList;
2019
import java.util.Collection;
21-
import java.util.Collections;
2220
import java.util.List;
21+
import java.util.Set;
2322

24-
import org.junit.jupiter.api.BeforeEach;
2523
import org.junit.jupiter.api.Test;
2624

2725
import org.springframework.core.MethodParameter;
28-
import org.springframework.core.convert.ConversionService;
2926
import org.springframework.core.convert.TypeDescriptor;
30-
import org.springframework.core.convert.support.DefaultConversionService;
31-
import org.springframework.expression.EvaluationException;
3227
import org.springframework.expression.Expression;
3328
import org.springframework.expression.TypeConverter;
3429
import org.springframework.expression.spel.support.StandardEvaluationContext;
30+
import org.springframework.expression.spel.support.StandardTypeConverter;
31+
import org.springframework.util.ReflectionUtils;
3532

3633
import static org.assertj.core.api.Assertions.assertThat;
3734

3835
/**
39-
* Expression evaluation where the TypeConverter plugged in is the
36+
* Expression evaluation where the TypeConverter plugged in uses the
4037
* {@link org.springframework.core.convert.support.GenericConversionService}.
4138
*
4239
* @author Andy Clement
4340
* @author Dave Syer
4441
*/
4542
class ExpressionWithConversionTests extends AbstractExpressionTests {
4643

47-
private static List<String> listOfString = new ArrayList<>();
48-
private static TypeDescriptor typeDescriptorForListOfString = null;
49-
private static List<Integer> listOfInteger = new ArrayList<>();
50-
private static TypeDescriptor typeDescriptorForListOfInteger = null;
51-
52-
static {
53-
listOfString.add("1");
54-
listOfString.add("2");
55-
listOfString.add("3");
56-
listOfInteger.add(4);
57-
listOfInteger.add(5);
58-
listOfInteger.add(6);
59-
}
44+
private static final List<String> listOfString = List.of("1", "2", "3");
45+
private static final List<Integer> listOfInteger = List.of(4, 5, 6);
6046

61-
@BeforeEach
62-
void setUp() throws Exception {
63-
ExpressionWithConversionTests.typeDescriptorForListOfString = new TypeDescriptor(ExpressionWithConversionTests.class.getDeclaredField("listOfString"));
64-
ExpressionWithConversionTests.typeDescriptorForListOfInteger = new TypeDescriptor(ExpressionWithConversionTests.class.getDeclaredField("listOfInteger"));
65-
}
47+
private static final TypeDescriptor typeDescriptorForListOfString =
48+
new TypeDescriptor(ReflectionUtils.findField(ExpressionWithConversionTests.class, "listOfString"));
49+
private static TypeDescriptor typeDescriptorForListOfInteger =
50+
new TypeDescriptor(ReflectionUtils.findField(ExpressionWithConversionTests.class, "listOfInteger"));
6651

6752

6853
/**
6954
* Test the service can convert what we are about to use in the expression evaluation tests.
7055
*/
7156
@Test
72-
void testConversionsAvailable() {
73-
TypeConvertorUsingConversionService tcs = new TypeConvertorUsingConversionService();
57+
void conversionsAreSupportedByStandardTypeConverter() {
58+
StandardTypeConverter typeConverter = new StandardTypeConverter();
7459

75-
// ArrayList containing List<Integer> to List<String>
60+
// List<Integer> to List<String>
7661
Class<?> clazz = typeDescriptorForListOfString.getElementTypeDescriptor().getType();
7762
assertThat(clazz).isEqualTo(String.class);
78-
List<?> l = (List<?>) tcs.convertValue(listOfInteger, TypeDescriptor.forObject(listOfInteger), typeDescriptorForListOfString);
63+
List<?> l = (List<?>) typeConverter.convertValue(listOfInteger, TypeDescriptor.forObject(listOfInteger), typeDescriptorForListOfString);
7964
assertThat(l).isNotNull();
8065

81-
// ArrayList containing List<String> to List<Integer>
66+
// List<String> to List<Integer>
8267
clazz = typeDescriptorForListOfInteger.getElementTypeDescriptor().getType();
8368
assertThat(clazz).isEqualTo(Integer.class);
8469

85-
l = (List<?>) tcs.convertValue(listOfString, TypeDescriptor.forObject(listOfString), typeDescriptorForListOfString);
70+
l = (List<?>) typeConverter.convertValue(listOfString, TypeDescriptor.forObject(listOfString), typeDescriptorForListOfString);
8671
assertThat(l).isNotNull();
8772
}
8873

8974
@Test
90-
void testSetParameterizedList() {
75+
void setParameterizedList() {
9176
StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
77+
9278
Expression e = parser.parseExpression("listOfInteger.size()");
9379
assertThat(e.getValue(context, Integer.class)).isZero();
94-
context.setTypeConverter(new TypeConvertorUsingConversionService());
80+
9581
// Assign a List<String> to the List<Integer> field - the component elements should be converted
9682
parser.parseExpression("listOfInteger").setValue(context,listOfString);
9783
// size now 3
@@ -101,7 +87,7 @@ void testSetParameterizedList() {
10187
}
10288

10389
@Test
104-
void testCoercionToCollectionOfPrimitive() throws Exception {
90+
void coercionToCollectionOfPrimitive() throws Exception {
10591

10692
class TestTarget {
10793
@SuppressWarnings("unused")
@@ -115,28 +101,28 @@ public int sum(Collection<Integer> numbers) {
115101
}
116102

117103
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
104+
TypeConverter typeConverter = evaluationContext.getTypeConverter();
118105

119106
TypeDescriptor collectionType = new TypeDescriptor(new MethodParameter(TestTarget.class.getDeclaredMethod(
120107
"sum", Collection.class), 0));
121108
// The type conversion is possible
122-
assertThat(evaluationContext.getTypeConverter()
123-
.canConvert(TypeDescriptor.valueOf(String.class), collectionType)).isTrue();
109+
assertThat(typeConverter.canConvert(TypeDescriptor.valueOf(String.class), collectionType)).isTrue();
124110
// ... and it can be done successfully
125-
assertThat(evaluationContext.getTypeConverter().convertValue("1,2,3,4", TypeDescriptor.valueOf(String.class), collectionType).toString()).isEqualTo("[1, 2, 3, 4]");
111+
assertThat(typeConverter.convertValue("1,2,3,4", TypeDescriptor.valueOf(String.class), collectionType))
112+
.hasToString("[1, 2, 3, 4]");
126113

127114
evaluationContext.setVariable("target", new TestTarget());
128115

129116
// OK up to here, so the evaluation should be fine...
130117
// ... but this fails
131-
int result = (Integer) parser.parseExpression("#target.sum(#root)").getValue(evaluationContext, "1,2,3,4");
132-
assertThat(result).as("Wrong result: " + result).isEqualTo(10);
133-
118+
int result = parser.parseExpression("#target.sum(#root)").getValue(evaluationContext, "1,2,3,4", int.class);
119+
assertThat(result).isEqualTo(10);
134120
}
135121

136122
@Test
137-
void testConvert() {
123+
void convert() {
138124
Foo root = new Foo("bar");
139-
Collection<String> foos = Collections.singletonList("baz");
125+
Collection<String> foos = Set.of("baz");
140126

141127
StandardEvaluationContext context = new StandardEvaluationContext(root);
142128

@@ -163,26 +149,7 @@ void testConvert() {
163149
expression = parser.parseExpression("setFoos(getFoosAsObjects())");
164150
expression.getValue(context);
165151
baz = root.getFoos().iterator().next();
166-
assertThat(baz.value).isEqualTo("baz");
167-
}
168-
169-
170-
/**
171-
* Type converter that uses the core conversion service.
172-
*/
173-
private static class TypeConvertorUsingConversionService implements TypeConverter {
174-
175-
private final ConversionService service = new DefaultConversionService();
176-
177-
@Override
178-
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
179-
return this.service.canConvert(sourceType, targetType);
180-
}
181-
182-
@Override
183-
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) throws EvaluationException {
184-
return this.service.convert(value, sourceType, targetType);
185-
}
152+
assertThat(baz.value).isEqualTo("quux");
186153
}
187154

188155

@@ -205,11 +172,11 @@ public Collection<Foo> getFoos() {
205172
}
206173

207174
public Collection<String> getFoosAsStrings() {
208-
return Collections.singletonList("baz");
175+
return Set.of("baz");
209176
}
210177

211178
public Collection<?> getFoosAsObjects() {
212-
return Collections.singletonList("baz");
179+
return Set.of("quux");
213180
}
214181
}
215182

0 commit comments

Comments
 (0)