1package org.testng; 2 3import org.testng.collections.Lists; 4import org.testng.collections.Maps; 5 6import java.util.List; 7import java.util.Map; 8 9/** 10 * A method interceptor that sorts its methods per instances (i.e. per class). 11 * 12 * 13 */ 14class 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