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