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.responders.run;
004:
005: import fitnesse.runner.*;
006: import fitnesse.components.FitProtocol;
007: import fitnesse.http.*;
008: import fitnesse.FitNesseContext;
009: import fitnesse.testutil.AbstractRegex;
010: import fit.Counts;
011: import java.io.*;
012:
013: public class TestResultFormattingResponderTest extends AbstractRegex {
014: private PipedOutputStream output;
015:
016: private PipedInputStream input;
017:
018: private TestResultFormattingResponder responder;
019:
020: private MockResultFormatter formatter;
021:
022: private PageResult result1;
023:
024: private PageResult result2;
025:
026: private FitNesseContext context;
027:
028: public void setUp() throws Exception {
029: output = new PipedOutputStream();
030: input = new PipedInputStream(output);
031:
032: responder = new TestResultFormattingResponder();
033: formatter = new MockResultFormatter();
034: responder.formatter = formatter;
035:
036: result1 = new PageResult("Result1Title",
037: new Counts(1, 2, 3, 4), "result1 data");
038: result2 = new PageResult("Result2Title",
039: new Counts(4, 3, 2, 1), "result2 data");
040:
041: context = new FitNesseContext();
042: }
043:
044: public void testOneResult() throws Exception {
045: FitProtocol.writeData(result1.toString(), output);
046: FitProtocol.writeCounts(new Counts(0, 0, 0, 0), output);
047: responder.processResults(input);
048:
049: assertEquals(1, formatter.results.size());
050: assertEquals(result1.toString(), formatter.results.get(0)
051: .toString());
052: }
053:
054: public void testTwoResults() throws Exception {
055: FitProtocol.writeData(result1.toString(), output);
056: FitProtocol.writeData(result2.toString(), output);
057: FitProtocol.writeCounts(new Counts(0, 0, 0, 0), output);
058: responder.processResults(input);
059:
060: assertEquals(2, formatter.results.size());
061: assertEquals(result1.toString(), formatter.results.get(0)
062: .toString());
063: assertEquals(result2.toString(), formatter.results.get(1)
064: .toString());
065: }
066:
067: public void testFinalCounts() throws Exception {
068: FitProtocol.writeData(result1.toString(), output);
069: Counts counts = new Counts(1, 2, 3, 4);
070: FitProtocol.writeCounts(counts, output);
071: responder.processResults(input);
072:
073: assertEquals(counts, formatter.finalCounts);
074: }
075:
076: public void testMakeResponse() throws Exception {
077: MockRequest request = new MockRequest();
078: ByteArrayOutputStream output = new ByteArrayOutputStream();
079: FitProtocol.writeData(result1.toString(), output);
080: FitProtocol.writeData(result2.toString(), output);
081: FitProtocol.writeCounts(new Counts(5, 5, 5, 5), output);
082: request.addInput("results", output.toString());
083:
084: Response response = responder.makeResponse(context, request);
085: String content = new MockResponseSender(response).sentData();
086:
087: assertSubString("Mock Results", content);
088: }
089:
090: public void testMockFormatter() throws Exception {
091: checkFormtterCreated(null, MockResultFormatter.class);
092: }
093:
094: public void testHtmlFormatter() throws Exception {
095: checkFormtterCreated("html", HtmlResultFormatter.class);
096: }
097:
098: public void testXmlFormatter() throws Exception {
099: checkFormtterCreated("xml", XmlResultFormatter.class);
100: }
101:
102: private void checkFormtterCreated(String format,
103: Class formatterClass) throws Exception {
104: MockRequest request = new MockRequest();
105: request.addHeader("Host", "locahost:8080");
106: request.setResource("/");
107: if (format != null)
108: request.addInput("format", format);
109: responder.init(context, request);
110: assertEquals(formatterClass, responder.formatter.getClass());
111: }
112: }
|