001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.testutil.common;
019:
020: import java.util.logging.Logger;
021:
022: import junit.framework.Assert;
023:
024: public abstract class AbstractTestServerBase extends Assert {
025: boolean inProcess;
026:
027: /**
028: * method implemented by test servers. Initialise
029: * servants and publish endpoints etc.
030: *
031: */
032: protected abstract void run();
033:
034: protected Logger getLog() {
035: String loggerName = this .getClass().getName();
036: return Logger.getLogger(loggerName);
037: }
038:
039: public void startInProcess() throws Exception {
040: inProcess = true;
041: //System.out.println("running server in-process");
042: run();
043: //System.out.println("signal ready");
044: ready();
045: }
046:
047: public boolean stopInProcess() throws Exception {
048: boolean ret = true;
049: if (verify(getLog())) {
050: if (!inProcess) {
051: System.out.println("server passed");
052: }
053: } else {
054: ret = false;
055: }
056: return ret;
057: }
058:
059: public void start() {
060: try {
061: System.out.println("running server");
062: run();
063: System.out.println("signal ready");
064: ready();
065:
066: // wait for a key press then shut
067: // down the server
068: //
069: System.in.read();
070: System.out.println("stopping bus");
071: tearDown();
072: } catch (Throwable ex) {
073: ex.printStackTrace();
074: startFailed();
075: } finally {
076: if (verify(getLog())) {
077: System.out.println("server passed");
078: } else {
079: System.out.println(ServerLauncher.SERVER_FAILED);
080: }
081: System.out.println("server stopped");
082: System.exit(0);
083: }
084: }
085:
086: public void setUp() throws Exception {
087: // emtpy
088: }
089:
090: public void tearDown() throws Exception {
091: // empty
092: }
093:
094: protected void ready() {
095: if (!inProcess) {
096: System.out.println("server ready");
097: }
098: }
099:
100: protected void startFailed() {
101: System.out.println(ServerLauncher.SERVER_FAILED);
102: System.exit(-1);
103: }
104:
105: /**
106: * Used to facilitate assertions on server-side behaviour.
107: *
108: * @param log logger to use for diagnostics if assertions fail
109: * @return true if assertions hold
110: */
111: protected boolean verify(Logger log) {
112: return true;
113: }
114: }
|