Skip to content

Commit 6b67972

Browse files
committed
Polishing
1 parent 64fc9ee commit 6b67972

File tree

1 file changed

+71
-98
lines changed

1 file changed

+71
-98
lines changed

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

Lines changed: 71 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@
1818

1919
import java.awt.Color;
2020
import java.util.Arrays;
21-
import java.util.HashMap;
2221
import java.util.List;
2322
import java.util.Map;
2423

2524
import org.junit.jupiter.api.Test;
2625

2726
import org.springframework.expression.EvaluationContext;
28-
import org.springframework.expression.EvaluationException;
2927
import org.springframework.expression.Expression;
30-
import org.springframework.expression.ParseException;
3128
import org.springframework.expression.PropertyAccessor;
3229
import org.springframework.expression.TypedValue;
3330
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -36,10 +33,8 @@
3633
import static org.assertj.core.api.Assertions.assertThat;
3734
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3835

39-
///CLOVER:OFF
40-
4136
/**
42-
* Testcases showing the common scenarios/use-cases for picking up the expression language support.
37+
* Test cases showing the common scenarios/use-cases for picking up the expression language support.
4338
* The first test shows very basic usage, just drop it in and go. By 'standard infrastructure', it means:<br>
4439
* <ul>
4540
* <li>The context classloader is used (so, the default classpath)
@@ -66,22 +61,17 @@ class ExpressionLanguageScenarioTests extends AbstractExpressionTests {
6661
*/
6762
@Test
6863
void testScenario_UsingStandardInfrastructure() {
69-
try {
70-
// Create a parser
71-
SpelExpressionParser parser = new SpelExpressionParser();
72-
// Parse an expression
73-
Expression expr = parser.parseRaw("new String('hello world')");
74-
// Evaluate it using a 'standard' context
75-
Object value = expr.getValue();
76-
// They are reusable
77-
value = expr.getValue();
78-
79-
assertThat(value).isEqualTo("hello world");
80-
assertThat(value.getClass()).isEqualTo(String.class);
81-
}
82-
catch (EvaluationException | ParseException ex) {
83-
throw new AssertionError(ex.getMessage(), ex);
84-
}
64+
// Create a parser
65+
SpelExpressionParser parser = new SpelExpressionParser();
66+
// Parse an expression
67+
Expression expr = parser.parseRaw("new String('hello world')");
68+
// Evaluate it using a 'standard' context
69+
Object value = expr.getValue();
70+
// They are reusable
71+
value = expr.getValue();
72+
73+
assertThat(value).isEqualTo("hello world");
74+
assertThat(value.getClass()).isEqualTo(String.class);
8575
}
8676

