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.fixtures;
004:
005: import fit.ColumnFixture;
006:
007: import java.util.regex.Pattern;
008: import java.util.regex.Matcher;
009: import java.util.*;
010: import fitnesse.wikitext.Utils;
011:
012: public class ResponseExaminer extends ColumnFixture {
013: public String type;
014:
015: public String pattern;
016:
017: public String value;
018:
019: public int number;
020:
021: private Matcher matcher;
022:
023: private int currentLine = 0;
024:
025: public String contents() throws Exception {
026: return Utils.escapeText(FitnesseFixtureContext.sender
027: .sentData());
028: }
029:
030: public String fullContents() throws Exception {
031: return Utils.escapeText(FitnesseFixtureContext.sender
032: .sentData());
033: }
034:
035: public boolean inOrder() throws Exception {
036: if (value == null) {
037: return false;
038: }
039: String pageContent = FitnesseFixtureContext.sender.sentData();
040: String[] lines = arrayifyLines(pageContent);
041: for (int i = currentLine; i < lines.length; i++) {
042: if (value.equals(lines[i].trim())) {
043: currentLine = i;
044: return true;
045: }
046: }
047: return false;
048: }
049:
050: public int matchCount() throws Exception {
051: Pattern p = Pattern.compile(Utils.escapeText(pattern),
052: Pattern.MULTILINE + Pattern.DOTALL);
053: value = null;
054: if (type.equals("contents"))
055: value = contents();
056: else if (type.equals("fullContents"))
057: value = fullContents();
058: else if (type.equals("status"))
059: value = "" + FitnesseFixtureContext.response.getStatus();
060: else if (type.equals("headers")) {
061: String text = FitnesseFixtureContext.sender.sentData();
062: int headerEnd = text.indexOf("\r\n\r\n");
063: value = text.substring(0, headerEnd + 2);
064: }
065:
066: matcher = p.matcher(value);
067: int matches = 0;
068: for (matches = 0; matcher.find(); matches++) {
069: // skip
070: }
071: return matches;
072: }
073:
074: public boolean matches() throws Exception {
075: return matchCount() > 0;
076: }
077:
078: public String string() throws Exception {
079: String value = null;
080: if (type.equals("contents")) {
081: return FitnesseFixtureContext.page.getData().getHtml();
082: } else if (type.equals("line")) {
083: String pageContent = FitnesseFixtureContext.page.getData()
084: .getHtml();
085: String lineizedContent = convertBreaksToLineSeparators(pageContent);
086: StringTokenizer tokenizedLines = tokenizeLines(lineizedContent);
087: for (int i = number; i != 0; i--) {
088: value = tokenizedLines.nextToken();
089: }
090: return value != null ? value.trim() : null;
091: } else {
092: throw new Exception("Bad type in ResponseExaminer");
093: }
094: }
095:
096: private StringTokenizer tokenizeLines(String lineizedContent) {
097: return new StringTokenizer(lineizedContent, System
098: .getProperty("line.separator"));
099: }
100:
101: private String[] arrayifyLines(String lineizedContent) {
102: return lineizedContent.split(System
103: .getProperty("line.separator"));
104: }
105:
106: private String convertBreaksToLineSeparators(String pageContent) {
107: String lineizedContent = pageContent.replaceAll("<br>", System
108: .getProperty("line.separator"));
109: lineizedContent = lineizedContent.replaceAll("<br/>", System
110: .getProperty("line.separator"));
111: lineizedContent = lineizedContent.replaceAll("<br />", System
112: .getProperty("line.separator"));
113: return lineizedContent;
114: }
115:
116: public String found() {
117: return matcher.group(0);
118: }
119:
120: public String source() {
121: return value;
122: }
123: }
|