Lifecycle.java

  1. package org.jbehave.core.model;

  2. import static org.codehaus.plexus.util.StringUtils.isNotBlank;

  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.LinkedHashSet;
  6. import java.util.List;
  7. import java.util.Set;

  8. import org.apache.commons.lang3.builder.ToStringBuilder;
  9. import org.apache.commons.lang3.builder.ToStringStyle;
  10. import org.jbehave.core.annotations.AfterScenario.Outcome;
  11. import org.jbehave.core.annotations.Scope;
  12. import org.jbehave.core.embedder.MetaFilter;

  13. public class Lifecycle {

  14.     public static final Lifecycle EMPTY = new Lifecycle();

  15.     private ExamplesTable examplesTable;
  16.     private List<Steps> before;
  17.     private List<Steps> after;
  18.    
  19.     public Lifecycle() {
  20.         this(Arrays.<Steps>asList(), Arrays.<Steps>asList());
  21.     }

  22.     public Lifecycle(ExamplesTable examplesTable) {
  23.         this(examplesTable, Arrays.<Steps>asList(), Arrays.<Steps>asList());
  24.     }

  25.     public Lifecycle(List<Steps> before, List<Steps> after) {
  26.         this(ExamplesTable.EMPTY, before, after);
  27.     }

  28.     public Lifecycle(ExamplesTable examplesTable, List<Steps> before, List<Steps> after) {
  29.         this.examplesTable = examplesTable;
  30.         this.before = before;
  31.         this.after = after;
  32.     }

  33.     public Set<Scope> getScopes() {
  34.         Set<Scope> scopes = new LinkedHashSet<>();
  35.         scopes.add(Scope.STEP);
  36.         scopes.add(Scope.SCENARIO);
  37.         scopes.add(Scope.STORY);
  38.         return scopes;
  39.     }

  40.     public ExamplesTable getExamplesTable() {
  41.         return examplesTable;
  42.     }

  43.     public boolean hasBeforeSteps() {
  44.         return !unwrap(before).isEmpty();
  45.     }

  46.     public List<String> getBeforeSteps() {
  47.         return getBeforeSteps(Scope.SCENARIO);
  48.     }

  49.     public List<String> getBeforeSteps(Scope scope) {
  50.         return unwrap(filter(this.before, scope));
  51.     }

  52.     public List<Steps> getBefore() {
  53.         return before;
  54.     }

  55.     public boolean hasAfterSteps() {
  56.         return !unwrap(this.after).isEmpty();
  57.     }

  58.     public List<String> getAfterSteps() {
  59.         return getAfterSteps(Scope.SCENARIO);
  60.     }

  61.     public List<String> getAfterSteps(Scope scope) {
  62.         return unwrap(filter(this.after, scope));
  63.     }

  64.     public List<String> getAfterSteps(Outcome outcome) {
  65.         return getAfterSteps(outcome, Meta.EMPTY);
  66.     }

  67.     public List<String> getAfterSteps(Outcome outcome, Meta meta) {
  68.         return getAfterSteps(Scope.SCENARIO, outcome, meta);
  69.     }

  70.     public List<String> getAfterSteps(Scope scope, Outcome outcome) {
  71.         return getAfterSteps(scope, outcome, Meta.EMPTY);
  72.     }

  73.     public List<String> getAfterSteps(Scope scope, Outcome outcome, Meta meta) {
  74.         MetaFilter filter = getMetaFilter(outcome);
  75.         List<Steps> afterSteps = new ArrayList<>();
  76.         for (Steps steps : after) {
  77.             if (outcome == steps.outcome && (meta.equals(Meta.EMPTY) || !filter.excluded(meta))) {
  78.                 afterSteps.add(stepsByScope(steps, scope));
  79.             }
  80.         }
  81.         return unwrap(afterSteps);
  82.     }

  83.     public List<Steps> getAfter() {
  84.         return after;
  85.     }

  86.     public Set<Outcome> getOutcomes() {
  87.         Set<Outcome> outcomes = new LinkedHashSet<>();
  88.         for (Steps steps : after) {
  89.             outcomes.add(steps.outcome);
  90.         }
  91.         return outcomes;
  92.     }

  93.     public MetaFilter getMetaFilter(Outcome outcome) {
  94.         for (Steps steps : after) {
  95.             if (outcome.equals(steps.outcome) && isNotBlank(steps.metaFilter)) {
  96.                 return new MetaFilter(steps.metaFilter);
  97.             }
  98.         }
  99.         return MetaFilter.EMPTY;
  100.     }

  101.     private List<String> unwrap(List<Steps> stepsCollection) {
  102.         List<String> allSteps = new ArrayList<>();
  103.         for (Steps steps : stepsCollection) {
  104.             allSteps.addAll(steps.steps);
  105.         }
  106.         return allSteps;
  107.     }

  108.     private List<Steps> filter(List<Steps> stepsCollection, Scope scope) {
  109.         List<Steps> filteredSteps = new ArrayList<>();
  110.         for (Steps steps : stepsCollection) {
  111.             filteredSteps.add(stepsByScope(steps, scope));
  112.         }
  113.         return filteredSteps;
  114.     }

  115.     private Steps stepsByScope(Steps steps, Scope scope) {
  116.         return steps.scope == scope ? steps : Steps.EMPTY;
  117.     }

  118.     public boolean isEmpty() {
  119.         return examplesTable.isEmpty() && before.isEmpty() && after.isEmpty();
  120.     }

  121.     @Override
  122.     public String toString() {
  123.         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  124.     }

  125.     public static class Steps {

  126.         public static Steps EMPTY = new Steps(Arrays.<String>asList());

  127.         private Scope scope;
  128.         private Outcome outcome;
  129.         private String metaFilter;
  130.         private List<String> steps;

  131.         public Steps(List<String> steps) {
  132.             this(Scope.SCENARIO, steps);
  133.         }

  134.         public Steps(Scope scope, List<String> steps) {
  135.             this(scope, Outcome.ANY, null, steps);
  136.         }

  137.         public Steps(Outcome outcome, List<String> steps) {
  138.             this(outcome, null, steps);
  139.         }

  140.         public Steps(Outcome outcome, String metaFilter, List<String> steps) {
  141.             this(Scope.SCENARIO, outcome, metaFilter, steps);
  142.         }

  143.         public Steps(Scope scope, Outcome outcome, List<String> steps) {
  144.             this(scope, outcome, null, steps);
  145.         }

  146.         public Steps(Scope scope, Outcome outcome, String metaFilter, List<String> steps) {
  147.             this.scope = scope;
  148.             this.outcome = outcome;
  149.             this.metaFilter = metaFilter;
  150.             this.steps = steps;
  151.         }

  152.         @Override
  153.         public String toString() {
  154.             return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  155.         }

  156.     }

  157.     public enum ExecutionType {

  158.         /**
  159.          * Represents steps declared in Lifecycle section: composite ones and steps annotated with @Given, @When, @Then
  160.          */
  161.         USER,

  162.         /**
  163.          * Represents steps annotated with @BeforeScenario, @AfterScenario, @BeforeStory, @AfterStory
  164.          */
  165.         SYSTEM;
  166.     }

  167. }