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.config.schema.setup;
006:
007: import com.tc.config.schema.SettableConfigItem;
008: import com.tc.object.config.schema.AutoLock;
009: import com.tc.object.config.schema.Lock;
010: import com.tc.object.config.schema.LockLevel;
011: import com.tc.test.TCTestCase;
012: import com.tc.util.Assert;
013: import com.terracottatech.config.Autolock;
014: import com.terracottatech.config.Locks;
015: import com.terracottatech.config.NamedLock;
016:
017: import java.io.File;
018:
019: /**
020: * Unit test for {@link TestTVSConfigurationSetupManagerFactory}. Because that class builds up a whole config system,
021: * this test actually stresses a large swath of the configuration system.
022: */
023: public class TestTVSConfigurationSetupManagerFactoryTest extends
024: TCTestCase {
025:
026: private TestTVSConfigurationSetupManagerFactory factory;
027: private L2TVSConfigurationSetupManager l2Manager;
028: private L1TVSConfigurationSetupManager l1Manager;
029:
030: public TestTVSConfigurationSetupManagerFactoryTest() {
031: // this.disableAllUntil(new Date(Long.MAX_VALUE));
032: }
033:
034: public void setUp() throws Exception {
035: this .factory = new TestTVSConfigurationSetupManagerFactory(
036: TestTVSConfigurationSetupManagerFactory.MODE_CENTRALIZED_CONFIG,
037: null, new FatalIllegalConfigurationChangeHandler());
038:
039: ((SettableConfigItem) this .factory.l2CommonConfig().logsPath())
040: .setValue(getTempFile("l2-logs").toString());
041: ((SettableConfigItem) this .factory.l1CommonConfig().logsPath())
042: .setValue(getTempFile("l1-logs").toString());
043:
044: this .l2Manager = this .factory
045: .createL2TVSConfigurationSetupManager(null);
046: this .l1Manager = this .factory
047: .createL1TVSConfigurationSetupManager();
048: }
049:
050: public void testSettingValues() throws Exception {
051: // A string array value
052: ((SettableConfigItem) factory.dsoApplicationConfig()
053: .transientFields()).setValue(new String[] { "Foo.foo",
054: "Bar.bar" });
055:
056: // Hit the remaining top-level config objects
057: ((SettableConfigItem) factory.l2DSOConfig()
058: .garbageCollectionInterval()).setValue(142);
059: ((SettableConfigItem) factory.l1CommonConfig().logsPath())
060: .setValue("whatever");
061: ((SettableConfigItem) factory.l2CommonConfig().dataPath())
062: .setValue("marph");
063:
064: // A complex value (locks)
065: ((SettableConfigItem) factory.dsoApplicationConfig().locks())
066: .setValue(createLocks(new Lock[] {
067: new AutoLock("* Foo.foo(..)",
068: LockLevel.CONCURRENT),
069: new com.tc.object.config.schema.NamedLock(
070: "bar", "* Baz.baz(..)", LockLevel.READ) }));
071:
072: // A sub-config object
073: ((SettableConfigItem) factory.l1DSOConfig()
074: .instrumentationLoggingOptions()
075: .logDistributedMethods()).setValue(true);
076:
077: this .factory.activateConfigurationChange();
078:
079: System.err
080: .println(this .l2Manager
081: .dsoApplicationConfigFor(TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME));
082: System.err.println(this .l2Manager.systemConfig());
083: System.err.println(this .l1Manager.dsoL1Config());
084:
085: assertEqualsOrdered(
086: new String[] { "Foo.foo", "Bar.bar" },
087: this .l2Manager
088: .dsoApplicationConfigFor(
089: TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME)
090: .transientFields().getObject());
091: assertEquals(142, this .l2Manager.dsoL2Config()
092: .garbageCollectionInterval().getInt());
093: assertEquals(new File("whatever"), this .l1Manager
094: .commonL1Config().logsPath().getFile());
095: assertEquals(new File("marph"), this .l2Manager.commonl2Config()
096: .dataPath().getFile());
097: assertEqualsUnordered(
098: new Lock[] {
099: new AutoLock("* Foo.foo(..)",
100: LockLevel.CONCURRENT),
101: new com.tc.object.config.schema.NamedLock(
102: "bar", "* Baz.baz(..)", LockLevel.READ) },
103: this .l2Manager
104: .dsoApplicationConfigFor(
105: TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME)
106: .locks().getObject());
107: assertTrue(this .l1Manager.dsoL1Config()
108: .instrumentationLoggingOptions()
109: .logDistributedMethods().getBoolean());
110: }
111:
112: private com.terracottatech.config.LockLevel.Enum level(LockLevel in) {
113: if (in.equals(LockLevel.CONCURRENT))
114: return com.terracottatech.config.LockLevel.CONCURRENT;
115: if (in.equals(LockLevel.READ))
116: return com.terracottatech.config.LockLevel.READ;
117: if (in.equals(LockLevel.WRITE))
118: return com.terracottatech.config.LockLevel.WRITE;
119: if (in.equals(LockLevel.SYNCHRONOUS_WRITE))
120: return com.terracottatech.config.LockLevel.SYNCHRONOUS_WRITE;
121: throw Assert.failure("Unknown lock level " + in);
122: }
123:
124: private Locks createLocks(Lock[] locks) {
125: Locks out = Locks.Factory.newInstance();
126: for (int i = 0; i < locks.length; ++i) {
127: if (locks[i].isAutoLock()) {
128: Autolock lock = out.addNewAutolock();
129: lock.setLockLevel(level(locks[i].lockLevel()));
130: lock.setMethodExpression(locks[i].methodExpression());
131: } else {
132: NamedLock lock = out.addNewNamedLock();
133: lock.setLockLevel(level(locks[i].lockLevel()));
134: lock.setMethodExpression(locks[i].methodExpression());
135: lock.setLockName(locks[i].lockName());
136: }
137: }
138: return out;
139: }
140:
141: }
|