From 97cddcc8b54e35da15e5f175683b6b4b617e9b46 Mon Sep 17 00:00:00 2001 From: Valentin Tronkov Date: Thu, 16 Nov 2023 14:53:02 +0200 Subject: [PATCH] Support @Tag annotation on a class level (#9927) Signed-off-by: Valentin Tronkov <99957253+vtronkov@users.noreply.github.com> --- .../services/bdd/junit/HapiTestEngine.java | 63 +++++++++++++------ 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/junit/HapiTestEngine.java b/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/junit/HapiTestEngine.java index e393837d38bd..14bfcb36a873 100644 --- a/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/junit/HapiTestEngine.java +++ b/hedera-node/test-clients/src/main/java/com/hedera/services/bdd/junit/HapiTestEngine.java @@ -229,6 +229,8 @@ 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 testTags; + /** Creates a new descriptor for the given test class. */ public ClassTestDescriptor(Class testClass, TestDescriptor parent, EngineDiscoveryRequest discoveryRequest) { super( @@ -236,6 +238,7 @@ public ClassTestDescriptor(Class testClass, TestDescriptor parent, EngineDisc testClass.getSimpleName(), ClassSource.from(testClass)); this.testClass = testClass; + this.testTags = getTagsIfAny(testClass); setParent(parent); // Currently we support only ASC MethodOrderer.OrderAnnotation sorting @@ -270,6 +273,11 @@ public ClassTestDescriptor(Class testClass, TestDescriptor parent, EngineDisc this.isolated = annotation.isolated(); } + @Override + public Set getTags() { + return this.testTags; + } + @Override public Type getType() { return Type.CONTAINER; @@ -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 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(); - 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; @@ -409,4 +399,39 @@ public Set getTags() { return this.testTags; } } + + private static Set 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 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 extractTags(Tags tagsAnnotation, Tag tagAnnotation) { + final var tags = new HashSet(); + 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; + } }