Search in sources :

Example 1 with MethodModel

use of com.carrotsearch.randomizedtesting.ClassModel.MethodModel in project lucene-solr by apache.

the class LuceneJUnit3MethodProvider method getTestMethods.

@Override
public Collection<Method> getTestMethods(Class<?> suiteClass, ClassModel classModel) {
    Map<Method, MethodModel> methods = classModel.getMethods();
    ArrayList<Method> result = new ArrayList<>();
    for (MethodModel mm : methods.values()) {
        // Skip any methods that have overrieds/ shadows.
        if (mm.getDown() != null)
            continue;
        Method m = mm.element;
        if (m.getName().startsWith("test") && Modifier.isPublic(m.getModifiers()) && !Modifier.isStatic(m.getModifiers()) && m.getParameterTypes().length == 0) {
            result.add(m);
        }
    }
    return result;
}
Also used : MethodModel(com.carrotsearch.randomizedtesting.ClassModel.MethodModel) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 2 with MethodModel

use of com.carrotsearch.randomizedtesting.ClassModel.MethodModel in project randomizedtesting by randomizedtesting.

the class JUnit3MethodProvider method getTestMethods.

@Override
public Collection<Method> getTestMethods(Class<?> suiteClass, ClassModel suiteClassModel) {
    Map<Method, MethodModel> methods = suiteClassModel.getMethods();
    ArrayList<Method> result = new ArrayList<Method>();
    for (MethodModel mm : methods.values()) {
        if (mm.getDown() == null && mm.element.getName().startsWith("test")) {
            result.add(mm.element);
        }
    }
    return result;
}
Also used : MethodModel(com.carrotsearch.randomizedtesting.ClassModel.MethodModel) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 3 with MethodModel

use of com.carrotsearch.randomizedtesting.ClassModel.MethodModel in project randomizedtesting by randomizedtesting.

the class NoShadowingOrOverridesOnMethodsRule method checkNoShadowsOrOverrides.

private void checkNoShadowsOrOverrides(Class<?> clazz, ClassModel classModel, Class<? extends Annotation> ann) {
    Map<Method, MethodModel> annotatedLeafMethods = classModel.getAnnotatedLeafMethods(ann);
    StringBuilder b = new StringBuilder();
    for (Map.Entry<Method, MethodModel> e : annotatedLeafMethods.entrySet()) {
        if (verify(e.getKey())) {
            MethodModel mm = e.getValue();
            if (mm.getDown() != null || mm.getUp() != null) {
                b.append("Methods annotated with @" + ann.getName() + " shadow or override each other:\n");
                while (mm.getUp() != null) {
                    mm = mm.getUp();
                }
                while (mm != null) {
                    b.append("  - ");
                    if (mm.element.isAnnotationPresent(ann))
                        b.append("@").append(ann.getSimpleName()).append(" ");
                    b.append(signature(mm.element)).append("\n");
                    mm = mm.getDown();
                }
            }
        }
    }
    if (b.length() > 0) {
        throw new RuntimeException("There are overridden methods annotated with " + ann.getName() + ". These methods would not be executed by JUnit and need to manually chain themselves which can lead to" + " maintenance problems. Consider using different method names or make hook methods private.\n" + b.toString().trim());
    }
}
Also used : MethodModel(com.carrotsearch.randomizedtesting.ClassModel.MethodModel) Method(java.lang.reflect.Method) Map(java.util.Map)

Example 4 with MethodModel

use of com.carrotsearch.randomizedtesting.ClassModel.MethodModel in project randomizedtesting by randomizedtesting.

the class RandomizedRunner method collectMethodExecutions.

/**
 * Collect test method executions from list of test methods and
 * potentially parameters from parameter factory methods.
 */
public List<TestMethodExecution> collectMethodExecutions(Constructor<?> constructor, List<Method> testMethods) {
    final List<TestMethodExecution> testCases = new ArrayList<>();
    String argFormattingTemplate = createDefaultArgumentFormatting(constructor);
    final Map<Method, MethodModel> factoryMethods = classModel.getAnnotatedLeafMethods(ParametersFactory.class);
    if (factoryMethods.isEmpty()) {
        Object[] noArgs = new Object[0];
        InstanceProvider instanceProvider = getInstanceProvider(constructor, noArgs);
        for (Method testMethod : testMethods) {
            testCases.add(new TestMethodExecution(testMethod, argFormattingTemplate, noArgs, instanceProvider));
        }
    } else {
        for (Method factoryMethod : factoryMethods.keySet()) {
            Validation.checkThat(factoryMethod).isStatic().isPublic();
            if (!Iterable.class.isAssignableFrom(factoryMethod.getReturnType())) {
                throw new RuntimeException("@" + ParametersFactory.class.getSimpleName() + " annotated " + "methods must be public, static and returning Iterable<Object[]>:" + factoryMethod);
            }
            ParametersFactory pfAnnotation = factoryMethod.getAnnotation(ParametersFactory.class);
            if (!pfAnnotation.argumentFormatting().equals(ParametersFactory.DEFAULT_FORMATTING)) {
                argFormattingTemplate = pfAnnotation.argumentFormatting();
            }
            List<Object[]> args = new ArrayList<>();
            try {
                Iterable<?> factoryArguments = Iterable.class.cast(factoryMethod.invoke(null));
                for (Object o : factoryArguments) {
                    if (!(o instanceof Object[])) {
                        throw new RuntimeException("Expected Object[] for each set of constructor arguments: " + o);
                    }
                    args.add((Object[]) o);
                }
            } catch (InvocationTargetException e) {
                if (isAssumptionViolated(e.getCause())) {
                    return Collections.emptyList();
                }
                Rethrow.rethrow(e.getCause());
            } catch (Throwable t) {
                throw new RuntimeException("Error collecting parameters from: " + factoryMethod, t);
            }
            if (pfAnnotation.shuffle()) {
                Collections.shuffle(args, new Random(runnerRandomness.getSeed()));
            }
            for (Object[] constructorArgs : args) {
                InstanceProvider instanceProvider = getInstanceProvider(constructor, constructorArgs);
                for (Method testMethod : testMethods) {
                    testCases.add(new TestMethodExecution(testMethod, argFormattingTemplate, constructorArgs, instanceProvider));
                }
            }
        }
    }
    return testCases;
}
Also used : MethodModel(com.carrotsearch.randomizedtesting.ClassModel.MethodModel) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) InvocationTargetException(java.lang.reflect.InvocationTargetException) Random(java.util.Random) ParametersFactory(com.carrotsearch.randomizedtesting.annotations.ParametersFactory) TestCaseInstanceProvider(com.carrotsearch.randomizedtesting.annotations.TestCaseInstanceProvider)

Aggregations

MethodModel (com.carrotsearch.randomizedtesting.ClassModel.MethodModel)4 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)3 ParametersFactory (com.carrotsearch.randomizedtesting.annotations.ParametersFactory)1 TestCaseInstanceProvider (com.carrotsearch.randomizedtesting.annotations.TestCaseInstanceProvider)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 Random (java.util.Random)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1