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