Package org.testng.xml

Source Code of org.testng.xml.XmlClass

package org.testng.xml;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.testng.TestNGException;
import org.testng.internal.ClassHelper;
import org.testng.reporters.XMLStringBuffer;

/**
* This class describes the tag <class> in testng.xml.
*
* @author <a href="mailto:cedric@beust.com">Cedric Beust</a>
*/
public class XmlClass implements Serializable, Cloneable {
  private List<String> m_includedMethods = new ArrayList<String>();
  private List<String> m_excludedMethods = new ArrayList<String>();
  private String       m_name = null;
  private Class        m_class = null;

  public XmlClass(String name) {
    init(name, null);
  }

  public XmlClass(Class className) {
    init(className.getName(), className);
  }

  private void init(String name, Class className) {
    m_name = name;
    m_class = className;
  }

  /**
   * @return Returns the className.
   */
  public Class getSupportClass() {
    if(null == m_class) {
      m_class = ClassHelper.forName(m_name);
     
      if(null == m_class) {
        throw new TestNGException("Cannot find class in classpath: " + m_name);
      }
    }

    return m_class;
  }

  /**
   * @param className The className to set.
   */
  public void setClass(Class className) {
    m_class = className;
  }

  /**
   * @return Returns the excludedMethods.
   */
  public List<String> getExcludedMethods() {
    return m_excludedMethods;
  }

  /**
   * @param excludedMethods The excludedMethods to set.
   */
  public void setExcludedMethods(List<String> excludedMethods) {
    m_excludedMethods = excludedMethods;
  }

  /**
   * @return Returns the includedMethods.
   */
  public List<String> getIncludedMethods() {
    return m_includedMethods;
  }

  /**
   * @param includedMethods The includedMethods to set.
   */
  public void setIncludedMethods(List<String> includedMethods) {
    m_includedMethods = includedMethods;
  }

  /**
   * @return Returns the name.
   */
  public String getName() {
    return m_name;
  }

  /**
   * @param name The name to set.
   */
  public void setName(String name) {
    m_name = name;
  }

  @Override
  public String toString() {
    return "[Class: " + m_name + "]";
  }

  public String toXml(String indent) {
    XMLStringBuffer xsb = new XMLStringBuffer(indent);
    Properties      pro = new Properties();
    pro.setProperty("name", getName());


    if (!m_includedMethods.isEmpty() || !m_excludedMethods.isEmpty()) {
      xsb.push("class", pro);
      xsb.push("methods");
     
      for (String m : getIncludedMethods()) {
        Properties p = new Properties();
        p.setProperty("name", m);
        xsb.addEmptyElement("include", p);
      }
      for (String m: getExcludedMethods()) {
        Properties p= new Properties();
        p.setProperty("name", m);
        xsb.addEmptyElement("exclude", p);
      }
     
      xsb.pop("methods");
      xsb.pop("class");
    }
    else {
      xsb.addEmptyElement("class", pro);
    }
   

    return xsb.toXML();

  }
 
  /**
   * Clone an XmlClass by copying all its components.
   *
   * @param source
   * @return
   */
  @Override
  public Object clone() {
    XmlClass result = new XmlClass(getName());
    result.setExcludedMethods(getExcludedMethods());
    result.setIncludedMethods(getIncludedMethods());
   
    return result;
  }


}
TOP

Related Classes of org.testng.xml.XmlClass

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.