001: package com.canoo.ant.task;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.Iterator;
006: import java.util.LinkedList;
007: import java.util.List;
008: import java.util.Properties;
009:
010: import org.apache.log4j.Logger;
011: import org.apache.tools.ant.BuildException;
012: import org.apache.tools.ant.Task;
013: import org.apache.tools.ant.TaskContainer;
014:
015: import com.canoo.ant.filter.ITableFilter;
016: import com.canoo.ant.table.APropertyTable;
017: import com.canoo.ant.table.ExcelPropertyTable;
018: import com.canoo.ant.table.IPropertyTable;
019: import com.canoo.ant.table.TableFactory;
020:
021: /**
022: *
023: * @author Dierk König
024: * @author Marc Guillemot
025: * @webtest.step
026: * category="Extension"
027: * name="dataDriven"
028: * description="This is an Ant task that calls its nested element as often as specified by the tableContainer.
029: * In each run a new set of properties from the the tableContainer is made available to the nested steps"
030: */
031: public class PropertyTableTask extends Task implements TaskContainer {
032: private static final Logger LOG = Logger
033: .getLogger(PropertyTableTask.class);
034:
035: private List fTasks = new LinkedList();
036: private Properties fProps = new Properties();
037: private String fName;
038: private String fValue;
039: private File fTableContainer;
040: private boolean replaceProperties;
041:
042: /**
043: * @webtest.parameter required="no"
044: * default="no"
045: * description="Specifies whether properties contained in the table's values
046: * should be expanded or not"
047: * @param replaceProperties
048: */
049: public void setReplaceProperties(boolean replaceProperties) {
050: this .replaceProperties = replaceProperties;
051: }
052:
053: public void setValue(final String value) {
054: fValue = value;
055: }
056:
057: public void setName(String name) {
058: fName = name;
059: }
060:
061: public void setTableclass(String tableClass) {
062: fProps.setProperty(TableFactory.KEY_TABLE_CLASS, tableClass);
063: }
064:
065: /**
066: * @webtest.parameter required="no" default="FirstEquals"
067: * description="The filter to apply on the foreing table after the lookup.
068: * One of \"All\", \"Empty\", \"FirstEquals\", \"AllEquals\" or \"Group\"."
069: */
070: public void setFilterclass(String filterClass) {
071: fProps.setProperty(TableFactory.KEY_FILTER_CLASS, filterClass);
072: }
073:
074: /**
075: * @webtest.parameter required="no"
076: * default="the first table"
077: * description="The name of the source table in the current container.
078: * For instance the name of the sheet when the table container is an Excel document."
079: */
080: public void setTable(final String table) {
081: fProps.setProperty(TableFactory.KEY_FOREIGN_TABLE, table); // unlucky naming in this context
082: }
083:
084: /**
085: * @webtest.parameter required="yes"
086: * description="The container for the table containing the data.
087: * Typically this is the path to an Excel file but this can also be a directory structure."
088: */
089: public void setTableContainer(final File container)
090: throws IOException {
091: fTableContainer = container;
092: }
093:
094: public void setContainer(final File container) throws IOException {
095: setTableContainer(container);
096: }
097:
098: /**
099: * Called by Ant to add nested tasks
100: * @see org.apache.tools.ant.TaskContainer#addTask(org.apache.tools.ant.Task)
101: * @webtest.nested.parameter required="yes"
102: * description="Any Ant task."
103: */
104: public void addTask(final Task task) {
105: fTasks.add(task);
106: }
107:
108: public void execute() throws BuildException {
109: final IPropertyTable table;
110: final ITableFilter filter;
111: try {
112: table = TableFactory.createTable(fProps,
113: ExcelPropertyTable.class.getName());
114: filter = TableFactory.createFilter(fProps);
115: } catch (final Exception e) {
116: throw new BuildException("cannot create container", e,
117: getLocation());
118: }
119:
120: TableFactory.initOrDefault(table, filter, fProps,
121: fTableContainer, fName);
122:
123: final List propertiesList = table.getPropertiesList(fValue,
124: null);
125: LOG.debug("propertiesList.size() = " + propertiesList.size());
126: if (propertiesList.isEmpty()) {
127: LOG.warn("no match found in table "
128: + table.getClass().getName() + " with filter "
129: + table.getFilter().getClass().getName()
130: + " and settings " + fProps.toString()
131: + " raw data:"
132: + ((APropertyTable) table).getRawTable());
133: }
134:
135: for (final Iterator eachPropSet = propertiesList.iterator(); eachPropSet
136: .hasNext();) {
137: final Properties propSet = (Properties) eachPropSet.next();
138: for (final Iterator eachKey = propSet.keySet().iterator(); eachKey
139: .hasNext();) {
140: final String key = (String) eachKey.next();
141: String value = propSet.getProperty(key);
142: if (replaceProperties) {
143: value = getProject().replaceProperties(value);
144: }
145: LOG.debug("setting key/value " + key + "/" + value);
146: getProject().setInheritedProperty(key, value);
147: }
148: for (final Iterator eachTask = fTasks.iterator(); eachTask
149: .hasNext();) {
150: final Task task = (Task) eachTask.next();
151: task.perform();
152: }
153: }
154: }
155: }
|