Coverage Report - org.jbehave.core.configuration.Keywords
 
Classes in this File Line Coverage Branch Coverage Complexity
Keywords
90%
107/118
55%
10/18
1.475
Keywords$KeywordNotFound
100%
2/2
N/A
1.475
Keywords$StartingWordNotFound
66%
4/6
N/A
1.475
 
 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, SCENARIO,
 46  
             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  2777
     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
 112  
      *            the Map of keywords indexed by their name
 113  
      */
 114  2777
     public Keywords(Map<String, String> keywords) {
 115  2777
         this.meta = keyword(META, keywords);
 116  2776
         this.metaProperty = keyword(META_PROPERTY, keywords);
 117  2776
         this.narrative = keyword(NARRATIVE, keywords);
 118  2776
         this.inOrderTo = keyword(IN_ORDER_TO, keywords);
 119  2776
         this.asA = keyword(AS_A, keywords);
 120  2776
         this.iWantTo = keyword(I_WANT_TO, keywords);
 121  2776
         this.scenario = keyword(SCENARIO, keywords);
 122  2776
         this.givenStories = keyword(GIVEN_STORIES, keywords);
 123  2776
         this.examplesTable = keyword(EXAMPLES_TABLE, keywords);
 124  2776
         this.examplesTableRow = keyword(EXAMPLES_TABLE_ROW, keywords);
 125  2776
         this.examplesTableHeaderSeparator = keyword(EXAMPLES_TABLE_HEADER_SEPARATOR, keywords);
 126  2776
         this.examplesTableValueSeparator = keyword(EXAMPLES_TABLE_VALUE_SEPARATOR, keywords);
 127  2776
         this.examplesTableIgnorableSeparator = keyword(EXAMPLES_TABLE_IGNORABLE_SEPARATOR, keywords);
 128  2776
         this.given = keyword(GIVEN, keywords);
 129  2776
         this.when = keyword(WHEN, keywords);
 130  2776
         this.then = keyword(THEN, keywords);
 131  2776
         this.and = keyword(AND, keywords);
 132  2776
         this.ignorable = keyword(IGNORABLE, keywords);
 133  2776
         this.pending = keyword(PENDING, keywords);
 134  2776
         this.notPerformed = keyword(NOT_PERFORMED, keywords);
 135  2776
         this.failed = keyword(FAILED, keywords);
 136  2776
         this.dryRun = keyword(DRY_RUN, keywords);
 137  
         
 138  2776
         startingWordsByType.put(StepType.GIVEN, given());
 139  2776
         startingWordsByType.put(StepType.WHEN, when());
 140  2776
         startingWordsByType.put(StepType.THEN, then());
 141  2776
         startingWordsByType.put(StepType.AND, and());
 142  2776
         startingWordsByType.put(StepType.IGNORABLE, ignorable());
 143  
 
 144  2776
     }
 145  
 
 146  
     private String keyword(String name, Map<String, String> keywords) {
 147  61073
         String keyword = keywords.get(name);
 148  61073
         if (keyword == null) {
 149  1
             throw new KeywordNotFound(name, keywords);
 150  
         }
 151  61072
         return keyword;
 152  
     }
 153  
     
 154  
 
 155  
     public String meta() {
 156  302
         return meta;
 157  
     }
 158  
 
 159  
     public String metaProperty() {
 160  43
         return metaProperty;
 161  
     }
 162  
 
 163  
     public String narrative() {
 164  75
         return narrative;
 165  
     }
 166  
 
 167  
     public String inOrderTo() {
 168  17
         return inOrderTo;
 169  
     }
 170  
 
 171  
     public String asA() {
 172  17
         return asA;
 173  
     }
 174  
 
 175  
     public String iWantTo() {
 176  17
         return iWantTo;
 177  
     }
 178  
 
 179  
     public String scenario() {
 180  221
         return scenario;
 181  
     }
 182  
 
 183  
     public String givenStories() {
 184  257
         return givenStories;
 185  
     }
 186  
 
 187  
     public String examplesTable() {
 188  257
         return examplesTable;
 189  
     }
 190  
 
 191  
     public String examplesTableRow() {
 192  27
         return examplesTableRow;
 193  
     }
 194  
 
 195  
     public String examplesTableHeaderSeparator() {
 196  139
         return examplesTableHeaderSeparator;
 197  
     }
 198  
 
 199  
     public String examplesTableValueSeparator() {
 200  130
         return examplesTableValueSeparator;
 201  
     }
 202  
 
 203  
     public String examplesTableIgnorableSeparator() {
 204  130
         return examplesTableIgnorableSeparator;
 205  
     }
 206  
 
 207  
     public String given() {
 208  2781
         return given;
 209  
     }
 210  
 
 211  
     public String when() {
 212  2781
         return when;
 213  
     }
 214  
 
 215  
     public String then() {
 216  2781
         return then;
 217  
     }
 218  
 
 219  
     public String and() {
 220  2781
         return and;
 221  
     }
 222  
 
 223  
     public String ignorable() {
 224  2781
         return ignorable;
 225  
     }
 226  
 
 227  
     public String pending() {
 228  22
         return pending;
 229  
     }
 230  
 
 231  
     public String notPerformed() {
 232  19
         return notPerformed;
 233  
     }
 234  
 
 235  
     public String failed() {
 236  20
         return failed;
 237  
     }
 238  
 
 239  
     public String dryRun() {
 240  13
         return dryRun;
 241  
     }
 242  
 
 243  
     public String[] startingWords() {
 244  605
         Collection<String> words = startingWordsByType().values();
 245  605
         return words.toArray(new String[words.size()]);
 246  
     }
 247  
 
 248  
     public Map<StepType, String> startingWordsByType() {
 249  607
         return startingWordsByType;
 250  
     }
 251  
 
 252  
     public boolean isAndStep(String stepAsString) {
 253  33
         String andWord = startingWordFor(StepType.AND);
 254  32
         return stepStartsWithWord(stepAsString, andWord);
 255  
     }
 256  
     
 257  
     public String stepWithoutStartingWord(String stepAsString, StepType stepType) {
 258  80
         String startingWord = startingWord(stepAsString, stepType);
 259  74
         return stepAsString.substring(startingWord.length() + 1); // 1 for the space after
 260  
     }
 261  
 
 262  
     public String startingWord(String stepAsString, StepType stepType) throws StartingWordNotFound {
 263  81
         String wordForType = startingWordFor(stepType);
 264  81
         if (stepStartsWithWord(stepAsString, wordForType)) {
 265  68
             return wordForType;
 266  
         }
 267  13
         String andWord = startingWordFor(StepType.AND);
 268  13
         if (stepStartsWithWord(stepAsString, andWord)) {
 269  7
             return andWord;
 270  
         }
 271  6
         throw new StartingWordNotFound(stepAsString, stepType, startingWordsByType);
 272  
     }
 273  
 
 274  
     public String startingWord(String stepAsString) throws StartingWordNotFound {
 275  0
         for (StepType stepType : startingWordsByType.keySet()) {
 276  0
             String wordForType = startingWordFor(stepType);
 277  0
             if (stepStartsWithWord(stepAsString, wordForType)) {
 278  0
                 return wordForType;
 279  
             }
 280  0
         }
 281  0
         String andWord = startingWordFor(StepType.AND);
 282  0
         if (stepStartsWithWord(stepAsString, andWord)) {
 283  0
             return andWord;
 284  
         }
 285  0
         throw new StartingWordNotFound(stepAsString, startingWordsByType);
 286  
     }
 287  
 
 288  
     public StepType stepTypeFor(String stepAsString) throws StartingWordNotFound {
 289  3
         for (StepType stepType : startingWordsByType.keySet()) {
 290  11
             String wordForType = startingWordFor(stepType);
 291  11
             if (stepStartsWithWord(stepAsString, wordForType)) {
 292  3
                 return stepType;
 293  
             }
 294  8
         }
 295  0
         throw new StartingWordNotFound(stepAsString, startingWordsByType);
 296  
     }
 297  
 
 298  
 
 299  
     public boolean stepStartsWithWord(String step, String word) {
 300  138
         return step.startsWith(word + " "); // space after qualifies it as word
 301  
     }
 302  
 
 303  
     public String startingWordFor(StepType stepType) {
 304  156
         String startingWord = startingWordsByType.get(stepType);
 305  156
         if (startingWord == null) {
 306  0
             throw new StartingWordNotFound(stepType, startingWordsByType);
 307  
         }
 308  156
         return startingWord;
 309  
     }
 310  
 
 311  
     @Override
 312  
     public String toString() {
 313  3
         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
 314  
     }
 315  
 
 316  
     @SuppressWarnings("serial")
 317  
     public static class KeywordNotFound extends RuntimeException {
 318  
 
 319  
         public KeywordNotFound(String name, Map<String, String> keywords) {
 320  1
             super("Keyword " + name + " not found amongst " + keywords);
 321  1
         }
 322  
 
 323  
     }
 324  
     
 325  
     @SuppressWarnings("serial")
 326  
     public static class StartingWordNotFound extends RuntimeException {
 327  
 
 328  
         public StartingWordNotFound(String step, StepType stepType, Map<StepType, String> startingWordsByType) {
 329  6
             super("No starting word found for step '" + step + "' of type '" + stepType + "' amongst '"
 330  
                     + startingWordsByType + "'");
 331  6
         }
 332  
 
 333  
         public StartingWordNotFound(StepType stepType, Map<StepType, String> startingWordsByType) {
 334  2
             super("No starting word found of type '" + stepType + "' amongst '" + startingWordsByType + "'");
 335  2
         }
 336  
 
 337  0
         public StartingWordNotFound(String stepAsString, Map<StepType, String> startingWordsByType) {
 338  
             // TODO Auto-generated constructor stub
 339  0
         }
 340  
 
 341  
     }
 342  
 
 343  
 
 344  
 
 345  
 }