1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .expression .spel ;
18
18
19
- import static org .junit .Assert .assertEquals ;
20
- import static org .junit .Assert .assertNotNull ;
21
- import static org .junit .Assert .assertTrue ;
22
-
23
19
import java .util .ArrayList ;
24
20
import java .util .Collection ;
25
21
import java .util .Collections ;
26
22
import java .util .List ;
27
23
28
24
import org .junit .Before ;
29
25
import org .junit .Test ;
26
+
30
27
import org .springframework .core .MethodParameter ;
31
28
import org .springframework .core .convert .ConversionService ;
32
29
import org .springframework .core .convert .TypeDescriptor ;
36
33
import org .springframework .expression .TypeConverter ;
37
34
import org .springframework .expression .spel .support .StandardEvaluationContext ;
38
35
36
+ import static org .junit .Assert .*;
37
+
39
38
/**
40
39
* Expression evaluation where the TypeConverter plugged in is the
41
40
* {@link org.springframework.core.convert.support.GenericConversionService}.
42
41
*
43
42
* @author Andy Clement
44
43
* @author Dave Syer
45
44
*/
46
- public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCase {
45
+ public class ExpressionWithConversionTests extends ExpressionTestCase {
47
46
48
47
private static List <String > listOfString = new ArrayList <String >();
49
48
private static TypeDescriptor typeDescriptorForListOfString = null ;
@@ -61,8 +60,8 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
61
60
62
61
@ Before
63
62
public void setUp () throws Exception {
64
- ExpressionTestsUsingCoreConversionService .typeDescriptorForListOfString = new TypeDescriptor (ExpressionTestsUsingCoreConversionService .class .getDeclaredField ("listOfString" ));
65
- ExpressionTestsUsingCoreConversionService .typeDescriptorForListOfInteger = new TypeDescriptor (ExpressionTestsUsingCoreConversionService .class .getDeclaredField ("listOfInteger" ));
63
+ ExpressionWithConversionTests .typeDescriptorForListOfString = new TypeDescriptor (ExpressionWithConversionTests .class .getDeclaredField ("listOfString" ));
64
+ ExpressionWithConversionTests .typeDescriptorForListOfInteger = new TypeDescriptor (ExpressionWithConversionTests .class .getDeclaredField ("listOfInteger" ));
66
65
}
67
66
68
67
@@ -133,45 +132,37 @@ public int sum(Collection<Integer> numbers) {
133
132
134
133
}
135
134
136
- public static class Foo {
137
-
138
- private Collection <Foo > foos ;
139
-
140
- public final String value ;
141
-
142
- public Foo (String value ) {
143
- this .value = value ;
144
- }
145
-
146
- public void setFoos (Collection <Foo > foos ) {
147
- this .foos = foos ;
148
- }
149
-
150
- public Collection <Foo > getFoos () {
151
- return this .foos ;
152
- }
153
-
154
- }
155
-
156
135
@ Test
157
136
public void testConvert () {
158
137
Foo root = new Foo ("bar" );
159
- StandardEvaluationContext context = new StandardEvaluationContext (root );
160
-
161
138
Collection <String > foos = Collections .singletonList ("baz" );
162
139
163
- // property access, works
140
+ StandardEvaluationContext context = new StandardEvaluationContext (root );
141
+
142
+ // property access
164
143
Expression expression = parser .parseExpression ("foos" );
165
144
expression .setValue (context , foos );
166
145
Foo baz = root .getFoos ().iterator ().next ();
167
146
assertEquals ("baz" , baz .value );
168
147
169
- // method call, fails (ClassCastException)
148
+ // method call
170
149
expression = parser .parseExpression ("setFoos(#foos)" );
171
150
context .setVariable ("foos" , foos );
172
151
expression .getValue (context );
173
152
baz = root .getFoos ().iterator ().next ();
174
153
assertEquals ("baz" , baz .value );
154
+
155
+ // method call with result from method call
156
+ expression = parser .parseExpression ("setFoos(getFoosAsStrings())" );
157
+ expression .getValue (context );
158
+ baz = root .getFoos ().iterator ().next ();
159
+ assertEquals ("baz" , baz .value );
160
+
161
+ // method call with result from method call
162
+ expression = parser .parseExpression ("setFoos(getFoosAsObjects())" );
163
+ expression .getValue (context );
164
+ baz = root .getFoos ().iterator ().next ();
165
+ assertEquals ("baz" , baz .value );
175
166
}
176
167
177
168
@@ -193,4 +184,32 @@ public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescript
193
184
}
194
185
}
195
186
187
+
188
+ public static class Foo {
189
+
190
+ public final String value ;
191
+
192
+ private Collection <Foo > foos ;
193
+
194
+ public Foo (String value ) {
195
+ this .value = value ;
196
+ }
197
+
198
+ public void setFoos (Collection <Foo > foos ) {
199
+ this .foos = foos ;
200
+ }
201
+
202
+ public Collection <Foo > getFoos () {
203
+ return this .foos ;
204
+ }
205
+
206
+ public Collection <String > getFoosAsStrings () {
207
+ return Collections .singletonList ("baz" );
208
+ }
209
+
210
+ public Collection <?> getFoosAsObjects () {
211
+ return Collections .singletonList ("baz" );
212
+ }
213
+ }
214
+
196
215
}
0 commit comments