1 16 17 package org.springframework.aop.framework.autoproxy; 18 19 import java.io.IOException ; 20 21 import junit.framework.AssertionFailedError; 22 import junit.framework.TestCase; 23 24 import org.springframework.aop.framework.CountingBeforeAdvice; 25 import org.springframework.aop.framework.Lockable; 26 import org.springframework.aop.framework.LockedException; 27 import org.springframework.aop.framework.TimeStamped; 28 import org.springframework.aop.interceptor.NopInterceptor; 29 import org.springframework.aop.support.AopUtils; 30 import org.springframework.beans.ITestBean; 31 import org.springframework.beans.TestBean; 32 import org.springframework.beans.factory.BeanFactory; 33 import org.springframework.context.support.ClassPathXmlApplicationContext; 34 35 40 public class BeanNameAutoProxyCreatorTests extends TestCase { 41 42 private BeanFactory bf; 43 44 protected void setUp() throws IOException { 45 this.bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/beanNameAutoProxyCreatorTests.xml"); 48 } 49 50 public void testNoProxy() { 51 TestBean tb = (TestBean) bf.getBean("noproxy"); 52 assertFalse(AopUtils.isAopProxy(tb)); 53 assertEquals("noproxy", tb.getName()); 54 } 55 56 public void testJdkProxyWithExactNameMatch() { 57 ITestBean tb = (ITestBean) bf.getBean("onlyJdk"); 58 jdkAssertions(tb); 59 assertEquals("onlyJdk", tb.getName()); 60 } 61 62 public void testJdkIntroduction() { 63 ITestBean tb = (ITestBean) bf.getBean("introductionUsingJdk"); 64 NopInterceptor nop = (NopInterceptor) bf.getBean("introductionNopInterceptor"); 65 assertEquals(0, nop.getCount()); 66 assertTrue(AopUtils.isJdkDynamicProxy(tb)); 67 int age = 5; 68 tb.setAge(age); 69 assertEquals(age, tb.getAge()); 70 assertTrue("Introduction was made", tb instanceof TimeStamped); 71 assertEquals(0, ((TimeStamped) tb).getTimeStamp()); 72 assertEquals(3, nop.getCount()); 73 assertEquals("introductionUsingJdk", tb.getName()); 74 75 ITestBean tb2 = (ITestBean) bf.getBean("second-introductionUsingJdk"); 76 77 Lockable lockable1 = (Lockable) tb; 79 Lockable lockable2 = (Lockable) tb2; 80 assertFalse(lockable1.locked()); 81 assertFalse(lockable2.locked()); 82 tb.setAge(65); 83 assertEquals(65, tb.getAge()); 84 lockable1.lock(); 85 assertTrue(lockable1.locked()); 86 assertFalse(lockable2.locked()); 88 tb2.setAge(12); 90 try { 92 tb.setAge(6); 93 fail("Mixin should have locked this object"); 94 } 95 catch (LockedException ex) { 96 } 98 } 99 100 105 public void testIntroductionOnFactoryBean() { 106 try { 107 BUGtestJdkIntroductionAppliesToCreatedObjectsNotFactoryBean(); 108 fail(); 109 } 110 catch (AssertionFailedError ex) { 111 System.err.println("****** SPR 337: Autoproxying currently applies to FactoryBeans, not objects they create"); 112 } 113 } 114 115 public void BUGtestJdkIntroductionAppliesToCreatedObjectsNotFactoryBean() { 116 ITestBean tb = (ITestBean) bf.getBean("factory-introductionUsingJdk"); 117 NopInterceptor nop = (NopInterceptor) bf.getBean("introductionNopInterceptor"); 118 assertEquals("NOP should not have done any work yet", 0, nop.getCount()); 119 assertTrue(AopUtils.isJdkDynamicProxy(tb)); 120 int age = 5; 121 tb.setAge(age); 122 assertEquals(age, tb.getAge()); 123 assertTrue("Introduction was made", tb instanceof TimeStamped); 124 assertEquals(0, ((TimeStamped) tb).getTimeStamp()); 125 assertEquals(3, nop.getCount()); 126 assertEquals("introductionUsingJdk", tb.getName()); 127 128 ITestBean tb2 = (ITestBean) bf.getBean("second-introductionUsingJdk"); 129 130 Lockable lockable1 = (Lockable) tb; 132 Lockable lockable2 = (Lockable) tb2; 133 assertFalse(lockable1.locked()); 134 assertFalse(lockable2.locked()); 135 tb.setAge(65); 136 assertEquals(65, tb.getAge()); 137 lockable1.lock(); 138 assertTrue(lockable1.locked()); 139 assertFalse(lockable2.locked()); 141 tb2.setAge(12); 143 try { 145 tb.setAge(6); 146 fail("Mixin should have locked this object"); 147 } 148 catch (LockedException ex) { 149 } 151 } 152 153 public void testJdkProxyWithWildcardMatch() { 154 ITestBean tb = (ITestBean) bf.getBean("jdk1"); 155 jdkAssertions(tb); 156 assertEquals("jdk1", tb.getName()); 157 } 158 159 public void testCglibProxyWithWildcardMatch() { 160 TestBean tb = (TestBean) bf.getBean("cglib1"); 161 cglibAssertions(tb); 162 assertEquals("cglib1", tb.getName()); 163 } 164 165 private void jdkAssertions(ITestBean tb) { 166 NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor"); 167 assertEquals(0, nop.getCount()); 168 assertTrue(AopUtils.isJdkDynamicProxy(tb)); 169 int age = 5; 170 tb.setAge(age); 171 assertEquals(age, tb.getAge()); 172 assertEquals(2, nop.getCount()); 173 } 174 175 178 private void cglibAssertions(TestBean tb) { 179 CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice"); 180 NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor"); 181 assertEquals(0, cba.getCalls()); 182 assertEquals(0, nop.getCount()); 183 assertTrue(AopUtils.isCglibProxy(tb)); 184 int age = 5; 185 tb.setAge(age); 186 assertEquals(age, tb.getAge()); 187 assertEquals(2, nop.getCount()); 188 assertEquals(2, cba.getCalls()); 189 } 190 } 191 | Popular Tags |