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 }
@Example void propertyWithTaggedContainer() { PropertyMethodDescriptor propertyMethodDescriptor = resolveMethodInClass("propertyWithTag", TaggedTestContainer.class); assertThat(propertyMethodDescriptor.getTags()) .containsExactlyInAnyOrder(TestTag.create("container-tag"), TestTag.create("property-tag")); }
/** * Create a {@code TestTag} from the supplied {@code name}. * * <p>Consider checking whether the syntax of the supplied {@code name} * is {@linkplain #isValid(String) valid} before attempting to create a * {@code TestTag} using this factory method. * * <p>Note: the supplied {@code name} will be {@linkplain String#trim() trimmed}. * * @param name the name of the tag; must be syntactically <em>valid</em> * @throws PreconditionViolationException if the supplied tag name is not * syntactically <em>valid</em> * @see TestTag#isValid(String) */ public static TestTag create(String name) throws PreconditionViolationException { return new TestTag(name); }
private TestTag(String name) { Preconditions.condition(TestTag.isValid(name), () -> String.format("Tag name [%s] must be syntactically valid", name)); this.name = name.trim(); }
@Example @Label("with tags") void withTags() { Set<TestDescriptor> descriptors = resolver.resolveElement(ContainerWithTags.class, engineDescriptor); Assertions.assertThat(descriptors).hasSize(1); ContainerClassDescriptor classTestDescriptor = (ContainerClassDescriptor) descriptors.iterator().next(); Assertions.assertThat(classTestDescriptor.getTags()) .containsExactly(TestTag.create("container-tag-1"), TestTag.create("container-tag-2")); }
protected static Set<TestTag> getTags(AnnotatedElement element) { return AnnotationUtils.findRepeatableAnnotations(element, Tag.class).stream().map(Tag::value).filter((tag) -> { boolean isValid = TestTag.isValid(tag); if (!isValid) { 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(Collectors.collectingAndThen(Collectors.toCollection(LinkedHashSet::new), Collections::unmodifiableSet)); } }
@Example @Label("with tag") void withTag() { Set<TestDescriptor> descriptors = resolver.resolveElement(ContainerWithTags.GroupWithTag.class, engineDescriptor); Assertions.assertThat(descriptors).hasSize(1); ContainerClassDescriptor classTestDescriptor = (ContainerClassDescriptor) descriptors.iterator().next(); Assertions.assertThat(classTestDescriptor.getTags()).containsExactly(TestTag.create("group-tag")); }
@Example void propertyWithTag() { PropertyMethodDescriptor propertyMethodDescriptor = resolveMethodInClass("propertyWithOneTag", TestContainer.class); assertThat(propertyMethodDescriptor.getTags()).containsExactly(TestTag.create("tag1")); }