KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > TransparentTestBase


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.tctest;
6
7 import com.tc.config.schema.SettableConfigItem;
8 import com.tc.config.schema.setup.TVSConfigurationSetupManagerFactory;
9 import com.tc.object.BaseDSOTestCase;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.objectserver.control.ExtraProcessServerControl;
12 import com.tc.objectserver.control.ServerControl;
13 import com.tc.simulator.app.ApplicationConfigBuilder;
14 import com.tc.simulator.app.ErrorContext;
15 import com.tc.test.TestConfigObject;
16 import com.tc.test.restart.RestartTestEnvironment;
17 import com.tc.test.restart.RestartTestHelper;
18 import com.tc.test.restart.ServerCrasher;
19 import com.tc.util.PortChooser;
20 import com.tc.util.runtime.ThreadDump;
21 import com.tctest.runner.DistributedTestRunner;
22 import com.tctest.runner.DistributedTestRunnerConfig;
23 import com.tctest.runner.TestGlobalIdGenerator;
24 import com.tctest.runner.TransparentAppConfig;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import junit.framework.AssertionFailedError;
33
34 public abstract class TransparentTestBase extends BaseDSOTestCase implements TransparentTestIface, TestConfigurator {
35
36   public static final int DEFAULT_CLIENT_COUNT = 2;
37   public static final int DEFAULT_INTENSITY = 10;
38
39   private TVSConfigurationSetupManagerFactory configFactory;
40   private DSOClientConfigHelper configHelper;
41   protected DistributedTestRunner runner;
42   private DistributedTestRunnerConfig runnerConfig = new DistributedTestRunnerConfig();
43   private TransparentAppConfig transparentAppConfig;
44   private ApplicationConfigBuilder possibleApplicationConfigBuilder;
45
46   private String JavaDoc mode;
47   private ServerControl serverControl;
48   private boolean controlledCrashMode = false;
49   private ServerCrasher crasher;
50
51   protected void setUp() throws Exception JavaDoc {
52     setUp(configFactory(), configHelper());
53
54     RestartTestHelper helper = null;
55
56     if (isCrashy()) {
57       helper = new RestartTestHelper(mode().equals(TestConfigObject.TRANSPARENT_TESTS_MODE_CRASH),
58                                      new RestartTestEnvironment(getTempDirectory(), new PortChooser(),
59                                                                 RestartTestEnvironment.PROD_MODE));
60       ((SettableConfigItem) configFactory().l2DSOConfig().listenPort()).setValue(helper.getServerPort());
61       serverControl = helper.getServerControl();
62     } else {
63       ((SettableConfigItem) configFactory().l2DSOConfig().listenPort()).setValue(0);
64     }
65
66     this.doSetUp(this);
67
68     if (isCrashy()) {
69       crasher = new ServerCrasher(serverControl, helper.getServerCrasherConfig().getRestartInterval(), helper
70           .getServerCrasherConfig().isCrashy());
71       crasher.startAutocrash();
72     }
73   }
74
75   protected final void setUpControlledServer(TVSConfigurationSetupManagerFactory factory, DSOClientConfigHelper helper,
76                                              int serverPort, int adminPort, String JavaDoc configFile) throws Exception JavaDoc {
77     controlledCrashMode = true;
78     serverControl = new ExtraProcessServerControl("localhost", serverPort, adminPort, configFile, true);
79     setUp(factory, helper);
80   }
81
82   private final void setUp(TVSConfigurationSetupManagerFactory factory, DSOClientConfigHelper helper) throws Exception JavaDoc {
83     super.setUp();
84     this.configFactory = factory;
85     this.configHelper = helper;
86     transparentAppConfig = new TransparentAppConfig(getApplicationClass().getName(), new TestGlobalIdGenerator(),
87                                                     DEFAULT_CLIENT_COUNT, DEFAULT_INTENSITY, serverControl);
88   }
89
90   protected synchronized final String JavaDoc mode() {
91     if (mode == null) {
92       try {
93         mode = TestConfigObject.getInstance().transparentTestsMode();
94       } catch (IOException JavaDoc ioe) {
95         throw new RuntimeException JavaDoc("Can't get mode", ioe);
96       }
97     }
98
99     return mode;
100   }
101
102   public void doSetUp(TransparentTestIface t) throws Exception JavaDoc {
103     // Nothing here, by default
104
}
105
106   private boolean isCrashy() {
107     return mode().equals(TestConfigObject.TRANSPARENT_TESTS_MODE_RESTART)
108            || mode().equals(TestConfigObject.TRANSPARENT_TESTS_MODE_CRASH);
109   }
110
111   public DSOClientConfigHelper getConfigHelper() {
112     return this.configHelper;
113   }
114
115   public TVSConfigurationSetupManagerFactory getConfigFactory() {
116     return configFactory;
117   }
118
119   public DistributedTestRunnerConfig getRunnerConfig() {
120     return this.runnerConfig;
121   }
122
123   public void setApplicationConfigBuilder(ApplicationConfigBuilder builder) {
124     this.possibleApplicationConfigBuilder = builder;
125   }
126
127   public TransparentAppConfig getTransparentAppConfig() {
128     return this.transparentAppConfig;
129   }
130
131   protected ApplicationConfigBuilder getApplicationConfigBuilder() {
132     if (possibleApplicationConfigBuilder != null) return possibleApplicationConfigBuilder;
133     else return transparentAppConfig;
134   }
135
136   public int getServerPort() {
137     if (getStartServer()) return this.runner.getServerPort();
138     else return new Integer JavaDoc(getServerPortProp()).intValue();
139   }
140
141   protected abstract Class JavaDoc getApplicationClass();
142
143   protected Map JavaDoc getOptionalAttributes() {
144     return new HashMap JavaDoc();
145   }
146
147   String JavaDoc getServerPortProp() {
148     return System.getProperty("test.base.server.port");
149   }
150
151   protected boolean getStartServer() {
152     return getServerPortProp() == null && mode().equals(TestConfigObject.TRANSPARENT_TESTS_MODE_NORMAL)
153            && !controlledCrashMode;
154   }
155
156   public void initializeTestRunner() throws Exception JavaDoc {
157     this.runner = new DistributedTestRunner(this.runnerConfig, configFactory, this.configHelper, getApplicationClass(),
158                                             getOptionalAttributes(), getApplicationConfigBuilder()
159                                                 .newApplicationConfig(), this.transparentAppConfig.getClientCount(),
160                                             this.transparentAppConfig.getApplicationInstancePerClientCount(),
161                                             getStartServer());
162   }
163
164   protected boolean canRun() {
165     return (mode().equals(TestConfigObject.TRANSPARENT_TESTS_MODE_NORMAL) && canRunNormal())
166            || (mode().equals(TestConfigObject.TRANSPARENT_TESTS_MODE_CRASH) && canRunCrash())
167            || (mode().equals(TestConfigObject.TRANSPARENT_TESTS_MODE_RESTART) && canRunRestart());
168   }
169
170   protected boolean canRunNormal() {
171     return true;
172   }
173
174   protected boolean canRunCrash() {
175     return false;
176   }
177
178   protected boolean canRunRestart() {
179     return false;
180   }
181
182   public void test() throws Exception JavaDoc {
183     if (canRun()) {
184       if (controlledCrashMode) serverControl.start(30 * 1000);
185       this.runner.run();
186
187       if (this.runner.executionTimedOut() || this.runner.startTimedOut()) {
188         try {
189           this.runner.dumpServer();
190         } finally {
191           ThreadDump.dumpThreadsMany(3, 1000L);
192         }
193       }
194
195       if (!this.runner.success()) {
196         AssertionFailedError e = new AssertionFailedError(new ErrorContextFormatter(this.runner.getErrors())
197             .formatForExceptionMessage());
198         throw e;
199       }
200     } else {
201       System.err.println("NOTE: " + getClass().getName() + " can't be run in mode '" + mode()
202                          + "', and thus will be skipped.");
203     }
204   }
205
206   protected void doDumpServerDetails() {
207     try {
208       if (this.runner != null) {
209         this.runner.dumpServer();
210       } else {
211         System.err.println("Runner is null !!");
212       }
213     } catch (Exception JavaDoc ex) {
214       ex.printStackTrace();
215     }
216   }
217
218   private static final class ErrorContextFormatter {
219     private final Collection JavaDoc contexts;
220     private final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
221
222     public ErrorContextFormatter(Collection JavaDoc contexts) {
223       this.contexts = contexts;
224     }
225
226     private void div() {
227       buf.append("\n**************************************************************\n");
228     }
229
230     private void println(Object JavaDoc message) {
231       buf.append(message + "\n");
232     }
233
234     public String JavaDoc formatForExceptionMessage() {
235       buf.delete(0, buf.length());
236       div();
237       println("There are " + contexts.size() + " error contexts:");
238       int count = 1;
239       for (Iterator JavaDoc i = contexts.iterator(); i.hasNext();) {
240         ErrorContext ctxt = (ErrorContext) i.next();
241         println("Error context " + count + "\n");
242         println(ctxt);
243         count++;
244       }
245       println("End error contexts.");
246       div();
247       return buf.toString();
248     }
249   }
250
251 }
252
Popular Tags