KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > ClassUtilsTest


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

16 package org.apache.commons.lang;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URLClassLoader JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29 import junit.textui.TestRunner;
30
31 /**
32  * Unit tests {@link org.apache.commons.lang.ClassUtils}.
33  *
34  * @author Stephen Colebourne
35  * @author Gary D. Gregory
36  * @version $Id: ClassUtilsTest.java 179242 2005-05-31 17:48:08Z ggregory $
37  */

38 public class ClassUtilsTest extends TestCase {
39
40     public ClassUtilsTest(String JavaDoc name) {
41         super(name);
42     }
43
44     public static void main(String JavaDoc[] args) {
45         TestRunner.run(suite());
46     }
47
48     public static Test suite() {
49         TestSuite suite = new TestSuite(ClassUtilsTest.class);
50         suite.setName("ClassUtils Tests");
51         return suite;
52     }
53
54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56     }
57
58     protected void tearDown() throws Exception JavaDoc {
59         super.tearDown();
60     }
61
62     private static class Inner {
63     }
64     
65     //-----------------------------------------------------------------------
66
public void testConstructor() {
67         assertNotNull(new ClassUtils());
68         Constructor JavaDoc[] cons = ClassUtils.class.getDeclaredConstructors();
69         assertEquals(1, cons.length);
70         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
71         assertEquals(true, Modifier.isPublic(ClassUtils.class.getModifiers()));
72         assertEquals(false, Modifier.isFinal(ClassUtils.class.getModifiers()));
73     }
74     
75     // -------------------------------------------------------------------------
76
public void test_getShortClassName_Object() {
77         assertEquals("ClassUtils", ClassUtils.getShortClassName(new ClassUtils(), "<null>"));
78         assertEquals("ClassUtilsTest.Inner", ClassUtils.getShortClassName(new Inner(), "<null>"));
79         assertEquals("String", ClassUtils.getShortClassName("hello", "<null>"));
80         assertEquals("<null>", ClassUtils.getShortClassName(null, "<null>"));
81     }
82     
83     public void test_getShortClassName_Class() {
84         assertEquals("ClassUtils", ClassUtils.getShortClassName(ClassUtils.class));
85         assertEquals("Map.Entry", ClassUtils.getShortClassName(Map.Entry JavaDoc.class));
86         assertEquals("", ClassUtils.getShortClassName((Class JavaDoc) null));
87     }
88     
89     public void test_getShortClassName_String() {
90         assertEquals("ClassUtils", ClassUtils.getShortClassName(ClassUtils.class.getName()));
91         assertEquals("Map.Entry", ClassUtils.getShortClassName(Map.Entry JavaDoc.class.getName()));
92         assertEquals("", ClassUtils.getShortClassName((String JavaDoc) null));
93         assertEquals("", ClassUtils.getShortClassName(""));
94     }
95     
96     // -------------------------------------------------------------------------
97
public void test_getPackageName_Object() {
98         assertEquals("org.apache.commons.lang", ClassUtils.getPackageName(new ClassUtils(), "<null>"));
99         assertEquals("org.apache.commons.lang", ClassUtils.getPackageName(new Inner(), "<null>"));
100         assertEquals("<null>", ClassUtils.getPackageName(null, "<null>"));
101     }
102     
103     public void test_getPackageName_Class() {
104         assertEquals("java.lang", ClassUtils.getPackageName(String JavaDoc.class));
105         assertEquals("java.util", ClassUtils.getPackageName(Map.Entry JavaDoc.class));
106         assertEquals("", ClassUtils.getPackageName((Class JavaDoc)null));
107     }
108     
109     public void test_getPackageName_String() {
110         assertEquals("org.apache.commons.lang", ClassUtils.getPackageName(ClassUtils.class.getName()));
111         assertEquals("java.util", ClassUtils.getPackageName(Map.Entry JavaDoc.class.getName()));
112         assertEquals("", ClassUtils.getPackageName((String JavaDoc)null));
113         assertEquals("", ClassUtils.getPackageName(""));
114     }
115     
116     // -------------------------------------------------------------------------
117
public void test_getAllSuperclasses_Class() {
118         List JavaDoc list = ClassUtils.getAllSuperclasses(CY.class);
119         assertEquals(2, list.size());
120         assertEquals(CX.class, list.get(0));
121         assertEquals(Object JavaDoc.class, list.get(1));
122         
123         assertEquals(null, ClassUtils.getAllSuperclasses(null));
124     }
125     
126     public void test_getAllInterfaces_Class() {
127         List JavaDoc list = ClassUtils.getAllInterfaces(CY.class);
128         assertEquals(6, list.size());
129         assertEquals(IB.class, list.get(0));
130         assertEquals(IC.class, list.get(1));
131         assertEquals(ID.class, list.get(2));
132         assertEquals(IE.class, list.get(3));
133         assertEquals(IF.class, list.get(4));
134         assertEquals(IA.class, list.get(5));
135         
136         assertEquals(null, ClassUtils.getAllInterfaces(null));
137     }
138     
139     private static interface IA {
140     }
141     private static interface IB {
142     }
143     private static interface IC extends ID, IE {
144     }
145     private static interface ID {
146     }
147     private static interface IE extends IF {
148     }
149     private static interface IF {
150     }
151     private static class CX implements IB, IA, IE {
152     }
153     private static class CY extends CX implements IB, IC {
154     }
155     
156     // -------------------------------------------------------------------------
157
public void test_convertClassNamesToClasses_List() {
158         List JavaDoc list = new ArrayList JavaDoc();
159         List JavaDoc result = ClassUtils.convertClassNamesToClasses(list);
160         assertEquals(0, result.size());
161         
162         list.add("java.lang.String");
163         list.add("java.lang.xxx");
164         list.add("java.lang.Object");
165         result = ClassUtils.convertClassNamesToClasses(list);
166         assertEquals(3, result.size());
167         assertEquals(String JavaDoc.class, result.get(0));
168         assertEquals(null, result.get(1));
169         assertEquals(Object JavaDoc.class, result.get(2));
170
171         list.add(new Object JavaDoc());
172         try {
173             ClassUtils.convertClassNamesToClasses(list);
174             fail();
175         } catch (ClassCastException JavaDoc ex) {}
176         assertEquals(null, ClassUtils.convertClassNamesToClasses(null));
177     }
178     
179     public void test_convertClassesToClassNames_List() {
180         List JavaDoc list = new ArrayList JavaDoc();
181         List JavaDoc result = ClassUtils.convertClassesToClassNames(list);
182         assertEquals(0, result.size());
183         
184         list.add(String JavaDoc.class);
185         list.add(null);
186         list.add(Object JavaDoc.class);
187         result = ClassUtils.convertClassesToClassNames(list);
188         assertEquals(3, result.size());
189         assertEquals("java.lang.String", result.get(0));
190         assertEquals(null, result.get(1));
191         assertEquals("java.lang.Object", result.get(2));
192
193         list.add(new Object JavaDoc());
194         try {
195             ClassUtils.convertClassesToClassNames(list);
196             fail();
197         } catch (ClassCastException JavaDoc ex) {}
198         assertEquals(null, ClassUtils.convertClassesToClassNames(null));
199     }
200     
201     // -------------------------------------------------------------------------
202
public void test_isInnerClass_Class() {
203         assertEquals(true, ClassUtils.isInnerClass(Inner.class));
204         assertEquals(true, ClassUtils.isInnerClass(Map.Entry JavaDoc.class));
205         assertEquals(true, ClassUtils.isInnerClass(new Cloneable JavaDoc() {
206         }.getClass()));
207         assertEquals(false, ClassUtils.isInnerClass(this.getClass()));
208         assertEquals(false, ClassUtils.isInnerClass(String JavaDoc.class));
209         assertEquals(false, ClassUtils.isInnerClass(null));
210     }
211     
212     // -------------------------------------------------------------------------
213
public void test_isAssignable_ClassArray_ClassArray() throws Exception JavaDoc {
214         Class JavaDoc[] array2 = new Class JavaDoc[] {Object JavaDoc.class, Object JavaDoc.class};
215         Class JavaDoc[] array1 = new Class JavaDoc[] {Object JavaDoc.class};
216         Class JavaDoc[] array1s = new Class JavaDoc[] {String JavaDoc.class};
217         Class JavaDoc[] array0 = new Class JavaDoc[] {};
218
219         assertEquals(false, ClassUtils.isAssignable(array1, array2));
220         assertEquals(false, ClassUtils.isAssignable(null, array2));
221         assertEquals(true, ClassUtils.isAssignable(null, array0));
222         assertEquals(true, ClassUtils.isAssignable(array0, array0));
223         assertEquals(true, ClassUtils.isAssignable(array0, null));
224         assertEquals(true, ClassUtils.isAssignable((Class JavaDoc[]) null, (Class JavaDoc[]) null));
225         
226         assertEquals(false, ClassUtils.isAssignable(array1, array1s));
227         assertEquals(true, ClassUtils.isAssignable(array1s, array1s));
228         assertEquals(true, ClassUtils.isAssignable(array1s, array1));
229     }
230     
231     public void test_isAssignable() throws Exception JavaDoc {
232         assertEquals(false, ClassUtils.isAssignable((Class JavaDoc) null, null));
233         assertEquals(false, ClassUtils.isAssignable(String JavaDoc.class, null));
234         
235         assertEquals(true, ClassUtils.isAssignable(null, Object JavaDoc.class));
236         assertEquals(true, ClassUtils.isAssignable(null, Integer JavaDoc.class));
237         assertEquals(false, ClassUtils.isAssignable(null, Integer.TYPE));
238         assertEquals(true, ClassUtils.isAssignable(String JavaDoc.class, Object JavaDoc.class));
239         assertEquals(true, ClassUtils.isAssignable(String JavaDoc.class, String JavaDoc.class));
240         assertEquals(false, ClassUtils.isAssignable(Object JavaDoc.class, String JavaDoc.class));
241         assertEquals(false, ClassUtils.isAssignable(Integer.TYPE, Integer JavaDoc.class));
242         assertEquals(false, ClassUtils.isAssignable(Integer JavaDoc.class, Integer.TYPE));
243         assertEquals(true, ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE));
244         assertEquals(true, ClassUtils.isAssignable(Integer JavaDoc.class, Integer JavaDoc.class));
245     }
246     
247     public void test_isAssignable_Widening() throws Exception JavaDoc {
248         // test byte conversions
249
assertEquals("byte -> char", false, ClassUtils.isAssignable(Byte.TYPE, Character.TYPE));
250         assertEquals("byte -> byte", true, ClassUtils.isAssignable(Byte.TYPE, Byte.TYPE));
251         assertEquals("byte -> short", true, ClassUtils.isAssignable(Byte.TYPE, Short.TYPE));
252         assertEquals("byte -> int", true, ClassUtils.isAssignable(Byte.TYPE, Integer.TYPE));
253         assertEquals("byte -> long", true, ClassUtils.isAssignable(Byte.TYPE, Long.TYPE));
254         assertEquals("byte -> float", true, ClassUtils.isAssignable(Byte.TYPE, Float.TYPE));
255         assertEquals("byte -> double", true, ClassUtils.isAssignable(Byte.TYPE, Double.TYPE));
256         assertEquals("byte -> boolean", false, ClassUtils.isAssignable(Byte.TYPE, Boolean.TYPE));
257         
258         // test short conversions
259
assertEquals("short -> char", false, ClassUtils.isAssignable(Short.TYPE, Character.TYPE));
260         assertEquals("short -> byte", false, ClassUtils.isAssignable(Short.TYPE, Byte.TYPE));
261         assertEquals("short -> short", true, ClassUtils.isAssignable(Short.TYPE, Short.TYPE));
262         assertEquals("short -> int", true, ClassUtils.isAssignable(Short.TYPE, Integer.TYPE));
263         assertEquals("short -> long", true, ClassUtils.isAssignable(Short.TYPE, Long.TYPE));
264         assertEquals("short -> float", true, ClassUtils.isAssignable(Short.TYPE, Float.TYPE));
265         assertEquals("short -> double", true, ClassUtils.isAssignable(Short.TYPE, Double.TYPE));
266         assertEquals("short -> boolean", false, ClassUtils.isAssignable(Short.TYPE, Boolean.TYPE));
267         
268         // test char conversions
269
assertEquals("char -> char", true, ClassUtils.isAssignable(Character.TYPE, Character.TYPE));
270         assertEquals("char -> byte", false, ClassUtils.isAssignable(Character.TYPE, Byte.TYPE));
271         assertEquals("char -> short", false, ClassUtils.isAssignable(Character.TYPE, Short.TYPE));
272         assertEquals("char -> int", true, ClassUtils.isAssignable(Character.TYPE, Integer.TYPE));
273         assertEquals("char -> long", true, ClassUtils.isAssignable(Character.TYPE, Long.TYPE));
274         assertEquals("char -> float", true, ClassUtils.isAssignable(Character.TYPE, Float.TYPE));
275         assertEquals("char -> double", true, ClassUtils.isAssignable(Character.TYPE, Double.TYPE));
276         assertEquals("char -> boolean", false, ClassUtils.isAssignable(Character.TYPE, Boolean.TYPE));
277         
278         // test int conversions
279
assertEquals("int -> char", false, ClassUtils.isAssignable(Integer.TYPE, Character.TYPE));
280         assertEquals("int -> byte", false, ClassUtils.isAssignable(Integer.TYPE, Byte.TYPE));
281         assertEquals("int -> short", false, ClassUtils.isAssignable(Integer.TYPE, Short.TYPE));
282         assertEquals("int -> int", true, ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE));
283         assertEquals("int -> long", true, ClassUtils.isAssignable(Integer.TYPE, Long.TYPE));
284         assertEquals("int -> float", true, ClassUtils.isAssignable(Integer.TYPE, Float.TYPE));
285         assertEquals("int -> double", true, ClassUtils.isAssignable(Integer.TYPE, Double.TYPE));
286         assertEquals("int -> boolean", false, ClassUtils.isAssignable(Integer.TYPE, Boolean.TYPE));
287  
288         // test long conversions
289
assertEquals("long -> char", false, ClassUtils.isAssignable(Long.TYPE, Character.TYPE));
290         assertEquals("long -> byte", false, ClassUtils.isAssignable(Long.TYPE, Byte.TYPE));
291         assertEquals("long -> short", false, ClassUtils.isAssignable(Long.TYPE, Short.TYPE));
292         assertEquals("long -> int", false, ClassUtils.isAssignable(Long.TYPE, Integer.TYPE));
293         assertEquals("long -> long", true, ClassUtils.isAssignable(Long.TYPE, Long.TYPE));
294         assertEquals("long -> float", true, ClassUtils.isAssignable(Long.TYPE, Float.TYPE));
295         assertEquals("long -> double", true, ClassUtils.isAssignable(Long.TYPE, Double.TYPE));
296         assertEquals("long -> boolean", false, ClassUtils.isAssignable(Long.TYPE, Boolean.TYPE));
297  
298         // test float conversions
299
assertEquals("float -> char", false, ClassUtils.isAssignable(Float.TYPE, Character.TYPE));
300         assertEquals("float -> byte", false, ClassUtils.isAssignable(Float.TYPE, Byte.TYPE));
301         assertEquals("float -> short", false, ClassUtils.isAssignable(Float.TYPE, Short.TYPE));
302         assertEquals("float -> int", false, ClassUtils.isAssignable(Float.TYPE, Integer.TYPE));
303         assertEquals("float -> long", false, ClassUtils.isAssignable(Float.TYPE, Long.TYPE));
304         assertEquals("float -> float", true, ClassUtils.isAssignable(Float.TYPE, Float.TYPE));
305         assertEquals("float -> double", true, ClassUtils.isAssignable(Float.TYPE, Double.TYPE));
306         assertEquals("float -> boolean", false, ClassUtils.isAssignable(Float.TYPE, Boolean.TYPE));
307         
308         // test float conversions
309
assertEquals("double -> char", false, ClassUtils.isAssignable(Double.TYPE, Character.TYPE));
310         assertEquals("double -> byte", false, ClassUtils.isAssignable(Double.TYPE, Byte.TYPE));
311         assertEquals("double -> short", false, ClassUtils.isAssignable(Double.TYPE, Short.TYPE));
312         assertEquals("double -> int", false, ClassUtils.isAssignable(Double.TYPE, Integer.TYPE));
313         assertEquals("double -> long", false, ClassUtils.isAssignable(Double.TYPE, Long.TYPE));
314         assertEquals("double -> float", false, ClassUtils.isAssignable(Double.TYPE, Float.TYPE));
315         assertEquals("double -> double", true, ClassUtils.isAssignable(Double.TYPE, Double.TYPE));
316         assertEquals("double -> boolean", false, ClassUtils.isAssignable(Double.TYPE, Boolean.TYPE));
317         
318         // test float conversions
319
assertEquals("boolean -> char", false, ClassUtils.isAssignable(Boolean.TYPE, Character.TYPE));
320         assertEquals("boolean -> byte", false, ClassUtils.isAssignable(Boolean.TYPE, Byte.TYPE));
321         assertEquals("boolean -> short", false, ClassUtils.isAssignable(Boolean.TYPE, Short.TYPE));
322         assertEquals("boolean -> int", false, ClassUtils.isAssignable(Boolean.TYPE, Integer.TYPE));
323         assertEquals("boolean -> long", false, ClassUtils.isAssignable(Boolean.TYPE, Long.TYPE));
324         assertEquals("boolean -> float", false, ClassUtils.isAssignable(Boolean.TYPE, Float.TYPE));
325         assertEquals("boolean -> double", false, ClassUtils.isAssignable(Boolean.TYPE, Double.TYPE));
326         assertEquals("boolean -> boolean", true, ClassUtils.isAssignable(Boolean.TYPE, Boolean.TYPE));
327     }
328     
329     public void testPrimitiveToWrapper() {
330        
331         // test primitive classes
332
assertEquals("boolean -> Boolean.class",
333             Boolean JavaDoc.class, ClassUtils.primitiveToWrapper(Boolean.TYPE));
334         assertEquals("byte -> Byte.class",
335             Byte JavaDoc.class, ClassUtils.primitiveToWrapper(Byte.TYPE));
336         assertEquals("char -> Character.class",
337             Character JavaDoc.class, ClassUtils.primitiveToWrapper(Character.TYPE));
338         assertEquals("short -> Short.class",
339             Short JavaDoc.class, ClassUtils.primitiveToWrapper(Short.TYPE));
340         assertEquals("int -> Integer.class",
341             Integer JavaDoc.class, ClassUtils.primitiveToWrapper(Integer.TYPE));
342         assertEquals("long -> Long.class",
343             Long JavaDoc.class, ClassUtils.primitiveToWrapper(Long.TYPE));
344         assertEquals("double -> Double.class",
345             Double JavaDoc.class, ClassUtils.primitiveToWrapper(Double.TYPE));
346         assertEquals("float -> Float.class",
347             Float JavaDoc.class, ClassUtils.primitiveToWrapper(Float.TYPE));
348         
349         // test a few other classes
350
assertEquals("String.class -> String.class",
351             String JavaDoc.class, ClassUtils.primitiveToWrapper(String JavaDoc.class));
352         assertEquals("ClassUtils.class -> ClassUtils.class",
353             org.apache.commons.lang.ClassUtils.class,
354             ClassUtils.primitiveToWrapper(org.apache.commons.lang.ClassUtils.class));
355             
356         // test null
357
assertNull("null -> null",
358             ClassUtils.primitiveToWrapper(null));
359     }
360
361     public void testPrimitivesToWrappers() {
362         // test null
363
assertNull("null -> null",
364             ClassUtils.primitivesToWrappers(null));
365         // test empty array
366
assertEquals("empty -> empty",
367                 ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.primitivesToWrappers(ArrayUtils.EMPTY_CLASS_ARRAY));
368
369         // test an array of various classes
370
final Class JavaDoc[] primitives = new Class JavaDoc[] {
371                 Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE,
372                 Integer.TYPE, Long.TYPE, Double.TYPE, Float.TYPE,
373                 String JavaDoc.class, ClassUtils.class
374         };
375         Class JavaDoc[] wrappers= ClassUtils.primitivesToWrappers(primitives);
376         
377         for (int i=0; i < primitives.length; i++) {
378             // test each returned wrapper
379
Class JavaDoc primitive = primitives[i];
380             Class JavaDoc expectedWrapper = ClassUtils.primitiveToWrapper(primitive);
381             
382             assertEquals(primitive + " -> " + expectedWrapper, expectedWrapper, wrappers[i]);
383         }
384
385         // test an array of no primitive classes
386
final Class JavaDoc[] noPrimitives = new Class JavaDoc[] {
387                 String JavaDoc.class, ClassUtils.class
388         };
389         // This used to return the exact same array, but no longer does.
390
assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives));
391     }
392
393     /**
394      * Creates a new instance of URLClassLoader with the system class loader's URLs and a <code>null</code> parent
395      * class loader.
396      *
397      * @see ClassLoader#getSystemClassLoader()
398      * @see URLClassLoader#newInstance(URL[], ClassLoader)
399      * @return the resulting class loader
400      */

401     public static ClassLoader JavaDoc newSystemClassLoader() throws SecurityException JavaDoc, IllegalArgumentException JavaDoc {
402         ClassLoader JavaDoc systemClassLoader = ClassLoader.getSystemClassLoader();
403         ClassLoader JavaDoc myClassLoader = ClassUtilsTest.class.getClassLoader();
404         if (!(myClassLoader instanceof URLClassLoader JavaDoc)) {
405             fail("Need a better test set up.");
406         }
407         if (!(systemClassLoader instanceof URLClassLoader JavaDoc)) {
408             fail("Need a better test set up.");
409         }
410         if (!myClassLoader.equals(systemClassLoader)) {
411             fail("Need a better test set up?");
412         }
413         URLClassLoader JavaDoc urlScl = (URLClassLoader JavaDoc) myClassLoader;
414         return URLClassLoader.newInstance(urlScl.getURLs(), null);
415     }
416
417 }
418
Popular Tags