org.junit.jupiter.engine.extension.ExtensionRegistry Maven / Gradle / Ivy
Show all versions of junit-jupiter-engine Show documentation
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.jupiter.engine.extension;
import static org.junit.platform.commons.meta.API.Usage.Internal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.Extension;
import org.junit.platform.commons.meta.API;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.commons.util.ReflectionUtils;
/**
* An {@code ExtensionRegistry} holds all registered extensions (i.e.
* instances of {@link Extension}) for a given
* {@link org.junit.platform.engine.support.hierarchical.Node}.
*
* A registry has a reference to its parent registry, and all lookups are
* performed first in the current registry itself and then recursively in its
* ancestors.
*
* @since 5.0
*/
@API(Internal)
public class ExtensionRegistry {
private static final Logger LOG = Logger.getLogger(ExtensionRegistry.class.getName());
private static final List DEFAULT_EXTENSIONS = Collections.unmodifiableList(
Arrays.asList(new DisabledCondition(), new TestInfoParameterResolver(), new TestReporterParameterResolver()));
/**
* Factory for creating and populating a new root registry with the default
* extensions.
*
* @return a new {@code ExtensionRegistry}
*/
public static ExtensionRegistry createRegistryWithDefaultExtensions() {
ExtensionRegistry extensionRegistry = new ExtensionRegistry(null);
DEFAULT_EXTENSIONS.forEach(extensionRegistry::registerDefaultExtension);
return extensionRegistry;
}
/**
* Factory for creating and populating a new registry from a list of
* extension types and a parent registry.
*
* @param parentRegistry the parent registry
* @param extensionTypes the types of extensions to be registered in
* the new registry
* @return a new {@code ExtensionRegistry}
*/
public static ExtensionRegistry createRegistryFrom(ExtensionRegistry parentRegistry,
List> extensionTypes) {
Preconditions.notNull(parentRegistry, "parentRegistry must not be null");
ExtensionRegistry registry = new ExtensionRegistry(parentRegistry);
extensionTypes.forEach(registry::registerExtension);
return registry;
}
private final ExtensionRegistry parent;
private final Set> registeredExtensionTypes = new LinkedHashSet<>();
private final List registeredExtensions = new ArrayList<>();
private ExtensionRegistry(ExtensionRegistry parent) {
this.parent = parent;
}
/**
* Get all {@code Extensions} in this registry or one of its ancestors of
* the specified type.
*
* @param extensionType the type of {@link Extension} to get
* @see #getReversedExtensions(Class)
* @see #stream(Class)
*/
public List getExtensions(Class extensionType) {
List extensions = new ArrayList<>();
if (this.parent != null) {
extensions.addAll(this.parent.getExtensions(extensionType));
}
// @formatter:off
this.registeredExtensions.stream()
.filter(extension -> extensionType.isAssignableFrom(extension.getClass()))
.map(extensionType::cast)
.forEach(extensions::add);
// @formatter:on
return extensions;
}
/**
* Get all {@code Extensions} in this registry or one of its ancestors of
* the specified type, in reverse order.
*
* @param extensionType the type of {@link Extension} to get
* @see #getExtensions(Class)
* @see #stream(Class)
*/
public List getReversedExtensions(Class extensionType) {
List extensions = getExtensions(extensionType);
Collections.reverse(extensions);
return extensions;
}
/**
* Generate a stream for iterating over all registered extensions of the
* specified type.
*
* @param extensionType the type of {@link Extension} to stream
* @see #getExtensions(Class)
* @see #getReversedExtensions(Class)
*/
public Stream stream(Class extensionType) {
return getExtensions(extensionType).stream();
}
/**
* Determine if the supplied type is already registered in this registry or in a
* parent registry.
*/
private boolean isAlreadyRegistered(Class extensionType) {
return (this.registeredExtensionTypes.contains(extensionType)
|| (this.parent != null && this.parent.isAlreadyRegistered(extensionType)));
}
/**
* Instantiate an extension of the given type using its default constructor
* and register it in this registry.
*
* A new {@link Extension} will not be registered if an extension of the
* given type already exists in this registry or a parent registry.
*
* @param extensionType the type of extension to register
*/
void registerExtension(Class extensionType) {
if (!isAlreadyRegistered(extensionType)) {
registerExtension(ReflectionUtils.newInstance(extensionType));
this.registeredExtensionTypes.add(extensionType);
}
}
private void registerDefaultExtension(Extension extension) {
this.registeredExtensions.add(extension);
this.registeredExtensionTypes.add(extension.getClass());
}
private void registerExtension(Extension extension) {
registerExtension(extension, extension);
}
/**
* Register the supplied {@link Extension} in this registry, without checking
* if an extension of that type already exists in this registry.
*
*
Semantics for Source
* If an extension is registered declaratively via {@link ExtendWith @ExtendWith},
* the {@code source} and the {@code extension} should be the same object. However,
* if an extension is registered programmatically — for example, as a lambda
* expression or method reference — the {@code source} object should be the
* underlying {@link java.lang.reflect.Method} that implements the extension
* API, or similar.
*
* @param extension the extension to register
* @param source the source of the extension
*/
public void registerExtension(Extension extension, Object source) {
LOG.finer(() -> String.format("Registering extension [%s] from source [%s].", extension, source));
this.registeredExtensions.add(extension);
}
}