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.
21
21
import java .lang .reflect .Method ;
22
22
import java .lang .reflect .Proxy ;
23
23
import java .util .ArrayList ;
24
+ import java .util .Arrays ;
24
25
import java .util .List ;
25
26
26
27
import org .junit .jupiter .api .Test ;
44
45
* Tests for reflection helper code.
45
46
*
46
47
* @author Andy Clement
48
+ * @author Sam Brannen
47
49
*/
48
- public class ReflectionHelperTests extends AbstractExpressionTests {
50
+ class ReflectionHelperTests extends AbstractExpressionTests {
49
51
50
52
@ Test
51
- public void testUtilities () throws ParseException {
53
+ void utilities () throws ParseException {
52
54
SpelExpression expr = (SpelExpression )parser .parseExpression ("3+4+5+6+7-2" );
53
55
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
54
56
PrintStream ps = new PrintStream (baos );
@@ -74,12 +76,12 @@ public void testUtilities() throws ParseException {
74
76
// CompoundExpression value:2
75
77
// IntLiteral value:2
76
78
// ===> Expression '3+4+5+6+7-2' - AST end
77
- assertThat (s .contains ("===> Expression '3+4+5+6+7-2' - AST start" )). isTrue ( );
78
- assertThat (s .contains (" OpPlus value:((((3 + 4) + 5) + 6) + 7) #children:2" )). isTrue ( );
79
+ assertThat (s ) .contains ("===> Expression '3+4+5+6+7-2' - AST start" );
80
+ assertThat (s ) .contains (" OpPlus value:((((3 + 4) + 5) + 6) + 7) #children:2" );
79
81
}
80
82
81
83
@ Test
82
- public void testTypedValue () {
84
+ void typedValue () {
83
85
TypedValue tv1 = new TypedValue ("hello" );
84
86
TypedValue tv2 = new TypedValue ("hello" );
85
87
TypedValue tv3 = new TypedValue ("bye" );
@@ -91,13 +93,13 @@ public void testTypedValue() {
91
93
assertThat (tv3 ).isNotEqualTo (tv2 );
92
94
assertThat (tv1 ).isNotEqualTo (tv3 );
93
95
assertThat (tv2 ).isNotEqualTo (tv3 );
94
- assertThat (tv2 . hashCode ()). isEqualTo (tv1 . hashCode () );
95
- assertThat (tv3 . hashCode ()). isNotEqualTo (tv1 . hashCode () );
96
- assertThat (tv3 . hashCode ()). isNotEqualTo (tv2 . hashCode () );
96
+ assertThat (tv2 ). hasSameHashCodeAs (tv1 );
97
+ assertThat (tv3 ). doesNotHaveSameHashCodeAs (tv1 );
98
+ assertThat (tv3 ). doesNotHaveSameHashCodeAs (tv2 );
97
99
}
98
100
99
101
@ Test
100
- public void testReflectionHelperCompareArguments_ExactMatching () {
102
+ void reflectionHelperCompareArguments_ExactMatching () {
101
103
StandardTypeConverter tc = new StandardTypeConverter ();
102
104
103
105
// Calling foo(String) with (String) is exact match
@@ -108,7 +110,7 @@ public void testReflectionHelperCompareArguments_ExactMatching() {
108
110
}
109
111
110
112
@ Test
111
- public void testReflectionHelperCompareArguments_CloseMatching () {
113
+ void reflectionHelperCompareArguments_CloseMatching () {
112
114
StandardTypeConverter tc = new StandardTypeConverter ();
113
115
114
116
// Calling foo(List) with (ArrayList) is close match (no conversion required)
@@ -122,7 +124,7 @@ public void testReflectionHelperCompareArguments_CloseMatching() {
122
124
}
123
125
124
126
@ Test
125
- public void testReflectionHelperCompareArguments_RequiresConversionMatching () {
127
+ void reflectionHelperCompareArguments_RequiresConversionMatching () {
126
128
StandardTypeConverter tc = new StandardTypeConverter ();
127
129
128
130
// Calling foo(String,int) with (String,Integer) requires boxing conversion of argument one
@@ -139,15 +141,15 @@ public void testReflectionHelperCompareArguments_RequiresConversionMatching() {
139
141
}
140
142
141
143
@ Test
142
- public void testReflectionHelperCompareArguments_NotAMatch () {
144
+ void reflectionHelperCompareArguments_NotAMatch () {
143
145
StandardTypeConverter typeConverter = new StandardTypeConverter ();
144
146
145
147
// Passing (Super,String) on call to foo(Sub,String) is not a match
146
148
checkMatch (new Class <?>[] {Super .class ,String .class }, new Class <?>[] {Sub .class ,String .class }, typeConverter , null );
147
149
}
148
150
149
151
@ Test
150
- public void testReflectionHelperCompareArguments_Varargs_ExactMatching () {
152
+ void reflectionHelperCompareArguments_Varargs_ExactMatching () {
151
153
StandardTypeConverter tc = new StandardTypeConverter ();
152
154
153
155
// Passing (String[]) on call to (String[]) is exact match
@@ -198,7 +200,7 @@ public void testReflectionHelperCompareArguments_Varargs_ExactMatching() {
198
200
}
199
201
200
202
@ Test
201
- public void testConvertArguments () throws Exception {
203
+ void convertArguments () throws Exception {
202
204
StandardTypeConverter tc = new StandardTypeConverter ();
203
205
Method oneArg = TestInterface .class .getMethod ("oneArg" , String .class );
204
206
Method twoArg = TestInterface .class .getMethod ("twoArg" , String .class , String [].class );
@@ -225,7 +227,7 @@ public void testConvertArguments() throws Exception {
225
227
}
226
228
227
229
@ Test
228
- public void testConvertArguments2 () throws Exception {
230
+ void convertArguments2 () throws Exception {
229
231
StandardTypeConverter tc = new StandardTypeConverter ();
230
232
Method oneArg = TestInterface .class .getMethod ("oneArg" , String .class );
231
233
Method twoArg = TestInterface .class .getMethod ("twoArg" , String .class , String [].class );
@@ -252,22 +254,19 @@ public void testConvertArguments2() throws Exception {
252
254
}
253
255
254
256
@ Test
255
- public void testSetupArguments () {
257
+ void setupArguments () {
256
258
Object [] newArray = ReflectionHelper .setupArgumentsForVarargsInvocation (
257
259
new Class <?>[] {String [].class }, "a" , "b" , "c" );
258
260
259
261
assertThat (newArray ).hasSize (1 );
260
262
Object firstParam = newArray [0 ];
261
263
assertThat (firstParam .getClass ().getComponentType ()).isEqualTo (String .class );
262
264
Object [] firstParamArray = (Object []) firstParam ;
263
- assertThat (firstParamArray ).hasSize (3 );
264
- assertThat (firstParamArray [0 ]).isEqualTo ("a" );
265
- assertThat (firstParamArray [1 ]).isEqualTo ("b" );
266
- assertThat (firstParamArray [2 ]).isEqualTo ("c" );
265
+ assertThat (firstParamArray ).containsExactly ("a" , "b" , "c" );
267
266
}
268
267
269
268
@ Test
270
- public void testReflectivePropertyAccessor () throws Exception {
269
+ void reflectivePropertyAccessor () throws Exception {
271
270
ReflectivePropertyAccessor rpa = new ReflectivePropertyAccessor ();
272
271
Tester t = new Tester ();
273
272
t .setProperty ("hello" );
@@ -328,7 +327,7 @@ public void testReflectivePropertyAccessor() throws Exception {
328
327
}
329
328
330
329
@ Test
331
- public void testOptimalReflectivePropertyAccessor () throws Exception {
330
+ void optimalReflectivePropertyAccessor () throws Exception {
332
331
ReflectivePropertyAccessor reflective = new ReflectivePropertyAccessor ();
333
332
Tester tester = new Tester ();
334
333
tester .setProperty ("hello" );
@@ -374,19 +373,28 @@ void reflectiveMethodResolverForJdkProxies() throws Exception {
374
373
MethodResolver resolver = new ReflectiveMethodResolver ();
375
374
StandardEvaluationContext evaluationContext = new StandardEvaluationContext ();
376
375
376
+ // Nonexistent method
377
377
MethodExecutor bogus = resolver .resolve (evaluationContext , proxy , "bogus" , List .of ());
378
378
assertThat (bogus ).as ("MethodExecutor for bogus()" ).isNull ();
379
+
380
+ // Method in interface
381
+ MethodExecutor run = resolver .resolve (evaluationContext , proxy , "run" , List .of ());
382
+ assertThat (run ).as ("MethodExecutor for run()" ).isNotNull ();
383
+
384
+ // Methods in Object
379
385
MethodExecutor toString = resolver .resolve (evaluationContext , proxy , "toString" , List .of ());
380
386
assertThat (toString ).as ("MethodExecutor for toString()" ).isNotNull ();
381
387
MethodExecutor hashCode = resolver .resolve (evaluationContext , proxy , "hashCode" , List .of ());
382
388
assertThat (hashCode ).as ("MethodExecutor for hashCode()" ).isNotNull ();
389
+ MethodExecutor equals = resolver .resolve (evaluationContext , proxy , "equals" , typeDescriptors (Object .class ));
390
+ assertThat (equals ).as ("MethodExecutor for equals()" ).isNotNull ();
383
391
}
384
392
385
393
/**
386
394
* Used to validate the match returned from a compareArguments call.
387
395
*/
388
396
private void checkMatch (Class <?>[] inputTypes , Class <?>[] expectedTypes , StandardTypeConverter typeConverter , ArgumentsMatchKind expectedMatchKind ) {
389
- ReflectionHelper .ArgumentsMatchInfo matchInfo = ReflectionHelper .compareArguments (getTypeDescriptors (expectedTypes ), getTypeDescriptors (inputTypes ), typeConverter );
397
+ ReflectionHelper .ArgumentsMatchInfo matchInfo = ReflectionHelper .compareArguments (typeDescriptors (expectedTypes ), typeDescriptors (inputTypes ), typeConverter );
390
398
if (expectedMatchKind == null ) {
391
399
assertThat (matchInfo ).as ("Did not expect them to match in any way" ).isNull ();
392
400
}
@@ -408,47 +416,42 @@ else if (expectedMatchKind == ArgumentsMatchKind.REQUIRES_CONVERSION) {
408
416
/**
409
417
* Used to validate the match returned from a compareArguments call.
410
418
*/
411
- private void checkMatch2 (Class <?>[] inputTypes , Class <?>[] expectedTypes , StandardTypeConverter typeConverter , ArgumentsMatchKind expectedMatchKind ) {
412
- ReflectionHelper .ArgumentsMatchInfo matchInfo = ReflectionHelper .compareArgumentsVarargs (getTypeDescriptors (expectedTypes ), getTypeDescriptors (inputTypes ), typeConverter );
419
+ private static void checkMatch2 (Class <?>[] inputTypes , Class <?>[] expectedTypes ,
420
+ StandardTypeConverter typeConverter , ArgumentsMatchKind expectedMatchKind ) {
421
+
422
+ ReflectionHelper .ArgumentsMatchInfo matchInfo =
423
+ ReflectionHelper .compareArgumentsVarargs (typeDescriptors (expectedTypes ), typeDescriptors (inputTypes ), typeConverter );
413
424
if (expectedMatchKind == null ) {
414
425
assertThat (matchInfo ).as ("Did not expect them to match in any way: " + matchInfo ).isNull ();
415
426
}
416
427
else {
417
428
assertThat (matchInfo ).as ("Should not be a null match" ).isNotNull ();
418
- }
419
-
420
- if (expectedMatchKind == ArgumentsMatchKind .EXACT ) {
421
- assertThat (matchInfo .isExactMatch ()).isTrue ();
422
- }
423
- else if (expectedMatchKind == ArgumentsMatchKind .CLOSE ) {
424
- assertThat (matchInfo .isCloseMatch ()).isTrue ();
425
- }
426
- else if (expectedMatchKind == ArgumentsMatchKind .REQUIRES_CONVERSION ) {
427
- assertThat (matchInfo .isMatchRequiringConversion ()).as ("expected to be a match requiring conversion, but was " + matchInfo ).isTrue ();
429
+ switch (expectedMatchKind ) {
430
+ case EXACT -> assertThat (matchInfo .isExactMatch ()).isTrue ();
431
+ case CLOSE -> assertThat (matchInfo .isCloseMatch ()).isTrue ();
432
+ case REQUIRES_CONVERSION -> assertThat (matchInfo .isMatchRequiringConversion ())
433
+ .as ("expected to be a match requiring conversion, but was " + matchInfo ).isTrue ();
434
+ }
428
435
}
429
436
}
430
437
431
- private void checkArguments (Object [] args , Object ... expected ) {
438
+ private static void checkArguments (Object [] args , Object ... expected ) {
432
439
assertThat (args ).hasSize (expected .length );
433
440
for (int i = 0 ; i < expected .length ; i ++) {
434
- checkArgument (expected [i ],args [i ]);
441
+ checkArgument (expected [i ], args [i ]);
435
442
}
436
443
}
437
444
438
- private void checkArgument (Object expected , Object actual ) {
445
+ private static void checkArgument (Object expected , Object actual ) {
439
446
assertThat (actual ).isEqualTo (expected );
440
447
}
441
448
442
- private List <TypeDescriptor > getTypeDescriptors (Class <?>... types ) {
443
- List <TypeDescriptor > typeDescriptors = new ArrayList <>(types .length );
444
- for (Class <?> type : types ) {
445
- typeDescriptors .add (TypeDescriptor .valueOf (type ));
446
- }
447
- return typeDescriptors ;
449
+ private static List <TypeDescriptor > typeDescriptors (Class <?>... types ) {
450
+ return Arrays .stream (types ).map (TypeDescriptor ::valueOf ).toList ();
448
451
}
449
452
450
453
451
- public interface TestInterface {
454
+ interface TestInterface {
452
455
453
456
void oneArg (String arg1 );
454
457
0 commit comments