org.testng.IMethodInstance Java Examples

The following examples show how to use org.testng.IMethodInstance. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestNGAop.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    for (IMethodInstance methodIns : methods) {
        ITestNGMethod method = methodIns.getMethod();
        ConstructorOrMethod meth = method.getConstructorOrMethod();
        Method m = meth.getMethod();
        if (m != null) {
            DB db = m.getAnnotation(DB.class);
            if (db != null) {
                TransactionLegacy txn = TransactionLegacy.open(m.getName());
            }
        }
    }

    // TODO Auto-generated method stub
    return methods;
}
 
Example #2
Source File: TestReorderInterceptor.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<IMethodInstance> intercept(final List<IMethodInstance> methods, final ITestContext context) {
    Collections.sort(methods, new Comparator<IMethodInstance>() {
        @Override
        public int compare(final IMethodInstance mi1, final IMethodInstance mi2) {
            // get test instances to order the tests.
            final Object o1 = mi1.getInstance();
            final Object o2 = mi2.getInstance();
            if (o1 instanceof ITest && o2 instanceof ITest) {
                return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName());
            }
            // something else, don't care about the order
            return 0;
        }
    });

    return methods;
}
 
Example #3
Source File: MethodPriorityComparator.java    From qaf with MIT License 6 votes vote down vote up
private Integer getGroupOrder(IMethodInstance o) {
	if (orderedGroups == null) {
		return 0;
	}
	String groupName = (o.getMethod().getGroups() != null) && (o.getMethod().getGroups().length > 0)
			? o.getMethod().getGroups()[0] : "NONE";
	if (!orderedGroups.contains("NONE")) {
		orderedGroups = orderedGroups + ", NONE";
	}
	if (!orderedGroups.contains(groupName)) {
		orderedGroups = orderedGroups + "," + groupName;
	}

	log.debug(o.getMethod().getMethodName() + " Group: " + groupName + " Order : " + orderedGroups);

	return Integer.valueOf(orderedGroups.toUpperCase().indexOf(groupName.toUpperCase()));
}
 
Example #4
Source File: TestReorderInterceptor.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<IMethodInstance> intercept(final List<IMethodInstance> methods, final ITestContext context) {
    Collections.sort(methods, new Comparator<IMethodInstance>() {
        @Override
        public int compare(final IMethodInstance mi1, final IMethodInstance mi2) {
            // get test instances to order the tests.
            final Object o1 = mi1.getInstance();
            final Object o2 = mi2.getInstance();
            if (o1 instanceof ITest && o2 instanceof ITest) {
                return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName());
            }
            // something else, don't care about the order
            return 0;
        }
    });

    return methods;
}
 
Example #5
Source File: TestReorderInterceptor.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    Collections.sort(methods, new Comparator<IMethodInstance>() {
        @Override
        public int compare(final IMethodInstance mi1, final IMethodInstance mi2) {
            // get test instances to order the tests.
            final Object o1 = mi1.getInstance();
            final Object o2 = mi2.getInstance();
            if (o1 instanceof ITest && o2 instanceof ITest) {
                return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName());
            } else {
                // something else, don't care about the order
                return 0;
            }
        }
    });

    return methods;
}
 
Example #6
Source File: TestReorderInterceptor.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    Collections.sort(methods, new Comparator<IMethodInstance>() {
        @Override
        public int compare(final IMethodInstance mi1, final IMethodInstance mi2) {
            // get test instances to order the tests.
            final Object o1 = mi1.getInstance();
            final Object o2 = mi2.getInstance();
            if (o1 instanceof ITest && o2 instanceof ITest) {
                return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName());
            } else {
                // something else, don't care about the order
                return 0;
            }
        }
    });

    return methods;
}
 
