1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.runtime.log.NullLogChute;
30
31
32
33
34
35
36
37
38
39 public class Introspector2TestCase extends BaseTestCase
40 {
41
42
43
44
45 public Introspector2TestCase(String name)
46 {
47 super(name);
48 }
49
50
51
52
53
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, NullLogChute.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
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 }