1   package org.apache.velocity.test;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
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  
52  
53      public IntrospectorTestCase (String name)
54      {
55          super(name);
56      }
57  
58      
59  
60  
61  
62  
63  
64      public static Test suite ()
65      {
66          return new TestSuite(IntrospectorTestCase.class);
67      }
68  
69      public void testIntrospectorBoolean()
70              throws Exception
71      {
72          
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          
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          
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         
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         
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         
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         
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         
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         
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         
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 
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         
218         private String reallyuntouchable() { return "yech!"; }
219 
220     }
221 }