001: package org.depunit;
002:
003: import javax.xml.xpath.*;
004: import org.w3c.dom.*;
005: import java.util.*;
006:
007: /**
008: This class represents all the data for one test run
009: */
010: public class TestRun {
011: public static class ClassConfig {
012: private String m_name;
013: private Map<String, String> m_params;
014: private DataDriver m_dataDriver;
015:
016: public ClassConfig(String name) {
017: m_name = name;
018: m_params = new HashMap<String, String>();
019: m_dataDriver = null;
020: }
021:
022: public ClassConfig(Element e) throws XMLException,
023: InitializationException {
024: m_params = null;
025: m_name = e.getAttribute("name");
026: if (m_name == null)
027: throw new XMLException(
028: "Missing name attribute on class definition");
029:
030: NodeList nl = e.getElementsByTagName("driver");
031: if (nl.getLength() != 0) {
032: Element driver = (Element) nl.item(0);
033: if (driver.hasAttribute("class")) //Custom driver
034: {
035: try {
036: Class driverClass = Class.forName(driver
037: .getAttribute("class"));
038: m_dataDriver = (DataDriver) driverClass
039: .newInstance();
040: } catch (ClassNotFoundException cnfe) {
041: throw new XMLException("Unable to find class "
042: + driver.getAttribute("class"));
043: } catch (InstantiationException ie) {
044: throw new XMLException(
045: "Unable to instantiate class "
046: + driver.getAttribute("class"));
047: } catch (IllegalAccessException iae) {
048: throw new XMLException(iae.toString());
049: }
050:
051: //Initialize data driver with params
052: Map<String, String> values = new HashMap<String, String>();
053:
054: NodeList subnl = driver
055: .getElementsByTagName("value");
056: for (int J = 0; J < subnl.getLength(); J++) {
057: Element value = (Element) subnl.item(J);
058: values.put(value.getAttribute("name"), value
059: .getFirstChild().getNodeValue());
060: }
061:
062: BeanUtil.initializeClass(m_dataDriver.getClass(),
063: values, m_dataDriver);
064: } else
065: m_dataDriver = new XMLDataDriver(driver);
066: } else {
067: nl = e.getElementsByTagName("value");
068: if (nl.getLength() != 0)
069: m_params = new HashMap<String, String>();
070:
071: for (int I = 0; I < nl.getLength(); I++) {
072: Element val = (Element) nl.item(I);
073: m_params.put(val.getAttribute("name"), val
074: .getFirstChild().getNodeValue());
075: }
076: }
077: }
078:
079: public String getName() {
080: return (m_name);
081: }
082:
083: public Map<String, String> getParams() {
084: return (m_params);
085: }
086:
087: public DataDriver getDataDriver() {
088: return (m_dataDriver);
089: }
090: }
091:
092: public static class MethodConfig {
093: private String m_name;
094:
095: public MethodConfig(String name) {
096: m_name = name;
097: }
098:
099: public MethodConfig(Element e) {
100: }
101:
102: public String getName() {
103: return (m_name);
104: }
105: }
106:
107: private Map<String, ClassConfig> m_classes;
108: private List<MethodConfig> m_methods;
109: private XPath m_xpath;
110:
111: public TestRun(List<String> classes, List<String> methods) {
112: m_classes = new HashMap<String, ClassConfig>();
113: m_methods = new ArrayList<MethodConfig>();
114:
115: for (String cl : classes)
116: m_classes.put(cl, new ClassConfig(cl));
117:
118: for (String m : methods)
119: m_methods.add(new MethodConfig(m));
120: }
121:
122: private Element getElement(Element parent, String name) {
123: NodeList nl = parent.getElementsByTagName(name);
124: if (nl.getLength() == 0)
125: return (null);
126: else
127: return ((Element) nl.item(0));
128: }
129:
130: public TestRun(Element e, Map<String, List<ClassConfig>> classGroups)
131: throws XMLException, InitializationException {
132: m_classes = new HashMap<String, ClassConfig>();
133: m_methods = new ArrayList<MethodConfig>();
134:
135: Element classes = getElement(e, "classes");
136: if (classes == null)
137: throw new XMLException("Missing classes tag");
138:
139: NodeList nl = classes.getElementsByTagName("includeGroup");
140: for (int I = 0; I < nl.getLength(); I++) {
141: String groupName = nl.item(I).getFirstChild()
142: .getNodeValue();
143: List<ClassConfig> groupList = classGroups.get(groupName);
144: for (ClassConfig cc : groupList)
145: m_classes.put(cc.getName(), cc);
146: }
147:
148: nl = classes.getElementsByTagName("class");
149: for (int I = 0; I < nl.getLength(); I++) {
150: ClassConfig cc = new ClassConfig((Element) nl.item(I));
151: m_classes.put(cc.getName(), cc);
152: }
153:
154: Element tests = getElement(e, "tests");
155: if (tests != null) {
156: nl = tests.getElementsByTagName("method");
157: for (int I = 0; I < nl.getLength(); I++)
158: m_methods.add(new MethodConfig(nl.item(I)
159: .getFirstChild().getNodeValue()));
160: }
161: }
162:
163: public Collection<ClassConfig> getClasses() {
164: return (m_classes.values());
165: }
166:
167: public List<MethodConfig> getMethods() {
168: return (m_methods);
169: }
170:
171: }
|