Coverage Report - org.jbehave.core.configuration.Keywords
 
Classes in this File Line Coverage Branch Coverage Complexity
Keywords
100%
87/87
100%
2/2
1.067
Keywords$KeywordNotFound
100%
2/2
N/A
1.067
 
 1  
 package org.jbehave.core.configuration;
 2  
 
 3  
 import static java.util.Arrays.asList;
 4  
 
 5  
 import java.util.Collection;
 6  
 import java.util.HashMap;
 7  
 import java.util.List;
 8  
 import java.util.Map;
 9  
 
 10  
 import org.apache.commons.lang.builder.ToStringBuilder;
 11  
 import org.apache.commons.lang.builder.ToStringStyle;
 12  
 import org.jbehave.core.steps.StepType;
 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  
 
 73  
     public static Map<String, String> defaultKeywords() {
 74  1
         Map<String, String> keywords = new HashMap<String, String>();
 75  1
         keywords.put(META, "Meta:");
 76  1
         keywords.put(META_PROPERTY, "@");
 77  1
         keywords.put(NARRATIVE, "Narrative:");
 78  1
         keywords.put(IN_ORDER_TO, "In order to:");
 79  1
         keywords.put(AS_A, "As a:");
 80  1
         keywords.put(I_WANT_TO, "I want to:");
 81  1
         keywords.put(SCENARIO, "Scenario:");
 82  1
         keywords.put(GIVEN_STORIES, "GivenStories:");
 83  1
         keywords.put(EXAMPLES_TABLE, "Examples:");
 84  1
         keywords.put(EXAMPLES_TABLE_ROW, "Example:");
 85  1
         keywords.put(EXAMPLES_TABLE_HEADER_SEPARATOR, "|");
 86  1
         keywords.put(EXAMPLES_TABLE_VALUE_SEPARATOR, "|");
 87  1
         keywords.put(EXAMPLES_TABLE_IGNORABLE_SEPARATOR, "|--");
 88  1
         keywords.put(GIVEN, "Given");
 89  1
         keywords.put(WHEN, "When");
 90  1
         keywords.put(THEN, "Then");
 91  1
         keywords.put(AND, "And");
 92  1
         keywords.put(IGNORABLE, "!--");
 93  1
         keywords.put(PENDING, "PENDING");
 94  1
         keywords.put(NOT_PERFORMED, "NOT PERFORMED");
 95  1
         keywords.put(FAILED, "FAILED");
 96  1
         keywords.put(DRY_RUN, "DRY RUN");
 97  1
         return keywords;
 98  
     }
 99  
 
 100  
     /**
 101  
      * Creates Keywords with default values {@link #defaultKeywords()}
 102  
      */
 103  
     public Keywords() {
 104  1
         this(defaultKeywords());
 105  1
     }
 106  
 
 107  
     /**
 108  
      * Creates Keywords with provided keywords Map and Encoding
 109  
      * 
 110  
      * @param keywords
 111  
      *            the Map of keywords indexed by their name
 112  
      */
 113  2314
     public Keywords(Map<String, String> keywords) {
 114  2314
         this.meta = keyword(META, keywords);
 115  2313
         this.metaProperty = keyword(META_PROPERTY, keywords);
 116  2313
         this.narrative = keyword(NARRATIVE, keywords);
 117  2313
         this.inOrderTo = keyword(IN_ORDER_TO, keywords);
 118  2313
         this.asA = keyword(AS_A, keywords);
 119  2313
         this.iWantTo = keyword(I_WANT_TO, keywords);
 120  2313
         this.scenario = keyword(SCENARIO, keywords);
 121  2313
         this.givenStories = keyword(GIVEN_STORIES, keywords);
 122  2313
         this.examplesTable = keyword(EXAMPLES_TABLE, keywords);
 123  2313
         this.examplesTableRow = keyword(EXAMPLES_TABLE_ROW, keywords);
 124  2313
         this.examplesTableHeaderSeparator = keyword(EXAMPLES_TABLE_HEADER_SEPARATOR, keywords);
 125  2313
         this.examplesTableValueSeparator = keyword(EXAMPLES_TABLE_VALUE_SEPARATOR, keywords);
 126  2313
         this.examplesTableIgnorableSeparator = keyword(EXAMPLES_TABLE_IGNORABLE_SEPARATOR, keywords);
 127  2313
         this.given = keyword(GIVEN, keywords);
 128  2313
         this.when = keyword(WHEN, keywords);
 129  2313
         this.then = keyword(THEN, keywords);
 130  2313
         this.and = keyword(AND, keywords);
 131  2313
         this.ignorable = keyword(IGNORABLE, keywords);
 132  2313
         this.pending = keyword(PENDING, keywords);
 133  2313
         this.notPerformed = keyword(NOT_PERFORMED, keywords);
 134  2313
         this.failed = keyword(FAILED, keywords);
 135  2313
         this.dryRun = keyword(DRY_RUN, keywords);
 136  2313
     }
 137  
 
 138  
     private String keyword(String name, Map<String, String> keywords) {
 139  50887
         String keyword = keywords.get(name);
 140  50887
         if (keyword == null) {
 141  1
             throw new KeywordNotFound(name, keywords);
 142  
         }
 143  50886
         return keyword;
 144  
     }
 145  
     
 146  
 
 147  
     public String meta() {
 148  302
         return meta;
 149  
     }
 150  
 
 151  
     public String metaProperty() {
 152  43
         return metaProperty;
 153  
     }
 154  
 
 155  
     public String narrative() {
 156  75
         return narrative;
 157  
     }
 158  
 
 159  
     public String inOrderTo() {
 160  17
         return inOrderTo;
 161  
     }
 162  
 
 163  
     public String asA() {
 164  17
         return asA;
 165  
     }
 166  
 
 167  
     public String iWantTo() {
 168  17
         return iWantTo;
 169  
     }
 170  
 
 171  
     public String scenario() {
 172  221
         return scenario;
 173  
     }
 174  
 
 175  
     public String givenStories() {
 176  257
         return givenStories;
 177  
     }
 178  
 
 179  
     public String examplesTable() {
 180  257
         return examplesTable;
 181  
     }
 182  
 
 183  
     public String examplesTableRow() {
 184  27
         return examplesTableRow;
 185  
     }
 186  
 
 187  
     public String examplesTableHeaderSeparator() {
 188  139
         return examplesTableHeaderSeparator;
 189  
     }
 190  
 
 191  
     public String examplesTableValueSeparator() {
 192  130
         return examplesTableValueSeparator;
 193  
     }
 194  
 
 195  
     public String examplesTableIgnorableSeparator() {
 196  130
         return examplesTableIgnorableSeparator;
 197  
     }
 198  
 
 199  
     public String given() {
 200  697
         return given;
 201  
     }
 202  
 
 203  
     public String when() {
 204  697
         return when;
 205  
     }
 206  
 
 207  
     public String then() {
 208  697
         return then;
 209  
     }
 210  
 
 211  
     public String and() {
 212  697
         return and;
 213  
     }
 214  
 
 215  
     public String ignorable() {
 216  697
         return ignorable;
 217  
     }
 218  
 
 219  
     public String pending() {
 220  22
         return pending;
 221  
     }
 222  
 
 223  
     public String notPerformed() {
 224  19
         return notPerformed;
 225  
     }
 226  
 
 227  
     public String failed() {
 228  20
         return failed;
 229  
     }
 230  
 
 231  
     public String dryRun() {
 232  13
         return dryRun;
 233  
     }
 234  
 
 235  
     public String[] startingWords() {
 236  605
         Collection<String> words = startingWordsByType().values();
 237  605
         return words.toArray(new String[words.size()]);
 238  
     }
 239  
 
 240  
     public Map<StepType, String> startingWordsByType() {
 241  692
         Map<StepType, String> words = new HashMap<StepType, String>();
 242  692
         words.put(StepType.GIVEN, given());
 243  692
         words.put(StepType.WHEN, when());
 244  692
         words.put(StepType.THEN, then());
 245  692
         words.put(StepType.AND, and());
 246  692
         words.put(StepType.IGNORABLE, ignorable());
 247  692
         return words;
 248  
     }
 249  
 
 250  
     @Override
 251  
     public String toString() {
 252  3
         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
 253  
     }
 254  
 
 255  
     @SuppressWarnings("serial")
 256  
     public static class KeywordNotFound extends RuntimeException {
 257  
 
 258  
         public KeywordNotFound(String name, Map<String, String> keywords) {
 259  1
             super("Keyword " + name + " not found amongst " + keywords);
 260  1
         }
 261  
 
 262  
     }
 263  
 
 264  
 }