1 17 package org.apache.tools.ant.taskdefs.optional.sitraka; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 22 import junit.framework.TestCase; 23 import org.apache.tools.ant.util.JavaEnvUtils; 24 import org.apache.tools.ant.taskdefs.optional.sitraka.bytecode.ClassFile; 25 import org.apache.tools.ant.taskdefs.optional.sitraka.bytecode.MethodInfo; 26 27 31 public class ClassFileTest extends TestCase { 32 public ClassFileTest(String s) { 33 super(s); 34 } 35 36 public void testVector() throws IOException { 37 String classname = ClassTest.class.getName().replace('.', '/') + ".class"; 38 InputStream is = getClass().getClassLoader().getResourceAsStream(classname); 39 assertNotNull("Unable to find resource " + classname + "in caller classloader"); 40 ClassFile clazzfile = new ClassFile(is); 41 assertEquals("ClassTest", clazzfile.getName()); 42 assertEquals("ClassFileTest.java", clazzfile.getSourceFile()); 43 MethodInfo[] methods = clazzfile.getMethods(); 44 assertEquals(3, methods.length); 45 assertHasMethod("void <init>()", 1, methods); 46 assertHasMethod("void testTwoLines()", 2, methods); 47 assertHasMethod("void testOneLine()", 3, methods); 48 } 49 50 protected void assertHasMethod(String methodsig, int line, MethodInfo[] methods) { 51 boolean found = false; 52 for (int i = 0; i < methods.length; i++) { 53 MethodInfo method = methods[i]; 54 if (methodsig.equals(method.getFullSignature())) { 55 56 assertTrue(methodsig, method.getNumberOfLines() >= line); 57 return; 58 } 59 } 60 fail("Could not find method " + methodsig); 61 } 62 } 63 64 class ClassTest { 65 66 public ClassTest() { 68 } 69 70 public void testTwoLines() { 72 System.out.println("This is 1 line"); 73 } 74 75 public void testOneLine() { 77 try { 78 throw new Exception (); 79 } catch (Exception e) { 80 } 81 } 82 } 83 | Popular Tags |