11/*
2- * Copyright 2002-2015 the original author or authors.
2+ * Copyright 2002-2016 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
3030 * Unit tests for {@link MethodBasedEvaluationContext}.
3131 *
3232 * @author Stephane Nicoll
33+ * @author Juergen Hoeller
34+ * @author Sergey Podgurskiy
3335 */
3436public class MethodBasedEvaluationContextTests {
3537
3638 private final ParameterNameDiscoverer paramDiscover = new DefaultParameterNameDiscoverer ();
3739
40+
3841 @ Test
3942 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 );
4345
4446 assertEquals ("test" , context .lookupVariable ("a0" ));
4547 assertEquals ("test" , context .lookupVariable ("p0" ));
@@ -50,19 +52,80 @@ public void simpleArguments() {
5052 assertEquals (true , context .lookupVariable ("flag" ));
5153
5254 assertNull (context .lookupVariable ("a2" ));
55+ assertNull (context .lookupVariable ("p2" ));
5356 }
5457
5558 @ Test
5659 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 });
6076
6177 assertNull (context .lookupVariable ("a0" ));
6278 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" ));
6398 }
6499
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 ) {
66129 return new MethodBasedEvaluationContext (this , method , args , this .paramDiscover );
67130 }
68131
@@ -73,6 +136,8 @@ private static class SampleMethods {
73136 private void hello (String foo , Boolean flag ) {
74137 }
75138
139+ private void hello (Boolean flag , String ... vararg ){
140+ }
76141 }
77142
78- }
143+ }
0 commit comments