KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > target > HotSwappableTargetSourceTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
33  * @author Rod Johnson
34  */

35 public class HotSwappableTargetSourceTests extends TestCase {
36
37     /** Initial count value set in bean factory XML */
38     private static final int INITIAL_COUNT = 10;
39
40     private XmlBeanFactory beanFactory;
41     
42     protected void setUp() throws Exception JavaDoc {
43         this.beanFactory = new XmlBeanFactory(new ClassPathResource("hotSwapTests.xml", getClass()));
44     }
45     
46     /**
47      * We must simulate container shutdown, which should clear threads.
48      */

49     protected void tearDown() {
50         // Will call pool.close()
51
this.beanFactory.destroySingletons();
52     }
53
54     /**
55      * Check it works like a normal invoker
56      *
57      */

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, ((Advised) proxied).getTarget());
76
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 JavaDoc old = swapper.swap(target2);
82         assertEquals("Correct old target was returned", target1, old);
83         
84         // TODO should be able to make this assertion: need to fix target handling
85
// in AdvisedSupport
86
//assertEquals(target2, ((Advised) proxied).getTarget());
87

88         assertEquals(20, proxied.getCount());
89         proxied.doWork();
90         assertEquals(21, target2.getCount());
91         
92         // Swap it back
93
swapper.swap(target1);
94         assertEquals(target1.getCount(), proxied.getCount());
95     }
96     
97     
98     /**
99      *
100      * @param invalid
101      * @return the message
102      */

103     private IllegalArgumentException JavaDoc testRejectsSwapToInvalidValue(Object JavaDoc invalid) {
104         HotSwappableTargetSource swapper = (HotSwappableTargetSource) beanFactory.getBean("swapper");
105         IllegalArgumentException JavaDoc aopex = null;
106         try {
107             swapper.swap(invalid);
108             fail("Shouldn't be able to swap to invalid value [" + invalid + "]");
109         }
110         catch (IllegalArgumentException JavaDoc ex) {
111             // Ok
112
aopex = ex;
113         }
114         
115         // It shouldn't be corrupted, it should still work
116
testBasicFunctionality();
117         return aopex;
118     }
119     
120     public void testRejectsSwapToNull() {
121         IllegalArgumentException JavaDoc ex = testRejectsSwapToInvalidValue(null);
122         assertTrue(ex.getMessage().indexOf("null") != -1);
123     }
124     
125     // TODO test reject swap to wrong interface or class?
126
// how to decide what's valid?
127

128     
129     public void testSerialization() throws Exception JavaDoc {
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         // We need to get a reference to the client-side targetsource
148
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