1 package org.testng.xml;
2 
3 import org.testng.collections.Lists;
4 import org.testng.collections.Maps;
5 import org.testng.reporters.XMLStringBuffer;
6 
7 import java.io.Serializable;
8 import java.util.Collections;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Properties;
12 
13 public class XmlInclude implements Serializable {
14   private static final long serialVersionUID = 1L;
15 
16   private String m_name;
17   private List<Integer> m_invocationNumbers = Lists.newArrayList();
18   private int m_index;
19   private String m_description;
20   private Map<String, String> m_parameters = Maps.newHashMap();
21 
22   private XmlClass m_xmlClass;
23 
XmlInclude()24   public XmlInclude() {
25   }
26 
XmlInclude(String n)27   public XmlInclude(String n) {
28     this(n, Collections.<Integer> emptyList(), 0);
29   }
30 
XmlInclude(String n, int index)31   public XmlInclude(String n, int index) {
32     this(n, Collections.<Integer> emptyList(), index);
33   }
34 
XmlInclude(String n, List<Integer> list, int index)35   public XmlInclude(String n, List<Integer> list, int index) {
36     m_name = n;
37     m_invocationNumbers = list;
38     m_index = index;
39   }
40 
setDescription(String description)41   public void setDescription(String description) {
42     m_description = description;
43   }
44 
getDescription()45   public String getDescription() {
46     return m_description;
47   }
48 
getName()49   public String getName() {
50     return m_name;
51   }
52 
getInvocationNumbers()53   public List<Integer> getInvocationNumbers() {
54     return m_invocationNumbers;
55   }
56 
getIndex()57   public int getIndex() {
58     return m_index;
59   }
60 
toXml(String indent)61   public String toXml(String indent) {
62     XMLStringBuffer xsb = new XMLStringBuffer(indent);
63     Properties p = new Properties();
64     p.setProperty("name", getName());
65     List<Integer> invocationNumbers = getInvocationNumbers();
66     if (invocationNumbers != null && invocationNumbers.size() > 0) {
67       p.setProperty("invocation-numbers",
68           XmlClass.listToString(invocationNumbers).toString());
69     }
70 
71     if (!m_parameters.isEmpty()){
72         xsb.push("include", p);
73         XmlUtils.dumpParameters(xsb, m_parameters);
74         xsb.pop("include");
75     } else {
76        xsb.addEmptyElement("include", p);
77     }
78 
79     return xsb.toXML();
80   }
81 
82   @Override
hashCode()83   public int hashCode() {
84     final int prime = 31;
85     int result = 1;
86     result = prime * result + m_index;
87     result = prime * result
88         + ((m_invocationNumbers == null) ? 0 : m_invocationNumbers.hashCode());
89     result = prime * result + (m_parameters == null ? 0 : m_parameters.hashCode());
90     result = prime * result + ((m_name == null) ? 0 : m_name.hashCode());
91     return result;
92   }
93 
94   @Override
equals(Object obj)95   public boolean equals(Object obj) {
96     if (this == obj)
97       return true;
98     if (obj == null)
99       return XmlSuite.f();
100     if (getClass() != obj.getClass())
101       return XmlSuite.f();
102     XmlInclude other = (XmlInclude) obj;
103     // if (m_index != other.m_index)
104     // return XmlSuite.f();
105     if (m_invocationNumbers == null) {
106       if (other.m_invocationNumbers != null)
107         return XmlSuite.f();
108     } else if (!m_invocationNumbers.equals(other.m_invocationNumbers))
109       return XmlSuite.f();
110     if (m_name == null) {
111       if (other.m_name != null)
112         return XmlSuite.f();
113     } else if (!m_name.equals(other.m_name))
114       return XmlSuite.f();
115     if (m_parameters == null) {
116       if (other.m_parameters != null) {
117         return XmlSuite.f();
118       }
119     } else if (!m_parameters.equals(other.m_parameters)) {
120       return XmlSuite.f();
121     }
122     return true;
123   }
124 
addParameter(String name, String value)125   public void addParameter(String name, String value) {
126     m_parameters.put(name, value);
127   }
128 
129   /**
130    * @deprecated Use {@code getLocalParameters()} or {@code getAllParameters()}
131    */
132   @Deprecated
getParameters()133   public Map<String, String> getParameters() {
134     return getAllParameters();
135   }
136 
137   /**
138    * @return the parameters defined in this test tag, and only this test tag. To retrieve
139    * the inherited parameters as well, call {@code getAllParameters()}.
140    */
getLocalParameters()141   public Map<String, String> getLocalParameters() {
142     return m_parameters;
143   }
144 
145   /**
146    * @return the parameters defined in this tag and the tags above it.
147    */
getAllParameters()148   public Map<String, String> getAllParameters() {
149     Map<String, String> result = Maps.newHashMap();
150     if (m_xmlClass != null) {
151       result.putAll(m_xmlClass.getAllParameters());
152     }
153     result.putAll(m_parameters);
154     return result;
155   }
156 
setXmlClass(XmlClass xmlClass)157   public void setXmlClass(XmlClass xmlClass) {
158     m_xmlClass = xmlClass;
159   }
160 
161 }
162