• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
VarargMethodsTestCase(final String name)29     public VarargMethodsTestCase(final String name)
30     {
31         super(name);
32     }
33 
34     @Override
setUpContext(VelocityContext context)35     protected void setUpContext(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", 1f);
43         context.put("ints", new int[] { 1, 2 });
44     }
45 
testStrings()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 
testDoubles()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 
testFloatToDoubleVarArg()62     public void testFloatToDoubleVarArg()
63     {
64         assertEvalEquals("1.0", "$nice.add($float)");
65     }
66 
testStringVsStrings()67     public void testStringVsStrings()
68     {
69         assertEvalEquals("onlyone", "$nasty.var('one')");
70         assertEvalEquals("onlynull", "$nasty.var($null)");
71         assertEvalEquals("", "$nasty.var()");
72     }
73 
testIntVsDoubles()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 
testInts()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 
testStringsVsObjectsAKASubclassVararg()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 
testObjectVarArgVsObjectEtc()97     public void testObjectVarArgVsObjectEtc()
98     {
99         assertEvalEquals("object,string", "$nasty.test($nice,'foo')");
100     }
101 
testObjectVarArgVsObjectVelocity605()102     public void testObjectVarArgVsObjectVelocity605()
103     {
104         assertEvalEquals("string", "$nasty.test('joe')");
105         assertEvalEquals("object", "$nasty.test($nice)");
106     }
107 
testNoArgs()108     public void testNoArgs()
109     {
110         assertEvalEquals("noargs", "$nasty.test()");
111     }
112 
testPassingArrayToVarArgVelocity642()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 
testNullToPrimitiveVarArg()119     public void testNullToPrimitiveVarArg()
120     {
121         assertEvalEquals("int[]", "$nasty.test649($null)");
122     }
123 
testArgsBeforeVarargWithNoArgs()124     public void testArgsBeforeVarargWithNoArgs()
125     {
126         assertEvalEquals("String,String,Object[]", "$nasty.test651('a','b')");
127     }
128 
testVelocity651()129     public void testVelocity651()
130     {
131         assertEvalEquals("String,List", "$nasty.test651('test',['TEST'])");
132     }
133 
testMax()134     public void testMax()
135     {
136         assertEvalEquals("4", "$nasty.max(4, 3.5)");
137     }
138 
139     public static class NiceTool
140     {
var(String[] ss)141         public String var(String[] ss)
142         {
143             StringBuilder out = new StringBuilder();
144             for (String s : ss)
145             {
146                 out.append(s);
147             }
148             return out.toString();
149         }
150 
add(double[] dd)151         public double add(double[] dd)
152         {
153             double total = 0;
154             for (double aDd : dd)
155             {
156                 total += aDd;
157             }
158             return total;
159         }
160 
test(Object[] oo)161         public String test(Object[] oo)
162         {
163             return "objects";
164         }
165 
test(String[] oo)166         public String test(String[] oo)
167         {
168             return "strings";
169         }
170 
171     }
172 
173     public static class NastyTool extends NiceTool
174     {
var(String s)175         public String var(String s)
176         {
177             return "only"+s;
178         }
179 
add(int[] ii)180         public int add(int[] ii)
181         {
182             int total = 0;
183             for (int anIi : ii)
184             {
185                 total += anIi;
186             }
187             return total;
188         }
189 
add(int i)190         public int add(int i)
191         {
192             return i;
193         }
194 
test()195         public String test()
196         {
197             return "noargs";
198         }
199 
test(Object arg)200         public Object test(Object arg)
201         {
202             return "object";
203         }
204 
test(String arg)205         public Object test(String arg)
206         {
207             return "string";
208         }
209 
210         @Override
test(Object[] array)211         public String test(Object[] array)
212         {
213             return "object[]";
214         }
215 
test(Object object, String property)216         public String test(Object object, String property)
217         {
218             return "object,string";
219         }
220 
test642(Object[] array)221         public String test642(Object[] array)
222         {
223             //JDK5: return Arrays.deepToString(array);
224             if (array == null)
225             {
226                 return null;
227             }
228             StringBuilder o = new StringBuilder("[");
229             for (int i=0; i < array.length; i++)
230             {
231                 if (i > 0)
232                 {
233                     o.append(", ");
234                 }
235                 o.append((array[i]));
236             }
237             o.append("]");
238             return o.toString();
239         }
240 
test649(int[] array)241         public String test649(int[] array)
242         {
243             return "int[]";
244         }
245 
test651(String s, String s2, Object[] args)246         public String test651(String s, String s2, Object[] args)
247         {
248             return "String,String,Object[]";
249         }
250 
test651(String s, java.util.List l)251         public String test651(String s, java.util.List l)
252         {
253             return "String,List";
254         }
255 
max(Number n1, Number n2)256         public Number max(Number n1, Number n2) { return max(new Number[] { n1, n2 }); }
257 
max(Number[] numbers)258         public Number max(Number[] numbers)
259         {
260             if (numbers.length == 0) return null;
261             int minindex = -1, i = 0;
262             double val = Double.MIN_VALUE;
263             for (Number n : numbers)
264             {
265                 if (n.floatValue() > val)
266                 {
267                     minindex = i;
268                     val = n.floatValue();
269                 }
270                 ++i;
271             }
272             if (minindex < 0) minindex = 0;
273             return numbers[minindex];
274         }
275 
276     }
277 
278 }
279