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

org.junit.jupiter.engine.descriptor.ClassExtensionContext Maven / Gradle / Ivy

There is a newer version: 5.11.3
Show newest version
/*
 * Copyright 2015-2020 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 v2.0 which
 * accompanies this distribution and is available at
 *
 * https://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.engine.descriptor;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Optional;

import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestInstances;
import org.junit.jupiter.engine.config.JupiterConfiguration;
import org.junit.platform.engine.EngineExecutionListener;
import org.junit.platform.engine.support.hierarchical.ThrowableCollector;

/**
 * @since 5.0
 */
final class ClassExtensionContext extends AbstractExtensionContext {

	private final Lifecycle lifecycle;

	private final ThrowableCollector throwableCollector;

	private TestInstances testInstances;

	/**
	 * Create a new {@code ClassExtensionContext} with {@link Lifecycle#PER_METHOD}.
	 *
	 * @see #ClassExtensionContext(ExtensionContext, EngineExecutionListener, ClassBasedTestDescriptor, Lifecycle, JupiterConfiguration, ThrowableCollector)
	 */
	ClassExtensionContext(ExtensionContext parent, EngineExecutionListener engineExecutionListener,
			ClassBasedTestDescriptor testDescriptor, JupiterConfiguration configuration,
			ThrowableCollector throwableCollector) {

		this(parent, engineExecutionListener, testDescriptor, Lifecycle.PER_METHOD, configuration, throwableCollector);
	}

	ClassExtensionContext(ExtensionContext parent, EngineExecutionListener engineExecutionListener,
			ClassBasedTestDescriptor testDescriptor, Lifecycle lifecycle, JupiterConfiguration configuration,
			ThrowableCollector throwableCollector) {

		super(parent, engineExecutionListener, testDescriptor, configuration);

		this.lifecycle = lifecycle;
		this.throwableCollector = throwableCollector;
	}

	@Override
	public Optional getElement() {
		return Optional.of(getTestDescriptor().getTestClass());
	}

	@Override
	public Optional> getTestClass() {
		return Optional.of(getTestDescriptor().getTestClass());
	}

	@Override
	public Optional getTestInstanceLifecycle() {
		return Optional.of(this.lifecycle);
	}

	@Override
	public Optional getTestInstance() {
		return getTestInstances().map(TestInstances::getInnermostInstance);
	}

	@Override
	public Optional getTestInstances() {
		return Optional.ofNullable(testInstances);
	}

	void setTestInstances(TestInstances testInstances) {
		this.testInstances = testInstances;
	}

	@Override
	public Optional getTestMethod() {
		return Optional.empty();
	}

	@Override
	public Optional getExecutionException() {
		return Optional.ofNullable(this.throwableCollector.getThrowable());
	}

}