1 8 package test; 9 10 import junit.framework.TestCase; 11 import org.codehaus.aspectwerkz.annotation.Before; 12 import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor; 13 import org.codehaus.aspectwerkz.transform.inlining.EmittedJoinPoint; 14 15 import java.io.ByteArrayOutputStream ; 16 import java.io.InputStream ; 17 18 21 public class LineNumberTest extends TestCase { 22 23 int m_field; 24 25 int s_field; 26 27 public LineNumberTest() { m_field = 1; int mget = m_field; s_field = 2; int sget = s_field; } 33 34 public void exec() { called(); } 37 38 public void called() { LineNumberTest t = new LineNumberTest(); try { 41 throw new RuntimeException ("fake"); } catch (RuntimeException e) { ; 44 } 45 } 46 47 48 public void testLineNumbers() throws Throwable { 49 AspectWerkzPreProcessor aw = new AspectWerkzPreProcessor(); 50 aw.initialize(); 51 String name = LineNumberTest.class.getName().replace('/', '.'); 52 InputStream in = LineNumberTest.class.getClassLoader().getResourceAsStream(name.replace('.','/')+".class"); 53 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 54 byte[] buffer = new byte[1024]; 55 while (in.available() > 0) { 56 int length = in.read(buffer); 57 if (length == -1) { 58 break; 59 } 60 bos.write(buffer, 0, length); 61 } 62 in.close(); 63 64 byte[] inBytes = bos.toByteArray(); 65 AspectWerkzPreProcessor.Output output = aw.preProcessWithOutput(name, inBytes, LineNumberTest.class.getClassLoader()); 66 67 assertEquals(11, output.emittedJoinPoints.length); 69 70 StringBuffer emitted = new StringBuffer (); 73 for (int i = 0; i < output.emittedJoinPoints.length; i++) { 74 EmittedJoinPoint emittedJoinPoint = output.emittedJoinPoints[i]; 75 emitted.append(emittedJoinPoint.toString()); 77 emitted.append("\n"); 78 } 79 String allJps = "ConstructorExecution , caller test/LineNumberTest.<init>()V , callee test/LineNumberTest.<init> ()V , line 0\n" + 80 "FieldSet , caller test/LineNumberTest.<init>()V , callee test/LineNumberTest.m_field I , line 28\n" + 81 "FieldGet , caller test/LineNumberTest.<init>()V , callee test/LineNumberTest.m_field I , line 29\n" + 82 "FieldSet , caller test/LineNumberTest.<init>()V , callee test/LineNumberTest.s_field I , line 30\n" + 83 "FieldGet , caller test/LineNumberTest.<init>()V , callee test/LineNumberTest.s_field I , line 31\n" + 84 "MethodExecution , caller test/LineNumberTest.exec()V , callee test/LineNumberTest.exec ()V , line 0\n" + 85 "MethodCall , caller test/LineNumberTest.exec()V , callee test/LineNumberTest.called ()V , line 35\n" + 86 "MethodExecution , caller test/LineNumberTest.called()V , callee test/LineNumberTest.called ()V , line 0\n" + 87 "ConstructorCall , caller test/LineNumberTest.called()V , callee test/LineNumberTest.<init> ()V , line 39\n" + 88 "ConstructorCall , caller test/LineNumberTest.called()V , callee java/lang/RuntimeException.<init> (Ljava/lang/String;)V , line 41\n" + 89 "Handler , caller test/LineNumberTest.called()V , callee java/lang/RuntimeException. Ljava/lang/RuntimeException; , line 42\n" + 90 ""; 91 assertEquals(allJps, emitted.toString()); 92 } 93 94 public static void main(String [] args) { 95 junit.textui.TestRunner.run(suite()); 96 } 97 98 public static junit.framework.Test suite() { 99 return new junit.framework.TestSuite(LineNumberTest.class); 100 } 101 102 public static class Aspect { 103 104 @Before("within(test.LineNumberTest) && !withincode(* test*(..)) && !withincode(* main(..)) && !withincode(* suite())") 105 void before() { 106 } 107 } 108 } 109 | Popular Tags |