001: // Copyright © 2004-2005 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.steps.verify;
003:
004: import org.apache.log4j.Logger;
005:
006: import com.canoo.webtest.engine.StepFailedException;
007:
008: /**
009: * Step which verifies the value of a property.<p>
010: *
011: * @author Paul King
012: * @webtest.step
013: * category="Core"
014: * name="verifyProperty"
015: * description="This step verifies whether the value of a webtest dynamic property or an ant property matches an expected text value. The text value can represent a <key>regex</key>. If the text value is omitted, the step simply checks for the presence of the property."
016: */
017: public class VerifyProperty extends AbstractVerifyTextStep {
018: private static final Logger LOG = Logger
019: .getLogger(VerifyProperty.class);
020: private String fPropName;
021: private String fPropertyType;
022:
023: {
024: setOptionalText(true);
025: setOptionalPreviousPage(true);
026: }
027:
028: public String getName() {
029: return fPropName;
030: }
031:
032: /**
033: * @param text
034: * @webtest.parameter
035: * required="no"
036: * description="The expected value of the property.
037: * If omitted just checks for existence of the property."
038: */
039: public void setText(final String text) {
040: super .setText(text);
041: }
042:
043: /**
044: * @param value
045: * @webtest.parameter
046: * required="no"
047: * description="Alias for <em>text</em>."
048: */
049: public void setValue(final String value) {
050: super .setText(value);
051: }
052:
053: public String getValue() {
054: return getText();
055: }
056:
057: /**
058: * Sets the property name to verify
059: *
060: * @param name The Property name
061: * @webtest.parameter
062: * required="yes/no"
063: * description="The name of a property to test. Must be set if <em>property</em> is not set."
064: */
065: public void setName(final String name) {
066: fPropName = name;
067: }
068:
069: /**
070: * Sets the property name to verify
071: *
072: * @param name The Property name
073: * @webtest.parameter
074: * required="yes/no"
075: * description="Alias for <em>name</em>. Must be set if <em>name</em> is not set."
076: */
077: public void setProperty(final String name) {
078: setName(name);
079: }
080:
081: public String getProperty() {
082: return getName();
083: }
084:
085: public String getPropertyType() {
086: return fPropertyType;
087: }
088:
089: /**
090: * Sets the property type to verify
091: *
092: * @param type The Property type
093: * @webtest.parameter
094: * required="no"
095: * description="The type of the property in which to store the value. Either \"ant\" or \"dynamic\"."
096: * default="the \"defaultPropertyType\" as specified in the \"config\" element is used."
097: */
098: public void setPropertyType(final String type) {
099: fPropertyType = type;
100: }
101:
102: /**
103: * Does the verification work
104: *
105: * @see com.canoo.webtest.steps.Step#doExecute()
106: */
107: public void doExecute() {
108: if (!getWebtestProperties(getPropertyType()).containsKey(
109: getName())) {
110: throw new StepFailedException("Expected property \""
111: + getName() + "\" to be defined!", this );
112: }
113: final String propValue = getWebtestProperty(getName(),
114: getPropertyType());
115: LOG.debug("propName=" + getName() + ", propertyType="
116: + getPropertyType() + ", value=" + propValue
117: + ", text=" + getText());
118:
119: // null text indicates we are just checking if property is defined
120: if (getText() != null && !verifyText(propValue)) {
121: throw new StepFailedException(
122: "Incorrect property value found!", getText(),
123: propValue, this );
124: }
125: }
126:
127: /** Verifies the parameters */
128: protected void verifyParameters() {
129: super .verifyParameters();
130: nullParamCheck(getName(), "name");
131: }
132: }
|