Example #7
Source File: TestReorderInterceptor.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<IMethodInstance> intercept(final List<IMethodInstance> methods, final ITestContext context) {
    Collections.sort(methods, new Comparator<IMethodInstance>() {
        @Override
        public int compare(final IMethodInstance mi1, final IMethodInstance mi2) {
            // get test instances to order the tests.
            final Object o1 = mi1.getInstance();
            final Object o2 = mi2.getInstance();
            if (o1 instanceof ITest && o2 instanceof ITest) {
                return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName());
            }
            // something else, don't care about the order
            return 0;
        }
    });

    return methods;
}
 
Example #8
Source File: ExecutionOrder.java    From functional-tests-core with Apache License 2.0 6 votes vote down vote up
@Override
public java.util.List<IMethodInstance> intercept(java.util.List<IMethodInstance> list, ITestContext iTestContext) {
    Comparator<IMethodInstance> comparator = new Comparator<IMethodInstance>() {
        private int getPriority(IMethodInstance mi) {
            int result = 0;
            int methodIndex = Integer.parseInt(mi.getMethod().getMethodName().replaceAll("\\D+", ""));
            return methodIndex;
        }

        public int compare(IMethodInstance m1, IMethodInstance m2) {
            return getPriority(m1) - getPriority(m2);
        }
    };

    IMethodInstance[] array = list.toArray(new IMethodInstance[list.size()]);
    Arrays.sort(array, comparator);
    return Arrays.asList(array);
}
 
Example #9
Source File: TestReorderInterceptor.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<IMethodInstance> intercept(final List<IMethodInstance> methods, final ITestContext context) {
    Collections.sort(methods, new Comparator<IMethodInstance>() {
        @Override
        public int compare(final IMethodInstance mi1, final IMethodInstance mi2) {
            // get test instances to order the tests.
            final Object o1 = mi1.getInstance();
            final Object o2 = mi2.getInstance();
            if (o1 instanceof ITest && o2 instanceof ITest) {
                return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName());
            }
            // something else, don't care about the order
            return 0;
        }
    });

    return methods;
}
 
Example #10
Source File: TestReorderInterceptor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<IMethodInstance> intercept(final List<IMethodInstance> methods, final ITestContext context) {
    Collections.sort(methods, new Comparator<IMethodInstance>() {
        @Override
        public int compare(final IMethodInstance mi1, final IMethodInstance mi2) {
            // get test instances to order the tests.
            final Object o1 = mi1.getInstance();
            final Object o2 = mi2.getInstance();
            if (o1 instanceof ITest && o2 instanceof ITest) {
                return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName());
            }
            // something else, don't care about the order
            return 0;
        }
    });

    return methods;
}
 
Example #11
Source File: PlatformInterceptor.java    From Selenium-Foundation with Apache License 2.0 6 votes vote down vote up
/**
 * Add the specified method to the method list
 *
 * @param methodList list of methods that are about to be run
 * @param testMethod method to be added to the list
 * @param  platformConstant target platform on which to run this method
 */
@SuppressWarnings("unchecked")
private static <P extends Enum<?> & PlatformEnum> void addMethodForPlatform(List<IMethodInstance> methodList, IMethodInstance testMethod, PlatformEnum platformConstant) {
    if (platformConstant != null) {
        ITestNGMethod method = testMethod.getMethod();
        PlatformIdentity<P> identity = new PlatformIdentity<>((P) platformConstant, method.getDescription());
        testMethod.getMethod().setDescription(DataUtils.toString(identity));
    }
    methodList.add(testMethod);
}
 
Example #12
Source File: PlatformInterceptor.java    From Selenium-Foundation with Apache License 2.0 6 votes vote down vote up
/**
 * Assemble a list of methods that support the current target platform
 *
 * @param methods list of methods that are about the be run
 * @param context test context
 */
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    List<IMethodInstance> result = new ArrayList<>();
    String contextPlatform = getContextPlatform(context);

    // iterate over method list
    for (IMethodInstance thisMethod : methods) {
        Object platformConstant = resolveTargetPlatform(thisMethod);
        
        // if this method supports the current target platform
        if (TargetPlatformHandler.shouldRun(contextPlatform, (PlatformEnum) platformConstant)) {
            addMethodForPlatform(result, thisMethod, (PlatformEnum) platformConstant);
        }
    }

    if (result.isEmpty()) {
        logger.warn("No tests were found for context platform '{}'", contextPlatform);
    }

    // indicate intercept has been invoked
    context.setAttribute(INTERCEPT, Boolean.TRUE);
    return result;
}
 
Example #13
Source File: TestReorderInterceptor.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<IMethodInstance> intercept(final List<IMethodInstance> methods, final ITestContext context) {
    Collections.sort(methods, new Comparator<IMethodInstance>() {
        @Override
        public int compare(final IMethodInstance mi1, final IMethodInstance mi2) {
            // get test instances to order the tests.
            final Object o1 = mi1.getInstance();
            final Object o2 = mi2.getInstance();
            if (o1 instanceof ITest && o2 instanceof ITest) {
                return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName());
            }
            // something else, don't care about the order
            return 0;
        }
    });

    return methods;
}
 
Example #14
Source File: TestReorderInterceptor.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<IMethodInstance> intercept(final List<IMethodInstance> methods, final ITestContext context) {
    Collections.sort(methods, new Comparator<IMethodInstance>() {
        @Override
        public int compare(final IMethodInstance mi1, final IMethodInstance mi2) {
            // get test instances to order the tests.
            final Object o1 = mi1.getInstance();
            final Object o2 = mi2.getInstance();
            if (o1 instanceof ITest && o2 instanceof ITest) {
                return ((ITest)o1).getTestName().compareTo(((ITest)o2).getTestName());
            }
            // something else, don't care about the order
            return 0;
        }
    });

    return methods;
}
 
Example #15
Source File: PriorityMethodInterceptor.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    Comparator<IMethodInstance> comparator = new Comparator<IMethodInstance>() {
        private int getPriority(IMethodInstance mi) {
            int result = 0;
            Method method = mi.getMethod().getConstructorOrMethod().getMethod();
            Priority a1 = method.getAnnotation(Priority.class);
            if (a1 != null) {
                result = a1.value();
            } else {
                Class<?> cls = method.getDeclaringClass();
                Priority classPriority = cls.getAnnotation(Priority.class);
                if (classPriority != null) {
                    result = classPriority.value();
                }
            }
            return result;
        }

        public int compare(IMethodInstance m1, IMethodInstance m2) {
            return getPriority(m1) - getPriority(m2);
        }
    };

    IMethodInstance[] array = methods.toArray(new IMethodInstance[methods.size()]);
    Arrays.sort(array, comparator);
    Arrays.stream(array).forEach(method -> LOGGER.info("###test priority: "
            + method.getMethod().getMethodName()));
    return Arrays.asList(array);
}
 
Example #16
Source File: CarbonBasedTestListener.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void onAfterClass(ITestClass iTestClass, IMethodInstance iMethodInstance) {

    MockInitialContextFactory.destroy();
    MicroserviceServer microserviceServer = microserviceServerMap.get(iMethodInstance.getInstance());
    if (microserviceServer != null) {
        microserviceServer.stop();
        microserviceServer.destroy();
        microserviceServerMap.remove(iMethodInstance.getInstance());
    }
}
 
Example #17
Source File: MethodPriorityComparator.java    From qaf with MIT License 5 votes vote down vote up
private int getClassOrder(IMethodInstance mi) {
	int result = -1;
	Class<?> cls = mi.getMethod().getConstructorOrMethod().getMethod().getDeclaringClass();
	Priority classPriority = cls.getAnnotation(Priority.class);
	if (classPriority != null) {
		result = classPriority.value();
	}

	return result;
}
 
Example #18
Source File: MethodPriorityComparator.java    From qaf with MIT License 5 votes vote down vote up
private int getMethodOrder(IMethodInstance mi) {
	int result = -1;
	Method method = mi.getMethod().getConstructorOrMethod().getMethod();
	Priority a1 = method.getAnnotation(Priority.class);

	if (a1 != null) {
		result = a1.value();
	}
	return result;
}
 
Example #19
Source File: QAFTestNGListener.java    From qaf with MIT License 5 votes vote down vote up
public List<IMethodInstance> intercept(List<IMethodInstance> lst,
		ITestContext context) {
	logger.debug("Method Order interceptor called");
	String order = context.getCurrentXmlTest().getParameter("groupOrder");
	MethodPriorityComparator comparator = new MethodPriorityComparator(order);
	Collections.sort(lst, comparator);
	return lst;
}
 
Example #20
Source File: PriorityInterceptor.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
private static int getClassPriority(IMethodInstance mi) {
    Class<?> cls = getDeclaringClassOfMethod(mi);
    Priority classPriority = cls.getAnnotation(Priority.class);
    if (classPriority != null) {
        return classPriority.value();
    }
    return 0;
}
 
Example #21
Source File: PriorityInterceptor.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
private static int getMethodPriority(IMethodInstance mi) {
    Method method = getMethod(mi);
    Priority a1 = method.getAnnotation(Priority.class);
    if (a1 != null) {
        return a1.value();
    }
    return 0;
}
 
Example #22
Source File: PriorityInterceptor.java    From teammates with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    methods.sort(COMPARATOR);
    return methods;
}
 
Example #23
Source File: MethodPriorityComparator.java    From qaf with MIT License 4 votes vote down vote up
@Override
public int compare(IMethodInstance o1, IMethodInstance o2) {
	log.debug(o1.getMethod().getMethodName() + " O2: " + o2.getMethod().getMethodName() + " Order : "
			+ orderedGroups);
	String method1Name = o1.getMethod().getMethodName();
	String method2Name = o2.getMethod().getMethodName();
	Set<String> depends, o1Set, o2Set, o1Dependency, o2Dependency;

	// check method dependency
	o1Dependency = new HashSet<String>(Arrays.asList(o1.getMethod().getMethodsDependedUpon()));
	o2Dependency = new HashSet<String>(Arrays.asList(o1.getMethod().getMethodsDependedUpon()));
	if (o1Dependency.contains(method2Name)) {
		return -1;
	}
	if (o2Dependency.contains(method1Name)) {
		return 1;
	}
	// check group dependency
	o1Set = new HashSet<String>(Arrays.asList(o1.getMethod().getGroups()));
	o2Set = new HashSet<String>(Arrays.asList(o1.getMethod().getGroups()));
	o1Dependency = new HashSet<String>(Arrays.asList(o1.getMethod().getGroupsDependedUpon()));
	o2Dependency = new HashSet<String>(Arrays.asList(o1.getMethod().getGroupsDependedUpon()));

	depends = new HashSet<String>(o1Dependency);
	depends.retainAll(o2Set);
	if (!depends.isEmpty()) {
		return -1;
	}
	depends = new HashSet<String>(o2Dependency);
	depends.retainAll(o1Set);
	if (!depends.isEmpty()) {
		return 1;
	}
	int o1Priority = getGroupOrder(o1);
	int o2Priority = getGroupOrder(o2);
	if (o1Priority == o2Priority) {
		o1Priority = getClassOrder(o1);
		o2Priority = getClassOrder(o2);
		if (o1Priority == o2Priority) {
			o1Priority = getMethodOrder(o1);
			o2Priority = getMethodOrder(o2);
		}
	}
	if (o1Priority == -1) {
		o1Priority = o2Priority + 1;
	}
	if (o2Priority == -1) {
		o2Priority = o1Priority + 1;
	}
	return o1Priority - o2Priority;

}
 
Example #24
Source File: PriorityInterceptor.java    From teammates with GNU General Public License v2.0 4 votes vote down vote up
private static String getClassName(IMethodInstance mi) {
    return getDeclaringClassOfMethod(mi).getName();
}
 
Example #25
Source File: PriorityInterceptor.java    From teammates with GNU General Public License v2.0 4 votes vote down vote up
private static String getPackageName(IMethodInstance mi) {
    return getDeclaringClassOfMethod(mi).getPackage().getName();
}
 
Example #26
Source File: PriorityInterceptor.java    From teammates with GNU General Public License v2.0 4 votes vote down vote up
private static Class<?> getDeclaringClassOfMethod(IMethodInstance mi) {
    return mi.getMethod().getRealClass();
}
 
Example #27
Source File: PriorityInterceptor.java    From teammates with GNU General Public License v2.0 4 votes vote down vote up
private static Method getMethod(IMethodInstance mi) {
    return mi.getMethod().getConstructorOrMethod().getMethod();
}
 
Example #28
Source File: ReactiveStreamsArquillianTck.java    From microprofile-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
@Test
public void runAllTckTests() throws Throwable {
    TestNG testng = new TestNG();

    ObjectFactoryImpl delegate = new ObjectFactoryImpl();
    testng.setObjectFactory((IObjectFactory) (constructor, params) -> {
        if (constructor.getDeclaringClass().equals(ReactiveStreamsCdiTck.class)) {
            return tck;
        }
        else {
            return delegate.newInstance(constructor, params);
        }
    });

    testng.setUseDefaultListeners(false);
    ResultListener resultListener = new ResultListener();
    testng.addListener((ITestNGListener) resultListener);
    testng.setTestClasses(new Class[]{ ReactiveStreamsCdiTck.class });
    testng.setMethodInterceptor(new IMethodInterceptor() {
        @Override
        public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
            methods.sort(Comparator.comparing(m -> m.getInstance().getClass().getName()));
            return methods;
        }
    });
    testng.run();
    int total = resultListener.success.get() + resultListener.failed.get() + resultListener.skipped.get();
    System.out.println(String.format("Ran %d tests, %d passed, %d failed, %d skipped.", total, resultListener.success.get(),
        resultListener.failed.get(), resultListener.skipped.get()));
    System.out.println("Failed tests:");
    resultListener.failures.forEach(result -> {
        System.out.println(result.getInstance().getClass().getName() + "." + result.getMethod().getMethodName());
    });
    if (resultListener.failed.get() > 0) {
        if (resultListener.lastFailure.get() != null) {
            throw resultListener.lastFailure.get();
        }
        else {
            throw new Exception("Tests failed with no exception");
        }
    }
}
 
Example #29
Source File: TestIdUtils.java    From frameworkium-core with Apache License 2.0 2 votes vote down vote up
/**
 * Get TMS Link or Issue ID value.
 *
 * @param iMethod the {@link IMethodInstance} to check for test ID annotations.
 * @return Optional of either the {@link TmsLink} or {@link Issue} value.
 * @throws IllegalStateException if {@link TmsLink} and {@link Issue}
 *                               are specified inconstantly.
 */
public static Optional<String> getIssueOrTmsLinkValue(IMethodInstance iMethod) {
    Method method = iMethod.getMethod().getConstructorOrMethod().getMethod();
    return getIssueOrTmsLinkValue(method);
}
 
Example #30
Source File: PlatformInterceptor.java    From Selenium-Foundation with Apache License 2.0 2 votes vote down vote up
/**
 * Get the target platform annotation for the specified method
 *
 * @param testMethod method for which annotation is to be retrieved
 * @return {@link TargetPlatform} annotation; 'null' if absent
 */
private static TargetPlatform getTargetPlatform(IMethodInstance testMethod) {
    Method realMethod = testMethod.getMethod().getConstructorOrMethod().getMethod();
    return realMethod.getAnnotation(TargetPlatform.class);
}