net.jqwik.engine.execution.AbstractLifecycleContext Maven / Gradle / Ivy
package net.jqwik.engine.execution;
import java.lang.annotation.*;
import java.util.*;
import java.util.function.*;
import org.junit.platform.commons.support.*;
import org.junit.platform.engine.*;
import net.jqwik.api.*;
import net.jqwik.api.lifecycle.*;
import net.jqwik.engine.descriptor.*;
import net.jqwik.engine.support.*;
import static net.jqwik.engine.execution.LifecycleContextSupport.*;
abstract class AbstractLifecycleContext implements LifecycleContext {
private Reporter reporter;
private final TestDescriptor descriptor;
protected AbstractLifecycleContext(Reporter reporter, TestDescriptor descriptor) {
this.reporter = reporter;
this.descriptor = descriptor;
}
@Override
public Reporter reporter() {
return reporter;
}
@Override
public void wrapReporter(Function wrapper) {
this.reporter = wrapper.apply(this.reporter);
}
@Override
public String label() {
return descriptor.getDisplayName();
}
@Override
public Optional findAnnotation(Class annotationClass) {
return optionalElement()
.flatMap(element -> AnnotationSupport.findAnnotation(element, annotationClass));
}
@Override
public List findAnnotationsInContainer(Class annotationClass) {
return optionalElement()
.map(element -> {
List annotations = new ArrayList<>();
appendAnnotations(parentContainer(), annotationClass, annotations);
return annotations;
})
.orElse(Collections.emptyList());
}
@Override
public List findRepeatableAnnotations(Class annotationClass) {
return AnnotationSupport.findRepeatableAnnotations(optionalElement(), annotationClass);
}
private Optional parentContainer() {
return parentContainer(descriptor);
}
private Optional parentContainer(TestDescriptor descriptor) {
return descriptor.getParent()
.filter(parent -> parent instanceof ContainerClassDescriptor)
.map(parent -> (ContainerClassDescriptor) parent);
}
private void appendAnnotations(
Optional optionalContainer,
Class annotationClass,
List annotations
) {
optionalContainer.ifPresent(container -> {
annotations.addAll(JqwikAnnotationSupport.findContainerAnnotations(container.getContainerClass(), annotationClass));
appendAnnotations(parentContainer(container), annotationClass, annotations);
});
}
protected String toString(Class extends LifecycleContext> contextType) {
String uniqueIdDescription = formatUniqueId(descriptor.getUniqueId());
return String.format("%s(%s)", contextType.getSimpleName(), uniqueIdDescription);
}
}