StoryMapper.java

  1. package org.jbehave.core.embedder;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;

  8. import org.jbehave.core.model.Meta;
  9. import org.jbehave.core.model.Story;
  10. import org.jbehave.core.model.StoryMap;
  11. import org.jbehave.core.model.StoryMaps;

  12. /**
  13.  * Maps {@link Story}s by a {@link MetaFilter}.
  14.  *
  15.  * @author Mauro Talevi
  16.  */
  17. public class StoryMapper {

  18.     private Map<String, Set<Story>> map = new HashMap<>();

  19.     /**
  20.      * Maps a story if it is not excluded by the meta filter
  21.      *
  22.      * @param story
  23.      *            the Story
  24.      * @param metaFilter
  25.      *            the meta filter
  26.      */
  27.     public void map(Story story, MetaFilter metaFilter) {
  28.         Meta storyMeta = story.getMeta();
  29.         if (!metaFilter.excluded(storyMeta)) {
  30.             boolean allScenariosExcluded = story.getScenarios().stream()
  31.                     // scenario also inherits meta from story
  32.                     .map(scenario -> scenario.getMeta().inheritFrom(storyMeta))
  33.                     .allMatch(metaFilter::excluded);
  34.             if (!allScenariosExcluded) {
  35.                 add(metaFilter.asString(), story);
  36.             }
  37.         }
  38.     }

  39.     public StoryMap getStoryMap(String filter) {
  40.         return new StoryMap(filter, storiesFor(filter));
  41.     }

  42.     public StoryMaps getStoryMaps() {
  43.         List<StoryMap> maps = new ArrayList<>();
  44.         for (String filter : map.keySet()) {
  45.             maps.add(getStoryMap(filter));
  46.         }
  47.         return new StoryMaps(maps);
  48.     }

  49.     private void add(String filter, Story story) {
  50.         storiesFor(filter).add(story);
  51.     }

  52.     private Set<Story> storiesFor(String filter) {
  53.         Set<Story> stories = map.get(filter);
  54.         if (stories == null) {
  55.             stories = new HashSet<>();
  56.             map.put(filter, stories);
  57.         }
  58.         return stories;
  59.     }

  60.     @Override
  61.     public String toString() {
  62.         return this.getClass().getSimpleName();
  63.     }

  64. }