| 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 | |
import org.jbehave.core.model.StoryDuration; |
| 20 | |
|
| 21 | |
import static java.util.Arrays.asList; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
public class PostStoryStatisticsCollector implements StoryReporter { |
| 30 | |
|
| 31 | |
private final OutputStream output; |
| 32 | 13 | private final Map<String, Integer> data = new HashMap<String, Integer>(); |
| 33 | 13 | private final List<String> events = asList("notAllowed", "pending", "scenariosNotAllowed", |
| 34 | |
"givenStoryScenariosNotAllowed", "steps", "stepsSuccessful", "stepsIgnorable", "stepsPending", |
| 35 | |
"stepsNotPerformed", "stepsFailed", "currentScenarioSteps", "currentScenarioStepsPending", "scenarios", |
| 36 | |
"scenariosSuccessful", "scenariosPending", "scenariosFailed", "givenStories", "givenStoryScenarios", |
| 37 | |
"givenStoryScenariosSuccessful", "givenStoryScenariosPending", "givenStoryScenariosFailed", "examples"); |
| 38 | |
|
| 39 | |
private Throwable cause; |
| 40 | |
private OutcomesTable outcomesFailed; |
| 41 | |
private int givenStories; |
| 42 | |
private long storyStartTime; |
| 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 | 5 | if (givenStory) { |
| 93 | 3 | this.givenStories++; |
| 94 | |
} |
| 95 | |
|
| 96 | 5 | if (!givenStory) { |
| 97 | 2 | resetData(); |
| 98 | 2 | storyStartTime = System.currentTimeMillis(); |
| 99 | |
} |
| 100 | 5 | } |
| 101 | |
|
| 102 | |
public void narrative(final Narrative narrative) { |
| 103 | 0 | } |
| 104 | |
|
| 105 | |
public void storyNotAllowed(Story story, String filter) { |
| 106 | 1 | resetData(); |
| 107 | 1 | add("notAllowed"); |
| 108 | 1 | writeData(); |
| 109 | 1 | } |
| 110 | |
|
| 111 | |
public void storyCancelled(Story story, StoryDuration storyDuration) { |
| 112 | 0 | add("cancelled"); |
| 113 | 0 | } |
| 114 | |
|
| 115 | |
public void afterStory(boolean givenStory) { |
| 116 | 5 | if (givenStory) { |
| 117 | 3 | this.givenStories--; |
| 118 | |
} else { |
| 119 | 2 | if (has("scenariosPending") || has("givenStoryScenariosPending")) { |
| 120 | 1 | add("pending"); |
| 121 | |
} |
| 122 | 2 | int duration = (int)(System.currentTimeMillis() - storyStartTime); |
| 123 | 2 | data.put("duration", duration); |
| 124 | 2 | writeData(); |
| 125 | |
} |
| 126 | 5 | } |
| 127 | |
|
| 128 | |
public void givenStories(GivenStories givenStories) { |
| 129 | 0 | add("givenStories"); |
| 130 | 0 | } |
| 131 | |
|
| 132 | |
public void givenStories(List<String> storyPaths) { |
| 133 | 2 | add("givenStories"); |
| 134 | 2 | } |
| 135 | |
|
| 136 | |
public void beforeScenario(String title) { |
| 137 | 7 | cause = null; |
| 138 | 7 | outcomesFailed = null; |
| 139 | 7 | currentScenarioNotAllowed = false; |
| 140 | 7 | reset("currentScenarioSteps"); |
| 141 | 7 | reset("currentScenarioStepsPending"); |
| 142 | 7 | } |
| 143 | |
|
| 144 | |
public void scenarioNotAllowed(Scenario scenario, String filter) { |
| 145 | 1 | if (givenStories > 0) { |
| 146 | 0 | add("givenStoryScenariosNotAllowed"); |
| 147 | |
} else { |
| 148 | 1 | add("scenariosNotAllowed"); |
| 149 | 1 | currentScenarioNotAllowed = true; |
| 150 | |
} |
| 151 | 1 | } |
| 152 | |
|
| 153 | |
public void scenarioMeta(Meta meta) { |
| 154 | 1 | } |
| 155 | |
|
| 156 | |
public void afterScenario() { |
| 157 | 7 | if (givenStories > 0) { |
| 158 | 3 | countScenarios("givenStoryScenarios"); |
| 159 | |
} else { |
| 160 | 4 | countScenarios("scenarios"); |
| 161 | |
} |
| 162 | 7 | if (has("currentScenarioStepsPending") || (!has("currentScenarioSteps") && !currentScenarioNotAllowed)) { |
| 163 | 2 | if (givenStories > 0) { |
| 164 | 1 | add("givenStoryScenariosPending"); |
| 165 | |
} else { |
| 166 | 1 | add("scenariosPending"); |
| 167 | |
} |
| 168 | |
} |
| 169 | 7 | } |
| 170 | |
|
| 171 | |
private void countScenarios(String namespace) { |
| 172 | 7 | add(namespace); |
| 173 | 7 | if (!currentScenarioNotAllowed){ |
| 174 | 6 | if (cause != null || outcomesFailed != null) { |
| 175 | 1 | add(namespace + "Failed"); |
| 176 | |
} else { |
| 177 | 5 | add(namespace + "Successful"); |
| 178 | |
} |
| 179 | |
} |
| 180 | 7 | } |
| 181 | |
|
| 182 | |
public void beforeExamples(List<String> steps, ExamplesTable table) { |
| 183 | 1 | } |
| 184 | |
|
| 185 | |
public void example(Map<String, String> tableRow) { |
| 186 | 2 | add("examples"); |
| 187 | 2 | } |
| 188 | |
|
| 189 | |
public void afterExamples() { |
| 190 | 1 | } |
| 191 | |
|
| 192 | |
public void dryRun() { |
| 193 | 1 | } |
| 194 | |
|
| 195 | |
public void pendingMethods(List<String> methods) { |
| 196 | 0 | } |
| 197 | |
|
| 198 | |
public void restarted(String step, Throwable cause) { |
| 199 | 0 | } |
| 200 | |
|
| 201 | |
private void add(String event) { |
| 202 | 59 | Integer count = data.get(event); |
| 203 | 59 | if (count == null) { |
| 204 | 6 | count = 0; |
| 205 | |
} |
| 206 | 59 | count++; |
| 207 | 59 | data.put(event, count); |
| 208 | 59 | } |
| 209 | |
|
| 210 | |
private boolean has(String event) { |
| 211 | 16 | Integer count = data.get(event); |
| 212 | 16 | if (count == null) { |
| 213 | 0 | count = 0; |
| 214 | |
} |
| 215 | 16 | return count > 0; |
| 216 | |
} |
| 217 | |
|
| 218 | |
private void writeData() { |
| 219 | 3 | Properties p = new Properties(); |
| 220 | 3 | for (String event : data.keySet()) { |
| 221 | 68 | if (!event.startsWith("current")) { |
| 222 | 62 | p.setProperty(event, data.get(event).toString()); |
| 223 | |
} |
| 224 | |
} |
| 225 | |
try { |
| 226 | 3 | p.store(output, this.getClass().getName()); |
| 227 | 0 | } catch (IOException e) { |
| 228 | 0 | e.printStackTrace(); |
| 229 | 3 | } |
| 230 | 3 | } |
| 231 | |
|
| 232 | |
private void resetData() { |
| 233 | 3 | data.clear(); |
| 234 | 3 | for (String event : events) { |
| 235 | 66 | reset(event); |
| 236 | |
} |
| 237 | 3 | } |
| 238 | |
|
| 239 | |
private void reset(String event) { |
| 240 | 80 | data.put(event, 0); |
| 241 | 80 | } |
| 242 | |
|
| 243 | |
@Override |
| 244 | |
public String toString() { |
| 245 | 1 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(output).append(data).toString(); |
| 246 | |
} |
| 247 | |
|
| 248 | |
|
| 249 | |
} |