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