001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: import java.lang.reflect.Constructor;
020: import java.lang.reflect.Method;
021: import java.lang.reflect.Modifier;
022: import java.net.URL;
023: import java.net.URLClassLoader;
024: import java.util.ArrayList;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Set;
028: import java.util.HashSet;
029: import java.util.Collections;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034: import junit.textui.TestRunner;
035:
036: /**
037: * Unit tests {@link org.apache.commons.lang.ClassUtils}.
038: *
039: * @author Stephen Colebourne
040: * @author Gary D. Gregory
041: * @version $Id: ClassUtilsTest.java 501606 2007-01-30 22:26:38Z bayard $
042: */
043: public class ClassUtilsTest extends TestCase {
044:
045: public ClassUtilsTest(String name) {
046: super (name);
047: }
048:
049: public static void main(String[] args) {
050: TestRunner.run(suite());
051: }
052:
053: public static Test suite() {
054: TestSuite suite = new TestSuite(ClassUtilsTest.class);
055: suite.setName("ClassUtils Tests");
056: return suite;
057: }
058:
059: protected void setUp() throws Exception {
060: super .setUp();
061: }
062:
063: protected void tearDown() throws Exception {
064: super .tearDown();
065: }
066:
067: private static class Inner {
068: }
069:
070: //-----------------------------------------------------------------------
071: public void testConstructor() {
072: assertNotNull(new ClassUtils());
073: Constructor[] cons = ClassUtils.class.getDeclaredConstructors();
074: assertEquals(1, cons.length);
075: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
076: assertEquals(true, Modifier.isPublic(ClassUtils.class
077: .getModifiers()));
078: assertEquals(false, Modifier.isFinal(ClassUtils.class
079: .getModifiers()));
080: }
081:
082: // -------------------------------------------------------------------------
083: public void test_getShortClassName_Object() {
084: assertEquals("ClassUtils", ClassUtils.getShortClassName(
085: new ClassUtils(), "<null>"));
086: assertEquals("ClassUtilsTest.Inner", ClassUtils
087: .getShortClassName(new Inner(), "<null>"));
088: assertEquals("String", ClassUtils.getShortClassName("hello",
089: "<null>"));
090: assertEquals("<null>", ClassUtils.getShortClassName(null,
091: "<null>"));
092: }
093:
094: public void test_getShortClassName_Class() {
095: assertEquals("ClassUtils", ClassUtils
096: .getShortClassName(ClassUtils.class));
097: assertEquals("Map.Entry", ClassUtils
098: .getShortClassName(Map.Entry.class));
099: assertEquals("", ClassUtils.getShortClassName((Class) null));
100: }
101:
102: public void test_getShortClassName_String() {
103: assertEquals("ClassUtils", ClassUtils
104: .getShortClassName(ClassUtils.class.getName()));
105: assertEquals("Map.Entry", ClassUtils
106: .getShortClassName(Map.Entry.class.getName()));
107: assertEquals("", ClassUtils.getShortClassName((String) null));
108: assertEquals("", ClassUtils.getShortClassName(""));
109: }
110:
111: // -------------------------------------------------------------------------
112: public void test_getPackageName_Object() {
113: assertEquals("org.apache.commons.lang", ClassUtils
114: .getPackageName(new ClassUtils(), "<null>"));
115: assertEquals("org.apache.commons.lang", ClassUtils
116: .getPackageName(new Inner(), "<null>"));
117: assertEquals("<null>", ClassUtils
118: .getPackageName(null, "<null>"));
119: }
120:
121: public void test_getPackageName_Class() {
122: assertEquals("java.lang", ClassUtils
123: .getPackageName(String.class));
124: assertEquals("java.util", ClassUtils
125: .getPackageName(Map.Entry.class));
126: assertEquals("", ClassUtils.getPackageName((Class) null));
127: }
128:
129: public void test_getPackageName_String() {
130: assertEquals("org.apache.commons.lang", ClassUtils
131: .getPackageName(ClassUtils.class.getName()));
132: assertEquals("java.util", ClassUtils
133: .getPackageName(Map.Entry.class.getName()));
134: assertEquals("", ClassUtils.getPackageName((String) null));
135: assertEquals("", ClassUtils.getPackageName(""));
136: }
137:
138: // -------------------------------------------------------------------------
139: public void test_getAllSuperclasses_Class() {
140: List list = ClassUtils.getAllSuperclasses(CY.class);
141: assertEquals(2, list.size());
142: assertEquals(CX.class, list.get(0));
143: assertEquals(Object.class, list.get(1));
144:
145: assertEquals(null, ClassUtils.getAllSuperclasses(null));
146: }
147:
148: public void test_getAllInterfaces_Class() {
149: List list = ClassUtils.getAllInterfaces(CY.class);
150: assertEquals(6, list.size());
151: assertEquals(IB.class, list.get(0));
152: assertEquals(IC.class, list.get(1));
153: assertEquals(ID.class, list.get(2));
154: assertEquals(IE.class, list.get(3));
155: assertEquals(IF.class, list.get(4));
156: assertEquals(IA.class, list.get(5));
157:
158: assertEquals(null, ClassUtils.getAllInterfaces(null));
159: }
160:
161: private static interface IA {
162: }
163:
164: private static interface IB {
165: }
166:
167: private static interface IC extends ID, IE {
168: }
169:
170: private static interface ID {
171: }
172:
173: private static interface IE extends IF {
174: }
175:
176: private static interface IF {
177: }
178:
179: private static class CX implements IB, IA, IE {
180: }
181:
182: private static class CY extends CX implements IB, IC {
183: }
184:
185: // -------------------------------------------------------------------------
186: public void test_convertClassNamesToClasses_List() {
187: List list = new ArrayList();
188: List result = ClassUtils.convertClassNamesToClasses(list);
189: assertEquals(0, result.size());
190:
191: list.add("java.lang.String");
192: list.add("java.lang.xxx");
193: list.add("java.lang.Object");
194: result = ClassUtils.convertClassNamesToClasses(list);
195: assertEquals(3, result.size());
196: assertEquals(String.class, result.get(0));
197: assertEquals(null, result.get(1));
198: assertEquals(Object.class, result.get(2));
199:
200: list.add(new Object());
201: try {
202: ClassUtils.convertClassNamesToClasses(list);
203: fail();
204: } catch (ClassCastException ex) {
205: }
206: assertEquals(null, ClassUtils.convertClassNamesToClasses(null));
207: }
208:
209: public void test_convertClassesToClassNames_List() {
210: List list = new ArrayList();
211: List result = ClassUtils.convertClassesToClassNames(list);
212: assertEquals(0, result.size());
213:
214: list.add(String.class);
215: list.add(null);
216: list.add(Object.class);
217: result = ClassUtils.convertClassesToClassNames(list);
218: assertEquals(3, result.size());
219: assertEquals("java.lang.String", result.get(0));
220: assertEquals(null, result.get(1));
221: assertEquals("java.lang.Object", result.get(2));
222:
223: list.add(new Object());
224: try {
225: ClassUtils.convertClassesToClassNames(list);
226: fail();
227: } catch (ClassCastException ex) {
228: }
229: assertEquals(null, ClassUtils.convertClassesToClassNames(null));
230: }
231:
232: // -------------------------------------------------------------------------
233: public void test_isInnerClass_Class() {
234: assertEquals(true, ClassUtils.isInnerClass(Inner.class));
235: assertEquals(true, ClassUtils.isInnerClass(Map.Entry.class));
236: assertEquals(true, ClassUtils.isInnerClass(new Cloneable() {
237: }.getClass()));
238: assertEquals(false, ClassUtils.isInnerClass(this .getClass()));
239: assertEquals(false, ClassUtils.isInnerClass(String.class));
240: assertEquals(false, ClassUtils.isInnerClass(null));
241: }
242:
243: // -------------------------------------------------------------------------
244: public void test_isAssignable_ClassArray_ClassArray()
245: throws Exception {
246: Class[] array2 = new Class[] { Object.class, Object.class };
247: Class[] array1 = new Class[] { Object.class };
248: Class[] array1s = new Class[] { String.class };
249: Class[] array0 = new Class[] {};
250:
251: assertEquals(false, ClassUtils.isAssignable(array1, array2));
252: assertEquals(false, ClassUtils.isAssignable(null, array2));
253: assertEquals(true, ClassUtils.isAssignable(null, array0));
254: assertEquals(true, ClassUtils.isAssignable(array0, array0));
255: assertEquals(true, ClassUtils.isAssignable(array0, null));
256: assertEquals(true, ClassUtils.isAssignable((Class[]) null,
257: (Class[]) null));
258:
259: assertEquals(false, ClassUtils.isAssignable(array1, array1s));
260: assertEquals(true, ClassUtils.isAssignable(array1s, array1s));
261: assertEquals(true, ClassUtils.isAssignable(array1s, array1));
262: }
263:
264: public void test_isAssignable() throws Exception {
265: assertEquals(false, ClassUtils.isAssignable((Class) null, null));
266: assertEquals(false, ClassUtils.isAssignable(String.class, null));
267:
268: assertEquals(true, ClassUtils.isAssignable(null, Object.class));
269: assertEquals(true, ClassUtils.isAssignable(null, Integer.class));
270: assertEquals(false, ClassUtils.isAssignable(null, Integer.TYPE));
271: assertEquals(true, ClassUtils.isAssignable(String.class,
272: Object.class));
273: assertEquals(true, ClassUtils.isAssignable(String.class,
274: String.class));
275: assertEquals(false, ClassUtils.isAssignable(Object.class,
276: String.class));
277: assertEquals(false, ClassUtils.isAssignable(Integer.TYPE,
278: Integer.class));
279: assertEquals(false, ClassUtils.isAssignable(Integer.class,
280: Integer.TYPE));
281: assertEquals(true, ClassUtils.isAssignable(Integer.TYPE,
282: Integer.TYPE));
283: assertEquals(true, ClassUtils.isAssignable(Integer.class,
284: Integer.class));
285: }
286:
287: public void test_isAssignable_Widening() throws Exception {
288: // test byte conversions
289: assertEquals("byte -> char", false, ClassUtils.isAssignable(
290: Byte.TYPE, Character.TYPE));
291: assertEquals("byte -> byte", true, ClassUtils.isAssignable(
292: Byte.TYPE, Byte.TYPE));
293: assertEquals("byte -> short", true, ClassUtils.isAssignable(
294: Byte.TYPE, Short.TYPE));
295: assertEquals("byte -> int", true, ClassUtils.isAssignable(
296: Byte.TYPE, Integer.TYPE));
297: assertEquals("byte -> long", true, ClassUtils.isAssignable(
298: Byte.TYPE, Long.TYPE));
299: assertEquals("byte -> float", true, ClassUtils.isAssignable(
300: Byte.TYPE, Float.TYPE));
301: assertEquals("byte -> double", true, ClassUtils.isAssignable(
302: Byte.TYPE, Double.TYPE));
303: assertEquals("byte -> boolean", false, ClassUtils.isAssignable(
304: Byte.TYPE, Boolean.TYPE));
305:
306: // test short conversions
307: assertEquals("short -> char", false, ClassUtils.isAssignable(
308: Short.TYPE, Character.TYPE));
309: assertEquals("short -> byte", false, ClassUtils.isAssignable(
310: Short.TYPE, Byte.TYPE));
311: assertEquals("short -> short", true, ClassUtils.isAssignable(
312: Short.TYPE, Short.TYPE));
313: assertEquals("short -> int", true, ClassUtils.isAssignable(
314: Short.TYPE, Integer.TYPE));
315: assertEquals("short -> long", true, ClassUtils.isAssignable(
316: Short.TYPE, Long.TYPE));
317: assertEquals("short -> float", true, ClassUtils.isAssignable(
318: Short.TYPE, Float.TYPE));
319: assertEquals("short -> double", true, ClassUtils.isAssignable(
320: Short.TYPE, Double.TYPE));
321: assertEquals("short -> boolean", false, ClassUtils
322: .isAssignable(Short.TYPE, Boolean.TYPE));
323:
324: // test char conversions
325: assertEquals("char -> char", true, ClassUtils.isAssignable(
326: Character.TYPE, Character.TYPE));
327: assertEquals("char -> byte", false, ClassUtils.isAssignable(
328: Character.TYPE, Byte.TYPE));
329: assertEquals("char -> short", false, ClassUtils.isAssignable(
330: Character.TYPE, Short.TYPE));
331: assertEquals("char -> int", true, ClassUtils.isAssignable(
332: Character.TYPE, Integer.TYPE));
333: assertEquals("char -> long", true, ClassUtils.isAssignable(
334: Character.TYPE, Long.TYPE));
335: assertEquals("char -> float", true, ClassUtils.isAssignable(
336: Character.TYPE, Float.TYPE));
337: assertEquals("char -> double", true, ClassUtils.isAssignable(
338: Character.TYPE, Double.TYPE));
339: assertEquals("char -> boolean", false, ClassUtils.isAssignable(
340: Character.TYPE, Boolean.TYPE));
341:
342: // test int conversions
343: assertEquals("int -> char", false, ClassUtils.isAssignable(
344: Integer.TYPE, Character.TYPE));
345: assertEquals("int -> byte", false, ClassUtils.isAssignable(
346: Integer.TYPE, Byte.TYPE));
347: assertEquals("int -> short", false, ClassUtils.isAssignable(
348: Integer.TYPE, Short.TYPE));
349: assertEquals("int -> int", true, ClassUtils.isAssignable(
350: Integer.TYPE, Integer.TYPE));
351: assertEquals("int -> long", true, ClassUtils.isAssignable(
352: Integer.TYPE, Long.TYPE));
353: assertEquals("int -> float", true, ClassUtils.isAssignable(
354: Integer.TYPE, Float.TYPE));
355: assertEquals("int -> double", true, ClassUtils.isAssignable(
356: Integer.TYPE, Double.TYPE));
357: assertEquals("int -> boolean", false, ClassUtils.isAssignable(
358: Integer.TYPE, Boolean.TYPE));
359:
360: // test long conversions
361: assertEquals("long -> char", false, ClassUtils.isAssignable(
362: Long.TYPE, Character.TYPE));
363: assertEquals("long -> byte", false, ClassUtils.isAssignable(
364: Long.TYPE, Byte.TYPE));
365: assertEquals("long -> short", false, ClassUtils.isAssignable(
366: Long.TYPE, Short.TYPE));
367: assertEquals("long -> int", false, ClassUtils.isAssignable(
368: Long.TYPE, Integer.TYPE));
369: assertEquals("long -> long", true, ClassUtils.isAssignable(
370: Long.TYPE, Long.TYPE));
371: assertEquals("long -> float", true, ClassUtils.isAssignable(
372: Long.TYPE, Float.TYPE));
373: assertEquals("long -> double", true, ClassUtils.isAssignable(
374: Long.TYPE, Double.TYPE));
375: assertEquals("long -> boolean", false, ClassUtils.isAssignable(
376: Long.TYPE, Boolean.TYPE));
377:
378: // test float conversions
379: assertEquals("float -> char", false, ClassUtils.isAssignable(
380: Float.TYPE, Character.TYPE));
381: assertEquals("float -> byte", false, ClassUtils.isAssignable(
382: Float.TYPE, Byte.TYPE));
383: assertEquals("float -> short", false, ClassUtils.isAssignable(
384: Float.TYPE, Short.TYPE));
385: assertEquals("float -> int", false, ClassUtils.isAssignable(
386: Float.TYPE, Integer.TYPE));
387: assertEquals("float -> long", false, ClassUtils.isAssignable(
388: Float.TYPE, Long.TYPE));
389: assertEquals("float -> float", true, ClassUtils.isAssignable(
390: Float.TYPE, Float.TYPE));
391: assertEquals("float -> double", true, ClassUtils.isAssignable(
392: Float.TYPE, Double.TYPE));
393: assertEquals("float -> boolean", false, ClassUtils
394: .isAssignable(Float.TYPE, Boolean.TYPE));
395:
396: // test float conversions
397: assertEquals("double -> char", false, ClassUtils.isAssignable(
398: Double.TYPE, Character.TYPE));
399: assertEquals("double -> byte", false, ClassUtils.isAssignable(
400: Double.TYPE, Byte.TYPE));
401: assertEquals("double -> short", false, ClassUtils.isAssignable(
402: Double.TYPE, Short.TYPE));
403: assertEquals("double -> int", false, ClassUtils.isAssignable(
404: Double.TYPE, Integer.TYPE));
405: assertEquals("double -> long", false, ClassUtils.isAssignable(
406: Double.TYPE, Long.TYPE));
407: assertEquals("double -> float", false, ClassUtils.isAssignable(
408: Double.TYPE, Float.TYPE));
409: assertEquals("double -> double", true, ClassUtils.isAssignable(
410: Double.TYPE, Double.TYPE));
411: assertEquals("double -> boolean", false, ClassUtils
412: .isAssignable(Double.TYPE, Boolean.TYPE));
413:
414: // test float conversions
415: assertEquals("boolean -> char", false, ClassUtils.isAssignable(
416: Boolean.TYPE, Character.TYPE));
417: assertEquals("boolean -> byte", false, ClassUtils.isAssignable(
418: Boolean.TYPE, Byte.TYPE));
419: assertEquals("boolean -> short", false, ClassUtils
420: .isAssignable(Boolean.TYPE, Short.TYPE));
421: assertEquals("boolean -> int", false, ClassUtils.isAssignable(
422: Boolean.TYPE, Integer.TYPE));
423: assertEquals("boolean -> long", false, ClassUtils.isAssignable(
424: Boolean.TYPE, Long.TYPE));
425: assertEquals("boolean -> float", false, ClassUtils
426: .isAssignable(Boolean.TYPE, Float.TYPE));
427: assertEquals("boolean -> double", false, ClassUtils
428: .isAssignable(Boolean.TYPE, Double.TYPE));
429: assertEquals("boolean -> boolean", true, ClassUtils
430: .isAssignable(Boolean.TYPE, Boolean.TYPE));
431: }
432:
433: public void testPrimitiveToWrapper() {
434:
435: // test primitive classes
436: assertEquals("boolean -> Boolean.class", Boolean.class,
437: ClassUtils.primitiveToWrapper(Boolean.TYPE));
438: assertEquals("byte -> Byte.class", Byte.class, ClassUtils
439: .primitiveToWrapper(Byte.TYPE));
440: assertEquals("char -> Character.class", Character.class,
441: ClassUtils.primitiveToWrapper(Character.TYPE));
442: assertEquals("short -> Short.class", Short.class, ClassUtils
443: .primitiveToWrapper(Short.TYPE));
444: assertEquals("int -> Integer.class", Integer.class, ClassUtils
445: .primitiveToWrapper(Integer.TYPE));
446: assertEquals("long -> Long.class", Long.class, ClassUtils
447: .primitiveToWrapper(Long.TYPE));
448: assertEquals("double -> Double.class", Double.class, ClassUtils
449: .primitiveToWrapper(Double.TYPE));
450: assertEquals("float -> Float.class", Float.class, ClassUtils
451: .primitiveToWrapper(Float.TYPE));
452:
453: // test a few other classes
454: assertEquals("String.class -> String.class", String.class,
455: ClassUtils.primitiveToWrapper(String.class));
456: assertEquals(
457: "ClassUtils.class -> ClassUtils.class",
458: org.apache.commons.lang.ClassUtils.class,
459: ClassUtils
460: .primitiveToWrapper(org.apache.commons.lang.ClassUtils.class));
461: assertEquals("Void.TYPE -> Void.TYPE", Void.TYPE, ClassUtils
462: .primitiveToWrapper(Void.TYPE));
463:
464: // test null
465: assertNull("null -> null", ClassUtils.primitiveToWrapper(null));
466: }
467:
468: public void testPrimitivesToWrappers() {
469: // test null
470: assertNull("null -> null", ClassUtils
471: .primitivesToWrappers(null));
472: // test empty array
473: assertEquals(
474: "empty -> empty",
475: ArrayUtils.EMPTY_CLASS_ARRAY,
476: ClassUtils
477: .primitivesToWrappers(ArrayUtils.EMPTY_CLASS_ARRAY));
478:
479: // test an array of various classes
480: final Class[] primitives = new Class[] { Boolean.TYPE,
481: Byte.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE,
482: Long.TYPE, Double.TYPE, Float.TYPE, String.class,
483: ClassUtils.class };
484: Class[] wrappers = ClassUtils.primitivesToWrappers(primitives);
485:
486: for (int i = 0; i < primitives.length; i++) {
487: // test each returned wrapper
488: Class primitive = primitives[i];
489: Class expectedWrapper = ClassUtils
490: .primitiveToWrapper(primitive);
491:
492: assertEquals(primitive + " -> " + expectedWrapper,
493: expectedWrapper, wrappers[i]);
494: }
495:
496: // test an array of no primitive classes
497: final Class[] noPrimitives = new Class[] { String.class,
498: ClassUtils.class, Void.TYPE };
499: // This used to return the exact same array, but no longer does.
500: assertNotSame("unmodified", noPrimitives, ClassUtils
501: .primitivesToWrappers(noPrimitives));
502: }
503:
504: public void testGetClassClassNotFound() throws Exception {
505: assertGetClassThrowsClassNotFound("bool");
506: assertGetClassThrowsClassNotFound("bool[]");
507: assertGetClassThrowsClassNotFound("integer[]");
508: }
509:
510: public void testGetClassInvalidArguments() throws Exception {
511: assertGetClassThrowsIllegalArgument(null);
512: assertGetClassThrowsClassNotFound("[][][]");
513: assertGetClassThrowsClassNotFound("[[]");
514: assertGetClassThrowsClassNotFound("[");
515: assertGetClassThrowsClassNotFound("java.lang.String][");
516: assertGetClassThrowsClassNotFound(".hello.world");
517: assertGetClassThrowsClassNotFound("hello..world");
518: }
519:
520: public void testWithInterleavingWhitespace()
521: throws ClassNotFoundException {
522: assertEquals(int[].class, ClassUtils.getClass(" int [ ] "));
523: assertEquals(long[].class, ClassUtils
524: .getClass("\rlong\t[\n]\r"));
525: assertEquals(short[].class, ClassUtils
526: .getClass("\tshort \t\t[]"));
527: assertEquals(byte[].class, ClassUtils
528: .getClass("byte[\t\t\n\r] "));
529: }
530:
531: public void testGetClassByNormalNameArrays()
532: throws ClassNotFoundException {
533: assertEquals(int[].class, ClassUtils.getClass("int[]"));
534: assertEquals(long[].class, ClassUtils.getClass("long[]"));
535: assertEquals(short[].class, ClassUtils.getClass("short[]"));
536: assertEquals(byte[].class, ClassUtils.getClass("byte[]"));
537: assertEquals(char[].class, ClassUtils.getClass("char[]"));
538: assertEquals(float[].class, ClassUtils.getClass("float[]"));
539: assertEquals(double[].class, ClassUtils.getClass("double[]"));
540: assertEquals(boolean[].class, ClassUtils.getClass("boolean[]"));
541: assertEquals(String[].class, ClassUtils
542: .getClass("java.lang.String[]"));
543: }
544:
545: public void testGetClassByNormalNameArrays2D()
546: throws ClassNotFoundException {
547: assertEquals(int[][].class, ClassUtils.getClass("int[][]"));
548: assertEquals(long[][].class, ClassUtils.getClass("long[][]"));
549: assertEquals(short[][].class, ClassUtils.getClass("short[][]"));
550: assertEquals(byte[][].class, ClassUtils.getClass("byte[][]"));
551: assertEquals(char[][].class, ClassUtils.getClass("char[][]"));
552: assertEquals(float[][].class, ClassUtils.getClass("float[][]"));
553: assertEquals(double[][].class, ClassUtils
554: .getClass("double[][]"));
555: assertEquals(boolean[][].class, ClassUtils
556: .getClass("boolean[][]"));
557: assertEquals(String[][].class, ClassUtils
558: .getClass("java.lang.String[][]"));
559: }
560:
561: public void testGetClassWithArrayClasses2D() throws Exception {
562: assertGetClassReturnsClass(String[][].class);
563: assertGetClassReturnsClass(int[][].class);
564: assertGetClassReturnsClass(long[][].class);
565: assertGetClassReturnsClass(short[][].class);
566: assertGetClassReturnsClass(byte[][].class);
567: assertGetClassReturnsClass(char[][].class);
568: assertGetClassReturnsClass(float[][].class);
569: assertGetClassReturnsClass(double[][].class);
570: assertGetClassReturnsClass(boolean[][].class);
571: }
572:
573: public void testGetClassWithArrayClasses() throws Exception {
574: assertGetClassReturnsClass(String[].class);
575: assertGetClassReturnsClass(int[].class);
576: assertGetClassReturnsClass(long[].class);
577: assertGetClassReturnsClass(short[].class);
578: assertGetClassReturnsClass(byte[].class);
579: assertGetClassReturnsClass(char[].class);
580: assertGetClassReturnsClass(float[].class);
581: assertGetClassReturnsClass(double[].class);
582: assertGetClassReturnsClass(boolean[].class);
583: }
584:
585: public void testGetClassRawPrimitives()
586: throws ClassNotFoundException {
587: assertEquals(int.class, ClassUtils.getClass("int"));
588: assertEquals(long.class, ClassUtils.getClass("long"));
589: assertEquals(short.class, ClassUtils.getClass("short"));
590: assertEquals(byte.class, ClassUtils.getClass("byte"));
591: assertEquals(char.class, ClassUtils.getClass("char"));
592: assertEquals(float.class, ClassUtils.getClass("float"));
593: assertEquals(double.class, ClassUtils.getClass("double"));
594: assertEquals(boolean.class, ClassUtils.getClass("boolean"));
595: }
596:
597: private void assertGetClassReturnsClass(Class c) throws Exception {
598: assertEquals(c, ClassUtils.getClass(c.getName()));
599: }
600:
601: private void assertGetClassThrowsException(String className,
602: Class exceptionType) throws Exception {
603: try {
604: ClassUtils.getClass(className);
605: fail("ClassUtils.getClass() should fail with an exception of type "
606: + exceptionType.getName()
607: + " when given class name \"" + className + "\".");
608: } catch (Exception e) {
609: assertTrue(exceptionType.isAssignableFrom(e.getClass()));
610: }
611: }
612:
613: private void assertGetClassThrowsIllegalArgument(String className)
614: throws Exception {
615: assertGetClassThrowsException(className,
616: IllegalArgumentException.class);
617: }
618:
619: private void assertGetClassThrowsClassNotFound(String className)
620: throws Exception {
621: assertGetClassThrowsException(className,
622: ClassNotFoundException.class);
623: }
624:
625: // Show the Java bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4071957
626: // We may have to delete this if a JDK fixes the bug.
627: public void testShowJavaBug() throws Exception {
628: // Tests with Collections$UnmodifiableSet
629: Set set = Collections.unmodifiableSet(new HashSet());
630: Method isEmptyMethod = set.getClass().getMethod("isEmpty",
631: new Class[0]);
632: try {
633: isEmptyMethod.invoke(set, new Object[0]);
634: fail("Failed to throw IllegalAccessException as expected");
635: } catch (IllegalAccessException iae) {
636: // expected
637: }
638: }
639:
640: public void testGetPublicMethod() throws Exception {
641: // Tests with Collections$UnmodifiableSet
642: Set set = Collections.unmodifiableSet(new HashSet());
643: Method isEmptyMethod = ClassUtils.getPublicMethod(set
644: .getClass(), "isEmpty", new Class[0]);
645: assertTrue(Modifier.isPublic(isEmptyMethod.getDeclaringClass()
646: .getModifiers()));
647:
648: try {
649: isEmptyMethod.invoke(set, new Object[0]);
650: } catch (java.lang.IllegalAccessException iae) {
651: fail("Should not have thrown IllegalAccessException");
652: }
653:
654: // Tests with a public Class
655: Method toStringMethod = ClassUtils.getPublicMethod(
656: Object.class, "toString", new Class[0]);
657: assertEquals(Object.class.getMethod("toString", new Class[0]),
658: toStringMethod);
659: }
660:
661: }
|