All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.junit.runners.model.Java8TestClass Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2015 Goldman Sachs.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.junit.runners.model;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.internal.MethodSorter;
import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.TestClass;

public class Java8TestClass extends TestClass
{
    /**
     * Creates a {@code TestClass} wrapping {@code klass}. Each time this
     * constructor executes, the class is scanned for annotations, which can be
     * an expensive process (we hope in future JDK's it will not be.) Therefore,
     * try to share instances of {@code TestClass} where possible.
     */
    public Java8TestClass(Class klass)
    {
        super(klass);
    }

    @Override
    protected void scanAnnotatedMembers(Map, List> methodsForAnnotations, Map, List> fieldsForAnnotations)
    {
        super.scanAnnotatedMembers(methodsForAnnotations, fieldsForAnnotations);

        this.getInterfaceMethodsForAnnotations(methodsForAnnotations, this.getJavaClass());
    }

    private void getInterfaceMethodsForAnnotations(Map, List> methodsForAnnotations, Class clazz)
    {
        List> interfaces = Java8TestClass.getInterfaces(this.getJavaClass());
        for (Class eachInterface : interfaces)
        {
            for (Method eachMethod : MethodSorter.getDeclaredMethods(eachInterface))
            {
                if (!Modifier.isAbstract(eachMethod.getModifiers()))
                {
                    TestClass.addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations);
                }
            }
        }
    }

    private static List> getInterfaces(Class testClass)
    {
        LinkedList> queue = new LinkedList>();
        queue.add(testClass);

        Set> visited = new HashSet>();
        visited.add(testClass);

        List> results = new ArrayList>();
        while (!queue.isEmpty())
        {
            Class anInterface = queue.poll();
            results.add(anInterface);

            Class[] parentInterfaces = anInterface.getInterfaces();
            for (Class parentInterface : parentInterfaces)
            {
                if (!visited.contains(parentInterface))
                {
                    visited.add(parentInterface);
                    queue.add(parentInterface);
                }
            }
        }
        return results;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy