001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.spring;
006:
007: import org.springframework.context.support.ClassPathXmlApplicationContext;
008:
009: import com.tc.object.config.ConfigVisitor;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tc.object.config.DSOSpringConfigHelper;
012: import com.tc.object.config.Root;
013: import com.tc.object.config.StandardDSOSpringConfigHelper;
014: import com.tc.simulator.app.ApplicationConfig;
015: import com.tc.simulator.listener.ListenerProvider;
016: import com.tc.util.runtime.Vm;
017: import com.tctest.TransparentTestBase;
018: import com.tctest.runner.AbstractTransparentApp;
019: import com.tctest.spring.bean.Singleton;
020:
021: import java.util.ArrayList;
022: import java.util.Date;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: /**
027: * Spring singleton test
028: */
029: public class Singleton2_Test extends TransparentTestBase {
030: private static final int LOOP_ITERATIONS = 1;
031: private static final int EXECUTION_COUNT = 1;
032: private static final int NODE_COUNT = 4;
033:
034: public Singleton2_Test() {
035: if (Vm.isIBM()) {
036: disableAllUntil(new Date(Long.MAX_VALUE));
037: }
038: }
039:
040: protected void setUp() throws Exception {
041: super .setUp();
042: getTransparentAppConfig().setClientCount(NODE_COUNT)
043: .setApplicationInstancePerClientCount(EXECUTION_COUNT)
044: .setIntensity(LOOP_ITERATIONS);
045: initializeTestRunner();
046: }
047:
048: protected Class getApplicationClass() {
049: return SingletonApp.class;
050: }
051:
052: public static class SingletonApp extends AbstractTransparentApp {
053: private List sharedSingletons1 = new ArrayList();
054: private List sharedSingletons2 = new ArrayList();
055:
056: public SingletonApp(String appId, ApplicationConfig cfg,
057: ListenerProvider listenerProvider) {
058: super (appId, cfg, listenerProvider);
059: }
060:
061: public void run() {
062: try {
063: ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext(
064: "com/tctest/spring/beanfactory.xml");
065: ClassPathXmlApplicationContext ctx2 = new ClassPathXmlApplicationContext(
066: "com/tctest/spring/beanfactory2.xml");
067:
068: Singleton singleton1 = (Singleton) ctx1
069: .getBean("singleton");
070: Singleton singleton2 = (Singleton) ctx2
071: .getBean("singleton");
072:
073: synchronized (sharedSingletons1) {
074: sharedSingletons1.add(singleton1);
075: }
076: synchronized (sharedSingletons2) {
077: sharedSingletons2.add(singleton2);
078: }
079:
080: moveToStageAndWait(1);
081:
082: synchronized (sharedSingletons1) {
083: // assertTrue("Expected more then one object in sharedSingletons1", sharedSingletons1.size()>1);
084:
085: for (Iterator it = sharedSingletons1.iterator(); it
086: .hasNext();) {
087: Object o = it.next();
088: assertTrue("Found non-singleton object",
089: o == singleton1);
090: if (o == singleton2) {
091: notifyError("Distinct objects expected");
092: }
093: }
094: }
095:
096: synchronized (sharedSingletons2) {
097: // assertTrue("Expected more then one object in sharedSingletons2", sharedSingletons2.size()>1);
098:
099: for (Iterator it = sharedSingletons2.iterator(); it
100: .hasNext();) {
101: Object o = it.next();
102: assertTrue("Found non-singleton object",
103: o == singleton2);
104: if (o == singleton1) {
105: notifyError("Distinct objects expected");
106: }
107: }
108: }
109:
110: } catch (Throwable e) {
111: moveToStage(1);
112: notifyError(e);
113:
114: }
115: }
116:
117: public static void visitL1DSOConfig(ConfigVisitor visitor,
118: DSOClientConfigHelper config) {
119: config.addRoot(new Root(
120: "com.tctest.spring.Singleton_Test$SingletonApp",
121: "sharedSingletons1", "sharedSingletons1"), false);
122: config.addRoot(new Root(
123: "com.tctest.spring.Singleton_Test$SingletonApp",
124: "sharedSingletons2", "sharedSingletons2"), false);
125:
126: DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
127: springConfig
128: .addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing
129: // framework
130: springConfig.addConfigPattern("*/beanfactory.xml");
131: springConfig.addConfigPattern("*/beanfactory2.xml");
132: springConfig.addBean("singleton");
133: config.addDSOSpringConfig(springConfig);
134: }
135:
136: }
137: }
|