Home | History | Annotate | Download | only in xml
      1 package org.testng.xml;
      2 
      3 import org.testng.TestNGException;
      4 import org.testng.reporters.XMLStringBuffer;
      5 import org.testng.xml.dom.OnElement;
      6 
      7 import java.util.Properties;
      8 
      9 /**
     10  * This class describes the tag <method-selector>  in testng.xml.
     11  *
     12  * Created on Sep 26, 2005
     13  * @author cbeust
     14  * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
     15  */
     16 public class XmlMethodSelector {
     17   // Either this:
     18   private String m_className;
     19   private int m_priority;
     20 
     21   // Or that:
     22   private XmlScript m_script = new XmlScript();
     23 
     24   // For YAML
     25   public void setClassName(String s) {
     26     m_className = s;
     27   }
     28 
     29   public String getClassName() {
     30     return m_className;
     31   }
     32 
     33   // For YAML
     34   @OnElement(tag = "selector-class", attributes = { "name", "priority" })
     35   public void setElement(String name, String priority) {
     36     setName(name);
     37     setPriority(Integer.parseInt(priority));
     38   }
     39 
     40   public void setName(String name) {
     41     m_className = name;
     42   }
     43 
     44   public void setScript(XmlScript script) {
     45     m_script = script;
     46   }
     47 
     48   /**
     49    * @return Returns the expression.
     50    */
     51   public String getExpression() {
     52     return m_script.getScript();
     53   }
     54 
     55   /**
     56    * @param expression The expression to set.
     57    */
     58   public void setExpression(String expression) {
     59     m_script.setScript(expression);
     60   }
     61 
     62   /**
     63    * @return Returns the language.
     64    */
     65   public String getLanguage() {
     66     return m_script.getLanguage();
     67   }
     68 
     69   /**
     70    * @param language The language to set.
     71    */
     72 //  @OnElement(tag = "script", attributes = "language")
     73   public void setLanguage(String language) {
     74     m_script.setLanguage(language);
     75 //    m_language = language;
     76   }
     77 
     78   public int getPriority() {
     79     return m_priority;
     80   }
     81 
     82   public void setPriority(int priority) {
     83     m_priority = priority;
     84   }
     85 
     86   private void ppp(String s) {
     87     System.out.println("[XmlMethodSelector] " + s);
     88   }
     89 
     90   public String toXml(String indent) {
     91     XMLStringBuffer xsb = new XMLStringBuffer(indent);
     92 
     93     xsb.push("method-selector");
     94 
     95     if (null != m_className) {
     96       Properties clsProp = new Properties();
     97       clsProp.setProperty("name", getClassName());
     98       if(getPriority() != -1) {
     99         clsProp.setProperty("priority", String.valueOf(getPriority()));
    100       }
    101       xsb.addEmptyElement("selector-class", clsProp);
    102     }
    103     else if (getLanguage() != null) {
    104       Properties scriptProp = new Properties();
    105       scriptProp.setProperty("language", getLanguage());
    106       xsb.push("script", scriptProp);
    107       xsb.addCDATA(getExpression());
    108       xsb.pop("script");
    109     }
    110     else {
    111       throw new TestNGException("Invalid Method Selector:  found neither class name nor language");
    112     }
    113 
    114     xsb.pop("method-selector");
    115 
    116     return xsb.toXML();
    117   }
    118 
    119   @Override
    120   public int hashCode() {
    121     final int prime = 31;
    122     int result = 1;
    123     result = prime * result
    124         + ((m_className == null) ? 0 : m_className.hashCode());
    125     result = prime * result
    126         + ((getExpression() == null) ? 0 : getExpression().hashCode());
    127     result = prime * result
    128         + ((getLanguage() == null) ? 0 : getLanguage().hashCode());
    129     result = prime * result + m_priority;
    130     return result;
    131   }
    132 
    133   @Override
    134   public boolean equals(Object obj) {
    135     if (this == obj)
    136       return true;
    137     if (obj == null)
    138       return XmlSuite.f();
    139     if (getClass() != obj.getClass())
    140       return XmlSuite.f();
    141     XmlMethodSelector other = (XmlMethodSelector) obj;
    142     if (m_className == null) {
    143       if (other.m_className != null)
    144         return XmlSuite.f();
    145     } else if (!m_className.equals(other.m_className))
    146       return XmlSuite.f();
    147     if (getExpression() == null) {
    148       if (other.getExpression() != null)
    149         return XmlSuite.f();
    150     } else if (!getExpression().equals(other.getExpression()))
    151       return XmlSuite.f();
    152     if (getLanguage() == null) {
    153       if (other.getLanguage() != null)
    154         return XmlSuite.f();
    155     } else if (!getLanguage().equals(other.getLanguage()))
    156       return XmlSuite.f();
    157     if (m_priority != other.m_priority)
    158       return XmlSuite.f();
    159     return true;
    160   }
    161 }
    162