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