KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ServerCrashingTestBase


1 /*
2  * All content copyright (c) 2003-2007 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 org.apache.commons.io.CopyUtils;
8
9 import com.tc.config.schema.setup.FatalIllegalConfigurationChangeHandler;
10 import com.tc.config.schema.setup.L1TVSConfigurationSetupManager;
11 import com.tc.config.schema.setup.StandardTVSConfigurationSetupManagerFactory;
12 import com.tc.config.schema.setup.TVSConfigurationSetupManagerFactory;
13 import com.tc.config.schema.test.TerracottaConfigBuilder;
14 import com.tc.object.config.StandardDSOClientConfigHelper;
15 import com.tc.util.Assert;
16 import com.tc.util.PortChooser;
17 import com.tctest.runner.TransparentAppConfig;
18
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21
22 public abstract class ServerCrashingTestBase extends TransparentTestBase {
23
24   private final int nodeCount;
25   private int port;
26   private int adminPort;
27   private File JavaDoc configFile;
28
29   protected ServerCrashingTestBase(int nodeCount) {
30     this.nodeCount = nodeCount;
31   }
32
33   public void setUp() throws Exception JavaDoc {
34     // XXX: ERR! HACK! Will collide eventually
35
PortChooser pc = new PortChooser();
36     port = pc.chooseRandomPort();
37     adminPort = pc.chooseRandomPort();
38     configFile = getTempFile("config-file.xml");
39     writeConfigFile();
40
41     TVSConfigurationSetupManagerFactory factory;
42     factory = new StandardTVSConfigurationSetupManagerFactory(new String JavaDoc[] {
43         StandardTVSConfigurationSetupManagerFactory.CONFIG_SPEC_ARGUMENT_WORD, configFile.getAbsolutePath() }, true,
44                                                               new FatalIllegalConfigurationChangeHandler());
45
46     L1TVSConfigurationSetupManager manager = factory.createL1TVSConfigurationSetupManager();
47     setUpControlledServer(factory, new StandardDSOClientConfigHelper(manager), port, adminPort, configFile
48         .getAbsolutePath());
49
50     getTransparentAppConfig().setClientCount(nodeCount);
51     initializeTestRunner();
52     TransparentAppConfig cfg = getTransparentAppConfig();
53     cfg.setAttribute(ServerCrashingAppBase.CONFIG_FILE, configFile.getAbsolutePath());
54     cfg.setAttribute(ServerCrashingAppBase.PORT_NUMBER, String.valueOf(port));
55     cfg.setAttribute(ServerCrashingAppBase.HOST_NAME, "localhost");
56
57     doSetUp(this);
58   }
59
60   private synchronized void writeConfigFile() {
61     try {
62       TerracottaConfigBuilder cb = new TerracottaConfigBuilder();
63
64       cb.getServers().getL2s()[0].setDSOPort(port);
65       cb.getServers().getL2s()[0].setJMXPort(adminPort);
66
67       createConfig(cb);
68
69       FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(configFile);
70       CopyUtils.copy(cb.toString(), out);
71       out.close();
72     } catch (Exception JavaDoc e) {
73       throw Assert.failure("Can't create config file", e);
74     }
75   }
76
77   protected abstract void createConfig(TerracottaConfigBuilder cb);
78
79   public int getPort() {
80     return port;
81   }
82
83   public int getAdminPort() {
84     return adminPort;
85   }
86
87   public File JavaDoc getConfigFile() {
88     return configFile;
89   }
90 }
91
Popular Tags