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

com.thoughtworks.gauge.scan.StepsScanner Maven / Gradle / Ivy

There is a newer version: 0.11.1
Show newest version
/*----------------------------------------------------------------
 *  Copyright (c) ThoughtWorks, Inc.
 *  Licensed under the Apache License, Version 2.0
 *  See LICENSE.txt in the project root for license information.
 *----------------------------------------------------------------*/
package com.thoughtworks.gauge.scan;

import com.thoughtworks.gauge.Logger;
import com.thoughtworks.gauge.Step;
import com.thoughtworks.gauge.StepRegistryEntry;
import com.thoughtworks.gauge.StepValue;
import com.thoughtworks.gauge.Util;
import com.thoughtworks.gauge.registry.StepRegistry;
import org.reflections.Reflections;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;

/**
 * Scans for step implementations.
 */
public class StepsScanner implements IScanner {
    private StepRegistry registry;

    public StepsScanner(StepRegistry registry) {
        this.registry = registry;
    }

    public void scan(Reflections reflections) {
        Logger.debug("Scanning packages for steps");
        Set stepImplementations = reflections.getMethodsAnnotatedWith(Step.class);
        buildStepRegistry(stepImplementations);
    }

    private void buildStepRegistry(Set stepImplementations) {
        StepsUtil stepsUtil = new StepsUtil();
        for (Method method : stepImplementations) {
            Step annotation = method.getAnnotation(Step.class);
            if (annotation != null) {
                for (String stepName : annotation.value()) {
                    String parameterizedStep = Util.trimQuotes(stepName);
                    String stepText = stepsUtil.getStepText(parameterizedStep);
                    if (registry.contains(stepText)) {
                        StepRegistryEntry entry = registry.getForCurrentProject(stepText, method);
                        if (entry != null) {
                            Logger.debug("Found " + stepText + " in current project scope.");
                            entry.setMethodInfo(method);
                        } else {
                            addExternalStepEntryToRegistry(stepsUtil, method, parameterizedStep, stepText);
                        }
                    } else {
                        addExternalStepEntryToRegistry(stepsUtil, method, parameterizedStep, stepText);
                    }
                }
            }
        }
    }

    private void addExternalStepEntryToRegistry(StepsUtil stepsUtil, Method method, String parameterizedStep, String stepText) {
        Logger.debug("Loading " + stepText + "via reflected sources.");
        List parameters = stepsUtil.getParameters(parameterizedStep);
        StepValue stepValue = new StepValue(stepText, parameterizedStep, parameters);
        registry.addStepImplementation(stepValue, method, true);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy