StorySplitter.java

  1. package org.jbehave.core.embedder;

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

  7. import org.jbehave.core.model.ExamplesTable;
  8. import org.jbehave.core.model.Lifecycle;
  9. import org.jbehave.core.model.Story;

  10. /**
  11.  * Splits story into the list of stories basing on Lifecycle ExamplesTable and the provided story index format.
  12.  */
  13. public class StorySplitter {

  14.     private final NumberFormat storyIndexFormat;

  15.     public StorySplitter(NumberFormat storyIndexFormat) {
  16.         this.storyIndexFormat = storyIndexFormat;
  17.     }

  18.     public List<Story> splitStories(List<Story> stories) {
  19.         List<Story> splitStories = new ArrayList<>();
  20.         for (Story story: stories) {
  21.             if (story.getLifecycle().getExamplesTable().getRowCount() > 1) {
  22.                 splitStories.addAll(splitStory(story));
  23.             } else {
  24.                 splitStories.add(story);
  25.             }
  26.         }
  27.         return splitStories;
  28.     }

  29.     private List<Story> splitStory(Story story) {
  30.         List<Story> splitStories = new ArrayList<>();
  31.         List<Map<String, String>> rows = story.getLifecycle().getExamplesTable().getRows();
  32.         for (int i = 0; i < rows.size(); i++) {
  33.             ExamplesTable examplesTable = ExamplesTable.empty().withRows(Collections.singletonList(rows.get(i)));
  34.             Lifecycle originLifecycle = story.getLifecycle();
  35.             Lifecycle lifecycle = new Lifecycle(examplesTable, originLifecycle.getBefore(), originLifecycle.getAfter());

  36.             String path = story.getPath().replace(".story", storyIndexFormat.format(i) + ".story");
  37.             Story splitStory = new Story(story, path, lifecycle);
  38.             splitStories.add(splitStory);
  39.         }
  40.         return splitStories;
  41.     }
  42. }