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

net.thucydides.core.steps.ExecutedStepDescription Maven / Gradle / Ivy

There is a newer version: 4.2.1
Show newest version
package net.thucydides.core.steps;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.thucydides.core.reflection.MethodFinder;

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

import static net.thucydides.core.util.NameConverter.humanize;
import static net.thucydides.core.util.NameConverter.withNoArguments;

/**
 * A description of a step executed during a Thucydides step run.
 * Used in the reporting to generate user-readable names for the executed steps.
 */
public class ExecutedStepDescription implements Cloneable {

    private final Class stepsClass;
    private final String name;
    private final List argumentsList;
    private final Map displayedFields;
    private boolean isAGroup;

    private final static Map NO_FIELDS = Maps.newHashMap();
    private final static List NO_ARGUMENTS = Lists.newArrayList();

    protected ExecutedStepDescription(final Class stepsClass,
                                      final String name,
                                      List argumentsList,
                                      Map displayedFields,
                                      final boolean isAGroup) {
        this.stepsClass = stepsClass;
        this.name = name;
        this.argumentsList = argumentsList;
        this.displayedFields = displayedFields;
        this.isAGroup = isAGroup;
    }

    protected ExecutedStepDescription(final Class stepsClass,
                                      final String name,
                                      final boolean isAGroup) {
        this(stepsClass, name, NO_ARGUMENTS, NO_FIELDS, isAGroup);
    }

    protected ExecutedStepDescription(final Class stepsClass,
                                      final String name) {
        this(stepsClass, name, NO_ARGUMENTS, NO_FIELDS, false);
    }

    protected ExecutedStepDescription(final String name) {
        this(null, name, NO_ARGUMENTS, NO_FIELDS, false);
    }

    public ExecutedStepDescription clone() {
        return new ExecutedStepDescription(stepsClass, name, argumentsList, displayedFields, isAGroup);
    }

    /**
     * The class of the step library being executed.
     */
    public Class getStepClass() {
        return stepsClass;
    }

    public String getName() {
        return name;
    }

    public List getArguments() {
        return argumentsList;
    }

    public ExecutedStepDescription withName(String newName) {
        return new ExecutedStepDescription(this.stepsClass, newName, isAGroup);
    }

    public ExecutedStepDescription withDisplayedFields(Map displayedFields) {
        return new ExecutedStepDescription(this.stepsClass, this.name, this.argumentsList, displayedFields, isAGroup);
    }

    /**
     * We might not have the test class provided (e.g. at the end of a test).
     */
    public static ExecutedStepDescription of(final Class stepsClass,
                                             final String name) {
        return new ExecutedStepDescription(stepsClass, name, NO_ARGUMENTS, NO_FIELDS, false);
    }

    public static ExecutedStepDescription of(final Class stepsClass,
                                             final String name,
                                             final Object[] arguments) {
        return new ExecutedStepDescription(stepsClass, name, convertArguments(arguments), NO_FIELDS, false);
    }

    private static List convertArguments(Object[] arguments) {
        List listResult = Lists.newArrayList();
        for (Object argument : arguments) {
            listResult.add(StepArgumentWriter.readableFormOf(argument));
        }
        return listResult;
    }

    public static ExecutedStepDescription withTitle(final String name) {
        return new ExecutedStepDescription(name);
    }

    public boolean isAGroup() {
        return isAGroup;
    }

    public void setAGroup(final boolean aGroup) {
        isAGroup = aGroup;
    }

    public Method getTestMethod() {
        if (getStepClass() != null) {
            return methodCalled(withNoArguments(getName()), getStepClass());
        } else {
            return null;
        }
    }


    private Method methodCalled(final String methodName, final Class testClass) {
        Method method = MethodFinder.inClass(testClass).getMethodNamed(methodName);
        if (method == null) {
            throw new IllegalArgumentException("No test method called " + methodName + " was found in " + testClass);
        }
        return method;
    }


    /**
     * Turns a method into a human-readable title.
     */
    public String getTitle() {
        try {
            return humanize(AnnotatedStepDescription.from(this).getName());
        } catch(IllegalArgumentException noMatchingMethod) {
            return humanize(name);
        }
    }

    public Map getDisplayedFields() {
        return displayedFields;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy