001: /**************************************************************************************
002: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test;
008:
009: import junit.framework.TestCase;
010: import org.codehaus.aspectwerkz.annotation.Before;
011: import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor;
012: import org.codehaus.aspectwerkz.transform.inlining.EmittedJoinPoint;
013:
014: import java.io.ByteArrayOutputStream;
015: import java.io.InputStream;
016:
017: /**
018: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
019: */
020: public class LineNumberTest extends TestCase {
021:
022: int m_field;
023:
024: int s_field;
025:
026: public LineNumberTest() {//ctor exe
027: m_field = 1;//field set
028: int mget = m_field;//field get
029: s_field = 2;//field set
030: int sget = s_field;//field get
031: }
032:
033: public void exec() {//method exe
034: called();//method call
035: }
036:
037: public void called() {//method exe
038: LineNumberTest t = new LineNumberTest();//ctor call
039: try {
040: throw new RuntimeException("fake");//ctor call
041: } catch (RuntimeException e) {//handler
042: ;
043: }
044: }
045:
046: public void testLineNumbers() throws Throwable {
047: AspectWerkzPreProcessor aw = new AspectWerkzPreProcessor();
048: aw.initialize();
049: String name = LineNumberTest.class.getName().replace('/', '.');
050: InputStream in = LineNumberTest.class.getClassLoader()
051: .getResourceAsStream(name.replace('.', '/') + ".class");
052: ByteArrayOutputStream bos = new ByteArrayOutputStream();
053: byte[] buffer = new byte[1024];
054: while (in.available() > 0) {
055: int length = in.read(buffer);
056: if (length == -1) {
057: break;
058: }
059: bos.write(buffer, 0, length);
060: }
061: in.close();
062:
063: byte[] inBytes = bos.toByteArray();
064: AspectWerkzPreProcessor.Output output = aw
065: .preProcessWithOutput(name, inBytes,
066: LineNumberTest.class.getClassLoader());
067:
068: // we have 11 joinpoints
069: assertEquals(11, output.emittedJoinPoints.length);
070:
071: //Note: this test depends upon visitor order
072: // adapt the output when needed
073: StringBuffer emitted = new StringBuffer();
074: for (int i = 0; i < output.emittedJoinPoints.length; i++) {
075: EmittedJoinPoint emittedJoinPoint = output.emittedJoinPoints[i];
076: //System.out.println(emittedJoinPoint);
077: emitted.append(emittedJoinPoint.toString());
078: emitted.append("\n");
079: }
080: String allJps = "ConstructorExecution , caller test/LineNumberTest.<init>()V , callee test/LineNumberTest.<init> ()V , line 0\n"
081: + "FieldSet , caller test/LineNumberTest.<init>()V , callee test/LineNumberTest.m_field I , line 28\n"
082: + "FieldGet , caller test/LineNumberTest.<init>()V , callee test/LineNumberTest.m_field I , line 29\n"
083: + "FieldSet , caller test/LineNumberTest.<init>()V , callee test/LineNumberTest.s_field I , line 30\n"
084: + "FieldGet , caller test/LineNumberTest.<init>()V , callee test/LineNumberTest.s_field I , line 31\n"
085: + "MethodExecution , caller test/LineNumberTest.exec()V , callee test/LineNumberTest.exec ()V , line 0\n"
086: + "MethodCall , caller test/LineNumberTest.exec()V , callee test/LineNumberTest.called ()V , line 35\n"
087: + "MethodExecution , caller test/LineNumberTest.called()V , callee test/LineNumberTest.called ()V , line 0\n"
088: + "ConstructorCall , caller test/LineNumberTest.called()V , callee test/LineNumberTest.<init> ()V , line 39\n"
089: + "ConstructorCall , caller test/LineNumberTest.called()V , callee java/lang/RuntimeException.<init> (Ljava/lang/String;)V , line 41\n"
090: + "Handler , caller test/LineNumberTest.called()V , callee java/lang/RuntimeException. Ljava/lang/RuntimeException; , line 42\n"
091: + "";
092: assertEquals(allJps, emitted.toString());
093: }
094:
095: public static void main(String[] args) {
096: junit.textui.TestRunner.run(suite());
097: }
098:
099: public static junit.framework.Test suite() {
100: return new junit.framework.TestSuite(LineNumberTest.class);
101: }
102:
103: public static class Aspect {
104:
105: @Before("within(test.LineNumberTest) && !withincode(* test*(..)) && !withincode(* main(..)) && !withincode(* suite())")
106: void before() {
107: }
108: }
109: }
|