001: package org.testng.xml;
002:
003: import java.io.Serializable;
004: import java.util.ArrayList;
005: import java.util.List;
006: import java.util.Properties;
007:
008: import org.testng.TestNGException;
009: import org.testng.internal.ClassHelper;
010: import org.testng.reporters.XMLStringBuffer;
011:
012: /**
013: * This class describes the tag <class> in testng.xml.
014: *
015: * @author <a href="mailto:cedric@beust.com">Cedric Beust</a>
016: */
017: public class XmlClass implements Serializable, Cloneable {
018: private List<String> m_includedMethods = new ArrayList<String>();
019: private List<String> m_excludedMethods = new ArrayList<String>();
020: private String m_name = null;
021: private Class m_class = null;
022:
023: public XmlClass(String name) {
024: init(name, null);
025: }
026:
027: public XmlClass(Class className) {
028: init(className.getName(), className);
029: }
030:
031: private void init(String name, Class className) {
032: m_name = name;
033: m_class = className;
034: }
035:
036: /**
037: * @return Returns the className.
038: */
039: public Class getSupportClass() {
040: if (null == m_class) {
041: m_class = ClassHelper.forName(m_name);
042:
043: if (null == m_class) {
044: throw new TestNGException(
045: "Cannot find class in classpath: " + m_name);
046: }
047: }
048:
049: return m_class;
050: }
051:
052: /**
053: * @param className The className to set.
054: */
055: public void setClass(Class className) {
056: m_class = className;
057: }
058:
059: /**
060: * @return Returns the excludedMethods.
061: */
062: public List<String> getExcludedMethods() {
063: return m_excludedMethods;
064: }
065:
066: /**
067: * @param excludedMethods The excludedMethods to set.
068: */
069: public void setExcludedMethods(List<String> excludedMethods) {
070: m_excludedMethods = excludedMethods;
071: }
072:
073: /**
074: * @return Returns the includedMethods.
075: */
076: public List<String> getIncludedMethods() {
077: return m_includedMethods;
078: }
079:
080: /**
081: * @param includedMethods The includedMethods to set.
082: */
083: public void setIncludedMethods(List<String> includedMethods) {
084: m_includedMethods = includedMethods;
085: }
086:
087: /**
088: * @return Returns the name.
089: */
090: public String getName() {
091: return m_name;
092: }
093:
094: /**
095: * @param name The name to set.
096: */
097: public void setName(String name) {
098: m_name = name;
099: }
100:
101: @Override
102: public String toString() {
103: return "[Class: " + m_name + "]";
104: }
105:
106: public String toXml(String indent) {
107: XMLStringBuffer xsb = new XMLStringBuffer(indent);
108: Properties pro = new Properties();
109: pro.setProperty("name", getName());
110:
111: if (!m_includedMethods.isEmpty()
112: || !m_excludedMethods.isEmpty()) {
113: xsb.push("class", pro);
114: xsb.push("methods");
115:
116: for (String m : getIncludedMethods()) {
117: Properties p = new Properties();
118: p.setProperty("name", m);
119: xsb.addEmptyElement("include", p);
120: }
121: for (String m : getExcludedMethods()) {
122: Properties p = new Properties();
123: p.setProperty("name", m);
124: xsb.addEmptyElement("exclude", p);
125: }
126:
127: xsb.pop("methods");
128: xsb.pop("class");
129: } else {
130: xsb.addEmptyElement("class", pro);
131: }
132:
133: return xsb.toXML();
134:
135: }
136:
137: /**
138: * Clone an XmlClass by copying all its components.
139: *
140: * @param source
141: * @return
142: */
143: @Override
144: public Object clone() {
145: XmlClass result = new XmlClass(getName());
146: result.setExcludedMethods(getExcludedMethods());
147: result.setIncludedMethods(getIncludedMethods());
148:
149: return result;
150: }
151:
152: }
|