KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.springframework.aop.TargetSource;
20 import org.springframework.beans.MutablePropertyValues;
21 import org.springframework.beans.SerializablePerson;
22 import org.springframework.beans.TestBean;
23 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
24 import org.springframework.beans.factory.support.RootBeanDefinition;
25 import org.springframework.util.SerializationTestUtils;
26
27 import junit.framework.TestCase;
28
29 /**
30  * Unit tests relating to the abstract AbstractPrototypeBasedTargetSource
31  * and not subclasses.
32  *
33  * @author Rod Johnson
34  * 16:20:14 trisberg Exp $
35  */

36 public class PrototypeBasedTargetSourceTests extends TestCase {
37
38     public void testSerializability() throws Exception JavaDoc {
39         MutablePropertyValues tsPvs = new MutablePropertyValues();
40         tsPvs.addPropertyValue("targetBeanName", "person");
41         RootBeanDefinition tsBd = new RootBeanDefinition(
42                 TestTargetSource.class, tsPvs);
43
44         MutablePropertyValues beanPvs = new MutablePropertyValues();
45         RootBeanDefinition beanBd = new RootBeanDefinition(
46                 SerializablePerson.class, beanPvs, false);
47
48         DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
49         bf.registerBeanDefinition("ts", tsBd);
50         bf.registerBeanDefinition("person", beanBd);
51
52         TestTargetSource cpts = (TestTargetSource) bf
53                 .getBean("ts");
54         TargetSource serialized = (TargetSource) SerializationTestUtils
55                 .serializeAndDeserialize(cpts);
56         assertTrue("Changed to SingletonTargetSource on deserialization",
57                 serialized instanceof SingletonTargetSource);
58         SingletonTargetSource sts = (SingletonTargetSource) serialized;
59         assertNotNull(sts.getTarget());
60     }
61
62     
63     private static class TestTargetSource extends AbstractPrototypeBasedTargetSource {
64         
65         /**
66          * Nonserializable test field to check that subclass
67          * state can't prevent serialization from working
68          */

69         private TestBean thisFieldIsNotSerializable = new TestBean();
70
71         /**
72          * @see org.springframework.aop.TargetSource#getTarget()
73          */

74         public Object JavaDoc getTarget() throws Exception JavaDoc {
75             return newPrototypeInstance();
76         }
77
78         /**
79          * @see org.springframework.aop.TargetSource#releaseTarget(java.lang.Object)
80          */

81         public void releaseTarget(Object JavaDoc target) throws Exception JavaDoc {
82             // Do nothing
83
}
84     }
85
86 }
Popular Tags