• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.testng.internal;
2 
3 import org.testng.IAttributes;
4 import org.testng.IClass;
5 import org.testng.ITest;
6 import org.testng.ITestContext;
7 import org.testng.ITestNGMethod;
8 import org.testng.ITestResult;
9 import org.testng.Reporter;
10 import org.testng.collections.Objects;
11 
12 import java.util.List;
13 import java.util.Set;
14 
15 
16 /**
17  * This class represents the result of a test.
18  *
19  * @author Cedric Beust, May 2, 2004
20  */
21 public class TestResult implements ITestResult {
22 
23   private static final long serialVersionUID = 6273017418233324556L;
24   private IClass m_testClass = null;
25   private ITestNGMethod m_method = null;
26   private int m_status = -1;
27   private Throwable m_throwable = null;
28   private long m_startMillis = 0;
29   private long m_endMillis = 0;
30   private String m_name = null;
31   private String m_host;
32   transient private Object[] m_parameters = {};
33   transient private Object m_instance;
34   private String m_instanceName;
35   private ITestContext m_context;
36 
TestResult()37   public TestResult() {
38 
39   }
40 
TestResult(IClass testClass, Object instance, ITestNGMethod method, Throwable throwable, long start, long end, ITestContext context)41   public TestResult(IClass testClass,
42       Object instance,
43       ITestNGMethod method,
44       Throwable throwable,
45       long start,
46       long end,
47       ITestContext context)
48   {
49     init(testClass, instance, method, throwable, start, end, context);
50   }
51 
52   /**
53    *
54    * @param testClass
55    * @param instance
56    * @param method
57    * @param throwable
58    * @param start
59    * @param end
60    */
init(IClass testClass, Object instance, ITestNGMethod method, Throwable throwable, long start, long end, ITestContext context)61   public void init (IClass testClass,
62       Object instance,
63       ITestNGMethod method,
64       Throwable throwable,
65       long start,
66       long end,
67       ITestContext context)
68   {
69     m_testClass = testClass;
70     m_throwable = throwable;
71     m_instanceName = m_testClass.getName();
72     if (null == m_throwable) {
73       m_status = ITestResult.SUCCESS;
74     }
75     m_startMillis = start;
76     m_endMillis = end;
77     m_method = method;
78     m_context = context;
79 
80     m_instance = instance;
81 
82     // Calculate the name: either the method name, ITest#getTestName or
83     // toString() if it's been overridden.
84     if (m_instance == null) {
85       m_name = m_method.getMethodName();
86     } else {
87       if (m_instance instanceof ITest) {
88         m_name = ((ITest) m_instance).getTestName();
89       }
90       else if (testClass.getTestName() != null) {
91         m_name = testClass.getTestName();
92       }
93       else {
94         String string = m_instance.toString();
95         // Only display toString() if it's been overridden by the user
96         m_name = getMethod().getMethodName();
97         try {
98           if (!Object.class.getMethod("toString")
99               .equals(m_instance.getClass().getMethod("toString"))) {
100             m_instanceName = string.startsWith("class ")
101                 ? string.substring("class ".length())
102                 : string;
103             m_name = m_name + " on " + m_instanceName;
104           }
105         }
106         catch(NoSuchMethodException ignore) {
107           // ignore
108         }
109       }
110     }
111   }
112 
ppp(String s)113   private static void ppp(String s) {
114     System.out.println("[TestResult] " + s);
115   }
116 
117   @Override
setEndMillis(long millis)118   public void setEndMillis(long millis) {
119     m_endMillis = millis;
120   }
121 
122   /**
123    * If this result's related instance implements ITest or use @Test(testName=...), returns its test name,
124    * otherwise returns null.
125    */
126   @Override
getTestName()127   public String getTestName() {
128     if (m_instance instanceof ITest) {
129       return ((ITest) m_instance).getTestName();
130     }
131     if (m_testClass.getTestName() != null) {
132       return m_testClass.getTestName();
133     }
134     return null;
135   }
136 
137   @Override
getName()138   public String getName() {
139     return m_name;
140   }
141 
142   /**
143    * @return Returns the method.
144    */
145   @Override
getMethod()146   public ITestNGMethod getMethod() {
147     return m_method;
148   }
149 
150   /**
151    * @param method The method to set.
152    */
setMethod(ITestNGMethod method)153   public void setMethod(ITestNGMethod method) {
154     m_method = method;
155   }
156 
157   /**
158    * @return Returns the status.
159    */
160   @Override
getStatus()161   public int getStatus() {
162     return m_status;
163   }
164 
165   /**
166    * @param status The status to set.
167    */
168   @Override
setStatus(int status)169   public void setStatus(int status) {
170     m_status = status;
171   }
172 
173   @Override
isSuccess()174   public boolean isSuccess() {
175     return ITestResult.SUCCESS == m_status;
176   }
177 
178   /**
179    * @return Returns the testClass.
180    */
181   @Override
getTestClass()182   public IClass getTestClass() {
183     return m_testClass;
184   }
185 
186   /**
187    * @param testClass The testClass to set.
188    */
setTestClass(IClass testClass)189   public void setTestClass(IClass testClass) {
190     m_testClass = testClass;
191   }
192 
193   /**
194    * @return Returns the throwable.
195    */
196   @Override
getThrowable()197   public Throwable getThrowable() {
198     return m_throwable;
199   }
200 
201   /**
202    * @param throwable The throwable to set.
203    */
204   @Override
setThrowable(Throwable throwable)205   public void setThrowable(Throwable throwable) {
206     m_throwable = throwable;
207   }
208 
209   /**
210    * @return Returns the endMillis.
211    */
212   @Override
getEndMillis()213   public long getEndMillis() {
214     return m_endMillis;
215   }
216 
217   /**
218    * @return Returns the startMillis.
219    */
220   @Override
getStartMillis()221   public long getStartMillis() {
222     return m_startMillis;
223   }
224 
225 //  public List<String> getOutput() {
226 //    return m_output;
227 //  }
228 
229   @Override
toString()230   public String toString() {
231     List<String> output = Reporter.getOutput(this);
232     String result = Objects.toStringHelper(getClass())
233         .omitNulls()
234         .omitEmptyStrings()
235         .add("name", getName())
236         .add("status", toString(m_status))
237         .add("method", m_method)
238         .add("output", output != null && output.size() > 0 ? output.get(0) : null)
239         .toString();
240 
241     return result;
242   }
243 
toString(int status)244   private String toString(int status) {
245     switch(status) {
246       case SUCCESS: return "SUCCESS";
247       case FAILURE: return "FAILURE";
248       case SKIP: return "SKIP";
249       case SUCCESS_PERCENTAGE_FAILURE: return "SUCCESS WITHIN PERCENTAGE";
250       case STARTED: return "STARTED";
251       default: throw new RuntimeException();
252     }
253   }
254 
255   @Override
getHost()256   public String getHost() {
257     return m_host;
258   }
259 
setHost(String host)260   public void setHost(String host) {
261     m_host = host;
262   }
263 
264   @Override
getParameters()265   public Object[] getParameters() {
266     return m_parameters;
267   }
268 
269   @Override
setParameters(Object[] parameters)270   public void setParameters(Object[] parameters) {
271     m_parameters = parameters;
272   }
273 
274   @Override
getInstance()275   public Object getInstance() {
276     return m_instance;
277   }
278 
279   private IAttributes m_attributes = new Attributes();
280 
281   @Override
getAttribute(String name)282   public Object getAttribute(String name) {
283     return m_attributes.getAttribute(name);
284   }
285 
286   @Override
setAttribute(String name, Object value)287   public void setAttribute(String name, Object value) {
288     m_attributes.setAttribute(name, value);
289   }
290 
291   @Override
getAttributeNames()292   public Set<String> getAttributeNames() {
293     return m_attributes.getAttributeNames();
294   }
295 
296   @Override
removeAttribute(String name)297   public Object removeAttribute(String name) {
298     return m_attributes.removeAttribute(name);
299   }
300 
301   @Override
getTestContext()302   public ITestContext getTestContext() {
303 	  return m_context;
304   }
305 
setContext(ITestContext context)306   public void setContext(ITestContext context) {
307 	  m_context = context;
308   }
309 
310   @Override
compareTo(ITestResult comparison)311   public int compareTo(ITestResult comparison) {
312 	  if( getStartMillis() > comparison.getStartMillis() ) {
313 		  return 1;
314 	  } else if( getStartMillis() < comparison.getStartMillis()) {
315 		  return -1;
316 	  } else {
317 		  return 0;
318 	  }
319   }
320 
321   @Override
getInstanceName()322   public String getInstanceName() {
323     return m_instanceName;
324   }
325 }
326 
327