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

de.bechte.junit.runners.model.TestClassPool Maven / Gradle / Ivy

Go to download

This is a runner implementation that supports context hierarchies in JUnit. For more details please visit: https://github.com/bechte/junit-hierarchicalcontextrunner/wiki

There is a newer version: 4.12.2
Show newest version
package de.bechte.junit.runners.model;

import org.junit.runners.model.TestClass;

import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * This pool of {@link TestClass}es is used to reduce the expensive look-ups and reflection calls that are performed
 * during the construction of the {@link TestClass} instances. The pool will keep a reference to each instance so that
 * no {@code TestClass} has to be constructed twice.
 *
 * Note: The additional memory consumption for keeping the instances is very low, but the pool results in a tremendous
 * speed-up in case of the {@link de.bechte.junit.runners.context.HierarchicalContextRunner}, when {@link TestClass}es
 * must be recreated several times along the context hierarchy.
 */
public final class TestClassPool {
    private static final Map, TestClass> testClasses =
            Collections.synchronizedMap(new WeakHashMap, TestClass>());

    // Avoid instantiation
    private TestClassPool() {
        super();
    }

    /**
     * Returns a {@link TestClass} wrapping the given {@code testClass}. {@link TestClass}es will be shared and held
     * within the pool, to avoid the expensive process that is performed during construction of the instances.
     *
     * @param testClass the {@link Class} to create an {@link TestClass} instance for
     * @return the {@link TestClass} instance
     * @throws IllegalArgumentException If {@code testClass} is {@code null}.
     */
    public static TestClass forClass(final Class testClass) {
        if (testClass == null)
            throw new IllegalArgumentException("TestClass must not be null!");

        if (!testClasses.containsKey(testClass))
            testClasses.put(testClass, new TestClass(testClass));

        return testClasses.get(testClass);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy