Coverage Report - org.jbehave.core.reporters.PostStoryStatisticsCollector
 
Classes in this File Line Coverage Branch Coverage Complexity
PostStoryStatisticsCollector
92%
102/110
88%
32/36
1.621
 
 1  
 package org.jbehave.core.reporters;
 2  
 
 3  
 import org.apache.commons.lang.builder.ToStringBuilder;
 4  
 import org.apache.commons.lang.builder.ToStringStyle;
 5  
 import org.jbehave.core.model.*;
 6  
 
 7  
 import java.io.IOException;
 8  
 import java.io.OutputStream;
 9  
 import java.util.HashMap;
 10  
 import java.util.List;
 11  
 import java.util.Map;
 12  
 import java.util.Properties;
 13  
 
 14  
 import static java.util.Arrays.asList;
 15  
 
 16  
 /**
 17  
  * <p>
 18  
  * Reporter that collects statistics and writes them as properties to output
 19  
  * stream after each story
 20  
  * </p>
 21  
  */
 22  
 public class PostStoryStatisticsCollector implements StoryReporter {
 23  
 
 24  
     private final OutputStream output;
 25  13
     private final Map<String, Integer> data = new HashMap<String, Integer>();
 26  13
     private final List<String> events = asList("notAllowed", "pending", "scenariosNotAllowed",
 27  
             "givenStoryScenariosNotAllowed", "steps", "stepsSuccessful", "stepsIgnorable", "stepsPending",
 28  
             "stepsNotPerformed", "stepsFailed", "currentScenarioSteps", "currentScenarioStepsPending", "scenarios",
 29  
             "scenariosSuccessful", "scenariosPending", "scenariosFailed", "givenStories", "givenStoryScenarios",
 30  
             "givenStoryScenariosSuccessful", "givenStoryScenariosPending", "givenStoryScenariosFailed", "examples");
 31  
 
 32  
     private Throwable cause;
 33  
     private OutcomesTable outcomesFailed;
 34  
     private int givenStories;
 35  
     private long storyStartTime;
 36  
 
 37  13
     public PostStoryStatisticsCollector(OutputStream output) {
 38  13
         this.output = output;
 39  13
     }
 40  
 
 41  
     public void successful(String step) {
 42  4
         add("steps");
 43  4
         add("stepsSuccessful");
 44  4
         add("currentScenarioSteps");
 45  4
     }
 46  
 
 47  
     public void ignorable(String step) {
 48  1
         add("steps");
 49  1
         add("stepsIgnorable");
 50  1
         add("currentScenarioSteps");
 51  1
     }
 52  
 
 53  
     public void pending(String step) {
 54  1
         add("steps");
 55  1
         add("stepsPending");
 56  1
         add("currentScenarioSteps");
 57  1
         add("currentScenarioStepsPending");
 58  1
     }
 59  
 
 60  
     public void notPerformed(String step) {
 61  2
         add("steps");
 62  2
         add("stepsNotPerformed");
 63  2
         add("currentScenarioSteps");
 64  2
     }
 65  
 
 66  
     public void failed(String step, Throwable cause) {
 67  2
         this.cause = cause;
 68  2
         add("steps");
 69  2
         add("stepsFailed");
 70  2
         add("currentScenarioSteps");
 71  2
     }
 72  
 
 73  
     public void failedOutcomes(String step, OutcomesTable table) {
 74  2
         this.outcomesFailed = table;
 75  2
         add("steps");
 76  2
         add("stepsFailed");
 77  2
         add("currentScenarioSteps");
 78  2
     }
 79  
 
 80  
     public void beforeStory(Story story, boolean givenStory) {
 81  5
         if (givenStory) {
 82  3
             this.givenStories++;
 83  
         }
 84  
 
 85  5
         if (!givenStory) {
 86  2
             resetData();
 87  2
             storyStartTime = System.currentTimeMillis();
 88  
         }
 89  5
     }
 90  
 
 91  
     public void narrative(final Narrative narrative) {
 92  0
     }
 93  
 
 94  
     public void storyNotAllowed(Story story, String filter) {
 95  1
         resetData();
 96  1
         add("notAllowed");
 97  1
         writeData();
 98  1
     }
 99  
 
 100  
     public void afterStory(boolean givenStory) {
 101  5
         if (givenStory) {
 102  3
             this.givenStories--;
 103  
         } else {
 104  2
             if (has("scenariosPending") || has("givenStoryScenariosPending")) {
 105  1
                 add("pending");
 106  
             }
 107  2
             int duration = (int)(System.currentTimeMillis() - storyStartTime);
 108  2
             data.put("duration", duration);
 109  2
             writeData();
 110  
         }
 111  5
     }
 112  
 
 113  
     public void givenStories(GivenStories givenStories) {
 114  0
         add("givenStories");
 115  0
     }
 116  
 
 117  
     public void givenStories(List<String> storyPaths) {
 118  2
         add("givenStories");
 119  2
     }
 120  
 
 121  
     public void beforeScenario(String title) {
 122  6
         cause = null;
 123  6
         outcomesFailed = null;
 124  6
         reset("currentScenarioSteps");
 125  6
         reset("currentScenarioStepsPending");
 126  6
     }
 127  
 
 128  
     public void scenarioNotAllowed(Scenario scenario, String filter) {
 129  1
         if (givenStories > 0) {
 130  0
             add("givenStoryScenariosNotAllowed");
 131  
         } else {
 132  1
             add("scenariosNotAllowed");
 133  
         }
 134  1
     }
 135  
 
 136  
     public void scenarioMeta(Meta meta) {
 137  1
     }
 138  
 
 139  
     public void afterScenario() {
 140  6
         if (givenStories > 0) {
 141  3
             countScenarios("givenStoryScenarios");
 142  
         } else {
 143  3
             countScenarios("scenarios");
 144  
         }
 145  6
         if (has("currentScenarioStepsPending") || !has("currentScenarioSteps")) {
 146  2
             if (givenStories > 0) {
 147  1
                 add("givenStoryScenariosPending");
 148  
             } else {
 149  1
                 add("scenariosPending");
 150  
             }
 151  
         }
 152  6
     }
 153  
 
 154  
     private void countScenarios(String namespace) {
 155  6
         add(namespace);
 156  6
         if (cause != null || outcomesFailed != null) {
 157  1
             add(namespace + "Failed");
 158  
         } else {
 159  5
             add(namespace + "Successful");
 160  
         }
 161  6
     }
 162  
 
 163  
     public void beforeExamples(List<String> steps, ExamplesTable table) {
 164  1
     }
 165  
 
 166  
     public void example(Map<String, String> tableRow) {
 167  2
         add("examples");
 168  2
     }
 169  
 
 170  
     public void afterExamples() {
 171  1
     }
 172  
 
 173  
     public void dryRun() {
 174  1
     }
 175  
 
 176  
     public void pendingMethods(List<String> methods) {
 177  0
     }
 178  
 
 179  
     private void add(String event) {
 180  58
         Integer count = data.get(event);
 181  58
         if (count == null) {
 182  6
             count = 0;
 183  
         }
 184  58
         count++;
 185  58
         data.put(event, count);
 186  58
     }
 187  
 
 188  
     private boolean has(String event) {
 189  14
         Integer count = data.get(event);
 190  14
         if (count == null) {
 191  0
             count = 0;
 192  
         }
 193  14
         return count > 0;
 194  
     }
 195  
 
 196  
     private void writeData() {
 197  3
         Properties p = new Properties();
 198  3
         for (String event : data.keySet()) {
 199  68
             if (!event.startsWith("current")) {
 200  62
                 p.setProperty(event, data.get(event).toString());
 201  
             }
 202  
         }
 203  
         try {
 204  3
             p.store(output, this.getClass().getName());
 205  0
         } catch (IOException e) {
 206  0
             e.printStackTrace();
 207  3
         }
 208  3
     }
 209  
 
 210  
     private void resetData() {
 211  3
         data.clear();
 212  3
         for (String event : events) {
 213  66
             reset(event);
 214  
         }
 215  3
     }
 216  
 
 217  
     private void reset(String event) {
 218  78
         data.put(event, 0);
 219  78
     }
 220  
 
 221  
     @Override
 222  
     public String toString() {
 223  1
         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(output).append(data).toString();
 224  
     }
 225  
 
 226  
 }