01: package com.canoo.webtest.steps.control;
02:
03: import java.util.Enumeration;
04: import java.util.Iterator;
05: import java.util.Map;
06:
07: import org.apache.log4j.Logger;
08: import org.apache.tools.ant.RuntimeConfigurable;
09: import org.apache.tools.ant.Task;
10: import org.apache.tools.ant.UnknownElement;
11:
12: import com.canoo.webtest.steps.AbstractStepContainer;
13:
14: /**
15: * Base class for containers requiring multiple execution of their nested steps.
16: * @author Marc Guillemot
17: */
18: public abstract class MultipleExecutionStepContainer extends
19: AbstractStepContainer {
20: private static final Logger LOG = Logger
21: .getLogger(MultipleExecutionStepContainer.class);
22:
23: /**
24: * Create a new task wrapping the tasks of this wrapper to execute them
25: * @param description the description for the wrapper task
26: * @return the task holding the subtasks
27: */
28: protected Task createIterationWrapper(final String description) {
29: final GroupStep group = new GroupStep();
30: group.setProject(getProject());
31: group.setTaskName("iteration wrapper");
32:
33: group.setLocation(getLocation());
34: group.setOwningTarget(getOwningTarget());
35: group.setDescription(description);
36:
37: final RuntimeConfigurable wrapper = new RuntimeConfigurable(
38: group, "group");
39: wrapper.setAttribute("description", description);
40:
41: // copy the children: both the UnknownElement and the associated RuntimeConfigurable
42: final Enumeration e = getRuntimeConfigurableWrapper()
43: .getChildren();
44: while (e.hasMoreElements()) {
45: final RuntimeConfigurable r = (RuntimeConfigurable) e
46: .nextElement();
47: final UnknownElement unknownElement = (UnknownElement) r
48: .getProxy();
49: final UnknownElement copy = copy(unknownElement);
50: group.addTask(copy);
51: wrapper.addChild(copy.getWrapper());
52: }
53:
54: return group;
55: }
56:
57: private UnknownElement copy(final UnknownElement ue) {
58: final UnknownElement ret = new UnknownElement(ue.getTag());
59: if (ue.getNamespace() != null)
60: ret.setNamespace(ue.getNamespace());
61: ret.setProject(getProject());
62: ret.setQName(ue.getQName());
63: ret.setTaskType(ue.getTaskType());
64: ret.setTaskName(ue.getTaskName());
65: ret.setLocation(ue.getLocation());
66: ret.setOwningTarget(getOwningTarget());
67:
68: final RuntimeConfigurable rc = new RuntimeConfigurable(ret, ue
69: .getTaskName());
70: rc.setPolyType(ue.getWrapper().getPolyType());
71:
72: final Map m = ue.getWrapper().getAttributeMap();
73: for (final Iterator i = m.entrySet().iterator(); i.hasNext();) {
74: final Map.Entry entry = (Map.Entry) i.next();
75: rc.setAttribute((String) entry.getKey(), (String) entry
76: .getValue());
77: }
78: rc.addText(ue.getWrapper().getText().toString());
79:
80: final Enumeration e = ue.getWrapper().getChildren();
81: while (e.hasMoreElements()) {
82: final RuntimeConfigurable r = (RuntimeConfigurable) e
83: .nextElement();
84: final UnknownElement unknownElement = (UnknownElement) r
85: .getProxy();
86: final UnknownElement child = copy(unknownElement);
87: rc.addChild(child.getWrapper());
88: ret.addChild(child);
89: }
90: return ret;
91: }
92: }
|