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