KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > utils > bytecode > TestChainedParamReader


1 package test.utils.bytecode;
2
3 import junit.framework.TestCase;
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6
7 import java.io.IOException JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
9 import java.lang.reflect.Constructor JavaDoc;
10
11 import org.apache.axis.utils.bytecode.ChainedParamReader;
12
13 /**
14  * Description
15  * User: pengyu
16  * Date: Sep 13, 2003
17  * Time: 10:59:00 PM
18  *
19  */

20 public class TestChainedParamReader extends TestCase{
21     private TestDerivedClass test =null;
22     private ChainedParamReader reader;
23     public TestChainedParamReader(String JavaDoc name) {
24         super(name);
25     }
26     public static Test suite() {
27         return new TestSuite(TestChainedParamReader.class);
28     }
29
30     protected void setup() {
31         test = this.new TestDerivedClass(1);
32     }
33
34     public void testGetMethodParameters(){
35         try {
36             reader = new ChainedParamReader(TestDerivedClass.class);
37         } catch (IOException JavaDoc e) {
38             fail("failed to setup paramreader:" + e.getMessage());
39         }
40         assertTrue("should not be null", reader != null);
41         //first get method1
42
try {
43             Method JavaDoc method1 = TestDerivedClass.class.getMethod("method1", new Class JavaDoc[] {Boolean.TYPE});
44             String JavaDoc [] params = reader.getParameterNames(method1);
45             assertTrue("one parameter only",params.length == 1);
46             assertTrue("It is 'boolValue'", params[0].equals("boolValue"));
47
48             Method JavaDoc method2 = TestDerivedClass.class.getMethod("method2", new Class JavaDoc[] {Boolean.TYPE});
49             params = reader.getParameterNames(method2);
50             assertTrue("one parameter only",params.length == 1);
51             assertTrue("It is 'boolValue'", params[0].equals("boolValue"));
52             method2= TestDerivedClass.class.getMethod("method2", new Class JavaDoc[] {Integer.TYPE});
53             params = reader.getParameterNames(method2);
54             assertTrue("one parameter only",params.length == 1);
55             assertTrue("It is 'intValue'", params[0].equals("intValue"));
56
57         } catch (NoSuchMethodException JavaDoc e) {
58             fail(e.toString());
59         } catch (SecurityException JavaDoc e) {
60             fail(e.toString());
61         }
62
63     }
64
65     public void testGetConstructorParameters() {
66         try {
67             reader = new ChainedParamReader(TestDerivedClass.class);
68             assertTrue("should not be null", reader != null);
69             Constructor JavaDoc ctor = TestDerivedClass.class.getConstructor(new Class JavaDoc[] {
70                 TestChainedParamReader.class, Integer.TYPE});
71
72             String JavaDoc [] params = reader.getParameterNames(ctor);
73             assertTrue("params is not null" , params.length == 2);
74             assertTrue("param name is 'in'", params[1].equals("in"));
75         }
76         catch (IOException JavaDoc e) {
77             fail("failed to setup paramreader:" + e.getMessage());
78         }
79         catch (NoSuchMethodException JavaDoc e) {
80             fail(e.getMessage());
81         }
82
83     }
84
85     public void testGetInheritedMethodParameters() {
86         try {
87             reader = new ChainedParamReader(TestDerivedClass.class);
88             Method JavaDoc method3 = TestDerivedClass.class.getMethod("subClassInherit", new Class JavaDoc[] {Integer.TYPE});
89             String JavaDoc [] params = reader.getParameterNames(method3);
90             assertTrue("It should find inherited method", params != null);
91         } catch (IOException JavaDoc e) {
92             fail("failed to setup paramreader:" + e.getMessage());
93         } catch (NoSuchMethodException JavaDoc e) {
94             fail(e.toString());
95         }
96     }
97
98     class TestBaseClass {
99         public void subClassInherit(int intValue) {
100         }
101     }
102     class TestDerivedClass extends TestBaseClass{
103         public TestDerivedClass() {
104         }
105         public TestDerivedClass(int in) {
106         }
107         public void method1(boolean boolValue) {
108         }
109         public void method2(int intValue) {
110         }
111         public void method2(boolean boolValue) {
112         }
113     }
114 }
115
116
Popular Tags