io.qameta.allure.util.AnnotationUtils Maven / Gradle / Ivy
/*
* Copyright 2019 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.util;
import io.qameta.allure.LabelAnnotation;
import io.qameta.allure.model.Label;
import io.qameta.allure.model.Link;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Collection of utils used by Allure integration to extract meta information from
* test cases via reflection.
*
* @author charlie (Dmitry Baev).
*/
public final class AnnotationUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationUtils.class);
private static final String VALUE_METHOD_NAME = "value";
private AnnotationUtils() {
throw new IllegalStateException("Do not instance");
}
/**
* Returns links created from Allure annotations specified on annotated element.
*
* @param annotatedElement the element to search annotations on.
* @return discovered links.
*/
public static List getLinks(final AnnotatedElement annotatedElement) {
final List result = new ArrayList<>();
result.addAll(extractLinks(annotatedElement, io.qameta.allure.Link.class, ResultsUtils::createLink));
result.addAll(extractLinks(annotatedElement, io.qameta.allure.Issue.class, ResultsUtils::createLink));
result.addAll(extractLinks(annotatedElement, io.qameta.allure.TmsLink.class, ResultsUtils::createLink));
return result;
}
/**
* Shortcut for {@link #getLinks(Collection)}.
*
* @param annotations annotations to analyse.
* @return discovered links.
*/
public static List getLinks(final Annotation... annotations) {
return getLinks(Arrays.asList(annotations));
}
/**
* Returns links from given annotations.
*
* @param annotations annotations to analyse.
* @return discovered links.
*/
public static List getLinks(final Collection annotations) {
final List result = new ArrayList<>();
result.addAll(extractLinks(annotations, io.qameta.allure.Link.class, ResultsUtils::createLink));
result.addAll(extractLinks(annotations, io.qameta.allure.Issue.class, ResultsUtils::createLink));
result.addAll(extractLinks(annotations, io.qameta.allure.TmsLink.class, ResultsUtils::createLink));
return result;
}
/**
* Returns labels created from Allure annotations specified on annotated element.
* Shortcut for {@link #getLinks(Annotation...)}
*
* @param annotatedElement the element to search annotations on.
* @return discovered labels.
*/
public static Set