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