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