Skip to content

Commit 8afff33

Browse files
committed
Polishing
1 parent c57c227 commit 8afff33

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

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

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.expression.spel.standard.SpelExpression;
3333
import org.springframework.expression.spel.standard.SpelExpressionParser;
3434
import org.springframework.expression.spel.support.StandardEvaluationContext;
35+
import org.springframework.expression.spel.testresources.Inventor;
3536
import org.springframework.expression.spel.testresources.PlaceOfBirth;
3637

3738
import static org.assertj.core.api.Assertions.assertThat;
@@ -48,45 +49,46 @@
4849
class MethodInvocationTests extends AbstractExpressionTests {
4950

5051
@Test
51-
void testSimpleAccess01() {
52+
void simpleAccess() {
5253
evaluate("getPlaceOfBirth().getCity()", "Smiljan", String.class);
5354
}
5455

5556
@Test
56-
void testStringClass() {
57+
void stringClass() {
5758
evaluate("new java.lang.String('hello').charAt(2)", 'l', Character.class);
5859
evaluate("new java.lang.String('hello').charAt(2).equals('l'.charAt(0))", true, Boolean.class);
5960
evaluate("'HELLO'.toLowerCase()", "hello", String.class);
6061
evaluate("' abcba '.trim()", "abcba", String.class);
6162
}
6263

6364
@Test
64-
void testNonExistentMethods() {
65+
void nonExistentMethods() {
6566
// name is ok but madeup() does not exist
6667
evaluateAndCheckError("name.madeup()", SpelMessage.METHOD_NOT_FOUND, 5);
6768
}
6869

6970
@Test
70-
void testWidening01() {
71+
void widening() {
7172
// widening of int 3 to double 3 is OK
7273
evaluate("new Double(3.0d).compareTo(8)", -1, Integer.class);
7374
evaluate("new Double(3.0d).compareTo(3)", 0, Integer.class);
7475
evaluate("new Double(3.0d).compareTo(2)", 1, Integer.class);
7576
}
7677

7778
@Test
78-
void testArgumentConversion01() {
79+
void argumentConversion() {
7980
// Rely on Double>String conversion for calling startsWith()
8081
evaluate("new String('hello 2.0 to you').startsWith(7.0d)", false, Boolean.class);
8182
evaluate("new String('7.0 foobar').startsWith(7.0d)", true, Boolean.class);
8283
}
8384

84-
@Test
85-
void testMethodThrowingException_SPR6760() {
85+
@Test // SPR-6760
86+
void methodThrowingException() {
8687
// Test method on inventor: throwException()
87-
// On 1 it will throw an IllegalArgumentException
88-
// On 2 it will throw a RuntimeException
89-
// On 3 it will exit normally
88+
// On 1 it will throw an IllegalArgumentException.
89+
// On 2 it will throw a RuntimeException.
90+
// On 4 it will throw a TestException.
91+
// Otherwise, it will exit normally.
9092
// In each case it increments the Inventor field 'counter' when invoked
9193

9294
SpelExpressionParser parser = new SpelExpressionParser();
@@ -115,7 +117,6 @@ void testMethodThrowingException_SPR6760() {
115117
assertThat(o).isEqualTo(3);
116118
assertThat(parser.parseExpression("counter").getValue(eContext)).isEqualTo(2);
117119

118-
119120
// Now cause it to throw an exception:
120121
eContext.setVariable("bar", 1);
121122
assertThatException()
@@ -135,12 +136,13 @@ void testMethodThrowingException_SPR6760() {
135136
/**
136137
* Check on first usage (when the cachedExecutor in MethodReference is null) that the exception is not wrapped.
137138
*/
138-
@Test
139-
void testMethodThrowingException_SPR6941() {
139+
@Test // SPR-6941
140+
void methodThrowingRuntimeException() {
140141
// Test method on inventor: throwException()
141-
// On 1 it will throw an IllegalArgumentException
142-
// On 2 it will throw a RuntimeException
143-
// On 3 it will exit normally
142+
// On 1 it will throw an IllegalArgumentException.
143+
// On 2 it will throw a RuntimeException.
144+
// On 4 it will throw a TestException.
145+
// Otherwise, it will exit normally.
144146
// In each case it increments the Inventor field 'counter' when invoked
145147

146148
SpelExpressionParser parser = new SpelExpressionParser();
@@ -152,12 +154,13 @@ void testMethodThrowingException_SPR6941() {
152154
.isNotInstanceOf(SpelEvaluationException.class);
153155
}
154156

155-
@Test
156-
void testMethodThrowingException_SPR6941_2() {
157+
@Test // SPR-6941
158+
void methodThrowingCustomException() {
157159
// Test method on inventor: throwException()
158-
// On 1 it will throw an IllegalArgumentException
159-
// On 2 it will throw a RuntimeException
160-
// On 3 it will exit normally
160+
// On 1 it will throw an IllegalArgumentException.
161+
// On 2 it will throw a RuntimeException.
162+
// On 4 it will throw a TestException.
163+
// Otherwise, it will exit normally.
161164
// In each case it increments the Inventor field 'counter' when invoked
162165

163166
SpelExpressionParser parser = new SpelExpressionParser();
@@ -166,12 +169,11 @@ void testMethodThrowingException_SPR6941_2() {
166169
context.setVariable("bar", 4);
167170
assertThatExceptionOfType(ExpressionInvocationTargetException.class)
168171
.isThrownBy(() -> expr.getValue(context))
169-
.satisfies(ex -> assertThat(ex.getCause().getClass().getName()).isEqualTo(
170-
"org.springframework.expression.spel.testresources.Inventor$TestException"));
172+
.withCauseExactlyInstanceOf(Inventor.TestException.class);
171173
}
172174

173-
@Test
174-
void testMethodFiltering_SPR6764() {
175+
@Test // SPR-6764
176+
void methodFiltering() {
175177
SpelExpressionParser parser = new SpelExpressionParser();
176178
StandardEvaluationContext context = new StandardEvaluationContext();
177179
context.setRootObject(new TestObject());
@@ -211,7 +213,7 @@ void testMethodFiltering_SPR6764() {
211213
}
212214

213215
@Test
214-
void testAddingMethodResolvers() {
216+
void addingMethodResolvers() {
215217
StandardEvaluationContext ctx = new StandardEvaluationContext();
216218

217219
// reflective method accessor is the only one by default
@@ -235,7 +237,7 @@ void testAddingMethodResolvers() {
235237
}
236238

237239
@Test
238-
void testVarargsInvocation01() {
240+
void varargsInvocation01() {
239241
// Calling 'public String aVarargsMethod(String... strings)'
240242
evaluate("aVarargsMethod('a','b','c')", "[a, b, c]", String.class);
241243
evaluate("aVarargsMethod('a')", "[a]", String.class);
@@ -252,7 +254,7 @@ void testVarargsInvocation01() {
252254
}
253255

254256
@Test
255-
void testVarargsInvocation02() {
257+
void varargsInvocation02() {
256258
// Calling 'public String aVarargsMethod2(int i, String... strings)'
257259
evaluate("aVarargsMethod2(5,'a','b','c')", "5-[a, b, c]", String.class);
258260
evaluate("aVarargsMethod2(2,'a')", "2-[a]", String.class);
@@ -267,7 +269,7 @@ void testVarargsInvocation02() {
267269
}
268270

269271
@Test
270-
void testVarargsInvocation03() {
272+
void varargsInvocation03() {
271273
// Calling 'public int aVarargsMethod3(String str1, String... strings)' - returns all strings concatenated with "-"
272274

273275
// No conversion necessary
@@ -295,7 +297,7 @@ void testVarargsInvocation03() {
295297
}
296298

297299
@Test // gh-33013
298-
void testVarargsWithObjectArrayType() {
300+
void varargsWithObjectArrayType() {
299301
// Calling 'public String formatObjectVarargs(String format, Object... args)' -> String.format(format, args)
300302

301303
// No var-args and no conversion necessary
@@ -336,7 +338,7 @@ void testVarargsWithObjectArrayType() {
336338
}
337339

338340
@Test
339-
void testVarargsWithPrimitiveArrayType() {
341+
void varargsWithPrimitiveArrayType() {
340342
// Calling 'public String formatPrimitiveVarargs(String format, int... nums)' -> effectively String.format(format, args)
341343

342344
// No var-args and no conversion necessary
@@ -357,13 +359,13 @@ void testVarargsWithPrimitiveArrayType() {
357359
}
358360

359361
@Test
360-
void testVarargsWithPrimitiveArrayToObjectArrayConversion() {
362+
void varargsWithPrimitiveArrayToObjectArrayConversion() {
361363
evaluate("formatObjectVarargs('x -> %s %s %s', new short[]{1, 2, 3})", "x -> 1 2 3", String.class); // short[] to Object[]
362364
evaluate("formatObjectVarargs('x -> %s %s %s', new int[]{1, 2, 3})", "x -> 1 2 3", String.class); // int[] to Object[]
363365
}
364366

365367
@Test
366-
void testVarargsOptionalInvocation() {
368+
void varargsOptionalInvocation() {
367369
// Calling 'public String optionalVarargsMethod(Optional<String>... values)'
368370
evaluate("optionalVarargsMethod()", "[]", String.class);
369371
evaluate("optionalVarargsMethod(new String[0])", "[]", String.class);
@@ -379,12 +381,12 @@ void testVarargsOptionalInvocation() {
379381
}
380382

381383
@Test
382-
void testInvocationOnNullContextObject() {
384+
void invocationOnNullContextObject() {
383385
evaluateAndCheckError("null.toString()",SpelMessage.METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED);
384386
}
385387

386388
@Test
387-
void testMethodOfClass() {
389+
void methodOfClass() {
388390
Expression expression = parser.parseExpression("getName()");
389391
Object value = expression.getValue(new StandardEvaluationContext(String.class));
390392
assertThat(value).isEqualTo("java.lang.String");

spring-expression/src/test/java/org/springframework/expression/spel/testresources/Inventor.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,16 @@ public PlaceOfBirth getPlaceOfBirth() {
119119

120120
public int throwException(int valueIn) throws Exception {
121121
counter++;
122-
if (valueIn==1) {
123-
throw new IllegalArgumentException("IllegalArgumentException for 1");
124-
}
125-
if (valueIn==2) {
126-
throw new RuntimeException("RuntimeException for 2");
127-
}
128-
if (valueIn==4) {
129-
throw new TestException();
122+
switch (valueIn) {
123+
case 1 -> throw new IllegalArgumentException("IllegalArgumentException for 1");
124+
case 2 -> throw new RuntimeException("RuntimeException for 2");
125+
case 4 -> throw new TestException();
130126
}
131127
return valueIn;
132128
}
133129

134130
@SuppressWarnings("serial")
135-
static class TestException extends Exception {}
131+
public static class TestException extends Exception {}
136132

137133
public String throwException(PlaceOfBirth pob) {
138134
return pob.getCity();

0 commit comments

Comments
 (0)