1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 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.
30
30
* Unit tests for {@link MethodBasedEvaluationContext}.
31
31
*
32
32
* @author Stephane Nicoll
33
+ * @author Juergen Hoeller
34
+ * @author Sergey Podgurskiy
33
35
*/
34
36
public class MethodBasedEvaluationContextTests {
35
37
36
38
private final ParameterNameDiscoverer paramDiscover = new DefaultParameterNameDiscoverer ();
37
39
40
+
38
41
@ Test
39
42
public void simpleArguments () {
40
- Method method = ReflectionUtils .findMethod (SampleMethods .class , "hello" ,
41
- String .class , Boolean .class );
42
- MethodBasedEvaluationContext context = createEvaluationContext (method , new Object [] {"test" , true });
43
+ Method method = ReflectionUtils .findMethod (SampleMethods .class , "hello" , String .class , Boolean .class );
44
+ MethodBasedEvaluationContext context = createEvaluationContext (method , "test" , true );
43
45
44
46
assertEquals ("test" , context .lookupVariable ("a0" ));
45
47
assertEquals ("test" , context .lookupVariable ("p0" ));
@@ -50,19 +52,80 @@ public void simpleArguments() {
50
52
assertEquals (true , context .lookupVariable ("flag" ));
51
53
52
54
assertNull (context .lookupVariable ("a2" ));
55
+ assertNull (context .lookupVariable ("p2" ));
53
56
}
54
57
55
58
@ Test
56
59
public void nullArgument () {
57
- Method method = ReflectionUtils .findMethod (SampleMethods .class , "hello" ,
58
- String .class , Boolean .class );
59
- MethodBasedEvaluationContext context = createEvaluationContext (method , new Object [] {null , null });
60
+ Method method = ReflectionUtils .findMethod (SampleMethods .class , "hello" , String .class , Boolean .class );
61
+ MethodBasedEvaluationContext context = createEvaluationContext (method , null , null );
62
+
63
+ assertNull (context .lookupVariable ("a0" ));
64
+ assertNull (context .lookupVariable ("p0" ));
65
+ assertNull (context .lookupVariable ("foo" ));
66
+
67
+ assertNull (context .lookupVariable ("a1" ));
68
+ assertNull (context .lookupVariable ("p1" ));
69
+ assertNull (context .lookupVariable ("flag" ));
70
+ }
71
+
72
+ @ Test
73
+ public void varArgEmpty () {
74
+ Method method = ReflectionUtils .findMethod (SampleMethods .class , "hello" , Boolean .class , String [].class );
75
+ MethodBasedEvaluationContext context = createEvaluationContext (method , new Object [] {null });
60
76
61
77
assertNull (context .lookupVariable ("a0" ));
62
78
assertNull (context .lookupVariable ("p0" ));
79
+ assertNull (context .lookupVariable ("flag" ));
80
+
81
+ assertNull (context .lookupVariable ("a1" ));
82
+ assertNull (context .lookupVariable ("p1" ));
83
+ assertNull (context .lookupVariable ("vararg" ));
84
+ }
85
+
86
+ @ Test
87
+ public void varArgNull () {
88
+ Method method = ReflectionUtils .findMethod (SampleMethods .class , "hello" , Boolean .class , String [].class );
89
+ MethodBasedEvaluationContext context = createEvaluationContext (method , null , null );
90
+
91
+ assertNull (context .lookupVariable ("a0" ));
92
+ assertNull (context .lookupVariable ("p0" ));
93
+ assertNull (context .lookupVariable ("flag" ));
94
+
95
+ assertNull (context .lookupVariable ("a1" ));
96
+ assertNull (context .lookupVariable ("p1" ));
97
+ assertNull (context .lookupVariable ("vararg" ));
63
98
}
64
99
65
- private MethodBasedEvaluationContext createEvaluationContext (Method method , Object [] args ) {
100
+ @ Test
101
+ public void varArgSingle () {
102
+ Method method = ReflectionUtils .findMethod (SampleMethods .class , "hello" , Boolean .class , String [].class );
103
+ MethodBasedEvaluationContext context = createEvaluationContext (method , null , "hello" );
104
+
105
+ assertNull (context .lookupVariable ("a0" ));
106
+ assertNull (context .lookupVariable ("p0" ));
107
+ assertNull (context .lookupVariable ("flag" ));
108
+
109
+ assertEquals ("hello" , context .lookupVariable ("a1" ));
110
+ assertEquals ("hello" , context .lookupVariable ("p1" ));
111
+ assertEquals ("hello" , context .lookupVariable ("vararg" ));
112
+ }
113
+
114
+ @ Test
115
+ public void varArgMultiple () {
116
+ Method method = ReflectionUtils .findMethod (SampleMethods .class , "hello" , Boolean .class , String [].class );
117
+ MethodBasedEvaluationContext context = createEvaluationContext (method , null , "hello" , "hi" );
118
+
119
+ assertNull (context .lookupVariable ("a0" ));
120
+ assertNull (context .lookupVariable ("p0" ));
121
+ assertNull (context .lookupVariable ("flag" ));
122
+
123
+ assertArrayEquals (new Object [] {"hello" , "hi" }, (Object []) context .lookupVariable ("a1" ));
124
+ assertArrayEquals (new Object [] {"hello" , "hi" }, (Object []) context .lookupVariable ("p1" ));
125
+ assertArrayEquals (new Object [] {"hello" , "hi" }, (Object []) context .lookupVariable ("vararg" ));
126
+ }
127
+
128
+ private MethodBasedEvaluationContext createEvaluationContext (Method method , Object ... args ) {
66
129
return new MethodBasedEvaluationContext (this , method , args , this .paramDiscover );
67
130
}
68
131
@@ -73,6 +136,8 @@ private static class SampleMethods {
73
136
private void hello (String foo , Boolean flag ) {
74
137
}
75
138
139
+ private void hello (Boolean flag , String ... vararg ){
140
+ }
76
141
}
77
142
78
- }
143
+ }
0 commit comments