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.io.StringWriter;
23  import java.math.BigDecimal;
24  import java.math.BigInteger;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  import org.apache.velocity.VelocityContext;
31  import org.apache.velocity.app.VelocityEngine;
32  import org.apache.velocity.context.Context;
33  import org.apache.velocity.runtime.RuntimeServices;
34  import org.apache.velocity.test.provider.NumberMethods;
35  
36  
37  /**
38   * Used to check that method calls with number parameters are executed correctly.
39   *
40   * @author <a href="mailto:wglass@forio.com">Peter Romianowski</a>
41   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
42   */
43  public class NumberMethodCallsTestCase extends TestCase
44  {
45      private VelocityEngine ve = null;
46  
47      private final static boolean PRINT_RESULTS = false;
48  
49      /**
50       * Default constructor.
51       */
52      public NumberMethodCallsTestCase(String name)
53      {
54          super(name);
55      }
56  
57      public void setUp()
58              throws Exception
59      {
60          ve = new VelocityEngine();
61          ve.init();
62      }
63  
64      public void init( RuntimeServices rs )
65      {
66          // do nothing with it
67      }
68  
69      public static Test suite ()
70      {
71          return new TestSuite(NumberMethodCallsTestCase.class);
72      }
73  
74      /**
75       * Runs the test.
76       */
77      public void testNumberMethodCalls ()
78              throws Exception
79      {
80          VelocityContext vc = new VelocityContext();
81  
82          // context object with overloaded methods with number arguments
83          vc.put("Test",new NumberMethods());
84  
85          // numbers for context
86          vc.put("AByte",new Byte("10"));
87          vc.put("AShort",new Short("10"));
88          vc.put("AInteger",new Integer(10));
89          vc.put("ALong",new Long(10));
90          vc.put("ADouble",new Double(10));
91          vc.put("AFloat",new Float(10));
92          vc.put("ABigDecimal",new BigDecimal(10));
93          vc.put("ABigInteger",new BigInteger("10"));
94  
95          // check context objects
96          System.out.println("Testing: method calls with arguments as context objects");
97          checkResults(vc,"$Test.numMethod($AByte)","byte (10)");
98          checkResults(vc,"$Test.numMethod($AShort)","short (10)");
99          checkResults(vc,"$Test.numMethod($AInteger)","int (10)");
100         checkResults(vc,"$Test.numMethod($ADouble)","double (10.0)");
101         checkResults(vc,"$Test.numMethod($AFloat)","double (10.0)");
102         checkResults(vc,"$Test.numMethod($ALong)","long (10)");
103         checkResults(vc,"$Test.numMethod($ABigDecimal)","BigDecimal (10)");
104         checkResults(vc,"$Test.numMethod($ABigInteger)","BigInteger (10)");
105 
106         // check literals
107         //    -- will cast floating point literal to smallest possible of Double, BigDecimal
108         //    -- will cast integer literal to smallest possible of Integer, Long, BigInteger
109         System.out.println("Testing: method calls with arguments as literals");
110         checkResults(vc,"$Test.numMethod(10.0)","double (10.0)");
111         checkResults(vc,"$Test.numMethod(10)","int (10)");
112         checkResults(vc,"$Test.numMethod(10000000000)","long (10000000000)");
113         checkResults(vc,"$Test.numMethod(10000000000000000000000)","BigInteger (10000000000000000000000)");
114 
115         // check calculated results
116         // -- note calculated value is cast to smallest possible type
117         // -- method invoked is smallest relevant method
118         // -- it's an unusual case here of both byte and int methods, but this works as expected
119         System.out.println("Testing: method calls with arguments as calculated values");
120         checkResults(vc,"#set($val = 10.0 + 1.5)$Test.numMethod($val)","double (11.5)");
121         checkResults(vc,"#set($val = 100 + 1)$Test.numMethod($val)","int (101)");
122         checkResults(vc,"#set($val = 100 * 1000)$Test.numMethod($val)","int (100000)");
123         checkResults(vc,"#set($val = 100 + 1.5)$Test.numMethod($val)","double (101.5)");
124         checkResults(vc,"#set($val = $ALong + $AInteger)$Test.numMethod($val)","long (20)");
125         checkResults(vc,"#set($val = $ABigInteger + $AInteger)$Test.numMethod($val)","BigInteger (20)");
126     }
127 
128 
129     private void checkResults(Context vc, String template, String compare) throws Exception
130     {
131 
132         StringWriter writer = new StringWriter();
133         ve.evaluate( vc, writer, "test", template);
134         assertEquals("Incorrect results for template '" + template + "'.",compare,writer.toString());
135 
136         if (PRINT_RESULTS)
137             System.out.println ("Method call successful: " + template);
138 
139     }
140 
141 
142 }
143 
144