1 16 17 package org.springframework.aop.target; 18 19 import junit.framework.TestCase; 20 21 import org.springframework.aop.framework.Advised; 22 import org.springframework.aop.interceptor.SideEffectBean; 23 import org.springframework.beans.Person; 24 import org.springframework.beans.factory.xml.XmlBeanFactory; 25 import org.springframework.core.io.ClassPathResource; 26 import org.springframework.util.SerializationTestUtils; 27 28 34 public class CommonsPoolTargetSourceTests extends TestCase { 35 36 37 private static final int INITIAL_COUNT = 10; 38 39 private XmlBeanFactory beanFactory; 40 41 protected void setUp() throws Exception { 42 this.beanFactory = new XmlBeanFactory(new ClassPathResource("commonsPoolTests.xml", getClass())); 43 } 44 45 48 protected void tearDown() { 49 this.beanFactory.destroySingletons(); 51 } 52 53 private void testFunctionality(String name) { 54 SideEffectBean pooled = (SideEffectBean) beanFactory.getBean(name); 55 assertEquals(INITIAL_COUNT, pooled.getCount() ); 56 pooled.doWork(); 57 assertEquals(INITIAL_COUNT + 1, pooled.getCount() ); 58 59 pooled = (SideEffectBean) beanFactory.getBean(name); 60 pooled.doWork(); 63 } 65 66 public void testFunctionality() { 67 testFunctionality("pooled"); 68 } 69 70 public void testFunctionalityWithNoInterceptors() { 71 testFunctionality("pooledNoInterceptors"); 72 } 73 74 public void testConfigMixin() { 75 SideEffectBean pooled = (SideEffectBean) beanFactory.getBean("pooledWithMixin"); 76 assertEquals(INITIAL_COUNT, pooled.getCount() ); 77 PoolingConfig conf = (PoolingConfig) beanFactory.getBean("pooledWithMixin"); 78 pooled.doWork(); 81 assertEquals("Correct target source", 25, conf.getMaxSize()); 83 assertEquals(25, conf.getMaxSize()); 86 } 87 88 public void testTargetSourceSerializableWithoutConfigMixin() throws Exception { 89 CommonsPoolTargetSource cpts = (CommonsPoolTargetSource) beanFactory.getBean("personPoolTargetSource"); 90 91 SingletonTargetSource serialized = (SingletonTargetSource) SerializationTestUtils.serializeAndDeserialize(cpts); 92 assertTrue(serialized.getTarget() instanceof Person); 93 } 94 95 96 public void testProxySerializableWithoutConfigMixin() throws Exception { 97 Person pooled = (Person) beanFactory.getBean("pooledPerson"); 98 99 assertTrue(((Advised) pooled).getTargetSource() instanceof CommonsPoolTargetSource); 101 102 Person serialized = (Person) SerializationTestUtils.serializeAndDeserialize(pooled); 104 assertTrue(((Advised) serialized).getTargetSource() instanceof SingletonTargetSource); 105 serialized.setAge(25); 106 assertEquals(25, serialized.getAge()); 107 } 108 109 } 110 | Popular Tags |