KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > utils > cache > TestJavaClass


1 package test.utils.cache;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.apache.axis.utils.cache.JavaClass;
7
8 import java.lang.reflect.Method JavaDoc;
9
10 public class TestJavaClass extends TestCase
11 {
12     public TestJavaClass (String JavaDoc name) {
13         super(name);
14     }
15
16     public static Test suite() {
17         return new TestSuite(TestJavaClass.class);
18     }
19     
20     protected void setup() {
21     }
22
23     public void testGetJavaClass()
24     {
25         Class JavaDoc c = new java.util.Date JavaDoc().getClass();
26         JavaClass jc = new JavaClass(c);
27         assertNotNull("The JavaClass was null", jc);
28         assertTrue("JavaClass name is not 'java.util.Date', it is: " + jc.getClass().getName(),
29                    jc.getJavaClass().getName().equals("java.util.Date"));
30         assertTrue("JavaClass cut off the name of the real class.",
31                    !jc.getJavaClass().getName().equals("java.util.D"));
32     }
33
34     public void testGetMethod()
35     {
36         Class JavaDoc v = new java.util.Vector JavaDoc().getClass();
37         Class JavaDoc st = new java.util.StringTokenizer JavaDoc("some string").getClass();
38         JavaClass jcVec = new JavaClass(v);
39         JavaClass jcST = new JavaClass(st);
40
41         Method JavaDoc countTkns = jcST.getMethod("countTokens")[0];
42         Method JavaDoc nextTkn = jcST.getMethod("nextToken")[0];
43
44         Method JavaDoc[] adds = jcVec.getMethod("add");
45
46         assertEquals("countTkns name was not 'countTokens', it is: " + countTkns.getName(),
47                      "countTokens", countTkns.getName());
48         assertEquals("nextTkn name was not 'nextToken', it is: " + nextTkn.getName(),
49                      "nextToken", nextTkn.getName());
50
51         assertEquals("There are not 2 add methods as expected, there are " + adds.length, 2, adds.length);
52
53         for (int i = 0; i < adds.length; ++i) {
54             if (adds[i].getReturnType().equals(boolean.class)) {
55                 assertEquals("Unexpected boolean add signature",
56                    "public synchronized boolean java.util.Vector.add(java.lang.Object)",
57                    adds[i].toString());
58             }
59             else {
60                 assertEquals("Unexpected void add signature",
61                     "public void java.util.Vector.add(int,java.lang.Object)",
62                     adds[i].toString());
63             }
64         }
65     }
66
67     public void testNoSuchMethod()
68     {
69         Class JavaDoc v = new java.util.Vector JavaDoc().getClass();
70         JavaClass jcVec = new JavaClass(v);
71
72         Method JavaDoc[] gorp = jcVec.getMethod("gorp");
73         assertNull("gorp was not null", gorp);
74     }
75 }
76
Popular Tags