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.runner;
004:
005: import java.io.*;
006: import fitnesse.http.*;
007: import fitnesse.components.CommandLine;
008: import fitnesse.util.FileUtil;
009:
010: public class FormattingOption {
011: public String format;
012:
013: public boolean usingStdout = false;
014:
015: public OutputStream output;
016:
017: public String host;
018:
019: public int port;
020:
021: public String rootPath;
022:
023: private int status;
024:
025: public String filename;
026:
027: private String resultFilename;
028:
029: public static void main(String[] args) throws Exception {
030: FormattingOption option = new FormattingOption();
031: option.args(args);
032: File inputFile = new File(option.resultFilename);
033: FileInputStream input = new FileInputStream(inputFile);
034: int byteCount = (int) inputFile.length();
035: option.process(input, byteCount);
036: }
037:
038: private FormattingOption() {
039: }
040:
041: private void args(String[] args) throws Exception {
042: CommandLine commandLine = new CommandLine(
043: "resultFilename format outputFilename host port rootPath");
044: if (!commandLine.parse(args))
045: usage();
046: resultFilename = commandLine.getArgument("resultFilename");
047: format = commandLine.getArgument("format");
048: filename = commandLine.getArgument("outputFilename");
049: host = commandLine.getArgument("host");
050: port = Integer.parseInt(commandLine.getArgument("port"));
051: rootPath = commandLine.getArgument("rootPath");
052: setOutput(System.out);
053: }
054:
055: private void usage() {
056: System.out
057: .println("java fitnesse.runner.FormattingOption resultFilename format outputFilename host port rootPath");
058: System.out
059: .println("\tresultFilename:\tthe name of the file containing test results");
060: System.out.println("\tformat: \traw|html|xml|...");
061: System.out
062: .println("\toutputfilename:\tstdout|a filename where the formatted results are to be stored");
063: System.out
064: .println("\thost: \tthe domain name of the hosting FitNesse server");
065: System.out
066: .println("\tport: \tthe port on which the hosting FitNesse server is running");
067: System.out
068: .println("\trootPath: \tname of the test page or suite page");
069: System.exit(-1);
070: }
071:
072: public FormattingOption(String format, String filename,
073: OutputStream stdout, String host, int port, String rootPath)
074: throws Exception {
075: this .format = format;
076: this .filename = filename;
077: setOutput(stdout);
078: this .host = host;
079: this .port = port;
080: this .rootPath = rootPath;
081: }
082:
083: private void setOutput(OutputStream stdout)
084: throws FileNotFoundException {
085: if ("stdout".equals(filename)) {
086: this .output = stdout;
087: this .usingStdout = true;
088: } else
089: this .output = new FileOutputStream(filename);
090: }
091:
092: public void process(InputStream inputStream, int size)
093: throws Exception {
094: if ("raw".equals(format))
095: FileUtil.copyBytes(inputStream, output);
096: else {
097: RequestBuilder request = buildRequest(inputStream, size);
098: ResponseParser response = ResponseParser
099: .performHttpRequest(host, port, request);
100: status = response.getStatus();
101: output.write(response.getBody().getBytes("UTF-8"));
102: }
103: if (!usingStdout)
104: output.close();
105: }
106:
107: public boolean wasSuccessful() {
108: return status == 200;
109: }
110:
111: public RequestBuilder buildRequest(InputStream inputStream, int size)
112: throws Exception {
113: RequestBuilder request = new RequestBuilder("/" + rootPath);
114: request.setMethod("POST");
115: request.setHostAndPort(host, port);
116: request.addInput("responder", "format");
117: request.addInput("format", format);
118: request.addInputAsPart("results", inputStream, size,
119: "text/plain");
120: return request;
121: }
122: }
|