All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.qameta.allure.util.AnnotationUtils Maven / Gradle / Ivy

There is a newer version: 2.29.0
Show newest version
/*
 *  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.LinkAnnotation;
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.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.qameta.allure.util.ResultsUtils.createLabel;
import static io.qameta.allure.util.ResultsUtils.createLink;
import static java.util.Arrays.asList;

/**
 * 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 meta annotations specified on annotated element.
     *
     * @param annotatedElement the element to search annotations on.
     * @return discovered links.
     */
    public static Set getLinks(final AnnotatedElement annotatedElement) {
        return getLinks(annotatedElement.getDeclaredAnnotations());
    }

    /**
     * Shortcut for {@link #getLinks(Collection)}.
     *
     * @param annotations annotations to analyse.
     * @return discovered links.
     */
    public static Set getLinks(final Annotation... annotations) {
        return getLinks(asList(annotations));
    }

    /**
     * Returns links from given annotations.
     *
     * @param annotations annotations to analyse.
     * @return discovered links.
     */
    public static Set getLinks(final Collection annotations) {
        return extractMetaAnnotations(LinkAnnotation.class, AnnotationUtils::extractLinks, annotations)
                .collect(Collectors.toSet());
    }

    /**
     * Returns labels created from Allure meta annotations specified on annotated element.
     * Shortcut for {@link #getLinks(Annotation...)}
     *
     * @param annotatedElement the element to search annotations on.
     * @return discovered labels.
     */
    public static Set