| 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 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 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 | |
|
| 36 | 13 | public PostStoryStatisticsCollector(OutputStream output) { |
| 37 | 13 | this.output = output; |
| 38 | 13 | } |
| 39 | |
|
| 40 | |
public void successful(String step) { |
| 41 | 4 | add("steps"); |
| 42 | 4 | add("stepsSuccessful"); |
| 43 | 4 | add("currentScenarioSteps"); |
| 44 | 4 | } |
| 45 | |
|
| 46 | |
public void ignorable(String step) { |
| 47 | 1 | add("steps"); |
| 48 | 1 | add("stepsIgnorable"); |
| 49 | 1 | add("currentScenarioSteps"); |
| 50 | 1 | } |
| 51 | |
|
| 52 | |
public void pending(String step) { |
| 53 | 1 | add("steps"); |
| 54 | 1 | add("stepsPending"); |
| 55 | 1 | add("currentScenarioSteps"); |
| 56 | 1 | add("currentScenarioStepsPending"); |
| 57 | 1 | } |
| 58 | |
|
| 59 | |
public void notPerformed(String step) { |
| 60 | 2 | add("steps"); |
| 61 | 2 | add("stepsNotPerformed"); |
| 62 | 2 | add("currentScenarioSteps"); |
| 63 | 2 | } |
| 64 | |
|
| 65 | |
public void failed(String step, Throwable cause) { |
| 66 | 2 | this.cause = cause; |
| 67 | 2 | add("steps"); |
| 68 | 2 | add("stepsFailed"); |
| 69 | 2 | add("currentScenarioSteps"); |
| 70 | 2 | } |
| 71 | |
|
| 72 | |
public void failedOutcomes(String step, OutcomesTable table) { |
| 73 | 2 | this.outcomesFailed = table; |
| 74 | 2 | add("steps"); |
| 75 | 2 | add("stepsFailed"); |
| 76 | 2 | add("currentScenarioSteps"); |
| 77 | 2 | } |
| 78 | |
|
| 79 | |
public void beforeStory(Story story, boolean givenStory) { |
| 80 | 5 | if (givenStory) { |
| 81 | 3 | this.givenStories++; |
| 82 | |
} |
| 83 | |
|
| 84 | 5 | if (!givenStory) { |
| 85 | 2 | resetData(); |
| 86 | |
} |
| 87 | 5 | } |
| 88 | |
|
| 89 | |
public void narrative(final Narrative narrative) { |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
public void storyNotAllowed(Story story, String filter) { |
| 93 | 1 | resetData(); |
| 94 | 1 | add("notAllowed"); |
| 95 | 1 | writeData(); |
| 96 | 1 | } |
| 97 | |
|
| 98 | |
public void afterStory(boolean givenStory) { |
| 99 | 5 | if (givenStory) { |
| 100 | 3 | this.givenStories--; |
| 101 | |
} else { |
| 102 | 2 | if (has("scenariosPending") || has("givenStoryScenariosPending")) { |
| 103 | 1 | add("pending"); |
| 104 | |
} |
| 105 | 2 | writeData(); |
| 106 | |
} |
| 107 | 5 | } |
| 108 | |
|
| 109 | |
public void givenStories(GivenStories givenStories) { |
| 110 | 0 | add("givenStories"); |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
public void givenStories(List<String> storyPaths) { |
| 114 | 2 | add("givenStories"); |
| 115 | 2 | } |
| 116 | |
|
| 117 | |
public void beforeScenario(String title) { |
| 118 | 6 | cause = null; |
| 119 | 6 | outcomesFailed = null; |
| 120 | 6 | reset("currentScenarioSteps"); |
| 121 | 6 | reset("currentScenarioStepsPending"); |
| 122 | 6 | } |
| 123 | |
|
| 124 | |
public void scenarioNotAllowed(Scenario scenario, String filter) { |
| 125 | 1 | if (givenStories > 0) { |
| 126 | 0 | add("givenStoryScenariosNotAllowed"); |
| 127 | |
} else { |
| 128 | 1 | add("scenariosNotAllowed"); |
| 129 | |
} |
| 130 | 1 | } |
| 131 | |
|
| 132 | |
public void scenarioMeta(Meta meta) { |
| 133 | 1 | } |
| 134 | |
|
| 135 | |
public void afterScenario() { |
| 136 | 6 | if (givenStories > 0) { |
| 137 | 3 | countScenarios("givenStoryScenarios"); |
| 138 | |
} else { |
| 139 | 3 | countScenarios("scenarios"); |
| 140 | |
} |
| 141 | 6 | if (has("currentScenarioStepsPending") || !has("currentScenarioSteps")) { |
| 142 | 2 | if (givenStories > 0) { |
| 143 | 1 | add("givenStoryScenariosPending"); |
| 144 | |
} else { |
| 145 | 1 | add("scenariosPending"); |
| 146 | |
} |
| 147 | |
} |
| 148 | 6 | } |
| 149 | |
|
| 150 | |
private void countScenarios(String namespace) { |
| 151 | 6 | add(namespace); |
| 152 | 6 | if (cause != null || outcomesFailed != null) { |
| 153 | 1 | add(namespace + "Failed"); |
| 154 | |
} else { |
| 155 | 5 | add(namespace + "Successful"); |
| 156 | |
} |
| 157 | 6 | } |
| 158 | |
|
| 159 | |
public void beforeExamples(List<String> steps, ExamplesTable table) { |
| 160 | 1 | } |
| 161 | |
|
| 162 | |
public void example(Map<String, String> tableRow) { |
| 163 | 2 | add("examples"); |
| 164 | 2 | } |
| 165 | |
|
| 166 | |
public void afterExamples() { |
| 167 | 1 | } |
| 168 | |
|
| 169 | |
public void dryRun() { |
| 170 | 1 | } |
| 171 | |
|
| 172 | |
public void pendingMethods(List<String> methods) { |
| 173 | 0 | } |
| 174 | |
|
| 175 | |
private void add(String event) { |
| 176 | 58 | Integer count = data.get(event); |
| 177 | 58 | if (count == null) { |
| 178 | 6 | count = 0; |
| 179 | |
} |
| 180 | 58 | count++; |
| 181 | 58 | data.put(event, count); |
| 182 | 58 | } |
| 183 | |
|
| 184 | |
private boolean has(String event) { |
| 185 | 14 | Integer count = data.get(event); |
| 186 | 14 | if (count == null) { |
| 187 | 0 | count = 0; |
| 188 | |
} |
| 189 | 14 | return count > 0; |
| 190 | |
} |
| 191 | |
|
| 192 | |
private void writeData() { |
| 193 | 3 | Properties p = new Properties(); |
| 194 | 3 | for (String event : data.keySet()) { |
| 195 | 66 | if (!event.startsWith("current")) { |
| 196 | 60 | p.setProperty(event, data.get(event).toString()); |
| 197 | |
} |
| 198 | |
} |
| 199 | |
try { |
| 200 | 3 | p.store(output, this.getClass().getName()); |
| 201 | 0 | } catch (IOException e) { |
| 202 | 0 | e.printStackTrace(); |
| 203 | 3 | } |
| 204 | 3 | } |
| 205 | |
|
| 206 | |
private void resetData() { |
| 207 | 3 | data.clear(); |
| 208 | 3 | for (String event : events) { |
| 209 | 66 | reset(event); |
| 210 | |
} |
| 211 | 3 | } |
| 212 | |
|
| 213 | |
private void reset(String event) { |
| 214 | 78 | data.put(event, 0); |
| 215 | 78 | } |
| 216 | |
|
| 217 | |
@Override |
| 218 | |
public String toString() { |
| 219 | 1 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(output).append(data).toString(); |
| 220 | |
} |
| 221 | |
|
| 222 | |
} |