KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > ClassUtilsTests


1 package org.springframework.util;
2
3 import java.io.Serializable JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6 import java.lang.reflect.Proxy JavaDoc;
7 import java.util.Arrays JavaDoc;
8 import java.util.List JavaDoc;
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 /**
19  * Tests the various functionality of the ClassUtils class.
20  *
21  * @author Colin Sampaleanu
22  */

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 JavaDoc className = ClassUtils.getShortName(getClass());
33         assertEquals("Class name did not match", "ClassUtilsTests", className);
34     }
35
36     public void testGetInnerClassShortName() {
37         String JavaDoc 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 JavaDoc className = ClassUtils.getShortName(proxy.getClass());
48         assertEquals("Class name did not match", "TestBean", className);
49     }
50
51     public void testGetShortNameAsProperty() {
52         String JavaDoc 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         // no args
59
assertTrue(ClassUtils.hasAtLeastOneMethodWithName(TestBean.class, "hashCode"));
60         // matches although it takes an arg
61
assertTrue(ClassUtils.hasAtLeastOneMethodWithName(TestBean.class, "setAge"));
62     }
63
64     public void testNoArgsStaticMethod() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
65         Method JavaDoc method = ClassUtils.getStaticMethod(InnerClass.class, "staticMethod", (Class JavaDoc[]) null);
66         method.invoke(null, (Object JavaDoc[]) null);
67         assertTrue("no argument method was not invoked.",
68             InnerClass.noArgCalled);
69     }
70
71     public void testArgsStaticMethod() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
72         Method JavaDoc method = ClassUtils.getStaticMethod(InnerClass.class, "argStaticMethod",
73             new Class JavaDoc[] {String JavaDoc.class});
74         method.invoke(null, new Object JavaDoc[] {"test"});
75         assertTrue("argument method was not invoked.", InnerClass.argCalled);
76     }
77
78     public void testOverloadedStaticMethod() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
79         Method JavaDoc method = ClassUtils.getStaticMethod(InnerClass.class, "staticMethod",
80             new Class JavaDoc[] {String JavaDoc.class});
81         method.invoke(null, new Object JavaDoc[] {"test"});
82         assertTrue("argument method was not invoked.",
83             InnerClass.overloadedCalled);
84     }
85
86     public void testClassPackageAsResourcePath() {
87         String JavaDoc result = ClassUtils.classPackageAsResourcePath(Proxy JavaDoc.class);
88         assertTrue(result.equals("java/lang/reflect"));
89     }
90
91     public void testAddResourcePathToPackagePath() {
92         String JavaDoc result = "java/lang/reflect/xyzabc.xml";
93         assertEquals(result, ClassUtils.addResourcePathToPackagePath(Proxy JavaDoc.class, "xyzabc.xml"));
94         assertEquals(result, ClassUtils.addResourcePathToPackagePath(Proxy JavaDoc.class, "/xyzabc.xml"));
95
96         assertEquals("java/lang/reflect/a/b/c/d.xml",
97             ClassUtils.addResourcePathToPackagePath(Proxy JavaDoc.class, "a/b/c/d.xml"));
98     }
99
100     public void testGetAllInterfaces() {
101         DerivedTestBean testBean = new DerivedTestBean();
102         List JavaDoc ifcs = Arrays.asList(ClassUtils.getAllInterfaces(testBean));
103         assertEquals("Correct number of interfaces", 7, ifcs.size());
104         assertTrue("Contains Serializable", ifcs.contains(Serializable JavaDoc.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 JavaDoc anArg) {
121             overloadedCalled = true;
122         }
123
124         public static void argStaticMethod(String JavaDoc anArg) {
125             argCalled = true;
126         }
127     }
128
129 }
130
Popular Tags