1 7 package com.tirsen.nanning; 8 9 import java.util.ArrayList ; 10 import java.util.List ; 11 12 import com.tirsen.nanning.definition.AspectClass; 13 import com.tirsen.nanning.definition.BasicInterceptor; 14 import com.tirsen.nanning.definition.InterceptorDefinition; 15 import junit.framework.TestCase; 16 17 25 26 public class MethodFilterTest extends TestCase { 27 28 private static List invokedMethods; 29 private static List expectedMethods; 30 31 protected void setUp() throws Exception { 32 invokedMethods = new ArrayList (); 33 expectedMethods = new ArrayList (); 34 } 35 36 42 private Object createAspectProxy(String filter) { 43 AspectClass aspectClass = new AspectClass(); 44 aspectClass.setInterface(SomeAspect.class); 45 InterceptorDefinition interceptorDefinition = new InterceptorDefinition(TestFilterMethodsInterceptor.class); 46 interceptorDefinition.setAttribute("methodNameFilter", filter); 47 aspectClass.addInterceptor(interceptorDefinition); 48 aspectClass.setTarget(SomeAspectImpl.class); 49 50 return aspectClass.newInstance(); 51 52 } 53 54 55 public void testA() { 56 Object proxy = createAspectProxy("doIt.*"); 57 expectedMethods.add("doIt"); 58 expectedMethods.add("doItAgain"); 59 SomeAspect aspect = (SomeAspect) proxy; 60 61 aspect.doIt(); 62 aspect.doItAgain(); 63 aspect.oupsIDidItAgain(); 64 65 verify(); 66 } 67 68 69 public void testB() { 70 Object proxy = createAspectProxy(".*Again.*"); 71 expectedMethods.add("doItAgain"); 72 expectedMethods.add("oupsIDidItAgain"); 73 74 SomeAspect aspect = (SomeAspect) proxy; 75 76 aspect.doIt(); 77 aspect.doItAgain(); 78 aspect.oupsIDidItAgain(); 79 80 verify(); 81 } 82 83 public void testC() { 84 Object proxy = createAspectProxy(".*"); 85 expectedMethods.add("doIt"); 86 expectedMethods.add("doItAgain"); 87 expectedMethods.add("oupsIDidItAgain"); 88 89 SomeAspect aspect = (SomeAspect) proxy; 90 aspect.doIt(); 91 aspect.doItAgain(); 92 aspect.oupsIDidItAgain(); 93 94 verify(); 95 } 96 97 public void testD() { 98 Object proxy = createAspectProxy("noMethodLikeThat"); 99 100 SomeAspect aspect = (SomeAspect) proxy; 101 aspect.doIt(); 102 aspect.doItAgain(); 103 aspect.oupsIDidItAgain(); 104 105 verify(); 106 } 107 108 private void verify() { 109 assertEquals("number of executed methods was not as expected", 110 expectedMethods.size(), invokedMethods.size()); 111 for (int i = 0; i < expectedMethods.size(); i++) { 112 String methodName = (String ) expectedMethods.get(i); 113 assertTrue(methodName + " was not invoked", invokedMethods.contains(methodName)); 114 } 115 } 116 117 118 public static class TestFilterMethodsInterceptor extends BasicInterceptor { 119 public Object invoke(Invocation invocation) throws Throwable { 120 String methodName = invocation.getMethod().getName(); 121 123 invokedMethods.add(methodName); 124 return invocation.invokeNext(); 125 } 126 127 } 128 129 130 public static class SomeAspectImpl implements SomeAspect { 131 public boolean doIt() { 132 return true; 134 } 135 136 137 public boolean doItAgain() { 138 return true; 140 } 141 142 public boolean oupsIDidItAgain() { 143 return true; 144 } 145 146 147 } 148 149 public static interface SomeAspect { 150 public boolean doIt(); 151 152 public boolean doItAgain(); 153 154 public boolean oupsIDidItAgain(); 155 156 } 157 158 159 } 160
| Popular Tags
|