Coverage Report - org.jbehave.core.reporters.PostStoryStatisticsCollector
 
Classes in this File Line Coverage Branch Coverage Complexity
PostStoryStatisticsCollector
94%
68/72
90%
9/10
1.25
 
 1  
 package org.jbehave.core.reporters;
 2  
 
 3  
 import static java.util.Arrays.asList;
 4  
 
 5  
 import java.io.IOException;
 6  
 import java.io.OutputStream;
 7  
 import java.util.HashMap;
 8  
 import java.util.List;
 9  
 import java.util.Map;
 10  
 import java.util.Properties;
 11  
 
 12  
 import org.apache.commons.lang.builder.ToStringBuilder;
 13  
 import org.apache.commons.lang.builder.ToStringStyle;
 14  
 import org.jbehave.core.model.ExamplesTable;
 15  
 import org.jbehave.core.model.GivenStories;
 16  
 import org.jbehave.core.model.Meta;
 17  
 import org.jbehave.core.model.OutcomesTable;
 18  
 import org.jbehave.core.model.Scenario;
 19  
 import org.jbehave.core.model.Story;
 20  
 
 21  
 /**
 22  
  * <p>
 23  
  * Reporter that collects statistics and writes them as properties to output
 24  
  * stream after each story
 25  
  * </p>
 26  
  */
 27  
 public class PostStoryStatisticsCollector implements StoryReporter {
 28  
 
 29  
     private final OutputStream output;
 30  10
     private final Map<String, Integer> data = new HashMap<String, Integer>();
 31  10
     private final List<String> events = asList("notAllowed", "scenariosNotAllowed", "steps", "stepsSuccessful", "stepsIgnorable", "stepsPending",
 32  
             "stepsNotPerformed", "stepsFailed", "scenarios", "scenariosSuccessful", "scenariosFailed", "givenStories",
 33  
             "examples");
 34  
 
 35  
     private Throwable cause;
 36  
     private OutcomesTable outcomesFailed;
 37  
 
 38  10
     public PostStoryStatisticsCollector(OutputStream output) {
 39  10
         this.output = output;
 40  10
     }
 41  
 
 42  
     public void successful(String step) {
 43  3
         count("steps");
 44  3
         count("stepsSuccessful");
 45  3
     }
 46  
 
 47  
     public void ignorable(String step) {
 48  1
         count("steps");
 49  1
         count("stepsIgnorable");
 50  1
     }
 51  
 
 52  
     public void pending(String step) {
 53  1
         count("steps");
 54  1
         count("stepsPending");
 55  1
     }
 56  
 
 57  
     public void notPerformed(String step) {
 58  1
         count("steps");
 59  1
         count("stepsNotPerformed");
 60  1
     }
 61  
 
 62  
     public void failed(String step, Throwable cause) {
 63  2
         this.cause = cause;
 64  2
         count("steps");
 65  2
         count("stepsFailed");
 66  2
     }
 67  
 
 68  
     public void failedOutcomes(String step, OutcomesTable table) {
 69  2
         this.outcomesFailed = table;
 70  2
         count("steps");
 71  2
         count("stepsFailed");
 72  2
     }
 73  
 
 74  
     public void beforeStory(Story story, boolean givenStory) {
 75  2
         resetData();
 76  2
     }
 77  
 
 78  
     public void storyNotAllowed(Story story, String filter) {
 79  1
         resetData();
 80  1
         count("notAllowed");
 81  1
         writeData();
 82  1
     }
 83  
 
 84  
     public void afterStory(boolean givenStory) {
 85  2
         writeData();
 86  2
     }
 87  
 
 88  
     public void givenStories(GivenStories givenStories) {
 89  0
         count("givenStories");            
 90  0
     }
 91  
 
 92  
     public void givenStories(List<String> storyPaths) {
 93  1
         count("givenStories");            
 94  1
     }
 95  
 
 96  
     public void beforeScenario(String title) {
 97  2
         cause = null;
 98  2
         outcomesFailed = null;
 99  2
     }
 100  
 
 101  
     public void scenarioNotAllowed(Scenario scenario, String filter) {
 102  1
         count("scenariosNotAllowed");
 103  1
     }
 104  
 
 105  
     public void scenarioMeta(Meta meta) {
 106  1
     }
 107  
 
 108  
     public void afterScenario() {
 109  2
         count("scenarios");
 110  2
         if (cause != null || outcomesFailed != null) {
 111  1
             count("scenariosFailed");
 112  
         } else {
 113  1
             count("scenariosSuccessful");
 114  
         }
 115  2
     }
 116  
 
 117  
     public void beforeExamples(List<String> steps, ExamplesTable table) {
 118  1
     }
 119  
 
 120  
     public void example(Map<String, String> tableRow) {
 121  2
         count("examples");
 122  2
     }
 123  
 
 124  
     public void afterExamples() {
 125  1
     }
 126  
 
 127  
     public void dryRun() {
 128  1
     }
 129  
 
 130  
     private void count(String event) {
 131  29
         Integer count = data.get(event);
 132  29
         if (count == null) {
 133  4
             count = 0;
 134  
         }
 135  29
         count++;
 136  29
         data.put(event, count);
 137  29
     }
 138  
 
 139  
     private void writeData() {
 140  3
         Properties p = new Properties();
 141  3
         for (String event : data.keySet()) {
 142  39
             p.setProperty(event, data.get(event).toString());
 143  
         }
 144  
         try {
 145  3
             p.store(output, this.getClass().getName());
 146  0
         } catch (IOException e) {
 147  0
             e.printStackTrace();
 148  3
         }
 149  3
     }
 150  
 
 151  
     private void resetData() {
 152  3
         data.clear();
 153  3
         for (String event : events) {
 154  39
             data.put(event, 0);
 155  
         }
 156  3
     }
 157  
 
 158  
     @Override
 159  
     public String toString() {
 160  1
         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(output).append(data).toString();
 161  
     }
 162  
 
 163  
 }