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.util.FileUtil;
007: import fitnesse.testutil.*;
008: import fitnesse.http.Request;
009: import fitnesse.wiki.InMemoryPage;
010: import fit.Counts;
011:
012: public class FormattingOptionTest extends AbstractRegex {
013: private ByteArrayOutputStream output;
014:
015: private FormattingOption option;
016:
017: private CachingResultFormatter formatter;
018:
019: private PageResult result1;
020:
021: private PageResult result2;
022:
023: private Counts finalCounts;
024:
025: private int port = FitNesseUtil.port;
026:
027: public void setUp() throws Exception {
028: output = new ByteArrayOutputStream();
029: }
030:
031: public void tearDown() throws Exception {
032: new File("testOutput.txt").delete();
033: }
034:
035: public void testConstruction() throws Exception {
036: option = new FormattingOption("mock", "stdout", output,
037: "localhost", 8081, "SomePage");
038: assertEquals("mock", option.format);
039: assertSame(output, option.output);
040: assertEquals("localhost", option.host);
041: assertEquals(8081, option.port);
042: assertEquals("SomePage", option.rootPath);
043: }
044:
045: public void testConstructionWithFile() throws Exception {
046: option = new FormattingOption("mock", "testOutput.txt", output,
047: "localhost", 8081, "SomePage");
048: assertEquals(FileOutputStream.class, option.output.getClass());
049: option.output.write("sample data".getBytes());
050: option.output.close();
051: assertEquals("sample data", FileUtil
052: .getFileContent("testOutput.txt"));
053: }
054:
055: public void testRawResults() throws Exception {
056: sampleFormatter();
057: option = new FormattingOption("raw", "stdout", output,
058: "localhost", port, "SomePage");
059: option.process(formatter.getResultStream(), formatter
060: .getByteCount());
061: String content = output.toString();
062: assertSubString(result1.toString(), content);
063: assertSubString(result2.toString(), content);
064: }
065:
066: public void testRequest() throws Exception {
067: option = new FormattingOption("mock", "stdout", output,
068: "localhost", 8081, "SomePage");
069: String requestString = option
070: .buildRequest(
071: new ByteArrayInputStream("test results"
072: .getBytes()), 12).getText();
073:
074: Request request = new Request(new ByteArrayInputStream(
075: requestString.getBytes()));
076: request.parse();
077: assertEquals("POST /SomePage HTTP/1.1", request
078: .getRequestLine());
079: assertTrue(request.getHeader("Content-Type").toString()
080: .startsWith("multipart"));
081: assertEquals("localhost:8081", request.getHeader("Host"));
082: assertEquals("format", request.getInput("responder"));
083: assertEquals("mock", request.getInput("format"));
084: assertEquals("test results", request.getInput("results"));
085: }
086:
087: public void testTheWholeDeal() throws Exception {
088: sampleFormatter();
089:
090: FitNesseUtil.startFitnesse(InMemoryPage.makeRoot("RooT"));
091: try {
092: option = new FormattingOption("mock", "stdout", output,
093: "localhost", port, "");
094: option.process(formatter.getResultStream(), formatter
095: .getByteCount());
096: } finally {
097: FitNesseUtil.stopFitnesse();
098: }
099:
100: String result = output.toString();
101: assertSubString("Mock Results", result);
102: assertSubString(result1.toString(), result);
103: assertSubString(result2.toString(), result);
104: }
105:
106: private void sampleFormatter() throws Exception {
107: formatter = new CachingResultFormatter();
108: result1 = new PageResult("ResultOne", new Counts(1, 2, 3, 4),
109: "result one content");
110: result2 = new PageResult("ResultTwo", new Counts(4, 3, 2, 1),
111: "result two content");
112: finalCounts = new Counts(5, 5, 5, 5);
113: formatter.acceptResult(result1);
114: formatter.acceptResult(result2);
115: formatter.acceptFinalCount(finalCounts);
116: }
117: }
|