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.html.*;
006: import fitnesse.wiki.*;
007: import fitnesse.components.*;
008: import fit.*;
009: import java.util.*;
010:
011: public class SuiteResponder extends TestResponder implements
012: FitClientListener {
013: public static final String SUITE_SETUP_NAME = "SuiteSetUp";
014:
015: public static final String SUITE_TEARDOWN_NAME = "SuiteTearDown";
016:
017: private LinkedList processingQueue = new LinkedList();
018:
019: private WikiPage currentTest = null;
020:
021: private SuiteHtmlFormatter suiteFormatter;
022:
023: protected HtmlTag addSummaryPlaceHolder() {
024: HtmlTag testSummaryDiv = new HtmlTag("div", "Running Tests ...");
025: testSummaryDiv.addAttribute("id", "test-summary");
026:
027: return testSummaryDiv;
028: }
029:
030: protected void doSending() throws Exception {
031: data = page.getData();
032: buildHtml();
033: addToResponse(formatter.head());
034:
035: List testPages = makePageList();
036:
037: String classPath = buildClassPath(testPages, page);
038: command = buildCommand(data, getClassName(data, request),
039: classPath);
040: client = new CommandRunningFitClient(this , command,
041: context.port, context.socketDealer);
042: log = new ExecutionLog(page, client.commandRunner);
043: client.start();
044: if (client.isSuccessfullyStarted()) {
045: processTestPages(testPages);
046:
047: client.done();
048: client.join();
049:
050: completeResponse();
051: }
052: }
053:
054: private void processTestPages(List testPages) throws Exception {
055: for (Iterator iterator = testPages.iterator(); iterator
056: .hasNext();) {
057: WikiPage testPage = (WikiPage) iterator.next();
058: processingQueue.addLast(testPage);
059: String testableHtml = HtmlUtil.testableHtml(testPage
060: .getData());
061: if (testableHtml.length() > 0)
062: client.send(testableHtml);
063: else
064: client.send(emptyPageContent);
065: }
066: }
067:
068: protected void close() throws Exception {
069: response.add(suiteFormatter.testOutput());
070: response.add(suiteFormatter.tail());
071: response.closeChunks();
072: response.addTrailingHeader("Exit-Code", String
073: .valueOf(exitCode()));
074: response.closeTrailer();
075: response.close();
076: }
077:
078: public void acceptOutput(String output) throws Exception {
079: WikiPage firstInLine = processingQueue.isEmpty() ? null
080: : (WikiPage) processingQueue.getFirst();
081: if (firstInLine != null && firstInLine != currentTest) {
082: PageCrawler pageCrawler = page.getPageCrawler();
083: String relativeName = pageCrawler.getRelativeName(page,
084: firstInLine);
085: WikiPagePath fullPath = pageCrawler
086: .getFullPath(firstInLine);
087: String fullPathName = PathParser.render(fullPath);
088: suiteFormatter.startOutputForNewTest(relativeName,
089: fullPathName);
090: currentTest = firstInLine;
091: }
092: suiteFormatter.acceptOutput(output);
093: }
094:
095: public void acceptResults(Counts counts) throws Exception {
096: super .acceptResults(counts);
097:
098: PageCrawler pageCrawler = page.getPageCrawler();
099: WikiPage testPage = (WikiPage) processingQueue.removeFirst();
100: String relativeName = pageCrawler.getRelativeName(page,
101: testPage);
102: addToResponse(suiteFormatter
103: .acceptResults(relativeName, counts));
104: }
105:
106: protected void makeFormatter() throws Exception {
107: suiteFormatter = new SuiteHtmlFormatter(html);
108: formatter = suiteFormatter;
109: }
110:
111: protected String pageType() {
112: return "Suite Results";
113: }
114:
115: public static String buildClassPath(List testPages, WikiPage page)
116: throws Exception {
117: final ClassPathBuilder classPathBuilder = new ClassPathBuilder();
118: final String pathSeparator = classPathBuilder
119: .getPathSeparator(page);
120: List classPathElements = new ArrayList();
121: Set visitedPages = new HashSet();
122:
123: for (Iterator iterator = testPages.iterator(); iterator
124: .hasNext();) {
125: WikiPage testPage = (WikiPage) iterator.next();
126: addClassPathElements(testPage, classPathElements,
127: visitedPages);
128: }
129: final String classPathString = classPathBuilder
130: .createClassPathString(classPathElements, pathSeparator);
131: return classPathString;
132: }
133:
134: private static void addClassPathElements(WikiPage page,
135: List classPathElements, Set visitedPages) throws Exception {
136: List pathElements = new ClassPathBuilder()
137: .getInheritedPathElements(page, visitedPages);
138: classPathElements.addAll(pathElements);
139: }
140:
141: public List makePageList() throws Exception {
142: return makePageList(page, root);
143: }
144:
145: public static List makePageList(WikiPage page, WikiPage root)
146: throws Exception {
147: LinkedList pages = getAllTestPagesUnder(page);
148: List referencedPages = gatherCrossReferencedTestPages(page,
149: root);
150: pages.addAll(referencedPages);
151:
152: WikiPage suiteSetUp = PageCrawlerImpl.getInheritedPage(
153: SUITE_SETUP_NAME, page);
154: if (suiteSetUp != null) {
155: if (pages.contains(suiteSetUp))
156: pages.remove(suiteSetUp);
157: pages.addFirst(suiteSetUp);
158: }
159: WikiPage suiteTearDown = PageCrawlerImpl.getInheritedPage(
160: SUITE_TEARDOWN_NAME, page);
161: if (suiteTearDown != null) {
162: if (pages.contains(suiteTearDown))
163: pages.remove(suiteTearDown);
164: pages.addLast(suiteTearDown);
165: }
166:
167: return pages;
168: }
169:
170: public static LinkedList getAllTestPagesUnder(WikiPage suiteRoot)
171: throws Exception {
172: LinkedList testPages = new LinkedList();
173: addTestPagesToList(testPages, suiteRoot);
174:
175: Collections.sort(testPages, new Comparator() {
176: public int compare(Object o, Object o1) {
177: try {
178: PageCrawler crawler = ((WikiPage) o)
179: .getPageCrawler();
180: WikiPagePath path1 = crawler
181: .getFullPath((WikiPage) o);
182: WikiPagePath path2 = crawler
183: .getFullPath((WikiPage) o1);
184:
185: return path1.compareTo(path2);
186: } catch (Exception e) {
187: e.printStackTrace();
188: return 0;
189: }
190: }
191: });
192:
193: return testPages;
194: }
195:
196: private static void addTestPagesToList(List testPages,
197: WikiPage context) throws Exception {
198: if (context.getData().hasAttribute(WikiPage.ACTION_TEST)) {
199: testPages.add(context);
200: }
201:
202: ArrayList children = new ArrayList();
203: children.addAll(context.getChildren());
204: if (context.hasExtension(VirtualCouplingExtension.NAME)) {
205: VirtualCouplingExtension extension = (VirtualCouplingExtension) context
206: .getExtension(VirtualCouplingExtension.NAME);
207: children.addAll(extension.getVirtualCoupling()
208: .getChildren());
209: }
210: for (Iterator iterator = children.iterator(); iterator
211: .hasNext();) {
212: WikiPage page = (WikiPage) iterator.next();
213: addTestPagesToList(testPages, page);
214: }
215: }
216:
217: public static List gatherCrossReferencedTestPages(
218: WikiPage testPage, WikiPage root) throws Exception {
219: LinkedList pages = new LinkedList();
220: PageData data = testPage.getData();
221: List pageReferences = data.getXrefPages();
222: PageCrawler crawler = testPage.getPageCrawler();
223: WikiPagePath testPagePath = crawler.getFullPath(testPage);
224: WikiPage parent = crawler.getPage(root, testPagePath
225: .parentPath());
226: for (Iterator i = pageReferences.iterator(); i.hasNext();) {
227: WikiPagePath path = PathParser.parse((String) i.next());
228: WikiPage referencedPage = crawler.getPage(parent, path);
229: if (referencedPage != null)
230: pages.add(referencedPage);
231: }
232: return pages;
233: }
234: }
|