KickJava   Java API By Example, From Geeks To Geeks.

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


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.interceptor.SideEffectBean;
22 import org.springframework.beans.ITestBean;
23 import org.springframework.beans.factory.xml.XmlBeanFactory;
24 import org.springframework.core.io.ClassPathResource;
25
26 /**
27  * @author Rod Johnson
28  */

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

43     protected void tearDown() {
44         this.beanFactory.destroySingletons();
45     }
46     
47     /**
48      * Check we can use two different ThreadLocalTargetSources
49      * managing objects of different types without them interfering
50      * with one another.
51      */

52     public void testUseDifferentManagedInstancesInSameThread() {
53         SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
54         assertEquals(INITIAL_COUNT, apartment.getCount() );
55         apartment.doWork();
56         assertEquals(INITIAL_COUNT + 1, apartment.getCount() );
57     
58         ITestBean test = (ITestBean) beanFactory.getBean("threadLocal2");
59         assertEquals("Rod", test.getName());
60         assertEquals("Kerry", test.getSpouse().getName());
61     }
62
63     public void testReuseInSameThread() {
64         SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
65         assertEquals(INITIAL_COUNT, apartment.getCount() );
66         apartment.doWork();
67         assertEquals(INITIAL_COUNT + 1, apartment.getCount() );
68         
69         apartment = (SideEffectBean) beanFactory.getBean("apartment");
70         assertEquals(INITIAL_COUNT + 1, apartment.getCount() );
71     }
72     
73     /**
74      * Relies on introduction.
75      */

76     public void testCanGetStatsViaMixin() {
77         ThreadLocalTargetSourceStats stats = (ThreadLocalTargetSourceStats) beanFactory.getBean("apartment");
78         // +1 because creating target for stats call counts
79
assertEquals(1, stats.getInvocationCount());
80         SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
81         apartment.doWork();
82         // +1 again
83
assertEquals(3, stats.getInvocationCount());
84         // + 1 for states call!
85
assertEquals(3, stats.getHitCount());
86         apartment.doWork();
87         assertEquals(6, stats.getInvocationCount());
88         assertEquals(6, stats.getHitCount());
89         // Only one thread so only one object can have been bound
90
assertEquals(1, stats.getObjectCount());
91     }
92     
93     public void testNewThreadHasOwnInstance() throws InterruptedException JavaDoc {
94         SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
95         assertEquals(INITIAL_COUNT, apartment.getCount() );
96         apartment.doWork();
97         apartment.doWork();
98         apartment.doWork();
99         assertEquals(INITIAL_COUNT + 3, apartment.getCount() );
100     
101         class Runner implements Runnable JavaDoc {
102             public SideEffectBean mine;
103             public void run() {
104                 this.mine = (SideEffectBean) beanFactory.getBean("apartment");
105                 assertEquals(INITIAL_COUNT, mine.getCount() );
106                 mine.doWork();
107                 assertEquals(INITIAL_COUNT + 1, mine.getCount() );
108             }
109         }
110         Runner r = new Runner();
111         Thread JavaDoc t = new Thread JavaDoc(r);
112         t.start();
113         t.join();
114         
115         assertNotNull(r);
116         
117         // Check it didn't affect the other thread's copy
118
assertEquals(INITIAL_COUNT + 3, apartment.getCount() );
119         
120         // When we use other thread's copy in this thread
121
// it should behave like ours
122
assertEquals(INITIAL_COUNT + 3, r.mine.getCount() );
123         
124         // Bound to two threads
125
assertEquals(2, ((ThreadLocalTargetSourceStats) apartment).getObjectCount());
126     }
127
128 }
129
Popular Tags