22
22
import java .util .Set ;
23
23
24
24
import org .jspecify .annotations .Nullable ;
25
+ import org .junit .jupiter .api .BeforeAll ;
25
26
import org .junit .jupiter .api .Test ;
26
27
27
28
import org .springframework .core .MethodParameter ;
28
29
import org .springframework .core .convert .TypeDescriptor ;
29
30
import org .springframework .expression .Expression ;
30
31
import org .springframework .expression .TypeConverter ;
31
32
import org .springframework .expression .spel .support .StandardEvaluationContext ;
32
- import org .springframework .expression .spel .support .StandardTypeConverter ;
33
33
import org .springframework .util .ReflectionUtils ;
34
34
35
35
import static org .assertj .core .api .Assertions .assertThat ;
36
36
37
37
/**
38
- * Expression evaluation where the TypeConverter plugged in uses the
38
+ * Expression evaluation where the {@link TypeConverter} plugged in uses the
39
39
* {@link org.springframework.core.convert.support.GenericConversionService}.
40
40
*
41
41
* @author Andy Clement
@@ -49,31 +49,35 @@ class ExpressionWithConversionTests extends AbstractExpressionTests {
49
49
50
50
private static final TypeDescriptor typeDescriptorForListOfString =
51
51
new TypeDescriptor (ReflectionUtils .findField (ExpressionWithConversionTests .class , "listOfString" ));
52
- private static TypeDescriptor typeDescriptorForListOfInteger =
52
+ private static final TypeDescriptor typeDescriptorForListOfInteger =
53
53
new TypeDescriptor (ReflectionUtils .findField (ExpressionWithConversionTests .class , "listOfInteger" ));
54
54
55
55
56
56
/**
57
57
* Test the service can convert what we are about to use in the expression evaluation tests.
58
58
*/
59
- @ Test
60
- void conversionsAreSupportedByStandardTypeConverter () {
61
- StandardTypeConverter typeConverter = new StandardTypeConverter ();
59
+ @ BeforeAll
60
+ @ SuppressWarnings ("unchecked" )
61
+ static void verifyConversionsAreSupportedByStandardTypeConverter () {
62
+ StandardEvaluationContext evaluationContext = new StandardEvaluationContext ();
63
+ TypeConverter typeConverter = evaluationContext .getTypeConverter ();
62
64
63
65
// List<Integer> to List<String>
64
- Class <?> clazz = typeDescriptorForListOfString .getElementTypeDescriptor ().getType ();
65
- assertThat (clazz ).isEqualTo (String .class );
66
- List <?> l = (List <?>) typeConverter .convertValue (listOfInteger , TypeDescriptor .forObject (listOfInteger ), typeDescriptorForListOfString );
67
- assertThat (l ).isNotNull ();
66
+ assertThat (typeDescriptorForListOfString .getElementTypeDescriptor ().getType ())
67
+ .isEqualTo (String .class );
68
+ List <String > strings = (List <String >) typeConverter .convertValue (listOfInteger ,
69
+ typeDescriptorForListOfInteger , typeDescriptorForListOfString );
70
+ assertThat (strings ).containsExactly ("4" , "5" , "6" );
68
71
69
72
// List<String> to List<Integer>
70
- clazz = typeDescriptorForListOfInteger .getElementTypeDescriptor ().getType ();
71
- assertThat ( clazz ) .isEqualTo (Integer .class );
72
-
73
- l = ( List <?>) typeConverter . convertValue ( listOfString , TypeDescriptor . forObject ( listOfString ), typeDescriptorForListOfString );
74
- assertThat (l ). isNotNull ( );
73
+ assertThat ( typeDescriptorForListOfInteger .getElementTypeDescriptor ().getType ())
74
+ .isEqualTo (Integer .class );
75
+ List < Integer > integers = ( List < Integer >) typeConverter . convertValue ( listOfString ,
76
+ typeDescriptorForListOfString , typeDescriptorForListOfInteger );
77
+ assertThat (integers ). containsExactly ( 1 , 2 , 3 );
75
78
}
76
79
80
+
77
81
@ Test
78
82
void setParameterizedList () {
79
83
StandardEvaluationContext context = TestScenarioCreator .getTestEvaluationContext ();
@@ -82,10 +86,11 @@ void setParameterizedList() {
82
86
assertThat (e .getValue (context , Integer .class )).isZero ();
83
87
84
88
// Assign a List<String> to the List<Integer> field - the component elements should be converted
85
- parser .parseExpression ("listOfInteger" ).setValue (context ,listOfString );
89
+ parser .parseExpression ("listOfInteger" ).setValue (context , listOfString );
86
90
// size now 3
87
91
assertThat (e .getValue (context , Integer .class )).isEqualTo (3 );
88
- Class <?> clazz = parser .parseExpression ("listOfInteger[1].getClass()" ).getValue (context , Class .class ); // element type correctly Integer
92
+ // element type correctly Integer
93
+ Class <?> clazz = parser .parseExpression ("listOfInteger[1].getClass()" ).getValue (context , Class .class );
89
94
assertThat (clazz ).isEqualTo (Integer .class );
90
95
}
91
96
@@ -95,11 +100,7 @@ void coercionToCollectionOfPrimitive() throws Exception {
95
100
class TestTarget {
96
101
@ SuppressWarnings ("unused" )
97
102
public int sum (Collection <Integer > numbers ) {
98
- int total = 0 ;
99
- for (int i : numbers ) {
100
- total += i ;
101
- }
102
- return total ;
103
+ return numbers .stream ().reduce (0 , (a , b ) -> a + b );
103
104
}
104
105
}
105
106
@@ -117,9 +118,8 @@ public int sum(Collection<Integer> numbers) {
117
118
evaluationContext .setVariable ("target" , new TestTarget ());
118
119
119
120
// OK up to here, so the evaluation should be fine...
120
- // ... but this fails
121
- int result = parser .parseExpression ("#target.sum(#root)" ).getValue (evaluationContext , "1,2,3,4" , int .class );
122
- assertThat (result ).isEqualTo (10 );
121
+ int sum = parser .parseExpression ("#target.sum(#root)" ).getValue (evaluationContext , "1,2,3,4" , int .class );
122
+ assertThat (sum ).isEqualTo (10 );
123
123
}
124
124
125
125
@ Test
0 commit comments