GivenStories.java

  1. package org.jbehave.core.model;

  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;

  7. import org.apache.commons.lang3.StringUtils;
  8. import org.apache.commons.lang3.builder.ToStringBuilder;
  9. import org.apache.commons.lang3.builder.ToStringStyle;

  10. public class GivenStories {

  11.     public static final GivenStories EMPTY = new GivenStories("");

  12.     private final List<GivenStory> stories = new ArrayList<>();
  13.     private final String asString;
  14.     private ExamplesTable examplesTable;

  15.     public GivenStories(String asString) {
  16.         this.asString = asString;
  17.         for (String path : asString.split(",")) {
  18.             if (StringUtils.isNotBlank(path)) {
  19.                 stories.add(new GivenStory(path));
  20.             }
  21.         }
  22.     }

  23.     public List<GivenStory> getStories() {
  24.         for (GivenStory story : stories) {
  25.             story.useParameters(parametersByAnchor(story.getAnchor()));
  26.         }
  27.         return stories;
  28.     }

  29.     private Map<String, String> parametersByAnchor(String anchor) {
  30.         int examplesRow = -1;
  31.         if (!StringUtils.isBlank(anchor)) {
  32.             try {
  33.                 examplesRow = Integer.parseInt(anchor);
  34.             } catch (NumberFormatException e) {
  35.                 // continue
  36.             }
  37.         }
  38.         Map<String, String> parameters = null;
  39.         if (examplesRow > -1 && examplesTable != null && examplesRow < examplesTable.getRowCount()) {
  40.             parameters = examplesTable.getRow(examplesRow);
  41.         }
  42.         if (parameters == null) {
  43.             return new HashMap<>();
  44.         }
  45.         return parameters;
  46.     }

  47.     public List<String> getPaths() {
  48.         List<String> paths = new ArrayList<>();
  49.         for (GivenStory story : stories) {
  50.             paths.add(story.asString().trim());
  51.         }
  52.         return Collections.unmodifiableList(paths);
  53.     }

  54.     public boolean requireParameters() {
  55.         for (GivenStory story : stories) {
  56.             if (story.hasAnchorWithExamplesReference()) {
  57.                 return true;
  58.             }
  59.         }
  60.         return false;
  61.     }

  62.     public void useExamplesTable(ExamplesTable examplesTable) {
  63.         this.examplesTable = examplesTable;
  64.     }
  65.    
  66.     public String asString() {
  67.         return asString;
  68.     }

  69.     @Override
  70.     public String toString() {
  71.         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  72.     }
  73. }