1 16 17 package org.springframework.aop.framework; 18 19 import junit.framework.TestCase; 20 import org.aopalliance.intercept.MethodInterceptor; 21 import org.aopalliance.intercept.MethodInvocation; 22 23 import org.springframework.beans.factory.xml.XmlBeanFactory; 24 import org.springframework.core.io.ClassPathResource; 25 26 30 public class PrototypeTargetTests extends TestCase { 31 32 public void testPrototypeProxyWithPrototypeTarget() { 33 TestBeanImpl.constructionCount = 0; 34 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("prototypeTarget.xml", getClass())); 35 for (int i = 0; i < 10; i++) { 36 TestBean tb = (TestBean) xbf.getBean("testBeanPrototype"); 37 tb.doSomething(); 38 } 39 TestInterceptor interceptor = (TestInterceptor) xbf.getBean("testInterceptor"); 40 assertEquals(10, TestBeanImpl.constructionCount); 41 assertEquals(10, interceptor.invocationCount); 42 } 43 44 public void testSingletonProxyWithPrototypeTarget() { 45 TestBeanImpl.constructionCount = 0; 46 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("prototypeTarget.xml", getClass())); 47 for (int i = 0; i < 10; i++) { 48 TestBean tb = (TestBean) xbf.getBean("testBeanSingleton"); 49 tb.doSomething(); 50 } 51 TestInterceptor interceptor = (TestInterceptor) xbf.getBean("testInterceptor"); 52 assertEquals(1, TestBeanImpl.constructionCount); 53 assertEquals(10, interceptor.invocationCount); 54 } 55 56 57 public static interface TestBean { 58 59 public void doSomething(); 60 } 61 62 63 public static class TestBeanImpl implements TestBean { 64 65 private static int constructionCount = 0; 66 67 public TestBeanImpl() { 68 constructionCount++; 69 } 70 71 public void doSomething() { 72 } 73 } 74 75 76 public static class TestInterceptor implements MethodInterceptor { 77 78 private int invocationCount = 0; 79 80 public Object invoke(MethodInvocation methodInvocation) throws Throwable { 81 invocationCount++; 82 return methodInvocation.proceed(); 83 } 84 } 85 86 } 87 | Popular Tags |