2222import java .util .Set ;
2323
2424import org .jspecify .annotations .Nullable ;
25+ import org .junit .jupiter .api .BeforeAll ;
2526import org .junit .jupiter .api .Test ;
2627
2728import org .springframework .core .MethodParameter ;
2829import org .springframework .core .convert .TypeDescriptor ;
2930import org .springframework .expression .Expression ;
3031import org .springframework .expression .TypeConverter ;
3132import org .springframework .expression .spel .support .StandardEvaluationContext ;
32- import org .springframework .expression .spel .support .StandardTypeConverter ;
3333import org .springframework .util .ReflectionUtils ;
3434
3535import static org .assertj .core .api .Assertions .assertThat ;
3636
3737/**
38- * Expression evaluation where the TypeConverter plugged in uses the
38+ * Expression evaluation where the {@link TypeConverter} plugged in uses the
3939 * {@link org.springframework.core.convert.support.GenericConversionService}.
4040 *
4141 * @author Andy Clement
@@ -49,31 +49,35 @@ class ExpressionWithConversionTests extends AbstractExpressionTests {
4949
5050 private static final TypeDescriptor typeDescriptorForListOfString =
5151 new TypeDescriptor (ReflectionUtils .findField (ExpressionWithConversionTests .class , "listOfString" ));
52- private static TypeDescriptor typeDescriptorForListOfInteger =
52+ private static final TypeDescriptor typeDescriptorForListOfInteger =
5353 new TypeDescriptor (ReflectionUtils .findField (ExpressionWithConversionTests .class , "listOfInteger" ));
5454
5555
5656 /**
5757 * Test the service can convert what we are about to use in the expression evaluation tests.
5858 */
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 ();
6264
6365 // 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" );
6871
6972 // 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 );
7578 }
7679
80+
7781 @ Test
7882 void setParameterizedList () {
7983 StandardEvaluationContext context = TestScenarioCreator .getTestEvaluationContext ();
@@ -82,10 +86,11 @@ void setParameterizedList() {
8286 assertThat (e .getValue (context , Integer .class )).isZero ();
8387
8488 // 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 );
8690 // size now 3
8791 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 );
8994 assertThat (clazz ).isEqualTo (Integer .class );
9095 }
9196
@@ -95,11 +100,7 @@ void coercionToCollectionOfPrimitive() throws Exception {
95100 class TestTarget {
96101 @ SuppressWarnings ("unused" )
97102 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 );
103104 }
104105 }
105106
@@ -117,9 +118,8 @@ public int sum(Collection<Integer> numbers) {
117118 evaluationContext .setVariable ("target" , new TestTarget ());
118119
119120 // 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 );
123123 }
124124
125125 @ Test
0 commit comments