PicoStepsFactory.java

  1. package org.jbehave.core.steps.pico;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. import org.jbehave.core.configuration.Configuration;
  5. import org.jbehave.core.steps.AbstractStepsFactory;
  6. import org.jbehave.core.steps.InjectableStepsFactory;
  7. import org.picocontainer.ComponentAdapter;
  8. import org.picocontainer.PicoContainer;

  9. /**
  10.  * An {@link InjectableStepsFactory} that uses a {@link PicoContainer} for the
  11.  * composition and instantiation of all components that contain JBehave
  12.  * annotated methods.
  13.  *
  14.  * @author Paul Hammant
  15.  * @author Mauro Talevi
  16.  */
  17. public class PicoStepsFactory extends AbstractStepsFactory {

  18.     private final PicoContainer parent;

  19.     public PicoStepsFactory(Configuration configuration, PicoContainer parent) {
  20.         super(configuration);
  21.         this.parent = parent;
  22.     }

  23.     @Override
  24.     protected List<Class<?>> stepsTypes() {
  25.         List<Class<?>> types = new ArrayList<>();
  26.         for (ComponentAdapter<?> adapter : parent.getComponentAdapters()) {
  27.             if (hasAnnotatedMethods(adapter.getComponentImplementation())) {
  28.                 types.add(adapter.getComponentImplementation());
  29.             }
  30.         }
  31.         return types;
  32.     }

  33.     @Override
  34.     public Object createInstanceOfType(Class<?> type) {
  35.         Object instance = parent.getComponent(type);
  36.         if (instance == null) {
  37.             throw new StepsInstanceNotFound(type, this);
  38.         }
  39.         return instance;
  40.     }

  41. }