001: package com.mockrunner.base;
002:
003: import java.io.BufferedReader;
004: import java.io.StringReader;
005:
006: import com.mockrunner.mock.web.WebMockObjectFactory;
007: import com.mockrunner.util.common.StringUtil;
008: import com.mockrunner.util.web.XmlUtil;
009:
010: /**
011: * Base class for modules which produce HTML
012: * code as output. These modules have to implement
013: * {@link #getOutput}. The HTML code is provided
014: * in different formats, e.g. as parsed XML
015: * documents.
016: */
017: public abstract class HTMLOutputModule extends WebTestModule {
018: private boolean caseSensitive;
019:
020: public HTMLOutputModule(WebMockObjectFactory mockFactory) {
021: super (mockFactory);
022: caseSensitive = true;
023: }
024:
025: /**
026: * Returns the output as a string. Concrete modules implement
027: * this method.
028: * @return the output
029: */
030: public abstract String getOutput();
031:
032: /**
033: * Returns the output as a <code>BufferedReader</code>.
034: * @return the output
035: */
036: public BufferedReader getOutputAsBufferedReader() {
037: return new BufferedReader(new StringReader(getOutput()));
038: }
039:
040: /**
041: * Parses the output with the NekoHTML parser and returns it
042: * as a W3C XML document. Throws a {@link NestedApplicationException}
043: * if the output is not parsable. NekoHTML is quite good in fixing sloppy
044: * HTML code. If you want to use a different parser configuration,
045: * you can use the method {@link com.mockrunner.util.web.XmlUtil#parse}
046: * to parse the string output yourself. Please note that
047: * HTML parsing is not very fast and may slow down
048: * your test suite.
049: * @return the output as <code>org.w3c.dom.Document</code>
050: * @throws RuntimeException if a parsing error occurs
051: */
052: public org.w3c.dom.Document getOutputAsW3CDocument() {
053: return XmlUtil.parseHTML(getOutput());
054: }
055:
056: /**
057: * Parses the output with the NekoHTML parser and returns it
058: * as a JDOM XML document. Throws a {@link NestedApplicationException}
059: * if the output is not parsable. NekoHTML is quite good in fixing sloppy
060: * HTML code. If you want to use a different parser configuration,
061: * you can use the method {@link com.mockrunner.util.web.XmlUtil#parse}
062: * to parse the string output yourself. Please note that
063: * HTML parsing is not very fast and may slow down
064: * your test suite.
065: * @return the output as <code>org.jdom.Document</code>
066: * @throws RuntimeException if a parsing error occurs
067: */
068: public org.jdom.Document getOutputAsJDOMDocument() {
069: return XmlUtil.createJDOMDocument(getOutputAsW3CDocument());
070: }
071:
072: /**
073: * Parses the output with the NekoHTML parser and returns it
074: * as fixed, wellformed XML. Throws a {@link NestedApplicationException}
075: * if the output is not parsable. NekoHTML is quite good in fixing sloppy
076: * HTML code. If you want to use a different parser configuration,
077: * you can use the method {@link com.mockrunner.util.web.XmlUtil#parse}
078: * to parse the string output yourself. Please note that
079: * HTML parsing is not very fast and may slow down
080: * your test suite.
081: * @return the output as wellformed XML
082: * @throws RuntimeException if a parsing error occurs
083: */
084: public String getOutputAsWellformedXML() {
085: return XmlUtil
086: .createStringFromJDOMDocument(getOutputAsJDOMDocument());
087: }
088:
089: /**
090: * Set if {@link #verifyOutput}, {@link #verifyOutputContains}
091: * and {@link #verifyOutputRegularExpression}.
092: * should compare case sensitive. Default is <code>true</code>.
093: * @param caseSensitive enable or disable case sensitivity
094: */
095: public void setCaseSensitive(boolean caseSensitive) {
096: this .caseSensitive = caseSensitive;
097: }
098:
099: /**
100: * Verifies the tag output.
101: * @param expectedOutput the expected output.
102: * @throws VerifyFailedException if verification fails
103: */
104: public void verifyOutput(String expectedOutput) {
105: String actualOutput = getOutput();
106: if (!StringUtil.matchesExact(actualOutput, expectedOutput,
107: caseSensitive)) {
108: throw new VerifyFailedException("actual output: "
109: + actualOutput + " does not match expected output");
110: }
111: }
112:
113: /**
114: * Verifies if the output contains the specified data.
115: * @param expectedOutput the data
116: * @throws VerifyFailedException if verification fails
117: */
118: public void verifyOutputContains(String expectedOutput) {
119: String actualOutput = getOutput();
120: if (!StringUtil.matchesContains(actualOutput, expectedOutput,
121: caseSensitive)) {
122: throw new VerifyFailedException("actual output: "
123: + actualOutput + " does not match expected output");
124: }
125: }
126:
127: /**
128: * Verifies if the output matches the specified regular expression.
129: * @param expression the data
130: * @throws VerifyFailedException if verification fails
131: */
132: public void verifyOutputRegularExpression(String expression) {
133: String actualOutput = getOutput();
134: if (!StringUtil.matchesPerl5(actualOutput, expression,
135: caseSensitive)) {
136: throw new VerifyFailedException("actual output: "
137: + actualOutput + " does not match expected output");
138: }
139: }
140: }
|