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.framework.ProxyFactory; 23 import org.springframework.aop.interceptor.SerializableNopInterceptor; 24 import org.springframework.aop.interceptor.SideEffectBean; 25 import org.springframework.aop.support.DefaultPointcutAdvisor; 26 import org.springframework.beans.Person; 27 import org.springframework.beans.SerializablePerson; 28 import org.springframework.beans.factory.xml.XmlBeanFactory; 29 import org.springframework.core.io.ClassPathResource; 30 import org.springframework.util.SerializationTestUtils; 31 32 35 public class HotSwappableTargetSourceTests extends TestCase { 36 37 38 private static final int INITIAL_COUNT = 10; 39 40 private XmlBeanFactory beanFactory; 41 42 protected void setUp() throws Exception { 43 this.beanFactory = new XmlBeanFactory(new ClassPathResource("hotSwapTests.xml", getClass())); 44 } 45 46 49 protected void tearDown() { 50 this.beanFactory.destroySingletons(); 52 } 53 54 58 public void testBasicFunctionality() { 59 SideEffectBean target1 = (SideEffectBean) beanFactory.getBean("target1"); 60 SideEffectBean proxied = (SideEffectBean) beanFactory.getBean("swappable"); 61 assertEquals(INITIAL_COUNT, proxied.getCount() ); 62 proxied.doWork(); 63 assertEquals(INITIAL_COUNT + 1, proxied.getCount() ); 64 65 proxied = (SideEffectBean) beanFactory.getBean("swappable"); 66 proxied.doWork(); 67 assertEquals(INITIAL_COUNT + 2, proxied.getCount() ); 68 } 69 70 public void testValidSwaps() { 71 SideEffectBean target1 = (SideEffectBean) beanFactory.getBean("target1"); 72 SideEffectBean target2 = (SideEffectBean) beanFactory.getBean("target2"); 73 74 SideEffectBean proxied = (SideEffectBean) beanFactory.getBean("swappable"); 75 assertEquals(target1.getCount(), proxied.getCount() ); 77 proxied.doWork(); 78 assertEquals(INITIAL_COUNT + 1, proxied.getCount() ); 79 80 HotSwappableTargetSource swapper = (HotSwappableTargetSource) beanFactory.getBean("swapper"); 81 Object old = swapper.swap(target2); 82 assertEquals("Correct old target was returned", target1, old); 83 84 88 assertEquals(20, proxied.getCount()); 89 proxied.doWork(); 90 assertEquals(21, target2.getCount()); 91 92 swapper.swap(target1); 94 assertEquals(target1.getCount(), proxied.getCount()); 95 } 96 97 98 103 private IllegalArgumentException testRejectsSwapToInvalidValue(Object invalid) { 104 HotSwappableTargetSource swapper = (HotSwappableTargetSource) beanFactory.getBean("swapper"); 105 IllegalArgumentException aopex = null; 106 try { 107 swapper.swap(invalid); 108 fail("Shouldn't be able to swap to invalid value [" + invalid + "]"); 109 } 110 catch (IllegalArgumentException ex) { 111 aopex = ex; 113 } 114 115 testBasicFunctionality(); 117 return aopex; 118 } 119 120 public void testRejectsSwapToNull() { 121 IllegalArgumentException ex = testRejectsSwapToInvalidValue(null); 122 assertTrue(ex.getMessage().indexOf("null") != -1); 123 } 124 125 128 129 public void testSerialization() throws Exception { 130 SerializablePerson sp1 = new SerializablePerson(); 131 sp1.setName("Tony"); 132 SerializablePerson sp2 = new SerializablePerson(); 133 sp1.setName("Gordon"); 134 135 HotSwappableTargetSource hts = new HotSwappableTargetSource(sp1); 136 ProxyFactory pf = new ProxyFactory(); 137 pf.addInterface(Person.class); 138 pf.setTargetSource(hts); 139 pf.addAdvisor(new DefaultPointcutAdvisor(new SerializableNopInterceptor())); 140 Person p = (Person) pf.getProxy(); 141 142 assertEquals(sp1.getName(), p.getName()); 143 hts.swap(sp2); 144 assertEquals(sp2.getName(), p.getName()); 145 146 p = (Person) SerializationTestUtils.serializeAndDeserialize(p); 147 hts = (HotSwappableTargetSource) ((Advised) p).getTargetSource(); 149 assertEquals(sp2.getName(), p.getName()); 150 hts.swap(sp1); 151 assertEquals(sp1.getName(), p.getName()); 152 153 } 154 } 155 | Popular Tags |