All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testng.InstanceOrderingMethodInterceptor Maven / Gradle / Ivy

The newest version!
package org.testng;

import java.util.List;
import java.util.Map;
import org.testng.collections.Lists;
import org.testng.collections.Maps;

/** A method interceptor that sorts its methods per instances (i.e. per class). */
class InstanceOrderingMethodInterceptor implements IMethodInterceptor {

  @Override
  public List intercept(List methods, ITestContext context) {
    return groupMethodsByInstance(methods);
  }

  /** The default method interceptor which sorts methods by instances (i.e. by class). */
  private List groupMethodsByInstance(List methods) {
    List instanceList = Lists.newArrayList();
    Map> map = Maps.newLinkedHashMap();
    for (IMethodInstance mi : methods) {
      Object instance = mi.getInstance();
      if (!instanceList.contains(instance)) {
        instanceList.add(instance);
      }
      List l = map.computeIfAbsent(instance, k -> Lists.newArrayList());
      l.add(mi);
    }

    List result = Lists.newArrayList();
    for (Object instance : instanceList) {
      result.addAll(map.get(instance));
    }

    return result;
  }
}