KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > IntrospectorTestCase


1 package org.apache.velocity.test;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.ArrayList JavaDoc;
20
21 import java.lang.reflect.Method JavaDoc;
22
23 import org.apache.velocity.runtime.RuntimeSingleton;
24
25 import junit.framework.TestCase;
26
27 /**
28  * Test case for the Velocity Introspector which uses
29  * the Java Reflection API to determine the correct
30  * signature of the methods used in VTL templates.
31  *
32  * This should be split into separate tests for each
33  * of the methods searched for but this is a start
34  * for now.
35  *
36  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
37  * @version $Id: IntrospectorTestCase.java,v 1.10.8.1 2004/03/03 23:23:04 geirm Exp $
38  */

39 public class IntrospectorTestCase extends BaseTestCase
40 {
41     private Method JavaDoc method;
42     private String JavaDoc result;
43     private String JavaDoc type;
44     private ArrayList JavaDoc failures = new ArrayList JavaDoc();
45
46     IntrospectorTestCase()
47     {
48         super("IntrospectorTestCase");
49     }
50
51     /**
52       * Creates a new instance.
53       */

54     public IntrospectorTestCase (String JavaDoc name)
55     {
56         super(name);
57     }
58
59     /**
60       * Get the containing <code>TestSuite</code>. This is always
61       * <code>VelocityTestSuite</code>.
62       *
63       * @return The <code>TestSuite</code> to run.
64       */

65     public static junit.framework.Test suite ()
66     {
67         return new IntrospectorTestCase();
68     }
69
70     public void runTest()
71     {
72         MethodProvider mp = new MethodProvider();
73     
74         try
75         {
76             // Test boolean primitive.
77
Object JavaDoc[] booleanParams = { new Boolean JavaDoc(true) };
78             type = "boolean";
79             method = RuntimeSingleton.getIntrospector().getMethod(
80                 MethodProvider.class, type + "Method", booleanParams);
81             result = (String JavaDoc) method.invoke(mp, booleanParams);
82             
83             if (!result.equals(type))
84                 failures.add(type + "Method could not be found!");
85             
86             // Test byte primitive.
87
Object JavaDoc[] byteParams = { new Byte JavaDoc("1") };
88             type = "byte";
89             method = RuntimeSingleton.getIntrospector().getMethod(
90                 MethodProvider.class, type + "Method", byteParams);
91             result = (String JavaDoc) method.invoke(mp, byteParams);
92
93             if (!result.equals(type))
94                 failures.add(type + "Method could not be found!");
95
96             // Test char primitive.
97
Object JavaDoc[] characterParams = { new Character JavaDoc('a') };
98             type = "character";
99             method = RuntimeSingleton.getIntrospector().getMethod(
100                 MethodProvider.class, type + "Method", characterParams);
101             result = (String JavaDoc) method.invoke(mp, characterParams);
102
103             if (!result.equals(type))
104                 failures.add(type + "Method could not be found!");
105
106             // Test double primitive.
107
Object JavaDoc[] doubleParams = { new Double JavaDoc((double)1) };
108             type = "double";
109             method = RuntimeSingleton.getIntrospector().getMethod(
110                 MethodProvider.class, type + "Method", doubleParams);
111             result = (String JavaDoc) method.invoke(mp, doubleParams);
112
113             if (!result.equals(type))
114                 failures.add(type + "Method could not be found!");
115
116             // Test float primitive.
117
Object JavaDoc[] floatParams = { new Float JavaDoc((float)1) };
118             type = "float";
119             method = RuntimeSingleton.getIntrospector().getMethod(
120                 MethodProvider.class, type + "Method", floatParams);
121             result = (String JavaDoc) method.invoke(mp, floatParams);
122
123             if (!result.equals(type))
124                 failures.add(type + "Method could not be found!");
125
126             // Test integer primitive.
127
Object JavaDoc[] integerParams = { new Integer JavaDoc((int)1) };
128             type = "integer";
129             method = RuntimeSingleton.getIntrospector().getMethod(
130                 MethodProvider.class, type + "Method", integerParams);
131             result = (String JavaDoc) method.invoke(mp, integerParams);
132
133             if (!result.equals(type))
134                 failures.add(type + "Method could not be found!");
135
136             // Test long primitive.
137
Object JavaDoc[] longParams = { new Long JavaDoc((long)1) };
138             type = "long";
139             method = RuntimeSingleton.getIntrospector().getMethod(
140                 MethodProvider.class, type + "Method", longParams);
141             result = (String JavaDoc) method.invoke(mp, longParams);
142
143             if (!result.equals(type))
144                 failures.add(type + "Method could not be found!");
145
146             // Test short primitive.
147
Object JavaDoc[] shortParams = { new Short JavaDoc((short)1) };
148             type = "short";
149             method = RuntimeSingleton.getIntrospector().getMethod(
150                 MethodProvider.class, type + "Method", shortParams);
151             result = (String JavaDoc) method.invoke(mp, shortParams);
152         
153             if (!result.equals(type))
154                 failures.add(type + "Method could not be found!");
155
156             // Test untouchable
157

158             Object JavaDoc[] params = {};
159            
160             method = RuntimeSingleton.getIntrospector().getMethod(
161                 MethodProvider.class, "untouchable", params);
162
163             if (method != null)
164                 failures.add(type + "able to access a private-access method.");
165
166             // Test really untouchable
167

168             method = RuntimeSingleton.getIntrospector().getMethod(
169                 MethodProvider.class, "reallyuntouchable", params);
170
171             if (method != null)
172                 failures.add(type + "able to access a default-access method.");
173
174             // There were any failures then show all the
175
// errors that occured.
176

177             int totalFailures = failures.size();
178             if (totalFailures > 0)
179             {
180                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc("\nIntrospection Errors:\n");
181                 for (int i = 0; i < totalFailures; i++)
182                     sb.append((String JavaDoc) failures.get(i)).append("\n");
183             
184                 fail(sb.toString());
185             }
186         }
187         catch (Exception JavaDoc e)
188         {
189             fail( e.toString() );
190         }
191     }
192
193     public static class MethodProvider
194     {
195         /*
196          * Methods with native parameter types.
197          */

198         public String JavaDoc booleanMethod (boolean p) { return "boolean"; }
199         public String JavaDoc byteMethod (byte p) { return "byte"; }
200         public String JavaDoc characterMethod (char p) { return "character"; }
201         public String JavaDoc doubleMethod (double p) { return "double"; }
202         public String JavaDoc floatMethod (float p) { return "float"; }
203         public String JavaDoc integerMethod (int p) { return "integer"; }
204         public String JavaDoc longMethod (long p) { return "long"; }
205         public String JavaDoc shortMethod (short p) { return "short"; }
206
207         String JavaDoc untouchable() { return "yech";}
208         private String JavaDoc reallyuntouchable() { return "yech!"; }
209
210     }
211 }
212
Popular Tags