001: package org.testng.xml;
002:
003: import java.util.Properties;
004:
005: import org.testng.TestNGException;
006: import org.testng.reporters.XMLStringBuffer;
007:
008: /**
009: * This class describes the tag <method-selector> in testng.xml.
010: *
011: * Created on Sep 26, 2005
012: * @author cbeust
013: * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
014: */
015: public class XmlMethodSelector {
016: // Either this:
017: private String m_className;
018: private int m_priority;
019:
020: // Or that:
021: private String m_language;
022: private String m_expression;
023:
024: public String getClassName() {
025: return m_className;
026: }
027:
028: public void setName(String name) {
029: m_className = name;
030: }
031:
032: /**
033: * @return Returns the expression.
034: */
035: public String getExpression() {
036: return m_expression;
037: }
038:
039: /**
040: * @param expression The expression to set.
041: */
042: public void setExpression(String expression) {
043: m_expression = expression;
044: }
045:
046: /**
047: * @return Returns the language.
048: */
049: public String getLanguage() {
050: return m_language;
051: }
052:
053: /**
054: * @param language The language to set.
055: */
056: public void setLanguage(String language) {
057: m_language = language;
058: }
059:
060: public int getPriority() {
061: return m_priority;
062: }
063:
064: public void setPriority(int priority) {
065: m_priority = priority;
066: }
067:
068: private void ppp(String s) {
069: System.out.println("[XmlMethodSelector] " + s);
070: }
071:
072: public String toXml(String indent) {
073: XMLStringBuffer xsb = new XMLStringBuffer(indent);
074:
075: xsb.push("method-selector");
076:
077: ppp("CLASSNAME:" + m_className);
078:
079: if (null != m_className) {
080: Properties clsProp = new Properties();
081: clsProp.setProperty("name", getClassName());
082: if (getPriority() != -1) {
083: ppp("SETTING PRIORITY:" + getPriority());
084: clsProp.setProperty("priority", String
085: .valueOf(getPriority()));
086: }
087: xsb.addEmptyElement("selector-class", clsProp);
088: } else if (getLanguage() != null) {
089: Properties scriptProp = new Properties();
090: ppp("LANGUAGE:" + getLanguage());
091: scriptProp.setProperty("language", getLanguage());
092: xsb.push("script", scriptProp);
093: xsb.addCDATA(getExpression());
094: xsb.pop("script");
095: } else {
096: throw new TestNGException(
097: "Invalid Method Selector: found neither class name nor language");
098: }
099:
100: xsb.pop("method-selector");
101:
102: return xsb.toXML();
103: }
104: }
|