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.ConfigLockLevel;
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.DSOSpringConfigHelper;
013: import com.tc.object.config.Root;
014: import com.tc.object.config.StandardDSOSpringConfigHelper;
015: import com.tc.simulator.app.ApplicationConfig;
016: import com.tc.simulator.listener.ListenerProvider;
017: import com.tc.util.runtime.Vm;
018: import com.tctest.TransparentTestBase;
019: import com.tctest.runner.AbstractTransparentApp;
020: import com.tctest.spring.bean.MasterBean;
021: import com.tctest.spring.bean.Singleton;
022:
023: import java.util.ArrayList;
024: import java.util.Date;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: /**
029: * Test for cross context initialization
030: */
031: public class MultipleContexts_Test extends TransparentTestBase {
032: private static final int LOOP_ITERATIONS = 1;
033: private static final int EXECUTION_COUNT = 1;
034: private static final int NODE_COUNT = 2;
035:
036: public MultipleContexts_Test() {
037: if (Vm.isIBM()) {
038: disableAllUntil(new Date(Long.MAX_VALUE));
039: }
040: }
041:
042: protected void setUp() throws Exception {
043: super .setUp();
044: getTransparentAppConfig().setClientCount(NODE_COUNT)
045: .setApplicationInstancePerClientCount(EXECUTION_COUNT)
046: .setIntensity(LOOP_ITERATIONS);
047: initializeTestRunner();
048: }
049:
050: protected Class getApplicationClass() {
051: return MultipleContextsApp.class;
052: }
053:
054: public static class MultipleContextsApp extends
055: AbstractTransparentApp {
056: private List sharedSingletons = new ArrayList();
057:
058: public MultipleContextsApp(String appId, ApplicationConfig cfg,
059: ListenerProvider listenerProvider) {
060: super (appId, cfg, listenerProvider);
061: }
062:
063: public void run() {
064: try {
065: // spring classes should be loaded before using any custom bean classes
066: ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
067: new String[] {
068: "com/tctest/spring/beanfactory-master.xml",
069: "com/tctest/spring/beanfactory.xml" });
070:
071: MasterBean distributedMasterBean;
072: Singleton singleton;
073:
074: synchronized (sharedSingletons) {
075: // MasterBean localMasterBean = (MasterBean) ctx.getBean("localMasterBean");
076: distributedMasterBean = (MasterBean) ctx
077: .getBean("distributedMasterBean");
078: distributedMasterBean.addValue(getApplicationId());
079:
080: sharedSingletons.add(distributedMasterBean);
081:
082: singleton = distributedMasterBean.getSingleton();
083: }
084:
085: moveToStageAndWait(1);
086:
087: synchronized (sharedSingletons) {
088: assertEquals("Expected more objects", NODE_COUNT,
089: sharedSingletons.size());
090:
091: for (Iterator it = sharedSingletons.iterator(); it
092: .hasNext();) {
093: MasterBean o = (MasterBean) it.next();
094: List values = o.getValues();
095: assertEquals("Missing values " + values,
096: NODE_COUNT, values.size());
097: assertTrue("Found non-singleton object " + o,
098: o == distributedMasterBean);
099: assertTrue("Found non-singleton object2 "
100: + o.getSingleton(),
101: o.getSingleton() == singleton);
102: }
103: }
104:
105: } catch (Throwable e) {
106: moveToStage(1);
107: notifyError(e);
108:
109: }
110: }
111:
112: public static void visitL1DSOConfig(ConfigVisitor visitor,
113: DSOClientConfigHelper config) {
114: config
115: .addRoot(
116: new Root(
117: "com.tctest.spring.MultipleContexts_Test$MultipleContextsApp",
118: "sharedSingletons",
119: "sharedSingletons"), false);
120: config
121: .addAutolock(
122: "* com.tctest.spring.MultipleContexts_Test$MultipleContextsApp.run()",
123: ConfigLockLevel.WRITE);
124:
125: DSOSpringConfigHelper springConfig = new StandardDSOSpringConfigHelper();
126: springConfig
127: .addApplicationNamePattern(SpringTestConstants.APPLICATION_NAME); // app name used by testing
128: // framework
129: springConfig.addConfigPattern("*/beanfactory-master.xml");
130: springConfig.addConfigPattern("*/beanfactory.xml");
131: springConfig.addBean("singleton");
132: springConfig.addBean("distributedMasterBean");
133: config.addDSOSpringConfig(springConfig);
134: }
135:
136: }
137:
138: }
|