1package org.testng.xml;
2
3import java.io.IOException;
4import java.io.Serializable;
5import java.util.List;
6import java.util.Properties;
7
8import org.testng.collections.Lists;
9import org.testng.internal.PackageUtils;
10import org.testng.internal.Utils;
11import org.testng.reporters.XMLStringBuffer;
12
13/**
14 * This class describes the tag <package>  in testng.xml.
15 *
16 * @author Cedric
17 * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
18 */
19public class XmlPackage implements Serializable {
20  /**
21   *
22   */
23  private static final long serialVersionUID = 1996341670354923204L;
24  private String m_name;
25  private List<String> m_include = Lists.newArrayList();
26  private List<String> m_exclude = Lists.newArrayList();
27  private List<XmlClass> m_xmlClasses= null;
28
29  public XmlPackage() {
30  }
31
32  // For YAML
33  public XmlPackage(String name) {
34    m_name = name;
35  }
36
37  /**
38   * @return the exclude
39   */
40  public List<String> getExclude() {
41    return m_exclude;
42  }
43
44  /**
45   * @param exclude the exclude to set
46   */
47  public void setExclude(List<String> exclude) {
48    m_exclude = exclude;
49  }
50
51  /**
52   * @return the include
53   */
54  public List<String> getInclude() {
55    return m_include;
56  }
57
58  /**
59   * @param include the include to set
60   */
61  public void setInclude(List<String> include) {
62    m_include = include;
63  }
64
65  /**
66   * @return the name
67   */
68  public String getName() {
69    return m_name;
70  }
71
72  /**
73   * @param name the name to set
74   */
75  public void setName(String name) {
76    m_name = name;
77  }
78
79  public List<XmlClass> getXmlClasses() {
80    if(null == m_xmlClasses) {
81      m_xmlClasses= initializeXmlClasses();
82    }
83
84    return m_xmlClasses;
85  }
86
87  private List<XmlClass> initializeXmlClasses() {
88    List<XmlClass> result= Lists.newArrayList();
89    try {
90      String[] classes = PackageUtils.findClassesInPackage(m_name, m_include, m_exclude);
91
92      int index = 0;
93      for(String className: classes) {
94        result.add(new XmlClass(className, index++, false /* don't load classes */));
95      }
96    }
97    catch(IOException ioex) {
98      Utils.log("XmlPackage", 1, ioex.getMessage());
99    }
100
101    return result;
102  }
103
104  public String toXml(String indent) {
105    XMLStringBuffer xsb = new XMLStringBuffer(indent);
106    Properties p = new Properties();
107    p.setProperty("name", getName());
108
109    if (getInclude().isEmpty() && getExclude().isEmpty()) {
110      xsb.addEmptyElement("package", p);
111    } else {
112      xsb.push("package", p);
113
114      for (String m : getInclude()) {
115        Properties includeProp= new Properties();
116        includeProp.setProperty("name", m);
117        xsb.addEmptyElement("include", includeProp);
118      }
119      for (String m: getExclude()) {
120        Properties excludeProp= new Properties();
121        excludeProp.setProperty("name", m);
122        xsb.addEmptyElement("exclude", excludeProp);
123      }
124
125      xsb.pop("package");
126    }
127
128    return xsb.toXML();
129  }
130
131  @Override
132  public int hashCode() {
133    final int prime = 31;
134    int result = 1;
135    result = prime * result + ((m_exclude == null) ? 0 : m_exclude.hashCode());
136    result = prime * result + ((m_include == null) ? 0 : m_include.hashCode());
137    result = prime * result + ((m_name == null) ? 0 : m_name.hashCode());
138    result = prime * result
139        + ((m_xmlClasses == null) ? 0 : m_xmlClasses.hashCode());
140    return result;
141  }
142
143  @Override
144  public boolean equals(Object obj) {
145    if (this == obj)
146      return true;
147    if (obj == null)
148      return XmlSuite.f();
149    if (getClass() != obj.getClass())
150      return XmlSuite.f();
151    XmlPackage other = (XmlPackage) obj;
152    if (m_exclude == null) {
153      if (other.m_exclude != null)
154        return XmlSuite.f();
155    } else if (!m_exclude.equals(other.m_exclude))
156      return XmlSuite.f();
157    if (m_include == null) {
158      if (other.m_include != null)
159        return XmlSuite.f();
160    } else if (!m_include.equals(other.m_include))
161      return XmlSuite.f();
162    if (m_name == null) {
163      if (other.m_name != null)
164        return XmlSuite.f();
165    } else if (!m_name.equals(other.m_name))
166      return XmlSuite.f();
167    if (m_xmlClasses == null) {
168      if (other.m_xmlClasses != null)
169        return XmlSuite.f();
170    } else if (!m_xmlClasses.equals(other.m_xmlClasses))
171      return XmlSuite.f();
172    return true;
173  }
174
175}
176