001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import org.apache.commons.io.CopyUtils;
008:
009: import com.tc.config.schema.SettableConfigItem;
010: import com.tc.config.schema.setup.FatalIllegalConfigurationChangeHandler;
011: import com.tc.config.schema.setup.L1TVSConfigurationSetupManager;
012: import com.tc.config.schema.setup.TestTVSConfigurationSetupManagerFactory;
013: import com.tc.config.schema.test.TerracottaConfigBuilder;
014: import com.tc.object.config.StandardDSOClientConfigHelperImpl;
015: import com.tc.util.Assert;
016: import com.tc.util.PortChooser;
017: import com.tctest.runner.TransparentAppConfig;
018:
019: import java.io.File;
020: import java.io.FileOutputStream;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: public abstract class ServerCrashingTestBase extends
025: TransparentTestBase {
026:
027: private final int nodeCount;
028: private int port;
029: private int adminPort;
030: private File configFile;
031: private final List jvmArgs;
032:
033: protected ServerCrashingTestBase(int nodeCount,
034: String jvmArgsArray[]) {
035: this .nodeCount = nodeCount;
036: this .jvmArgs = new ArrayList();
037: for (int i = 0; jvmArgsArray != null && i < jvmArgsArray.length; i++) {
038: jvmArgs.add(jvmArgsArray[i]);
039: }
040: }
041:
042: protected ServerCrashingTestBase(int nodeCount) {
043: this (nodeCount, null);
044: }
045:
046: public void setUp() throws Exception {
047:
048: // for some test cases to enable l1reconnect
049: if (enableL1Reconnect()) {
050: setJvmArgsL1Reconnect((ArrayList) jvmArgs);
051: }
052:
053: // XXX: ERR! HACK! Will collide eventually
054: PortChooser pc = new PortChooser();
055: port = pc.chooseRandomPort();
056: adminPort = pc.chooseRandomPort();
057: configFile = getTempFile("config-file.xml");
058: writeConfigFile();
059:
060: TestTVSConfigurationSetupManagerFactory factory = new TestTVSConfigurationSetupManagerFactory(
061: TestTVSConfigurationSetupManagerFactory.MODE_DISTRIBUTED_CONFIG,
062: null, new FatalIllegalConfigurationChangeHandler());
063:
064: ((SettableConfigItem) configFactory().l2DSOConfig()
065: .listenPort()).setValue(port);
066: ((SettableConfigItem) configFactory().l2CommonConfig()
067: .jmxPort()).setValue(adminPort);
068: factory.addServerToL1Config(null, port, adminPort);
069: L1TVSConfigurationSetupManager manager = factory
070: .createL1TVSConfigurationSetupManager();
071: setUpControlledServer(factory,
072: new StandardDSOClientConfigHelperImpl(manager), port,
073: adminPort, configFile.getAbsolutePath(), jvmArgs);
074:
075: getTransparentAppConfig().setClientCount(nodeCount);
076: initializeTestRunner();
077: TransparentAppConfig cfg = getTransparentAppConfig();
078: cfg.setAttribute(ServerCrashingAppBase.CONFIG_FILE, configFile
079: .getAbsolutePath());
080: cfg.setAttribute(ServerCrashingAppBase.PORT_NUMBER, String
081: .valueOf(port));
082: cfg.setAttribute(ServerCrashingAppBase.HOST_NAME, "localhost");
083:
084: doSetUp(this );
085: }
086:
087: private synchronized void writeConfigFile() {
088: try {
089: TerracottaConfigBuilder cb = new TerracottaConfigBuilder();
090:
091: cb.getServers().getL2s()[0].setDSOPort(port);
092: cb.getServers().getL2s()[0].setJMXPort(adminPort);
093:
094: createConfig(cb);
095:
096: FileOutputStream out = new FileOutputStream(configFile);
097: CopyUtils.copy(cb.toString(), out);
098: out.close();
099: } catch (Exception e) {
100: throw Assert.failure("Can't create config file", e);
101: }
102: }
103:
104: protected abstract void createConfig(TerracottaConfigBuilder cb);
105:
106: public int getPort() {
107: return port;
108: }
109:
110: public int getAdminPort() {
111: return adminPort;
112: }
113:
114: public File getConfigFile() {
115: return configFile;
116: }
117: }
|