/** * If the method has parameters, fill them in. Either by using a @DataProvider if any was * provided, or by looking up <parameters> in testng.xml * * @return An Iterator over the values for each parameter of this method. */ public static Iterator<Object[]> handleParameters( ITestNGMethod testMethod, Map<String, String> allParameterNames, Object instance, MethodParameters methodParams, XmlSuite xmlSuite, IAnnotationFinder annotationFinder, Object fedInstance) { Iterator<Object[]> result = null; // // Do we have a @DataProvider? If yes, then we have several // sets of parameters for this method // Method dataProvider = findDataProvider( testMethod.getTestClass().getRealClass(), testMethod.getMethod(), annotationFinder); if (null != dataProvider) { int parameterCount = testMethod.getMethod().getParameterTypes().length; for (int i = 0; i < parameterCount; i++) { String n = "param" + i; allParameterNames.put(n, n); } result = MethodHelper.invokeDataProvider( instance, /* a test instance or null if the dataprovider is static*/ dataProvider, testMethod, methodParams.context, fedInstance, annotationFinder); } else { // // Normal case: we have only one set of parameters coming from testng.xml // allParameterNames.putAll(methodParams.xmlParameters); // Create an Object[][] containing just one row of parameters Object[][] allParameterValuesArray = new Object[1][]; allParameterValuesArray[0] = createParameters( testMethod.getMethod(), methodParams, annotationFinder, xmlSuite, ITestAnnotation.class, "@Test"); // Mark that this method needs to have at least a certain // number of invocations (needed later to call AfterGroups // at the right time). testMethod.setParameterInvocationCount(allParameterValuesArray.length); // Turn it into an Iterable result = MethodHelper.createArrayIterator(allParameterValuesArray); } return result; }
/** * Fix the methodsDependedUpon to make sure that @Configuration methods respect inheritance * (before methods are invoked in the order Base first and after methods are invoked in the order * Child first) * * @param methods the list of methods * @param before true if we are handling a before method (meaning, the methods need to be sorted * base class first and subclass last). false otherwise (subclass methods first, base classes * last). */ public static void fixMethodInheritance(ITestNGMethod[] methods, boolean before) { // Map of classes -> List of methods that belong to this class or same hierarchy Map<Class, List<ITestNGMethod>> map = Maps.newHashMap(); // // Put the list of methods in their hierarchy buckets // for (ITestNGMethod method : methods) { Class<? extends ITestNGMethod> methodClass = method.getRealClass(); List<ITestNGMethod> l = findMethodListSuperClass(map, methodClass); if (null != l) { l.add(method); } else { Class subClass = findSubClass(map, methodClass); if (null != subClass) { l = map.get(subClass); l.add(method); map.remove(subClass); map.put(methodClass, l); } else { l = Lists.newArrayList(); l.add(method); map.put(methodClass, l); } } } // // Each bucket that has a list bigger than one element gets sorted // for (List<ITestNGMethod> l : map.values()) { if (l.size() > 1) { // Sort them sortMethodsByInheritance(l, before); /* * Set methodDependedUpon accordingly * E.g. Base class can have multiple @BeforeClass methods. Need to ensure * that @BeforeClass methods in derived class depend on all @BeforeClass methods * of base class. Vice versa for @AfterXXX methods */ for (int i = 0; i < l.size() - 1; i++) { ITestNGMethod m1 = l.get(i); for (int j = i + 1; j < l.size(); j++) { ITestNGMethod m2 = l.get(j); if (!equalsEffectiveClass(m1, m2) && !dependencyExists(m1, m2, methods)) { Utils.log("MethodInheritance", 4, m2 + " DEPENDS ON " + m1); m2.addMethodDependedUpon(MethodHelper.calculateMethodCanonicalName(m1)); } } } } } }
private static boolean internalDependencyExists( ITestNGMethod m1, ITestNGMethod m2, ITestNGMethod[] methods) { ITestNGMethod[] methodsNamed = MethodHelper.findDependedUponMethods(m1, methods); for (ITestNGMethod method : methodsNamed) { if (method.equals(m2)) { return true; } } for (String group : m1.getGroupsDependedUpon()) { ITestNGMethod[] methodsThatBelongToGroup = MethodGroupsHelper.findMethodsThatBelongToGroup(m1, methods, group); for (ITestNGMethod method : methodsThatBelongToGroup) { if (method.equals(m2)) { return true; } } } return false; }