| 1 package com.tirsen.nanning.samples; 2 3 import com.tirsen.nanning.attribute.AbstractAttributesTest; 4 import com.tirsen.nanning.AspectInstance; 5 import com.tirsen.nanning.MixinInstance; 6 7 import java.lang.reflect.Method ; 8 9 public class CacheInterceptorTest extends AbstractAttributesTest { 10 private Method someHeavyCalculationMethod; 11 private CacheInterceptor cacheInterceptor; 12 private boolean wasCalled; 13 14 protected void setUp() throws Exception { 15 super.setUp(); 16 17 cacheInterceptor = new CacheInterceptor(); 18 someHeavyCalculationMethod = 19 CacheTestCalculations.class.getMethod("someHeavyCalculation", 20 new Class [] { double.class } ); 21 } 22 23 public void testIntercept() { 24 AspectInstance aspectInstance = new AspectInstance(); 25 MixinInstance mixin = new MixinInstance(CacheTestCalculations.class, null); 26 mixin.addInterceptor(someHeavyCalculationMethod, cacheInterceptor); 27 aspectInstance.addMixin(mixin); 28 mixin.setTarget(new CacheTestCalculations() { 29 public double someHeavyCalculation(double input) { 30 wasCalled = true; 31 return input; 32 } 33 }); 34 CacheTestCalculations cacheTestCalculations = (CacheTestCalculations) aspectInstance.getProxy(); 35 36 assertFalse(wasCalled); 38 assertEquals(0, cacheTestCalculations.someHeavyCalculation(0), 0); 39 assertTrue(wasCalled); 40 41 wasCalled = false; 43 assertEquals(0, cacheTestCalculations.someHeavyCalculation(0), 0); 44 assertFalse(wasCalled); 45 46 cacheInterceptor.clearCache(); 48 wasCalled = false; 49 assertEquals(0, cacheTestCalculations.someHeavyCalculation(0), 0); 50 assertTrue(wasCalled); 51 } 52 53 public void testFilterMethod() throws NoSuchMethodException { 54 assertTrue(cacheInterceptor.interceptsMethod( 55 someHeavyCalculationMethod)); 56 assertFalse(cacheInterceptor.interceptsMethod( 57 Object .class.getMethod("toString", null))); 58 } 59 } 60 | Popular Tags |