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

patterntesting.runtime.junit.AbstractTester Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

There is a newer version: 2.5.0
Show newest version
/*
 * $Id: AbstractTester.java,v 1.5 2016/03/14 22:01:56 oboehm Exp $
 *
 * Copyright (c) 2015 by Oliver Boehm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 07.01.2015 by oliver ([email protected])
 */

package patterntesting.runtime.junit;

import java.lang.reflect.Modifier;
import java.util.*;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This is the common superclass for some tester classes defined in this
 * package.
 *
 * @author oliver
 * @version $Revision: 1.5 $
 * @since 1.6 (07.01.2015)
 */
public abstract class AbstractTester {

    private static final Logger LOG = LoggerFactory.getLogger(AbstractTester.class);

    /** Utility class - no need to instantiate it. */
    protected AbstractTester() {
    }

    /**
     * Removes "excluded" from the given classes. If one of the "excluded"
     * class is an interface or abstract class all implementing or subclasses
     * will be excluded.
     *
     * @param classes the classes
     * @param excluded the excluded
     */
    protected static void removeClasses(final Collection classes, final List excluded) {
        classes.removeAll(excluded);
        for (Object obj : excluded) {
            Class clazz = (Class) obj;
            if (clazz.isInterface() || isAbstract(clazz)) {
                removeAssignableClasses(classes, clazz);
            }
        }
    }

    private static boolean isAbstract(final Class clazz) {
        return Modifier.isAbstract(clazz.getModifiers());
    }

    private static void removeAssignableClasses(final Collection classes, final Class superclass) {
        Collection> toBeDeleted = new ArrayList>();
        for (Object obj : classes) {
            Class clazz = (Class) obj;
            if (superclass.isAssignableFrom(clazz)) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("removing " + clazz + " from list of classes...");
                }
                toBeDeleted.add(clazz);
            }
        }
        classes.removeAll(toBeDeleted);
    }

    /**
     * Removes "excluded" from the given classes.
     *
     * @param  the generic type
     * @param classes the classes
     * @param excluded the excluded
     */
    protected static  void removeClasses(final Collection> classes, final Pattern... excluded) {
        final List> toBeExcluded = new ArrayList>();
        for (Class clazz : classes) {
            if (matches(clazz, excluded)) {
                toBeExcluded.add(clazz);
            }
        }
        removeClasses(classes, toBeExcluded);
    }

    private static boolean matches(final Class clazz, final Pattern[] excluded) {
        String classname = clazz.getName();
        for (int i = 0; i < excluded.length; i++) {
            if (excluded[i].matcher(classname).matches()) {
                return true;
            }
        }
        return false;
    }

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy