001: package fitnesse.html;
002:
003: import fitnesse.FitNesseContext;
004: import fitnesse.Responder;
005: import fitnesse.http.MockRequest;
006: import fitnesse.http.SimpleResponse;
007: import fitnesse.responders.WikiPageResponder;
008: import fitnesse.runner.SuiteExporter;
009: import fitnesse.util.FileUtil;
010: import fitnesse.wiki.PageCrawler;
011: import fitnesse.wiki.PathParser;
012: import fitnesse.wiki.WikiPagePath;
013:
014: public class StiqSuite {
015:
016: private String suiteHtml;
017: private WikiPagePath wikiPagePath;
018: private FitNesseContext context;
019: private String LINK_BEGIN = "<a href=\"";
020: private String LINK_END = "\">";
021:
022: public StiqSuite(FitNesseContext context, WikiPagePath wikiPagePath)
023: throws Exception {
024: this .wikiPagePath = wikiPagePath;
025: this .context = context;
026: MockRequest request = new MockRequest();
027: validateWikiPagePath(wikiPagePath);
028: request.setResource(PathParser.render(wikiPagePath));
029: Responder responder = new WikiPageResponder();
030: SimpleResponse response = (SimpleResponse) responder
031: .makeResponse(this .context, request);
032: suiteHtml = response.getContent();
033: prepareHtmlForExport();
034: }
035:
036: private void validateWikiPagePath(WikiPagePath wikiPagePath)
037: throws Exception {
038:
039: PageCrawler crawler = this .context.root.getPageCrawler();
040: boolean exists = crawler.pageExists(this .context.root,
041: wikiPagePath);
042: if (exists == false || wikiPagePath.isEmpty()) {
043: String message = "Unable to locate Wiki Page ["
044: + PathParser.render(wikiPagePath)
045: + "] in repository.";
046: throw new IllegalArgumentException(message);
047: }
048: }
049:
050: public String[] getTestLinks() {
051: String[] testLinks = {};
052: String anchorTag = "<a href=\"";
053: String interestingPart = suiteHtml.substring(suiteHtml
054: .indexOf("id=\"mainDiv\""));
055: if (interestingPart.indexOf(anchorTag) > 0) {
056: interestingPart = interestingPart.substring(interestingPart
057: .indexOf(anchorTag)
058: + anchorTag.length());
059: testLinks = interestingPart.split(anchorTag);
060:
061: for (int i = 0; i < testLinks.length; i++) {
062: testLinks[i] = testLinks[i].substring(0, testLinks[i]
063: .indexOf("\""));
064: }
065: }
066: return testLinks;
067: }
068:
069: public StiqTest[] getTests() throws Exception {
070: StiqTest stiqTest = null;
071: WikiPagePath testWikiPagePath = null;
072: String[] testLinks = getTestLinks();
073: StiqTest[] stiqTests = new StiqTest[testLinks.length];
074: for (int i = 0; i < testLinks.length; i++) {
075: testWikiPagePath = PathParser.parse(testLinks[i]);
076: stiqTest = new StiqTest(this .context, testWikiPagePath);
077: stiqTests[i] = stiqTest;
078: }
079: return stiqTests;
080: }
081:
082: public void writeTests(String outputDir) throws Exception {
083: StiqTest[] tests = getTests();
084: for (int i = 0; i < tests.length; i++) {
085: tests[i].writeHtml(outputDir);
086: }
087: }
088:
089: public void writeSuite(String outputDir) throws Exception {
090:
091: String outputFilePath = outputDir + "/"
092: + PathParser.render(getWikiPagePath())
093: + SuiteExporter.FILE_EXTENSION;
094: String localizedLinksHtml = localizeTestLinks();
095: FileUtil.createFile2(outputFilePath, localizedLinksHtml);
096:
097: }
098:
099: public void writeSuiteAndTests(String outputDir) throws Exception {
100: writeSuite(outputDir);
101: writeTests(outputDir);
102: }
103:
104: public String localizeTestLinks() {
105: String[] testLinks = getTestLinks();
106: String linksHtml = suiteHtml;
107:
108: for (int i = 0; i < testLinks.length; i++) {
109: linksHtml = linksHtml.replaceAll(LINK_BEGIN + testLinks[i]
110: + LINK_END, LINK_BEGIN + testLinks[i]
111: + SuiteExporter.FILE_EXTENSION + LINK_END);
112: }
113: return linksHtml;
114: }
115:
116: public void cleanHtml() {
117: String tempHtml = suiteHtml;
118: String docTypeRegex = "<!DOCTYPE.*\r\n";
119: String linkRegex = "<link rel=.*\r\n";
120: String scriptRegex = "<script src=.*\r\n";
121: String actionsRegex = "<div class=\"actions\"[\\s|\\S]*class=\"toolbarImage\"/></a></div>";
122:
123: tempHtml = tempHtml.replaceAll(docTypeRegex, "");
124: tempHtml = tempHtml.replaceAll(linkRegex, "");
125: tempHtml = tempHtml.replaceAll(scriptRegex, "");
126: tempHtml = tempHtml.replaceAll(actionsRegex, "");
127:
128: suiteHtml = tempHtml;
129: }
130:
131: public void prepareHtmlForExport() {
132: localizeTestLinks();
133: cleanHtml();
134: }
135:
136: public WikiPagePath getWikiPagePath() {
137: return this.wikiPagePath;
138: }
139: }
|