001: // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.store;
003:
004: import org.apache.commons.lang.StringUtils;
005:
006: import com.canoo.webtest.engine.StepFailedException;
007: import com.canoo.webtest.util.ConversionUtil;
008: import com.canoo.webtest.util.Evaluator;
009: import com.canoo.webtest.util.Expression;
010:
011: /**
012: * Step which stores a value into a property.<p>
013: *
014: * @author Paul King
015: * @webtest.step
016: * category="Core"
017: * name="storeProperty"
018: * alias="storeDynamicProperty"
019: * description="Provides the ability to store a given value into a property. It supports both dynamic properties and traditional ant properties."
020: */
021: public class StorePropertyStep extends BaseStoreStep implements
022: Evaluator {
023: private String fPropertyValue;
024: private String fEval;
025: private String fName;
026:
027: public void doExecute() {
028: final String value = isEval() ? doEvaluate(getValue())
029: : getValue();
030: storeProperty(value, getName());
031: }
032:
033: String doEvaluate(final String propertyValue) {
034: final double d = Expression.evaluateExpression(propertyValue,
035: this );
036: if (d == (int) d) {
037: return Integer.toString((int) d);
038: }
039: return Double.toString(d);
040: }
041:
042: public String getName() {
043: return fName;
044: }
045:
046: /**
047: * Sets the property name
048: *
049: * @param name
050: * @webtest.parameter
051: * required="yes/no"
052: * description="The property name. Must be set if <em>property</em> is not set."
053: */
054: public void setName(final String name) {
055: fName = name;
056: }
057:
058: public String getValue() {
059: return fPropertyValue;
060: }
061:
062: /**
063: * Sets the property value
064: *
065: * @param value
066: * @webtest.parameter
067: * required="yes"
068: * description="The property value."
069: */
070: public void setValue(final String value) {
071: fPropertyValue = value;
072: }
073:
074: public boolean isEval() {
075: return ConversionUtil.convertToBoolean(getEval(), false);
076: }
077:
078: public String getEval() {
079: return fEval;
080: }
081:
082: /**
083: * Sets the eval flag
084: *
085: * @param eval
086: * @webtest.parameter
087: * required="no"
088: * default="false"
089: * description="A flag which determines if the value is to be treated as a mathematical expression to be evaluated."
090: */
091: public void setEval(final String eval) {
092: fEval = eval;
093: }
094:
095: protected void verifyParameters() {
096: super .verifyParameters();
097: nullParamCheck(StringUtils.defaultString(getProperty(), fName),
098: "name");
099: nullParamCheck(getValue(), "value");
100: }
101:
102: public double evaluate(final String s) {
103: final String result = getProject().replaceProperties(s);
104: try {
105: return Double.parseDouble(result);
106: } catch (final NumberFormatException e) {
107: throw new StepFailedException(
108: "Attempted to evaluate non-numeric property '" + s
109: + "': " + e.getMessage(), this );
110: }
111: }
112:
113: /**
114: * The property value if inlined.
115: *
116: * @param text The new value
117: * @webtest.nested.parameter
118: * required="no"
119: * description="An alternative to the attribute value."
120: */
121: public void addText(final String text) {
122: fPropertyValue = text;
123: }
124: }
|