KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > reflect > ReflectionTest


1 /*
2  * Copyright(C) 2002 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or(at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.reflect;
20
21 import java.lang.reflect.Modifier JavaDoc;
22
23 import junit.framework.TestCase;
24
25 /**
26  * A JUnit test case for InstructionList.
27  * NOTE! By default this tests BCEL provider. If another provider
28  * needs to be tested a system property alt.jiapi.provider.factory
29  * can be used.
30  * <p>
31  * The test first creates two object representations for class
32  * <code>java.net.URLClassLoader</code>. NOTE! Almost any class
33  * would be feasible to be used for testing. The reason this is chosen
34  * is that it is reasonable complicated and is certainly in a CLASSPATH.
35  * <p>
36  * The test then creates a few test cases.
37  *
38  * @author Mika Riekkinen
39  * @author Joni Suominen
40  * @version $Revision: 1.2 $ $Date: 2004/03/29 10:10:24 $
41  */

42 public class ReflectionTest extends TestCase {
43     private Object JavaDoc testField; // Used in testing modifier changing
44

45     private Loader loader;
46     private JiapiClass clazz;
47
48     public ReflectionTest(String JavaDoc name) {
49         super(name);
50     }
51
52     public String JavaDoc getName() {
53         return "ReflectionTest: " + super.getName();
54     }
55
56
57     protected void setUp() {
58         try {
59             loader = new Loader();
60             // this class --> clazz
61
clazz = loader.loadClass(getClass().getName());
62         }
63         catch (Exception JavaDoc e) {
64             fail(e.getMessage());
65         }
66     }
67
68     /**
69      * Try to get methods of this Class through JiapiClass APIs.
70      * See end of the file for methods that are to be get.
71      */

72     public void testMethodGetting() {
73         try {
74             JiapiMethod m = null;
75
76             m = clazz.getDeclaredMethod("m_void", new String JavaDoc[0]);
77             assertNotNull(m);
78             m = clazz.getDeclaredMethod("m_object",
79                                         new String JavaDoc[]{"java.lang.Object"});
80             assertNotNull(m);
81             m = clazz.getDeclaredMethod("m_int", new String JavaDoc[]{"int"});
82             assertNotNull(m);
83             m = clazz.getDeclaredMethod("m_long", new String JavaDoc[]{"long"});
84             assertNotNull(m);
85             m = clazz.getDeclaredMethod("m_byte", new String JavaDoc[]{"byte"});
86             assertNotNull(m);
87             m = clazz.getDeclaredMethod("m_char", new String JavaDoc[]{"char"});
88             assertNotNull(m);
89             m = clazz.getDeclaredMethod("m_float", new String JavaDoc[]{"float"});
90             assertNotNull(m);
91             m = clazz.getDeclaredMethod("m_double", new String JavaDoc[]{"double"});
92             assertNotNull(m);
93             m = clazz.getDeclaredMethod("m_boolean", new String JavaDoc[]{"boolean"});
94             assertNotNull(m);
95             m = clazz.getDeclaredMethod("boolean_m_boolean",
96                                         new String JavaDoc[]{"boolean"});
97             assertNotNull(m);
98             m = clazz.getDeclaredMethod("m_object_array",
99                                 new String JavaDoc[]{"java.lang.Object[]"});
100             assertNotNull(m);
101             m = clazz.getDeclaredMethod("m_int_array", new String JavaDoc[]{"int[]"});
102             assertNotNull(m);
103             m = clazz.getDeclaredMethod("m_int_array_long_array",
104                                         new String JavaDoc[]{"int[]", "long[]"});
105             assertNotNull(m);
106             m = clazz.getDeclaredMethod("m_int_array_long_array",
107                                         new String JavaDoc[]{"int []", "long []"});
108             assertNotNull(m);
109
110             m = null;
111             try {
112                 m = clazz.getDeclaredMethod("nonExistentMethod",
113                                             new String JavaDoc[]{"int[]", "long[]"});
114             }
115             catch(Exception JavaDoc e) {
116                 // Supposed to trap here
117
}
118             assertNull(m);
119         }
120         catch (Exception JavaDoc e) {
121             fail(e.getMessage()); // Force test failure
122
}
123     }
124
125
126
127     /**
128      * Tests field additions(and method additions in the future)
129      */

130     public void testAdditions() {
131         // Create new field
132
JiapiField f = null;
133         try {
134             f = clazz.addField("newField1");
135             assertNotNull(f);
136
137             // Create same field -> exception
138
try {
139                 f = clazz.addField(f.getName());
140                 fail("FieldExistsException should have been thrown");
141             }
142             catch (Exception JavaDoc e) {
143                 assertTrue(true); // We should trap here
144
}
145         }
146         catch (Exception JavaDoc e) {
147             fail(e.getMessage());
148         }
149
150         try {
151             // Create integer field
152
f = clazz.addField(Modifier.PUBLIC, "int", "newField2");
153             assertNotNull(f);
154             // Create java.lang.Object field
155
f = clazz.addField(Modifier.PUBLIC, "java.lang.Object",
156                                "newField3");
157             assertNotNull(f);
158         }
159         catch (Exception JavaDoc e) {
160             fail(e.getMessage());
161         }
162
163
164         JiapiMethod m = null;
165         try {
166             m = clazz.addMethod(Modifier.PUBLIC, "newMethod",
167                                 new Signature("void", new String JavaDoc[0]));
168             assertNotNull(m);
169
170             // Create same method -> exception
171
try {
172                 m = clazz.addMethod(m);
173                 fail("MethodExistsException should have been thrown");
174             }
175             catch (Exception JavaDoc e) {
176                 assertTrue(true); // We should trap here
177
}
178         }
179         catch (Exception JavaDoc e) {
180             fail(e.getMessage());
181         }
182     }
183
184     //
185
// Methods used for testing method gets
186
//
187
public void m_void() {}
188     public void m_object(Object JavaDoc o) {}
189     public void m_int(int i) {}
190     public void m_long(long l) {}
191     public void m_byte(byte b) {}
192     public void m_char(char l) {}
193     public void m_float(float l) {}
194     public void m_double(double l) {}
195     public void m_boolean(boolean l) {}
196
197     public boolean boolean_m_boolean(boolean l) { return false;}
198
199     public void m_object_array(Object JavaDoc[] o) {}
200     public void m_int_array(int[] i) {}
201     public void m_int_array_long_array(int[] i, long[] l) {}
202 }
203
204
Popular Tags