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.app.Velocity;
28  import org.apache.velocity.runtime.RuntimeSingleton;
29  import org.apache.velocity.test.misc.TestLogChute;
30  
31  /**
32   * Test case for the Velocity Introspector which
33   *  tests the ability to find a 'best match'
34   *
35   *
36   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
37   * @version $Id: Introspector2TestCase.java 704299 2008-10-14 03:13:16Z nbubna $
38   */
39  public class Introspector2TestCase extends BaseTestCase
40  {
41  
42      /**
43        * Creates a new instance.
44        */
45      public Introspector2TestCase(String name)
46      {
47          super(name);
48      }
49  
50      /**
51        * Get the containing <code>TestSuite</code>.
52        *
53        * @return The <code>TestSuite</code> to run.
54        */
55      public static Test suite ()
56      {
57          return new TestSuite(Introspector2TestCase.class);
58      }
59  
60      public void testIntrospector()
61              throws Exception
62      {
63          Velocity.setProperty(
64                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
65  
66          Velocity.init();
67  
68          Method method;
69          String result;
70          Tester t = new Tester();
71  
72          Object[] params = { new Foo(), new Foo() };
73  
74          method = RuntimeSingleton.getIntrospector()
75              .getMethod( Tester.class, "find", params );
76  
77          if ( method == null)
78              fail("Returned method was null");
79  
80          result = (String) method.invoke( t, params);
81  
82          if ( !result.equals( "Bar-Bar" ) )
83          {
84              fail("Should have gotten 'Bar-Bar' : received '" + result + "'");
85          }
86  
87          /*
88           *  now test for failure due to ambiguity
89           */
90  
91          method = RuntimeSingleton.getIntrospector()
92              .getMethod( Tester2.class, "find", params );
93  
94          if ( method != null)
95              fail("Introspector shouldn't have found a method as it's ambiguous.");
96      }
97  
98      public interface Woogie
99      {
100     }
101 
102     public static class Bar implements Woogie
103     {
104         int i;
105     }
106 
107     public static class Foo extends Bar
108     {
109         int j;
110     }
111 
112     public static class Tester
113     {
114         public static String find(Woogie w, Object o )
115         {
116             return "Woogie-Object";
117         }
118 
119         public static String find(Object w, Bar o )
120         {
121             return "Object-Bar";
122         }
123 
124         public static String find(Bar w, Bar o )
125         {
126             return "Bar-Bar";
127         }
128 
129         public static String find( Object o )
130         {
131             return "Object";
132         }
133 
134         public static String find( Woogie o )
135         {
136             return "Woogie";
137         }
138     }
139 
140     public static class Tester2
141     {
142         public static String find(Woogie w, Object o )
143         {
144             return "Woogie-Object";
145         }
146 
147         public static String find(Object w, Bar o )
148         {
149             return "Object-Bar";
150         }
151 
152         public static String find(Bar w, Object o )
153         {
154             return "Bar-Object";
155         }
156 
157         public static String find( Object o )
158         {
159             return "Object";
160         }
161 
162         public static String find( Woogie o )
163         {
164             return "Woogie";
165         }
166     }
167 }