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