KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > BaseDSOTestCase


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

4 package com.tc.object;
5
6 import org.apache.commons.lang.ArrayUtils;
7
8 import com.tc.config.schema.IllegalConfigurationChangeHandler;
9 import com.tc.config.schema.SettableConfigItem;
10 import com.tc.config.schema.dynamic.ConfigItem;
11 import com.tc.config.schema.setup.ConfigurationSetupException;
12 import com.tc.config.schema.setup.L1TVSConfigurationSetupManager;
13 import com.tc.config.schema.setup.TestTVSConfigurationSetupManagerFactory;
14 import com.tc.management.L1Management;
15 import com.tc.object.config.DSOClientConfigHelper;
16 import com.tc.object.config.StandardDSOClientConfigHelper;
17 import com.tc.test.TCTestCase;
18 import com.terracottatech.config.AdditionalBootJarClasses;
19
20 import java.io.IOException JavaDoc;
21
22 /**
23  * The base of all DSO tests that use config.
24  */

25 public class BaseDSOTestCase extends TCTestCase {
26
27   private Exception JavaDoc failTestException;
28
29   static {
30     L1Management.forceCreateMBeanServer();
31   }
32
33   private class TestFailingIllegalConfigChangeHandler implements IllegalConfigurationChangeHandler {
34     public void changeFailed(ConfigItem item, Object JavaDoc oldValue, Object JavaDoc newValue) {
35       failTestException = new Exception JavaDoc("An attempt was made to illegally change the config item " + item + " from "
36                                         + ArrayUtils.toString(oldValue) + " to " + ArrayUtils.toString(newValue));
37     }
38   }
39
40   public void runBare() throws Throwable JavaDoc {
41     super.runBare();
42     if (this.failTestException != null) throw this.failTestException;
43   }
44
45   private TestTVSConfigurationSetupManagerFactory factory;
46   private L1TVSConfigurationSetupManager l1Manager;
47   private DSOClientConfigHelper configHelper;
48
49   protected final synchronized TestTVSConfigurationSetupManagerFactory configFactory()
50       throws ConfigurationSetupException {
51     if (this.factory == null) this.factory = createDistributedConfigFactory();
52     return this.factory;
53   }
54
55   protected final TestTVSConfigurationSetupManagerFactory createDistributedConfigFactory()
56       throws ConfigurationSetupException {
57     TestTVSConfigurationSetupManagerFactory out;
58     out = new TestTVSConfigurationSetupManagerFactory(TestTVSConfigurationSetupManagerFactory.MODE_DISTRIBUTED_CONFIG,
59                                                       null, new TestFailingIllegalConfigChangeHandler());
60
61     prepareFactory(out);
62     return out;
63   }
64
65   private void prepareFactory(TestTVSConfigurationSetupManagerFactory out) throws ConfigurationSetupException {
66     // We add a root to make sure there's at least *some* application config. Otherwise, the config system will wait for
67
// it on system startup.
68
/*
69      * Roots roots = Roots.Factory.newInstance(); Root dummyRoot = roots.addNewRoot();
70      * dummyRoot.setFieldName("com.dummy.whatever.Bar.x");
71      */

72
73     AdditionalBootJarClasses classes = AdditionalBootJarClasses.Factory.newInstance();
74     classes.setIncludeArray(new String JavaDoc[] { "com.dummy.whatever.Bar" });
75
76     ((SettableConfigItem) out.dsoApplicationConfig().additionalBootJarClasses()).setValue(classes);
77     // ((SettableConfigItem) out.dsoApplicationConfig().roots()).setValue(roots);
78

79     try {
80       ((SettableConfigItem) out.l2CommonConfig().dataPath()).setValue(getTempFile("l2-data").toString());
81       ((SettableConfigItem) out.l2CommonConfig().logsPath()).setValue(getTempFile("l2-logs").toString());
82       ((SettableConfigItem) out.l1CommonConfig().logsPath()).setValue(getTempFile("l1-logs").toString());
83     } catch (IOException JavaDoc ioe) {
84       throw new ConfigurationSetupException("Can't set up log and data paths", ioe);
85     }
86
87     out.activateConfigurationChange();
88   }
89
90   protected final TestTVSConfigurationSetupManagerFactory createCentralizedConfigFactory()
91       throws ConfigurationSetupException {
92     TestTVSConfigurationSetupManagerFactory out;
93     out = new TestTVSConfigurationSetupManagerFactory(new TestFailingIllegalConfigChangeHandler());
94
95     prepareFactory(out);
96     return out;
97   }
98
99   protected final synchronized L1TVSConfigurationSetupManager l1Manager() throws ConfigurationSetupException {
100     if (this.l1Manager == null) this.l1Manager = createL1ConfigManager();
101     return this.l1Manager;
102   }
103
104   protected final L1TVSConfigurationSetupManager createL1ConfigManager() throws ConfigurationSetupException {
105     return configFactory().createL1TVSConfigurationSetupManager();
106   }
107
108   protected final synchronized DSOClientConfigHelper configHelper() throws ConfigurationSetupException {
109     if (this.configHelper == null) this.configHelper = createClientConfigHelper();
110     return this.configHelper;
111   }
112
113   protected final DSOClientConfigHelper createClientConfigHelper() throws ConfigurationSetupException {
114     return new StandardDSOClientConfigHelper(true, createL1ConfigManager());
115   }
116
117   protected final void makeClientUsePort(int whichPort) throws ConfigurationSetupException {
118     ((SettableConfigItem) configFactory().l2DSOConfig().listenPort()).setValue(whichPort);
119   }
120
121   public BaseDSOTestCase() {
122     super();
123   }
124
125   public BaseDSOTestCase(String JavaDoc arg0) {
126     super(arg0);
127   }
128
129   protected void tearDown() throws Exception JavaDoc {
130     this.factory = null;
131     this.configHelper = null;
132     this.l1Manager = null;
133   }
134 }
135
Popular Tags