ConfigurableEmbedder.java

  1. package org.jbehave.core;

  2. import org.jbehave.core.configuration.Configuration;
  3. import org.jbehave.core.embedder.Embedder;
  4. import org.jbehave.core.junit.JUnitStories;
  5. import org.jbehave.core.steps.InjectableStepsFactory;

  6. /**
  7.  * <p>
  8.  * Abstract implementation of {@link Embeddable} which extends {@link InjectableEmbedder}
  9.  * to allow the configuration using the {@link Configuration} and
  10.  * the {@link InjectableStepsFactory} specified.
  11.  * </p>
  12.  * <p>
  13.  * The instances of the {@link Configuration} and the
  14.  * {@link InjectableStepsFactory} can be provided either via the
  15.  * {@link #useConfiguration(Configuration)} and
  16.  * {@link #useStepsFactory(InjectableStepsFactory)} methods or overriding the
  17.  * {@link #configuration()} and {@link #stepsFactory()} methods.
  18.  * </p>
  19.  * <p>
  20.  * If overriding the {@link #configuration()} method and providing an
  21.  * {@link InjectableStepsFactory} which requires a {@link Configuration}, then
  22.  * care must be taken to avoid re-instantiating the {@link Configuration}. E.g.:
  23.  *
  24.  * <pre>
  25.  * <code>public Configuration configuration() {
  26.  *   if (super.hasConfiguration()) {
  27.  *     return super.configuration();
  28.  *   }
  29.  *   return new MostUsefulConfiguration()...;
  30.  * }</code>
  31.  * </pre>
  32.  *
  33.  * </p>
  34.  * <p>
  35.  * Note that no defaults are provided by this implementation. If no values are
  36.  * set by the subclasses, then <code>null</code> values are passed to the
  37.  * configured {@link Embedder}, which is responsible for setting the default
  38.  * values.
  39.  * </p>
  40.  * <p>
  41.  * Typically, users that use JUnit will find it easier to extend other
  42.  * implementations, such as {@link JUnitStories} which
  43.  * implement the {@link #run()} using the configured {@link Embedder} and
  44.  * annotate it with JUnit's annotations.
  45.  * </p>
  46.  */
  47. public abstract class ConfigurableEmbedder extends InjectableEmbedder {

  48.     private Configuration configuration;
  49.     private InjectableStepsFactory stepsFactory;

  50.     public void useConfiguration(Configuration configuration) {
  51.         this.configuration = configuration;
  52.     }

  53.     public Configuration configuration() {
  54.         return configuration;
  55.     }

  56.     public boolean hasConfiguration() {
  57.         return configuration != null;
  58.     }

  59.     public void useStepsFactory(InjectableStepsFactory stepsFactory) {
  60.         this.stepsFactory = stepsFactory;
  61.     }

  62.     public InjectableStepsFactory stepsFactory() {
  63.         return stepsFactory;
  64.     }

  65.     public boolean hasStepsFactory() {
  66.         return stepsFactory != null;
  67.     }

  68.     public Embedder configuredEmbedder() {
  69.         if (configuration == null) {
  70.             configuration = configuration();
  71.         }
  72.         Embedder embedder = injectedEmbedder();
  73.         embedder.useConfiguration(configuration);
  74.         if (stepsFactory == null) {
  75.             stepsFactory = stepsFactory();
  76.         }
  77.         embedder.useStepsFactory(stepsFactory);
  78.         return embedder;
  79.     }

  80. }