Coverage Report - org.jbehave.core.parsers.RegexStoryParser
 
Classes in this File Line Coverage Branch Coverage Complexity
RegexStoryParser
98%
111/113
97%
43/44
1.871
 
 1  
 package org.jbehave.core.parsers;
 2  
 
 3  
 import java.io.File;
 4  
 import java.util.ArrayList;
 5  
 import java.util.List;
 6  
 import java.util.regex.Matcher;
 7  
 import java.util.regex.Pattern;
 8  
 
 9  
 import org.apache.commons.lang.StringUtils;
 10  
 import org.jbehave.core.configuration.Keywords;
 11  
 import org.jbehave.core.i18n.LocalizedKeywords;
 12  
 import org.jbehave.core.model.Description;
 13  
 import org.jbehave.core.model.ExamplesTable;
 14  
 import org.jbehave.core.model.ExamplesTableFactory;
 15  
 import org.jbehave.core.model.GivenStories;
 16  
 import org.jbehave.core.model.Meta;
 17  
 import org.jbehave.core.model.Narrative;
 18  
 import org.jbehave.core.model.Scenario;
 19  
 import org.jbehave.core.model.Story;
 20  
 
 21  
 import static java.util.regex.Pattern.DOTALL;
 22  
 import static java.util.regex.Pattern.compile;
 23  
 import static org.apache.commons.lang.StringUtils.removeStart;
 24  
 
 25  
 /**
 26  
  * Pattern-based story parser, which uses the keywords provided to parse the
 27  
  * textual story into a {@link Story}.
 28  
  */
 29  
 public class RegexStoryParser implements StoryParser {
 30  
 
 31  
     private static final String NONE = "";
 32  
     private final Keywords keywords;
 33  
     private final ExamplesTableFactory tableFactory;
 34  
 
 35  
     public RegexStoryParser() {
 36  33
         this(new LocalizedKeywords());
 37  33
     }
 38  
 
 39  
     public RegexStoryParser(Keywords keywords) {
 40  667
         this(keywords, new ExamplesTableFactory());
 41  667
     }
 42  
 
 43  
     public RegexStoryParser(ExamplesTableFactory tableFactory) {
 44  0
         this(new LocalizedKeywords(), tableFactory);
 45  0
     }
 46  
 
 47  667
     public RegexStoryParser(Keywords keywords, ExamplesTableFactory tableFactory) {
 48  667
         this.keywords = keywords;
 49  667
         this.tableFactory = tableFactory;
 50  667
     }
 51  
 
 52  
     public Story parseStory(String storyAsText) {
 53  3
         return parseStory(storyAsText, null);
 54  
     }
 55  
 
 56  
     public Story parseStory(String storyAsText, String storyPath) {
 57  25
         Description description = parseDescriptionFrom(storyAsText);
 58  25
         Meta meta = parseStoryMetaFrom(storyAsText);
 59  25
         Narrative narrative = parseNarrativeFrom(storyAsText);
 60  25
         List<Scenario> scenarios = parseScenariosFrom(storyAsText);
 61  25
         Story story = new Story(storyPath, description, meta, narrative, scenarios);
 62  25
         if (storyPath != null) {
 63  19
             story.namedAs(new File(storyPath).getName());
 64  
         }
 65  25
         return story;
 66  
     }
 67  
 
 68  
     private Description parseDescriptionFrom(String storyAsText) {
 69  25
         Matcher findingDescription = patternToPullDescriptionIntoGroupOne().matcher(storyAsText);
 70  25
         if (findingDescription.matches()) {
 71  14
             return new Description(findingDescription.group(1).trim());
 72  
         }
 73  11
         return Description.EMPTY;
 74  
     }
 75  
 
 76  
     private Meta parseStoryMetaFrom(String storyAsText) {
 77  25
         Matcher findingMeta = patternToPullStoryMetaIntoGroupOne().matcher(preScenarioText(storyAsText));
 78  25
         if (findingMeta.matches()) {
 79  3
             String meta = findingMeta.group(1).trim();
 80  3
             return createMeta(meta);
 81  
         }
 82  22
         return Meta.EMPTY;
 83  
     }
 84  
 
 85  
     private String preScenarioText(String storyAsText) {
 86  25
         String[] split = storyAsText.split(keywords.scenario());
 87  25
         return split.length > 0 ? split[0] : storyAsText;
 88  
     }
 89  
 
 90  
     private Meta createMeta(String meta) {
 91  7
         List<String> properties = new ArrayList<String>();
 92  24
         for (String property : meta.split(keywords.metaProperty())) {
 93  17
             if (StringUtils.isNotBlank(property)) {
 94  12
                 String beforeIgnorable = StringUtils.substringBefore(property,keywords.ignorable());
 95  12
                 if ( StringUtils.isNotBlank(beforeIgnorable)){
 96  10
                     properties.add(beforeIgnorable);
 97  
                 }
 98  
             }
 99  
         }
 100  7
         return new Meta(properties);
 101  
     }
 102  
 
 103  
     private Narrative parseNarrativeFrom(String storyAsText) {
 104  25
         Matcher findingNarrative = patternToPullNarrativeIntoGroupOne().matcher(storyAsText);
 105  25
         if (findingNarrative.matches()) {
 106  3
             String narrative = findingNarrative.group(1).trim();
 107  3
             return createNarrative(narrative);
 108  
         }
 109  22
         return Narrative.EMPTY;
 110  
     }
 111  
 
 112  
     private Narrative createNarrative(String narrative) {
 113  3
         Pattern findElements = patternToPullNarrativeElementsIntoGroups();
 114  3
         Matcher findingElements = findElements.matcher(narrative);
 115  3
         if (findingElements.matches()) {
 116  2
             String inOrderTo = findingElements.group(1).trim();
 117  2
             String asA = findingElements.group(2).trim();
 118  2
             String iWantTo = findingElements.group(3).trim();
 119  2
             return new Narrative(inOrderTo, asA, iWantTo);
 120  
         }
 121  1
         return Narrative.EMPTY;
 122  
     }
 123  
 
 124  
     private List<Scenario> parseScenariosFrom(String storyAsText) {
 125  25
         List<Scenario> parsed = new ArrayList<Scenario>();
 126  25
         for (String scenarioAsText : splitScenarios(storyAsText)) {
 127  126
             parsed.add(parseScenario(scenarioAsText));
 128  
         }
 129  25
         return parsed;
 130  
     }
 131  
 
 132  
     private List<String> splitScenarios(String storyAsText) {
 133  25
         List<String> scenarios = new ArrayList<String>();
 134  25
         String scenarioKeyword = keywords.scenario();
 135  
 
 136  
         // remove anything after scenario keyword, if found
 137  25
         if (StringUtils.contains(storyAsText, scenarioKeyword)) {
 138  14
             storyAsText = StringUtils.substringAfter(storyAsText, scenarioKeyword);
 139  
         }
 140  
 
 141  153
         for (String scenarioAsText : storyAsText.split(scenarioKeyword)) {
 142  128
             if (scenarioAsText.trim().length() > 0) {
 143  126
                 scenarios.add(scenarioKeyword + "\n" + scenarioAsText);
 144  
             }
 145  
         }
 146  25
         return scenarios;
 147  
     }
 148  
 
 149  
     private Scenario parseScenario(String scenarioAsText) {
 150  126
         String title = findScenarioTitle(scenarioAsText);
 151  126
         String scenarioWithoutTitle = removeStart(scenarioAsText, title);
 152  126
         Meta meta = findScenarioMeta(scenarioWithoutTitle);
 153  126
         ExamplesTable examplesTable = findExamplesTable(scenarioWithoutTitle);
 154  126
         GivenStories givenStories = findGivenStories(scenarioWithoutTitle);
 155  126
         if (givenStories.requireParameters()) {
 156  1
             givenStories.useExamplesTable(examplesTable);
 157  
         }
 158  126
         List<String> steps = findSteps(scenarioWithoutTitle);
 159  126
         return new Scenario(title, meta, givenStories, examplesTable, steps);
 160  
     }
 161  
 
 162  
     private String findScenarioTitle(String scenarioAsText) {
 163  126
         Matcher findingTitle = patternToPullScenarioTitleIntoGroupOne().matcher(scenarioAsText);
 164  126
         return findingTitle.find() ? findingTitle.group(1).trim() : NONE;
 165  
     }
 166  
 
 167  
     private Meta findScenarioMeta(String scenarioAsText) {
 168  126
         Matcher findingMeta = patternToPullScenarioMetaIntoGroupOne().matcher(scenarioAsText);
 169  126
         if (findingMeta.matches()) {
 170  4
             String meta = findingMeta.group(1).trim();
 171  4
             return createMeta(meta);
 172  
         }
 173  122
         return Meta.EMPTY;
 174  
     }
 175  
 
 176  
     private ExamplesTable findExamplesTable(String scenarioAsText) {
 177  126
         Matcher findingTable = patternToPullExamplesTableIntoGroupOne().matcher(scenarioAsText);
 178  126
         String tableInput = findingTable.find() ? findingTable.group(1).trim() : NONE;
 179  126
         return tableFactory.createExamplesTable(tableInput);
 180  
     }
 181  
 
 182  
     private GivenStories findGivenStories(String scenarioAsText) {
 183  126
         Matcher findingGivenStories = patternToPullGivenStoriesIntoGroupOne().matcher(scenarioAsText);
 184  126
         String givenStories = findingGivenStories.find() ? findingGivenStories.group(1).trim() : NONE;
 185  126
         return new GivenStories(givenStories);
 186  
     }
 187  
 
 188  
     private List<String> findSteps(String scenarioAsText) {
 189  126
         Matcher matcher = patternToPullStepsIntoGroupOne().matcher(scenarioAsText);
 190  126
         List<String> steps = new ArrayList<String>();
 191  126
         int startAt = 0;
 192  15187
         while (matcher.find(startAt)) {
 193  15061
             steps.add(StringUtils.substringAfter(matcher.group(1), "\n"));
 194  15061
             startAt = matcher.start(4);
 195  
         }
 196  126
         return steps;
 197  
     }
 198  
 
 199  
     // Regex Patterns
 200  
 
 201  
     private Pattern patternToPullDescriptionIntoGroupOne() {
 202  25
         String metaOrNarrativeOrScenario = concatenateWithOr(keywords.meta(), keywords.narrative(), keywords.scenario());
 203  25
         return compile("(.*?)(" + metaOrNarrativeOrScenario + ").*", DOTALL);
 204  
     }
 205  
 
 206  
     private Pattern patternToPullStoryMetaIntoGroupOne() {
 207  25
         return compile(".*" + keywords.meta() + "(.*?)\\s*(\\Z|" + keywords.narrative() + ").*", DOTALL);
 208  
     }
 209  
 
 210  
     private Pattern patternToPullNarrativeIntoGroupOne() {
 211  25
         return compile(".*" + keywords.narrative() + "(.*?)\\s*(" + keywords.scenario() + ").*", DOTALL);
 212  
     }
 213  
 
 214  
     private Pattern patternToPullNarrativeElementsIntoGroups() {
 215  3
         return compile(".*" + keywords.inOrderTo() + "(.*)\\s*" + keywords.asA() + "(.*)\\s*" + keywords.iWantTo()
 216  
                 + "(.*)", DOTALL);
 217  
     }
 218  
 
 219  
     private Pattern patternToPullScenarioTitleIntoGroupOne() {
 220  126
         String startingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 221  126
         return compile(keywords.scenario() + "((.)*?)\\s*(" + keywords.meta() + "|" + startingWords + ").*", DOTALL);
 222  
     }
 223  
 
 224  
     private Pattern patternToPullScenarioMetaIntoGroupOne() {
 225  126
         String startingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 226  126
         return compile(".*" + keywords.meta() + "(.*?)\\s*(" + keywords.givenStories() + "|" + startingWords + ").*",
 227  
                 DOTALL);
 228  
     }
 229  
 
 230  
     private Pattern patternToPullGivenStoriesIntoGroupOne() {
 231  126
         String startingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 232  126
         return compile("\\n" + keywords.givenStories() + "((.|\\n)*?)\\s*(" + startingWords + ").*", DOTALL);
 233  
     }
 234  
 
 235  
     private Pattern patternToPullStepsIntoGroupOne() {
 236  126
         String initialStartingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 237  126
         String followingStartingWords = concatenateWithOr("\\n", "\\s", keywords.startingWords());
 238  126
         return compile(
 239  
                 "((" + initialStartingWords + ") (.)*?)\\s*(\\Z|" + followingStartingWords + "|\\n"
 240  
                         + keywords.examplesTable() + ")", DOTALL);
 241  
     }
 242  
 
 243  
     private Pattern patternToPullExamplesTableIntoGroupOne() {
 244  126
         return compile("\\n" + keywords.examplesTable() + "\\s*(.*)", DOTALL);
 245  
     }
 246  
 
 247  
     private String concatenateWithOr(String... keywords) {
 248  25
         return concatenateWithOr(null, null, keywords);
 249  
     }
 250  
 
 251  
     private String concatenateWithOr(String beforeKeyword, String afterKeyword, String[] keywords) {
 252  655
         StringBuilder builder = new StringBuilder();
 253  655
         String before = beforeKeyword != null ? beforeKeyword : NONE;
 254  655
         String after = afterKeyword != null ? afterKeyword : NONE;
 255  3880
         for (String keyword : keywords) {
 256  3225
             builder.append(before).append(keyword).append(after).append("|");
 257  
         }
 258  655
         return StringUtils.chomp(builder.toString(), "|"); // chop off the last
 259  
                                                            // "|"
 260  
     }
 261  
 
 262  
 }