|
| 1 | +package org.testng; |
| 2 | + |
| 3 | +import org.testng.collections.ListMultiMap; |
| 4 | +import org.testng.collections.Maps; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +/** |
| 9 | + * Helper class to keep track of dependencies. |
| 10 | + * |
| 11 | + * @author Cedric Beust <[email protected]> |
| 12 | + */ |
| 13 | +public class DependencyMap { |
| 14 | + private ListMultiMap<String, ITestNGMethod> m_dependencies = Maps.newListMultiMap(); |
| 15 | + private ListMultiMap<String, ITestNGMethod> m_groups = Maps.newListMultiMap(); |
| 16 | + |
| 17 | + public DependencyMap(ITestNGMethod[] methods) { |
| 18 | + for (ITestNGMethod m : methods) { |
| 19 | + m_dependencies.put(/* m.getTestClass().getName() + "." + */ m.getMethodName(), m); |
| 20 | + for (String g : m.getGroups()) { |
| 21 | + m_groups.put(g, m); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public List<ITestNGMethod> getMethodsThatBelongTo(String group, ITestNGMethod fromMethod) { |
| 27 | + List<ITestNGMethod> result = m_groups.get(group); |
| 28 | + if (result == null) { |
| 29 | + throw new TestNGException("Method \"" + fromMethod |
| 30 | + + "\" depends on nonexistent group \"" + group + "\""); |
| 31 | + } else { |
| 32 | + return result; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public ITestNGMethod getMethodDependingOn(String methodName, ITestNGMethod fromMethod) { |
| 37 | + List<ITestNGMethod> l = m_dependencies.get(methodName); |
| 38 | + for (ITestNGMethod m : l) { |
| 39 | + // If they are in the same class hierarchy, they must belong to the same instance, |
| 40 | + // otherwise, it's a method depending on a method in a different class so we |
| 41 | + // don't bother checking the instance |
| 42 | + if (fromMethod.getRealClass().isAssignableFrom(m.getRealClass())) { |
| 43 | + if (m.getInstance() == fromMethod.getInstance()) return m; |
| 44 | + } else { |
| 45 | + return m; |
| 46 | + } |
| 47 | + } |
| 48 | + throw new TestNGException("Method \"" + fromMethod |
| 49 | + + "\" depends on nonexistent method \"" + methodName + "\""); |
| 50 | + } |
| 51 | +} |
0 commit comments