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 
125 
126     public static class NiceTool
127     {
128         public String var(String[] ss)
129         {
130             StringBuffer out = new StringBuffer();
131             for (int i=0; i < ss.length; i++)
132             {
133                 out.append(ss[i]);
134             }
135             return out.toString();
136         }
137 
138         public double add(double[] dd)
139         {
140             double total = 0;
141             for (int i=0; i < dd.length; i++)
142             {
143                 total += dd[i];
144             }
145             return total;
146         }
147 
148         public String test(Object[] oo)
149         {
150             return "objects";
151         }
152 
153         public String test(String[] oo)
154         {
155             return "strings";
156         }
157 
158     }
159 
160     public static class NastyTool extends NiceTool
161     {
162         public String var(String s)
163         {
164             return "only"+s;
165         }
166 
167         public int add(int[] ii)
168         {
169             int total = 0;
170             for (int i=0; i < ii.length; i++)
171             {
172                 total += ii[i];
173             }
174             return total;
175         }
176 
177         public int add(int i)
178         {
179             return i;
180         }
181 
182         public String test()
183         {
184             return "noargs";
185         }
186 
187         public Object test(Object arg)
188         {
189             return "object";
190         }
191 
192         public Object test(String arg)
193         {
194             return "string";
195         }
196 
197         public String test(Object[] array)
198         {
199             return "object[]";
200         }
201 
202         public String test(Object object, String property)
203         {
204             return "object,string";
205         }
206 
207         public String test642(Object[] array)
208         {
209             //JDK5: return Arrays.deepToString(array);
210             if (array == null)
211             {
212                 return null;
213             }
214             StringBuffer o = new StringBuffer("[");
215             for (int i=0; i < array.length; i++)
216             {
217                 if (i > 0)
218                 {
219                     o.append(", ");
220                 }
221                 o.append(String.valueOf(array[i]));
222             }
223             o.append("]");
224             return o.toString();
225         }
226 
227         public String test649(int[] array)
228         {
229             return "int[]";
230         }
231 
232     }
233 
234 }
235 
236