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.tc.object;
006:
007: import org.apache.commons.lang.ArrayUtils;
008:
009: import com.tc.config.schema.IllegalConfigurationChangeHandler;
010: import com.tc.config.schema.SettableConfigItem;
011: import com.tc.config.schema.dynamic.ConfigItem;
012: import com.tc.config.schema.setup.ConfigurationSetupException;
013: import com.tc.config.schema.setup.L1TVSConfigurationSetupManager;
014: import com.tc.config.schema.setup.TestTVSConfigurationSetupManagerFactory;
015: import com.tc.object.config.DSOClientConfigHelper;
016: import com.tc.object.config.StandardDSOClientConfigHelperImpl;
017: import com.tc.test.TCTestCase;
018: import com.terracottatech.config.AdditionalBootJarClasses;
019:
020: import java.io.IOException;
021:
022: /**
023: * The base of all DSO tests that use config.
024: */
025: public class BaseDSOTestCase extends TCTestCase {
026:
027: private Exception failTestException;
028:
029: private class TestFailingIllegalConfigChangeHandler implements
030: IllegalConfigurationChangeHandler {
031: public void changeFailed(ConfigItem item, Object oldValue,
032: Object newValue) {
033: failTestException = new Exception(
034: "An attempt was made to illegally change the config item "
035: + item + " from "
036: + ArrayUtils.toString(oldValue) + " to "
037: + ArrayUtils.toString(newValue));
038: }
039: }
040:
041: public void runBare() throws Throwable {
042: super .runBare();
043: if (this .failTestException != null)
044: throw this .failTestException;
045: }
046:
047: private TestTVSConfigurationSetupManagerFactory factory;
048: private L1TVSConfigurationSetupManager l1Manager;
049: private DSOClientConfigHelper configHelper;
050:
051: protected final synchronized TestTVSConfigurationSetupManagerFactory configFactory()
052: throws ConfigurationSetupException {
053: if (this .factory == null)
054: this .factory = createDistributedConfigFactory();
055: return this .factory;
056: }
057:
058: protected final TestTVSConfigurationSetupManagerFactory createDistributedConfigFactory()
059: throws ConfigurationSetupException {
060: TestTVSConfigurationSetupManagerFactory out;
061: out = new TestTVSConfigurationSetupManagerFactory(
062: TestTVSConfigurationSetupManagerFactory.MODE_DISTRIBUTED_CONFIG,
063: null, new TestFailingIllegalConfigChangeHandler());
064:
065: prepareFactory(out);
066: return out;
067: }
068:
069: private void prepareFactory(
070: TestTVSConfigurationSetupManagerFactory out)
071: throws ConfigurationSetupException {
072: // We add a root to make sure there's at least *some* application config. Otherwise, the config system will wait for
073: // it on system startup.
074: /*
075: * Roots roots = Roots.Factory.newInstance(); Root dummyRoot = roots.addNewRoot();
076: * dummyRoot.setFieldName("com.dummy.whatever.Bar.x");
077: */
078:
079: AdditionalBootJarClasses classes = AdditionalBootJarClasses.Factory
080: .newInstance();
081: classes
082: .setIncludeArray(new String[] { "com.dummy.whatever.Bar" });
083:
084: ((SettableConfigItem) out.dsoApplicationConfig()
085: .additionalBootJarClasses()).setValue(classes);
086: // ((SettableConfigItem) out.dsoApplicationConfig().roots()).setValue(roots);
087:
088: try {
089: ((SettableConfigItem) out.l2CommonConfig().dataPath())
090: .setValue(getTempFile("l2-data").toString());
091: ((SettableConfigItem) out.l2CommonConfig().logsPath())
092: .setValue(getTempFile("l2-logs").toString());
093: ((SettableConfigItem) out.l1CommonConfig().logsPath())
094: .setValue(getTempFile("l1-logs").toString());
095: } catch (IOException ioe) {
096: throw new ConfigurationSetupException(
097: "Can't set up log and data paths", ioe);
098: }
099:
100: out.activateConfigurationChange();
101: }
102:
103: protected final TestTVSConfigurationSetupManagerFactory createCentralizedConfigFactory()
104: throws ConfigurationSetupException {
105: TestTVSConfigurationSetupManagerFactory out;
106: out = new TestTVSConfigurationSetupManagerFactory(
107: new TestFailingIllegalConfigChangeHandler());
108:
109: prepareFactory(out);
110: return out;
111: }
112:
113: protected final synchronized L1TVSConfigurationSetupManager l1Manager()
114: throws ConfigurationSetupException {
115: if (this .l1Manager == null)
116: this .l1Manager = createL1ConfigManager();
117: return this .l1Manager;
118: }
119:
120: protected final L1TVSConfigurationSetupManager createL1ConfigManager()
121: throws ConfigurationSetupException {
122: return configFactory().createL1TVSConfigurationSetupManager();
123: }
124:
125: protected final synchronized DSOClientConfigHelper configHelper()
126: throws ConfigurationSetupException {
127: if (this .configHelper == null)
128: this .configHelper = createClientConfigHelper();
129: return this .configHelper;
130: }
131:
132: protected final DSOClientConfigHelper createClientConfigHelper()
133: throws ConfigurationSetupException {
134: return new StandardDSOClientConfigHelperImpl(true,
135: createL1ConfigManager());
136: }
137:
138: // TODO: fix this
139: protected final void makeClientUsePort(int whichPort)
140: throws ConfigurationSetupException {
141: ((SettableConfigItem) configFactory().l2DSOConfig()
142: .listenPort()).setValue(whichPort);
143: }
144:
145: public BaseDSOTestCase() {
146: super ();
147: }
148:
149: public BaseDSOTestCase(String arg0) {
150: super (arg0);
151: }
152:
153: protected void tearDown() throws Exception {
154: this.factory = null;
155: this.configHelper = null;
156: this.l1Manager = null;
157: }
158: }
|