KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.ArrayList JavaDoc;
10
11 import org.apache.axis.utils.bytecode.ParamNameExtractor;
12
13 /**
14  * Description
15  * User: pengyu
16  * Date: Sep 12, 2003
17  * Time: 11:47:48 PM
18  *
19  */

20 public class TestParamNameExtractor extends TestCase {
21     TestClass t = null;
22
23     public TestParamNameExtractor(String JavaDoc name) {
24         super(name);
25     }
26
27     public static Test suite() {
28         return new TestSuite(TestParamNameExtractor.class);
29     }
30
31     protected void setup() {
32         t = this.new TestClass();
33     }
34
35     public void testExtractParameter() {
36         //now get the nonoverloadmethod
37
Method JavaDoc[] methods = TestClass.class.getMethods();
38         Method JavaDoc method = null;
39         for (int i = 0; i < methods.length; i++) {
40             if (methods[i].getName().equals("nonOverloadMethod")) {
41                 method = methods[i];
42             }
43         }
44         assertTrue("Find nonOverloadMethod", method != null);
45         String JavaDoc[] params = ParamNameExtractor.getParameterNamesFromDebugInfo(method);
46         assertTrue("Number of parameter is right", params.length == 2);
47         assertTrue("First name of parameter is intValue", params[0].equals("intValue"));
48         assertTrue("Second name of parameter is boolValue", params[1].equals("boolValue"));
49     }
50
51     public void testExtractOverloadedParameter() {
52         Method JavaDoc[] methods = TestClass.class.getMethods();
53         List JavaDoc matchMethods = new ArrayList JavaDoc();
54         for (int i = 0; i < methods.length; i++) {
55             if (methods[i].getName().equals("overloadedMethod")) {
56                 matchMethods.add(methods[i]);
57             }
58         }
59         assertTrue("Found two overloaded methods", matchMethods.size() == 2);
60         boolean foundBoolean = false;
61         boolean foundInt = false;
62         for (int i = 0; i < 2; i++) {
63             Method JavaDoc method = (Method JavaDoc) matchMethods.get(i);
64             Class JavaDoc[] paramTypes = method.getParameterTypes();
65             assertTrue("only one parameter found", paramTypes.length == 1);
66             assertTrue("It has to be either boolean or int",
67                     (paramTypes[0] == Integer.TYPE) ||
68                     (paramTypes[0] == Boolean.TYPE));
69             String JavaDoc[] params = ParamNameExtractor.getParameterNamesFromDebugInfo(method);
70             assertTrue("Only parameter found", params.length == 1);
71             if (paramTypes[0] == Integer.TYPE) {
72                 if (foundInt) { //already found such method so something is wrong
73
fail("It is wrong type, should not be int");
74                 }else {
75                     foundInt = true;
76                 }
77                 assertTrue("parameter is 'intValue'", params[0].equals("intValue"));
78             } else if (paramTypes[0] == Boolean.TYPE) {
79                 if (foundBoolean) {
80                     fail("It is wrong type, should not be boolean");
81                 }else {
82                     foundBoolean = true;
83                 }
84                 assertTrue("parameter is 'boolValue'", params[0].equals("boolValue"));
85             }
86         }
87     }
88
89     class TestClass {
90         public void nonOverloadMethod(int intValue, boolean boolValue) {
91         }
92
93         public void overloadedMethod(int intValue) {
94         }
95
96         public void overloadedMethod(boolean boolValue) {
97         }
98     }
99 }
100
Popular Tags