0001: // $Id: JVMAspectInterfaceTest.java,v 1.3 2004/05/12 17:26:51 anicoara Exp $
0002: // =====================================================================
0003: //
0004: // (history at end)
0005: //
0006:
0007: package ch.ethz.prose;
0008:
0009: //used packages
0010: import java.lang.reflect.Field;
0011: import java.lang.reflect.Method;
0012:
0013: import junit.framework.*;
0014: import ch.ethz.jvmai.*;
0015:
0016: /**
0017: * JUnit testcase for class JVMAspectInterface.
0018: *
0019: * @version $Revision: 1.3 $
0020: * @author Stephan Markwalder
0021: * @author Angela Nicoara
0022: */
0023: public class JVMAspectInterfaceTest extends TestCase {
0024:
0025: JVMAspectInterface aspectInterface = null;
0026: Method method = null;
0027: Field field = null;
0028: Class exceptionClass = null;
0029:
0030: /**
0031: * Construct test with given name.
0032: * @param name test name
0033: */
0034: public JVMAspectInterfaceTest(String name) {
0035: super (name);
0036: }
0037:
0038: static class TestHook extends JoinPointHook {
0039: public int oFA, oFM, oMEn, oMEx, oExt, oExc, oCL;
0040:
0041: public void reset() {
0042: oFA = 0;
0043: oFM = 0;
0044: oMEn = 0;
0045: oMEx = 0;
0046: oExt = 0;
0047: oExc = 0;
0048: oCL = 0;
0049: }
0050:
0051: public void onFieldAccess(FieldAccessJoinPoint joinPoint) {
0052: oFA++;
0053: }
0054:
0055: public void onFieldModification(
0056: FieldModificationJoinPoint joinPoint) {
0057: oFM++;
0058: }
0059:
0060: public void onMethodEntry(MethodEntryJoinPoint joinPoint) {
0061: oMEn++;
0062: }
0063:
0064: public void onMethodExit(MethodExitJoinPoint joinPoint) {
0065: oMEx++;
0066: }
0067:
0068: public void onExceptionThrow(ExceptionJoinPoint joinPoint) {
0069: oExt++;
0070: }
0071:
0072: public void onExceptionCatch(ExceptionCatchJoinPoint joinPoint) {
0073: oExc++;
0074: }
0075:
0076: public void onClassLoad(Class cls) {
0077: oCL++;
0078: }
0079:
0080: public void onConstructor(ConstructorJoinPoint joinPoint) {
0081: }
0082: }
0083:
0084: static class TestException extends Exception {
0085: public TestException() {
0086: super ();
0087: }
0088:
0089: public TestException(String s) {
0090: super (s);
0091: }
0092: }
0093:
0094: static class TestException2 extends Exception {
0095: public TestException2() {
0096: super ();
0097: }
0098:
0099: public TestException2(String s) {
0100: super (s);
0101: }
0102: }
0103:
0104: static class TestException3 extends Exception {
0105: public TestException3() {
0106: super ();
0107: }
0108:
0109: public TestException3(String s) {
0110: super (s);
0111: }
0112: }
0113:
0114: static class TestException4 extends RuntimeException {
0115: }
0116:
0117: static class TestCatch {
0118: public int field1;
0119: public int field2;
0120:
0121: public void method1() {
0122: }
0123:
0124: public void throwMethod2() {
0125: try {
0126: if (field1 == 1)
0127: throw new TestException();
0128: } catch (TestException e) {
0129: }
0130: }
0131:
0132: public void throwMethod3() {
0133: try {
0134: throw new TestException2();
0135: } catch (TestException2 e) {
0136: }
0137: }
0138:
0139: public void throwMethod4() {
0140: try {
0141: if (field2 == 2)
0142: throw new TestException2();
0143: } catch (TestException2 e) {
0144: }
0145: }
0146:
0147: public void throwMethod5() {
0148: try {
0149: if (field2 == 2)
0150: throw new TestException3();
0151: } catch (TestException3 e) {
0152: }
0153: }
0154:
0155: public void throwMethod6() {
0156: try {
0157: throw new TestException4();
0158: } catch (Exception e) {
0159: }
0160: }
0161: }
0162:
0163: static class TestClass1 {
0164: public void method() {
0165: }
0166:
0167: public int field;
0168:
0169: public void throwMethod() {
0170: try {
0171: throw new TestException();
0172: } catch (TestException e) {
0173: }
0174: }
0175:
0176: public void run() {
0177: method();
0178: field = field + 1;
0179: throwMethod();
0180: }
0181: }
0182:
0183: static abstract class TestClass2 {
0184: }
0185:
0186: static abstract class TestClass3 {
0187: public abstract void method();
0188: }
0189:
0190: static abstract class TestAbstractClass {
0191:
0192: // abstract methods (implemented by TestClass4)
0193: abstract void method08();
0194:
0195: abstract public void method09();
0196:
0197: abstract protected void method10();
0198:
0199: // standard methods (overwritten by TestClass4)
0200: void method11() {
0201: }
0202:
0203: public void method12() {
0204: }
0205:
0206: protected void method13() {
0207: }
0208:
0209: private void method14() {
0210: }
0211:
0212: // standard methods (inherited by TestClass4)
0213: void method17() {
0214: }
0215:
0216: public void method18() {
0217: }
0218:
0219: protected void method19() {
0220: }
0221:
0222: // static methods (inherited by TestClass4)
0223: static void method20() {
0224: }
0225:
0226: static public void method21() {
0227: }
0228:
0229: static protected void method22() {
0230: }
0231:
0232: // standard fields (shadowed by TestClass4)
0233: int field00;
0234: public int field01;
0235: protected int field02;
0236: private int field03;
0237:
0238: // standard fields (inherited by TestClass4)
0239: int field04;
0240: public int field05;
0241: protected int field06;
0242:
0243: // static fields (inherited by TestClass4)
0244: static int field07;
0245: static public int field08;
0246: static protected int field09;
0247:
0248: }
0249:
0250: static interface TestInterface {
0251: // interface methods (implemented by TestClass4)
0252: void method15();
0253:
0254: public void method16();
0255:
0256: }
0257:
0258: static class TestClass4 extends TestAbstractClass implements
0259: TestInterface {
0260:
0261: // standard fields
0262: int field00;
0263: public int field01;
0264: protected int field02;
0265: private int field03;
0266:
0267: // standard fields
0268: int field10;
0269: public int field11;
0270: protected int field12;
0271: private int field13;
0272:
0273: // static fields
0274: static int field14;
0275: static public int field15;
0276: static protected int field16;
0277: static private int field17;
0278:
0279: // standard methods, different access modifiers
0280: void method00() {
0281: }
0282:
0283: public void method01() {
0284: }
0285:
0286: protected void method02() {
0287: }
0288:
0289: private void method03() {
0290: }
0291:
0292: // static methods
0293: static void method04() {
0294: }
0295:
0296: static public void method05() {
0297: }
0298:
0299: static protected void method06() {
0300: }
0301:
0302: static private void method07() {
0303: }
0304:
0305: // implementation of abstract methods in TestAbstractClass
0306: void method08() {
0307: }
0308:
0309: public void method09() {
0310: }
0311:
0312: protected void method10() {
0313: }
0314:
0315: // methods overwriting TestAbstractClass's methods
0316: void method11() {
0317: }
0318:
0319: public void method12() {
0320: }
0321:
0322: protected void method13() {
0323: }
0324:
0325: private void method14() {
0326: }
0327:
0328: // implementation of methods in interface TestInterface
0329: public void method15() {
0330: }
0331:
0332: public void method16() {
0333: }
0334:
0335: public void callMethods() {
0336: method00();
0337: method01();
0338: method02();
0339: method03();
0340: method04();
0341: method05();
0342: method06();
0343: method07();
0344: method08();
0345: method09();
0346: method10();
0347: method11();
0348: method12();
0349: method13();
0350: method14();
0351: method15();
0352: method16();
0353: method17();
0354: method18();
0355: method19();
0356: method20();
0357: method21();
0358: method22();
0359: }
0360:
0361: public void accessFields() {
0362: field00 = field00 + 1;
0363: field01 = field01 + 1;
0364: field02 = field02 + 1;
0365: field03 = field03 + 1;
0366: field04 = field04 + 1;
0367: field05 = field05 + 1;
0368: field06 = field06 + 1;
0369: field07 = field07 + 1;
0370: field08 = field08 + 1;
0371: field09 = field09 + 1;
0372: field10 = field10 + 1;
0373: field11 = field11 + 1;
0374: field12 = field12 + 1;
0375: field13 = field13 + 1;
0376: field14 = field14 + 1;
0377: field15 = field15 + 1;
0378: field16 = field16 + 1;
0379: field17 = field17 + 1;
0380: }
0381:
0382: }
0383:
0384: static class TestClass5 {
0385: int exit;
0386:
0387: public void method0() {
0388: if (exit == 0)
0389: return;
0390: else {
0391: switch (exit) {
0392: case 2:
0393: return;
0394: case 3:
0395: return;
0396: }
0397: return;
0398: }
0399: }
0400:
0401: public int method1() {
0402: if (exit == 0)
0403: return 0;
0404: else {
0405: switch (exit) {
0406: case 2:
0407: return 2;
0408: case 3:
0409: return 3;
0410: }
0411: return 4;
0412: }
0413: }
0414:
0415: public Object method2() {
0416: if (exit == 0)
0417: return "hi0";
0418: else {
0419: switch (exit) {
0420: case 2:
0421: return "hi2";
0422: case 3:
0423: return "hi3";
0424: }
0425: return "hi4";
0426: }
0427: }
0428:
0429: public double method3() {
0430: if (exit == 0)
0431: return 1.0;
0432: else {
0433: switch (exit) {
0434: case 2:
0435: return 2.0;
0436: case 3:
0437: return 3.0;
0438: }
0439: return 4.0;
0440: }
0441: }
0442: }
0443:
0444: static class TestThread extends Thread {
0445: private TestClass1 test;
0446:
0447: public TestThread(TestClass1 t) {
0448: test = t;
0449: }
0450:
0451: public void run() {
0452: if (test == null)
0453: return;
0454: test.run();
0455: }
0456: }
0457:
0458: protected void setUp() throws Exception {
0459: String providerClassName = System.getProperty(
0460: "ch.ethz.prose.JVMAIProvider",
0461: "ch.ethz.inf.iks.jvmai.jvmdi.DebuggerProvider");
0462: Class providerClass = Class.forName(providerClassName);
0463: Provider provider = (Provider) providerClass.newInstance();
0464: aspectInterface = provider.getAspectInterface();
0465: method = TestClass1.class.getDeclaredMethods()[0];
0466: field = TestClass1.class.getDeclaredFields()[0];
0467: }
0468:
0469: protected void tearDown() {
0470: try {
0471:
0472: try {
0473: aspectInterface.clearMethodEntryWatch(method);
0474: } catch (RuntimeException e) {
0475: }
0476: try {
0477: aspectInterface.clearMethodExitWatch(method);
0478: } catch (RuntimeException e) {
0479: }
0480: try {
0481: aspectInterface.clearFieldAccessWatch(field);
0482: } catch (RuntimeException e) {
0483: }
0484: try {
0485: aspectInterface.clearFieldModificationWatch(field);
0486: } catch (RuntimeException e) {
0487: }
0488: try {
0489: aspectInterface
0490: .clearExceptionThrowWatch(TestException.class);
0491: } catch (RuntimeException e) {
0492: }
0493: try {
0494: aspectInterface
0495: .clearExceptionCatchWatch(TestException.class);
0496: } catch (RuntimeException e) {
0497: }
0498: try {
0499: aspectInterface.setJoinPointHook(null);
0500: } catch (RuntimeException e) {
0501: }
0502: } catch (RuntimeException e) {
0503: }
0504: }
0505:
0506: public void test_0010_NotInitializedExceptions() {
0507: // calling any method from aspect-interface before initialization should throw a NotInitializedException
0508: int exceptions = 0;
0509:
0510: try {
0511: aspectInterface.setJoinPointHook(null);
0512: } catch (RuntimeException e) {
0513: if (e instanceof NotInitializedException) {
0514: exceptions++;
0515: }
0516: }
0517:
0518: try {
0519: aspectInterface.setFieldAccessWatch(null, null);
0520: } catch (RuntimeException e) {
0521: if (e instanceof NotInitializedException) {
0522: exceptions++;
0523: }
0524: }
0525:
0526: try {
0527: aspectInterface.setFieldModificationWatch(null, null);
0528: } catch (RuntimeException e) {
0529: if (e instanceof NotInitializedException) {
0530: exceptions++;
0531: }
0532: }
0533:
0534: try {
0535: aspectInterface.setMethodEntryWatch(null, null);
0536: } catch (RuntimeException e) {
0537: if (e instanceof NotInitializedException) {
0538: exceptions++;
0539: }
0540: }
0541:
0542: try {
0543: aspectInterface.setMethodExitWatch(null, null);
0544: } catch (RuntimeException e) {
0545: if (e instanceof NotInitializedException) {
0546: exceptions++;
0547: }
0548: }
0549:
0550: try {
0551: aspectInterface.setExceptionThrowWatch(null, null);
0552: } catch (RuntimeException e) {
0553: if (e instanceof NotInitializedException) {
0554: exceptions++;
0555: }
0556: }
0557:
0558: try {
0559: aspectInterface.setExceptionCatchWatch(null, null);
0560: } catch (RuntimeException e) {
0561: if (e instanceof NotInitializedException) {
0562: exceptions++;
0563: }
0564: }
0565:
0566: try {
0567: aspectInterface.clearFieldAccessWatch(null);
0568: } catch (RuntimeException e) {
0569: if (e instanceof NotInitializedException) {
0570: exceptions++;
0571: }
0572: }
0573:
0574: try {
0575: aspectInterface.clearFieldModificationWatch(null);
0576: } catch (RuntimeException e) {
0577: if (e instanceof NotInitializedException) {
0578: exceptions++;
0579: }
0580: }
0581:
0582: try {
0583: aspectInterface.clearMethodEntryWatch(null);
0584: } catch (RuntimeException e) {
0585: if (e instanceof NotInitializedException) {
0586: exceptions++;
0587: }
0588: }
0589:
0590: try {
0591: aspectInterface.clearMethodExitWatch(null);
0592: } catch (RuntimeException e) {
0593: if (e instanceof NotInitializedException) {
0594: exceptions++;
0595: }
0596: }
0597:
0598: try {
0599: aspectInterface.clearExceptionThrowWatch(null);
0600: } catch (RuntimeException e) {
0601: if (e instanceof NotInitializedException) {
0602: exceptions++;
0603: }
0604: }
0605:
0606: try {
0607: aspectInterface.clearExceptionCatchWatch(null);
0608: } catch (RuntimeException e) {
0609: if (e instanceof NotInitializedException) {
0610: exceptions++;
0611: }
0612: }
0613:
0614: try {
0615: aspectInterface.suspendNotification(null);
0616: } catch (RuntimeException e) {
0617: if (e instanceof NotInitializedException) {
0618: exceptions++;
0619: }
0620: }
0621:
0622: try {
0623: aspectInterface.resumeNotification(null);
0624: } catch (RuntimeException e) {
0625: if (e instanceof NotInitializedException) {
0626: exceptions++;
0627: }
0628: }
0629:
0630: assertEquals("15 NotInitializedException", exceptions, 15);
0631:
0632: }
0633:
0634: public void test_0020_Startup() {
0635: try {
0636: try {
0637: aspectInterface.startup(new String[0], true);
0638: aspectInterface.startup(new String[0], false);
0639: } catch (RuntimeException e) {
0640: assertTrue("calling startup a second time is ignored",
0641: false);
0642: }
0643: } catch (Throwable e) {
0644: e.printStackTrace();
0645: throw new RuntimeException("startup failed" + e);
0646: }
0647:
0648: }
0649:
0650: public void test_0030_SetClearWatch() {
0651:
0652: Object aopTag = new Object();
0653: int exceptions = 0;
0654:
0655: // set all possible watches in TestClass : no exceptions should be thrown
0656: try {
0657: aspectInterface.setMethodEntryWatch(method, aopTag);
0658: } catch (RuntimeException e) {
0659: exceptions++;
0660: System.err.println("A1");
0661: }
0662: try {
0663: aspectInterface.setMethodExitWatch(method, aopTag);
0664: } catch (RuntimeException e) {
0665: exceptions++;
0666: System.err.println("A2");
0667: }
0668: try {
0669: aspectInterface.setFieldAccessWatch(field, aopTag);
0670: } catch (RuntimeException e) {
0671: exceptions++;
0672: System.err.println("A3");
0673: }
0674: try {
0675: aspectInterface.setFieldModificationWatch(field, aopTag);
0676: } catch (RuntimeException e) {
0677: exceptions++;
0678: System.err.println("A4");
0679: }
0680: try {
0681: aspectInterface.setExceptionThrowWatch(TestException.class,
0682: aopTag);
0683: } catch (RuntimeException e) {
0684: exceptions++;
0685: System.err.println("A5");
0686: }
0687: try {
0688: aspectInterface.setExceptionCatchWatch(TestException.class,
0689: aopTag);
0690: } catch (RuntimeException e) {
0691: exceptions++;
0692: System.err.println("A6");
0693: }
0694:
0695: assertEquals("no exceptions when setting watches", 0,
0696: exceptions);
0697:
0698: // clear all watches in TestClass
0699: try {
0700: aspectInterface.clearMethodEntryWatch(method);
0701: } catch (RuntimeException e) {
0702: exceptions++;
0703: System.err.println("B1");
0704: }
0705: try {
0706: aspectInterface.clearMethodExitWatch(method);
0707: } catch (RuntimeException e) {
0708: exceptions++;
0709: System.err.println("B2");
0710: }
0711: try {
0712: aspectInterface.clearFieldAccessWatch(field);
0713: } catch (RuntimeException e) {
0714: exceptions++;
0715: System.err.println("B3");
0716: }
0717: try {
0718: aspectInterface.clearFieldModificationWatch(field);
0719: } catch (RuntimeException e) {
0720: exceptions++;
0721: System.err.println("B4");
0722: }
0723: try {
0724: aspectInterface
0725: .clearExceptionThrowWatch(TestException.class);
0726: } catch (RuntimeException e) {
0727: exceptions++;
0728: System.err.println("B5");
0729: }
0730: try {
0731: aspectInterface
0732: .clearExceptionCatchWatch(TestException.class);
0733: } catch (RuntimeException e) {
0734: exceptions++;
0735: System.err.println("B6");
0736: }
0737: assertEquals("no exceptions when clearing watches", 0,
0738: exceptions);
0739: }
0740:
0741: public void test_0040_SetClearWatch_Exceptions() {
0742:
0743: Object aopTag = new Object();
0744: int exceptions = 0;
0745: long id = 0;
0746:
0747: // try again to set watches twice : every second set**Watch should throw a WatchAlreadySetException
0748: exceptions = 0;
0749:
0750: aspectInterface.setMethodEntryWatch(method, aopTag);
0751: try {
0752: aspectInterface.setMethodEntryWatch(method, aopTag);
0753: } catch (WatchAlreadySetException e) {
0754: exceptions++;
0755: }
0756:
0757: aspectInterface.setMethodExitWatch(method, aopTag);
0758: try {
0759: aspectInterface.setMethodExitWatch(method, aopTag);
0760: } catch (WatchAlreadySetException e) {
0761: exceptions++;
0762: }
0763:
0764: aspectInterface.setFieldAccessWatch(field, aopTag);
0765: try {
0766: aspectInterface.setFieldAccessWatch(field, aopTag);
0767: } catch (WatchAlreadySetException e) {
0768: exceptions++;
0769: }
0770:
0771: aspectInterface.setFieldModificationWatch(field, aopTag);
0772: try {
0773: aspectInterface.setFieldModificationWatch(field, aopTag);
0774: } catch (WatchAlreadySetException e) {
0775: exceptions++;
0776: }
0777:
0778: aspectInterface.setExceptionThrowWatch(TestException.class,
0779: aopTag);
0780: try {
0781: aspectInterface.setExceptionThrowWatch(TestException.class,
0782: aopTag);
0783: } catch (WatchAlreadySetException e) {
0784: exceptions++;
0785: }
0786:
0787: aspectInterface.setExceptionCatchWatch(TestException.class,
0788: aopTag);
0789: try {
0790: aspectInterface.setExceptionCatchWatch(TestException.class,
0791: aopTag);
0792: } catch (WatchAlreadySetException e) {
0793: exceptions++;
0794: }
0795:
0796: assertEquals("6 WatchAlreadySetExceptions", 6, exceptions);
0797:
0798: // try again to clear watches twice : every second clear**Watch should throw a WatchNotSetException
0799: exceptions = 0;
0800: aspectInterface.clearMethodEntryWatch(method);
0801:
0802: try {
0803: aspectInterface.clearMethodEntryWatch(method);
0804: } catch (WatchNotSetException e) {
0805: exceptions++;
0806: }
0807:
0808: aspectInterface.clearMethodExitWatch(method);
0809: try {
0810: aspectInterface.clearMethodExitWatch(method);
0811: } catch (WatchNotSetException e) {
0812: exceptions++;
0813: }
0814:
0815: aspectInterface.clearFieldAccessWatch(field);
0816: try {
0817: aspectInterface.clearFieldAccessWatch(field);
0818: } catch (WatchNotSetException e) {
0819: exceptions++;
0820: }
0821:
0822: aspectInterface.clearFieldModificationWatch(field);
0823: try {
0824: aspectInterface.clearFieldModificationWatch(field);
0825: } catch (WatchNotSetException e) {
0826: exceptions++;
0827: }
0828:
0829: aspectInterface.clearExceptionThrowWatch(TestException.class);
0830: try {
0831: aspectInterface
0832: .clearExceptionThrowWatch(TestException.class);
0833: } catch (WatchNotSetException e) {
0834: exceptions++;
0835: }
0836:
0837: aspectInterface.clearExceptionCatchWatch(TestException.class);
0838: try {
0839: aspectInterface
0840: .clearExceptionCatchWatch(TestException.class);
0841: } catch (WatchNotSetException e) {
0842: exceptions++;
0843: }
0844:
0845: assertEquals("6 WatchNotSetExceptions", 6, exceptions);
0846:
0847: exceptions = 0;
0848:
0849: // try to set watches on an interface method : every set**Watch should throw a CannotSetWatchException
0850: method = TestInterface.class.getDeclaredMethods()[0];
0851: exceptions = 0;
0852: try {
0853: aspectInterface.setMethodEntryWatch(method, aopTag);
0854: } catch (CannotSetWatchException e) {
0855: exceptions++;
0856: }
0857: try {
0858: aspectInterface.setMethodExitWatch(method, aopTag);
0859: } catch (CannotSetWatchException e) {
0860: exceptions++;
0861: }
0862: assertTrue("2 CannotSetWatchExceptions (interface method)",
0863: exceptions == 2);
0864:
0865: // try to set watches on an abstract method : every set**Watch should throw a CannotSetWatchException
0866: method = TestClass3.class.getDeclaredMethods()[0];
0867: exceptions = 0;
0868: try {
0869: aspectInterface.setMethodEntryWatch(method, aopTag);
0870: } catch (CannotSetWatchException e) {
0871: exceptions++;
0872: }
0873: try {
0874: aspectInterface.setMethodExitWatch(method, aopTag);
0875: } catch (CannotSetWatchException e) {
0876: exceptions++;
0877: }
0878: assertTrue("2 CannotSetWatchExceptions (abstract method)",
0879: exceptions == 2);
0880:
0881: // try to set/clear watches on class 'null' : every set**Watch/clear**Watch should throw a NullPointerException
0882: exceptions = 0;
0883: try {
0884: aspectInterface.setMethodEntryWatch(null, aopTag);
0885: } catch (NullPointerException e) {
0886: exceptions++;
0887: }
0888: try {
0889: aspectInterface.setMethodExitWatch(null, aopTag);
0890: } catch (NullPointerException e) {
0891: exceptions++;
0892: }
0893: try {
0894: aspectInterface.setFieldAccessWatch(null, aopTag);
0895: } catch (NullPointerException e) {
0896: exceptions++;
0897: }
0898: try {
0899: aspectInterface.setFieldModificationWatch(null, aopTag);
0900: } catch (NullPointerException e) {
0901: exceptions++;
0902: }
0903: try {
0904: aspectInterface.setExceptionThrowWatch(null, aopTag);
0905: } catch (NullPointerException e) {
0906: exceptions++;
0907: }
0908: try {
0909: aspectInterface.setExceptionCatchWatch(null, aopTag);
0910: } catch (NullPointerException e) {
0911: exceptions++;
0912: }
0913: try {
0914: aspectInterface.clearMethodEntryWatch(null);
0915: } catch (NullPointerException e) {
0916: exceptions++;
0917: }
0918: try {
0919: aspectInterface.clearMethodExitWatch(null);
0920: } catch (NullPointerException e) {
0921: exceptions++;
0922: }
0923: try {
0924: aspectInterface.clearFieldAccessWatch(null);
0925: } catch (NullPointerException e) {
0926: exceptions++;
0927: }
0928: try {
0929: aspectInterface.clearFieldModificationWatch(null);
0930: } catch (NullPointerException e) {
0931: exceptions++;
0932: }
0933: try {
0934: aspectInterface.clearExceptionThrowWatch(null);
0935: } catch (NullPointerException e) {
0936: exceptions++;
0937: }
0938: try {
0939: aspectInterface.clearExceptionCatchWatch(null);
0940: } catch (NullPointerException e) {
0941: exceptions++;
0942: }
0943: assertTrue("12 NullPointerExceptions", exceptions == 12);
0944:
0945: }
0946:
0947: public void test_0050_SetJoinPointHook() {
0948: int exceptions = 0;
0949: TestHook hook = new TestHook();
0950:
0951: try {
0952: aspectInterface.setJoinPointHook(hook);
0953: } catch (RuntimeException e) {
0954: exceptions++;
0955: }
0956: assertTrue("no exceptions while setting hook", exceptions == 0);
0957:
0958: // check if setting again the same hook changes the result
0959: try {
0960: aspectInterface.setJoinPointHook(hook);
0961: } catch (RuntimeException e) {
0962: exceptions++;
0963: }
0964: assertTrue("no exceptions while re-setting hook",
0965: exceptions == 0);
0966:
0967: // it should be allowed to set 'null' as hook
0968: try {
0969: aspectInterface.setJoinPointHook(null);
0970: } catch (RuntimeException e) {
0971: exceptions++;
0972: }
0973: assertTrue("no exceptions while setting hook to 'null'",
0974: exceptions == 0);
0975:
0976: try {
0977: aspectInterface.setJoinPointHook(hook);
0978: } catch (RuntimeException e) {
0979: exceptions++;
0980: }
0981: assertTrue("no exceptions while setting hook", exceptions == 0);
0982:
0983: }
0984:
0985: public void test_0060_HookNotification() {
0986:
0987: Object aopTag = new Object();
0988: TestHook hook = new TestHook();
0989: TestClass1 test = new TestClass1();
0990:
0991: aspectInterface.setMethodEntryWatch(method, aopTag);
0992: aspectInterface.setMethodExitWatch(method, aopTag);
0993: aspectInterface.setFieldAccessWatch(field, aopTag);
0994: aspectInterface.setFieldModificationWatch(field, aopTag);
0995: aspectInterface.setExceptionThrowWatch(TestException.class,
0996: aopTag);
0997: aspectInterface.setExceptionCatchWatch(TestException.class,
0998: aopTag);
0999: aspectInterface.resumeNotification(Thread.currentThread());
1000:
1001: aspectInterface.setJoinPointHook(hook);
1002: hook.reset();
1003: test.run();
1004:
1005: assertEquals("1 field access event", 1, hook.oFA);
1006: assertEquals("1 field modification event", 1, hook.oFM);
1007: assertEquals("1 method entry event", 1, hook.oMEn);
1008: assertEquals("1 method exit event", 1, hook.oMEx);
1009: assertEquals("1 exception throw event(1)", 1, hook.oExt);
1010: assertEquals("1 exception catch event(1)", 1, hook.oExc);
1011:
1012: // check if setting again the same hook changes the result
1013: aspectInterface.setJoinPointHook(hook);
1014: hook.reset();
1015: test.run();
1016:
1017: assertEquals("1 field access event", 1, hook.oFA);
1018: assertEquals("1 field modification event", 1, hook.oFM);
1019: assertEquals("1 method entry event", 1, hook.oMEn);
1020: assertEquals("1 method exit event", 1, hook.oMEx);
1021: assertEquals("1 exception throw event(2)", 1, hook.oExt);
1022: assertEquals("1 exception catch event(2)", 1, hook.oExc);
1023:
1024: // check wether events are forwarded to the hook after he has been removed (set 'null')
1025: aspectInterface.setJoinPointHook(null);
1026: hook.reset();
1027: test.run();
1028:
1029: assertEquals("no field access events", 0, hook.oFA);
1030: assertEquals("no field modification events", 0, hook.oFM);
1031: assertEquals("no method entry events", 0, hook.oMEn);
1032: assertEquals("no method exit events", 0, hook.oMEx);
1033: assertEquals("no exception throw events", 0, hook.oExt);
1034: assertEquals("no exception catch events", 0, hook.oExc);
1035:
1036: aspectInterface.setJoinPointHook(hook);
1037: hook.reset();
1038: test.run();
1039:
1040: assertEquals("1 field access event", 1, hook.oFA);
1041: assertEquals("1 field modification event", 1, hook.oFM);
1042: assertEquals("1 method entry event", 1, hook.oMEn);
1043: assertEquals("1 method exit event", 1, hook.oMEx);
1044: assertEquals("1 exception throw event", 1, hook.oExt);
1045: assertEquals("1 exception catch event", 1, hook.oExc);
1046:
1047: aspectInterface.clearMethodEntryWatch(method);
1048: aspectInterface.clearMethodExitWatch(method);
1049: aspectInterface.clearFieldAccessWatch(field);
1050: aspectInterface.clearFieldModificationWatch(field);
1051: aspectInterface.clearExceptionThrowWatch(TestException.class);
1052: aspectInterface.clearExceptionCatchWatch(TestException.class);
1053: aspectInterface.resumeNotification(Thread.currentThread());
1054: }
1055:
1056: public void test_0070_AopTagHandling() {
1057:
1058: Class exception = TestException.class;
1059: int exceptions = 0;
1060: JoinPointHook hook = null;
1061: TestClass1 test = new TestClass1();
1062:
1063: // set all watches in TestClass with 'null' as aopTag
1064: exceptions = 0;
1065: try {
1066: aspectInterface.setMethodEntryWatch(method, null);
1067: } catch (RuntimeException e) {
1068: exceptions++;
1069: }
1070: try {
1071: aspectInterface.setMethodExitWatch(method, null);
1072: } catch (RuntimeException e) {
1073: exceptions++;
1074: }
1075: try {
1076: aspectInterface.setFieldAccessWatch(field, null);
1077: } catch (RuntimeException e) {
1078: exceptions++;
1079: }
1080: try {
1081: aspectInterface.setFieldModificationWatch(field, null);
1082: } catch (RuntimeException e) {
1083: exceptions++;
1084: }
1085: try {
1086: aspectInterface.setExceptionThrowWatch(TestException.class,
1087: null);
1088: } catch (RuntimeException e) {
1089: exceptions++;
1090: }
1091: try {
1092: aspectInterface.setExceptionCatchWatch(TestException.class,
1093: null);
1094: } catch (RuntimeException e) {
1095: exceptions++;
1096: }
1097: assertEquals(
1098: "exceptions while setting watches with aopTag equals to 'null'",
1099: exceptions, 6);
1100:
1101: // check if watches have been set
1102: exceptions = 0;
1103: final Object stringTag = "hi, I'm a tag";
1104: try {
1105: aspectInterface.setMethodEntryWatch(method, stringTag);
1106: } catch (WatchAlreadySetException e) {
1107: exceptions++;
1108: }
1109:
1110: try {
1111: aspectInterface.setMethodExitWatch(method, stringTag);
1112: } catch (WatchAlreadySetException e) {
1113: exceptions++;
1114: }
1115:
1116: try {
1117: aspectInterface.setFieldAccessWatch(field, stringTag);
1118: } catch (WatchAlreadySetException e) {
1119: exceptions++;
1120: }
1121:
1122: try {
1123: aspectInterface.setFieldModificationWatch(field, stringTag);
1124: } catch (WatchAlreadySetException e) {
1125: exceptions++;
1126: }
1127:
1128: try {
1129: aspectInterface.setExceptionThrowWatch(TestException.class,
1130: stringTag);
1131: } catch (WatchAlreadySetException e) {
1132: exceptions++;
1133: }
1134:
1135: try {
1136: aspectInterface.setExceptionCatchWatch(TestException.class,
1137: stringTag);
1138: } catch (WatchAlreadySetException e) {
1139: exceptions++;
1140: }
1141:
1142: assertEquals(
1143: "NO exceptions while re-setting watches with non-null tags",
1144: exceptions, 0);
1145:
1146: // check if double setting watches leads to exceptions
1147: exceptions = 0;
1148: try {
1149: aspectInterface.setMethodEntryWatch(method, stringTag);
1150: } catch (WatchAlreadySetException e) {
1151: exceptions++;
1152: }
1153:
1154: try {
1155: aspectInterface.setMethodExitWatch(method, stringTag);
1156: } catch (WatchAlreadySetException e) {
1157: exceptions++;
1158: }
1159:
1160: try {
1161: aspectInterface.setFieldAccessWatch(field, stringTag);
1162: } catch (WatchAlreadySetException e) {
1163: exceptions++;
1164: }
1165:
1166: try {
1167: aspectInterface.setFieldModificationWatch(field, stringTag);
1168: } catch (WatchAlreadySetException e) {
1169: exceptions++;
1170: }
1171:
1172: try {
1173: aspectInterface.setExceptionThrowWatch(TestException.class,
1174: stringTag);
1175: } catch (WatchAlreadySetException e) {
1176: exceptions++;
1177: }
1178:
1179: try {
1180: aspectInterface.setExceptionCatchWatch(TestException.class,
1181: stringTag);
1182: } catch (WatchAlreadySetException e) {
1183: exceptions++;
1184: }
1185:
1186: assertEquals(
1187: "6 exceptions while re-setting watches with non null tags",
1188: exceptions, 6);
1189:
1190: // check if aopTags are correctly passed to hook in case of notification
1191: class TagTestHook extends JoinPointHook {
1192: Object omenTag;
1193: Object omexTag;
1194: Object ofmTag;
1195: Object ofaTag;
1196: Object oexTag;
1197: Object oexcTag;
1198:
1199: public void onFieldAccess(FieldAccessJoinPoint joinPoint) {
1200: ofaTag = joinPoint.getAopTag();
1201: }
1202:
1203: public void onMethodEntry(MethodEntryJoinPoint joinPoint) {
1204: omenTag = joinPoint.getAopTag();
1205: }
1206:
1207: public void onMethodExit(MethodExitJoinPoint joinPoint) {
1208: omexTag = joinPoint.getAopTag();
1209: }
1210:
1211: public void onExceptionThrow(ExceptionJoinPoint joinPoint) {
1212: oexTag = joinPoint.getAopTag();
1213: }
1214:
1215: public void onExceptionCatch(
1216: ExceptionCatchJoinPoint joinPoint) {
1217: oexcTag = joinPoint.getAopTag();
1218: }
1219:
1220: public void onFieldModification(
1221: FieldModificationJoinPoint joinPoint) {
1222: ofmTag = joinPoint.getAopTag();
1223: }
1224:
1225: public void onClassLoad(Class cls) {
1226: }
1227:
1228: public void onConstructor(ConstructorJoinPoint joinPoint) {
1229: }
1230: }
1231: ;
1232:
1233: TagTestHook tthook = new TagTestHook();
1234:
1235: aspectInterface.resumeNotification(Thread.currentThread());
1236: aspectInterface.setJoinPointHook(tthook);
1237: test.run();
1238:
1239: assertEquals("FA:aopTag:", stringTag, tthook.ofaTag);
1240: assertEquals("MEN:aopTag:", stringTag, tthook.omenTag);
1241: assertEquals("MEX:aopTag:", stringTag, tthook.omexTag);
1242: assertEquals("ET:aopTag:", stringTag, tthook.oexTag);
1243: assertEquals("EC:aopTag:", stringTag, tthook.oexcTag);
1244: assertEquals("FM:aopTag:", stringTag, tthook.ofmTag);
1245:
1246: // try to clear watches
1247: exceptions = 0;
1248: try {
1249: aspectInterface.clearMethodEntryWatch(method);
1250: } catch (RuntimeException e) {
1251: exceptions++;
1252: }
1253: try {
1254: aspectInterface.clearMethodExitWatch(method);
1255: } catch (RuntimeException e) {
1256: exceptions++;
1257: }
1258: try {
1259: aspectInterface.clearFieldAccessWatch(field);
1260: } catch (RuntimeException e) {
1261: exceptions++;
1262: }
1263: try {
1264: aspectInterface.clearFieldModificationWatch(field);
1265: } catch (RuntimeException e) {
1266: exceptions++;
1267: }
1268: try {
1269: aspectInterface
1270: .clearExceptionThrowWatch(TestException.class);
1271: } catch (RuntimeException e) {
1272: exceptions++;
1273: }
1274: try {
1275: aspectInterface
1276: .clearExceptionCatchWatch(TestException.class);
1277: } catch (RuntimeException e) {
1278: exceptions++;
1279: }
1280: assertTrue(
1281: "no exceptions while clearing watches with aopTag equals to 'null'",
1282: exceptions == 0);
1283:
1284: // check if watches have been cleaned
1285: exceptions = 0;
1286: try {
1287: aspectInterface.clearFieldAccessWatch(field);
1288: } catch (WatchNotSetException e) {
1289: exceptions++;
1290: }
1291:
1292: try {
1293: aspectInterface.clearFieldModificationWatch(field);
1294: } catch (WatchNotSetException e) {
1295: exceptions++;
1296: }
1297:
1298: try {
1299: aspectInterface.clearMethodExitWatch(method);
1300: } catch (WatchNotSetException e) {
1301: exceptions++;
1302: }
1303:
1304: try {
1305: aspectInterface.clearMethodEntryWatch(method);
1306: } catch (WatchNotSetException e) {
1307: exceptions++;
1308: }
1309:
1310: try {
1311: aspectInterface
1312: .clearExceptionThrowWatch(TestException.class);
1313: } catch (WatchNotSetException e) {
1314: exceptions++;
1315: }
1316:
1317: try {
1318: aspectInterface
1319: .clearExceptionCatchWatch(TestException.class);
1320: } catch (WatchNotSetException e) {
1321: exceptions++;
1322: }
1323:
1324: assertEquals("exceptions/re-clearing watches", 6, exceptions);
1325:
1326: // check if "normal" aopTags are treated correctly
1327: aspectInterface.setMethodEntryWatch(method, method);
1328: aspectInterface.setMethodExitWatch(method, method);
1329: aspectInterface.setFieldAccessWatch(field, field);
1330: aspectInterface.setFieldModificationWatch(field, field);
1331: aspectInterface.setExceptionThrowWatch(TestException.class,
1332: exception);
1333: aspectInterface.setExceptionCatchWatch(TestException.class,
1334: exception);
1335:
1336: hook = new JoinPointHook() {
1337: public void onFieldAccess(FieldAccessJoinPoint joinPoint) {
1338: Field field = joinPoint.getField();
1339: assertTrue(
1340: "FieldAccessJoinPoint.getAopTag() contains correct field",
1341: field.equals(joinPoint.getAopTag()));
1342: }
1343:
1344: public void onFieldModification(
1345: FieldModificationJoinPoint joinPoint) {
1346: Field field = joinPoint.getField();
1347: assertTrue(
1348: "FieldModificationJoinPoint.getAopTag() contains correct field",
1349: field.equals(joinPoint.getAopTag()));
1350: }
1351:
1352: public void onMethodEntry(MethodEntryJoinPoint joinPoint) {
1353: Method method = joinPoint.getMethod();
1354: assertTrue(
1355: "MethodEntryJoinPoint.getAopTag() contains correct method",
1356: method.equals(joinPoint.getAopTag()));
1357: }
1358:
1359: public void onMethodExit(MethodExitJoinPoint joinPoint) {
1360: Method method = joinPoint.getMethod();
1361: assertTrue(
1362: "MethodExitJoinPoint.getAopTag() contains correct method",
1363: method.equals(joinPoint.getAopTag()));
1364: }
1365:
1366: public void onExceptionThrow(ExceptionJoinPoint joinPoint) {
1367: Class exceptionClass = joinPoint.getException()
1368: .getClass();
1369: assertTrue(
1370: "ExceptionThrowJoinPoint.getAopTag() contains correct exception",
1371: exceptionClass.equals(joinPoint.getAopTag()));
1372: }
1373:
1374: public void onExceptionCatch(
1375: ExceptionCatchJoinPoint joinPoint) {
1376: Class exceptionCatchClass = joinPoint.getException()
1377: .getClass();
1378: assertTrue(
1379: "ExceptionCatchJoinPoint.getAopTag() contains correct exception",
1380: exceptionCatchClass.equals(joinPoint
1381: .getAopTag()));
1382: }
1383:
1384: public void onClassLoad(Class cls) {
1385: }
1386:
1387: public void onConstructor(ConstructorJoinPoint joinPoint) {
1388: }
1389: };
1390:
1391: aspectInterface.resumeNotification(Thread.currentThread());
1392: aspectInterface.setJoinPointHook(hook);
1393: test.method();
1394: test.field = test.field + 1;
1395: test.throwMethod();
1396:
1397: aspectInterface.clearMethodEntryWatch(method);
1398: aspectInterface.clearMethodExitWatch(method);
1399: aspectInterface.clearFieldAccessWatch(field);
1400: aspectInterface.clearFieldModificationWatch(field);
1401: aspectInterface.clearExceptionThrowWatch(TestException.class);
1402: aspectInterface.clearExceptionCatchWatch(TestException.class);
1403:
1404: aspectInterface.setJoinPointHook(null);
1405: aspectInterface.resumeNotification(Thread.currentThread());
1406:
1407: }
1408:
1409: public void test_0080_SuspendResumeNotification() {
1410:
1411: Object aopTag = new Object();
1412: int exceptions = 0;
1413: TestHook hook = new TestHook();
1414: TestClass1 test = new TestClass1();
1415: TestThread thread = null;
1416:
1417: aspectInterface.setMethodEntryWatch(method, aopTag);
1418: aspectInterface.setMethodExitWatch(method, aopTag);
1419: aspectInterface.setFieldAccessWatch(field, aopTag);
1420: aspectInterface.setFieldModificationWatch(field, aopTag);
1421: aspectInterface.setExceptionThrowWatch(TestException.class,
1422: aopTag);
1423: aspectInterface.setExceptionCatchWatch(TestException.class,
1424: aopTag);
1425:
1426: aspectInterface.resumeNotification(Thread.currentThread());
1427: aspectInterface.setJoinPointHook(hook);
1428:
1429: // do not suspend current thread and let it access test : 6 notifications
1430: hook.reset();
1431: test.run();
1432: assertEquals("no suspend test, notifications", 6, hook.oMEx
1433: + hook.oMEn + hook.oFA + hook.oFM + hook.oExt
1434: + hook.oExc);
1435:
1436: // suspend current thread and let it access test : 0 notifications
1437: hook.reset();
1438: aspectInterface.suspendNotification(Thread.currentThread());
1439: test.run();
1440: aspectInterface.resumeNotification(Thread.currentThread());
1441: assertTrue("no notifications", hook.oMEx + hook.oMEn + hook.oFA
1442: + hook.oFM + hook.oExt + hook.oExc == 0);
1443:
1444: // suspend a second thread and let the current thread access test : 6 notifications
1445: thread = new TestThread(test);
1446: hook.reset();
1447: aspectInterface.suspendNotification(thread);
1448: thread.start();
1449: test.run();
1450: try {
1451: thread.join();
1452: } catch (InterruptedException e) {
1453: }
1454: aspectInterface.resumeNotification(thread);
1455: assertEquals("notifications from this thread", 6, hook.oMEx
1456: + hook.oMEn + hook.oFA + hook.oFM + hook.oExt
1457: + hook.oExc);
1458:
1459: // let a second thread access test : 5 notifications
1460: thread = new TestThread(test);
1461: hook.reset();
1462: thread.start();
1463: try {
1464: thread.join();
1465: } catch (InterruptedException e) {
1466: }
1467: assertEquals("notifications from other thread", 6, hook.oMEx
1468: + hook.oMEn + hook.oFA + hook.oFM + hook.oExt
1469: + hook.oExc);
1470:
1471: // suspend the second thread and let it access testClass : 0 notifications
1472: thread = new TestThread(test);
1473: hook.reset();
1474:
1475: aspectInterface.suspendNotification(thread);
1476: // test.method();
1477: //test.field = test.field + 1;
1478: //test.throwMethod();
1479: //aspectInterface.resumeNotification(Thread.currentThread());
1480: thread.start();
1481: try {
1482: thread.join();
1483: } catch (InterruptedException e) {
1484: }
1485: aspectInterface.resumeNotification(thread);
1486: assertEquals("notifications after suspend", 0, hook.oMEx
1487: + hook.oMEn + hook.oFA + hook.oFM + hook.oExt
1488: + hook.oExc);
1489:
1490: aspectInterface.clearMethodEntryWatch(method);
1491: aspectInterface.clearMethodExitWatch(method);
1492: aspectInterface.clearFieldAccessWatch(field);
1493: aspectInterface.clearFieldModificationWatch(field);
1494: aspectInterface.clearExceptionThrowWatch(TestException.class);
1495: aspectInterface.clearExceptionCatchWatch(TestException.class);
1496: aspectInterface.resumeNotification(Thread.currentThread());
1497: }
1498:
1499: public void test_0090_ClassLoadNotification() throws Exception {
1500: TestHook hook = new TestHook();
1501: aspectInterface.setJoinPointHook(hook);
1502: Class cls = Class
1503: .forName("ch.ethz.prose.UnreferencedTestClass");
1504: aspectInterface.setJoinPointHook(null);
1505: assertEquals("Number of 'onClassLoad' calls was increased", 1,
1506: hook.oCL);
1507: }
1508:
1509: public void test_0100_ModifiersAndInheritance() {
1510:
1511: TestHook hook = null;
1512: TestClass4 test = null;
1513: Method[] methods = null;
1514: Field[] fields = null;
1515:
1516: methods = TestClass4.class.getDeclaredMethods();
1517: fields = TestClass4.class.getDeclaredFields();
1518: for (int m = 0; m < methods.length; m++) {
1519: aspectInterface.setMethodEntryWatch(methods[m], "entag");
1520: aspectInterface.setMethodExitWatch(methods[m], "extag");
1521: }
1522:
1523: for (int f = 0; f < fields.length; f++) {
1524: aspectInterface
1525: .setFieldAccessWatch(fields[f], new Object());
1526: aspectInterface.setFieldModificationWatch(fields[f],
1527: new Object());
1528: }
1529:
1530: test = new TestClass4();
1531: hook = new TestHook();
1532: aspectInterface.setJoinPointHook(hook);
1533: aspectInterface.resumeNotification(Thread.currentThread());
1534:
1535: test.callMethods();
1536: test.accessFields();
1537: aspectInterface.setJoinPointHook(null);
1538: assertTrue(hook.oMEn + " of " + methods.length
1539: + " method entry events received",
1540: hook.oMEn == methods.length);
1541: assertTrue(hook.oMEx + " of " + methods.length
1542: + " method entry events received",
1543: hook.oMEx == methods.length);
1544: assertTrue(hook.oFA + " of " + fields.length
1545: + " field access events received",
1546: hook.oFA == fields.length);
1547: assertTrue(hook.oFM + " of " + fields.length
1548: + " field modification events received",
1549: hook.oFM == fields.length);
1550:
1551: for (int m = 0; m < methods.length; m++) {
1552: aspectInterface.clearMethodEntryWatch(methods[m]);
1553: aspectInterface.clearMethodExitWatch(methods[m]);
1554: }
1555: for (int f = 0; f < fields.length; f++) {
1556: aspectInterface.clearFieldAccessWatch(fields[f]);
1557: aspectInterface.clearFieldModificationWatch(fields[f]);
1558: }
1559: aspectInterface.resumeNotification(Thread.currentThread());
1560: }
1561:
1562: public void test_0110_MethodExitJoinPoints() {
1563:
1564: TestHook hook = null;
1565: TestClass5 test = null;
1566: Method[] methods = null;
1567:
1568: methods = TestClass5.class.getDeclaredMethods();
1569:
1570: for (int m = 0; m < methods.length; m++)
1571: aspectInterface.setMethodExitWatch(methods[m], "extag");
1572:
1573: aspectInterface.resumeNotification(Thread.currentThread());
1574:
1575: test = new TestClass5();
1576: hook = new TestHook();
1577: aspectInterface.setJoinPointHook(hook);
1578: for (int exitPoint = 0; exitPoint < 5; exitPoint++) {
1579: test.exit = exitPoint;
1580: test.method0();
1581: test.method1();
1582: test.method2();
1583: test.method3();
1584: }
1585:
1586: aspectInterface.setJoinPointHook(null);
1587:
1588: assertEquals(hook.oMEx + " of " + methods.length
1589: + " method entry events received", methods.length * 5,
1590: hook.oMEx);
1591:
1592: for (int m = 0; m < methods.length; m++)
1593: aspectInterface.clearMethodExitWatch(methods[m]);
1594: aspectInterface.resumeNotification(Thread.currentThread());
1595: }
1596:
1597: public void test_0120_CatchThrow() {
1598: Object aopTag = new Object();
1599: Object aopTag2 = new Object();
1600: TestHook hook = new TestHook();
1601: TestCatch testCatch = new TestCatch();
1602: int exceptions = 0;
1603:
1604: aspectInterface.setExceptionThrowWatch(TestException.class,
1605: aopTag);
1606: aspectInterface.setExceptionCatchWatch(TestException.class,
1607: aopTag);
1608: aspectInterface.setJoinPointHook(hook);
1609:
1610: hook.reset();
1611: testCatch.field1 = 0;
1612: testCatch.field1 = testCatch.field1 + 1;
1613: testCatch.throwMethod2();
1614:
1615: assertEquals("1 exception throw event(1)", 1, hook.oExt);
1616: assertEquals("1 exception catch event(1)", 1, hook.oExc);
1617:
1618: //*************************
1619:
1620: // check if setting again the same hook changes the result
1621: aspectInterface.setJoinPointHook(hook);
1622: hook.reset();
1623: testCatch.field1 = 0;
1624: testCatch.field1 = testCatch.field1 + 1;
1625: testCatch.throwMethod2();
1626:
1627: assertEquals("1 exception throw event(2)", 1, hook.oExt);
1628: assertEquals("1 exception catch event(2)", 1, hook.oExc);
1629:
1630: //*************************
1631:
1632: // check wether events are forwarded to the hook after he has been removed (set 'null')
1633: aspectInterface.setJoinPointHook(null);
1634:
1635: hook.reset();
1636: testCatch.field1 = 0;
1637: testCatch.field1 = testCatch.field1 + 1;
1638: testCatch.throwMethod2();
1639:
1640: assertEquals("no exception throw events", 0, hook.oExt);
1641: assertEquals("no exception catch events", 0, hook.oExc);
1642:
1643: //*************************
1644:
1645: // check if the events are forwarded to the hook after it has been set
1646: aspectInterface.setJoinPointHook(hook);
1647: hook.reset();
1648: testCatch.field1 = 0;
1649: testCatch.field1 = testCatch.field1 + 1;
1650: testCatch.throwMethod2();
1651:
1652: assertEquals("1 exception throw event", 1, hook.oExt);
1653: assertEquals("1 exception catch event", 1, hook.oExc);
1654:
1655: aspectInterface.clearExceptionThrowWatch(TestException.class);
1656: aspectInterface.clearExceptionCatchWatch(TestException.class);
1657:
1658: //*************************
1659:
1660: // try to set watches twice : every second set**Watch should throw a WatchAlreadySetException
1661: exceptions = 0;
1662:
1663: aspectInterface.setExceptionThrowWatch(TestException.class,
1664: aopTag);
1665: try {
1666: aspectInterface.setExceptionThrowWatch(TestException.class,
1667: aopTag);
1668: } catch (WatchAlreadySetException e) {
1669: exceptions++;
1670: }
1671:
1672: aspectInterface.setExceptionCatchWatch(TestException.class,
1673: aopTag);
1674: try {
1675: aspectInterface.setExceptionCatchWatch(TestException.class,
1676: aopTag);
1677: } catch (WatchAlreadySetException e) {
1678: exceptions++;
1679: }
1680:
1681: assertEquals("2 WatchAlreadySetExceptions", 2, exceptions);
1682:
1683: // try again to clear watches twice : every second clear**Watch should throw a WatchNotSetException
1684: exceptions = 0;
1685:
1686: aspectInterface.clearExceptionThrowWatch(TestException.class);
1687: try {
1688: aspectInterface
1689: .clearExceptionThrowWatch(TestException.class);
1690: } catch (WatchNotSetException e) {
1691: exceptions++;
1692: }
1693:
1694: aspectInterface.clearExceptionCatchWatch(TestException.class);
1695: try {
1696: aspectInterface
1697: .clearExceptionCatchWatch(TestException.class);
1698: } catch (WatchNotSetException e) {
1699: exceptions++;
1700: }
1701:
1702: assertEquals("2 WatchNotSetExceptions", 2, exceptions);
1703:
1704: //*************************
1705:
1706: // set 3 different watches for 3 types of exceptions for catch and throw
1707: aspectInterface.setExceptionThrowWatch(TestException.class,
1708: aopTag);
1709: aspectInterface.setExceptionThrowWatch(TestException2.class,
1710: aopTag);
1711: aspectInterface.setExceptionThrowWatch(TestException3.class,
1712: aopTag);
1713:
1714: aspectInterface.setExceptionCatchWatch(TestException.class,
1715: aopTag);
1716: aspectInterface.setExceptionCatchWatch(TestException2.class,
1717: aopTag);
1718: aspectInterface.setExceptionCatchWatch(TestException3.class,
1719: aopTag);
1720:
1721: aspectInterface.setJoinPointHook(hook);
1722:
1723: // check if 3 catch and throw events are forwarded to the hook after it has been set
1724: hook.reset();
1725: testCatch.field1 = 0;
1726: testCatch.field2 = 0;
1727: testCatch.field1 = testCatch.field1 + 2;
1728: testCatch.field2 = testCatch.field2 + 2;
1729: testCatch.throwMethod4();
1730: testCatch.throwMethod5();
1731: testCatch.throwMethod3();
1732:
1733: assertEquals(
1734: "3 exceptions throw events for TestException, 2 and 3",
1735: 3, hook.oExt);
1736: assertEquals(
1737: "3 exceptions catch events for TestException, 2 and 3",
1738: 3, hook.oExc);
1739:
1740: //*************************
1741: // check if 3 catch and throw events are forwarded to the hook after it has been set
1742: hook.reset();
1743: testCatch.field1 = 0;
1744: testCatch.field2 = 0;
1745: testCatch.field1 = testCatch.field1 + 3;
1746: testCatch.field2 = testCatch.field2 + 3;
1747: testCatch.throwMethod4();
1748: testCatch.throwMethod5();
1749: testCatch.throwMethod3();
1750:
1751: assertEquals(
1752: "1 exception throw events for TestException, 2 and 3",
1753: 1, hook.oExt);
1754: assertEquals(
1755: "1 exception catch events for TestException, 2 and 3",
1756: 1, hook.oExc);
1757:
1758: aspectInterface.clearExceptionThrowWatch(TestException.class);
1759: aspectInterface.clearExceptionThrowWatch(TestException2.class);
1760: aspectInterface.clearExceptionThrowWatch(TestException3.class);
1761:
1762: aspectInterface.clearExceptionCatchWatch(TestException.class);
1763: aspectInterface.clearExceptionCatchWatch(TestException2.class);
1764: aspectInterface.clearExceptionCatchWatch(TestException3.class);
1765:
1766: //*************************
1767:
1768: // set throw and catch waches on TestException4
1769: aspectInterface.setExceptionThrowWatch(TestException4.class,
1770: aopTag);
1771: aspectInterface.setExceptionCatchWatch(TestException4.class,
1772: aopTag);
1773:
1774: //aspectInterface.setExceptionThrowWatch (TestException2.class, aopTag);
1775: //aspectInterface.setExceptionCatchWatch (TestException2.class, aopTag);
1776:
1777: aspectInterface.setJoinPointHook(hook);
1778:
1779: //check if only the events for the catch wach that was set are forwarded to the hook after it has been set
1780: hook.reset();
1781: testCatch.field2 = 0;
1782: testCatch.field2 = testCatch.field2 + 2;
1783: testCatch.throwMethod6();
1784: testCatch.throwMethod4();
1785:
1786: assertEquals("1 exception throw events for TestException4", 1,
1787: hook.oExt);
1788: assertEquals("1 exception catch events for TestException4", 1,
1789: hook.oExc);
1790:
1791: aspectInterface.clearExceptionThrowWatch(TestException4.class);
1792: aspectInterface.clearExceptionCatchWatch(TestException4.class);
1793:
1794: //aspectInterface.clearExceptionThrowWatch (TestException2.class);
1795: //aspectInterface.clearExceptionCatchWatch (TestException2.class);
1796: }
1797:
1798: /**
1799: * Test suite.
1800: * @return test instance
1801: */
1802: public static Test suite() {
1803: return new TestSuite(JVMAspectInterfaceTest.class);
1804: }
1805:
1806: }
1807:
1808: //======================================================================
1809: //
1810: // $Log: JVMAspectInterfaceTest.java,v $
1811: // Revision 1.3 2004/05/12 17:26:51 anicoara
1812: // Adapt Junit tests to 3.8.1 version and the new package structure
1813: //
1814: // Revision 1.1.1.1 2003/07/02 15:30:42 apopovic
1815: // Imported from ETH Zurich
1816: //
1817: // Revision 1.2 2003/07/02 12:42:33 anicoara
1818: // Added CatchJoinPoint Functionality (Requests, Join-Points, Filters, CatchCuts, Tests)
1819: //
1820: // Revision 1.1 2003/05/05 14:02:29 popovici
1821: // renaming from runes to prose
1822: //
1823: // Revision 1.4 2003/04/30 20:16:17 popovici
1824: // Changes for windows portability
1825: //
1826: // Revision 1.3 2003/04/26 14:09:55 popovici
1827: // Fixed test class load test; an old string used to be used
1828: //
1829: // Revision 1.2 2003/03/04 18:36:06 popovici
1830: // Organization of imprts
1831: //
1832: // Revision 1.1 2003/03/04 12:10:27 popovici
1833: // Moved from inf/jvmai (whitebox location) to inf/runes/ (bb location)
1834: //
1835: // Revision 1.8 2002/11/27 17:09:31 popovici
1836: // asertions more verbose, changes to show exaclty where the
1837: // exception bug appears in 1.4.
1838: //
1839: // Revision 1.7 2002/10/17 17:05:45 pschoch
1840: // Added throw capabability to JVMAI
1841: //
1842: // Revision 1.6 2002/10/17 12:23:37 pschoch
1843: // Added throw capabability to JVMAI
1844: //
1845: // Revision 1.5 2002/05/07 10:41:28 popovici
1846: // Minor improvement in the junit message error
1847: //
1848: // Revision 1.4 2002/03/11 11:02:21 smarkwal
1849: // JVMInfoInterface and JoinPointHook changed to abstract classes
1850: //
1851: // Revision 1.3 2002/02/18 14:41:38 popovici
1852: // testcases JVMAspect interfaces relaxed to allow null aop tags
1853: //
1854: // Revision 1.2 2002/02/15 12:27:11 smarkwal
1855: // doesn't rely anymore on Provider.getProvider(...)
1856: //
1857: // Revision 1.1 2002/02/05 11:13:09 smarkwal
1858: // Initial revision
1859: //
1860: //
|