Search in sources :

Example 1 with IMethodInterceptor

use of org.testng.IMethodInterceptor in project infinispan by infinispan.

the class ChainMethodInterceptor method intercept.

@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    try {
        Set<Class<? extends IMethodInterceptor>> interceptorSet = new HashSet<>();
        List<Class<? extends IMethodInterceptor>> interceptorList = new ArrayList<>();
        Set<Class<? extends Predicate<IMethodInstance>>> filters = new HashSet<>();
        for (IMethodInstance method : methods) {
            findInterceptors(method.getInstance().getClass(), interceptorSet, interceptorList, filters);
        }
        if (!filters.isEmpty()) {
            List<? extends Predicate<IMethodInstance>> filterInstances = filters.stream().map(clazz -> {
                try {
                    return clazz.getConstructor().newInstance();
                } catch (Exception e) {
                    throw new IllegalStateException("Cannot construct filter", e);
                }
            }).collect(Collectors.toList());
            ArrayList<IMethodInstance> filteredMethods = new ArrayList<>(methods.size());
            METHODS: for (IMethodInstance m : methods) {
                for (Predicate<IMethodInstance> filter : filterInstances) {
                    if (hasFilter(m.getInstance().getClass(), filter.getClass()) && !filter.test(m))
                        continue METHODS;
                }
                filteredMethods.add(m);
            }
            methods = filteredMethods;
        }
        for (Class<? extends IMethodInterceptor> interceptor : interceptorList) {
            methods = interceptor.getConstructor().newInstance().intercept(methods, context);
        }
        return methods;
    } catch (Throwable t) {
        MethodInstance methodInstance = FakeTestClass.newFailureMethodInstance(new TestNGException(t), context.getCurrentXmlTest(), context, methods.get(0).getInstance());
        return Collections.singletonList(methodInstance);
    }
}
Also used : IMethodInstance(org.testng.IMethodInstance) ITestContext(org.testng.ITestContext) LogFactory(org.infinispan.util.logging.LogFactory) Predicate(java.util.function.Predicate) Set(java.util.Set) Collectors(java.util.stream.Collectors) TestNGException(org.testng.TestNGException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MethodInstance(org.testng.internal.MethodInstance) List(java.util.List) Log(org.infinispan.util.logging.Log) Collections(java.util.Collections) IMethodInterceptor(org.testng.IMethodInterceptor) IMethodInstance(org.testng.IMethodInstance) MethodInstance(org.testng.internal.MethodInstance) ArrayList(java.util.ArrayList) IMethodInterceptor(org.testng.IMethodInterceptor) TestNGException(org.testng.TestNGException) TestNGException(org.testng.TestNGException) Predicate(java.util.function.Predicate) IMethodInstance(org.testng.IMethodInstance) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Log (org.infinispan.util.logging.Log)1 LogFactory (org.infinispan.util.logging.LogFactory)1 IMethodInstance (org.testng.IMethodInstance)1 IMethodInterceptor (org.testng.IMethodInterceptor)1 ITestContext (org.testng.ITestContext)1 TestNGException (org.testng.TestNGException)1 MethodInstance (org.testng.internal.MethodInstance)1