Home | History | Annotate | Download | only in testng
      1 package org.testng;
      2 
      3 import org.testng.collections.Lists;
      4 import org.testng.collections.Maps;
      5 
      6 import java.util.List;
      7 import java.util.Map;
      8 
      9 /**
     10  * A method interceptor that sorts its methods per instances (i.e. per class).
     11  *
     12  *
     13  */
     14 class InstanceOrderingMethodInterceptor implements IMethodInterceptor {
     15 
     16   @Override
     17   public List<IMethodInstance> intercept(List<IMethodInstance> methods,
     18       ITestContext context)  {
     19     return groupMethodsByInstance(methods);
     20   }
     21 
     22   /**
     23    * The default method interceptor which sorts methods by instances (i.e. by class).
     24    */
     25   private List<IMethodInstance> groupMethodsByInstance(List<IMethodInstance> methods) {
     26     List<Object> instanceList = Lists.newArrayList();
     27     Map<Object, List<IMethodInstance>> map = Maps.newHashMap();
     28     for (IMethodInstance mi : methods) {
     29       Object[] methodInstances = mi.getInstances();
     30       for (Object instance : methodInstances) {
     31         if (!instanceList.contains(instance)) {
     32           instanceList.add(instance);
     33         }
     34         List<IMethodInstance> l = map.get(instance);
     35         if (l == null) {
     36           l = Lists.newArrayList();
     37           map.put(instance, l);
     38         }
     39         l.add(mi);
     40       }
     41     }
     42 
     43     List<IMethodInstance> result = Lists.newArrayList();
     44     for (Object instance : instanceList) {
     45       result.addAll(map.get(instance));
     46     }
     47 
     48     return result;
     49   }
     50 
     51 }
     52