001: package org.objectweb.celtix.testutil.common;
002:
003: import java.io.IOException;
004: import java.util.ArrayList;
005: import java.util.List;
006: import java.util.Map;
007:
008: import junit.extensions.TestSetup;
009: import junit.framework.Test;
010:
011: public abstract class AbstractClientServerSetupBase extends TestSetup {
012: private final List<ServerLauncher> launchers = new ArrayList<ServerLauncher>();
013:
014: public AbstractClientServerSetupBase(Test arg0) {
015: super (arg0);
016: }
017:
018: public void setUp() throws Exception {
019: startServers();
020: }
021:
022: public abstract void startServers() throws Exception;
023:
024: public void tearDown() throws Exception {
025: boolean serverPassed = stopAllServers();
026: launchers.clear();
027: System.gc();
028: assertTrue("server failed", serverPassed);
029: }
030:
031: protected boolean stopAllServers() {
032: boolean passed = true;
033: for (ServerLauncher sl : launchers) {
034: try {
035: sl.signalStop();
036: } catch (IOException ex) {
037: ex.printStackTrace();
038: }
039: }
040: for (ServerLauncher sl : launchers) {
041: try {
042: passed = passed && sl.stopServer();
043: } catch (IOException ex) {
044: ex.printStackTrace();
045: }
046: }
047: launchers.clear();
048: return passed;
049: }
050:
051: public boolean launchServer(Class<?> clz) {
052: boolean ok = false;
053: try {
054: ServerLauncher sl = new ServerLauncher(clz.getName());
055: ok = sl.launchServer();
056: assertTrue("server failed to launch", ok);
057: launchers.add(sl);
058: } catch (IOException ex) {
059: ex.printStackTrace();
060: fail("failed to launch server " + clz);
061: }
062:
063: return ok;
064: }
065:
066: public boolean launchServer(Class<?> clz, boolean inProcess) {
067: boolean ok = false;
068: try {
069: ServerLauncher sl = new ServerLauncher(clz.getName(),
070: inProcess);
071: ok = sl.launchServer();
072: assertTrue("server failed to launch", ok);
073: launchers.add(sl);
074: } catch (IOException ex) {
075: ex.printStackTrace();
076: fail("failed to launch server " + clz);
077: }
078:
079: return ok;
080: }
081:
082: public boolean launchServer(Class<?> clz,
083: Map<String, String> props, String[] args) {
084: return launchServer(clz, props, args, false);
085: }
086:
087: public boolean launchServer(Class<?> clz,
088: Map<String, String> props, String[] args, boolean inProcess) {
089: boolean ok = false;
090: try {
091: ServerLauncher sl = new ServerLauncher(clz.getName(),
092: props, args, inProcess);
093: ok = sl.launchServer();
094: assertTrue("server failed to launch", ok);
095: launchers.add(sl);
096: } catch (IOException ex) {
097: ex.printStackTrace();
098: fail("failed to launch server " + clz);
099: }
100:
101: return ok;
102: }
103:
104: }
|