1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 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 java .util .HashMap ;
20
19
import java .util .Map ;
21
20
22
21
import org .junit .jupiter .api .Test ;
34
33
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
35
34
36
35
/**
37
- * Tests for the expression state object - some features are not yet exploited in the language (eg nested scopes)
36
+ * Tests for {@link ExpressionState}.
37
+ *
38
+ * <p>Some features are not yet exploited in the language, such as nested scopes
39
+ * or local variables scoped to the currently evaluated expression.
40
+ *
41
+ * <p>Local variables are in variable scopes which come and go during evaluation.
42
+ * Normal/global variables are accessible through the {@link EvaluationContext}.
38
43
*
39
44
* @author Andy Clement
40
45
* @author Juergen Hoeller
41
46
*/
42
- public class ExpressionStateTests extends AbstractExpressionTests {
47
+ class ExpressionStateTests extends AbstractExpressionTests {
48
+
49
+ private ExpressionState state = new ExpressionState (TestScenarioCreator .getTestEvaluationContext ());
50
+
43
51
44
52
@ Test
45
- public void testConstruction () {
53
+ void construction () {
46
54
EvaluationContext context = TestScenarioCreator .getTestEvaluationContext ();
47
55
ExpressionState state = new ExpressionState (context );
48
56
assertThat (state .getEvaluationContext ()).isEqualTo (context );
49
57
}
50
58
51
- // Local variables are in variable scopes which come and go during evaluation. Normal variables are
52
- // accessible through the evaluation context
53
-
54
59
@ Test
55
- public void testLocalVariables () {
56
- ExpressionState state = getState ();
57
-
60
+ void localVariables () {
58
61
Object value = state .lookupLocalVariable ("foo" );
59
62
assertThat (value ).isNull ();
60
63
@@ -68,8 +71,7 @@ public void testLocalVariables() {
68
71
}
69
72
70
73
@ Test
71
- public void testVariables () {
72
- ExpressionState state = getState ();
74
+ void globalVariables () {
73
75
TypedValue typedValue = state .lookupVariable ("foo" );
74
76
assertThat (typedValue ).isEqualTo (TypedValue .NULL );
75
77
@@ -85,8 +87,7 @@ public void testVariables() {
85
87
}
86
88
87
89
@ Test
88
- public void testNoVariableInterference () {
89
- ExpressionState state = getState ();
90
+ void noVariableInterference () {
90
91
TypedValue typedValue = state .lookupVariable ("foo" );
91
92
assertThat (typedValue ).isEqualTo (TypedValue .NULL );
92
93
@@ -99,8 +100,7 @@ public void testNoVariableInterference() {
99
100
}
100
101
101
102
@ Test
102
- public void testLocalVariableNestedScopes () {
103
- ExpressionState state = getState ();
103
+ void localVariableNestedScopes () {
104
104
assertThat (state .lookupLocalVariable ("foo" )).isNull ();
105
105
106
106
state .setLocalVariable ("foo" ,12 );
@@ -120,30 +120,26 @@ public void testLocalVariableNestedScopes() {
120
120
}
121
121
122
122
@ Test
123
- public void testRootContextObject () {
124
- ExpressionState state = getState ();
123
+ void rootContextObject () {
125
124
assertThat (state .getRootContextObject ().getValue ().getClass ()).isEqualTo (Inventor .class );
126
125
127
- // although the root object is being set on the evaluation context, the value in the 'state' remains what it was when constructed
126
+ // Although the root object is being set on the evaluation context,
127
+ // the value in the 'state' remains what it was when constructed.
128
128
((StandardEvaluationContext ) state .getEvaluationContext ()).setRootObject (null );
129
- assertThat (state .getRootContextObject ().getValue ().getClass ()).isEqualTo (Inventor .class );
130
- // assertEquals(null, state.getRootContextObject().getValue());
129
+ assertThat (state .getRootContextObject ().getValue ()).isInstanceOf (Inventor .class );
131
130
132
131
state = new ExpressionState (new StandardEvaluationContext ());
133
132
assertThat (state .getRootContextObject ()).isEqualTo (TypedValue .NULL );
134
133
135
-
136
134
((StandardEvaluationContext ) state .getEvaluationContext ()).setRootObject (null );
137
135
assertThat (state .getRootContextObject ().getValue ()).isNull ();
138
136
}
139
137
140
138
@ Test
141
- public void testActiveContextObject () {
142
- ExpressionState state = getState ();
139
+ void activeContextObject () {
143
140
assertThat (state .getActiveContextObject ().getValue ()).isEqualTo (state .getRootContextObject ().getValue ());
144
141
145
- assertThatIllegalStateException ().isThrownBy (
146
- state ::popActiveContextObject );
142
+ assertThatIllegalStateException ().isThrownBy (state ::popActiveContextObject );
147
143
148
144
state .pushActiveContextObject (new TypedValue (34 ));
149
145
assertThat (state .getActiveContextObject ().getValue ()).isEqualTo (34 );
@@ -162,8 +158,7 @@ public void testActiveContextObject() {
162
158
}
163
159
164
160
@ Test
165
- public void testPopulatedNestedScopes () {
166
- ExpressionState state = getState ();
161
+ void populatedNestedScopes () {
167
162
assertThat (state .lookupLocalVariable ("foo" )).isNull ();
168
163
169
164
state .enterScope ("foo" ,34 );
@@ -181,27 +176,22 @@ public void testPopulatedNestedScopes() {
181
176
}
182
177
183
178
@ Test
184
- public void testRootObjectConstructor () {
185
- EvaluationContext ctx = getContext ();
179
+ void rootObjectConstructor () {
180
+ EvaluationContext ctx = TestScenarioCreator . getTestEvaluationContext ();
186
181
// TypedValue root = ctx.getRootObject();
187
182
// supplied should override root on context
188
- ExpressionState state = new ExpressionState (ctx ,new TypedValue ("i am a string" ));
183
+ ExpressionState state = new ExpressionState (ctx , new TypedValue ("i am a string" ));
189
184
TypedValue stateRoot = state .getRootContextObject ();
190
185
assertThat (stateRoot .getTypeDescriptor ().getType ()).isEqualTo (String .class );
191
186
assertThat (stateRoot .getValue ()).isEqualTo ("i am a string" );
192
187
}
193
188
194
189
@ Test
195
- public void testPopulatedNestedScopesMap () {
196
- ExpressionState state = getState ();
190
+ void populatedNestedScopesMap () {
197
191
assertThat (state .lookupLocalVariable ("foo" )).isNull ();
198
192
assertThat (state .lookupLocalVariable ("goo" )).isNull ();
199
193
200
- Map <String ,Object > m = new HashMap <>();
201
- m .put ("foo" , 34 );
202
- m .put ("goo" , "abc" );
203
-
204
- state .enterScope (m );
194
+ state .enterScope (Map .of ("foo" , 34 , "goo" , "abc" ));
205
195
assertThat (state .lookupLocalVariable ("foo" )).isEqualTo (34 );
206
196
assertThat (state .lookupLocalVariable ("goo" )).isEqualTo ("abc" );
207
197
@@ -217,61 +207,42 @@ public void testPopulatedNestedScopesMap() {
217
207
}
218
208
219
209
@ Test
220
- public void testOperators () {
221
- ExpressionState state = getState ();
222
- assertThatExceptionOfType (SpelEvaluationException .class ).isThrownBy (() ->
223
- state .operate (Operation .ADD ,1 ,2 ))
210
+ void operators () {
211
+ assertThatExceptionOfType (SpelEvaluationException .class )
212
+ .isThrownBy (() -> state .operate (Operation .ADD ,1 ,2 ))
224
213
.satisfies (ex -> assertThat (ex .getMessageCode ()).isEqualTo (SpelMessage .OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES ));
225
214
226
- assertThatExceptionOfType (SpelEvaluationException .class ). isThrownBy (() ->
227
- state .operate (Operation .ADD ,null ,null ))
215
+ assertThatExceptionOfType (SpelEvaluationException .class )
216
+ . isThrownBy (() -> state .operate (Operation .ADD ,null ,null ))
228
217
.satisfies (ex -> assertThat (ex .getMessageCode ()).isEqualTo (SpelMessage .OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES ));
229
218
}
230
219
231
220
@ Test
232
- public void testComparator () {
233
- ExpressionState state = getState ();
221
+ void comparator () {
234
222
assertThat (state .getTypeComparator ()).isEqualTo (state .getEvaluationContext ().getTypeComparator ());
235
223
}
236
224
237
225
@ Test
238
- public void testTypeLocator () throws EvaluationException {
239
- ExpressionState state = getState ();
226
+ void typeLocator () throws EvaluationException {
240
227
assertThat (state .getEvaluationContext ().getTypeLocator ()).isNotNull ();
241
228
assertThat (state .findType ("java.lang.Integer" )).isEqualTo (Integer .class );
242
- assertThatExceptionOfType (SpelEvaluationException .class ). isThrownBy (() ->
243
- state .findType ("someMadeUpName" ))
229
+ assertThatExceptionOfType (SpelEvaluationException .class )
230
+ . isThrownBy (() -> state .findType ("someMadeUpName" ))
244
231
.satisfies (ex -> assertThat (ex .getMessageCode ()).isEqualTo (SpelMessage .TYPE_NOT_FOUND ));
245
-
246
232
}
247
233
248
234
@ Test
249
- public void testTypeConversion () throws EvaluationException {
250
- ExpressionState state = getState ();
235
+ void typeConversion () throws EvaluationException {
251
236
String s = (String ) state .convertValue (34 , TypeDescriptor .valueOf (String .class ));
252
237
assertThat (s ).isEqualTo ("34" );
253
238
254
- s = (String )state .convertValue (new TypedValue (34 ), TypeDescriptor .valueOf (String .class ));
239
+ s = (String ) state .convertValue (new TypedValue (34 ), TypeDescriptor .valueOf (String .class ));
255
240
assertThat (s ).isEqualTo ("34" );
256
241
}
257
242
258
243
@ Test
259
- public void testPropertyAccessors () {
260
- ExpressionState state = getState ();
244
+ void propertyAccessors () {
261
245
assertThat (state .getPropertyAccessors ()).isEqualTo (state .getEvaluationContext ().getPropertyAccessors ());
262
246
}
263
247
264
- /**
265
- * @return a new ExpressionState
266
- */
267
- private ExpressionState getState () {
268
- EvaluationContext context = TestScenarioCreator .getTestEvaluationContext ();
269
- ExpressionState state = new ExpressionState (context );
270
- return state ;
271
- }
272
-
273
- private EvaluationContext getContext () {
274
- return TestScenarioCreator .getTestEvaluationContext ();
275
- }
276
-
277
248
}
0 commit comments