1 package test.utils.bytecode; 2 import junit.framework.TestCase; 3 import junit.framework.Test; 4 import junit.framework.TestSuite; 5 6 import java.lang.reflect.Method ; 7 import java.lang.reflect.Constructor ; 8 import java.io.IOException ; 9 10 import org.apache.axis.utils.bytecode.ParamReader; 11 12 19 public class TestParamReader extends TestCase{ 20 private TestDerivedClass test =null; 21 private ParamReader reader; 22 public TestParamReader(String name) { 23 super(name); 24 } 25 public static Test suite() { 26 return new TestSuite(TestParamReader.class); 27 } 28 29 protected void setup() { 30 test = this.new TestDerivedClass(1); 31 } 32 33 public void testGetMethodParameters(){ 34 try { 35 reader = new ParamReader(TestDerivedClass.class); 36 } catch (IOException e) { 37 fail("failed to setup paramreader:" + e.getMessage()); 38 } 39 assertTrue("should not be null", reader != null); 40 try { 42 Method method1 = TestDerivedClass.class.getMethod("method1", new Class [] {Boolean.TYPE}); 43 String [] params = reader.getParameterNames(method1); 44 assertTrue("one parameter only",params.length == 1); 45 assertTrue("It is 'boolValue'", params[0].equals("boolValue")); 46 47 Method method2 = TestDerivedClass.class.getMethod("method2", new Class [] {Boolean.TYPE}); 48 params = reader.getParameterNames(method2); 49 assertTrue("one parameter only",params.length == 1); 50 assertTrue("It is 'boolValue'", params[0].equals("boolValue")); 51 method2= TestDerivedClass.class.getMethod("method2", new Class [] {Integer.TYPE}); 52 params = reader.getParameterNames(method2); 53 assertTrue("one parameter only",params.length == 1); 54 assertTrue("It is 'intValue'", params[0].equals("intValue")); 55 56 Method method3 = TestDerivedClass.class.getMethod("subClassInherit", new Class [] {Integer.TYPE}); 57 params = reader.getParameterNames(method3); 58 assertTrue("It should not find inherited method", params == null); 59 } catch (NoSuchMethodException e) { 60 fail(e.toString()); 61 } catch (SecurityException e) { 62 fail(e.toString()); 63 } 64 65 } 66 67 public void testGetConstructorParameters() { 68 try { 69 reader = new ParamReader(TestDerivedClass.class); 70 assertTrue("should not be null", reader != null); 71 Constructor ctor = TestDerivedClass.class.getConstructor(new Class [] { 72 TestParamReader.class, Integer.TYPE}); 73 74 String [] params = reader.getParameterNames(ctor); 75 assertTrue("params is not null" , params.length == 2); 76 assertTrue("param name is 'in'", params[1].equals("in")); 77 } 78 catch (IOException e) { 79 fail("failed to setup paramreader:" + e.getMessage()); 80 } 81 catch (NoSuchMethodException e) { 82 fail(e.getMessage()); 83 } 84 85 } 86 87 public static void main(String [] arg) { 88 TestParamReader t = new TestParamReader(""); 89 t.testGetConstructorParameters(); 90 } 91 class TestBaseClass { 92 public void subClassInherit(int intValue) { 93 } 94 } 95 class TestDerivedClass extends TestBaseClass{ 96 public TestDerivedClass() { 97 } 98 public TestDerivedClass(int in) { 99 } 100 public void method1(boolean boolValue) { 101 } 102 public void method2(int intValue) { 103 } 104 public void method2(boolean boolValue) { 105 } 106 } 107 } 108 | Popular Tags |