001: package org.apache.velocity.test;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.lang.reflect.Method;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: import org.apache.velocity.runtime.RuntimeSingleton;
028:
029: /**
030: * Test case for the Velocity Introspector which uses
031: * the Java Reflection API to determine the correct
032: * signature of the methods used in VTL templates.
033: *
034: * This should be split into separate tests for each
035: * of the methods searched for but this is a start
036: * for now.
037: *
038: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
039: * @version $Id: IntrospectorTestCase.java 463298 2006-10-12 16:10:32Z henning $
040: */
041: public class IntrospectorTestCase extends BaseTestCase {
042: private static MethodProvider mp;
043:
044: public void setUp() {
045: mp = new MethodProvider();
046: }
047:
048: /**
049: * Creates a new instance.
050: */
051: public IntrospectorTestCase(String name) {
052: super (name);
053: }
054:
055: /**
056: * Get the containing <code>TestSuite</code>. This is always
057: * <code>VelocityTestSuite</code>.
058: *
059: * @return The <code>TestSuite</code> to run.
060: */
061: public static Test suite() {
062: return new TestSuite(IntrospectorTestCase.class);
063: }
064:
065: public void testIntrospectorBoolean() throws Exception {
066: // Test boolean primitive.
067: Object[] booleanParams = { new Boolean(true) };
068: String type = "boolean";
069: Method method = RuntimeSingleton.getIntrospector().getMethod(
070: MethodProvider.class, type + "Method", booleanParams);
071: String result = (String) method.invoke(mp, booleanParams);
072:
073: assertEquals("Method could not be found", type, result);
074: }
075:
076: public void testIntrospectorByte() throws Exception {
077: // Test byte primitive.
078: Object[] byteParams = { new Byte("1") };
079: String type = "byte";
080: Method method = RuntimeSingleton.getIntrospector().getMethod(
081: MethodProvider.class, type + "Method", byteParams);
082: String result = (String) method.invoke(mp, byteParams);
083:
084: assertEquals("Method could not be found", type, result);
085: }
086:
087: public void testIntrospectorChar() throws Exception {
088: // Test char primitive.
089: Object[] characterParams = { new Character('a') };
090: String type = "character";
091: Method method = RuntimeSingleton.getIntrospector().getMethod(
092: MethodProvider.class, type + "Method", characterParams);
093: String result = (String) method.invoke(mp, characterParams);
094:
095: assertEquals("Method could not be found", type, result);
096: }
097:
098: public void testIntrospectorDouble() throws Exception {
099:
100: // Test double primitive.
101: Object[] doubleParams = { new Double((double) 1) };
102: String type = "double";
103: Method method = RuntimeSingleton.getIntrospector().getMethod(
104: MethodProvider.class, type + "Method", doubleParams);
105: String result = (String) method.invoke(mp, doubleParams);
106:
107: assertEquals("Method could not be found", type, result);
108: }
109:
110: public void testIntrospectorFloat() throws Exception {
111:
112: // Test float primitive.
113: Object[] floatParams = { new Float((float) 1) };
114: String type = "float";
115: Method method = RuntimeSingleton.getIntrospector().getMethod(
116: MethodProvider.class, type + "Method", floatParams);
117: String result = (String) method.invoke(mp, floatParams);
118:
119: assertEquals("Method could not be found", type, result);
120: }
121:
122: public void testIntrospectorInteger() throws Exception {
123:
124: // Test integer primitive.
125: Object[] integerParams = { new Integer((int) 1) };
126: String type = "integer";
127: Method method = RuntimeSingleton.getIntrospector().getMethod(
128: MethodProvider.class, type + "Method", integerParams);
129: String result = (String) method.invoke(mp, integerParams);
130:
131: assertEquals("Method could not be found", type, result);
132: }
133:
134: public void testIntrospectorPrimitiveLong() throws Exception {
135:
136: // Test long primitive.
137: Object[] longParams = { new Long((long) 1) };
138: String type = "long";
139: Method method = RuntimeSingleton.getIntrospector().getMethod(
140: MethodProvider.class, type + "Method", longParams);
141: String result = (String) method.invoke(mp, longParams);
142:
143: assertEquals("Method could not be found", type, result);
144: }
145:
146: public void testIntrospectorPrimitiveShort() throws Exception {
147: // Test short primitive.
148: Object[] shortParams = { new Short((short) 1) };
149: String type = "short";
150: Method method = RuntimeSingleton.getIntrospector().getMethod(
151: MethodProvider.class, type + "Method", shortParams);
152: String result = (String) method.invoke(mp, shortParams);
153:
154: assertEquals("Method could not be found", type, result);
155: }
156:
157: public void testIntrospectorUntouchable() throws Exception {
158: // Test untouchable
159:
160: Object[] params = {};
161:
162: Method method = RuntimeSingleton.getIntrospector().getMethod(
163: MethodProvider.class, "untouchable", params);
164:
165: assertNull("able to access a private-access method.", method);
166: }
167:
168: public void testIntrospectorReallyUntouchable() throws Exception {
169: // Test really untouchable
170: Object[] params = {};
171:
172: Method method = RuntimeSingleton.getIntrospector().getMethod(
173: MethodProvider.class, "reallyuntouchable", params);
174:
175: assertNull("able to access a private-access method.", method);
176: }
177:
178: public static class MethodProvider {
179: /*
180: * Methods with native parameter types.
181: */
182: public String booleanMethod(boolean p) {
183: return "boolean";
184: }
185:
186: public String byteMethod(byte p) {
187: return "byte";
188: }
189:
190: public String characterMethod(char p) {
191: return "character";
192: }
193:
194: public String doubleMethod(double p) {
195: return "double";
196: }
197:
198: public String floatMethod(float p) {
199: return "float";
200: }
201:
202: public String integerMethod(int p) {
203: return "integer";
204: }
205:
206: public String longMethod(long p) {
207: return "long";
208: }
209:
210: public String shortMethod(short p) {
211: return "short";
212: }
213:
214: String untouchable() {
215: return "yech";
216: }
217:
218: // don't remove! Used through introspection for testing!
219: private String reallyuntouchable() {
220: return "yech!";
221: }
222:
223: }
224: }
|