/** * @since 5.4 */ private static int getOrder(Field field) { return findAnnotation(field, Order.class).map(Order::value).orElse(Integer.MAX_VALUE); }
protected Optional<ExecutionMode> getExecutionModeFromAnnotation(AnnotatedElement element) { // @formatter:off return findAnnotation(element, Execution.class) .map(Execution::value) .map(JupiterTestDescriptor::toExecutionMode); // @formatter:on }
/** * Find the first {@code DisplayNameGeneration} annotation that is either * <em>directly present</em>, <em>meta-present</em>, <em>indirectly present</em> * on the supplied {@code testClass} or on an enclosing class. */ 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(); } }
@Override public <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType) { return AnnotationUtils.findAnnotation(getEffectiveAnnotatedParameter(), annotationType); }
/** * Containers/tests are disabled if {@code @Disabled} is present on the test * class or method. */ @Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Optional<AnnotatedElement> element = context.getElement(); Optional<Disabled> disabled = findAnnotation(element, Disabled.class); if (disabled.isPresent()) { String reason = disabled.map(Disabled::value).filter(StringUtils::isNotBlank).orElseGet( () -> element.get() + " is @Disabled"); return ConditionEvaluationResult.disabled(reason); } return ENABLED; }
static TestInstance.Lifecycle getTestInstanceLifecycle(Class<?> testClass, JupiterConfiguration configuration) { Preconditions.notNull(testClass, "testClass must not be null"); Preconditions.notNull(configuration, "configuration must not be null"); // @formatter:off return AnnotationUtils.findAnnotation(testClass, TestInstance.class) .map(TestInstance::value) .orElseGet(configuration::getDefaultTestInstanceLifecycle); // @formatter:on }
@Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Optional<DisabledOnJre> optional = findAnnotation(context.getElement(), DisabledOnJre.class); if (optional.isPresent()) { JRE[] versions = optional.get().value(); Preconditions.condition(versions.length > 0, "You must declare at least one JRE in @DisabledOnJre"); return (Arrays.stream(versions).anyMatch(JRE::isCurrentVersion)) ? DISABLED_ON_CURRENT_JRE : ENABLED_ON_CURRENT_JRE; } return ENABLED_BY_DEFAULT; }
@Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Optional<EnabledOnJre> optional = findAnnotation(context.getElement(), EnabledOnJre.class); if (optional.isPresent()) { JRE[] versions = optional.get().value(); Preconditions.condition(versions.length > 0, "You must declare at least one JRE in @EnabledOnJre"); return (Arrays.stream(versions).anyMatch(JRE::isCurrentVersion)) ? ENABLED_ON_CURRENT_JRE : DISABLED_ON_CURRENT_JRE; } return ENABLED_BY_DEFAULT; }
@Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Optional<EnabledOnOs> optional = findAnnotation(context.getElement(), EnabledOnOs.class); if (optional.isPresent()) { OS[] operatingSystems = optional.get().value(); Preconditions.condition(operatingSystems.length > 0, "You must declare at least one OS in @EnabledOnOs"); return (Arrays.stream(operatingSystems).anyMatch(OS::isCurrentOs)) ? ENABLED_ON_CURRENT_OS : DISABLED_ON_CURRENT_OS; } return ENABLED_BY_DEFAULT; }
@Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Optional<DisabledOnOs> optional = findAnnotation(context.getElement(), DisabledOnOs.class); if (optional.isPresent()) { OS[] operatingSystems = optional.get().value(); Preconditions.condition(operatingSystems.length > 0, "You must declare at least one OS in @DisabledOnOs"); return (Arrays.stream(operatingSystems).anyMatch(OS::isCurrentOs)) ? DISABLED_ON_CURRENT_OS : ENABLED_ON_CURRENT_OS; } return ENABLED_BY_DEFAULT; }
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T initialize(AnnotatedElement annotatedElement, T instance) { if (instance instanceof AnnotationConsumer) { Method method = findMethods(instance.getClass(), isAnnotationConsumerAcceptMethod, BOTTOM_UP).get(0); Class<? extends Annotation> annotationType = (Class<? extends Annotation>) method.getParameterTypes()[0]; Annotation annotation = AnnotationUtils.findAnnotation(annotatedElement, annotationType) // .orElseThrow(() -> new JUnitException(instance.getClass().getName() + " must be used with an annotation of type " + annotationType.getName())); initializeAnnotationConsumer((AnnotationConsumer) instance, annotation); } return instance; }
@Override Resolver createResolver(ParameterContext parameterContext) { try { // @formatter:off return AnnotationUtils.findAnnotation(parameterContext.getParameter(), AggregateWith.class) .map(AggregateWith::value) .map(clazz -> (ArgumentsAggregator) ReflectionSupport.newInstance(clazz)) .map(Aggregator::new) .orElse(Aggregator.DEFAULT); } // @formatter:on catch (Exception ex) { throw parameterResolutionException("Error creating ArgumentsAggregator", ex, parameterContext); } } };
@Override public <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType) { Preconditions.notNull(annotationType, "annotationType must not be null"); return AnnotationUtils.findAnnotation(getMethod(), annotationType); }
private ParameterizedTestNameFormatter createNameFormatter(Method templateMethod, String displayName) { ParameterizedTest parameterizedTest = findAnnotation(templateMethod, ParameterizedTest.class).get(); String pattern = Preconditions.notBlank(parameterizedTest.name().trim(), () -> String.format( "Configuration error: @ParameterizedTest on method [%s] must be declared with a non-empty name.", templateMethod)); return new ParameterizedTestNameFormatter(pattern, displayName); }
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(); // TODO [#242] Replace logging with precondition check once we have a proper mechanism for // handling validation exceptions during the TestEngine discovery phase. 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; } } // else let a 'DisplayNameGenerator' generate a display name return displayNameSupplier.get(); }
@Override Resolver createResolver(ParameterContext parameterContext) { try { // @formatter:off return AnnotationUtils.findAnnotation(parameterContext.getParameter(), ConvertWith.class) .map(ConvertWith::value) .map(clazz -> (ArgumentConverter) ReflectionUtils.newInstance(clazz)) .map(converter -> AnnotationConsumerInitializer.initialize(parameterContext.getParameter(), converter)) .map(Converter::new) .orElse(Converter.DEFAULT); } // @formatter:on catch (Exception ex) { throw parameterResolutionException("Error creating ArgumentConverter", ex, parameterContext); } } },
@Override public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) { Method testMethod = context.getRequiredTestMethod(); String displayName = context.getDisplayName(); RepeatedTest repeatedTest = AnnotationUtils.findAnnotation(testMethod, RepeatedTest.class).get(); int totalRepetitions = totalRepetitions(repeatedTest, testMethod); RepeatedTestDisplayNameFormatter formatter = displayNameFormatter(repeatedTest, testMethod, displayName); // @formatter:off return IntStream .rangeClosed(1, totalRepetitions) .mapToObj(repetition -> new RepeatedTestInvocationContext(repetition, totalRepetitions, formatter)); // @formatter:on }
private Script createDisabledIfScriptOrNull(AnnotatedElement annotatedElement) { Optional<DisabledIf> disabled = findAnnotation(annotatedElement, DisabledIf.class); if (!disabled.isPresent()) { return null; } DisabledIf annotation = disabled.get(); String source = createSource(annotation.value()); return new Script(annotation, annotation.engine(), source, annotation.reason()); }
private Script createEnabledIfScriptOrNull(AnnotatedElement annotatedElement) { Optional<EnabledIf> enabled = findAnnotation(annotatedElement, EnabledIf.class); if (!enabled.isPresent()) { return null; } EnabledIf annotation = enabled.get(); String source = createSource(annotation.value()); return new Script(annotation, annotation.engine(), source, annotation.reason()); }
@Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { Optional<EnabledIfEnvironmentVariable> optional = findAnnotation(context.getElement(), EnabledIfEnvironmentVariable.class); if (!optional.isPresent()) { return ENABLED_BY_DEFAULT; } EnabledIfEnvironmentVariable annotation = optional.get(); String name = annotation.named().trim(); String regex = annotation.matches(); Preconditions.notBlank(name, () -> "The 'named' attribute must not be blank in " + annotation); Preconditions.notBlank(regex, () -> "The 'matches' attribute must not be blank in " + annotation); String actual = getEnvironmentVariable(name); // Nothing to match against? if (actual == null) { return disabled(format("Environment variable [%s] does not exist", name)); } if (actual.matches(regex)) { return enabled(format("Environment variable [%s] with value [%s] matches regular expression [%s]", name, actual, regex)); } return disabled(format("Environment variable [%s] with value [%s] does not match regular expression [%s]", name, actual, regex)); }