01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.responders.run;
04:
05: import fitnesse.runner.*;
06: import fitnesse.*;
07: import fitnesse.components.FitProtocol;
08: import fitnesse.util.*;
09: import fitnesse.http.*;
10: import java.io.*;
11: import fit.Counts;
12:
13: public class TestResultFormattingResponder implements Responder {
14: public ResultFormatter formatter = new MockResultFormatter();
15:
16: public Counts finalCounts;
17:
18: private FitNesseContext context;
19:
20: private String baseUrl;
21:
22: private String rootPath;
23:
24: public Response makeResponse(FitNesseContext context,
25: Request request) throws Exception {
26: init(context, request);
27:
28: String results = (String) request.getInput("results");
29: byte[] bytes = results.getBytes("UTF-8");
30: processResults(new ByteArrayInputStream(bytes));
31:
32: InputStreamResponse response = new InputStreamResponse();
33: response.setBody(formatter.getResultStream(), formatter
34: .getByteCount());
35: // InputStream resultsStream = formatter.getResultStream();
36: // StreamReader reader = new StreamReader(resultsStream);
37: // response.setContent(reader.read(formatter.getByteCount()));
38:
39: return response;
40: }
41:
42: public void init(FitNesseContext context, Request request)
43: throws Exception {
44: this .context = context;
45: baseUrl = (String) request.getHeader("Host");
46: rootPath = (String) request.getResource();
47: formatter = makeFormatter(request);
48: }
49:
50: public void processResults(InputStream input) throws Exception {
51: StreamReader reader = new StreamReader(input);
52: boolean readingResults = true;
53: while (readingResults) {
54: int bytesToRead = FitProtocol.readSize(reader);
55: if (bytesToRead != 0) {
56: String resultString = reader.read(bytesToRead);
57: PageResult result = PageResult.parse(resultString);
58: formatter.acceptResult(result);
59: } else
60: readingResults = false;
61: }
62: formatter.acceptFinalCount(FitProtocol.readCounts(reader));
63: }
64:
65: public ResultFormatter makeFormatter(Request request)
66: throws Exception {
67: String format = (String) request.getInput("format");
68: if (format != null) {
69: if ("html".equals(format))
70: return new HtmlResultFormatter(context.htmlPageFactory,
71: baseUrl, rootPath);
72: if ("xml".equals(format))
73: return new XmlResultFormatter(baseUrl, rootPath);
74: }
75: return new MockResultFormatter();
76: }
77: }
|