##### Coverage Report - org.jbehave.core.embedder.StoryMapper

Classes in this File | Line Coverage | Branch Coverage | Complexity
StoryMapper |
96% | 25/26
75% | 9/12
| 2.0;2

1 |  |
     package org.jbehave.core.embedder;
2 |  |
3 |  |
     import java.util.ArrayList;
4 |  |
     import java.util.HashMap;
5 |  |
     import java.util.HashSet;
6 |  |
     import java.util.List;
7 |  |
     import java.util.Map;
8 |  |
     import java.util.Set;
9 |  |
10 |  |
     import org.jbehave.core.model.Meta;
11 |  |
     import org.jbehave.core.model.Scenario;
12 |  |
     import org.jbehave.core.model.Story;
13 |  |
     import org.jbehave.core.model.StoryMap;
14 |  |
     import org.jbehave.core.model.StoryMaps;
15 |  |
16 |  |
     /**
17 |  |
      * Maps {@link Story}s by a {@link MetaFilter}.
18 |  |
      *
19 |  |
      * @author Mauro Talevi
20 |  |
      */
21 | 74 |
     public class StoryMapper {
22 |  |
23 | 74 |
         private Map<String, Set<Story>> map = new HashMap<String, Set<Story>>();
24 |  |
25 |  |
         /**
26 |  |
          * Maps a story if it is allowed by the meta filter
27 |  |
          *
28 |  |
          * @param story
29 |  |
          *            the Story
30 |  |
          * @param metaFilter
31 |  |
          *            the meta filter
32 |  |
          */
33 |  |
         public void map(Story story, MetaFilter metaFilter) {
34 | 2 |
             if (metaFilter.allow(story.getMeta())) {
35 | 1 |
                 boolean allowed = false;
36 | 1 |
                 for (Scenario scenario : story.getScenarios()) {
37 |  |
                     // scenario also inherits meta from story
38 | 1 |
                     Meta inherited = scenario.getMeta().inheritFrom(story.getMeta());
39 | 1 |
                     if (metaFilter.allow(inherited)) {
40 | 1 |
                         allowed = true;
41 | 1 |
                         break;
42 |  |
                     }
43 | 0 |
                 }
44 | 1 |
                 if (allowed) {
45 | 1 |
                     add(metaFilter.asString(), story);
46 |  |
                 }
47 |  |
             }
48 | 2 |
         }
49 |  |
50 |  |
         public StoryMap getStoryMap(String filter) {
51 | 1 |
             return new StoryMap(filter, storiesFor(filter));
52 |  |
         }
53 |  |
54 |  |
         public StoryMaps getStoryMaps() {
55 | 1 |
             List<StoryMap> maps = new ArrayList<StoryMap>();
56 | 1 |
             for (String filter : map.keySet()) {
57 | 1 |
                 maps.add(getStoryMap(filter));
58 |  |
             }
59 | 1 |
             return new StoryMaps(maps);
60 |  |
         }
61 |  |
62 |  |
         private void add(String filter, Story story) {
63 | 1 |
             storiesFor(filter).add(story);
64 | 1 |
         }
65 |  |
66 |  |
         private Set<Story> storiesFor(String filter) {
67 | 2 |
             Set<Story> stories = map.get(filter);
68 | 2 |
             if (stories == null) {
69 | 1 |
                 stories = new HashSet<Story>();
70 | 1 |
                 map.put(filter, stories);
71 |  |
             }
72 | 2 |
             return stories;
73 |  |
         }
74 |  |
75 |  |
         @Override
76 |  |
         public String toString() {
77 | 1 |
             return this.getClass().getSimpleName();
78 |  |
         }
79 |  |
80 |  |
     }