1 package org.testng.internal; 2 3 import org.testng.IMethodInstance; 4 import org.testng.ITestNGMethod; 5 import org.testng.collections.Objects; 6 import org.testng.xml.XmlClass; 7 import org.testng.xml.XmlInclude; 8 import org.testng.xml.XmlTest; 9 10 import java.util.Comparator; 11 import java.util.List; 12 13 public class MethodInstance implements IMethodInstance { 14 private ITestNGMethod m_method; 15 MethodInstance(ITestNGMethod method)16 public MethodInstance(ITestNGMethod method) { 17 m_method = method; 18 } 19 20 @Override getMethod()21 public ITestNGMethod getMethod() { 22 return m_method; 23 } 24 25 @Override getInstances()26 public Object[] getInstances() { 27 return new Object[] { getInstance() }; 28 } 29 30 @Override getInstance()31 public Object getInstance() { 32 return m_method.getInstance(); 33 } 34 35 @Override toString()36 public String toString() { 37 return Objects.toStringHelper(getClass()) 38 .add("method", m_method) 39 .add("instance", getInstance()) 40 .toString(); 41 } 42 43 44 public static final Comparator<IMethodInstance> SORT_BY_INDEX 45 = new Comparator<IMethodInstance>() { 46 @Override 47 public int compare(IMethodInstance o1, IMethodInstance o2) { 48 // If the two methods are in different <test> 49 XmlTest test1 = o1.getMethod().getTestClass().getXmlTest(); 50 XmlTest test2 = o2.getMethod().getTestClass().getXmlTest(); 51 52 // If the two methods are not in the same <test>, we can't compare them 53 if (! test1.getName().equals(test2.getName())) { 54 return 0; 55 } 56 57 int result = 0; 58 59 // If the two methods are in the same <class>, compare them by their method 60 // index, otherwise compare them with their class index. 61 XmlClass class1 = o1.getMethod().getTestClass().getXmlClass(); 62 XmlClass class2 = o2.getMethod().getTestClass().getXmlClass(); 63 64 // This can happen if these classes came from a @Factory, in which case, they 65 // don't have an associated XmlClass 66 if (class1 == null || class2 == null) { 67 if (class1 != null) return -1; 68 if (class2 != null) return 1; 69 return 0; 70 } 71 72 if (! class1.getName().equals(class2.getName())) { 73 int index1 = class1.getIndex(); 74 int index2 = class2.getIndex(); 75 result = index1 - index2; 76 } 77 else { 78 XmlInclude include1 = 79 findXmlInclude(class1.getIncludedMethods(), o1.getMethod().getMethodName()); 80 XmlInclude include2 = 81 findXmlInclude(class2.getIncludedMethods(), o2.getMethod().getMethodName()); 82 if (include1 != null && include2 != null) { 83 result = include1.getIndex() - include2.getIndex(); 84 } 85 } 86 87 return result; 88 } 89 90 private XmlInclude findXmlInclude(List<XmlInclude> includedMethods, String methodName) { 91 for (XmlInclude xi : includedMethods) { 92 if (xi.getName().equals(methodName)) { 93 return xi; 94 } 95 } 96 return null; 97 } 98 }; 99 100 // public static final Comparator<IMethodInstance> SORT_BY_CLASS 101 // = new Comparator<IMethodInstance>() { 102 // public int compare(IMethodInstance o1, IMethodInstance o2) { 103 // int result= o1.getMethod().getTestClass().getName() 104 // .compareTo(o2.getMethod().getTestClass().getName()); 105 // return result; 106 // } 107 // }; 108 109 } 110