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