Coverage Report - org.jbehave.core.configuration.Keywords
 
Classes in this File Line Coverage Branch Coverage Complexity
Keywords
90%
109/120
55%
10/18
1.463
Keywords$KeywordNotFound
100%
2/2
N/A
1.463
Keywords$StartingWordNotFound
66%
4/6
N/A
1.463
 
 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  
 
 45  1
     public static final List<String> KEYWORDS = asList(META, META_PROPERTY, NARRATIVE, IN_ORDER_TO, AS_A, I_WANT_TO,
 46  
             SCENARIO, GIVEN_STORIES, EXAMPLES_TABLE, EXAMPLES_TABLE_ROW, EXAMPLES_TABLE_HEADER_SEPARATOR,
 47  
             EXAMPLES_TABLE_VALUE_SEPARATOR, EXAMPLES_TABLE_IGNORABLE_SEPARATOR, GIVEN, WHEN, THEN, AND, IGNORABLE,
 48  
             PENDING, NOT_PERFORMED, FAILED, DRY_RUN);
 49  
 
 50  
     private final String meta;
 51  
     private final String metaProperty;
 52  
     private final String narrative;
 53  
     private final String inOrderTo;
 54  
     private final String asA;
 55  
     private final String iWantTo;
 56  
     private final String scenario;
 57  
     private final String givenStories;
 58  
     private final String examplesTable;
 59  
     private final String examplesTableRow;
 60  
     private final String examplesTableHeaderSeparator;
 61  
     private final String examplesTableValueSeparator;
 62  
     private final String examplesTableIgnorableSeparator;
 63  
     private final String given;
 64  
     private final String when;
 65  
     private final String then;
 66  
     private final String and;
 67  
     private final String ignorable;
 68  
     private final String pending;
 69  
     private final String notPerformed;
 70  
     private final String failed;
 71  
     private final String dryRun;
 72  3842
     private final Map<StepType, String> startingWordsByType = new HashMap<StepType, String>();
 73  
 
 74  
     public static Map<String, String> defaultKeywords() {
 75  1
         Map<String, String> keywords = new HashMap<String, String>();
 76  1
         keywords.put(META, "Meta:");
 77  1
         keywords.put(META_PROPERTY, "@");
 78  1
         keywords.put(NARRATIVE, "Narrative:");
 79  1
         keywords.put(IN_ORDER_TO, "In order to:");
 80  1
         keywords.put(AS_A, "As a:");
 81  1
         keywords.put(I_WANT_TO, "I want to:");
 82  1
         keywords.put(SCENARIO, "Scenario:");
 83  1
         keywords.put(GIVEN_STORIES, "GivenStories:");
 84  1
         keywords.put(EXAMPLES_TABLE, "Examples:");
 85  1
         keywords.put(EXAMPLES_TABLE_ROW, "Example:");
 86  1
         keywords.put(EXAMPLES_TABLE_HEADER_SEPARATOR, "|");
 87  1
         keywords.put(EXAMPLES_TABLE_VALUE_SEPARATOR, "|");
 88  1
         keywords.put(EXAMPLES_TABLE_IGNORABLE_SEPARATOR, "|--");
 89  1
         keywords.put(GIVEN, "Given");
 90  1
         keywords.put(WHEN, "When");
 91  1
         keywords.put(THEN, "Then");
 92  1
         keywords.put(AND, "And");
 93  1
         keywords.put(IGNORABLE, "!--");
 94  1
         keywords.put(PENDING, "PENDING");
 95  1
         keywords.put(NOT_PERFORMED, "NOT PERFORMED");
 96  1
         keywords.put(FAILED, "FAILED");
 97  1
         keywords.put(DRY_RUN, "DRY RUN");
 98  1
         return keywords;
 99  
     }
 100  
 
 101  
     /**
 102  
      * Creates Keywords with default values {@link #defaultKeywords()}
 103  
      */
 104  
     public Keywords() {
 105  1
         this(defaultKeywords());
 106  1
     }
 107  
 
 108  
     /**
 109  
      * Creates Keywords with provided keywords Map and Encoding
 110  
      * 
 111  
      * @param keywords the Map of keywords indexed by their name
 112  
      */
 113  3842
     public Keywords(Map<String, String> keywords) {
 114  3842
         this.meta = keyword(META, keywords);
 115  3841
         this.metaProperty = keyword(META_PROPERTY, keywords);
 116  3841
         this.narrative = keyword(NARRATIVE, keywords);
 117  3841
         this.inOrderTo = keyword(IN_ORDER_TO, keywords);
 118  3841
         this.asA = keyword(AS_A, keywords);
 119  3841
         this.iWantTo = keyword(I_WANT_TO, keywords);
 120  3841
         this.scenario = keyword(SCENARIO, keywords);
 121  3841
         this.givenStories = keyword(GIVEN_STORIES, keywords);
 122  3841
         this.examplesTable = keyword(EXAMPLES_TABLE, keywords);
 123  3841
         this.examplesTableRow = keyword(EXAMPLES_TABLE_ROW, keywords);
 124  3841
         this.examplesTableHeaderSeparator = keyword(EXAMPLES_TABLE_HEADER_SEPARATOR, keywords);
 125  3841
         this.examplesTableValueSeparator = keyword(EXAMPLES_TABLE_VALUE_SEPARATOR, keywords);
 126  3841
         this.examplesTableIgnorableSeparator = keyword(EXAMPLES_TABLE_IGNORABLE_SEPARATOR, keywords);
 127  3841
         this.given = keyword(GIVEN, keywords);
 128  3841
         this.when = keyword(WHEN, keywords);
 129  3841
         this.then = keyword(THEN, keywords);
 130  3841
         this.and = keyword(AND, keywords);
 131  3841
         this.ignorable = keyword(IGNORABLE, keywords);
 132  3841
         this.pending = keyword(PENDING, keywords);
 133  3841
         this.notPerformed = keyword(NOT_PERFORMED, keywords);
 134  3841
         this.failed = keyword(FAILED, keywords);
 135  3841
         this.dryRun = keyword(DRY_RUN, keywords);
 136  
 
 137  3841
         startingWordsByType.put(StepType.GIVEN, given());
 138  3841
         startingWordsByType.put(StepType.WHEN, when());
 139  3841
         startingWordsByType.put(StepType.THEN, then());
 140  3841
         startingWordsByType.put(StepType.AND, and());
 141  3841
         startingWordsByType.put(StepType.IGNORABLE, ignorable());
 142  
 
 143  3841
     }
 144  
 
 145  
     private String keyword(String name, Map<String, String> keywords) {
 146  84503
         String keyword = keywords.get(name);
 147  84503
         if (keyword == null) {
 148  1
             throw new KeywordNotFound(name, keywords);
 149  
         }
 150  84502
         return keyword;
 151  
     }
 152  
 
 153  
     public String meta() {
 154  314
         return meta;
 155  
     }
 156  
 
 157  
     public String metaProperty() {
 158  45
         return metaProperty;
 159  
     }
 160  
 
 161  
     public String narrative() {
 162  84
         return narrative;
 163  
     }
 164  
 
 165  
     public String inOrderTo() {
 166  17
         return inOrderTo;
 167  
     }
 168  
 
 169  
     public String asA() {
 170  17
         return asA;
 171  
     }
 172  
 
 173  
     public String iWantTo() {
 174  17
         return iWantTo;
 175  
     }
 176  
 
 177  
     public String scenario() {
 178  236
         return scenario;
 179  
     }
 180  
 
 181  
     public String givenStories() {
 182  263
         return givenStories;
 183  
     }
 184  
 
 185  
     public String examplesTable() {
 186  263
         return examplesTable;
 187  
     }
 188  
 
 189  
     public String examplesTableRow() {
 190  27
         return examplesTableRow;
 191  
     }
 192  
 
 193  
     public String examplesTableHeaderSeparator() {
 194  145
         return examplesTableHeaderSeparator;
 195  
     }
 196  
 
 197  
     public String examplesTableValueSeparator() {
 198  134
         return examplesTableValueSeparator;
 199  
     }
 200  
 
 201  
     public String examplesTableIgnorableSeparator() {
 202  134
         return examplesTableIgnorableSeparator;
 203  
     }
 204  
 
 205  
     public String given() {
 206  3846
         return given;
 207  
     }
 208  
 
 209  
     public String when() {
 210  3846
         return when;
 211  
     }
 212  
 
 213  
     public String then() {
 214  3846
         return then;
 215  
     }
 216  
 
 217  
     public String and() {
 218  3846
         return and;
 219  
     }
 220  
 
 221  
     public String ignorable() {
 222  3858
         return ignorable;
 223  
     }
 224  
 
 225  
     public String pending() {
 226  22
         return pending;
 227  
     }
 228  
 
 229  
     public String notPerformed() {
 230  19
         return notPerformed;
 231  
     }
 232  
 
 233  
     public String failed() {
 234  22
         return failed;
 235  
     }
 236  
 
 237  
     public String dryRun() {
 238  13
         return dryRun;
 239  
     }
 240  
 
 241  
     public String[] startingWords() {
 242  620
         Collection<String> words = startingWordsByType().values();
 243  620
         return words.toArray(new String[words.size()]);
 244  
     }
 245  
 
 246  
     public Map<StepType, String> startingWordsByType() {
 247  622
         return startingWordsByType;
 248  
     }
 249  
 
 250  
     public boolean isAndStep(String stepAsString) {
 251  33
         String andWord = startingWordFor(StepType.AND);
 252  32
         return stepStartsWithWord(stepAsString, andWord);
 253  
     }
 254  
 
 255  
     public boolean isIgnorableStep(String stepAsString) {
 256  11
         String ignorableWord = startingWordFor(StepType.IGNORABLE);
 257  11
         return stepStartsWithWord(stepAsString, ignorableWord);
 258  
     }
 259  
 
 260  
     public String stepWithoutStartingWord(String stepAsString, StepType stepType) {
 261  91
         String startingWord = startingWord(stepAsString, stepType);
 262  85
         return stepAsString.substring(startingWord.length() + 1); // 1 for the
 263  
                                                                   // space after
 264  
     }
 265  
 
 266  
     public String startingWord(String stepAsString, StepType stepType) throws StartingWordNotFound {
 267  92
         String wordForType = startingWordFor(stepType);
 268  92
         if (stepStartsWithWord(stepAsString, wordForType)) {
 269  79
             return wordForType;
 270  
         }
 271  13
         String andWord = startingWordFor(StepType.AND);
 272  13
         if (stepStartsWithWord(stepAsString, andWord)) {
 273  7
             return andWord;
 274  
         }
 275  6
         throw new StartingWordNotFound(stepAsString, stepType, startingWordsByType);
 276  
     }
 277  
 
 278  
     public String startingWord(String stepAsString) throws StartingWordNotFound {
 279  0
         for (StepType stepType : startingWordsByType.keySet()) {
 280  0
             String wordForType = startingWordFor(stepType);
 281  0
             if (stepStartsWithWord(stepAsString, wordForType)) {
 282  0
                 return wordForType;
 283  
             }
 284  0
         }
 285  0
         String andWord = startingWordFor(StepType.AND);
 286  0
         if (stepStartsWithWord(stepAsString, andWord)) {
 287  0
             return andWord;
 288  
         }
 289  0
         throw new StartingWordNotFound(stepAsString, startingWordsByType);
 290  
     }
 291  
 
 292  
     public StepType stepTypeFor(String stepAsString) throws StartingWordNotFound {
 293  3
         for (StepType stepType : startingWordsByType.keySet()) {
 294  4
             String wordForType = startingWordFor(stepType);
 295  4
             if (stepStartsWithWord(stepAsString, wordForType)) {
 296  3
                 return stepType;
 297  
             }
 298  1
         }
 299  0
         throw new StartingWordNotFound(stepAsString, startingWordsByType);
 300  
     }
 301  
 
 302  
     public boolean stepStartsWithWord(String step, String word) {
 303  153
         return step.startsWith(word + " "); // space after qualifies it as word
 304  
     }
 305  
 
 306  
     public String startingWordFor(StepType stepType) {
 307  198
         String startingWord = startingWordsByType.get(stepType);
 308  198
         if (startingWord == null) {
 309  0
             throw new StartingWordNotFound(stepType, startingWordsByType);
 310  
         }
 311  198
         return startingWord;
 312  
     }
 313  
 
 314  
     @Override
 315  
     public String toString() {
 316  2
         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
 317  
     }
 318  
 
 319  
     @SuppressWarnings("serial")
 320  
     public static class KeywordNotFound extends RuntimeException {
 321  
 
 322  
         public KeywordNotFound(String name, Map<String, String> keywords) {
 323  1
             super("Keyword " + name + " not found amongst " + keywords);
 324  1
         }
 325  
 
 326  
     }
 327  
 
 328  
     @SuppressWarnings("serial")
 329  
     public static class StartingWordNotFound extends RuntimeException {
 330  
 
 331  
         public StartingWordNotFound(String step, StepType stepType, Map<StepType, String> startingWordsByType) {
 332  6
             super("No starting word found for step '" + step + "' of type '" + stepType + "' amongst '"
 333  
                     + startingWordsByType + "'");
 334  6
         }
 335  
 
 336  
         public StartingWordNotFound(String step, Map<StepType, String> startingWordsByType) {
 337  0
             super("No starting word found for step '" + step + "' amongst '" + startingWordsByType + "'");
 338  0
         }
 339  
 
 340  
         public StartingWordNotFound(StepType stepType, Map<StepType, String> startingWordsByType) {
 341  2
             super("No starting word found of type '" + stepType + "' amongst '" + startingWordsByType + "'");
 342  2
         }
 343  
 
 344  
     }
 345  
 
 346  
 }