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