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