git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271036 13f79535-47bb-0310-9956-ffa450edef68master
@@ -1,170 +0,0 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs.optional.junit; | |||||
import org.apache.avalon.excalibur.i18n.ResourceManager; | |||||
import org.apache.avalon.excalibur.i18n.Resources; | |||||
import org.apache.tools.ant.BuildException; | |||||
import org.apache.tools.ant.Task; | |||||
/** | |||||
* The core JUnit task. | |||||
* | |||||
* @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a> | |||||
*/ | |||||
public class JUnitTask extends Task { | |||||
private final static Resources RES = | |||||
ResourceManager.getPackageResources(JUnitTask.class); | |||||
/** port to run the server on */ | |||||
private int port = -1; | |||||
/** timeout period in ms */ | |||||
private long timeout = -1; | |||||
/** client configuraiton element */ | |||||
private ClientElement client = null; | |||||
/** server configuration element */ | |||||
private ServerElement server = null; | |||||
// task implementation | |||||
public void execute() throws BuildException { | |||||
if (client == null && server == null) { | |||||
throw new BuildException("Invalid state: need to be server, client or both"); | |||||
} | |||||
// 1) server and client | |||||
if (server != null && client != null) { | |||||
ServerWorker worker = new ServerWorker(); | |||||
worker.start(); | |||||
client.execute(); | |||||
Exception caught = null; | |||||
try { | |||||
worker.join(); | |||||
caught = worker.getException(); | |||||
} catch (InterruptedException e){ | |||||
caught = e; | |||||
} | |||||
if (caught != null){ | |||||
throw new BuildException(caught); | |||||
} | |||||
return; | |||||
} | |||||
// 2) server only (waiting for client) | |||||
if (server != null && client == null) { | |||||
server.execute(); | |||||
return; | |||||
} | |||||
// 3) client only (connecting to server) | |||||
if (server == null && client != null) { | |||||
client.execute(); | |||||
return; | |||||
} | |||||
} | |||||
// Ant bean accessors | |||||
public void setPort(int port) { | |||||
this.port = port; | |||||
} | |||||
public void setTimeout(long timeout) { | |||||
this.timeout = timeout; | |||||
} | |||||
/** | |||||
* create a new client in charge of running tests and sending | |||||
* the results to the server that collect them. | |||||
*/ | |||||
public ClientElement createClient() { | |||||
if (client == null) { | |||||
client = new ClientElement(this); | |||||
} | |||||
return client; | |||||
} | |||||
/** | |||||
* create a new client in charge of running tests and sending | |||||
* the results to the server that collect them. | |||||
*/ | |||||
public ServerElement createServer() { | |||||
if (server == null) { | |||||
server = new ServerElement(this); | |||||
} | |||||
return server; | |||||
} | |||||
/** the worker to run the server on */ | |||||
class ServerWorker extends Thread { | |||||
private Exception caught = null; | |||||
public void run() { | |||||
try { | |||||
server.execute(); | |||||
} catch (Exception e) { | |||||
caught = e; | |||||
} | |||||
} | |||||
public Exception getException() { | |||||
return caught; | |||||
} | |||||
} | |||||
} |
@@ -167,7 +167,7 @@ public class XMLFormatter extends BaseStreamFormatter { | |||||
} | } | ||||
public void onTestFailure(TestRunEvent evt) { | public void onTestFailure(TestRunEvent evt) { | ||||
String type = evt == evt.getType() == TestRunEvent.TEST_FAILURE ? FAILURE : ERROR; | |||||
String type = evt.getType() == TestRunEvent.TEST_FAILURE ? FAILURE : ERROR; | |||||
Element nested = doc.createElement(type); | Element nested = doc.createElement(type); | ||||
Element currentTest = (Element) testElements.get(evt.getName()); | Element currentTest = (Element) testElements.get(evt.getName()); | ||||
currentTest.appendChild(nested); | currentTest.appendChild(nested); | ||||
@@ -1,101 +0,0 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs.optional.junit.remote; | |||||
/** | |||||
* A set of messages identifiers to be used for communication | |||||
* between server/client(TestRunner). | |||||
* | |||||
* <i> | |||||
* This code is based on the code from Erich Gamma made for the | |||||
* JUnit plugin for <a href="http://www.eclipse.org">Eclipse</a> and is | |||||
* merged with code originating from Ant 1.4.x. | |||||
* </i> | |||||
* | |||||
* @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a> | |||||
*/ | |||||
public interface MessageIds { | |||||
int MSG_HEADER_LENGTH = 8; | |||||
// messages send by TestRunServer | |||||
String TRACE_START = "%TRACES "; | |||||
String TRACE_END = "%TRACEE "; | |||||
// a line printed on stdout | |||||
String STDOUT_START = "%STDOUTS"; | |||||
String STDOUT_END = "%STDOUTE"; | |||||
// a line printed on stderr | |||||
String STDERR_START = "%STDERRS"; | |||||
String STDERR_END = "%STDERRE"; | |||||
// JVM system properties used in the VM | |||||
String PROPS_START = "%SYSPROS"; | |||||
String PROPS_END = "%SYSPROE"; | |||||
// test run started... | |||||
String TEST_COUNT = "%TESTC "; | |||||
// a test just started | |||||
String TEST_START = "%TESTS "; | |||||
// a test is finished | |||||
String TEST_END = "%TESTE "; | |||||
String TEST_ERROR = "%ERROR "; | |||||
String TEST_FAILED = "%FAILED "; | |||||
String TEST_ELAPSED_TIME = "%RUNTIME"; | |||||
String TEST_STOPPED = "%TSTSTP "; | |||||
// messages understood by the Server | |||||
String TEST_STOP = ">STOP "; | |||||
} |
@@ -1,243 +0,0 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs.optional.junit.remote; | |||||
import java.io.BufferedReader; | |||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.io.InputStreamReader; | |||||
import java.util.Properties; | |||||
import java.util.Vector; | |||||
import org.apache.tools.ant.taskdefs.optional.junit.TestRunListener; | |||||
/** | |||||
* Read and dispatch messages received via an input stream. | |||||
* The inputstream should be the connection to the remote client. | |||||
* <p> | |||||
* All messages are dispatched to the registered listeners. | |||||
* </p> | |||||
* <i> | |||||
* This code is based on the code from Erich Gamma made for the | |||||
* JUnit plugin for <a href="http://www.eclipse.org">Eclipse</a> and is | |||||
* merged with code originating from Ant 1.4.x. | |||||
* </i> | |||||
* | |||||
* @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a> | |||||
*/ | |||||
public class MessageReader { | |||||
/** the set of registered listeners */ | |||||
private Vector listeners = new Vector(); | |||||
// communication states with client | |||||
private boolean inReadTrace = false; | |||||
private boolean inFailedMessage = false; | |||||
private String failedTest; | |||||
private String failedMessage; | |||||
private String failedTrace; | |||||
private int failureKind; | |||||
private long elapsedTime; | |||||
private Properties sysprops; | |||||
public MessageReader() { | |||||
} | |||||
/** | |||||
* Add a new listener. | |||||
* @param listener a listener that will receive events from the client. | |||||
*/ | |||||
public void addListener(TestRunListener listener) { | |||||
listeners.addElement(listener); | |||||
} | |||||
public void removeListener(TestRunListener listener) { | |||||
listeners.removeElement(listener); | |||||
} | |||||
/** | |||||
* Read a complete stream from a client, it will only return | |||||
* once the connection is stopped. You'd better not reuse | |||||
* an instance of this class since there are instance variables used | |||||
* to keep track of the client state. | |||||
* @param in the inputstream to the client. | |||||
*/ | |||||
public void process(InputStream in) throws IOException { | |||||
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF8")); | |||||
String line; | |||||
while ((line = reader.readLine()) != null) { | |||||
processMessage(line); | |||||
} | |||||
} | |||||
/** | |||||
* Process a message from the client and dispatch the | |||||
* appropriate message to the listeners. | |||||
*/ | |||||
protected void processMessage(String message) { | |||||
if (message == null) { | |||||
return; | |||||
} | |||||
String arg = message.substring(MessageIds.MSG_HEADER_LENGTH); | |||||
if (message.startsWith(MessageIds.TRACE_START)) { | |||||
failedTrace = arg.substring(0, arg.indexOf(MessageIds.TRACE_END)); | |||||
failedTrace = new String(Base64.decode(failedTrace.getBytes())); | |||||
notifyTestFailed(failureKind, failedTest, failedTrace); | |||||
return; | |||||
} | |||||
if (message.startsWith(MessageIds.TEST_COUNT)) { | |||||
int count = Integer.parseInt(arg); | |||||
notifyTestSuiteStarted(count); | |||||
return; | |||||
} | |||||
if (message.startsWith(MessageIds.TEST_START)) { | |||||
notifyTestStarted(arg); | |||||
return; | |||||
} | |||||
if (message.startsWith(MessageIds.TEST_END)) { | |||||
notifyTestEnded(arg); | |||||
return; | |||||
} | |||||
if (message.startsWith(MessageIds.TEST_ERROR)) { | |||||
failedTest = arg; | |||||
failureKind = TestRunListener.STATUS_ERROR; | |||||
return; | |||||
} | |||||
if (message.startsWith(MessageIds.TEST_FAILED)) { | |||||
failedTest = arg; | |||||
failureKind = TestRunListener.STATUS_FAILURE; | |||||
return; | |||||
} | |||||
if (message.startsWith(MessageIds.TEST_ELAPSED_TIME)) { | |||||
elapsedTime = Long.parseLong(arg); | |||||
notifyTestSuiteEnded(elapsedTime); | |||||
return; | |||||
} | |||||
if (message.startsWith(MessageIds.TEST_STOPPED)) { | |||||
elapsedTime = Long.parseLong(arg); | |||||
notifyTestSuiteStopped(elapsedTime); | |||||
return; | |||||
} | |||||
if (message.startsWith(MessageIds.PROPS_START)) { | |||||
try { | |||||
byte[] bytes = arg.substring(0, arg.indexOf(MessageIds.PROPS_END)).getBytes(); | |||||
bytes = Base64.decode(bytes); | |||||
sysprops = (Properties) SocketUtil.deserialize(bytes); | |||||
notifyTestSystemProperties(sysprops); | |||||
} catch (Exception e) { | |||||
// ignore now | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} | |||||
protected void notifyTestStarted(String testname) { | |||||
synchronized (listeners) { | |||||
for (int i = 0; i < listeners.size(); i++) { | |||||
((TestRunListener) listeners.elementAt(i)).onTestStarted(testname); | |||||
} | |||||
} | |||||
} | |||||
protected void notifyTestEnded(String testname) { | |||||
synchronized (listeners) { | |||||
for (int i = 0; i < listeners.size(); i++) { | |||||
((TestRunListener) listeners.elementAt(i)).onTestEnded(testname); | |||||
} | |||||
} | |||||
} | |||||
protected void notifyTestFailed(int kind, String testname, String trace) { | |||||
synchronized (listeners) { | |||||
for (int i = 0; i < listeners.size(); i++) { | |||||
((TestRunListener) listeners.elementAt(i)).onTestFailure(kind); | |||||
} | |||||
} | |||||
} | |||||
protected void notifyTestSuiteStarted(int count) { | |||||
synchronized (listeners) { | |||||
for (int i = 0; i < listeners.size(); i++) { | |||||
((TestRunListener) listeners.elementAt(i)).onRunStarted(count); | |||||
} | |||||
} | |||||
} | |||||
protected void notifyTestSuiteEnded(long elapsedtime) { | |||||
synchronized (listeners) { | |||||
for (int i = 0; i < listeners.size(); i++) { | |||||
((TestRunListener) listeners.elementAt(i)).onRunEnded(elapsedtime); | |||||
} | |||||
} | |||||
} | |||||
protected void notifyTestSuiteStopped(long elapsedtime) { | |||||
synchronized (listeners) { | |||||
for (int i = 0; i < listeners.size(); i++) { | |||||
((TestRunListener) listeners.elementAt(i)).onRunStopped(elapsedtime); | |||||
} | |||||
} | |||||
} | |||||
protected void notifyTestSystemProperties(Properties props) { | |||||
synchronized (listeners) { | |||||
for (int i = 0; i < listeners.size(); i++) { | |||||
((TestRunListener) listeners.elementAt(i)).onTestRunSystemProperties(props); | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -1,152 +0,0 @@ | |||||
/* | |||||
* The Apache Software License, Version 1.1 | |||||
* | |||||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||||
* reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in | |||||
* the documentation and/or other materials provided with the | |||||
* distribution. | |||||
* | |||||
* 3. The end-user documentation included with the redistribution, if | |||||
* any, must include the following acknowlegement: | |||||
* "This product includes software developed by the | |||||
* Apache Software Foundation (http://www.apache.org/)." | |||||
* Alternately, this acknowlegement may appear in the software itself, | |||||
* if and wherever such third-party acknowlegements normally appear. | |||||
* | |||||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||||
* Foundation" must not be used to endorse or promote products derived | |||||
* from this software without prior written permission. For written | |||||
* permission, please contact apache@apache.org. | |||||
* | |||||
* 5. Products derived from this software may not be called "Apache" | |||||
* nor may "Apache" appear in their names without prior written | |||||
* permission of the Apache Group. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
* ==================================================================== | |||||
* | |||||
* This software consists of voluntary contributions made by many | |||||
* individuals on behalf of the Apache Software Foundation. For more | |||||
* information on the Apache Software Foundation, please see | |||||
* <http://www.apache.org/>. | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs.optional.junit.remote; | |||||
import java.io.OutputStream; | |||||
import java.io.PrintWriter; | |||||
import org.apache.tools.ant.taskdefs.optional.junit.TestRunListener; | |||||
/** | |||||
* A wrapper that sends string messages to a given stream. | |||||
* | |||||
* <i> | |||||
* This code is based on the code from Erich Gamma made for the | |||||
* JUnit plugin for <a href="http://www.eclipse.org">Eclipse</a> and is | |||||
* merged with code originating from Ant 1.4.x. | |||||
* </i> | |||||
* | |||||
* @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a> | |||||
*/ | |||||
public class MessageWriter implements MessageIds { | |||||
private PrintWriter pw; | |||||
public MessageWriter(OutputStream out) { | |||||
this.pw = new PrintWriter(out, true); | |||||
} | |||||
protected void finalize() { | |||||
close(); | |||||
} | |||||
public void close() { | |||||
if (pw != null) { | |||||
pw.close(); | |||||
pw = null; | |||||
} | |||||
} | |||||
public void sendMessage(String msg) { | |||||
pw.println(msg); | |||||
} | |||||
// -------- notifier helper methods | |||||
public void notifyTestRunStarted(int testCount) { | |||||
sendMessage(MessageIds.TEST_COUNT + testCount); | |||||
} | |||||
public void notifyTestRunEnded(long elapsedTime) { | |||||
sendMessage(MessageIds.TEST_ELAPSED_TIME + elapsedTime); | |||||
} | |||||
public void notifyTestRunStopped(long elapsedTime) { | |||||
sendMessage(MessageIds.TEST_STOPPED + elapsedTime); | |||||
} | |||||
public void notifyTestStarted(String testName) { | |||||
sendMessage(MessageIds.TEST_START + testName); | |||||
} | |||||
public void notifyTestEnded(String testName) { | |||||
sendMessage(MessageIds.TEST_END + testName); | |||||
} | |||||
public void notifyTestFailed(int status, String testName, String trace) { | |||||
if (status == TestRunListener.STATUS_FAILURE) { | |||||
sendMessage(MessageIds.TEST_FAILED + testName); | |||||
} else { | |||||
sendMessage(MessageIds.TEST_ERROR + testName); | |||||
} | |||||
sendMessage(MessageIds.TRACE_START + new String(Base64.encode(trace.getBytes())) + MessageIds.TRACE_END); | |||||
} | |||||
public void notifyStdOutLine(String testname, String line) { | |||||
sendMessage(MessageIds.STDOUT_START); | |||||
sendMessage(line); | |||||
sendMessage(MessageIds.STDOUT_END); | |||||
} | |||||
public void notifyStdErrLine(String testname, String line) { | |||||
sendMessage(MessageIds.STDERR_START); | |||||
sendMessage(line); | |||||
sendMessage(MessageIds.STDERR_END); | |||||
} | |||||
public void notifySystemProperties() { | |||||
try { | |||||
StringBuffer msg = new StringBuffer(512); | |||||
msg.append(MessageIds.PROPS_START); | |||||
byte[] data = SocketUtil.serialize(System.getProperties()); | |||||
msg.append(Base64.encode(data)); | |||||
msg.append(MessageIds.PROPS_END); | |||||
sendMessage(msg.toString()); | |||||
} catch (Exception e) { | |||||
// ignore | |||||
e.printStackTrace(); | |||||
} | |||||
} | |||||
} |