1 | |
package org.jbehave.core.configuration; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.HashMap; |
5 | |
import java.util.List; |
6 | |
import java.util.Map; |
7 | |
|
8 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
9 | |
import org.apache.commons.lang.builder.ToStringStyle; |
10 | |
import org.jbehave.core.steps.StepType; |
11 | |
|
12 | |
import static java.util.Arrays.asList; |
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public class Keywords { |
21 | |
|
22 | |
private static final String SYNONYM_SEPARATOR = "\\|"; |
23 | |
|
24 | |
public static final String META = "Meta"; |
25 | |
public static final String META_PROPERTY = "MetaProperty"; |
26 | |
public static final String NARRATIVE = "Narrative"; |
27 | |
public static final String IN_ORDER_TO = "InOrderTo"; |
28 | |
public static final String AS_A = "AsA"; |
29 | |
public static final String I_WANT_TO = "IWantTo"; |
30 | |
public static final String SO_THAT = "SoThat"; |
31 | |
public static final String SCENARIO = "Scenario"; |
32 | |
public static final String GIVEN_STORIES = "GivenStories"; |
33 | |
public static final String LIFECYCLE = "Lifecycle"; |
34 | |
public static final String BEFORE = "Before"; |
35 | |
public static final String AFTER = "After"; |
36 | |
public static final String EXAMPLES_TABLE = "ExamplesTable"; |
37 | |
public static final String EXAMPLES_TABLE_ROW = "ExamplesTableRow"; |
38 | |
public static final String EXAMPLES_TABLE_HEADER_SEPARATOR = "ExamplesTableHeaderSeparator"; |
39 | |
public static final String EXAMPLES_TABLE_VALUE_SEPARATOR = "ExamplesTableValueSeparator"; |
40 | |
public static final String EXAMPLES_TABLE_IGNORABLE_SEPARATOR = "ExamplesTableIgnorableSeparator"; |
41 | |
public static final String GIVEN = "Given"; |
42 | |
public static final String WHEN = "When"; |
43 | |
public static final String THEN = "Then"; |
44 | |
public static final String AND = "And"; |
45 | |
public static final String IGNORABLE = "Ignorable"; |
46 | |
public static final String PENDING = "Pending"; |
47 | |
public static final String NOT_PERFORMED = "NotPerformed"; |
48 | |
public static final String FAILED = "Failed"; |
49 | |
public static final String DRY_RUN = "DryRun"; |
50 | |
public static final String STORY_CANCELLED = "StoryCancelled"; |
51 | |
public static final String DURATION = "Duration"; |
52 | |
public static final String OUTCOME = "Outcome"; |
53 | |
public static final String OUTCOME_ANY = "OutcomeAny"; |
54 | |
public static final String OUTCOME_SUCCESS = "OutcomeSuccess"; |
55 | |
public static final String OUTCOME_FAILURE = "OutcomeFailure"; |
56 | |
public static final String OUTCOME_DESCRIPTION = "OutcomeDescription"; |
57 | |
public static final String OUTCOME_VALUE = "OutcomeValue"; |
58 | |
public static final String OUTCOME_MATCHER = "OutcomeMatcher"; |
59 | |
public static final String OUTCOME_VERIFIED = "OutcomeVerified"; |
60 | |
public static final String YES = "Yes"; |
61 | |
public static final String NO = "No"; |
62 | |
|
63 | 1 | public static final List<String> KEYWORDS = asList(META, META_PROPERTY, NARRATIVE, IN_ORDER_TO, AS_A, I_WANT_TO, SO_THAT, |
64 | |
SCENARIO, GIVEN_STORIES, LIFECYCLE, BEFORE, AFTER, EXAMPLES_TABLE, EXAMPLES_TABLE_ROW, EXAMPLES_TABLE_HEADER_SEPARATOR, |
65 | |
EXAMPLES_TABLE_VALUE_SEPARATOR, EXAMPLES_TABLE_IGNORABLE_SEPARATOR, GIVEN, WHEN, THEN, AND, IGNORABLE, |
66 | |
PENDING, NOT_PERFORMED, FAILED, DRY_RUN, STORY_CANCELLED, DURATION, OUTCOME, OUTCOME_ANY, OUTCOME_SUCCESS, OUTCOME_FAILURE, |
67 | |
OUTCOME_DESCRIPTION, OUTCOME_VALUE, OUTCOME_MATCHER, OUTCOME_VERIFIED, YES, NO); |
68 | |
|
69 | |
|
70 | |
private final String meta; |
71 | |
private final String metaProperty; |
72 | |
private final String narrative; |
73 | |
private final String inOrderTo; |
74 | |
private final String asA; |
75 | |
private final String iWantTo; |
76 | |
private final String soThat; |
77 | |
private final String scenario; |
78 | |
private final String givenStories; |
79 | |
private final String lifecycle; |
80 | |
private final String before; |
81 | |
private final String after; |
82 | |
private final String examplesTable; |
83 | |
private final String examplesTableRow; |
84 | |
private final String examplesTableHeaderSeparator; |
85 | |
private final String examplesTableValueSeparator; |
86 | |
private final String examplesTableIgnorableSeparator; |
87 | |
private final String given; |
88 | |
private final String when; |
89 | |
private final String then; |
90 | |
private final String and; |
91 | |
private final String ignorable; |
92 | |
private final String pending; |
93 | |
private final String notPerformed; |
94 | |
private final String failed; |
95 | |
private final String dryRun; |
96 | |
private final String storyCancelled; |
97 | |
private final String duration; |
98 | |
private final String outcome; |
99 | |
private final String outcomeAny; |
100 | |
private final String outcomeSuccess; |
101 | |
private final String outcomeFailure; |
102 | |
private final String outcomeDescription; |
103 | |
private final String outcomeValue; |
104 | |
private final String outcomeMatcher; |
105 | |
private final String outcomeVerified; |
106 | |
private final String yes; |
107 | |
private final String no; |
108 | 1404 | private final Map<StepType, String> startingWordsByType = new HashMap<StepType, String>(); |
109 | |
|
110 | |
|
111 | |
|
112 | |
public static Map<String, String> defaultKeywords() { |
113 | 1 | Map<String, String> keywords = new HashMap<String, String>(); |
114 | 1 | keywords.put(META, "Meta:"); |
115 | 1 | keywords.put(META_PROPERTY, "@"); |
116 | 1 | keywords.put(NARRATIVE, "Narrative:"); |
117 | 1 | keywords.put(IN_ORDER_TO, "In order to"); |
118 | 1 | keywords.put(AS_A, "As a"); |
119 | 1 | keywords.put(I_WANT_TO, "I want to"); |
120 | 1 | keywords.put(SO_THAT, "So that"); |
121 | 1 | keywords.put(SCENARIO, "Scenario:"); |
122 | 1 | keywords.put(GIVEN_STORIES, "GivenStories:"); |
123 | 1 | keywords.put(LIFECYCLE, "Lifecycle:"); |
124 | 1 | keywords.put(BEFORE, "Before:"); |
125 | 1 | keywords.put(AFTER, "After:"); |
126 | 1 | keywords.put(EXAMPLES_TABLE, "Examples:"); |
127 | 1 | keywords.put(EXAMPLES_TABLE_ROW, "Example:"); |
128 | 1 | keywords.put(EXAMPLES_TABLE_HEADER_SEPARATOR, "|"); |
129 | 1 | keywords.put(EXAMPLES_TABLE_VALUE_SEPARATOR, "|"); |
130 | 1 | keywords.put(EXAMPLES_TABLE_IGNORABLE_SEPARATOR, "|--"); |
131 | 1 | keywords.put(GIVEN, "Given"); |
132 | 1 | keywords.put(WHEN, "When"); |
133 | 1 | keywords.put(THEN, "Then"); |
134 | 1 | keywords.put(AND, "And"); |
135 | 1 | keywords.put(IGNORABLE, "!--"); |
136 | 1 | keywords.put(PENDING, "PENDING"); |
137 | 1 | keywords.put(NOT_PERFORMED, "NOT PERFORMED"); |
138 | 1 | keywords.put(FAILED, "FAILED"); |
139 | 1 | keywords.put(DRY_RUN, "DRY RUN"); |
140 | 1 | keywords.put(STORY_CANCELLED, "STORY CANCELLED"); |
141 | 1 | keywords.put(DURATION, "DURATION"); |
142 | 1 | keywords.put(OUTCOME, "Outcome:"); |
143 | 1 | keywords.put(OUTCOME_ANY, "ANY"); |
144 | 1 | keywords.put(OUTCOME_SUCCESS, "SUCCESS"); |
145 | 1 | keywords.put(OUTCOME_FAILURE, "FAILURE"); |
146 | 1 | keywords.put(OUTCOME_DESCRIPTION, "DESCRIPTION"); |
147 | 1 | keywords.put(OUTCOME_MATCHER, "MATCHER"); |
148 | 1 | keywords.put(OUTCOME_VALUE, "VALUE"); |
149 | 1 | keywords.put(OUTCOME_VERIFIED, "VERIFIED"); |
150 | 1 | keywords.put(YES, "Yes"); |
151 | 1 | keywords.put(NO, "No"); |
152 | 1 | return keywords; |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public Keywords() { |
159 | 1 | this(defaultKeywords()); |
160 | 1 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | 1404 | public Keywords(Map<String, String> keywords) { |
168 | 1404 | this.meta = keyword(META, keywords); |
169 | 1403 | this.metaProperty = keyword(META_PROPERTY, keywords); |
170 | 1403 | this.narrative = keyword(NARRATIVE, keywords); |
171 | 1403 | this.inOrderTo = keyword(IN_ORDER_TO, keywords); |
172 | 1403 | this.asA = keyword(AS_A, keywords); |
173 | 1403 | this.iWantTo = keyword(I_WANT_TO, keywords); |
174 | 1403 | this.soThat = keyword(SO_THAT, keywords); |
175 | 1403 | this.scenario = keyword(SCENARIO, keywords); |
176 | 1403 | this.givenStories = keyword(GIVEN_STORIES, keywords); |
177 | 1403 | this.lifecycle = keyword(LIFECYCLE, keywords); |
178 | 1403 | this.before = keyword(BEFORE, keywords); |
179 | 1403 | this.after = keyword(AFTER, keywords); |
180 | 1403 | this.examplesTable = keyword(EXAMPLES_TABLE, keywords); |
181 | 1403 | this.examplesTableRow = keyword(EXAMPLES_TABLE_ROW, keywords); |
182 | 1403 | this.examplesTableHeaderSeparator = keyword(EXAMPLES_TABLE_HEADER_SEPARATOR, keywords); |
183 | 1403 | this.examplesTableValueSeparator = keyword(EXAMPLES_TABLE_VALUE_SEPARATOR, keywords); |
184 | 1403 | this.examplesTableIgnorableSeparator = keyword(EXAMPLES_TABLE_IGNORABLE_SEPARATOR, keywords); |
185 | 1403 | this.given = keyword(GIVEN, keywords); |
186 | 1403 | this.when = keyword(WHEN, keywords); |
187 | 1403 | this.then = keyword(THEN, keywords); |
188 | 1403 | this.and = keyword(AND, keywords); |
189 | 1403 | this.ignorable = keyword(IGNORABLE, keywords); |
190 | 1403 | this.pending = keyword(PENDING, keywords); |
191 | 1403 | this.notPerformed = keyword(NOT_PERFORMED, keywords); |
192 | 1403 | this.failed = keyword(FAILED, keywords); |
193 | 1403 | this.dryRun = keyword(DRY_RUN, keywords); |
194 | 1403 | this.storyCancelled = keyword(STORY_CANCELLED, keywords); |
195 | 1403 | this.duration = keyword(DURATION, keywords); |
196 | 1403 | this.outcome = keyword(OUTCOME, keywords); |
197 | 1403 | this.outcomeAny = keyword(OUTCOME_ANY, keywords); |
198 | 1403 | this.outcomeSuccess = keyword(OUTCOME_SUCCESS, keywords); |
199 | 1403 | this.outcomeFailure = keyword(OUTCOME_FAILURE, keywords); |
200 | 1403 | this.outcomeDescription = keyword(OUTCOME_DESCRIPTION, keywords); |
201 | 1403 | this.outcomeMatcher = keyword(OUTCOME_MATCHER, keywords); |
202 | 1403 | this.outcomeValue = keyword(OUTCOME_VALUE, keywords); |
203 | 1403 | this.outcomeVerified = keyword(OUTCOME_VERIFIED, keywords); |
204 | 1403 | this.yes = keyword(YES, keywords); |
205 | 1403 | this.no = keyword(NO, keywords); |
206 | |
|
207 | 1403 | startingWordsByType.put(StepType.GIVEN, given()); |
208 | 1403 | startingWordsByType.put(StepType.WHEN, when()); |
209 | 1403 | startingWordsByType.put(StepType.THEN, then()); |
210 | 1403 | startingWordsByType.put(StepType.AND, and()); |
211 | 1403 | startingWordsByType.put(StepType.IGNORABLE, ignorable()); |
212 | |
|
213 | 1403 | } |
214 | |
|
215 | |
private String keyword(String name, Map<String, String> keywords) { |
216 | 53315 | String keyword = keywords.get(name); |
217 | 53315 | if (keyword == null) { |
218 | 1 | throw new KeywordNotFound(name, keywords); |
219 | |
} |
220 | 53314 | return keyword; |
221 | |
} |
222 | |
|
223 | |
public String meta() { |
224 | 548 | return meta; |
225 | |
} |
226 | |
|
227 | |
public String metaProperty() { |
228 | 54 | return metaProperty; |
229 | |
} |
230 | |
|
231 | |
public String narrative() { |
232 | 263 | return narrative; |
233 | |
} |
234 | |
|
235 | |
public String inOrderTo() { |
236 | 30 | return inOrderTo; |
237 | |
} |
238 | |
|
239 | |
public String asA() { |
240 | 78 | return asA; |
241 | |
} |
242 | |
|
243 | |
public String iWantTo() { |
244 | 78 | return iWantTo; |
245 | |
} |
246 | |
|
247 | |
public String soThat() { |
248 | 48 | return soThat; |
249 | |
} |
250 | |
|
251 | |
public String scenario() { |
252 | 1064 | return scenario; |
253 | |
} |
254 | |
|
255 | |
public String givenStories() { |
256 | 623 | return givenStories; |
257 | |
} |
258 | |
|
259 | |
public String lifecycle() { |
260 | 316 | return lifecycle; |
261 | |
} |
262 | |
|
263 | |
public String before() { |
264 | 156 | return before; |
265 | |
} |
266 | |
|
267 | |
public String after() { |
268 | 155 | return after; |
269 | |
} |
270 | |
|
271 | |
public String examplesTable() { |
272 | 398 | return examplesTable; |
273 | |
} |
274 | |
|
275 | |
public String examplesTableRow() { |
276 | 40 | return examplesTableRow; |
277 | |
} |
278 | |
|
279 | |
public String examplesTableHeaderSeparator() { |
280 | 235 | return examplesTableHeaderSeparator; |
281 | |
} |
282 | |
|
283 | |
public String examplesTableValueSeparator() { |
284 | 211 | return examplesTableValueSeparator; |
285 | |
} |
286 | |
|
287 | |
public String examplesTableIgnorableSeparator() { |
288 | 211 | return examplesTableIgnorableSeparator; |
289 | |
} |
290 | |
|
291 | |
public String given() { |
292 | 1417 | return given; |
293 | |
} |
294 | |
|
295 | |
public String when() { |
296 | 1417 | return when; |
297 | |
} |
298 | |
|
299 | |
public String then() { |
300 | 1417 | return then; |
301 | |
} |
302 | |
|
303 | |
public String and() { |
304 | 1417 | return and; |
305 | |
} |
306 | |
|
307 | |
public String ignorable() { |
308 | 1431 | return ignorable; |
309 | |
} |
310 | |
|
311 | |
public String pending() { |
312 | 45 | return pending; |
313 | |
} |
314 | |
|
315 | |
public String notPerformed() { |
316 | 35 | return notPerformed; |
317 | |
} |
318 | |
|
319 | |
public String failed() { |
320 | 72 | return failed; |
321 | |
} |
322 | |
|
323 | |
public String dryRun() { |
324 | 13 | return dryRun; |
325 | |
} |
326 | |
|
327 | |
public String storyCancelled() { |
328 | 38 | return storyCancelled; |
329 | |
} |
330 | |
|
331 | |
public String duration() { |
332 | 38 | return duration; |
333 | |
} |
334 | |
|
335 | |
public String outcome() { |
336 | 5 | return outcome; |
337 | |
} |
338 | |
|
339 | |
public String outcomeAny(){ |
340 | 12 | return outcomeAny; |
341 | |
} |
342 | |
|
343 | |
public String outcomeSuccess(){ |
344 | 18 | return outcomeSuccess; |
345 | |
} |
346 | |
|
347 | |
public String outcomeFailure(){ |
348 | 16 | return outcomeFailure; |
349 | |
} |
350 | |
|
351 | |
public List<String> outcomeFields() { |
352 | 16 | return asList(outcomeDescription, outcomeValue, outcomeMatcher, outcomeVerified); |
353 | |
} |
354 | |
|
355 | |
public String yes() { |
356 | 0 | return yes; |
357 | |
} |
358 | |
|
359 | |
public String no() { |
360 | 29 | return no; |
361 | |
} |
362 | |
|
363 | |
public String[] synonymsOf(String word) { |
364 | 5332 | return word.split(SYNONYM_SEPARATOR); |
365 | |
} |
366 | |
|
367 | |
public String[] startingWords() { |
368 | 933 | List<String> words = new ArrayList<String>(); |
369 | 933 | for (String word : startingWordsByType().values()) { |
370 | 4665 | words.addAll(asList(synonymsOf(word))); |
371 | 4665 | } |
372 | 933 | return words.toArray(new String[words.size()]); |
373 | |
} |
374 | |
|
375 | |
public Map<StepType, String> startingWordsByType() { |
376 | 935 | return startingWordsByType; |
377 | |
} |
378 | |
|
379 | |
private boolean ofStepType(String stepAsString, StepType stepType) { |
380 | 382 | boolean isType = false; |
381 | 756 | for (String word : startingWordsFor(stepType)) { |
382 | 381 | isType = stepStartsWithWord(stepAsString, word); |
383 | 381 | if (isType) |
384 | 7 | break; |
385 | |
} |
386 | 381 | return isType; |
387 | |
} |
388 | |
|
389 | |
public boolean isAndStep(String stepAsString) { |
390 | 254 | return ofStepType(stepAsString, StepType.AND); |
391 | |
} |
392 | |
|
393 | |
public boolean isIgnorableStep(String stepAsString) { |
394 | 128 | return ofStepType(stepAsString, StepType.IGNORABLE); |
395 | |
} |
396 | |
|
397 | |
public String stepWithoutStartingWord(String stepAsString, StepType stepType) { |
398 | 261 | String startingWord = startingWord(stepAsString, stepType); |
399 | 253 | return stepAsString.substring(startingWord.length() + 1); |
400 | |
|
401 | |
} |
402 | |
|
403 | |
public String startingWord(String stepAsString, StepType stepType) throws StartingWordNotFound { |
404 | 277 | for (String wordForType : startingWordsFor(stepType)) { |
405 | 262 | if (stepStartsWithWord(stepAsString, wordForType)) { |
406 | 247 | return wordForType; |
407 | |
} |
408 | |
} |
409 | 23 | for (String andWord : startingWordsFor(StepType.AND)) { |
410 | 15 | if (stepStartsWithWord(stepAsString, andWord)) { |
411 | 7 | return andWord; |
412 | |
} |
413 | |
} |
414 | 8 | throw new StartingWordNotFound(stepAsString, stepType, startingWordsByType); |
415 | |
} |
416 | |
|
417 | |
public String startingWord(String stepAsString) throws StartingWordNotFound { |
418 | 0 | for (StepType stepType : startingWordsByType.keySet()) { |
419 | 0 | for (String wordForType : startingWordsFor(stepType)) { |
420 | 0 | if (stepStartsWithWord(stepAsString, wordForType)) { |
421 | 0 | return wordForType; |
422 | |
} |
423 | |
} |
424 | 0 | } |
425 | 0 | throw new StartingWordNotFound(stepAsString, startingWordsByType); |
426 | |
} |
427 | |
|
428 | |
public StepType stepTypeFor(String stepAsString) throws StartingWordNotFound { |
429 | 5 | for (StepType stepType : startingWordsByType.keySet()) { |
430 | 13 | for (String wordForType : startingWordsFor(stepType)) { |
431 | 9 | if (stepStartsWithWord(stepAsString, wordForType)) { |
432 | 5 | return stepType; |
433 | |
} |
434 | |
} |
435 | 4 | } |
436 | 0 | throw new StartingWordNotFound(stepAsString, startingWordsByType); |
437 | |
} |
438 | |
|
439 | |
public boolean stepStartsWithWord(String step, String word) { |
440 | 725 | return step.startsWith(word + " "); |
441 | |
} |
442 | |
|
443 | |
public String startingWordFor(StepType stepType) { |
444 | 802 | String startingWord = startingWordsByType.get(stepType); |
445 | 802 | if (startingWord == null) { |
446 | 0 | throw new StartingWordNotFound(stepType, startingWordsByType); |
447 | |
} |
448 | 802 | return startingWord; |
449 | |
} |
450 | |
|
451 | |
public String[] startingWordsFor(StepType stepType) { |
452 | 668 | return synonymsOf(startingWordFor(stepType)); |
453 | |
} |
454 | |
|
455 | |
@Override |
456 | |
public String toString() { |
457 | 3 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
458 | |
} |
459 | |
|
460 | |
@SuppressWarnings("serial") |
461 | |
public static class KeywordNotFound extends RuntimeException { |
462 | |
|
463 | |
public KeywordNotFound(String name, Map<String, String> keywords) { |
464 | 1 | super("Keyword " + name + " not found amongst " + keywords); |
465 | 1 | } |
466 | |
|
467 | |
} |
468 | |
|
469 | |
@SuppressWarnings("serial") |
470 | |
public static class StartingWordNotFound extends RuntimeException { |
471 | |
|
472 | |
public StartingWordNotFound(String step, StepType stepType, Map<StepType, String> startingWordsByType) { |
473 | 8 | super("No starting word found for step '" + step + "' of type '" + stepType + "' amongst '" |
474 | |
+ startingWordsByType + "'"); |
475 | 8 | } |
476 | |
|
477 | |
public StartingWordNotFound(String step, Map<StepType, String> startingWordsByType) { |
478 | 0 | super("No starting word found for step '" + step + "' amongst '" + startingWordsByType + "'"); |
479 | 0 | } |
480 | |
|
481 | |
public StartingWordNotFound(StepType stepType, Map<StepType, String> startingWordsByType) { |
482 | 2 | super("No starting word found of type '" + stepType + "' amongst '" + startingWordsByType + "'"); |
483 | 2 | } |
484 | |
|
485 | |
} |
486 | |
|
487 | |
|
488 | |
} |