1 16 17 package org.springframework.aop.framework; 18 19 import junit.framework.TestCase; 20 import org.aopalliance.aop.Advice; 21 import org.aopalliance.intercept.Interceptor; 22 23 import org.springframework.aop.Advisor; 24 import org.springframework.aop.interceptor.NopInterceptor; 25 import org.springframework.aop.support.DefaultIntroductionAdvisor; 26 import org.springframework.aop.support.DefaultPointcutAdvisor; 27 import org.springframework.beans.IOther; 28 import org.springframework.beans.ITestBean; 29 import org.springframework.beans.TestBean; 30 import org.springframework.transaction.interceptor.TransactionInterceptor; 31 32 38 public class ProxyFactoryTests extends TestCase { 39 40 public void testNullTarget() { 41 try { 42 new ProxyFactory((Object ) null); 44 fail("Should't allow proxy with null target"); 45 } 46 catch (AopConfigException ex) { 47 } 48 } 49 50 public void testIndexOfMethods() { 51 TestBean target = new TestBean(); 52 ProxyFactory pf = new ProxyFactory(target); 53 NopInterceptor nop = new NopInterceptor(); 54 Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice()); 55 Advised advised = (Advised) pf.getProxy(); 56 advised.addAdvice(nop); 58 pf.addAdvisor(advisor); 59 assertEquals(-1, pf.indexOf((Advice) null)); 60 assertEquals(-1, pf.indexOf(new NopInterceptor())); 61 assertEquals(0, pf.indexOf(nop)); 62 assertEquals(-1, advised.indexOf((Advisor) null)); 63 assertEquals(1, pf.indexOf(advisor)); 64 assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null))); 65 } 66 67 public void testRemoveAdvisorByReference() { 68 TestBean target = new TestBean(); 69 ProxyFactory pf = new ProxyFactory(target); 70 NopInterceptor nop = new NopInterceptor(); 71 CountingBeforeAdvice cba = new CountingBeforeAdvice(); 72 Advisor advisor = new DefaultPointcutAdvisor(cba); 73 pf.addAdvice(nop); 74 pf.addAdvisor(advisor); 75 ITestBean proxied = (ITestBean) pf.getProxy(); 76 proxied.setAge(5); 77 assertEquals(1, cba.getCalls()); 78 assertEquals(1, nop.getCount()); 79 assertFalse(pf.removeAdvisor(null)); 80 assertTrue(pf.removeAdvisor(advisor)); 81 assertEquals(5, proxied.getAge()); 82 assertEquals(1, cba.getCalls()); 83 assertEquals(2, nop.getCount()); 84 assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null))); 85 } 86 87 88 public void testRemoveAdvisorByIndex() { 89 TestBean target = new TestBean(); 90 ProxyFactory pf = new ProxyFactory(target); 91 NopInterceptor nop = new NopInterceptor(); 92 CountingBeforeAdvice cba = new CountingBeforeAdvice(); 93 Advisor advisor = new DefaultPointcutAdvisor(cba); 94 pf.addAdvice(nop); 95 pf.addAdvisor(advisor); 96 NopInterceptor nop2 = new NopInterceptor(); 97 pf.addAdvice(nop2); 98 ITestBean proxied = (ITestBean) pf.getProxy(); 99 proxied.setAge(5); 100 assertEquals(1, cba.getCalls()); 101 assertEquals(1, nop.getCount()); 102 assertEquals(1, nop2.getCount()); 103 pf.removeAdvisor(1); 105 assertEquals(5, proxied.getAge()); 106 assertEquals(1, cba.getCalls()); 107 assertEquals(2, nop.getCount()); 108 assertEquals(2, nop2.getCount()); 109 pf.removeAdvisor(0); 111 assertEquals(5, proxied.getAge()); 112 assertEquals(1, cba.getCalls()); 113 assertEquals(2, nop.getCount()); 114 assertEquals(3, nop2.getCount()); 115 116 try { 118 pf.removeAdvisor(-1); 119 } 120 catch (AopConfigException ex) { 121 } 123 124 try { 125 pf.removeAdvisor(2); 126 } 127 catch (AopConfigException ex) { 128 } 130 131 assertEquals(5, proxied.getAge()); 132 assertEquals(4, nop2.getCount()); 133 } 134 135 136 public void testReplaceAdvisor() { 137 TestBean target = new TestBean(); 138 ProxyFactory pf = new ProxyFactory(target); 139 NopInterceptor nop = new NopInterceptor(); 140 CountingBeforeAdvice cba1 = new CountingBeforeAdvice(); 141 CountingBeforeAdvice cba2 = new CountingBeforeAdvice(); 142 Advisor advisor1 = new DefaultPointcutAdvisor(cba1); 143 Advisor advisor2 = new DefaultPointcutAdvisor(cba2); 144 pf.addAdvisor(advisor1); 145 pf.addAdvice(nop); 146 ITestBean proxied = (ITestBean) pf.getProxy(); 147 Advised advised = (Advised) proxied; 150 proxied.setAge(5); 151 assertEquals(1, cba1.getCalls()); 152 assertEquals(0, cba2.getCalls()); 153 assertEquals(1, nop.getCount()); 154 assertFalse(advised.replaceAdvisor(null, null)); 155 assertFalse(advised.replaceAdvisor(null, advisor2)); 156 assertFalse(advised.replaceAdvisor(advisor1, null)); 157 assertTrue(advised.replaceAdvisor(advisor1, advisor2)); 158 assertEquals(advisor2, pf.getAdvisors()[0]); 159 assertEquals(5, proxied.getAge()); 160 assertEquals(1, cba1.getCalls()); 161 assertEquals(2, nop.getCount()); 162 assertEquals(1, cba2.getCalls()); 163 assertFalse(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)); 164 } 165 166 public static class Concrete { 167 public void foo() { 168 } 169 } 170 171 public void testAddRepeatedInterface() { 172 TimeStamped tst = new TimeStamped() { 173 public long getTimeStamp() { 174 throw new UnsupportedOperationException ("getTimeStamp"); 175 } 176 }; 177 ProxyFactory pf = new ProxyFactory(tst); 178 pf.addInterface(TimeStamped.class); 181 TimeStamped ts = (TimeStamped) pf.getProxy(); 183 } 184 185 public void testGetsAllInterfaces() throws Exception { 186 class TestBeanSubclass extends TestBean implements Comparable { 188 public int compareTo(Object arg0) { 189 throw new UnsupportedOperationException ("compareTo"); 190 } 191 } 192 TestBeanSubclass raw = new TestBeanSubclass(); 193 ProxyFactory factory = new ProxyFactory(raw); 194 assertEquals("Found correct number of interfaces", 5, factory.getProxiedInterfaces().length); 195 ITestBean tb = (ITestBean) factory.getProxy(); 197 assertTrue("Picked up secondary interface", tb instanceof IOther); 198 199 raw.setAge(25); 200 assertTrue(tb.getAge() == raw.getAge()); 201 202 long t = 555555L; 203 TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t); 204 205 Class [] oldProxiedInterfaces = factory.getProxiedInterfaces(); 206 207 factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class)); 208 209 Class [] newProxiedInterfaces = factory.getProxiedInterfaces(); 210 assertEquals("Advisor proxies one more interface after introduction", oldProxiedInterfaces.length + 1, newProxiedInterfaces.length); 211 212 TimeStamped ts = (TimeStamped) factory.getProxy(); 213 assertTrue(ts.getTimeStamp() == t); 214 ((IOther) ts).absquatulate(); 216 } 217 218 public void testCanOnlyAddMethodInterceptors() { 219 ProxyFactory factory = new ProxyFactory(new TestBean()); 220 factory.addAdvice(0, new NopInterceptor()); 221 try { 222 factory.addAdvice(0, new Interceptor() { 223 }); 224 fail("Should only be able to add MethodInterceptors"); 225 } 226 catch (AopConfigException ex) { 227 } 228 229 IOther other = (IOther) factory.getProxy(); 231 other.absquatulate(); 232 } 233 234 public void testInterceptorInclusionMethods() { 235 NopInterceptor di = new NopInterceptor(); 236 NopInterceptor diUnused = new NopInterceptor(); 237 ProxyFactory factory = new ProxyFactory(new TestBean()); 238 factory.addAdvice(0, di); 239 ITestBean tb = (ITestBean) factory.getProxy(); 240 assertTrue(factory.adviceIncluded(di)); 241 assertTrue(!factory.adviceIncluded(diUnused)); 242 assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 1); 243 assertTrue(factory.countAdvicesOfType(TransactionInterceptor.class) == 0); 244 245 factory.addAdvice(0, diUnused); 246 assertTrue(factory.adviceIncluded(diUnused)); 247 assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 2); 248 } 249 250 } 251 | Popular Tags |