KickJava   Java API By Example, From Geeks To Geeks.

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


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.app.Velocity;
24
25 import org.apache.velocity.runtime.RuntimeSingleton;
26
27 import junit.framework.TestCase;
28
29 /**
30  * Test case for the Velocity Introspector which
31  * tests the ability to find a 'best match'
32  *
33  *
34  * @author <a HREF="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
35  * @version $Id: IntrospectorTestCase2.java,v 1.1.8.1 2004/03/03 23:23:04 geirm Exp $
36  */

37 public class IntrospectorTestCase2 extends BaseTestCase
38 {
39
40     IntrospectorTestCase2()
41     {
42         super("IntrospectorTestCase2");
43     }
44
45     /**
46       * Creates a new instance.
47       */

48     public IntrospectorTestCase2(String JavaDoc name)
49     {
50         super(name);
51     }
52
53     /**
54       * Get the containing <code>TestSuite</code>.
55       *
56       * @return The <code>TestSuite</code> to run.
57       */

58     public static junit.framework.Test suite ()
59     {
60         return new IntrospectorTestCase2();
61     }
62
63     public void runTest()
64     {
65         try
66         {
67             Velocity.init();
68
69             Method JavaDoc method;
70             String JavaDoc result;
71             Tester t = new Tester();
72             
73             Object JavaDoc[] params = { new Foo(), new Foo() };
74     
75             method = RuntimeSingleton.getIntrospector()
76                 .getMethod( Tester.class, "find", params );
77          
78             if ( method == null)
79                 fail("Returned method was null");
80
81             result = (String JavaDoc) method.invoke( t, params);
82             
83             if ( !result.equals( "Bar-Bar" ) )
84             {
85                 fail("Should have gotten 'Bar-Bar' : recieved '" + result + "'");
86             }
87
88             /*
89              * now test for failure due to ambiguity
90              */

91
92             method = RuntimeSingleton.getIntrospector()
93                 .getMethod( Tester2.class, "find", params );
94          
95             if ( method != null)
96                 fail("Introspector shouldn't have found a method as it's ambiguous.");
97         }
98         catch (Exception JavaDoc e)
99         {
100             fail( e.toString() );
101         }
102     }
103     
104     public interface Woogie
105     {
106     }
107     
108     public static class Bar implements Woogie
109     {
110         int i;
111     }
112     
113     public static class Foo extends Bar
114     {
115         int j;
116     }
117     
118     public static class Tester
119     {
120         public static String JavaDoc find(Woogie w, Object JavaDoc o )
121         {
122             return "Woogie-Object";
123         }
124         
125         public static String JavaDoc find(Object JavaDoc w, Bar o )
126         {
127             return "Object-Bar";
128         }
129         
130         public static String JavaDoc find(Bar w, Bar o )
131         {
132             return "Bar-Bar";
133         }
134    
135         public static String JavaDoc find( Object JavaDoc o )
136         {
137             return "Object";
138         }
139
140         public static String JavaDoc find( Woogie o )
141         {
142             return "Woogie";
143         }
144     }
145
146     public static class Tester2
147     {
148         public static String JavaDoc find(Woogie w, Object JavaDoc o )
149         {
150             return "Woogie-Object";
151         }
152         
153         public static String JavaDoc find(Object JavaDoc w, Bar o )
154         {
155             return "Object-Bar";
156         }
157         
158         public static String JavaDoc find(Bar w, Object JavaDoc o )
159         {
160             return "Bar-Object";
161         }
162
163         public static String JavaDoc find( Object JavaDoc o )
164         {
165             return "Object";
166         }
167
168         public static String JavaDoc find( Woogie o )
169         {
170             return "Woogie";
171         }
172     }
173 }
174
Popular Tags