KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > setup > TestTVSConfigurationSetupManagerFactoryTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.config.schema.setup;
6
7 import com.tc.config.schema.SettableConfigItem;
8 import com.tc.object.config.schema.AutoLock;
9 import com.tc.object.config.schema.Lock;
10 import com.tc.object.config.schema.LockLevel;
11 import com.tc.test.TCTestCase;
12 import com.tc.util.Assert;
13 import com.terracottatech.config.Autolock;
14 import com.terracottatech.config.Locks;
15 import com.terracottatech.config.NamedLock;
16
17 import java.io.File JavaDoc;
18
19 /**
20  * Unit test for {@link TestTVSConfigurationSetupManagerFactory}. Because that class builds up a whole config system,
21  * this test actually stresses a large swath of the configuration system.
22  */

23 public class TestTVSConfigurationSetupManagerFactoryTest extends TCTestCase {
24
25   private TestTVSConfigurationSetupManagerFactory factory;
26   private L2TVSConfigurationSetupManager l2Manager;
27   private L1TVSConfigurationSetupManager l1Manager;
28
29   public void setUp() throws Exception JavaDoc {
30     this.factory = new TestTVSConfigurationSetupManagerFactory(
31                                                                TestTVSConfigurationSetupManagerFactory.MODE_DISTRIBUTED_CONFIG,
32                                                                null, new FatalIllegalConfigurationChangeHandler());
33
34     ((SettableConfigItem) this.factory.l2CommonConfig().logsPath()).setValue(getTempFile("l2-logs").toString());
35     ((SettableConfigItem) this.factory.l1CommonConfig().logsPath()).setValue(getTempFile("l1-logs").toString());
36
37     this.l2Manager = this.factory.createL2TVSConfigurationSetupManager(null);
38     this.l1Manager = this.factory.createL1TVSConfigurationSetupManager();
39   }
40
41   public void testSettingValues() throws Exception JavaDoc {
42     // A string array value
43
((SettableConfigItem) factory.dsoApplicationConfig().transientFields()).setValue(new String JavaDoc[] { "Foo.foo",
44         "Bar.bar" });
45
46     // Hit the remaining top-level config objects
47
((SettableConfigItem) factory.l2DSOConfig().garbageCollectionInterval()).setValue(142);
48     ((SettableConfigItem) factory.l1CommonConfig().logsPath()).setValue("whatever");
49     ((SettableConfigItem) factory.l2CommonConfig().dataPath()).setValue("marph");
50
51     // A complex value (locks)
52
((SettableConfigItem) factory.dsoApplicationConfig().locks()).setValue(createLocks(new Lock[] {
53         new AutoLock("Foo.foo(..)", LockLevel.CONCURRENT),
54         new com.tc.object.config.schema.NamedLock("bar", "Baz.baz(..)", LockLevel.READ) }));
55
56     // A sub-config object
57
((SettableConfigItem) factory.l1DSOConfig().instrumentationLoggingOptions().logDistributedMethods()).setValue(true);
58
59     this.factory.activateConfigurationChange();
60
61     System.err.println(this.l2Manager
62         .dsoApplicationConfigFor(TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME));
63     System.err.println(this.l2Manager.systemConfig());
64     System.err.println(this.l1Manager.dsoL1Config());
65
66     assertEqualsOrdered(new String JavaDoc[] { "Foo.foo", "Bar.bar" }, this.l2Manager
67         .dsoApplicationConfigFor(TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME).transientFields()
68         .getObject());
69     assertEquals(142, this.l2Manager.dsoL2Config().garbageCollectionInterval().getInt());
70     assertEquals(new File JavaDoc("whatever"), this.l1Manager.commonL1Config().logsPath().getFile());
71     assertEquals(new File JavaDoc("marph"), this.l2Manager.commonl2Config().dataPath().getFile());
72     assertEqualsUnordered(new Lock[] { new AutoLock("Foo.foo(..)", LockLevel.CONCURRENT),
73         new com.tc.object.config.schema.NamedLock("bar", "Baz.baz(..)", LockLevel.READ) }, this.l2Manager
74         .dsoApplicationConfigFor(TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME).locks().getObject());
75     assertTrue(this.l1Manager.dsoL1Config().instrumentationLoggingOptions().logDistributedMethods().getBoolean());
76   }
77
78   private com.terracottatech.config.LockLevel.Enum level(LockLevel in) {
79     if (in.equals(LockLevel.CONCURRENT)) return com.terracottatech.config.LockLevel.CONCURRENT;
80     if (in.equals(LockLevel.READ)) return com.terracottatech.config.LockLevel.READ;
81     if (in.equals(LockLevel.WRITE)) return com.terracottatech.config.LockLevel.WRITE;
82     if (in.equals(LockLevel.SYNCHRONOUS_WRITE)) return com.terracottatech.config.LockLevel.SYNCHRONOUS_WRITE;
83     throw Assert.failure("Unknown lock level " + in);
84   }
85
86   private Locks createLocks(Lock[] locks) {
87     Locks out = Locks.Factory.newInstance();
88     for (int i = 0; i < locks.length; ++i) {
89       if (locks[i].isAutoLock()) {
90         Autolock lock = out.addNewAutolock();
91         lock.setLockLevel(level(locks[i].lockLevel()));
92         lock.setMethodExpression(locks[i].methodExpression());
93       } else {
94         NamedLock lock = out.addNewNamedLock();
95         lock.setLockLevel(level(locks[i].lockLevel()));
96         lock.setMethodExpression(locks[i].methodExpression());
97         lock.setLockName(locks[i].lockName());
98       }
99     }
100     return out;
101   }
102
103 }
104
Popular Tags