KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > Singleton_Test


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.spring;
5
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8 import com.tc.object.config.ConfigLockLevel;
9 import com.tc.object.config.ConfigVisitor;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.object.config.DSOSpringConfigHelper;
12 import com.tc.object.config.StandardDSOSpringConfigHelper;
13 import com.tc.simulator.app.ApplicationConfig;
14 import com.tc.simulator.listener.ListenerProvider;
15 import com.tctest.TransparentTestBase;
16 import com.tctest.runner.AbstractTransparentApp;
17 import com.tctest.spring.bean.ISingleton;
18 import com.tctest.spring.bean.Singleton;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * Spring singleton tests
27  */

28 public class Singleton_Test extends TransparentTestBase {
29   private static final int LOOP_ITERATIONS = 1;
30   private static final int EXECUTION_COUNT = 1;
31   private static final int NODE_COUNT = 2;
32
33   public Singleton_Test() {
34     disableAllUntil("2008-01-01"); //covered by system test
35
}
36
37   protected void setUp() throws Exception JavaDoc {
38     super.setUp();
39     getTransparentAppConfig().setClientCount(NODE_COUNT).setApplicationInstancePerClientCount(EXECUTION_COUNT)
40         .setIntensity(LOOP_ITERATIONS);
41     initializeTestRunner();
42   }
43
44   protected Class JavaDoc getApplicationClass() {
45     return SingletonApp.class;
46   }
47   
48
49   public static class SingletonApp extends AbstractTransparentApp {
50     private List JavaDoc sharedSingletons = new ArrayList JavaDoc();
51     
52     
53     public SingletonApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
54       super(appId, cfg, listenerProvider);
55     }
56
57     public void run() {
58         testSimpleSingleton();
59         testSingletonWithParent();
60         testLifeCycle();
61     }
62
63     private void testSimpleSingleton() {
64       try {
65         moveToStageAndWait(10);
66
67         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("com/tctest/spring/beanfactory.xml");
68         
69         ISingleton singleton = (ISingleton) ctx.getBean("singleton");
70         singleton.incrementCounter();
71
72         String JavaDoc applicationId = getApplicationId();
73         singleton.setTransientValue(applicationId);
74         
75         synchronized (sharedSingletons) {
76           sharedSingletons.add(singleton);
77         }
78         
79         moveToStageAndWait(11);
80
81         synchronized (sharedSingletons) {
82           assertTrue("Expected more then one object in the collection", sharedSingletons.size()>1);
83           
84           HashSet JavaDoc transientValues = new HashSet JavaDoc();
85           for (Iterator JavaDoc it = sharedSingletons.iterator(); it.hasNext();) {
86             ISingleton o = (ISingleton) it.next();
87             assertTrue("Found non-singleton object", o==singleton);
88             assertTrue("Invalid value in shared field", o.getCounter()>1);
89             transientValues.add(o.getTransientValue());
90           }
91           
92           // TODO investigate why all transient values are the same in sharedSingletons collection on given node
93
// assertEquals("Invalid value in transient field", sharedSingletons.size(), transientValues.size());
94
}
95
96         moveToStageAndWait(12);
97         
98       } catch (Throwable JavaDoc e) {
99         moveToStage(11);
100         moveToStage(12);
101         notifyError(e);
102         
103       } finally {
104         clear();
105         
106       }
107     }
108     
109     private void testSingletonWithParent() {
110       try {
111         moveToStageAndWait(20);
112
113         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("com/tctest/spring/beanfactory-withParent.xml");
114         
115         ISingleton singleton = (ISingleton) ctx.getBean("singletonDelegator");
116         singleton.incrementCounter();
117         
118         String JavaDoc applicationId = getApplicationId();
119         singleton.setTransientValue(applicationId);
120         
121         synchronized (sharedSingletons) {
122           sharedSingletons.add(singleton);
123         }
124         
125         moveToStageAndWait(21);
126         
127         synchronized (sharedSingletons) {
128           assertTrue("Expected more then one object in the collection", sharedSingletons.size()>1);
129           
130           HashSet JavaDoc transientValues = new HashSet JavaDoc();
131           for (Iterator JavaDoc it = sharedSingletons.iterator(); it.hasNext();) {
132             ISingleton o = (ISingleton) it.next();
133             assertTrue("Found non-singleton object", o==singleton);
134             assertTrue("Invalid value in shared field", o.getCounter()>1);
135             transientValues.add(o.getTransientValue());
136           }
137         }
138         
139         moveToStageAndWait(22);
140         
141       } catch (Throwable JavaDoc e) {
142         moveToStage(21);
143         moveToStage(22);
144         notifyError(e);
145         
146       } finally {
147         clear();
148         
149       }
150     }
151     
152     private void testLifeCycle() {
153       try {
154         moveToStageAndWait(31);
155
156         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("com/tctest/spring/beanfactory.xml");
157         
158         Singleton singleton = (Singleton) ctx.getBean("singleton");
159         
160         List JavaDoc recorder = singleton.getRecorder();
161         List JavaDoc localRecorder = singleton.getLocalRecorder();
162         List JavaDoc transientRecorder = singleton.getTransientRecorder();
163         
164         moveToStageAndWait(32);
165         
166         ctx.close();
167
168         moveToStageAndWait(33);
169         
170         // System.err.println("### "+Thread.currentThread().getName()+" 1 "+recorder);
171
// System.err.println("### "+Thread.currentThread().getName()+" 2 "+localRecorder);
172
// System.err.println("### "+Thread.currentThread().getName()+" 3 "+transientRecorder);
173

174         assertEquals("Invalid clustered "+recorder.toString(), 4, recorder.size());
175         assertEquals("Invalid local "+localRecorder.toString(), 4, localRecorder.size());
176         assertEquals("Invalid transient "+transientRecorder.toString(), 2, transientRecorder.size());
177         
178       } catch (Throwable JavaDoc e) {
179         moveToStage(31);
180         moveToStage(32);
181         moveToStage(33);
182         notifyError(e);
183         
184       } finally {
185         clear();
186         
187       }
188     }
189
190     
191     
192     private void clear() {
193       synchronized (sharedSingletons) {
194         sharedSingletons.clear();
195       }
196     }
197     
198     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
199       config.addRoot("com.tctest.spring.Singleton_Test$SingletonApp", "sharedSingletons", "sharedSingletons", false);
200       config.addAutolock("* com.tctest.spring.Singleton_Test$SingletonApp.*()", ConfigLockLevel.WRITE);
201       
202       {
203         DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
204         springConfig.addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing framework
205
springConfig.addConfigPattern("*/beanfactory.xml");
206         springConfig.addBean("singleton");
207         springConfig.addBean("recorder");
208         springConfig.excludeField("singleton", "transientValue");
209         springConfig.excludeField("singleton", "transientBoolean");
210         
211         config.addDSOSpringConfig(springConfig);
212       }
213       
214       {
215         DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
216         springConfig.addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing framework
217
springConfig.addConfigPattern("*/beanfactory-withParent.xml");
218         springConfig.addBean("singletonDelegator");
219         springConfig.excludeField("singletonDelegator", "transientValue");
220         springConfig.excludeField("singletonDelegator", "transientBoolean");
221         
222         config.addDSOSpringConfig(springConfig);
223       }
224     }
225     
226   }
227
228 }
229
230
Popular Tags