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

org.jbehave.scenario.steps.pico.PicoStepsFactory Maven / Gradle / Ivy

There is a newer version: 5.2.0
Show newest version
package org.jbehave.scenario.steps.pico;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.jbehave.scenario.steps.CandidateSteps;
import org.jbehave.scenario.steps.Steps;
import org.jbehave.scenario.steps.StepsConfiguration;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.PicoContainer;

/**
 * A factory class for {@link CandidateSteps} that uses a {@link PicoContainer}
 * for the composition and instantiation of all components that contains
 * scenario annotated methods.
 * 
 * @author Paul Hammant
 * @author Mauro Talevi
 */
public class PicoStepsFactory {

    private final StepsConfiguration configuration;
    private final PicoContainer parent;

    public PicoStepsFactory(StepsConfiguration configuration, PicoContainer parent) {
        this.configuration = configuration;
        this.parent = parent;
    }

    public CandidateSteps[] createCandidateSteps() {
        List steps = new ArrayList();
        for (ComponentAdapter adapter : parent.getComponentAdapters()) {
            if (containsScenarioAnnotations(adapter.getComponentImplementation())) {
                steps.add(new Steps(configuration, parent.getComponent(adapter.getComponentKey())));
            }
        }
        return steps.toArray(new CandidateSteps[steps.size()]);
    }

    private boolean containsScenarioAnnotations(Class componentClass) {
        for (Method method : componentClass.getMethods()) {
            for (Annotation annotation : method.getAnnotations()) {
                if (annotation.annotationType().getName().startsWith("org.jbehave.scenario.annotations")) {
                    return true;
                }
            }
        }
        return false;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy