01: package org.objectweb.celtix.testutil.common;
02:
03: import java.util.logging.Logger;
04:
05: import junit.framework.Assert;
06:
07: public abstract class AbstractTestServerBase extends Assert {
08:
09: /**
10: * template method implemented by test servers. Initialise
11: * servants and publish endpoints etc.
12: *
13: */
14: protected abstract void run();
15:
16: protected Logger getLog() {
17: String loggerName = this .getClass().getName();
18: return Logger.getLogger(loggerName);
19: }
20:
21: public void startInProcess() throws Exception {
22: System.out.println("running server in-process");
23: run();
24: System.out.println("signal ready");
25: ready();
26: }
27:
28: public boolean stopInProcess() throws Exception {
29: boolean ret = true;
30: if (verify(getLog())) {
31: System.out.println("server passed");
32: } else {
33: ret = false;
34: }
35: return ret;
36: }
37:
38: public void start() {
39: try {
40: System.out.println("running server");
41: run();
42: System.out.println("signal ready");
43: ready();
44:
45: // wait for a key press then shut
46: // down the server
47: //
48: System.in.read();
49: System.out.println("stopping bus");
50: tearDown();
51: } catch (Throwable ex) {
52: ex.printStackTrace();
53: startFailed();
54: } finally {
55: if (verify(getLog())) {
56: System.out.println("server passed");
57: } else {
58: System.out.println(ServerLauncher.SERVER_FAILED);
59: }
60: System.out.println("server stopped");
61: System.exit(0);
62: }
63: }
64:
65: public void setUp() throws Exception {
66: // emtpy
67: }
68:
69: public void tearDown() throws Exception {
70: // empty
71: }
72:
73: protected void ready() {
74: System.out.println("server ready");
75: }
76:
77: protected void startFailed() {
78: System.out.println(ServerLauncher.SERVER_FAILED);
79: System.exit(-1);
80: }
81:
82: /**
83: * Used to facilitate assertions on server-side behaviour.
84: *
85: * @param log logger to use for diagnostics if assertions fail
86: * @return true if assertions hold
87: */
88: protected boolean verify(Logger l) {
89: return true;
90: }
91: }
|