8777
/**
@@ -95,7 +85,7 @@ void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() {
9585
StandardEvaluationContext ctx = new StandardEvaluationContext();
9686
ctx.setVariable("favouriteColour","blue");
9787
List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17);
98-
ctx.setVariable("primes",primes);
88+
ctx.setVariable("primes", primes);
9989

10090
Expression expr = parser.parseRaw("#favouriteColour");
10191
Object value = expr.getValue(ctx);
@@ -111,14 +101,6 @@ void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() {
111101
assertThat(value.toString()).isEqualTo("[11, 13, 17]");
112102
}
113103

114-
115-
static class TestClass {
116-
public String str;
117-
private int property;
118-
public int getProperty() { return property; }
119-
public void setProperty(int i) { property = i; }
120-
}
121-
122104
/**
123105
* Scenario: using your own root context object
124106
*/
@@ -169,61 +151,50 @@ void testScenario_UsingADifferentRootContextObject() {
169151
* Scenario: using your own java methods and calling them from the expression
170152
*/
171153
@Test
172-
void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem() throws SecurityException, NoSuchMethodException {
173-
try {
174-
// Create a parser
175-
SpelExpressionParser parser = new SpelExpressionParser();
176-
// Use the standard evaluation context
177-
StandardEvaluationContext ctx = new StandardEvaluationContext();
178-
ctx.registerFunction("repeat",ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat",String.class));
179-
180-
Expression expr = parser.parseRaw("#repeat('hello')");
181-
Object value = expr.getValue(ctx);
182-
assertThat(value).isEqualTo("hellohello");
154+
void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem() throws Exception {
155+
// Create a parser
156+
SpelExpressionParser parser = new SpelExpressionParser();
157+
// Use the standard evaluation context
158+
StandardEvaluationContext ctx = new StandardEvaluationContext();
159+
ctx.registerFunction("repeat", ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat", String.class));
183160

184-
}
185-
catch (EvaluationException | ParseException ex) {
186-
throw new AssertionError(ex.getMessage(), ex);
187-
}
161+
Expression expr = parser.parseRaw("#repeat('hello')");
162+
Object value = expr.getValue(ctx);
163+
assertThat(value).isEqualTo("hellohello");
188164
}
189165

190166
/**
191167
* Scenario: looking up your own MethodHandles and calling them from the expression
192168
*/
193169
@Test
194-
void testScenario_RegisteringJavaMethodsAsMethodHandlesAndCallingThem() throws SecurityException {
195-
try {
196-
// Create a parser
197-
SpelExpressionParser parser = new SpelExpressionParser();
198-
//this.context is already populated with all relevant MethodHandle examples
199-
200-
Expression expr = parser.parseRaw("#message('Message with %s words: <%s>', 2, 'Hello World', 'ignored')");
201-
Object value = expr.getValue(this.context);
202-
assertThat(value).isEqualTo("Message with 2 words: <Hello World>");
203-
204-
expr = parser.parseRaw("#messageTemplate('bound', 2, 'Hello World', 'ignored')");
205-
value = expr.getValue(this.context);
206-
assertThat(value).isEqualTo("This is a bound message with 2 words: <Hello World>");
207-
208-
expr = parser.parseRaw("#messageBound()");
209-
value = expr.getValue(this.context);
210-
assertThat(value).isEqualTo("This is a prerecorded message with 3 words: <Oh Hello World>");
211-
212-
Expression staticExpr = parser.parseRaw("#messageStatic('Message with %s words: <%s>', 2, 'Hello World', 'ignored')");
213-
Object staticValue = staticExpr.getValue(this.context);
214-
assertThat(staticValue).isEqualTo("Message with 2 words: <Hello World>");
215-
216-
staticExpr = parser.parseRaw("#messageStaticTemplate('bound', 2, 'Hello World', 'ignored')");
217-
staticValue = staticExpr.getValue(this.context);
218-
assertThat(staticValue).isEqualTo("This is a bound message with 2 words: <Hello World>");
219-
220-
staticExpr = parser.parseRaw("#messageStaticBound()");
221-
staticValue = staticExpr.getValue(this.context);
222-
assertThat(staticValue).isEqualTo("This is a prerecorded message with 3 words: <Oh Hello World>");
223-
}
224-
catch (EvaluationException | ParseException ex) {
225-
throw new AssertionError(ex.getMessage(), ex);
226-
}
170+
void testScenario_RegisteringJavaMethodsAsMethodHandlesAndCallingThem() throws Exception {
171+
// Create a parser
172+
SpelExpressionParser parser = new SpelExpressionParser();
173+
//this.context is already populated with all relevant MethodHandle examples
174+
175+
Expression expr = parser.parseRaw("#message('Message with %s words: <%s>', 2, 'Hello World', 'ignored')");
176+
Object value = expr.getValue(this.context);
177+
assertThat(value).isEqualTo("Message with 2 words: <Hello World>");
178+
179+
expr = parser.parseRaw("#messageTemplate('bound', 2, 'Hello World', 'ignored')");
180+
value = expr.getValue(this.context);
181+
assertThat(value).isEqualTo("This is a bound message with 2 words: <Hello World>");
182+
183+
expr = parser.parseRaw("#messageBound()");
184+
value = expr.getValue(this.context);
185+
assertThat(value).isEqualTo("This is a prerecorded message with 3 words: <Oh Hello World>");
186+
187+
Expression staticExpr = parser.parseRaw("#messageStatic('Message with %s words: <%s>', 2, 'Hello World', 'ignored')");
188+
Object staticValue = staticExpr.getValue(this.context);
189+
assertThat(staticValue).isEqualTo("Message with 2 words: <Hello World>");
190+
191+
staticExpr = parser.parseRaw("#messageStaticTemplate('bound', 2, 'Hello World', 'ignored')");
192+
staticValue = staticExpr.getValue(this.context);
193+
assertThat(staticValue).isEqualTo("This is a bound message with 2 words: <Hello World>");
194+
195+
staticExpr = parser.parseRaw("#messageStaticBound()");
196+
staticValue = staticExpr.getValue(this.context);
197+
assertThat(staticValue).isEqualTo("This is a prerecorded message with 3 words: <Oh Hello World>");
227198
}
228199

229200
/**
@@ -240,8 +211,8 @@ void testScenario_AddingYourOwnPropertyResolvers_1() {
240211
Expression expr = parser.parseRaw("orange");
241212
Object value = expr.getValue(ctx);
242213
assertThat(value).isEqualTo(Color.orange);
243-
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
244-
expr.setValue(ctx, Color.blue))
214+
assertThatExceptionOfType(SpelEvaluationException.class)
215+
.isThrownBy(() -> expr.setValue(ctx, Color.blue))
245216
.satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL));
246217
}
247218

@@ -257,25 +228,31 @@ void testScenario_AddingYourOwnPropertyResolvers_2() {
257228
Object value = expr.getValue(ctx);
258229
assertThat(value).isEqualTo(Color.green);
259230

260-
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
261-
expr.setValue(ctx, Color.blue))
231+
assertThatExceptionOfType(SpelEvaluationException.class)
232+
.isThrownBy(() -> expr.setValue(ctx, Color.blue))
262233
.satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL));
263234
}
264235

265236

237+
static class TestClass {
238+
public String str;
239+
private int property;
240+
public int getProperty() { return property; }
241+
public void setProperty(int i) { property = i; }
242+
}
243+
244+
266245
/**
267246
* Regardless of the current context object, or root context object, this resolver can tell you what colour a fruit is !
268247
* It only supports property reading, not writing. To support writing it would need to override canWrite() and write()
269248
*/
270249
private static class FruitColourAccessor implements PropertyAccessor {
271250

272-
private static Map<String,Color> propertyMap = new HashMap<>();
273-
274-
static {
275-
propertyMap.put("banana",Color.yellow);
276-
propertyMap.put("apple",Color.red);
277-
propertyMap.put("orange",Color.orange);
278-
}
251+
private static final Map<String,Color> propertyMap = Map.of(
252+
"banana", Color.yellow,
253+
"apple", Color.red,
254+
"orange", Color.orange
255+
);
279256

280257
/**
281258
* Null means you might be able to read any property, if an earlier property resolver hasn't beaten you to it
@@ -303,7 +280,6 @@ public boolean canWrite(EvaluationContext context, Object target, String name) {
303280
@Override
304281
public void write(EvaluationContext context, Object target, String name, Object newValue) {
305282
}
306-
307283
}
308284

309285

@@ -313,12 +289,10 @@ public void write(EvaluationContext context, Object target, String name, Object
313289
*/
314290
private static class VegetableColourAccessor implements PropertyAccessor {
315291

316-
private static Map<String,Color> propertyMap = new HashMap<>();
317-
318-
static {
319-
propertyMap.put("carrot",Color.orange);
320-
propertyMap.put("pea",Color.green);
321-
}
292+
private static Map<String,Color> propertyMap = Map.of(
293+
"carrot", Color.orange,
294+
"pea", Color.green
295+
);
322296

323297
/**
324298
* Null means you might be able to read any property, if an earlier property resolver hasn't beaten you to it
@@ -346,7 +320,6 @@ public boolean canWrite(EvaluationContext context, Object target, String name) {
346320
@Override
347321
public void write(EvaluationContext context, Object target, String name, Object newValue) {
348322
}
349-
350323
}
351324

352325
}

0 commit comments

Comments
 (0)