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