Coverage Report - org.jbehave.core.configuration.Keywords
 
Classes in this File Line Coverage Branch Coverage Complexity
Keywords
91%
129/141
55%
10/18
1.413
Keywords$KeywordNotFound
100%
2/2
N/A
1.413
Keywords$StartingWordNotFound
66%
4/6
N/A
1.413
 
 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  
  * Provides the keywords which allow parsers to find steps in stories and match
 16  
  * those steps with candidates through the annotations. It provides the starting
 17  
  * words (Given, When, Then And, "!--") using in parsing, as well as providing
 18  
  * keywords used in reporting.
 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  3716
     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  
      * Creates Keywords with default values {@link #defaultKeywords()}
 128  
      */
 129  
     public Keywords() {
 130  1
         this(defaultKeywords());
 131  1
     }
 132  
 
 133  
     /**
 134  
      * Creates Keywords with provided keywords Map and Encoding
 135  
      * 
 136  
      * @param keywords the Map of keywords indexed by their name
 137  
      */
 138  3716
     public Keywords(Map<String, String> keywords) {
 139  3716
         this.meta = keyword(META, keywords);
 140  3715
         this.metaProperty = keyword(META_PROPERTY, keywords);
 141  3715
         this.narrative = keyword(NARRATIVE, keywords);
 142  3715
         this.inOrderTo = keyword(IN_ORDER_TO, keywords);
 143  3715
         this.asA = keyword(AS_A, keywords);
 144  3715
         this.iWantTo = keyword(I_WANT_TO, keywords);
 145  3715
         this.scenario = keyword(SCENARIO, keywords);
 146  3715
         this.givenStories = keyword(GIVEN_STORIES, keywords);
 147  3715
         this.examplesTable = keyword(EXAMPLES_TABLE, keywords);
 148  3715
         this.examplesTableRow = keyword(EXAMPLES_TABLE_ROW, keywords);
 149  3715
         this.examplesTableHeaderSeparator = keyword(EXAMPLES_TABLE_HEADER_SEPARATOR, keywords);
 150  3715
         this.examplesTableValueSeparator = keyword(EXAMPLES_TABLE_VALUE_SEPARATOR, keywords);
 151  3715
         this.examplesTableIgnorableSeparator = keyword(EXAMPLES_TABLE_IGNORABLE_SEPARATOR, keywords);
 152  3715
         this.given = keyword(GIVEN, keywords);
 153  3715
         this.when = keyword(WHEN, keywords);
 154  3715
         this.then = keyword(THEN, keywords);
 155  3715
         this.and = keyword(AND, keywords);
 156  3715
         this.ignorable = keyword(IGNORABLE, keywords);
 157  3715
         this.pending = keyword(PENDING, keywords);
 158  3715
         this.notPerformed = keyword(NOT_PERFORMED, keywords);
 159  3715
         this.failed = keyword(FAILED, keywords);
 160  3715
         this.dryRun = keyword(DRY_RUN, keywords);
 161  3715
         this.storyCancelled = keyword(STORY_CANCELLED, keywords);
 162  3715
         this.duration = keyword(DURATION, keywords);
 163  3715
         this.outcomeDescription = keyword(OUTCOME_DESCRIPTION, keywords);
 164  3715
         this.outcomeMatcher = keyword(OUTCOME_MATCHER, keywords);
 165  3715
         this.outcomeValue = keyword(OUTCOME_VALUE, keywords);
 166  3715
         this.outcomeVerified = keyword(OUTCOME_VERIFIED, keywords);
 167  3715
         this.yes = keyword(YES, keywords);
 168  3715
         this.no = keyword(NO, keywords);
 169  
         
 170  3715
         startingWordsByType.put(StepType.GIVEN, given());
 171  3715
         startingWordsByType.put(StepType.WHEN, when());
 172  3715
         startingWordsByType.put(StepType.THEN, then());
 173  3715
         startingWordsByType.put(StepType.AND, and());
 174  3715
         startingWordsByType.put(StepType.IGNORABLE, ignorable());
 175  
 
 176  3715
     }
 177  
 
 178  
     private String keyword(String name, Map<String, String> keywords) {
 179  111451
         String keyword = keywords.get(name);
 180  111451
         if (keyword == null) {
 181  1
             throw new KeywordNotFound(name, keywords);
 182  
         }
 183  111450
         return keyword;
 184  
     }
 185  
 
 186  
     public String meta() {
 187  325
         return meta;
 188  
     }
 189  
 
 190  
     public String metaProperty() {
 191  55
         return metaProperty;
 192  
     }
 193  
 
 194  
     public String narrative() {
 195  95
         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  252
         return scenario;
 212  
     }
 213  
 
 214  
     public String givenStories() {
 215  273
         return givenStories;
 216  
     }
 217  
 
 218  
     public String examplesTable() {
 219  273
         return examplesTable;
 220  
     }
 221  
 
 222  
     public String examplesTableRow() {
 223  37
         return examplesTableRow;
 224  
     }
 225  
 
 226  
     public String examplesTableHeaderSeparator() {
 227  160
         return examplesTableHeaderSeparator;
 228  
     }
 229  
 
 230  
     public String examplesTableValueSeparator() {
 231  145
         return examplesTableValueSeparator;
 232  
     }
 233  
 
 234  
     public String examplesTableIgnorableSeparator() {
 235  145
         return examplesTableIgnorableSeparator;
 236  
     }
 237  
 
 238  
     public String given() {
 239  3726
         return given;
 240  
     }
 241  
 
 242  
     public String when() {
 243  3726
         return when;
 244  
     }
 245  
 
 246  
     public String then() {
 247  3726
         return then;
 248  
     }
 249  
 
 250  
     public String and() {
 251  3726
         return and;
 252  
     }
 253  
 
 254  
     public String ignorable() {
 255  3738
         return ignorable;
 256  
     }
 257  
 
 258  
     public String pending() {
 259  28
         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  625
         Collection<String> words = startingWordsByType().values();
 296  625
         return words.toArray(new String[words.size()]);
 297  
     }
 298  
 
 299  
     public Map<StepType, String> startingWordsByType() {
 300  627
         return startingWordsByType;
 301  
     }
 302  
 
 303  
     public boolean isAndStep(String stepAsString) {
 304  40
         String andWord = startingWordFor(StepType.AND);
 305  39
         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  97
         String startingWord = startingWord(stepAsString, stepType);
 315  91
         return stepAsString.substring(startingWord.length() + 1); // 1 for the
 316  
                                                                   // space after
 317  
     }
 318  
 
 319  
     public String startingWord(String stepAsString, StepType stepType) throws StartingWordNotFound {
 320  98
         String wordForType = startingWordFor(stepType);
 321  98
         if (stepStartsWithWord(stepAsString, wordForType)) {
 322  85
             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  3
         for (StepType stepType : startingWordsByType.keySet()) {
 347  9
             String wordForType = startingWordFor(stepType);
 348  9
             if (stepStartsWithWord(stepAsString, wordForType)) {
 349  3
                 return stepType;
 350  
             }
 351  6
         }
 352  0
         throw new StartingWordNotFound(stepAsString, startingWordsByType);
 353  
     }
 354  
 
 355  
     public boolean stepStartsWithWord(String step, String word) {
 356  177
         return step.startsWith(word + " "); // space after qualifies it as word
 357  
     }
 358  
 
 359  
     public String startingWordFor(StepType stepType) {
 360  222
         String startingWord = startingWordsByType.get(stepType);
 361  222
         if (startingWord == null) {
 362  0
             throw new StartingWordNotFound(stepType, startingWordsByType);
 363  
         }
 364  222
         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  
 }