1 7 package com.tirsen.nanning; 8 9 import com.tirsen.nanning.definition.AspectClass; 10 import com.tirsen.nanning.definition.AspectDefinition; 11 import junit.framework.TestCase; 12 13 21 public class AspectClassTest extends TestCase { 22 public static class BlahongaException extends RuntimeException { 23 } 24 25 public void testThrowsCorrectExceptions() { 26 AspectClass aspectClass = new AspectClass(); 27 aspectClass.setInterface(Intf.class); 28 aspectClass.addInterceptor(MockInterceptor.class); 29 aspectClass.addInterceptor(MockInterceptor.class); 30 aspectClass.setTarget(IntfImpl.class); 31 32 Intf proxy = (Intf) aspectClass.newInstance(); 33 34 Aspects.setTarget(proxy, Intf.class, new IntfImpl() { 35 public void call() { 36 throw new BlahongaException(); 37 } 38 }); 39 40 try { 41 proxy.call(); 42 fail(); 43 } catch (BlahongaException shouldHappen) { 44 } catch (Exception e) { 45 fail(); 46 } 47 } 48 49 public void testSideAspectAndAspectsOnProxy() throws IllegalAccessException , InstantiationException , NoSuchMethodException { 50 AspectClass aspectClass = new AspectClass(); 51 aspectClass.setInterface(Intf.class); 52 aspectClass.addInterceptor(MockInterceptor.class); 53 aspectClass.addInterceptor(NullInterceptor.class); 54 aspectClass.setTarget(IntfImpl.class); 55 AspectDefinition aspectDefinition = new AspectDefinition(); 56 aspectDefinition.setInterface(TestMixin.class); 57 aspectDefinition.addInterceptor(NullInterceptor.class); 58 aspectDefinition.addInterceptor(MockInterceptor.class); 59 aspectDefinition.setTarget(TestMixinImpl.class); 60 aspectClass.addAspect(aspectDefinition); 61 62 Object bigMomma = aspectClass.newInstance(); 63 64 assertEquals(4, Aspects.getInterceptors(bigMomma).size()); 65 66 verifySideAspect(bigMomma); 67 } 68 69 public static void verifySideAspect(Object bigMomma) throws NoSuchMethodException { 70 IntfImpl target = (IntfImpl) Aspects.getTarget(bigMomma, Intf.class); 71 target.expectThis(bigMomma); 72 MockInterceptor classInterceptor = 73 (MockInterceptor) (Aspects.getInterceptors(bigMomma, Intf.class.getMethod("call", null))[0]); 74 classInterceptor.expectAtIndex(0); 75 classInterceptor.expectNumberOfInterceptors(2); 76 classInterceptor.expectCalledTimes(1); 77 classInterceptor.expectProxy(bigMomma); 78 classInterceptor.expectMethod(Intf.class.getMethod("call", null)); 79 classInterceptor.expectTarget(target); 80 81 TestMixinImpl sideTarget = (TestMixinImpl) Aspects.getTarget(bigMomma, TestMixin.class); 82 MockInterceptor sideInterceptor = 83 (MockInterceptor) (Aspects.getInterceptors(bigMomma, TestMixin.class.getMethod("mixinCall", null))[1]); 84 sideInterceptor.expectAtIndex(1); 85 sideInterceptor.expectNumberOfInterceptors(2); 86 sideInterceptor.expectCalledTimes(1); 87 sideInterceptor.expectProxy(bigMomma); 88 sideInterceptor.expectMethod(TestMixin.class.getMethod("mixinCall", null)); 89 sideInterceptor.expectTarget(sideTarget); 90 91 ((Intf) bigMomma).call(); 93 classInterceptor.expectTarget(null); 95 classInterceptor.expectMethod(null); 96 classInterceptor.expectNumberOfInterceptors(2); 97 ((TestMixin) bigMomma).mixinCall(); 98 99 classInterceptor.verify(); 100 target.verify(); 101 sideInterceptor.verify(); 102 sideTarget.verify(); 103 } 104 105 public void testNoAspects() throws IllegalAccessException , InstantiationException { 106 AspectClass aspectClass = new AspectClass(); 107 aspectClass.setInterface(Intf.class); 108 aspectClass.setTarget(IntfImpl.class); 109 Intf intf = (Intf) aspectClass.newInstance(); 110 111 IntfImpl impl = (IntfImpl) Aspects.getTarget(intf, Intf.class); 112 113 intf.call(); 114 impl.verify(); 115 } 116 } 117 | Popular Tags |