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;
final class ClassExtensionContext extends AbstractExtensionContext<ClassBasedTestDescriptor> {
private final Lifecycle lifecycle;
private final ThrowableCollector throwableCollector;
private TestInstances testInstances;
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<AnnotatedElement> getElement() {
return Optional.of(getTestDescriptor().getTestClass());
}
@Override
public Optional<Class<?>> getTestClass() {
return Optional.of(getTestDescriptor().getTestClass());
}
@Override
public Optional<Lifecycle> getTestInstanceLifecycle() {
return Optional.of(this.lifecycle);
}
@Override
public Optional<Object> getTestInstance() {
return getTestInstances().map(TestInstances::getInnermostInstance);
}
@Override
public Optional<TestInstances> getTestInstances() {
return Optional.ofNullable(testInstances);
}
void setTestInstances(TestInstances testInstances) {
this.testInstances = testInstances;
}
@Override
public Optional<Method> getTestMethod() {
return Optional.empty();
}
@Override
public Optional<Throwable> getExecutionException() {
return Optional.ofNullable(this.throwableCollector.getThrowable());
}
}