<commit_before>package ru.yandex.qatools.allure.cucumberjvm;

import gherkin.formatter.model.Scenario;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Ignore;
import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import ru.yandex.qatools.allure.Allure;
import ru.yandex.qatools.allure.config.AllureModelUtils;
import ru.yandex.qatools.allure.events.ClearStepStorageEvent;
import ru.yandex.qatools.allure.events.TestCaseCanceledEvent;
import ru.yandex.qatools.allure.events.TestCaseFailureEvent;
import ru.yandex.qatools.allure.events.TestCaseFinishedEvent;
import ru.yandex.qatools.allure.events.TestCasePendingEvent;
import ru.yandex.qatools.allure.events.TestCaseStartedEvent;
import ru.yandex.qatools.allure.events.TestSuiteFinishedEvent;
import ru.yandex.qatools.allure.events.TestSuiteStartedEvent;
import ru.yandex.qatools.allure.utils.AnnotationManager;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.lang3.reflect.FieldUtils;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Stories;

/**
 * @author <NAME> <EMAIL>
 */
public class AllureRunListener extends RunListener {

    private Allure lifecycle = Allure.LIFECYCLE;

    private final Map<String, String> suites = new HashMap<>();

    /**
     * All tests object
     */
    private Description parentDescription;

    @Override
    public void testRunStarted(Description description) {
        parentDescription = description;
    }

    /**
     * Find feature and story for given scenario
     *
     * @param scenarioName
     * @return array of {"<FEATURE_NAME>", "<STORY_NAME>"}
     * @throws IllegalAccessException
     */
    String[] findFeatureByScenarioName(String scenarioName) throws IllegalAccessException {
        ArrayList<Description> features = parentDescription.getChildren().get(0).getChildren();
        //Feature cycle
        for (Description feature : features) {
            //Story cycle
            for (Description story : feature.getChildren()) {
                Object scenarioType = FieldUtils.readField(story, "fUniqueId", true);

                //Scenario
                if (scenarioType instanceof Scenario) {
                    if (story.getDisplayName().equals(scenarioName)) {
                        return new String[]{feature.getDisplayName(), scenarioName};
                    }

                    //Scenario Outline
                } else {
                    ArrayList<Description> examples = story.getChildren().get(0).getChildren();
                    // we need to go deeper :)
                    for (Description example : examples) {
                        if (example.getDisplayName().equals(scenarioName)) {
                            return new String[]{feature.getDisplayName(), story.getDisplayName()};
                        }
                    }
                }
            }
        }
        //TODO: change method return type to smth better
        return new String[]{"Undefined Feature", scenarioName};
    }

    public void testSuiteStarted(Description description) throws IllegalAccessException {

        String[] annotationParams = findFeatureByScenarioName(description.getDisplayName());

        //Create feature and story annotations. Remove unnecessary words from it
        Features feature = getFeaturesAnnotation(new String[]{annotationParams[0].split(":")[1].trim()});
        Stories story = getStoriesAnnotation(new String[]{annotationParams[1].split(":")[1].trim()});

        //If it`s Scenario Outline, add example string to story name
        if (description.getDisplayName().startsWith("|")
                || description.getDisplayName().endsWith("|")) {
            story = getStoriesAnnotation(new String[]{annotationParams[1].split(":")[1].trim()
                + " " + description.getDisplayName()});
        }

        String uid = generateSuiteUid(description.getClassName());
        TestSuiteStartedEvent event = new TestSuiteStartedEvent(uid, story.value()[0]);

        //Add feature and story annotations
        Collection<Annotation> annotations = new ArrayList<>();
        for (Annotation annotation : description.getAnnotations()) {
            annotations.add(annotation);
        }
        annotations.add(story);
        annotations.add(feature);
        AnnotationManager am = new AnnotationManager(annotations);
        am.update(event);

        event.withLabels(AllureModelUtils.createTestFrameworkLabel("JUnit"));

        getLifecycle().fire(event);
    }

