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 java.util.Arrays;
23  import org.apache.velocity.VelocityContext;
24  
25  /**
26   * Used to check that vararg method calls on references work properly
27   */
28  public class VarargMethodsTestCase extends BaseEvalTestCase
29  {
30      public VarargMethodsTestCase(final String name)
31      {
32          super(name);
33      }
34  
35      protected void setContext(VelocityContext context)
36      {
37          context.put("nice", new NiceTool());
38          context.put("nasty", new NastyTool());
39          context.put("objects", new Object[] { this, VelocityContext.class });
40          context.put("strings", new String[] { "one", "two" });
41          context.put("doubles", new double[] { 1.5, 2.5 });
42          context.put("float", new Float(1f));
43          context.put("ints", new int[] { 1, 2 });
44      }
45  
46      public void testStrings()
47      {
48          assertEvalEquals("onetwo", "$nice.var($strings)");
49          assertEvalEquals("onetwo", "$nice.var('one','two')");
50          assertEvalEquals("one", "$nice.var('one')");
51          assertEvalEquals("", "$nice.var()");
52      }
53  
54      public void testDoubles()
55      {
56          assertEvalEquals("4.0", "$nice.add($doubles)");
57          assertEvalEquals("3.0", "$nice.add(1,2)");
58          assertEvalEquals("1.0", "$nice.add(1)");
59          assertEvalEquals("0.0", "$nice.add()");
60      }
61  
62      public void testFloatToDoubleVarArg()
63      {
64          assertEvalEquals("1.0", "$nice.add($float)");
65      }
66  
67      public void testStringVsStrings()
68      {
69          assertEvalEquals("onlyone", "$nasty.var('one')");
70          assertEvalEquals("onlynull", "$nasty.var($null)");
71          assertEvalEquals("", "$nasty.var()");
72      }
73  
74      public void testIntVsDoubles()
75      {
76          assertEvalEquals("1", "$nasty.add(1)");
77          assertEvalEquals("1.0", "$nasty.add(1.0)");
78          assertEvalEquals("3.0", "$nasty.add(1.0,2)");
79      }
80  
81      public void testInts()
82      {
83          assertEvalEquals("3", "$nasty.add($ints)");
84          assertEvalEquals("3", "$nasty.add(1,2)");
85          assertEvalEquals("1", "$nasty.add(1)");
86          // add(int[]) wins because it is "more specific"
87          assertEvalEquals("0", "$nasty.add()");
88      }
89  
90      public void testStringsVsObjectsAKASubclassVararg()
91      {
92          assertEvalEquals("objects", "$nice.test($objects)");
93          assertEvalEquals("objects", "$nice.test($nice,$nasty,$ints)");
94          assertEvalEquals("strings", "$nice.test('foo')");
95      }
96  
97      public void testObjectVarArgVsObjectEtc()
98      {
99          assertEvalEquals("object,string", "$nasty.test($nice,'foo')");
100     }
101 
102     public void testObjectVarArgVsObjectVelocity605()
103     {
104         assertEvalEquals("string", "$nasty.test('joe')");
105         assertEvalEquals("object", "$nasty.test($nice)");
106     }
107 
108     public void testNoArgs()
109     {
110         assertEvalEquals("noargs", "$nasty.test()");
111     }
112 
113     public void testPassingArrayToVarArgVelocity642()
114     {
115         assertEvalEquals("[one, two]", "$nasty.test642($strings)");
116         assertEvalEquals("[1, 2]", "#set( $list = [1..2] )$nasty.test642($list.toArray())");
117     }
118 
119     public void testNullToPrimitiveVarArg()
120     {
121         assertEvalEquals("int[]", "$nasty.test649($null)");
122     }
123 
124     public void testVelocity651()
125     {
126         assertEvalEquals("String,List", "$nasty.test651('test',['TEST'])");
127     }
128 
129 
130     public static class NiceTool
131     {
132         public String var(String[] ss)
133         {
134             StringBuffer out = new StringBuffer();
135             for (int i=0; i < ss.length; i++)
136             {
137                 out.append(ss[i]);
138             }
139             return out.toString();
140         }
141 
142         public double add(double[] dd)
143         {
144             double total = 0;
145             for (int i=0; i < dd.length; i++)
146             {
147                 total += dd[i];
148             }
149             return total;
150         }
151 
152         public String test(Object[] oo)
153         {
154             return "objects";
155         }
156 
157         public String test(String[] oo)
158         {
159             return "strings";
160         }
161 
162     }
163 
164     public static class NastyTool extends NiceTool
165     {
166         public String var(String s)
167         {
168             return "only"+s;
169         }
170 
171         public int add(int[] ii)
172         {
173             int total = 0;
174             for (int i=0; i < ii.length; i++)
175             {
176                 total += ii[i];
177             }
178             return total;
179         }
180 
181         public int add(int i)
182         {
183             return i;
184         }
185 
186         public String test()
187         {
188             return "noargs";
189         }
190 
191         public Object test(Object arg)
192         {
193             return "object";
194         }
195 
196         public Object test(String arg)
197         {
198             return "string";
199         }
200 
201         public String test(Object[] array)
202         {
203             return "object[]";
204         }
205 
206         public String test(Object object, String property)
207         {
208             return "object,string";
209         }
210 
211         public String test642(Object[] array)
212         {
213             //JDK5: return Arrays.deepToString(array);
214             if (array == null)
215             {
216                 return null;
217             }
218             StringBuffer o = new StringBuffer("[");
219             for (int i=0; i < array.length; i++)
220             {
221                 if (i > 0)
222                 {
223                     o.append(", ");
224                 }
225                 o.append(String.valueOf(array[i]));
226             }
227             o.append("]");
228             return o.toString();
229         }
230 
231         public String test649(int[] array)
232         {
233             return "int[]";
234         }
235 
236         public String test651(String s, String s2, Object[] args)
237         {
238             return "String,String,Object[]";
239         }
240 
241         public String test651(String s, java.util.List l)
242         {
243             return "String,List";
244         }
245 
246     }
247 
248 }
249 
250