| 1 | |
package org.jbehave.core.configuration; |
| 2 | |
|
| 3 | |
import java.util.Collection; |
| 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 | |
public static final String META = "Meta"; |
| 23 | |
public static final String META_PROPERTY = "MetaProperty"; |
| 24 | |
public static final String NARRATIVE = "Narrative"; |
| 25 | |
public static final String IN_ORDER_TO = "InOrderTo"; |
| 26 | |
public static final String AS_A = "AsA"; |
| 27 | |
public static final String I_WANT_TO = "IWantTo"; |
| 28 | |
public static final String SCENARIO = "Scenario"; |
| 29 | |
public static final String GIVEN_STORIES = "GivenStories"; |
| 30 | |
public static final String EXAMPLES_TABLE = "ExamplesTable"; |
| 31 | |
public static final String EXAMPLES_TABLE_ROW = "ExamplesTableRow"; |
| 32 | |
public static final String EXAMPLES_TABLE_HEADER_SEPARATOR = "ExamplesTableHeaderSeparator"; |
| 33 | |
public static final String EXAMPLES_TABLE_VALUE_SEPARATOR = "ExamplesTableValueSeparator"; |
| 34 | |
public static final String EXAMPLES_TABLE_IGNORABLE_SEPARATOR = "ExamplesTableIgnorableSeparator"; |
| 35 | |
public static final String GIVEN = "Given"; |
| 36 | |
public static final String WHEN = "When"; |
| 37 | |
public static final String THEN = "Then"; |
| 38 | |
public static final String AND = "And"; |
| 39 | |
public static final String IGNORABLE = "Ignorable"; |
| 40 | |
public static final String PENDING = "Pending"; |
| 41 | |
public static final String NOT_PERFORMED = "NotPerformed"; |
| 42 | |
public static final String FAILED = "Failed"; |
| 43 | |
public static final String DRY_RUN = "DryRun"; |
| 44 | |
public static final String STORY_CANCELLED = "StoryCancelled"; |
| 45 | |
public static final String DURATION = "Duration"; |
| 46 | |
public static final String OUTCOME_DESCRIPTION = "OutcomeDescription"; |
| 47 | |
public static final String OUTCOME_VALUE = "OutcomeValue"; |
| 48 | |
public static final String OUTCOME_MATCHER = "OutcomeMatcher"; |
| 49 | |
public static final String OUTCOME_VERIFIED = "OutcomeVerified"; |
| 50 | |
public static final String YES = "Yes"; |
| 51 | |
public static final String NO = "No"; |
| 52 | |
|
| 53 | 1 | public static final List<String> KEYWORDS = asList(META, META_PROPERTY, NARRATIVE, IN_ORDER_TO, AS_A, I_WANT_TO, |
| 54 | |
SCENARIO, GIVEN_STORIES, EXAMPLES_TABLE, EXAMPLES_TABLE_ROW, EXAMPLES_TABLE_HEADER_SEPARATOR, |
| 55 | |
EXAMPLES_TABLE_VALUE_SEPARATOR, EXAMPLES_TABLE_IGNORABLE_SEPARATOR, GIVEN, WHEN, THEN, AND, IGNORABLE, |
| 56 | |
PENDING, NOT_PERFORMED, FAILED, DRY_RUN, STORY_CANCELLED, DURATION, OUTCOME_DESCRIPTION, OUTCOME_VALUE, |
| 57 | |
OUTCOME_MATCHER, OUTCOME_VERIFIED, YES, NO); |
| 58 | |
|
| 59 | |
private final String meta; |
| 60 | |
private final String metaProperty; |
| 61 | |
private final String narrative; |
| 62 | |
private final String inOrderTo; |
| 63 | |
private final String asA; |
| 64 | |
private final String iWantTo; |
| 65 | |
private final String scenario; |
| 66 | |
private final String givenStories; |
| 67 | |
private final String examplesTable; |
| 68 | |
private final String examplesTableRow; |
| 69 | |
private final String examplesTableHeaderSeparator; |
| 70 | |
private final String examplesTableValueSeparator; |
| 71 | |
private final String examplesTableIgnorableSeparator; |
| 72 | |
private final String given; |
| 73 | |
private final String when; |
| 74 | |
private final String then; |
| 75 | |
private final String and; |
| 76 | |
private final String ignorable; |
| 77 | |
private final String pending; |
| 78 | |
private final String notPerformed; |
| 79 | |
private final String failed; |
| 80 | |
private final String dryRun; |
| 81 | |
private final String storyCancelled; |
| 82 | |
private final String duration; |
| 83 | |
private final String outcomeDescription; |
| 84 | |
private final String outcomeValue; |
| 85 | |
private final String outcomeMatcher; |
| 86 | |
private final String outcomeVerified; |
| 87 | |
private final String yes; |
| 88 | |
private final String no; |
| 89 | 3846 | private final Map<StepType, String> startingWordsByType = new HashMap<StepType, String>(); |
| 90 | |
|
| 91 | |
public static Map<String, String> defaultKeywords() { |
| 92 | 1 | Map<String, String> keywords = new HashMap<String, String>(); |
| 93 | 1 | keywords.put(META, "Meta:"); |
| 94 | 1 | keywords.put(META_PROPERTY, "@"); |
| 95 | 1 | keywords.put(NARRATIVE, "Narrative:"); |
| 96 | 1 | keywords.put(IN_ORDER_TO, "In order to:"); |
| 97 | 1 | keywords.put(AS_A, "As a:"); |
| 98 | 1 | keywords.put(I_WANT_TO, "I want to:"); |
| 99 | 1 | keywords.put(SCENARIO, "Scenario:"); |
| 100 | 1 | keywords.put(GIVEN_STORIES, "GivenStories:"); |
| 101 | 1 | keywords.put(EXAMPLES_TABLE, "Examples:"); |
| 102 | 1 | keywords.put(EXAMPLES_TABLE_ROW, "Example:"); |
| 103 | 1 | keywords.put(EXAMPLES_TABLE_HEADER_SEPARATOR, "|"); |
| 104 | 1 | keywords.put(EXAMPLES_TABLE_VALUE_SEPARATOR, "|"); |
| 105 | 1 | keywords.put(EXAMPLES_TABLE_IGNORABLE_SEPARATOR, "|--"); |
| 106 | 1 | keywords.put(GIVEN, "Given"); |
| 107 | 1 | keywords.put(WHEN, "When"); |
| 108 | 1 | keywords.put(THEN, "Then"); |
| 109 | 1 | keywords.put(AND, "And"); |
| 110 | 1 | keywords.put(IGNORABLE, "!--"); |
| 111 | 1 | keywords.put(PENDING, "PENDING"); |
| 112 | 1 | keywords.put(NOT_PERFORMED, "NOT PERFORMED"); |
| 113 | 1 | keywords.put(FAILED, "FAILED"); |
| 114 | 1 | keywords.put(DRY_RUN, "DRY RUN"); |
| 115 | 1 | keywords.put(STORY_CANCELLED, "STORY CANCELLED"); |
| 116 | 1 | keywords.put(DURATION, "DURATION"); |
| 117 | 1 | keywords.put(OUTCOME_DESCRIPTION, "DESCRIPTION"); |
| 118 | 1 | keywords.put(OUTCOME_MATCHER, "MATCHER"); |
| 119 | 1 | keywords.put(OUTCOME_VALUE, "VALUE"); |
| 120 | 1 | keywords.put(OUTCOME_VERIFIED, "VERIFIED"); |
| 121 | 1 | keywords.put(YES, "Yes"); |
| 122 | 1 | keywords.put(NO, "No"); |
| 123 | 1 | return keywords; |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public Keywords() { |
| 130 | 1 | this(defaultKeywords()); |
| 131 | 1 | } |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | 3846 | public Keywords(Map<String, String> keywords) { |
| 139 | 3846 | this.meta = keyword(META, keywords); |
| 140 | 3845 | this.metaProperty = keyword(META_PROPERTY, keywords); |
| 141 | 3845 | this.narrative = keyword(NARRATIVE, keywords); |
| 142 | 3845 | this.inOrderTo = keyword(IN_ORDER_TO, keywords); |
| 143 | 3845 | this.asA = keyword(AS_A, keywords); |
| 144 | 3845 | this.iWantTo = keyword(I_WANT_TO, keywords); |
| 145 | 3845 | this.scenario = keyword(SCENARIO, keywords); |
| 146 | 3845 | this.givenStories = keyword(GIVEN_STORIES, keywords); |
| 147 | 3845 | this.examplesTable = keyword(EXAMPLES_TABLE, keywords); |
| 148 | 3845 | this.examplesTableRow = keyword(EXAMPLES_TABLE_ROW, keywords); |
| 149 | 3845 | this.examplesTableHeaderSeparator = keyword(EXAMPLES_TABLE_HEADER_SEPARATOR, keywords); |
| 150 | 3845 | this.examplesTableValueSeparator = keyword(EXAMPLES_TABLE_VALUE_SEPARATOR, keywords); |
| 151 | 3845 | this.examplesTableIgnorableSeparator = keyword(EXAMPLES_TABLE_IGNORABLE_SEPARATOR, keywords); |
| 152 | 3845 | this.given = keyword(GIVEN, keywords); |
| 153 | 3845 | this.when = keyword(WHEN, keywords); |
| 154 | 3845 | this.then = keyword(THEN, keywords); |
| 155 | 3845 | this.and = keyword(AND, keywords); |
| 156 | 3845 | this.ignorable = keyword(IGNORABLE, keywords); |
| 157 | 3845 | this.pending = keyword(PENDING, keywords); |
| 158 | 3845 | this.notPerformed = keyword(NOT_PERFORMED, keywords); |
| 159 | 3845 | this.failed = keyword(FAILED, keywords); |
| 160 | 3845 | this.dryRun = keyword(DRY_RUN, keywords); |
| 161 | 3845 | this.storyCancelled = keyword(STORY_CANCELLED, keywords); |
| 162 | 3845 | this.duration = keyword(DURATION, keywords); |
| 163 | 3845 | this.outcomeDescription = keyword(OUTCOME_DESCRIPTION, keywords); |
| 164 | 3845 | this.outcomeMatcher = keyword(OUTCOME_MATCHER, keywords); |
| 165 | 3845 | this.outcomeValue = keyword(OUTCOME_VALUE, keywords); |
| 166 | 3845 | this.outcomeVerified = keyword(OUTCOME_VERIFIED, keywords); |
| 167 | 3845 | this.yes = keyword(YES, keywords); |
| 168 | 3845 | this.no = keyword(NO, keywords); |
| 169 | |
|
| 170 | 3845 | startingWordsByType.put(StepType.GIVEN, given()); |
| 171 | 3845 | startingWordsByType.put(StepType.WHEN, when()); |
| 172 | 3845 | startingWordsByType.put(StepType.THEN, then()); |
| 173 | 3845 | startingWordsByType.put(StepType.AND, and()); |
| 174 | 3845 | startingWordsByType.put(StepType.IGNORABLE, ignorable()); |
| 175 | |
|
| 176 | 3845 | } |
| 177 | |
|
| 178 | |
private String keyword(String name, Map<String, String> keywords) { |
| 179 | 115351 | String keyword = keywords.get(name); |
| 180 | 115351 | if (keyword == null) { |
| 181 | 1 | throw new KeywordNotFound(name, keywords); |
| 182 | |
} |
| 183 | 115350 | return keyword; |
| 184 | |
} |
| 185 | |
|
| 186 | |
public String meta() { |
| 187 | 329 | return meta; |
| 188 | |
} |
| 189 | |
|
| 190 | |
public String metaProperty() { |
| 191 | 55 | return metaProperty; |
| 192 | |
} |
| 193 | |
|
| 194 | |
public String narrative() { |
| 195 | 98 | return narrative; |
| 196 | |
} |
| 197 | |
|
| 198 | |
public String inOrderTo() { |
| 199 | 25 | return inOrderTo; |
| 200 | |
} |
| 201 | |
|
| 202 | |
public String asA() { |
| 203 | 25 | return asA; |
| 204 | |
} |
| 205 | |
|
| 206 | |
public String iWantTo() { |
| 207 | 25 | return iWantTo; |
| 208 | |
} |
| 209 | |
|
| 210 | |
public String scenario() { |
| 211 | 260 | return scenario; |
| 212 | |
} |
| 213 | |
|
| 214 | |
public String givenStories() { |
| 215 | 275 | return givenStories; |
| 216 | |
} |
| 217 | |
|
| 218 | |
public String examplesTable() { |
| 219 | 275 | return examplesTable; |
| 220 | |
} |
| 221 | |
|
| 222 | |
public String examplesTableRow() { |
| 223 | 37 | return examplesTableRow; |
| 224 | |
} |
| 225 | |
|
| 226 | |
public String examplesTableHeaderSeparator() { |
| 227 | 161 | return examplesTableHeaderSeparator; |
| 228 | |
} |
| 229 | |
|
| 230 | |
public String examplesTableValueSeparator() { |
| 231 | 146 | return examplesTableValueSeparator; |
| 232 | |
} |
| 233 | |
|
| 234 | |
public String examplesTableIgnorableSeparator() { |
| 235 | 146 | return examplesTableIgnorableSeparator; |
| 236 | |
} |
| 237 | |
|
| 238 | |
public String given() { |
| 239 | 3856 | return given; |
| 240 | |
} |
| 241 | |
|
| 242 | |
public String when() { |
| 243 | 3856 | return when; |
| 244 | |
} |
| 245 | |
|
| 246 | |
public String then() { |
| 247 | 3856 | return then; |
| 248 | |
} |
| 249 | |
|
| 250 | |
public String and() { |
| 251 | 3856 | return and; |
| 252 | |
} |
| 253 | |
|
| 254 | |
public String ignorable() { |
| 255 | 3868 | return ignorable; |
| 256 | |
} |
| 257 | |
|
| 258 | |
public String pending() { |
| 259 | 30 | return pending; |
| 260 | |
} |
| 261 | |
|
| 262 | |
public String notPerformed() { |
| 263 | 25 | return notPerformed; |
| 264 | |
} |
| 265 | |
|
| 266 | |
public String failed() { |
| 267 | 31 | return failed; |
| 268 | |
} |
| 269 | |
|
| 270 | |
public String dryRun() { |
| 271 | 13 | return dryRun; |
| 272 | |
} |
| 273 | |
|
| 274 | |
public String storyCancelled() { |
| 275 | 17 | return storyCancelled; |
| 276 | |
} |
| 277 | |
|
| 278 | |
public String duration() { |
| 279 | 17 | return duration; |
| 280 | |
} |
| 281 | |
|
| 282 | |
public List<String> outcomeFields() { |
| 283 | 13 | return asList(outcomeDescription, outcomeValue, outcomeMatcher, outcomeVerified); |
| 284 | |
} |
| 285 | |
|
| 286 | |
public String yes(){ |
| 287 | 0 | return yes; |
| 288 | |
} |
| 289 | |
|
| 290 | |
public String no(){ |
| 291 | 12 | return no; |
| 292 | |
} |
| 293 | |
|
| 294 | |
public String[] startingWords() { |
| 295 | 630 | Collection<String> words = startingWordsByType().values(); |
| 296 | 630 | return words.toArray(new String[words.size()]); |
| 297 | |
} |
| 298 | |
|
| 299 | |
public Map<StepType, String> startingWordsByType() { |
| 300 | 632 | return startingWordsByType; |
| 301 | |
} |
| 302 | |
|
| 303 | |
public boolean isAndStep(String stepAsString) { |
| 304 | 67 | String andWord = startingWordFor(StepType.AND); |
| 305 | 66 | return stepStartsWithWord(stepAsString, andWord); |
| 306 | |
} |
| 307 | |
|
| 308 | |
public boolean isIgnorableStep(String stepAsString) { |
| 309 | 15 | String ignorableWord = startingWordFor(StepType.IGNORABLE); |
| 310 | 15 | return stepStartsWithWord(stepAsString, ignorableWord); |
| 311 | |
} |
| 312 | |
|
| 313 | |
public String stepWithoutStartingWord(String stepAsString, StepType stepType) { |
| 314 | 127 | String startingWord = startingWord(stepAsString, stepType); |
| 315 | 121 | return stepAsString.substring(startingWord.length() + 1); |
| 316 | |
|
| 317 | |
} |
| 318 | |
|
| 319 | |
public String startingWord(String stepAsString, StepType stepType) throws StartingWordNotFound { |
| 320 | 128 | String wordForType = startingWordFor(stepType); |
| 321 | 128 | if (stepStartsWithWord(stepAsString, wordForType)) { |
| 322 | 115 | return wordForType; |
| 323 | |
} |
| 324 | 13 | String andWord = startingWordFor(StepType.AND); |
| 325 | 13 | if (stepStartsWithWord(stepAsString, andWord)) { |
| 326 | 7 | return andWord; |
| 327 | |
} |
| 328 | 6 | throw new StartingWordNotFound(stepAsString, stepType, startingWordsByType); |
| 329 | |
} |
| 330 | |
|
| 331 | |
public String startingWord(String stepAsString) throws StartingWordNotFound { |
| 332 | 0 | for (StepType stepType : startingWordsByType.keySet()) { |
| 333 | 0 | String wordForType = startingWordFor(stepType); |
| 334 | 0 | if (stepStartsWithWord(stepAsString, wordForType)) { |
| 335 | 0 | return wordForType; |
| 336 | |
} |
| 337 | 0 | } |
| 338 | 0 | String andWord = startingWordFor(StepType.AND); |
| 339 | 0 | if (stepStartsWithWord(stepAsString, andWord)) { |
| 340 | 0 | return andWord; |
| 341 | |
} |
| 342 | 0 | throw new StartingWordNotFound(stepAsString, startingWordsByType); |
| 343 | |
} |
| 344 | |
|
| 345 | |
public StepType stepTypeFor(String stepAsString) throws StartingWordNotFound { |
| 346 | 5 | for (StepType stepType : startingWordsByType.keySet()) { |
| 347 | 12 | String wordForType = startingWordFor(stepType); |
| 348 | 12 | if (stepStartsWithWord(stepAsString, wordForType)) { |
| 349 | 5 | return stepType; |
| 350 | |
} |
| 351 | 7 | } |
| 352 | 0 | throw new StartingWordNotFound(stepAsString, startingWordsByType); |
| 353 | |
} |
| 354 | |
|
| 355 | |
public boolean stepStartsWithWord(String step, String word) { |
| 356 | 237 | return step.startsWith(word + " "); |
| 357 | |
} |
| 358 | |
|
| 359 | |
public String startingWordFor(StepType stepType) { |
| 360 | 284 | String startingWord = startingWordsByType.get(stepType); |
| 361 | 284 | if (startingWord == null) { |
| 362 | 0 | throw new StartingWordNotFound(stepType, startingWordsByType); |
| 363 | |
} |
| 364 | 284 | return startingWord; |
| 365 | |
} |
| 366 | |
|
| 367 | |
@Override |
| 368 | |
public String toString() { |
| 369 | 3 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
| 370 | |
} |
| 371 | |
|
| 372 | |
@SuppressWarnings("serial") |
| 373 | |
public static class KeywordNotFound extends RuntimeException { |
| 374 | |
|
| 375 | |
public KeywordNotFound(String name, Map<String, String> keywords) { |
| 376 | 1 | super("Keyword " + name + " not found amongst " + keywords); |
| 377 | 1 | } |
| 378 | |
|
| 379 | |
} |
| 380 | |
|
| 381 | |
@SuppressWarnings("serial") |
| 382 | |
public static class StartingWordNotFound extends RuntimeException { |
| 383 | |
|
| 384 | |
public StartingWordNotFound(String step, StepType stepType, Map<StepType, String> startingWordsByType) { |
| 385 | 6 | super("No starting word found for step '" + step + "' of type '" + stepType + "' amongst '" |
| 386 | |
+ startingWordsByType + "'"); |
| 387 | 6 | } |
| 388 | |
|
| 389 | |
public StartingWordNotFound(String step, Map<StepType, String> startingWordsByType) { |
| 390 | 0 | super("No starting word found for step '" + step + "' amongst '" + startingWordsByType + "'"); |
| 391 | 0 | } |
| 392 | |
|
| 393 | |
public StartingWordNotFound(StepType stepType, Map<StepType, String> startingWordsByType) { |
| 394 | 2 | super("No starting word found of type '" + stepType + "' amongst '" + startingWordsByType + "'"); |
| 395 | 2 | } |
| 396 | |
|
| 397 | |
} |
| 398 | |
|
| 399 | |
} |