1 package org.testng; 2 3 import org.testng.collections.ListMultiMap; 4 import org.testng.collections.Lists; 5 import org.testng.collections.Maps; 6 import org.testng.collections.Sets; 7 8 import java.util.ArrayList; 9 import java.util.List; 10 import java.util.Set; 11 import java.util.regex.Pattern; 12 13 /** 14 * Helper class to keep track of dependencies. 15 * 16 * @author Cedric Beust <cedric@beust.com> 17 */ 18 public class DependencyMap { 19 private ListMultiMap<String, ITestNGMethod> m_dependencies = Maps.newListMultiMap(); 20 private ListMultiMap<String, ITestNGMethod> m_groups = Maps.newListMultiMap(); 21 DependencyMap(ITestNGMethod[] methods)22 public DependencyMap(ITestNGMethod[] methods) { 23 for (ITestNGMethod m : methods) { 24 m_dependencies.put( m.getRealClass().getName() + "." + m.getMethodName(), m); 25 for (String g : m.getGroups()) { 26 m_groups.put(g, m); 27 } 28 } 29 } 30 getMethodsThatBelongTo(String group, ITestNGMethod fromMethod)31 public List<ITestNGMethod> getMethodsThatBelongTo(String group, ITestNGMethod fromMethod) { 32 Set<String> uniqueKeys = m_groups.keySet(); 33 34 List<ITestNGMethod> result = Lists.newArrayList(); 35 36 for (String k : uniqueKeys) { 37 if (Pattern.matches(group, k)) { 38 List<ITestNGMethod> temp = m_groups.get(k); 39 if (temp != null) 40 result.addAll(m_groups.get(k)); 41 } 42 } 43 44 if (result.isEmpty() && !fromMethod.ignoreMissingDependencies()) { 45 throw new TestNGException("DependencyMap::Method \"" + fromMethod 46 + "\" depends on nonexistent group \"" + group + "\""); 47 } else { 48 return result; 49 } 50 } 51 getMethodDependingOn(String methodName, ITestNGMethod fromMethod)52 public ITestNGMethod getMethodDependingOn(String methodName, ITestNGMethod fromMethod) { 53 List<ITestNGMethod> l = m_dependencies.get(methodName); 54 if (l == null && fromMethod.ignoreMissingDependencies()){ 55 return fromMethod; 56 } 57 if (l != null) { 58 for (ITestNGMethod m : l) { 59 // If they are in the same class hierarchy, they must belong to the same instance, 60 // otherwise, it's a method depending on a method in a different class so we 61 // don't bother checking the instance 62 if (fromMethod.getRealClass().isAssignableFrom(m.getRealClass())) { 63 if (m.getInstance() == fromMethod.getInstance()) return m; 64 } else { 65 return m; 66 } 67 } 68 } 69 throw new TestNGException("Method \"" + fromMethod 70 + "\" depends on nonexistent method \"" + methodName + "\""); 71 } 72 } 73