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:
011: import java.util.SortedSet;
012: import java.util.TreeSet;
013: import java.util.Set;
014:
015: import org.codehaus.aspectwerkz.annotation.Before;
016: import org.codehaus.aspectwerkz.annotation.Around;
017: import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
018:
019: /**
020: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
021: */
022: public class InterfaceDefinedMethodTest extends TestCase {
023:
024: public static String s_log = "";
025:
026: public InterfaceDefinedMethodTest(String s) {
027: super (s);
028: }
029:
030: public InterfaceDefinedMethodTest() {
031: SortedSet ss = new TreeSet();
032: ss.add("foo"); // Warning, add is in super interface
033: ss.first(); // Ok, first is in SortedSet
034:
035: try {
036: Set s = ss;
037: s.add("bar"); // Ok, add is in Set
038: throw new NullPointerException("fake");
039: } catch (NullPointerException npe) {
040: ;
041: }
042: }
043:
044: /**
045: * When visiting the bytecode of this method, the classInfo must lookup in the class + intf
046: * hierarchy
047: */
048: public void testInterfaceDefinedMethod() {
049: s_log = "";
050: SortedSet ss = new TreeSet();
051: ss.add("foo"); // Warning, add is in super interface
052: ss.first(); // Ok, first is in SortedSet
053:
054: try {
055: Set s = ss;
056: s.add("bar"); // Ok, add is in Set
057: throw new NullPointerException("fake");
058: } catch (NullPointerException npe) {
059: ;
060: }
061: assertEquals(
062: "advice advice advice advice advice advice advice ",
063: s_log);
064: }
065:
066: public void testWithinCtor() {
067: s_log = "";
068: InterfaceDefinedMethodTest me = new InterfaceDefinedMethodTest();
069: assertEquals(
070: "around around around around around around around ",
071: s_log);
072: }
073:
074: public void testWithinNot() {
075: s_log = "";
076: withinNot();
077: assertEquals("withincode withincode withincode ", s_log);
078: }
079:
080: private void withinNot() {
081: InterfaceDefinedMethodTest me = new InterfaceDefinedMethodTest(
082: "ignore");
083: subWithinNot();
084: }
085:
086: private void subWithinNot() {
087: ;
088: }
089:
090: public static class Aspect {
091:
092: @Before("withincode(* test.InterfaceDefinedMethodTest.testInterfaceDefinedMethod(..))")
093: public void before(StaticJoinPoint sjp) {
094: s_log += "advice ";
095: }
096:
097: @Around("withincode(test.InterfaceDefinedMethodTest.new())")
098: public Object around(StaticJoinPoint sjp) throws Throwable {
099: s_log += "around ";
100: return sjp.proceed();
101: }
102:
103: @Before("cflow(within(test.InterfaceDefinedMethodTest) && call(* test.InterfaceDefinedMethodTest.withinNot()))" + "&& !withincode(* test.InterfaceDefinedMethodTest.withinNot())" + "&& within(test.InterfaceDefinedMethodTest)")
104: public void neverCalled(StaticJoinPoint sjp) {
105: s_log += "withincode ";
106: //System.out.println(sjp.getType() + " " + sjp.getSignature());
107: }
108: }
109:
110: public static void main(String[] args) {
111: junit.textui.TestRunner.run(suite());
112: }
113:
114: public static junit.framework.Test suite() {
115: return new junit.framework.TestSuite(
116: InterfaceDefinedMethodTest.class);
117: }
118: }
|