KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > LineNumberTest


1 /**************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

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 JavaDoc;
16 import java.io.InputStream JavaDoc;
17
18 /**
19  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
20  */

21 public class LineNumberTest extends TestCase {
22
23     int m_field;
24
25     int s_field;
26
27     public LineNumberTest() {//ctor exe
28
m_field = 1;//field set
29
int mget = m_field;//field get
30
s_field = 2;//field set
31
int sget = s_field;//field get
32
}
33
34     public void exec() {//method exe
35
called();//method call
36
}
37
38     public void called() {//method exe
39
LineNumberTest t = new LineNumberTest();//ctor call
40
try {
41             throw new RuntimeException JavaDoc("fake");//ctor call
42
} catch (RuntimeException JavaDoc e) {//handler
43
;
44         }
45     }
46
47
48     public void testLineNumbers() throws Throwable JavaDoc {
49         AspectWerkzPreProcessor aw = new AspectWerkzPreProcessor();
50         aw.initialize();
51         String JavaDoc name = LineNumberTest.class.getName().replace('/', '.');
52         InputStream JavaDoc in = LineNumberTest.class.getClassLoader().getResourceAsStream(name.replace('.','/')+".class");
53         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
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         // we have 11 joinpoints
68
assertEquals(11, output.emittedJoinPoints.length);
69
70         //Note: this test depends upon visitor order
71
// adapt the output when needed
72
StringBuffer JavaDoc emitted = new StringBuffer JavaDoc();
73         for (int i = 0; i < output.emittedJoinPoints.length; i++) {
74             EmittedJoinPoint emittedJoinPoint = output.emittedJoinPoints[i];
75             //System.out.println(emittedJoinPoint);
76
emitted.append(emittedJoinPoint.toString());
77             emitted.append("\n");
78         }
79         String JavaDoc 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 JavaDoc[] 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