001: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
002: // Released under the terms of the GNU General Public License version 2 or later.
003:
004: package fat;
005:
006: import fit.*;
007:
008: import java.net.URL;
009: import java.io.*;
010:
011: public class Tests extends Fixture {
012:
013: Parse heads;
014: String page;
015:
016: public void doRows(Parse rows) {
017: heads = rows.parts;
018: super .doRows(rows.more);
019: }
020:
021: public void doCell(Parse cell, int column) {
022: switch (column) {
023: case (0):
024: page = cell.text();
025: break;
026: default:
027: String language = heads.at(column).text();
028: String runscript = (String) Frameworks.runscripts
029: .get(language);
030: performTest(cell, runscript, page);
031: }
032: }
033:
034: public void performTest(Parse cell, String runscript, String page) {
035: if (runscript == null || runscript.equals("null")
036: || page.startsWith("?")) {
037: ignore(cell);
038: return;
039: }
040: String pageUrl = "http://fit.c2.com/wiki.cgi?" + page;
041: String testUrl = runscript + pageUrl;
042: try {
043: String testResult = get(new URL(testUrl));
044: Parse data = testResult.indexOf("<wiki>") >= 0 ? new Parse(
045: testResult, new String[] { "wiki", "td" }).parts
046: : new Parse(testResult, new String[] { "td" });
047: Counts c = count(data);
048: String message = anchor(c.right + "/" + c.wrong + "/"
049: + c.exceptions + " ", testUrl);
050: cell.addToBody(message);
051: if (c.right > 0 && c.wrong == 0 && c.exceptions == 0) {
052: right(cell);
053: } else {
054: wrong(cell);
055: cell.addToBody(data.footnote());
056: }
057: } catch (Throwable e) {
058: if (e.getMessage().indexOf("Can't find tag: td") >= 0) {
059: cell.addToBody("Can't parse <a href=\"" + testUrl
060: + "\">page</a>");
061: ignore(cell);
062: } else {
063: exception(cell, e);
064: }
065: }
066: }
067:
068: protected String anchor(String body, String link) {
069: return "<a href=\"" + link + "\">" + body + "</a>";
070: }
071:
072: protected String get(URL url) throws IOException {
073: InputStream stream = (InputStream) url.getContent();
074: BufferedReader reader = new BufferedReader(
075: new InputStreamReader(stream));
076: StringBuffer buffer = new StringBuffer(10000);
077: for (String line; (line = reader.readLine()) != null;) {
078: buffer.append(line);
079: buffer.append('\n');
080: }
081: return buffer.toString();
082: }
083:
084: protected Counts count(Parse data) throws Exception {
085: Counts counts = new Counts();
086: while (data != null) {
087: Color c = Color.parse(data);
088: data = data.more;
089: if (c == null) {
090: continue;
091: } else if (c.isGreen()) {
092: counts.right++;
093: } else if (c.isRed()) {
094: counts.wrong++;
095: } else if (c.isYellow()) {
096: counts.exceptions++;
097: } else if (c.isGray()) {
098: counts.ignores++;
099: }
100: }
101: return counts;
102: }
103:
104: public static class Color {
105:
106: int r, g, b;
107:
108: public Color(int r, int g, int b) {
109: this .r = r;
110: this .g = g;
111: this .b = b;
112: }
113:
114: public static Color parse(Parse cell) throws Exception {
115: if (cell == null) {
116: return null;
117: } else {
118: String pattern = " bgcolor=";
119: String tag = cell.tag.toLowerCase();
120: int index = tag.indexOf(pattern);
121: if (index >= 0) {
122: try {
123: index += pattern.length();
124: if (tag.charAt(index) == '"')
125: index++;
126: if (tag.charAt(index) == '#')
127: index++;
128: String hex = tag.substring(index, index + 6);
129: int rgb = Integer.parseInt(hex, 16);
130: return new Color(rgb >> 16 & 255,
131: rgb >> 8 & 255, rgb & 255);
132: } catch (Exception e) {
133: throw new Exception("Can't parse bgcolor in: "
134: + cell.tag);
135: }
136: } else {
137: return null;
138: }
139: }
140: }
141:
142: boolean isRed() {
143: return r > g && r > b;
144: }
145:
146: boolean isGreen() {
147: return g > r && g > b;
148: }
149:
150: boolean isYellow() {
151: return r > b && g > b;
152: }
153:
154: boolean isGray() {
155: return r == b && g == b;
156: }
157: }
158:
159: }
|