Skip to content

Commit

Permalink
Merge pull request #132 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth authored Nov 10, 2020
2 parents 17295ef + 507c2c1 commit e14ed09
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
### Changed
- Test Case ID generation methods now accepts `Executable` as a parameter instead of `Method`

## [5.0.13]
### Fixed
Expand Down
93 changes: 57 additions & 36 deletions src/main/java/com/epam/reportportal/utils/TestCaseIdUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,47 +33,46 @@
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<List<?>, String> TRANSFORM_PARAMETERS = it -> it.stream()
.map(String::valueOf)
.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 <T> 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 <T> a type of parameters
* @return a string representation of parameters
*/
@Nonnull
public static <T> String getCodeRef(@Nonnull final Constructor<T> method) {
return method.getName();
}

@Nullable
public static <T> String getParametersForTestCaseId(Method method, List<T> parameters) {
if (method == null || parameters == null || parameters.isEmpty()) {
public static <T> String getParametersForTestCaseId(Executable executable, List<T> parameters) {
if (executable == null || parameters == null || parameters.isEmpty()) {
return null;
}
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
Annotation[][] parameterAnnotations = executable.getParameterAnnotations();
List<Integer> keys = new ArrayList<>();
for (int paramIndex = 0; paramIndex < parameterAnnotations.length; paramIndex++) {
for (int annotationIndex = 0; annotationIndex < parameterAnnotations[paramIndex].length; annotationIndex++) {
Expand All @@ -92,52 +91,74 @@ public static <T> String getParametersForTestCaseId(Method method, List<T> 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 <T> a type of parameters
* @return Test Case ID or null if not possible to generate (all parameters are nulls, empty, etc.)
*/
@Nullable
public static <T> TestCaseIdEntry getTestCaseId(@Nullable TestCaseId annotation, @Nullable Method method, @Nullable List<T> parameters) {
return getTestCaseId(annotation, method, null, parameters);
public static <T> TestCaseIdEntry getTestCaseId(@Nullable TestCaseId annotation, @Nullable Executable executable,
@Nullable List<T> 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 <T> a type of parameters
* @return Test Case ID or null if not possible to generate (all parameters are nulls, empty, etc.)
*/
@Nullable
public static <T> TestCaseIdEntry getTestCaseId(@Nullable TestCaseId annotation, @Nullable Method method, @Nullable String codRef,
@Nullable List<T> parameters) {
public static <T> TestCaseIdEntry getTestCaseId(@Nullable TestCaseId annotation, @Nullable Executable executable,
@Nullable String codRef, @Nullable List<T> 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 <T> parameters type
* @param method a {@link Method} object
* @param <T> parameters type
* @param executable a {@link Executable} object
* @param parameters a list of parameters
* @return a Test Case ID or null
*/
@Nullable
public static <T> TestCaseIdEntry getTestCaseId(@Nullable Method method, @Nullable List<T> parameters) {
return ofNullable(method).map(m -> getTestCaseId(getCodeRef(m), parameters)).orElse(getTestCaseId(parameters));
public static <T> TestCaseIdEntry getTestCaseId(@Nullable Executable executable, @Nullable List<T> 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 <T> parameters type
* @param <T> parameters type
* @param codeRef a code reference
* @param parameters a list of parameters
* @return a Test Case ID or null
Expand All @@ -151,7 +172,7 @@ public static <T> TestCaseIdEntry getTestCaseId(@Nullable String codeRef, @Nulla
/**
* Generates Test Case ID based on a list of parameters
*
* @param <T> parameters type
* @param <T> parameters type
* @param parameters a list of parameters
* @return a Test Case ID or null
*/
Expand Down

0 comments on commit e14ed09

Please sign in to comment.