1 16 package org.apache.commons.lang; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.Modifier ; 20 import java.net.URL ; 21 import java.net.URLClassLoader ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import junit.framework.Test; 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 import junit.textui.TestRunner; 30 31 38 public class ClassUtilsTest extends TestCase { 39 40 public ClassUtilsTest(String name) { 41 super(name); 42 } 43 44 public static void main(String [] 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 { 55 super.setUp(); 56 } 57 58 protected void tearDown() throws Exception { 59 super.tearDown(); 60 } 61 62 private static class Inner { 63 } 64 65 public void testConstructor() { 67 assertNotNull(new ClassUtils()); 68 Constructor [] 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 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 .class)); 86 assertEquals("", ClassUtils.getShortClassName((Class ) 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 .class.getName())); 92 assertEquals("", ClassUtils.getShortClassName((String ) null)); 93 assertEquals("", ClassUtils.getShortClassName("")); 94 } 95 96 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 .class)); 105 assertEquals("java.util", ClassUtils.getPackageName(Map.Entry .class)); 106 assertEquals("", ClassUtils.getPackageName((Class )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 .class.getName())); 112 assertEquals("", ClassUtils.getPackageName((String )null)); 113 assertEquals("", ClassUtils.getPackageName("")); 114 } 115 116 public void test_getAllSuperclasses_Class() { 118 List list = ClassUtils.getAllSuperclasses(CY.class); 119 assertEquals(2, list.size()); 120 assertEquals(CX.class, list.get(0)); 121 assertEquals(Object .class, list.get(1)); 122 123 assertEquals(null, ClassUtils.getAllSuperclasses(null)); 124 } 125 126 public void test_getAllInterfaces_Class() { 127 List 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 public void test_convertClassNamesToClasses_List() { 158 List list = new ArrayList (); 159 List 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 .class, result.get(0)); 168 assertEquals(null, result.get(1)); 169 assertEquals(Object .class, result.get(2)); 170 171 list.add(new Object ()); 172 try { 173 ClassUtils.convertClassNamesToClasses(list); 174 fail(); 175 } catch (ClassCastException ex) {} 176 assertEquals(null, ClassUtils.convertClassNamesToClasses(null)); 177 } 178 179 public void test_convertClassesToClassNames_List() { 180 List list = new ArrayList (); 181 List result = ClassUtils.convertClassesToClassNames(list); 182 assertEquals(0, result.size()); 183 184 list.add(String .class); 185 list.add(null); 186 list.add(Object .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 ()); 194 try { 195 ClassUtils.convertClassesToClassNames(list); 196 fail(); 197 } catch (ClassCastException ex) {} 198 assertEquals(null, ClassUtils.convertClassesToClassNames(null)); 199 } 200 201 public void test_isInnerClass_Class() { 203 assertEquals(true, ClassUtils.isInnerClass(Inner.class)); 204 assertEquals(true, ClassUtils.isInnerClass(Map.Entry .class)); 205 assertEquals(true, ClassUtils.isInnerClass(new Cloneable () { 206 }.getClass())); 207 assertEquals(false, ClassUtils.isInnerClass(this.getClass())); 208 assertEquals(false, ClassUtils.isInnerClass(String .class)); 209 assertEquals(false, ClassUtils.isInnerClass(null)); 210 } 211 212 public void test_isAssignable_ClassArray_ClassArray() throws Exception { 214 Class [] array2 = new Class [] {Object .class, Object .class}; 215 Class [] array1 = new Class [] {Object .class}; 216 Class [] array1s = new Class [] {String .class}; 217 Class [] array0 = new Class [] {}; 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 []) null, (Class []) 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 { 232 assertEquals(false, ClassUtils.isAssignable((Class ) null, null)); 233 assertEquals(false, ClassUtils.isAssignable(String .class, null)); 234 235 assertEquals(true, ClassUtils.isAssignable(null, Object .class)); 236 assertEquals(true, ClassUtils.isAssignable(null, Integer .class)); 237 assertEquals(false, ClassUtils.isAssignable(null, Integer.TYPE)); 238 assertEquals(true, ClassUtils.isAssignable(String .class, Object .class)); 239 assertEquals(true, ClassUtils.isAssignable(String .class, String .class)); 240 assertEquals(false, ClassUtils.isAssignable(Object .class, String .class)); 241 assertEquals(false, ClassUtils.isAssignable(Integer.TYPE, Integer .class)); 242 assertEquals(false, ClassUtils.isAssignable(Integer .class, Integer.TYPE)); 243 assertEquals(true, ClassUtils.isAssignable(Integer.TYPE, Integer.TYPE)); 244 assertEquals(true, ClassUtils.isAssignable(Integer .class, Integer .class)); 245 } 246 247 public void test_isAssignable_Widening() throws Exception { 248 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 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 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 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 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 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 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 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 assertEquals("boolean -> Boolean.class", 333 Boolean .class, ClassUtils.primitiveToWrapper(Boolean.TYPE)); 334 assertEquals("byte -> Byte.class", 335 Byte .class, ClassUtils.primitiveToWrapper(Byte.TYPE)); 336 assertEquals("char -> Character.class", 337 Character .class, ClassUtils.primitiveToWrapper(Character.TYPE)); 338 assertEquals("short -> Short.class", 339 Short .class, ClassUtils.primitiveToWrapper(Short.TYPE)); 340 assertEquals("int -> Integer.class", 341 Integer .class, ClassUtils.primitiveToWrapper(Integer.TYPE)); 342 assertEquals("long -> Long.class", 343 Long .class, ClassUtils.primitiveToWrapper(Long.TYPE)); 344 assertEquals("double -> Double.class", 345 Double .class, ClassUtils.primitiveToWrapper(Double.TYPE)); 346 assertEquals("float -> Float.class", 347 Float .class, ClassUtils.primitiveToWrapper(Float.TYPE)); 348 349 assertEquals("String.class -> String.class", 351 String .class, ClassUtils.primitiveToWrapper(String .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 assertNull("null -> null", 358 ClassUtils.primitiveToWrapper(null)); 359 } 360 361 public void testPrimitivesToWrappers() { 362 assertNull("null -> null", 364 ClassUtils.primitivesToWrappers(null)); 365 assertEquals("empty -> empty", 367 ArrayUtils.EMPTY_CLASS_ARRAY, ClassUtils.primitivesToWrappers(ArrayUtils.EMPTY_CLASS_ARRAY)); 368 369 final Class [] primitives = new Class [] { 371 Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE, 372 Integer.TYPE, Long.TYPE, Double.TYPE, Float.TYPE, 373 String .class, ClassUtils.class 374 }; 375 Class [] wrappers= ClassUtils.primitivesToWrappers(primitives); 376 377 for (int i=0; i < primitives.length; i++) { 378 Class primitive = primitives[i]; 380 Class expectedWrapper = ClassUtils.primitiveToWrapper(primitive); 381 382 assertEquals(primitive + " -> " + expectedWrapper, expectedWrapper, wrappers[i]); 383 } 384 385 final Class [] noPrimitives = new Class [] { 387 String .class, ClassUtils.class 388 }; 389 assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives)); 391 } 392 393 401 public static ClassLoader newSystemClassLoader() throws SecurityException , IllegalArgumentException { 402 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); 403 ClassLoader myClassLoader = ClassUtilsTest.class.getClassLoader(); 404 if (!(myClassLoader instanceof URLClassLoader )) { 405 fail("Need a better test set up."); 406 } 407 if (!(systemClassLoader instanceof URLClassLoader )) { 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 urlScl = (URLClassLoader ) myClassLoader; 414 return URLClassLoader.newInstance(urlScl.getURLs(), null); 415 } 416 417 } 418 | Popular Tags |