KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > Singleton2_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.ConfigVisitor;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.config.DSOSpringConfigHelper;
11 import com.tc.object.config.StandardDSOSpringConfigHelper;
12 import com.tc.simulator.app.ApplicationConfig;
13 import com.tc.simulator.listener.ListenerProvider;
14 import com.tctest.TransparentTestBase;
15 import com.tctest.runner.AbstractTransparentApp;
16 import com.tctest.spring.bean.Singleton;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * Spring singleton test
24  */

25 public class Singleton2_Test extends TransparentTestBase {
26   private static final int LOOP_ITERATIONS = 1;
27   private static final int EXECUTION_COUNT = 1;
28   private static final int NODE_COUNT = 4;
29
30   public Singleton2_Test() {
31     disableAllUntil("2008-01-01"); //covered by system test
32
}
33
34   protected void setUp() throws Exception JavaDoc {
35     super.setUp();
36     getTransparentAppConfig().setClientCount(NODE_COUNT).setApplicationInstancePerClientCount(EXECUTION_COUNT)
37         .setIntensity(LOOP_ITERATIONS);
38     initializeTestRunner();
39   }
40
41   protected Class JavaDoc getApplicationClass() {
42     return SingletonApp.class;
43   }
44
45   public static class SingletonApp extends AbstractTransparentApp {
46     private List JavaDoc sharedSingletons1 = new ArrayList JavaDoc();
47     private List JavaDoc sharedSingletons2 = new ArrayList JavaDoc();
48
49     public SingletonApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
50       super(appId, cfg, listenerProvider);
51     }
52
53     public void run() {
54       try {
55         ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext("com/tctest/spring/beanfactory.xml");
56         ClassPathXmlApplicationContext ctx2 = new ClassPathXmlApplicationContext("com/tctest/spring/beanfactory2.xml");
57
58         Singleton singleton1 = (Singleton) ctx1.getBean("singleton");
59         Singleton singleton2 = (Singleton) ctx2.getBean("singleton");
60
61         synchronized (sharedSingletons1) {
62           sharedSingletons1.add(singleton1);
63         }
64         synchronized (sharedSingletons2) {
65           sharedSingletons2.add(singleton2);
66         }
67
68         moveToStageAndWait(1);
69
70         synchronized (sharedSingletons1) {
71           // assertTrue("Expected more then one object in sharedSingletons1", sharedSingletons1.size()>1);
72

73           for (Iterator JavaDoc it = sharedSingletons1.iterator(); it.hasNext();) {
74             Object JavaDoc o = it.next();
75             assertTrue("Found non-singleton object", o == singleton1);
76             if (o == singleton2) {
77               notifyError("Distinct objects expected");
78             }
79           }
80         }
81
82         synchronized (sharedSingletons2) {
83           // assertTrue("Expected more then one object in sharedSingletons2", sharedSingletons2.size()>1);
84

85           for (Iterator JavaDoc it = sharedSingletons2.iterator(); it.hasNext();) {
86             Object JavaDoc o = it.next();
87             assertTrue("Found non-singleton object", o == singleton2);
88             if (o == singleton1) {
89               notifyError("Distinct objects expected");
90             }
91           }
92         }
93
94       } catch (Throwable JavaDoc e) {
95         moveToStage(1);
96         notifyError(e);
97
98       }
99     }
100
101     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
102       config.addRoot("com.tctest.spring.Singleton_Test$SingletonApp", "sharedSingletons1", "sharedSingletons1", false);
103       config.addRoot("com.tctest.spring.Singleton_Test$SingletonApp", "sharedSingletons2", "sharedSingletons2", false);
104
105       DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
106       springConfig.addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing
107
// framework
108
springConfig.addConfigPattern("*/beanfactory.xml");
109       springConfig.addConfigPattern("*/beanfactory2.xml");
110       springConfig.addBean("singleton");
111       config.addDSOSpringConfig(springConfig);
112     }
113
114   }
115 }
116
Popular Tags