Skip to content

Commit

Permalink
Support @tag annotation on a class level (#9927)
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Tronkov <99957253+vtronkov@users.noreply.github.com>
  • Loading branch information
vtronkov committed Nov 16, 2023
1 parent ba5e074 commit 97cddcc
Showing 1 changed file with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,16 @@ private static final class ClassTestDescriptor extends AbstractTestDescriptor
/** Whether a separate cluster of nodes should be created for this test class (or reset the normal cluster) */
private final boolean isolated;

private final Set<TestTag> testTags;

/** Creates a new descriptor for the given test class. */
public ClassTestDescriptor(Class<?> testClass, TestDescriptor parent, EngineDiscoveryRequest discoveryRequest) {
super(
parent.getUniqueId().append("class", testClass.getName()),
testClass.getSimpleName(),
ClassSource.from(testClass));
this.testClass = testClass;
this.testTags = getTagsIfAny(testClass);
setParent(parent);

// Currently we support only ASC MethodOrderer.OrderAnnotation sorting
Expand Down Expand Up @@ -270,6 +273,11 @@ public ClassTestDescriptor(Class<?> testClass, TestDescriptor parent, EngineDisc
this.isolated = annotation.isolated();
}

@Override
public Set<TestTag> getTags() {
return this.testTags;
}

@Override
public Type getType() {
return Type.CONTAINER;
Expand Down Expand Up @@ -331,28 +339,10 @@ public MethodTestDescriptor(Method testMethod, ClassTestDescriptor parent) {
MethodSource.from(testMethod));
this.testMethod = testMethod;
this.testTags = getTagsIfAny(testMethod);
this.testTags.addAll(parent.getTags());
setParent(parent);
}

private Set<TestTag> getTagsIfAny(Method testMethod) {
// When a method has a single @Tag annotation, we retrieve it by filtering for Tag.class.
// In cases where a method has multiple @Tag annotations, we use Tags.class to access all of them.
// Ideally, Tags.class should encompass both single and multiple @Tag annotations,
// but the current implementation does not support this.
final var tagsAnnotation = testMethod.getAnnotation(Tags.class);
final var tagAnnotation = testMethod.getAnnotation(Tag.class);

final var tags = new HashSet<TestTag>();
if (tagsAnnotation != null) {
tags.addAll(Arrays.stream(tagsAnnotation.value())
.map(t -> TestTag.create(t.value()))
.toList());
} else if (tagAnnotation != null) {
tags.add(TestTag.create(tagAnnotation.value()));
}
return tags;
}

@Override
public Type getType() {
return Type.TEST;
Expand Down Expand Up @@ -409,4 +399,39 @@ public Set<TestTag> getTags() {
return this.testTags;
}
}

private static Set<TestTag> getTagsIfAny(Class<?> testClass) {
// When a class has a single @Tag annotation, we retrieve it by filtering for Tag.class.
// In cases where a class has multiple @Tag annotations, we use Tags.class to access all of them.
// Ideally, Tags.class should encompass both single and multiple @Tag annotations,
// but the current implementation does not support this.
final var tagsAnnotation = testClass.getAnnotation(Tags.class);
final var tagAnnotation = testClass.getAnnotation(Tag.class);

return extractTags(tagsAnnotation, tagAnnotation);
}

private static Set<TestTag> getTagsIfAny(Method testMethod) {
// When a method has a single @Tag annotation, we retrieve it by filtering for Tag.class.
// In cases where a method has multiple @Tag annotations, we use Tags.class to access all of them.
// Ideally, Tags.class should encompass both single and multiple @Tag annotations,
// but the current implementation does not support this.
final var tagsAnnotation = testMethod.getAnnotation(Tags.class);
final var tagAnnotation = testMethod.getAnnotation(Tag.class);

return extractTags(tagsAnnotation, tagAnnotation);
}

// A helper method that extracts the value from either a @Tags annotation or a @Tag annotation
private static Set<TestTag> extractTags(Tags tagsAnnotation, Tag tagAnnotation) {
final var tags = new HashSet<TestTag>();
if (tagsAnnotation != null) {
tags.addAll(Arrays.stream(tagsAnnotation.value())
.map(t -> TestTag.create(t.value()))
.toList());
} else if (tagAnnotation != null) {
tags.add(TestTag.create(tagAnnotation.value()));
}
return tags;
}
}

0 comments on commit 97cddcc

Please sign in to comment.