1 package org.springframework.util; 2 3 import java.io.Serializable ; 4 import java.lang.reflect.InvocationTargetException ; 5 import java.lang.reflect.Method ; 6 import java.lang.reflect.Proxy ; 7 import java.util.Arrays ; 8 import java.util.List ; 9 10 import junit.framework.TestCase; 11 12 import org.springframework.aop.framework.ProxyFactory; 13 import org.springframework.beans.DerivedTestBean; 14 import org.springframework.beans.IOther; 15 import org.springframework.beans.ITestBean; 16 import org.springframework.beans.TestBean; 17 18 23 public class ClassUtilsTests extends TestCase { 24 25 public void setUp() { 26 InnerClass.noArgCalled = false; 27 InnerClass.argCalled = false; 28 InnerClass.overloadedCalled = false; 29 } 30 31 public void testGetShortName() { 32 String className = ClassUtils.getShortName(getClass()); 33 assertEquals("Class name did not match", "ClassUtilsTests", className); 34 } 35 36 public void testGetInnerClassShortName() { 37 String className = ClassUtils.getShortName(InnerClass.class); 38 assertEquals("Class name did not match", "ClassUtilsTests.InnerClass", className); 39 } 40 41 public void testGetCglibClassShortName() { 42 TestBean tb = new TestBean(); 43 ProxyFactory pf = new ProxyFactory(); 44 pf.setTarget(tb); 45 pf.setProxyTargetClass(true); 46 TestBean proxy = (TestBean) pf.getProxy(); 47 String className = ClassUtils.getShortName(proxy.getClass()); 48 assertEquals("Class name did not match", "TestBean", className); 49 } 50 51 public void testGetShortNameAsProperty() { 52 String shortName = ClassUtils.getShortNameAsProperty(this.getClass()); 53 assertEquals("Class name did not match", "classUtilsTests", shortName); 54 } 55 56 public void testCountOverloadedMethods() { 57 assertFalse(ClassUtils.hasAtLeastOneMethodWithName(TestBean.class, "foobar")); 58 assertTrue(ClassUtils.hasAtLeastOneMethodWithName(TestBean.class, "hashCode")); 60 assertTrue(ClassUtils.hasAtLeastOneMethodWithName(TestBean.class, "setAge")); 62 } 63 64 public void testNoArgsStaticMethod() throws IllegalAccessException , InvocationTargetException { 65 Method method = ClassUtils.getStaticMethod(InnerClass.class, "staticMethod", (Class []) null); 66 method.invoke(null, (Object []) null); 67 assertTrue("no argument method was not invoked.", 68 InnerClass.noArgCalled); 69 } 70 71 public void testArgsStaticMethod() throws IllegalAccessException , InvocationTargetException { 72 Method method = ClassUtils.getStaticMethod(InnerClass.class, "argStaticMethod", 73 new Class [] {String .class}); 74 method.invoke(null, new Object [] {"test"}); 75 assertTrue("argument method was not invoked.", InnerClass.argCalled); 76 } 77 78 public void testOverloadedStaticMethod() throws IllegalAccessException , InvocationTargetException { 79 Method method = ClassUtils.getStaticMethod(InnerClass.class, "staticMethod", 80 new Class [] {String .class}); 81 method.invoke(null, new Object [] {"test"}); 82 assertTrue("argument method was not invoked.", 83 InnerClass.overloadedCalled); 84 } 85 86 public void testClassPackageAsResourcePath() { 87 String result = ClassUtils.classPackageAsResourcePath(Proxy .class); 88 assertTrue(result.equals("java/lang/reflect")); 89 } 90 91 public void testAddResourcePathToPackagePath() { 92 String result = "java/lang/reflect/xyzabc.xml"; 93 assertEquals(result, ClassUtils.addResourcePathToPackagePath(Proxy .class, "xyzabc.xml")); 94 assertEquals(result, ClassUtils.addResourcePathToPackagePath(Proxy .class, "/xyzabc.xml")); 95 96 assertEquals("java/lang/reflect/a/b/c/d.xml", 97 ClassUtils.addResourcePathToPackagePath(Proxy .class, "a/b/c/d.xml")); 98 } 99 100 public void testGetAllInterfaces() { 101 DerivedTestBean testBean = new DerivedTestBean(); 102 List ifcs = Arrays.asList(ClassUtils.getAllInterfaces(testBean)); 103 assertEquals("Correct number of interfaces", 7, ifcs.size()); 104 assertTrue("Contains Serializable", ifcs.contains(Serializable .class)); 105 assertTrue("Contains ITestBean", ifcs.contains(ITestBean.class)); 106 assertTrue("Contains IOther", ifcs.contains(IOther.class)); 107 } 108 109 110 public static class InnerClass { 111 112 static boolean noArgCalled; 113 static boolean argCalled; 114 static boolean overloadedCalled; 115 116 public static void staticMethod() { 117 noArgCalled = true; 118 } 119 120 public static void staticMethod(String anArg) { 121 overloadedCalled = true; 122 } 123 124 public static void argStaticMethod(String anArg) { 125 argCalled = true; 126 } 127 } 128 129 } 130 | Popular Tags |