KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > ClassloadingTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.kernel;
19
20 import java.util.Set JavaDoc;
21 import java.util.LinkedHashSet JavaDoc;
22 import java.io.Serializable JavaDoc;
23
24 import junit.framework.TestCase;
25 import org.apache.geronimo.kernel.basic.BasicKernel;
26
27 /**
28  * Unit test for {@link org.apache.geronimo.kernel.ClassLoading} class.
29  *
30  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class ClassloadingTest extends TestCase {
33     public void testLoadClass_Null() {
34         try {
35             ClassLoading.loadClass("org.apache.geronimo.kernel.ClassLoading", null);
36             fail("Expected NullArgumentException");
37         } catch (IllegalArgumentException JavaDoc ignore) {
38         } catch (ClassNotFoundException JavaDoc e) {
39             fail("Class should have been found: " + e);
40         }
41     }
42
43     public void testLoadClass_Simple() {
44         String JavaDoc className = "org.apache.geronimo.kernel.ClassLoading";
45         Class JavaDoc type = loadClass(className);
46         assertEquals(className, type.getName());
47     }
48
49     public void testLoadClass_Primitives() {
50         String JavaDoc className = "boolean";
51         Class JavaDoc type = loadClass(className);
52         assertEquals(className, type.getName());
53     }
54
55     public void testLoadClass_VMPrimitives() {
56         String JavaDoc className = "B";
57         Class JavaDoc type = loadClass(className);
58         assertEquals(byte.class, type);
59     }
60
61     public void testLoadClass_VMClassSyntax() {
62         String JavaDoc className = "org.apache.geronimo.kernel.ClassLoading";
63         Class JavaDoc type = loadClass("L" + className + ";");
64         assertEquals(className, type.getName());
65     }
66
67     public void testLoadClass_VMArraySyntax() {
68         String JavaDoc className = "[B";
69         Class JavaDoc type = loadClass(className);
70         assertEquals(byte[].class, type);
71
72         className = "[java.lang.String";
73         type = loadClass(className);
74         assertEquals(String JavaDoc[].class, type);
75     }
76
77     public void testLoadClass_UserFriendlySyntax() {
78         String JavaDoc className = "I[]";
79         Class JavaDoc type = loadClass(className);
80         assertEquals(int[].class, type);
81
82         className = "I[][][]";
83         type = loadClass(className);
84         assertEquals(int[][][].class, type);
85     }
86
87     public void testgetClassName() throws ClassNotFoundException JavaDoc {
88         Class JavaDoc t;
89         Class JavaDoc y;
90         String JavaDoc x;
91
92         t = String JavaDoc.class;
93         x = ClassLoading.getClassName(t);
94         y = loadClass(x);
95         assertEquals(t, y);
96
97         t = int.class;
98         x = ClassLoading.getClassName(t);
99         y = loadClass(x);
100         assertEquals(t, y);
101
102         t = String JavaDoc[].class;
103         x = ClassLoading.getClassName(t);
104         y = loadClass(x);
105         assertEquals(t, y);
106
107         t = int[].class;
108         x = ClassLoading.getClassName(t);
109         y = loadClass(x);
110         assertEquals(t, y);
111
112         t = String JavaDoc[][].class;
113         x = ClassLoading.getClassName(t);
114         y = loadClass(x);
115         assertEquals(t, y);
116
117         t = int[][].class;
118         x = ClassLoading.getClassName(t);
119         y = loadClass(x);
120         assertEquals(t, y);
121
122     }
123
124     public void testGetAllTypes() throws Exception JavaDoc {
125         Set JavaDoc allTypes = ClassLoading.getAllTypes(MockGBean.class);
126         assertTrue(allTypes.contains(MockGBean.class));
127         assertTrue(allTypes.contains(Object JavaDoc.class));
128         assertTrue(allTypes.contains(MockEndpoint.class));
129         assertTrue(allTypes.contains(MockParentInterface1.class));
130         assertTrue(allTypes.contains(MockParentInterface2.class));
131         assertTrue(allTypes.contains(MockChildInterface1.class));
132         assertTrue(allTypes.contains(MockChildInterface2.class));
133         assertFalse(allTypes.contains(Comparable JavaDoc.class));
134     }
135
136     public void testReduceInterfaces() throws Exception JavaDoc {
137         Set JavaDoc types = new LinkedHashSet JavaDoc();
138
139         // single class
140
types.add(MockGBean.class);
141         types = ClassLoading.reduceInterfaces(types);
142         assertTrue(types.contains(MockGBean.class));
143         assertFalse(types.contains(Object JavaDoc.class));
144         assertFalse(types.contains(MockEndpoint.class));
145         assertFalse(types.contains(MockParentInterface1.class));
146         assertFalse(types.contains(MockParentInterface2.class));
147         assertFalse(types.contains(MockChildInterface1.class));
148         assertFalse(types.contains(MockChildInterface2.class));
149         assertFalse(types.contains(Comparable JavaDoc.class));
150
151         // all types
152
types = ClassLoading.getAllTypes(MockGBean.class);
153         types = ClassLoading.reduceInterfaces(types);
154         assertTrue(types.contains(MockGBean.class));
155         assertFalse(types.contains(Object JavaDoc.class));
156         assertFalse(types.contains(MockEndpoint.class));
157         assertFalse(types.contains(MockParentInterface1.class));
158         assertFalse(types.contains(MockParentInterface2.class));
159         assertFalse(types.contains(MockChildInterface1.class));
160         assertFalse(types.contains(MockChildInterface2.class));
161         assertFalse(types.contains(Comparable JavaDoc.class));
162
163         // double all types
164
types = ClassLoading.getAllTypes(MockGBean.class);
165         types.addAll(ClassLoading.getAllTypes(MockGBean.class));
166         types = ClassLoading.reduceInterfaces(types);
167         assertTrue(types.contains(MockGBean.class));
168         assertFalse(types.contains(Object JavaDoc.class));
169         assertFalse(types.contains(MockEndpoint.class));
170         assertFalse(types.contains(MockParentInterface1.class));
171         assertFalse(types.contains(MockParentInterface2.class));
172         assertFalse(types.contains(MockChildInterface1.class));
173         assertFalse(types.contains(MockChildInterface2.class));
174         assertFalse(types.contains(Comparable JavaDoc.class));
175
176         // extra interfaces
177
types = ClassLoading.getAllTypes(MockGBean.class);
178         types.addAll(ClassLoading.getAllTypes(Kernel.class));
179         types.addAll(ClassLoading.getAllTypes(Serializable JavaDoc.class));
180         types = ClassLoading.reduceInterfaces(types);
181         assertTrue(types.contains(Kernel.class));
182         assertTrue(types.contains(Serializable JavaDoc.class));
183         assertTrue(types.contains(MockGBean.class));
184         assertFalse(types.contains(Object JavaDoc.class));
185         assertFalse(types.contains(MockEndpoint.class));
186         assertFalse(types.contains(MockParentInterface1.class));
187         assertFalse(types.contains(MockParentInterface2.class));
188         assertFalse(types.contains(MockChildInterface1.class));
189         assertFalse(types.contains(MockChildInterface2.class));
190         assertFalse(types.contains(Comparable JavaDoc.class));
191
192         // two different types
193
types = ClassLoading.getAllTypes(MockGBean.class);
194         try {
195             types.addAll(ClassLoading.getAllTypes(BasicKernel.class));
196         } catch (IllegalArgumentException JavaDoc e) {
197             // expected
198
}
199     }
200
201     private Class JavaDoc loadClass(String JavaDoc name) {
202         Class JavaDoc type = null;
203
204         try {
205             type = ClassLoading.loadClass(name, getClass().getClassLoader());
206         } catch (ClassNotFoundException JavaDoc e) {
207             fail("Class should have been found: " + e);
208         }
209
210         assertNotNull(type);
211
212         return type;
213     }
214 }
215
Popular Tags