Search in sources :

Example 1 with MethodInstance

use of org.testng.internal.MethodInstance in project infinispan by infinispan.

the class FakeTestClass method newFailureMethodInstance.

public static MethodInstance newFailureMethodInstance(Exception e, XmlTest xmlTest, ITestContext context, Object instance) {
    Method failMethod;
    try {
        failMethod = TestFrameworkFailure.class.getMethod("fail");
    } catch (NoSuchMethodException e1) {
        e1.addSuppressed(e);
        e1.printStackTrace(System.err);
        throw new TestNGException(e1);
    }
    Class<?> testClass = instance.getClass();
    TestFrameworkFailure<?> fakeInstance = new TestFrameworkFailure<>(testClass, e);
    TestNGMethod testNGMethod = new TestNGMethod(failMethod, context.getSuite().getAnnotationFinder(), xmlTest, fakeInstance);
    ITestClass fakeTestClass = new FakeTestClass(testNGMethod, fakeInstance, xmlTest);
    testNGMethod.setTestClass(fakeTestClass);
    return new MethodInstance(testNGMethod);
}
Also used : ITestClass(org.testng.ITestClass) MethodInstance(org.testng.internal.MethodInstance) ITestNGMethod(org.testng.ITestNGMethod) TestNGMethod(org.testng.internal.TestNGMethod) ITestNGMethod(org.testng.ITestNGMethod) TestNGMethod(org.testng.internal.TestNGMethod) Method(java.lang.reflect.Method) TestNGException(org.testng.TestNGException)

Example 2 with MethodInstance

use of org.testng.internal.MethodInstance 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

TestNGException (org.testng.TestNGException)2 MethodInstance (org.testng.internal.MethodInstance)2 Method (java.lang.reflect.Method)1 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 ITestClass (org.testng.ITestClass)1 ITestContext (org.testng.ITestContext)1 ITestNGMethod (org.testng.ITestNGMethod)1 TestNGMethod (org.testng.internal.TestNGMethod)1