Coverage Report - org.jbehave.core.configuration.Keywords
 
Classes in this File Line Coverage Branch Coverage Complexity
Keywords
100%
78/78
100%
2/2
1.074
Keywords$KeywordNotFound
100%
2/2
N/A
1.074
 
 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 NARRATIVE = "Narrative";
 23  
     public static final String IN_ORDER_TO = "InOrderTo";
 24  
     public static final String AS_A = "AsA";
 25  
     public static final String I_WANT_TO = "IWantTo";
 26  
     public static final String SCENARIO = "Scenario";
 27  
     public static final String GIVEN_STORIES = "GivenStories";
 28  
     public static final String EXAMPLES_TABLE = "ExamplesTable";
 29  
     public static final String EXAMPLES_TABLE_ROW = "ExamplesTableRow";
 30  
     public static final String EXAMPLES_TABLE_HEADER_SEPARATOR = "ExamplesTableHeaderSeparator";
 31  
     public static final String EXAMPLES_TABLE_VALUE_SEPARATOR = "ExamplesTableValueSeparator";
 32  
     public static final String GIVEN = "Given";
 33  
     public static final String WHEN = "When";
 34  
     public static final String THEN = "Then";
 35  
     public static final String AND = "And";
 36  
     public static final String IGNORABLE = "Ignorable";
 37  
     public static final String PENDING = "Pending";
 38  
     public static final String NOT_PERFORMED = "NotPerformed";
 39  
     public static final String FAILED = "Failed";
 40  
     public static final String DRY_RUN = "DryRun";
 41  
 
 42  1
     public static final List<String> KEYWORDS = asList(NARRATIVE, IN_ORDER_TO, AS_A, I_WANT_TO, SCENARIO,
 43  
             GIVEN_STORIES, EXAMPLES_TABLE, EXAMPLES_TABLE_ROW, EXAMPLES_TABLE_HEADER_SEPARATOR,
 44  
             EXAMPLES_TABLE_VALUE_SEPARATOR, GIVEN, WHEN, THEN, AND, IGNORABLE, PENDING, NOT_PERFORMED, FAILED, DRY_RUN);
 45  
 
 46  
     private final String narrative;
 47  
     private final String inOrderTo;
 48  
     private final String asA;
 49  
     private final String iWantTo;
 50  
     private final String scenario;
 51  
     private final String givenStories;
 52  
     private final String examplesTable;
 53  
     private final String examplesTableRow;
 54  
     private final String examplesTableHeaderSeparator;
 55  
     private final String examplesTableValueSeparator;
 56  
     private final String given;
 57  
     private final String when;
 58  
     private final String then;
 59  
     private final String and;
 60  
     private final String ignorable;
 61  
     private final String pending;
 62  
     private final String notPerformed;
 63  
     private final String failed;
 64  
     private final String dryRun;
 65  
 
 66  
     public static Map<String, String> defaultKeywords() {
 67  1
         Map<String, String> keywords = new HashMap<String, String>();
 68  1
         keywords.put(NARRATIVE, "Narrative:");
 69  1
         keywords.put(IN_ORDER_TO, "In order to:");
 70  1
         keywords.put(AS_A, "As a:");
 71  1
         keywords.put(I_WANT_TO, "I want to:");
 72  1
         keywords.put(SCENARIO, "Scenario:");
 73  1
         keywords.put(GIVEN_STORIES, "GivenStories:");
 74  1
         keywords.put(EXAMPLES_TABLE, "Examples:");
 75  1
         keywords.put(EXAMPLES_TABLE_ROW, "Example:");
 76  1
         keywords.put(EXAMPLES_TABLE_HEADER_SEPARATOR, "|");
 77  1
         keywords.put(EXAMPLES_TABLE_VALUE_SEPARATOR, "|");
 78  1
         keywords.put(GIVEN, "Given");
 79  1
         keywords.put(WHEN, "When");
 80  1
         keywords.put(THEN, "Then");
 81  1
         keywords.put(AND, "And");
 82  1
         keywords.put(IGNORABLE, "!--");
 83  1
         keywords.put(PENDING, "PENDING");
 84  1
         keywords.put(NOT_PERFORMED, "NOT PERFORMED");
 85  1
         keywords.put(FAILED, "FAILED");
 86  1
         keywords.put(DRY_RUN, "DRY RUN");
 87  1
         return keywords;
 88  
     }
 89  
 
 90  
     /**
 91  
      * Creates Keywords with default values {@link #defaultKeywords()}
 92  
      */
 93  
     public Keywords() {
 94  1
         this(defaultKeywords());
 95  1
     }
 96  
 
 97  
     /**
 98  
      * Creates Keywords with provided keywords Map and Encoding
 99  
      * 
 100  
      * @param keywords
 101  
      *            the Map of keywords indexed by their name
 102  
      */
 103  1075
     public Keywords(Map<String, String> keywords) {
 104  1075
         this.narrative = keyword(NARRATIVE, keywords);
 105  1074
         this.inOrderTo = keyword(IN_ORDER_TO, keywords);
 106  1074
         this.asA = keyword(AS_A, keywords);
 107  1074
         this.iWantTo = keyword(I_WANT_TO, keywords);
 108  1074
         this.scenario = keyword(SCENARIO, keywords);
 109  1074
         this.givenStories = keyword(GIVEN_STORIES, keywords);
 110  1074
         this.examplesTable = keyword(EXAMPLES_TABLE, keywords);
 111  1074
         this.examplesTableRow = keyword(EXAMPLES_TABLE_ROW, keywords);
 112  1074
         this.examplesTableHeaderSeparator = keyword(EXAMPLES_TABLE_HEADER_SEPARATOR, keywords);
 113  1074
         this.examplesTableValueSeparator = keyword(EXAMPLES_TABLE_VALUE_SEPARATOR, keywords);
 114  1074
         this.given = keyword(GIVEN, keywords);
 115  1074
         this.when = keyword(WHEN, keywords);
 116  1074
         this.then = keyword(THEN, keywords);
 117  1074
         this.and = keyword(AND, keywords);
 118  1074
         this.ignorable = keyword(IGNORABLE, keywords);
 119  1074
         this.pending = keyword(PENDING, keywords);
 120  1074
         this.notPerformed = keyword(NOT_PERFORMED, keywords);
 121  1074
         this.failed = keyword(FAILED, keywords);
 122  1074
         this.dryRun = keyword(DRY_RUN, keywords);
 123  1074
     }
 124  
 
 125  
     private String keyword(String name, Map<String, String> keywords) {
 126  20407
         String keyword = keywords.get(name);
 127  20407
         if (keyword == null) {
 128  1
             throw new KeywordNotFound(name, keywords);
 129  
         }
 130  20406
         return keyword;
 131  
     }
 132  
 
 133  
     public String narrative() {
 134  47
         return narrative;
 135  
     }
 136  
 
 137  
     public String inOrderTo() {
 138  17
         return inOrderTo;
 139  
     }
 140  
 
 141  
     public String asA() {
 142  17
         return asA;
 143  
     }
 144  
 
 145  
     public String iWantTo() {
 146  17
         return iWantTo;
 147  
     }
 148  
 
 149  
     public String scenario() {
 150  181
         return scenario;
 151  
     }
 152  
 
 153  
     public String givenStories() {
 154  131
         return givenStories;
 155  
     }
 156  
 
 157  
     public String examplesTable() {
 158  247
         return examplesTable;
 159  
     }
 160  
 
 161  
     public String examplesTableRow() {
 162  27
         return examplesTableRow;
 163  
     }
 164  
 
 165  
     public String examplesTableHeaderSeparator() {
 166  119
         return examplesTableHeaderSeparator;
 167  
     }
 168  
 
 169  
     public String examplesTableValueSeparator() {
 170  119
         return examplesTableValueSeparator;
 171  
     }
 172  
 
 173  
     public String given() {
 174  544
         return given;
 175  
     }
 176  
 
 177  
     public String when() {
 178  544
         return when;
 179  
     }
 180  
 
 181  
     public String then() {
 182  544
         return then;
 183  
     }
 184  
 
 185  
     public String and() {
 186  544
         return and;
 187  
     }
 188  
 
 189  
     public String ignorable() {
 190  544
         return ignorable;
 191  
     }
 192  
 
 193  
     public String pending() {
 194  19
         return pending;
 195  
     }
 196  
 
 197  
     public String notPerformed() {
 198  19
         return notPerformed;
 199  
     }
 200  
 
 201  
     public String failed() {
 202  20
         return failed;
 203  
     }
 204  
 
 205  
     public String dryRun() {
 206  13
         return dryRun;
 207  
     }
 208  
 
 209  
     public String[] startingWords() {
 210  464
         Collection<String> words = startingWordsByType().values();
 211  464
         return words.toArray(new String[words.size()]);
 212  
     }
 213  
 
 214  
     public Map<StepType, String> startingWordsByType() {
 215  539
         Map<StepType, String> words = new HashMap<StepType, String>();
 216  539
         words.put(StepType.GIVEN, given());
 217  539
         words.put(StepType.WHEN, when());
 218  539
         words.put(StepType.THEN, then());
 219  539
         words.put(StepType.AND, and());
 220  539
         words.put(StepType.IGNORABLE, ignorable());
 221  539
         return words;
 222  
     }
 223  
 
 224  
     @Override
 225  
     public String toString() {
 226  3
         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
 227  
     }
 228  
 
 229  
     @SuppressWarnings("serial")
 230  
     public static class KeywordNotFound extends RuntimeException {
 231  
 
 232  
         public KeywordNotFound(String name, Map<String, String> keywords) {
 233  1
             super("Keyword " + name + " not found amongst " + keywords);
 234  1
         }
 235  
 
 236  
     }
 237  
 }