public Comparator<IMethodInstance> byDeclaration(){ return new Comparator<IMethodInstance>() { List<Method> classMethodsInOrder; @Override public int compare(IMethodInstance o1, IMethodInstance o2) { if(classMethodsInOrder == null){ classMethodsInOrder = Stream.of(Reflect.getDeclaredMethodsInOrder(o1.getInstance().getClass())).collect(toList()); } return Integer.compare( classMethodsInOrder.indexOf(o1.getMethod().getConstructorOrMethod().getMethod()), classMethodsInOrder.indexOf((o2.getMethod().getConstructorOrMethod().getMethod())) ); } }; }
@Override public List<IMethodInstance> intercept(final List<IMethodInstance> methods, final ITestContext context) { Collections.sort(methods, new Comparator<IMethodInstance>() { @Override public int compare(final IMethodInstance mi1, final IMethodInstance mi2) { // get test instances to order the tests. final Object o1 = mi1.getInstance(); final Object o2 = mi2.getInstance(); if (o1 instanceof ITest && o2 instanceof ITest) { return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName()); } // something else, don't care about the order return 0; } }); return methods; }
private Integer getGroupOrder(IMethodInstance o) { if (orderedGroups == null) { return 0; } String groupName = (o.getMethod().getGroups() != null) && (o.getMethod().getGroups().length > 0) ? o.getMethod().getGroups()[0] : "NONE"; if (!orderedGroups.contains("NONE")) { orderedGroups = orderedGroups + ", NONE"; } if (!orderedGroups.contains(groupName)) { orderedGroups = orderedGroups + "," + groupName; } log.debug(o.getMethod().getMethodName() + " Group: " + groupName + " Order : " + orderedGroups); return Integer.valueOf(orderedGroups.toUpperCase().indexOf(groupName.toUpperCase())); }
/** * This controls the sorting logic of the paxml test case executions. */ @Override public List<IMethodInstance> intercept(List<IMethodInstance> list, ITestContext context) { List<IMethodInstance> result = new ArrayList<IMethodInstance>(list); Collections.sort(result, new Comparator<IMethodInstance>() { @Override public int compare(IMethodInstance m1, IMethodInstance m2) { Long pid1 = getPid(m1.getInstances()[0]); Long pid2 = getPid(m2.getInstances()[0]); return pid1.compareTo(pid2); } }); return result; }
@Override public synchronized List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { /* * For some unknown reason TestNG calls method interceptors more than once for * same methods set. We determine if we already seen method by looking at type of internal TestNGMethod field. */ List<IMethodInstance> allExpandedMethods = Lists.newArrayList(); for (IMethodInstance method : methods) { if (isMethodAlreadyExpanded(method)) { allExpandedMethods.add(method); } else { List<IMethodInstance> newExpandedMethods = expandMethod(method); incrementSeenMethodsCount(newExpandedMethods); allExpandedMethods.addAll(newExpandedMethods); } } context.setAttribute(METHODS_COUNT_KEY, seenMethodsCount); return allExpandedMethods; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { Collections.sort(methods, new Comparator<IMethodInstance>() { @Override public int compare(final IMethodInstance mi1, final IMethodInstance mi2) { // get test instances to order the tests. final Object o1 = mi1.getInstance(); final Object o2 = mi2.getInstance(); if (o1 instanceof ITest && o2 instanceof ITest) { return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName()); } else { // something else, don't care about the order return 0; } } }); return methods; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { if ( !Compatibility.isZK34() ) { return methods; } List<IMethodInstance> filteredMethods = new ArrayList<>(); for ( IMethodInstance method : methods ) { if ( !isInGroup(method.getMethod().getGroups()) ) { filteredMethods.add(method); } } return filteredMethods; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { //Package Name -> Package priority Comparator<IMethodInstance> compareByPackage = (m1, m2) -> { String p1 = getPackageName(m1); String p2 = getPackageName(m2); return p1.compareTo(p2) - getPackagePriority(p1) + getPackagePriority(p2); }; //(Package name -> package priority) -> class priority -> class name -> method priority Comparator<IMethodInstance> comparator = compareByPackage .thenComparing(m -> getClassPriority(m)) .thenComparing(m -> getClassName(m)) .thenComparing(m -> getMethodPriority(m)); methods.sort(comparator); return methods; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { List<IMethodInstance> methodsToRun = new ArrayList<IMethodInstance>(); for (IMethodInstance instance : methods) { String clazz = instance.getMethod().getRealClass().getName(); if (!DriverType.isMobile()) { if(clazz.endsWith("WebTest")) { methodsToRun.add(instance); } } else { if(DriverType.isNative() && clazz.endsWith("AppTest")) { methodsToRun.add(instance); } else if(!DriverType.isNative() && clazz.endsWith("MobiTest")){ methodsToRun.add(instance); } } } return methodsToRun; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { for (IMethodInstance methodIns : methods) { ITestNGMethod method = methodIns.getMethod(); ConstructorOrMethod meth = method.getConstructorOrMethod(); Method m = meth.getMethod(); if (m != null) { DB db = m.getAnnotation(DB.class); if (db != null) { TransactionLegacy txn = TransactionLegacy.open(m.getName()); } } } // TODO Auto-generated method stub return methods; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { ISuite suite = context.getSuite(); List<IMethodInstance> filtered = new LinkedList<IMethodInstance>(); for (IMethodInstance candidate : methods) { if (matcher.matchesTest(candidate.getMethod().getTestClass().getName(), candidate.getMethod().getMethodName()) || matcher.matchesTest(suite.getName(), null)) { filtered.add(candidate); } } return filtered; }
/** * [IMethodInterceptor] * Invoked to enable alteration of the list of test methods that TestNG is about to run. * * @param methods list of test methods. * @param context test context. * @return the list of test methods to run. */ @Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { synchronized (methodInterceptors) { for (IMethodInterceptor interceptor : methodInterceptors) { methods = interceptor.intercept(methods, context); } } return methods; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { // target platform support: filter out non-targeted methods // target platform support: set method target descriptions return methods; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { return methods.stream() .collect(groupingBy(IMethodInstance::getInstance)) .entrySet().stream() .flatMap(OrderByGroupsListener::orderByGroupsIfListenerIsApplied) .collect(toList()); }
private static Stream<IMethodInstance> orderByGroups(Entry<Object, List<IMethodInstance>> entry) { return entry.getValue().stream() .collect(groupingBy(method -> asList(method.getMethod().getGroups()))) .entrySet().stream() .sorted(byGroupOrder()) .flatMap(e -> e.getValue().stream()); }
private static int getGroupIndex(Entry<List<String>, List<IMethodInstance>> o1, String[] order) { return Stream.of(order) .filter(item -> o1.getKey().contains(item)) .findFirst() .map(item -> asList(order).indexOf(item)) .orElse(-1); }
private static Optional<String[]> getGroupOrder(Entry<List<String>, List<IMethodInstance>> comparisonInstance){ val testClassInstance = comparisonInstance.getValue().get(0).getInstance(); return Stream.of(testClassInstance.getClass().getDeclaredMethods()) .filter(method -> method.isAnnotationPresent(GroupOrder.class)) .findFirst() .map(method -> (String[]) ReflectionUtils.invokeMethod(method, testClassInstance)); }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { return methods.stream() .collect(groupingBy(IMethodInstance::getInstance)) .entrySet().stream() .flatMap(e -> shouldBeInvoked(e.getKey().getClass(), OrderByDeclarationListener.class) ? e.getValue().stream().sorted(byDeclaration()) : e.getValue().stream()) .collect(toList()); }
private int getClassOrder(IMethodInstance mi) { int result = -1; Class<?> cls = mi.getMethod().getConstructorOrMethod().getMethod().getDeclaringClass(); Priority classPriority = cls.getAnnotation(Priority.class); if (classPriority != null) { result = classPriority.value(); } return result; }
private int getMethodOrder(IMethodInstance mi) { int result = -1; Method method = mi.getMethod().getConstructorOrMethod().getMethod(); Priority a1 = method.getAnnotation(Priority.class); if (a1 != null) { result = a1.value(); } return result; }
public List<IMethodInstance> intercept(List<IMethodInstance> lst, ITestContext context) { logger.debug("Method Order interceptor called"); String order = context.getCurrentXmlTest().getParameter("groupOrder"); MethodPriorityComparator comparator = new MethodPriorityComparator(order); Collections.sort(lst, comparator); return lst; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methodInstances, ITestContext context) { List<IMethodInstance> selection = new LinkedList<>(); for (IMethodInstance currentMethodInstance : methodInstances) { if (currentMethodInstance.getMethod().getMethodName().equals(m_methodName)) { selection.add(currentMethodInstance); } } return selection; }
private void incrementSeenMethodsCount(List<IMethodInstance> newExpandedMethods) { for (IMethodInstance newExpandedMethod : newExpandedMethods) { Optional<Object[][]> parametersForMethod = DataProviders.getParametersForMethod(newExpandedMethod.getMethod()); if (parametersForMethod.isPresent()) { seenMethodsCount += parametersForMethod.get().length; } else { seenMethodsCount++; } } }
private List<IMethodInstance> expandMethod(IMethodInstance method) { List<IMethodInstance> extendedMethods = Lists.newArrayList(); Set<Set<Requirement>> testSpecificRequirements = testSpecificRequirementsResolver.resolve(method.getMethod()); if (testSpecificRequirements.size() == 1) { extendedMethods.add(new MethodInstance(new RequirementsAwareTestNGMethod(method.getMethod(), getOnlyElement(testSpecificRequirements)))); } else { for (Set<Requirement> requirementSet : testSpecificRequirements) { TestNGMethod clonedMethod = (TestNGMethod) method.getMethod().clone(); extendedMethods.add(new MethodInstance(new RequirementsAwareTestNGMethod(clonedMethod, requirementSet))); } } return extendedMethods; }
public List<IMethodInstance> duplicate(ITestNGMethod testNGMethod, List <WTFCountry> requestedCountryList, List <WTFBrowser> requestedBrowserList, List <Environment> requestedEnvironmentList) { List <IMethodInstance> allowedMethods = new ArrayList <IMethodInstance>(); for (Environment environment : requestedEnvironmentList) { for (WTFCountry country : requestedCountryList) { for (WTFBrowser browser : requestedBrowserList) { final ITestNGMethod clonedTestNGMethod = testNGMethod.clone(); final Object[] instances = testNGMethod.getTestClass().getInstances(true); BaseTestRetryAnalyzer bt = new BaseTestRetryAnalyzer(); bt.environment = environment; bt.country = country; bt.browser = browser; clonedTestNGMethod.setRetryAnalyzer(bt); allowedMethods.add(new IMethodInstance() { public Object[] getInstances() { return instances; } public ITestNGMethod getMethod() { return clonedTestNGMethod; } public Object getInstance() { return instances; } }); } } } return allowedMethods; }
private int getMethodPriority(IMethodInstance mi) { Method method = getMethod(mi); Priority a1 = method.getAnnotation(Priority.class); if (a1 != null) { return a1.value(); } return 0; }
private int getClassPriority(IMethodInstance mi) { Class<?> cls = getDeclaringClassOfMethod(mi); Priority classPriority = cls.getAnnotation(Priority.class); if (classPriority != null) { return classPriority.value(); } return 0; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { Collections.sort(methods, new Comparator<IMethodInstance>() { @Override public int compare(IMethodInstance o1, IMethodInstance o2) { HasPriority i1 = (HasPriority) o1.getInstance(); HasPriority i2 = (HasPriority) o2.getInstance(); return Integer.compare(i1.getPriority(), i2.getPriority()); } }); return methods; }
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { Comparator<IMethodInstance> comparator = new Comparator<IMethodInstance>() { private int getPriority(IMethodInstance mi) { int result = 0; Method method = mi.getMethod().getMethod(); Priority a1 = method.getAnnotation(Priority.class); if (a1 != null) { result = a1.value(); } else { Class<?> cls = method.getDeclaringClass(); Priority classPriority = cls.getAnnotation(Priority.class); if (classPriority != null) { result = classPriority.value(); } } return result; } public int compare(IMethodInstance m1, IMethodInstance m2) { return getPriority(m1) - getPriority(m2); } }; IMethodInstance[] array = methods.toArray(new IMethodInstance[methods.size()]); Arrays.sort(array, comparator); Arrays.stream(array).forEach(method-> LOGGER.info("###test priority: " + method.getMethod().getMethodName())); return Arrays.asList(array); }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { interceptor.add(context.getName()); return methods; }
private static Stream<? extends IMethodInstance> orderByGroupsIfListenerIsApplied(Entry<Object, List<IMethodInstance>> entry) { if (shouldBeInvoked(entry.getKey().getClass(), OrderByGroupsListener.class)) { return orderByGroups(entry); } else return entry.getValue().stream(); }
public static Comparator<Entry<List<String>, List<IMethodInstance>>> byGroupOrder(){ return (o1, o2) -> getGroupOrder(o1) .map(order -> Integer.compare(getGroupIndex(o1, order), getGroupIndex(o2, order))) .orElse(0); }
@Override public void onAfterClass(ITestClass iTestClass, IMethodInstance iMethodInstance) { MockInitialContextFactory.destroy(); }
@Override public int compare(IMethodInstance o1, IMethodInstance o2) { log.debug(o1.getMethod().getMethodName() + " O2: " + o2.getMethod().getMethodName() + " Order : " + orderedGroups); String method1Name = o1.getMethod().getMethodName(); String method2Name = o2.getMethod().getMethodName(); Set<String> depends, o1Set, o2Set, o1Dependency, o2Dependency; // check method dependency o1Dependency = new HashSet<String>(Arrays.asList(o1.getMethod().getMethodsDependedUpon())); o2Dependency = new HashSet<String>(Arrays.asList(o1.getMethod().getMethodsDependedUpon())); if (o1Dependency.contains(method2Name)) { return -1; } if (o2Dependency.contains(method1Name)) { return 1; } // check group dependency o1Set = new HashSet<String>(Arrays.asList(o1.getMethod().getGroups())); o2Set = new HashSet<String>(Arrays.asList(o1.getMethod().getGroups())); o1Dependency = new HashSet<String>(Arrays.asList(o1.getMethod().getGroupsDependedUpon())); o2Dependency = new HashSet<String>(Arrays.asList(o1.getMethod().getGroupsDependedUpon())); depends = new HashSet<String>(o1Dependency); depends.retainAll(o2Set); if (!depends.isEmpty()) { return -1; } depends = new HashSet<String>(o2Dependency); depends.retainAll(o1Set); if (!depends.isEmpty()) { return 1; } int o1Priority = getGroupOrder(o1); int o2Priority = getGroupOrder(o2); if (o1Priority == o2Priority) { o1Priority = getClassOrder(o1); o2Priority = getClassOrder(o2); if (o1Priority == o2Priority) { o1Priority = getMethodOrder(o1); o2Priority = getMethodOrder(o2); } } if (o1Priority == -1) { o1Priority = o2Priority + 1; } if (o2Priority == -1) { o2Priority = o1Priority + 1; } return o1Priority - o2Priority; }