001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse;
004:
005: import fitnesse.http.*;
006:
007: import java.net.*;
008: import java.io.*;
009:
010: //TODO-MdM DELETE ME SOON
011: public class TestRunner {
012: public URL url;
013:
014: public boolean verbose = false;
015:
016: public boolean showHtml = false;
017:
018: public static void main(String[] args) throws Exception {
019: System.out.println("***************************************");
020: System.out.println("THIS TEST RUNNER HAS BEEN DEPRECATED!!!");
021: System.out.println("Use fitnesse.runner.TestRunner instead.");
022: System.out.println("***************************************");
023: TestRunner runner = new TestRunner();
024: int exitCode = runner.run(args);
025:
026: System.exit(exitCode);
027: }
028:
029: public int run(String[] args) throws Exception {
030: int exitCode = -1;
031: if (acceptAgrs(args)) {
032: printMessage("Running tests at: " + url);
033: ResponseParser response = getResponse(url);
034: exitCode = getExitCode(response);
035: printMessage(exitCode + " failure(s)");
036: if (showHtml)
037: System.out.println(response.getBody());
038: } else
039: printUsage();
040: return exitCode;
041: }
042:
043: public int getExitCode(ResponseParser response) throws Exception {
044: int retValue = -1;
045: String exitCodeString = response.getHeader("Exit-Code");
046: if (exitCodeString == null)
047: throw new Exception(
048: "The response did not contain the needed 'Exit-Code' header. Was the URL correct?");
049: else
050: retValue = Integer.parseInt(exitCodeString);
051:
052: return retValue;
053: }
054:
055: private void printMessage(String message) {
056: if (verbose) {
057: System.out.println(message);
058: }
059: }
060:
061: public ResponseParser getResponse(URL url) throws Exception {
062: String resource = url.getPath() + "?" + url.getQuery();
063: RequestBuilder request = new RequestBuilder(resource);
064: int port = url.getPort() == -1 ? 80 : url.getPort();
065: String host = url.getHost();
066:
067: Socket s = new Socket(host, port);
068: OutputStream output = s.getOutputStream();
069: output.write(request.getText().getBytes());
070: InputStream input = s.getInputStream();
071: ResponseParser response = new ResponseParser(input);
072: output.close();
073: input.close();
074: s.close();
075:
076: return response;
077: }
078:
079: public boolean acceptAgrs(String[] args) throws Exception {
080: if (args.length < 1)
081: return false;
082: try {
083: for (int i = 0; i < args.length; i++) {
084: String arg = args[i];
085: if (arg.startsWith("-")) {
086: boolean isValid = setOption(arg.substring(1));
087: if (!isValid)
088: return false;
089: } else {
090: boolean isValid = setUrl(arg);
091: if (!isValid)
092: return false;
093: }
094: }
095: return true;
096: } catch (Exception e) {
097: return false;
098: }
099: }
100:
101: private boolean setOption(String option) {
102: boolean isValidOption = true;
103: if ("v".equals(option))
104: verbose = true;
105: else if ("h".equals(option))
106: showHtml = true;
107: else
108: isValidOption = false;
109:
110: return isValidOption;
111: }
112:
113: private boolean setUrl(String urlString) {
114: try {
115: url = new URL(urlString);
116: return true;
117: } catch (MalformedURLException e) {
118: printMessage(e.getMessage());
119: return false;
120: }
121: }
122:
123: private void printUsage() {
124: System.err
125: .println("Usage: java fitnesse.TestRunner [-vh] <URL>");
126: System.err.println("\t-v verbose");
127: System.err.println("\t-h show html output");
128: System.err
129: .println("\tThe URL should be a FitNesse page test execution, ");
130: System.err.println("\tending in either '?suite' or '?test");
131: }
132: }
|