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.annotation.Around;
012: import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
013: import org.codehaus.aspectwerkz.transform.inlining.weaver.SerialVersionUidVisitor;
014:
015: import java.io.Serializable;
016: import java.lang.reflect.Field;
017:
018: /**
019: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
020: */
021: public class CtorExecution extends TestCase implements Serializable {
022:
023: static int s_count = 0;
024:
025: public CtorExecution m_ref;
026:
027: public int m_i;// = 1;
028:
029: public CtorExecution(CtorExecution ref) {
030: postInit(this );
031: //m_ref = ref;
032: }
033:
034: static void postInit(CtorExecution target) {
035: ;
036: }
037:
038: public CtorExecution() {
039: // tricky INVOKESPECIAL indexing
040: this (new CtorExecution((CtorExecution) null));
041: new CtorExecution((CtorExecution) null);
042: postInit(this );
043: }
044:
045: public CtorExecution(String s) {
046: // tricky INVOKESPECIAL indexing
047: // and tricky new CtorExecution() call before instance initialization
048: // and tricky method call before instance initialization
049: super ((new CtorExecution()).string(s));
050: (new CtorExecution()).string(s);
051: }
052:
053: public CtorExecution(int i) {
054: // tricky field get and set before instance initialization
055: super ("" + (new CtorExecution()).m_i++);
056: (new CtorExecution()).m_i++;
057: }
058:
059: public String string(String s) {
060: return s;
061: }
062:
063: public void testSome() {
064: s_count = 0;
065: CtorExecution me = new CtorExecution();
066: me = new CtorExecution(me);
067: me = new CtorExecution("foo");
068: me = new CtorExecution(2);
069: assertEquals(116, s_count);// don't know if it is the right number but decompiled seems ok..
070: }
071:
072: public void testSerialVer() throws Throwable {
073: Class x = CtorExecution.class;
074: long l = SerialVersionUidVisitor.calculateSerialVersionUID(x);
075: // uncomment me and turn off weaver to compute the expected serialVerUID
076: //System.out.println(l);
077:
078: Field f = x.getDeclaredField("serialVersionUID");
079: long uid = ((Long) f.get(null)).longValue();
080: //System.out.println(uid);
081: assertEquals(3813928159352352835L, uid);
082: }
083:
084: public static class Aspect {
085: @Before("within(test.CtorExecution)")
086: void before(StaticJoinPoint sjp) {
087: s_count++;
088: //System.err.println(sjp.getSignature());
089: }
090:
091: @Around("execution(test.CtorExecution.new(..))" + " || (call(test.CtorExecution.new(..)) && within(test.CtorExecution))")
092: Object around(StaticJoinPoint sjp) throws Throwable {
093: s_count++;
094: //System.out.println(sjp.getSignature());
095: return sjp.proceed();
096: }
097:
098: }
099:
100: public static void main(String[] args) {
101: junit.textui.TestRunner.run(suite());
102: }
103:
104: public static junit.framework.Test suite() {
105: return new junit.framework.TestSuite(CtorExecution.class);
106: }
107:
108: }
|