KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
29  * Tests for pooling invoker interceptor
30  * TODO need to make these tests stronger: it's hard to
31  * make too many assumptions about a pool
32  * @author Rod Johnson
33  */

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

48     protected void tearDown() {
49         // Will call pool.close()
50
this.beanFactory.destroySingletons();
51     }
52
53     private void testFunctionality(String JavaDoc 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         // Just check that it works--we can't make assumptions
61
// about the count
62
pooled.doWork();
63         //assertEquals(INITIAL_COUNT + 1, apartment.getCount() );
64
}
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         // TODO one invocation from setup
79
//assertEquals(1, conf.getInvocations());
80
pooled.doWork();
81     // assertEquals("No objects active", 0, conf.getActive());
82
assertEquals("Correct target source", 25, conf.getMaxSize());
83 // assertTrue("Some free", conf.getFree() > 0);
84
//assertEquals(2, conf.getInvocations());
85
assertEquals(25, conf.getMaxSize());
86     }
87     
88     public void testTargetSourceSerializableWithoutConfigMixin() throws Exception JavaDoc {
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 JavaDoc {
97         Person pooled = (Person) beanFactory.getBean("pooledPerson");
98
99         //System.out.println(((Advised) pooled).toProxyConfigString());
100
assertTrue(((Advised) pooled).getTargetSource() instanceof CommonsPoolTargetSource);
101         
102         //((Advised) pooled).setTargetSource(new SingletonTargetSource(new SerializablePerson()));
103
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