1   package org.apache.velocity.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.velocity.VelocityContext;
23  
24  /**
25   * Used to check that vararg method calls on references work properly
26   */
27  public class VarargMethodsTestCase extends BaseTestCase
28  {
29      public VarargMethodsTestCase(final String name)
30      {
31          super(name);
32      }
33  
34      protected void setUpContext(VelocityContext context)
35      {
36          context.put("nice", new NiceTool());
37          context.put("nasty", new NastyTool());
38          context.put("objects", new Object[] { this, VelocityContext.class });
39          context.put("strings", new String[] { "one", "two" });
40          context.put("doubles", new double[] { 1.5, 2.5 });
41          context.put("float", new Float(1f));
42          context.put("ints", new int[] { 1, 2 });
43      }
44  
45      public void testStrings()
46      {
47          assertEvalEquals("onetwo", "$nice.var($strings)");
48          assertEvalEquals("onetwo", "$nice.var('one','two')");
49          assertEvalEquals("one", "$nice.var('one')");
50          assertEvalEquals("", "$nice.var()");
51      }
52  
53      public void testDoubles()
54      {
55          assertEvalEquals("4.0", "$nice.add($doubles)");
56          assertEvalEquals("3.0", "$nice.add(1,2)");
57          assertEvalEquals("1.0", "$nice.add(1)");
58          assertEvalEquals("0.0", "$nice.add()");
59      }
60  
61      public void testFloatToDoubleVarArg()
62      {
63          assertEvalEquals("1.0", "$nice.add($float)");
64      }
65  
66      public void testStringVsStrings()
67      {
68          assertEvalEquals("onlyone", "$nasty.var('one')");
69          assertEvalEquals("onlynull", "$nasty.var($null)");
70          assertEvalEquals("", "$nasty.var()");
71      }
72  
73      public void testIntVsDoubles()
74      {
75          assertEvalEquals("1", "$nasty.add(1)");
76          assertEvalEquals("1.0", "$nasty.add(1.0)");
77          assertEvalEquals("3.0", "$nasty.add(1.0,2)");
78      }
79  
80      public void testInts()
81      {
82          assertEvalEquals("3", "$nasty.add($ints)");
83          assertEvalEquals("3", "$nasty.add(1,2)");
84          assertEvalEquals("1", "$nasty.add(1)");
85          // add(int[]) wins because it is "more specific"
86          assertEvalEquals("0", "$nasty.add()");
87      }
88  
89      public void testStringsVsObjectsAKASubclassVararg()
90      {
91          assertEvalEquals("objects", "$nice.test($objects)");
92          assertEvalEquals("objects", "$nice.test($nice,$nasty,$ints)");
93          assertEvalEquals("strings", "$nice.test('foo')");
94      }
95  
96      public void testObjectVarArgVsObjectEtc()
97      {
98          assertEvalEquals("object,string", "$nasty.test($nice,'foo')");
99      }
100 
101     public void testObjectVarArgVsObjectVelocity605()
102     {
103         assertEvalEquals("string", "$nasty.test('joe')");
104         assertEvalEquals("object", "$nasty.test($nice)");
105     }
106 
107     public void testNoArgs()
108     {
109         assertEvalEquals("noargs", "$nasty.test()");
110     }
111 
112     public void testPassingArrayToVarArgVelocity642()
113     {
114         assertEvalEquals("[one, two]", "$nasty.test642($strings)");
115         assertEvalEquals("[1, 2]", "#set( $list = [1..2] )$nasty.test642($list.toArray())");
116     }
117 
118     public void testNullToPrimitiveVarArg()
119     {
120         assertEvalEquals("int[]", "$nasty.test649($null)");
121     }
122 
123     public void testArgsBeforeVarargWithNoArgs()
124     {
125         assertEvalEquals("String,String,Object[]", "$nasty.test651('a','b')");
126     }
127 
128     public void testVelocity651()
129     {
130         assertEvalEquals("String,List", "$nasty.test651('test',['TEST'])");
131     }
132 
133 
134 
135     public static class NiceTool
136     {
137         public String var(String[] ss)
138         {
139             StringBuffer out = new StringBuffer();
140             for (int i=0; i < ss.length; i++)
141             {
142                 out.append(ss[i]);
143             }
144             return out.toString();
145         }
146 
147         public double add(double[] dd)
148         {
149             double total = 0;
150             for (int i=0; i < dd.length; i++)
151             {
152                 total += dd[i];
153             }
154             return total;
155         }
156 
157         public String test(Object[] oo)
158         {
159             return "objects";
160         }
161 
162         public String test(String[] oo)
163         {
164             return "strings";
165         }
166 
167     }
168 
169     public static class NastyTool extends NiceTool
170     {
171         public String var(String s)
172         {
173             return "only"+s;
174         }
175 
176         public int add(int[] ii)
177         {
178             int total = 0;
179             for (int i=0; i < ii.length; i++)
180             {
181                 total += ii[i];
182             }
183             return total;
184         }
185 
186         public int add(int i)
187         {
188             return i;
189         }
190 
191         public String test()
192         {
193             return "noargs";
194         }
195 
196         public Object test(Object arg)
197         {
198             return "object";
199         }
200 
201         public Object test(String arg)
202         {
203             return "string";
204         }
205 
206         public String test(Object[] array)
207         {
208             return "object[]";
209         }
210 
211         public String test(Object object, String property)
212         {
213             return "object,string";
214         }
215 
216         public String test642(Object[] array)
217         {
218             //JDK5: return Arrays.deepToString(array);
219             if (array == null)
220             {
221                 return null;
222             }
223             StringBuffer o = new StringBuffer("[");
224             for (int i=0; i < array.length; i++)
225             {
226                 if (i > 0)
227                 {
228                     o.append(", ");
229                 }
230                 o.append(String.valueOf(array[i]));
231             }
232             o.append("]");
233             return o.toString();
234         }
235 
236         public String test649(int[] array)
237         {
238             return "int[]";
239         }
240 
241         public String test651(String s, String s2, Object[] args)
242         {
243             return "String,String,Object[]";
244         }
245 
246         public String test651(String s, java.util.List l)
247         {
248             return "String,List";
249         }
250 
251     }
252 
253 }
254 
255