package org.junit.jupiter.engine.descriptor;
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.function.Supplier;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.DisplayNameGenerator.Standard;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.util.StringUtils;
final class DisplayNameUtils {
private static final Logger logger = LoggerFactory.getLogger(DisplayNameUtils.class);
private static final DisplayNameGenerator standardGenerator = new Standard();
private static final DisplayNameGenerator replaceUnderscoresGenerator = new ReplaceUnderscores();
static String determineDisplayName(AnnotatedElement element, Supplier<String> displayNameSupplier) {
Preconditions.notNull(element, "Annotated element must not be null");
Optional<DisplayName> displayNameAnnotation = findAnnotation(element, DisplayName.class);
if (displayNameAnnotation.isPresent()) {
String displayName = displayNameAnnotation.get().value().trim();
if (StringUtils.isBlank(displayName)) {
logger.warn(() -> String.format(
"Configuration error: @DisplayName on [%s] must be declared with a non-empty value.", element));
}
else {
return displayName;
}
}
return displayNameSupplier.get();
}
static String determineDisplayNameForMethod(Class<?> testClass, Method testMethod) {
DisplayNameGenerator generator = getDisplayNameGenerator(testClass);
return determineDisplayName(testMethod, () -> generator.generateDisplayNameForMethod(testClass, testMethod));
}
static Supplier<String> createDisplayNameSupplierForClass(Class<?> testClass) {
return () -> getDisplayNameGenerator(testClass).generateDisplayNameForClass(testClass);
}
static Supplier<String> createDisplayNameSupplierForNestedClass(Class<?> testClass) {
return () -> getDisplayNameGenerator(testClass).generateDisplayNameForNestedClass(testClass);
}
private static DisplayNameGenerator getDisplayNameGenerator(Class<?> testClass) {
Preconditions.notNull(testClass, "Test class must not be null");
DisplayNameGeneration generation = getDisplayNameGeneration(testClass).orElse(null);
if (generation == null) {
return standardGenerator;
}
Class<? extends DisplayNameGenerator> displayNameGeneratorClass = generation.value();
if (displayNameGeneratorClass == Standard.class) {
return standardGenerator;
}
if (displayNameGeneratorClass == ReplaceUnderscores.class) {
return replaceUnderscoresGenerator;
}
return ReflectionUtils.newInstance(displayNameGeneratorClass);
}
private static Optional<DisplayNameGeneration> getDisplayNameGeneration(Class<?> testClass) {
Class<?> candidate = testClass;
do {
Optional<DisplayNameGeneration> generation = findAnnotation(candidate, DisplayNameGeneration.class);
if (generation.isPresent()) {
return generation;
}
candidate = candidate.getEnclosingClass();
} while (candidate != null);
return Optional.empty();
}
}