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.http.*;
006: import fitnesse.wiki.*;
007: import fitnesse.*;
008: import fitnesse.authentication.*;
009: import fitnesse.responders.SecureResponder;
010: import fitnesse.testutil.*;
011: import java.util.regex.*;
012:
013: public class TestResponderTest extends AbstractRegex {
014: private WikiPage root;
015:
016: private MockRequest request;
017:
018: private TestResponder responder;
019:
020: private FitNesseContext context;
021:
022: private int port = 9123;
023:
024: private Response response;
025:
026: private MockResponseSender sender;
027:
028: private WikiPage testPage;
029:
030: private String results;
031:
032: private FitSocketReceiver receiver;
033:
034: private WikiPage errorLogsParentPage;
035:
036: private PageCrawler crawler;
037:
038: private String simpleRunPageName;
039:
040: public void setUp() throws Exception {
041: root = InMemoryPage.makeRoot("RooT");
042: crawler = root.getPageCrawler();
043: errorLogsParentPage = crawler.addPage(root, PathParser
044: .parse("ErrorLogs"));
045: request = new MockRequest();
046: responder = new TestResponder();
047: context = new FitNesseContext(root);
048: context.port = port;
049:
050: receiver = new FitSocketReceiver(port, context.socketDealer);
051: receiver.receiveSocket();
052: }
053:
054: public void tearDown() throws Exception {
055: receiver.close();
056: receiver = null;
057: super .tearDown();
058: }
059:
060: public void testSimpleRun() throws Exception {
061: doSimpleRun(passFixtureTable());
062:
063: assertSubString(testPage.getName(), results);
064: assertSubString("Test Results", results);
065: assertSubString("class", results);
066: assertNotSubString("ClassNotFoundException", results);
067: }
068:
069: private void doSimpleRun(String fixtureTable) throws Exception {
070: simpleRunPageName = "TestPage";
071: testPage = crawler.addPage(root, PathParser
072: .parse(simpleRunPageName), classpathWidgets()
073: + fixtureTable);
074: request.setResource(testPage.getName());
075:
076: response = responder.makeResponse(context, request);
077: sender = new MockResponseSender(response);
078:
079: results = sender.sentData();
080: }
081:
082: // FIXME
083: public void FIXME_testEmptyTestPage() throws Exception {
084: PageData data = root.getData();
085: data.setContent(classpathWidgets());
086: root.commit(data);
087: testPage = crawler.addPage(root, PathParser
088: .parse("EmptyTestPage"));
089: request.setResource(testPage.getName());
090:
091: response = responder.makeResponse(context, request);
092: sender = new MockResponseSender(response);
093: sender.sentData();
094:
095: WikiPagePath errorLogPath = PathParser
096: .parse("ErrorLogs.EmptyTestPage");
097: WikiPage errorLogPage = crawler.getPage(root, errorLogPath);
098: String errorLogContent = errorLogPage.getData().getContent();
099: assertNotSubString("Exception", errorLogContent);
100: }
101:
102: // FIXME
103: public void FIXME_testFitSocketGetsClosed() throws Exception {
104: doSimpleRun(passFixtureTable());
105: assertTrue(receiver.socket.isClosed());
106: }
107:
108: // FIXME
109: public void FIXME_testStandardOutput() throws Exception {
110: String content = classpathWidgets()
111: + outputWritingTable("output1")
112: + outputWritingTable("output2")
113: + outputWritingTable("output3");
114:
115: String errorLogContent = doRunAndGetErrorLog(content);
116:
117: assertHasRegexp("output1", errorLogContent);
118: assertHasRegexp("output2", errorLogContent);
119: assertHasRegexp("output3", errorLogContent);
120: }
121:
122: // FIXME
123: public void FIXME_testErrorOutput() throws Exception {
124: String content = classpathWidgets()
125: + errorWritingTable("error1")
126: + errorWritingTable("error2")
127: + errorWritingTable("error3");
128:
129: String errorLogContent = doRunAndGetErrorLog(content);
130:
131: assertHasRegexp("error1", errorLogContent);
132: assertHasRegexp("error2", errorLogContent);
133: assertHasRegexp("error3", errorLogContent);
134: }
135:
136: private String doRunAndGetErrorLog(String content) throws Exception {
137: WikiPage testPage = crawler.addPage(root, PathParser
138: .parse("TestPage"), content);
139: request.setResource(testPage.getName());
140:
141: Response response = responder.makeResponse(context, request);
142: MockResponseSender sender = new MockResponseSender(response);
143: String results = sender.sentData();
144:
145: assertHasRegexp("ErrorLog", results);
146:
147: WikiPage errorLog = errorLogsParentPage.getChildPage(testPage
148: .getName());
149: String errorLogContent = errorLog.getData().getContent();
150: return errorLogContent;
151: }
152:
153: // FIXME
154: public void FIXME_testHasExitValueHeader() throws Exception {
155: WikiPage testPage = crawler.addPage(root, PathParser
156: .parse("TestPage"), classpathWidgets()
157: + passFixtureTable());
158: request.setResource(testPage.getName());
159:
160: Response response = responder.makeResponse(context, request);
161: MockResponseSender sender = new MockResponseSender(response);
162: String results = sender.sentData();
163:
164: assertSubString("Exit-Code: 0", results);
165: }
166:
167: public void testFixtureThatCrashes() throws Exception {
168: WikiPage testPage = crawler.addPage(root, PathParser
169: .parse("TestPage"), classpathWidgets()
170: + crashFixtureTable());
171: request.setResource(testPage.getName());
172:
173: Response response = responder.makeResponse(context, request);
174: MockResponseSender sender = new MockResponseSender(response);
175:
176: String results = sender.sentData();
177: assertSubString("ErrorLog", results);
178: }
179:
180: public void testResultsIncludeActions() throws Exception {
181: doSimpleRun(passFixtureTable());
182: assertSubString("<div class=\"actions\" id=\"actions\">",
183: results);
184: }
185:
186: public void testResultsHaveHeaderAndFooter() throws Exception {
187: crawler.addPage(root, PathParser.parse("PageHeader"), "HEADER");
188: crawler.addPage(root, PathParser.parse("PageFooter"), "FOOTER");
189: doSimpleRun(passFixtureTable());
190: assertSubString("HEADER", results);
191: assertSubString("FOOTER", results);
192: }
193:
194: public void testExecutionStatusAppears() throws Exception {
195: doSimpleRun(passFixtureTable());
196: assertHasRegexp("<div id=\"execution-status\">.*?</div>",
197: results);
198: }
199:
200: private String getExecutionStatusMessage() throws Exception {
201: Pattern pattern = Pattern
202: .compile(
203: "<div id=\"execution-status\">.*?<a href=\"ErrorLogs\\.[^\"]*\">([^<>]*?)</a>.*?</div>",
204: Pattern.DOTALL);
205: Matcher matcher = pattern.matcher(results);
206: matcher.find();
207: return matcher.group(1);
208: }
209:
210: private String getExecutionStatusIconFilename() {
211: Pattern pattern = Pattern
212: .compile(
213: "<div id=\"execution-status\">.*?<img.*?src=\"(?:[^/]*/)*([^/]*\\.gif)\".*?/>.*?</div>",
214: Pattern.DOTALL);
215: Matcher matcher = pattern.matcher(results);
216: matcher.find();
217: return matcher.group(1);
218: }
219:
220: // FIXME
221: public void FIXME_testExecutionStatusOk() throws Exception {
222: doSimpleRun(passFixtureTable());
223: assertEquals("Tests Executed OK", getExecutionStatusMessage());
224: assertEquals("ok.gif", getExecutionStatusIconFilename());
225: }
226:
227: // FIXME
228: public void FIXME_testExecutionStatusOutputCaptured()
229: throws Exception {
230: doSimpleRun(outputWritingTable("blah"));
231: assertEquals("Output Captured", getExecutionStatusMessage());
232: assertEquals("output.gif", getExecutionStatusIconFilename());
233: }
234:
235: // FIXME
236: public void testExecutionStatusError() throws Exception {
237: doSimpleRun(crashFixtureTable());
238: assertEquals("Errors Occurred", getExecutionStatusMessage());
239: assertEquals("error.gif", getExecutionStatusIconFilename());
240: }
241:
242: // FIXME
243: public void FIXME_testExecutionStatusErrorHasPriority()
244: throws Exception {
245: doSimpleRun(errorWritingTable("blah") + crashFixtureTable());
246: assertEquals("Errors Occurred", getExecutionStatusMessage());
247: }
248:
249: public void testTestSummaryAppears() throws Exception {
250: doSimpleRun(passFixtureTable());
251: assertHasRegexp(divWithIdAndContent("test-summary", ".*?"),
252: results);
253: }
254:
255: public void testTestSummaryInformationAppears() throws Exception {
256: doSimpleRun(passFixtureTable());
257: assertHasRegexp(
258: "<script>.*?document\\.getElementById\\(\"test-summary\"\\)\\.innerHTML = \".*?Assertions:.*?\";.*?</script>",
259: results);
260: assertHasRegexp(
261: "<script>.*?document\\.getElementById\\(\"test-summary\"\\)\\.className = \".*?\";.*?</script>",
262: results);
263: }
264:
265: // FIXME
266: public void FIXME_testTestSummaryHasRightClass() throws Exception {
267: doSimpleRun(passFixtureTable());
268: assertHasRegexp(
269: "<script>.*?document\\.getElementById\\(\"test-summary\"\\)\\.className = \"pass\";.*?</script>",
270: results);
271: }
272:
273: public void testAuthentication_RequiresTestPermission()
274: throws Exception {
275: assertTrue(responder instanceof SecureResponder);
276: SecureOperation operation = ((SecureResponder) responder)
277: .getSecureOperation();
278: assertEquals(SecureTestOperation.class, operation.getClass());
279: }
280:
281: private String errorWritingTable(String message) {
282: return "\n|!-fitnesse.testutil.ErrorWritingFixture-!|\n|"
283: + message + "|\n\n";
284:
285: }
286:
287: private String outputWritingTable(String message) {
288: return "\n|!-fitnesse.testutil.OutputWritingFixture-!|\n|"
289: + message + "|\n\n";
290: }
291:
292: private String classpathWidgets() {
293: return "!path classes\n";
294: }
295:
296: private String crashFixtureTable() {
297: return "|!-fitnesse.testutil.CrashFixture-!|\n";
298: }
299:
300: private String passFixtureTable() {
301: return "|!-fitnesse.testutil.PassFixture-!|\n";
302: }
303: }
|