    /**
     * Creates Story annotation object<commit_msg>Change createTestFrameworkLabel to CucumberJVM<commit_after>package ru.yandex.qatools.allure.cucumberjvm;

import gherkin.formatter.model.Scenario;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Ignore;
import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import ru.yandex.qatools.allure.Allure;
import ru.yandex.qatools.allure.config.AllureModelUtils;
import ru.yandex.qatools.allure.events.ClearStepStorageEvent;
import ru.yandex.qatools.allure.events.TestCaseCanceledEvent;
import ru.yandex.qatools.allure.events.TestCaseFailureEvent;
import ru.yandex.qatools.allure.events.TestCaseFinishedEvent;
import ru.yandex.qatools.allure.events.TestCasePendingEvent;
import ru.yandex.qatools.allure.events.TestCaseStartedEvent;
import ru.yandex.qatools.allure.events.TestSuiteFinishedEvent;
import ru.yandex.qatools.allure.events.TestSuiteStartedEvent;
import ru.yandex.qatools.allure.utils.AnnotationManager;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.lang3.reflect.FieldUtils;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Stories;

/**
 * @author <NAME> <EMAIL>
 */
public class AllureRunListener extends RunListener {

    private Allure lifecycle = Allure.LIFECYCLE;

    private final Map<String, String> suites = new HashMap<>();

    /**
     * All tests object
     */
    private Description parentDescription;

    @Override
    public void testRunStarted(Description description) {
        parentDescription = description;
    }

    /**
     * Find feature and story for given scenario
     *
     * @param scenarioName
     * @return array of {"<FEATURE_NAME>", "<STORY_NAME>"}
     * @throws IllegalAccessException
     */
    String[] findFeatureByScenarioName(String scenarioName) throws IllegalAccessException {
        ArrayList<Description> features = parentDescription.getChildren().get(0).getChildren();
        //Feature cycle
        for (Description feature : features) {
            //Story cycle
            for (Description story : feature.getChildren()) {
                Object scenarioType = FieldUtils.readField(story, "fUniqueId", true);

                //Scenario
                if (scenarioType instanceof Scenario) {
                    if (story.getDisplayName().equals(scenarioName)) {
                        return new String[]{feature.getDisplayName(), scenarioName};
                    }

                    //Scenario Outline
                } else {
                    ArrayList<Description> examples = story.getChildren().get(0).getChildren();
                    // we need to go deeper :)
                    for (Description example : examples) {
                        if (example.getDisplayName().equals(scenarioName)) {
                            return new String[]{feature.getDisplayName(), story.getDisplayName()};
                        }
                    }
                }
            }
        }
        //TODO: change method return type to smth better
        return new String[]{"Undefined Feature", scenarioName};
    }

    public void testSuiteStarted(Description description) throws IllegalAccessException {

        String[] annotationParams = findFeatureByScenarioName(description.getDisplayName());

        //Create feature and story annotations. Remove unnecessary words from it
        Features feature = getFeaturesAnnotation(new String[]{annotationParams[0].split(":")[1].trim()});
        Stories story = getStoriesAnnotation(new String[]{annotationParams[1].split(":")[1].trim()});

        //If it`s Scenario Outline, add example string to story name
        if (description.getDisplayName().startsWith("|")
                || description.getDisplayName().endsWith("|")) {
            story = getStoriesAnnotation(new String[]{annotationParams[1].split(":")[1].trim()
                + " " + description.getDisplayName()});
        }

        String uid = generateSuiteUid(description.getClassName());
        TestSuiteStartedEvent event = new TestSuiteStartedEvent(uid, story.value()[0]);

        //Add feature and story annotations
        Collection<Annotation> annotations = new ArrayList<>();
        for (Annotation annotation : description.getAnnotations()) {
            annotations.add(annotation);
        }
        annotations.add(story);
        annotations.add(feature);
        AnnotationManager am = new AnnotationManager(annotations);
        am.update(event);

        event.withLabels(AllureModelUtils.createTestFrameworkLabel("CucumberJVM"));

        getLifecycle().fire(event);
    }

    /**
     * Creates Story annotation object
{'fraction_non_alphanumeric': 0.10656162537576448, 'fraction_numerical': 0.002073183373069348, 'mean_word_length': 2.9411764705882355, 'pattern_counts': {'":': 6, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7591900', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2509, 'n_words': 504}