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.lang.reflect.Method;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import org.apache.velocity.runtime.RuntimeSingleton;
28  
29  /**
30   * Test case for the Velocity Introspector which uses
31   * the Java Reflection API to determine the correct
32   * signature of the methods used in VTL templates.
33   *
34   * This should be split into separate tests for each
35   * of the methods searched for but this is a start
36   * for now.
37   *
38   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
39   * @version $Id: IntrospectorTestCase.java 463298 2006-10-12 16:10:32Z henning $
40   */
41  public class IntrospectorTestCase extends BaseTestCase
42  {
43      private static MethodProvider mp;
44  
45      public void setUp()
46      {
47          mp = new MethodProvider();
48      }
49  
50      /**
51       * Creates a new instance.
52       */
53      public IntrospectorTestCase (String name)
54      {
55          super(name);
56      }
57  
58      /**
59        * Get the containing <code>TestSuite</code>.  This is always
60        * <code>VelocityTestSuite</code>.
61        *
62        * @return The <code>TestSuite</code> to run.
63        */
64      public static Test suite ()
65      {
66          return new TestSuite(IntrospectorTestCase.class);
67      }
68  
69      public void testIntrospectorBoolean()
70              throws Exception
71      {
72          // Test boolean primitive.
73          Object[] booleanParams = { new Boolean(true) };
74          String type = "boolean";
75          Method method = RuntimeSingleton.getIntrospector().getMethod(
76              MethodProvider.class, type + "Method", booleanParams);
77          String result = (String) method.invoke(mp, booleanParams);
78  
79          assertEquals("Method could not be found", type, result);
80      }
81  
82      public void testIntrospectorByte()
83              throws Exception
84      {
85          // Test byte primitive.
86          Object[] byteParams = { new Byte("1") };
87          String type = "byte";
88          Method method = RuntimeSingleton.getIntrospector().getMethod(
89              MethodProvider.class, type + "Method", byteParams);
90          String result = (String) method.invoke(mp, byteParams);
91  
92          assertEquals("Method could not be found", type, result);
93      }
94  
95      public void testIntrospectorChar()
96              throws Exception
97      {
98          // Test char primitive.
99          Object[] characterParams = { new Character('a') };
100         String type = "character";
101         Method method = RuntimeSingleton.getIntrospector().getMethod(
102             MethodProvider.class, type + "Method", characterParams);
103         String result = (String) method.invoke(mp, characterParams);
104 
105         assertEquals("Method could not be found", type, result);
106     }
107 
108     public void testIntrospectorDouble()
109             throws Exception
110     {
111 
112         // Test double primitive.
113         Object[] doubleParams = { new Double((double)1) };
114         String type = "double";
115         Method method = RuntimeSingleton.getIntrospector().getMethod(
116             MethodProvider.class, type + "Method", doubleParams);
117         String result = (String) method.invoke(mp, doubleParams);
118 
119         assertEquals("Method could not be found", type, result);
120     }
121 
122     public void testIntrospectorFloat()
123             throws Exception
124     {
125 
126         // Test float primitive.
127         Object[] floatParams = { new Float((float)1) };
128         String type = "float";
129         Method method = RuntimeSingleton.getIntrospector().getMethod(
130             MethodProvider.class, type + "Method", floatParams);
131         String result = (String) method.invoke(mp, floatParams);
132 
133         assertEquals("Method could not be found", type, result);
134     }
135 
136     public void testIntrospectorInteger()
137             throws Exception
138     {
139 
140         // Test integer primitive.
141         Object[] integerParams = { new Integer((int)1) };
142         String type = "integer";
143         Method method = RuntimeSingleton.getIntrospector().getMethod(
144             MethodProvider.class, type + "Method", integerParams);
145         String result = (String) method.invoke(mp, integerParams);
146 
147         assertEquals("Method could not be found", type, result);
148     }
149 
150     public void testIntrospectorPrimitiveLong()
151             throws Exception
152     {
153 
154         // Test long primitive.
155         Object[] longParams = { new Long((long)1) };
156         String type = "long";
157         Method method = RuntimeSingleton.getIntrospector().getMethod(
158             MethodProvider.class, type + "Method", longParams);
159         String result = (String) method.invoke(mp, longParams);
160 
161         assertEquals("Method could not be found", type, result);
162     }
163 
164     public void testIntrospectorPrimitiveShort()
165             throws Exception
166     {
167         // Test short primitive.
168         Object[] shortParams = { new Short((short)1) };
169         String type = "short";
170         Method method = RuntimeSingleton.getIntrospector().getMethod(
171             MethodProvider.class, type + "Method", shortParams);
172         String result = (String) method.invoke(mp, shortParams);
173 
174         assertEquals("Method could not be found", type, result);
175     }
176 
177     public void testIntrospectorUntouchable()
178             throws Exception
179     {
180         // Test untouchable
181 
182         Object[] params = {};
183 
184         Method method = RuntimeSingleton.getIntrospector().getMethod(
185             MethodProvider.class, "untouchable", params);
186 
187         assertNull("able to access a private-access method.", method);
188     }
189 
190     public void testIntrospectorReallyUntouchable()
191             throws Exception
192     {
193         // Test really untouchable
194         Object[] params = {};
195 
196         Method method = RuntimeSingleton.getIntrospector().getMethod(
197             MethodProvider.class, "reallyuntouchable", params);
198 
199         assertNull("able to access a private-access method.", method);
200     }
201 
202     public static class MethodProvider
203     {
204         /*
205          * Methods with native parameter types.
206          */
207         public String booleanMethod (boolean p) { return "boolean"; }
208         public String byteMethod (byte p) { return "byte"; }
209         public String characterMethod (char p) { return "character"; }
210         public String doubleMethod (double p) { return "double"; }
211         public String floatMethod (float p) { return "float"; }
212         public String integerMethod (int p) { return "integer"; }
213         public String longMethod (long p) { return "long"; }
214         public String shortMethod (short p) { return "short"; }
215 
216         String untouchable() { return "yech";}
217         // don't remove! Used through introspection for testing!
218         private String reallyuntouchable() { return "yech!"; }
219 
220     }
221 }