From c585faff3e1416fe4118178fa2ee1daa1f977bbc Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Tue, 10 Nov 2020 14:59:13 +0300 Subject: [PATCH 1/2] Test Casi ID generation methods now accepts `Executable` as a parameter instead of `Method` --- CHANGELOG.md | 2 + .../reportportal/utils/TestCaseIdUtils.java | 93 ++++++++++++------- 2 files changed, 59 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 299defc7..83c99516 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## [Unreleased] +### Changed +- Test Casi ID generation methods now accepts `Executable` as a parameter instead of `Method` ## [5.0.13] ### Fixed diff --git a/src/main/java/com/epam/reportportal/utils/TestCaseIdUtils.java b/src/main/java/com/epam/reportportal/utils/TestCaseIdUtils.java index 0b6d3a62..ecb249b8 100644 --- a/src/main/java/com/epam/reportportal/utils/TestCaseIdUtils.java +++ b/src/main/java/com/epam/reportportal/utils/TestCaseIdUtils.java @@ -24,7 +24,7 @@ import javax.annotation.Nullable; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; -import java.lang.reflect.Method; +import java.lang.reflect.Executable; import java.util.ArrayList; import java.util.List; import java.util.function.Function; @@ -33,12 +33,12 @@ import static java.util.Optional.ofNullable; /** - * + * A static class which contains methods to generate Test Case IDs and code references. */ public class TestCaseIdUtils { private TestCaseIdUtils() { - //static only + throw new IllegalStateException("Static only class"); } private static final Function, String> TRANSFORM_PARAMETERS = it -> it.stream() @@ -46,34 +46,33 @@ private TestCaseIdUtils() { .collect(Collectors.joining(",", "[", "]")); /** - * Generates a text code reference by consuming a {@link Method} + * Generates a text code reference by consuming a {@link Executable} * - * @param method a method, the value should not be null + * @param executable a executable or constructor, the value should not be null * @return a text code reference */ @Nonnull - public static String getCodeRef(@Nonnull final Method method) { - return method.getDeclaringClass().getCanonicalName() + "." + method.getName(); + public static String getCodeRef(@Nonnull final Executable executable) { + if (executable instanceof Constructor) { + return executable.getName(); + } + return executable.getDeclaringClass().getCanonicalName() + "." + executable.getName(); } /** - * Generates a text code reference by consuming a {@link Constructor} + * Returns a string of parameters values, separated by comma and embraced by '[]'. * - * @param constructor type - * @param method a constructor, the value should not be null - * @return a text code reference + * @param executable a constructor or method + * @param parameters a list of parameter values + * @param a type of parameters + * @return a string representation of parameters */ - @Nonnull - public static String getCodeRef(@Nonnull final Constructor method) { - return method.getName(); - } - @Nullable - public static String getParametersForTestCaseId(Method method, List parameters) { - if (method == null || parameters == null || parameters.isEmpty()) { + public static String getParametersForTestCaseId(Executable executable, List parameters) { + if (executable == null || parameters == null || parameters.isEmpty()) { return null; } - Annotation[][] parameterAnnotations = method.getParameterAnnotations(); + Annotation[][] parameterAnnotations = executable.getParameterAnnotations(); List keys = new ArrayList<>(); for (int paramIndex = 0; paramIndex < parameterAnnotations.length; paramIndex++) { for (int annotationIndex = 0; annotationIndex < parameterAnnotations[paramIndex].length; annotationIndex++) { @@ -92,52 +91,74 @@ public static String getParametersForTestCaseId(Method method, List param return TRANSFORM_PARAMETERS.apply(keys.stream().map(parameters::get).collect(Collectors.toList())); } + /** + * Generates a {@link TestCaseIdEntry} + * + * @param annotation a {@link TestCaseId} annotation instance + * @param executable a constructor or method for {@link TestCaseIdKey} scan + * @param parameters a list of parameter values + * @param a type of parameters + * @return Test Case ID or null if not possible to generate (all parameters are nulls, empty, etc.) + */ @Nullable - public static TestCaseIdEntry getTestCaseId(@Nullable TestCaseId annotation, @Nullable Method method, @Nullable List parameters) { - return getTestCaseId(annotation, method, null, parameters); + public static TestCaseIdEntry getTestCaseId(@Nullable TestCaseId annotation, @Nullable Executable executable, + @Nullable List parameters) { + return getTestCaseId(annotation, executable, null, parameters); } + /** + * Generates a {@link TestCaseIdEntry} + * + * @param annotation a {@link TestCaseId} annotation instance + * @param executable a constructor or method for {@link TestCaseIdKey} scan + * @param codRef a code reference to use to generate the ID + * @param parameters a list of parameter values + * @param a type of parameters + * @return Test Case ID or null if not possible to generate (all parameters are nulls, empty, etc.) + */ @Nullable - public static TestCaseIdEntry getTestCaseId(@Nullable TestCaseId annotation, @Nullable Method method, @Nullable String codRef, - @Nullable List parameters) { + public static TestCaseIdEntry getTestCaseId(@Nullable TestCaseId annotation, @Nullable Executable executable, + @Nullable String codRef, @Nullable List parameters) { if (annotation != null) { if (annotation.value().isEmpty()) { if (annotation.parametrized()) { - return ofNullable(getParametersForTestCaseId(method, parameters)).map(TestCaseIdEntry::new) - .orElse(ofNullable(codRef).map(c -> getTestCaseId(c, parameters)).orElse(getTestCaseId(method, parameters))); + return ofNullable(getParametersForTestCaseId(executable, parameters)).map(TestCaseIdEntry::new) + .orElse(ofNullable(codRef).map(c -> getTestCaseId(c, parameters)) + .orElse(getTestCaseId(executable, parameters))); } else { - return ofNullable(codRef).map(c -> getTestCaseId(c, parameters)).orElse(getTestCaseId(method, parameters)); + return ofNullable(codRef).map(c -> getTestCaseId(c, parameters)).orElse(getTestCaseId(executable, parameters)); } } else { if (annotation.parametrized()) { - return ofNullable(getParametersForTestCaseId(method, parameters)).map(p -> new TestCaseIdEntry( + return ofNullable(getParametersForTestCaseId(executable, parameters)).map(p -> new TestCaseIdEntry( annotation.value() + (p.startsWith("[") ? p : "[" + p + "]"))) - .orElse(ofNullable(codRef).map(c -> getTestCaseId(c, parameters)).orElse(getTestCaseId(method, parameters))); + .orElse(ofNullable(codRef).map(c -> getTestCaseId(c, parameters)) + .orElse(getTestCaseId(executable, parameters))); } else { return new TestCaseIdEntry(annotation.value()); } } } - return ofNullable(codRef).map(c -> getTestCaseId(c, parameters)).orElse(getTestCaseId(method, parameters)); + return ofNullable(codRef).map(c -> getTestCaseId(c, parameters)).orElse(getTestCaseId(executable, parameters)); } /** - * Generates Test Case ID based on a method reference and a list of parameters + * Generates Test Case ID based on a executable reference and a list of parameters * - * @param parameters type - * @param method a {@link Method} object + * @param parameters type + * @param executable a {@link Executable} object * @param parameters a list of parameters * @return a Test Case ID or null */ @Nullable - public static TestCaseIdEntry getTestCaseId(@Nullable Method method, @Nullable List parameters) { - return ofNullable(method).map(m -> getTestCaseId(getCodeRef(m), parameters)).orElse(getTestCaseId(parameters)); + public static TestCaseIdEntry getTestCaseId(@Nullable Executable executable, @Nullable List parameters) { + return ofNullable(executable).map(m -> getTestCaseId(getCodeRef(m), parameters)).orElse(getTestCaseId(parameters)); } /** * Generates Test Case ID based on a code reference and a list of parameters * - * @param parameters type + * @param parameters type * @param codeRef a code reference * @param parameters a list of parameters * @return a Test Case ID or null @@ -151,7 +172,7 @@ public static TestCaseIdEntry getTestCaseId(@Nullable String codeRef, @Nulla /** * Generates Test Case ID based on a list of parameters * - * @param parameters type + * @param parameters type * @param parameters a list of parameters * @return a Test Case ID or null */ From 507c2c11d8e400956d3a5247dbf4043b5b7cfcce Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Tue, 10 Nov 2020 15:09:38 +0300 Subject: [PATCH 2/2] CHANGELOG.md update --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83c99516..ad65d5fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## [Unreleased] ### Changed -- Test Casi ID generation methods now accepts `Executable` as a parameter instead of `Method` +- Test Case ID generation methods now accepts `Executable` as a parameter instead of `Method` ## [5.0.13] ### Fixed