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.app.Velocity;
028: import org.apache.velocity.runtime.RuntimeSingleton;
029: import org.apache.velocity.runtime.log.NullLogChute;
030:
031: /**
032: * Test case for the Velocity Introspector which
033: * tests the ability to find a 'best match'
034: *
035: *
036: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
037: * @version $Id: Introspector2TestCase.java 477002 2006-11-20 01:07:43Z henning $
038: */
039: public class Introspector2TestCase extends BaseTestCase {
040:
041: /**
042: * Creates a new instance.
043: */
044: public Introspector2TestCase(String name) {
045: super (name);
046: }
047:
048: /**
049: * Get the containing <code>TestSuite</code>.
050: *
051: * @return The <code>TestSuite</code> to run.
052: */
053: public static Test suite() {
054: return new TestSuite(Introspector2TestCase.class);
055: }
056:
057: public void testIntrospector() throws Exception {
058: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
059: NullLogChute.class.getName());
060:
061: Velocity.init();
062:
063: Method method;
064: String result;
065: Tester t = new Tester();
066:
067: Object[] params = { new Foo(), new Foo() };
068:
069: method = RuntimeSingleton.getIntrospector().getMethod(
070: Tester.class, "find", params);
071:
072: if (method == null)
073: fail("Returned method was null");
074:
075: result = (String) method.invoke(t, params);
076:
077: if (!result.equals("Bar-Bar")) {
078: fail("Should have gotten 'Bar-Bar' : received '" + result
079: + "'");
080: }
081:
082: /*
083: * now test for failure due to ambiguity
084: */
085:
086: method = RuntimeSingleton.getIntrospector().getMethod(
087: Tester2.class, "find", params);
088:
089: if (method != null)
090: fail("Introspector shouldn't have found a method as it's ambiguous.");
091: }
092:
093: public interface Woogie {
094: }
095:
096: public static class Bar implements Woogie {
097: int i;
098: }
099:
100: public static class Foo extends Bar {
101: int j;
102: }
103:
104: public static class Tester {
105: public static String find(Woogie w, Object o) {
106: return "Woogie-Object";
107: }
108:
109: public static String find(Object w, Bar o) {
110: return "Object-Bar";
111: }
112:
113: public static String find(Bar w, Bar o) {
114: return "Bar-Bar";
115: }
116:
117: public static String find(Object o) {
118: return "Object";
119: }
120:
121: public static String find(Woogie o) {
122: return "Woogie";
123: }
124: }
125:
126: public static class Tester2 {
127: public static String find(Woogie w, Object o) {
128: return "Woogie-Object";
129: }
130:
131: public static String find(Object w, Bar o) {
132: return "Object-Bar";
133: }
134:
135: public static String find(Bar w, Object o) {
136: return "Bar-Object";
137: }
138:
139: public static String find(Object o) {
140: return "Object";
141: }
142:
143: public static String find(Woogie o) {
144: return "Woogie";
145: }
146: }
147: }
|