##### Coverage Report - org.jbehave.core.model.GivenStories

Classes in this File | Line Coverage | Branch Coverage | Complexity
GivenStories |
100% | 38/38
100% | 20/20
| 2.4444444444444446;2.444

1 |  |
     package org.jbehave.core.model;
2 |  |
3 |  |
     import java.util.ArrayList;
4 |  |
     import java.util.Collections;
5 |  |
     import java.util.HashMap;
6 |  |
     import java.util.List;
7 |  |
     import java.util.Map;
8 |  |
9 |  |
     import org.apache.commons.lang.StringUtils;
10 |  |
     import org.apache.commons.lang.builder.ToStringBuilder;
11 |  |
     import org.apache.commons.lang.builder.ToStringStyle;
12 |  |
13 |  |
     public class GivenStories {
14 |  |
15 | 1 |
         public static final GivenStories EMPTY = new GivenStories("");
16 |  |
17 | 178 |
         private final List<GivenStory> givenStories = new ArrayList<GivenStory>();
18 |  |
         private final String givenStoriesAsString;
19 | 178 |
         private ExamplesTable examplesTable = ExamplesTable.EMPTY;
20 |  |
21 | 178 |
         public GivenStories(String givenStoriesAsString) {
22 | 178 |
             this.givenStoriesAsString = givenStoriesAsString;
23 | 178 |
             if ( !StringUtils.isBlank(givenStoriesAsString) ){
24 | 28 |
                 parse();
25 |  |
             }
26 | 178 |
         }
27 |  |
28 |  |
         private void parse() {
29 | 28 |
             givenStories.clear();
30 | 82 |
             for (String storyPath : givenStoriesAsString.split(",", -1)) {
31 | 54 |
                 givenStories.add(new GivenStory(storyPath));
32 |  |
             }
33 | 28 |
         }
34 |  |
35 |  |
         public List<GivenStory> getStories() {
36 | 24 |
             for (GivenStory givenStory : givenStories) {
37 | 60 |
                 givenStory.useParameters(parametersByAnchor(givenStory.getAnchor()));
38 |  |
             }
39 | 24 |
             return givenStories;
40 |  |
         }
41 |  |
42 |  |
         private Map<String, String> parametersByAnchor(String anchor) {
43 | 60 |
             int examplesRow = -1;
44 | 60 |
             if ( !StringUtils.isBlank(anchor) ){
45 |  |
                 try {
46 | 20 |
                     examplesRow = Integer.parseInt(anchor);
47 | 5 |
                 } catch (NumberFormatException e) {
48 |  |
                     // continue
49 | 15 |
                 }
50 |  |
             }
51 | 60 |
             Map<String, String> parameters = null;
52 | 60 |
             if ( examplesRow > -1 && examplesRow < examplesTable.getRowCount() ){
53 | 10 |
                  parameters = examplesTable.getRow(examplesRow);
54 |  |
             }
55 | 60 |
             if ( parameters == null ){
56 | 50 |
                 return new HashMap<String, String>();
57 |  |
             }
58 | 10 |
             return parameters;
59 |  |
         }
60 |  |
61 |  |
         public List<String> getPaths() {
62 | 59 |
             List<String> paths = new ArrayList<String>();
63 | 59 |
             for (GivenStory story : givenStories) {
64 | 17 |
                 paths.add(story.asString().trim());
65 |  |
             }
66 | 59 |
             return Collections.unmodifiableList(paths);
67 |  |
         }
68 |  |
69 |  |
         public boolean requireParameters() {
70 | 131 |
             for (GivenStory givenStory : givenStories) {
71 | 11 |
                 if ( givenStory.hasAnchor() ){
72 | 2 |
                     return true;
73 |  |
                 }
74 |  |
             }
75 | 129 |
             return false;
76 |  |
         }
77 |  |
78 |  |
         public void useExamplesTable(ExamplesTable examplesTable) {
79 | 1 |
             this.examplesTable = examplesTable;
80 | 1 |
         }
81 |  |
82 |  |
         public String asString() {
83 | 36 |
             return givenStoriesAsString;
84 |  |
         }
85 |  |
86 |  |
         public String toString() {
87 | 21 |
             return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
88 |  |
         }
89 |  |
90 |  |
91 |  |
     }