/** * @since 5.4 */ private static int getOrder(Field field) { return findAnnotation(field, Order.class).map(Order::value).orElse(Integer.MAX_VALUE); }
/** * Determine if the supplied {@link Parameter} is an aggregator (i.e., of * type {@link ArgumentsAccessor} or annotated with {@link AggregateWith}). * * @return {@code true} if the parameter is an aggregator */ private static boolean isAggregator(Parameter parameter) { return ArgumentsAccessor.class.isAssignableFrom(parameter.getType()) || isAnnotated(parameter, AggregateWith.class); }
@Override public <A extends Annotation> List<A> findRepeatableAnnotations(Class<A> annotationType) { return AnnotationUtils.findRepeatableAnnotations(getEffectiveAnnotatedParameter(), annotationType); }
private List<Class> allMaterialConfigsWhichAreDataPointsInThisTest() throws Exception { List<Field> fields = AnnotationUtils.findAnnotatedFields(getClass(), DataPoint.class, field -> true); ArrayList<Class> allDataPointMaterialConfigClasses = new ArrayList<>(); for (Field field : fields) { allDataPointMaterialConfigClasses.add(field.get(this).getClass()); } return allDataPointMaterialConfigClasses; }
private List<Field> findAnnotatedFields(Object testInstance) { return findPublicAnnotatedFields(testInstance.getClass(), TestRule.class, Rule.class); }
private void injectFields(ExtensionContext context, Object testInstance, Predicate<Field> predicate) { findAnnotatedFields(context.getRequiredTestClass(), TempDir.class, predicate).forEach(field -> { assertValidFieldCandidate(field); try { makeAccessible(field).set(testInstance, getPathOrFile(field.getType(), context)); } catch (Throwable t) { ExceptionUtils.throwAsUncheckedException(t); } }); }
@Override protected List<Field> findRuleAnnotatedMembers(Object testInstance) { return findPublicAnnotatedFields(testInstance.getClass(), getRuleType(), Rule.class); }
protected Optional<ExecutionMode> getExecutionModeFromAnnotation(AnnotatedElement element) { // @formatter:off return findAnnotation(element, Execution.class) .map(Execution::value) .map(JupiterTestDescriptor::toExecutionMode); // @formatter:on }
@Override public boolean test(Class<?> candidate) { //please do not collapse into single return if (!isInnerClass.test(candidate)) { return false; } return isAnnotated(candidate, Nested.class); }
protected static Set<TestTag> getTags(AnnotatedElement element) { // @formatter:off return findRepeatableAnnotations(element, Tag.class).stream() .map(Tag::value) .filter(tag -> { boolean isValid = TestTag.isValid(tag); if (!isValid) { // TODO [#242] Replace logging with precondition check once we have a proper mechanism for // handling validation exceptions during the TestEngine discovery phase. // // As an alternative to a precondition check here, we could catch any // PreconditionViolationException thrown by TestTag::create. logger.warn(() -> String.format( "Configuration error: invalid tag syntax in @Tag(\"%s\") declaration on [%s]. Tag will be ignored.", tag, element)); } return isValid; }) .map(TestTag::create) .collect(collectingAndThen(toCollection(LinkedHashSet::new), Collections::unmodifiableSet)); // @formatter:on }
/** * Register extensions in the supplied registry from fields in the supplied * class that are annotated with {@link RegisterExtension @RegisterExtension}. * * <p>The extensions will be sorted according to {@link Order @Order} semantics * prior to registration. * * @param registry the registry in which to register the extensions; never {@code null} * @param clazz the class or interface in which to find the fields; never {@code null} * @param instance the instance of the supplied class; may be {@code null} * when searching for {@code static} fields in the class */ static void registerExtensionsFromFields(ExtensionRegistry registry, Class<?> clazz, Object instance) { Preconditions.notNull(clazz, "Class must not be null"); Preconditions.notNull(registry, "ExtensionRegistry must not be null"); Predicate<Field> predicate = (instance == null) ? isStaticExtension : isNonStaticExtension; // Ensure that the list is modifiable, since findAnnotatedFields() returns an unmodifiable list. List<Field> fields = new ArrayList<>(findAnnotatedFields(clazz, RegisterExtension.class, predicate)); // Sort fields based on @Order. fields.sort(orderComparator); fields.forEach(field -> { tryToReadFieldValue(field, instance).ifSuccess(value -> { Preconditions.notNull(value, () -> String.format( "Failed to register extension via @RegisterExtension field [%s]: field must not be null when evaluated.", field)); Extension extension = (Extension) value; registry.registerExtension(extension, field); }); }); }
/** * 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 boolean supportsTestTemplate(ExtensionContext context) { return isAnnotated(context.getTestMethod(), RepeatedTest.class); }
protected Set<ExclusiveResource> getExclusiveResourcesFromAnnotation(AnnotatedElement element) { // @formatter:off return findRepeatableAnnotations(element, ResourceLock.class).stream() .map(resource -> new ExclusiveResource(resource.value(), toLockMode(resource.mode()))) .collect(toSet()); // @formatter:on }
@Override public <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType) { return AnnotationUtils.findAnnotation(getEffectiveAnnotatedParameter(), annotationType); }
@Override public boolean isAnnotated(Class<? extends Annotation> annotationType) { return AnnotationUtils.isAnnotated(getEffectiveAnnotatedParameter(), annotationType); }
@Override public <A extends Annotation> List<A> findRepeatableAnnotations(Class<A> annotationType) { Preconditions.notNull(annotationType, "annotationType must not be null"); return AnnotationUtils.findRepeatableAnnotations(getMethod(), 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; }
@Override public boolean isAnnotated(Class<? extends Annotation> annotationType) { Preconditions.notNull(annotationType, "annotationType must not be null"); return AnnotationUtils.isAnnotated(getMethod(), annotationType); }
/** * Populate a new {@link ExtensionRegistry} from extension types declared via * {@link ExtendWith @ExtendWith} on the supplied {@link AnnotatedElement}. * * @param parentRegistry the parent extension registry to set in the newly * created registry; never {@code null} * @param annotatedElement the annotated element on which to search for * declarations of {@code @ExtendWith}; never {@code null} * * @return the new extension registry; never {@code null} * @since 5.0 */ static ExtensionRegistry populateNewExtensionRegistryFromExtendWithAnnotation(ExtensionRegistry parentRegistry, AnnotatedElement annotatedElement) { Preconditions.notNull(annotatedElement, "AnnotatedElement must not be null"); Preconditions.notNull(parentRegistry, "Parent ExtensionRegistry must not be null"); // @formatter:off List<Class<? extends Extension>> extensionTypes = findRepeatableAnnotations(annotatedElement, ExtendWith.class).stream() .map(ExtendWith::value) .flatMap(Arrays::stream) .collect(toList()); // @formatter:on return ExtensionRegistry.createRegistryFrom(parentRegistry, extensionTypes); }