Coverage Report - org.jbehave.core.steps.PendingStepMethodGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
PendingStepMethodGenerator
86%
38/44
68%
11/16
2.167
PendingStepMethodGenerator$StartingWordNotFound
0%
0/4
N/A
2.167
 
 1  
 package org.jbehave.core.steps;
 2  
 
 3  
 import java.util.Map;
 4  
 
 5  
 import org.apache.commons.lang.StringEscapeUtils;
 6  
 import org.apache.commons.lang.StringUtils;
 7  
 import org.apache.commons.lang.WordUtils;
 8  
 import org.jbehave.core.annotations.Pending;
 9  
 import org.jbehave.core.configuration.Keywords;
 10  
 import org.jbehave.core.steps.StepCreator.PendingStep;
 11  
 
 12  
 import static java.text.MessageFormat.format;
 13  
 import static java.util.Arrays.asList;
 14  
 
 15  
 public class PendingStepMethodGenerator {
 16  
 
 17  
     private static final String METHOD_SOURCE = "@{0}(\"{1}\")\n@{2}\npublic void {3}()'{'\n  // {4}\n'}'\n";
 18  
 
 19  
     private final Keywords keywords;
 20  
     private final Map<StepType, String> startingWordsByType;
 21  
 
 22  3
     public PendingStepMethodGenerator(Keywords keywords) {
 23  3
         this.keywords = keywords;
 24  3
         this.startingWordsByType = keywords.startingWordsByType();
 25  3
     }
 26  
 
 27  
     public String generateMethod(PendingStep step) {    
 28  3
         String stepAsString = step.stepAsString();
 29  3
         String previousNonAndStepAsString = step.previousNonAndStepAsString();
 30  3
         StepType stepType = null;
 31  3
         if (isAndStep(stepAsString)) {
 32  1
             stepType = findStepType(previousNonAndStepAsString);
 33  
         } else {
 34  2
             stepType = findStepType(stepAsString);
 35  
         }
 36  3
         String stepPattern = stripStartingWord(stepAsString);
 37  3
         String stepAnnotation = StringUtils.capitalize(stepType.name().toLowerCase());
 38  3
         String methodName = methodName(stepType, stepPattern);
 39  3
         String pendingAnnotation = Pending.class.getSimpleName();
 40  3
         return format(METHOD_SOURCE, stepAnnotation, StringEscapeUtils.escapeJava(stepPattern), pendingAnnotation, methodName, keywords.pending());
 41  
     }
 42  
 
 43  
     private String methodName(StepType stepType, String stepPattern) {
 44  3
         String name = stepType.name().toLowerCase() + WordUtils.capitalize(stepPattern);
 45  3
         for (String remove : asList(" ", "\'", "\"", "\\.", "\\,", "\\;", "\\:", "\\!", "\\|", "<", ">", "\\*")) {
 46  36
             name = name.replaceAll(remove, "");
 47  
         }
 48  3
         return name;
 49  
     }
 50  
 
 51  
     private boolean isAndStep(String stepAsString) {
 52  3
         String andWord = startingWordFor(StepType.AND);
 53  3
         return stepStartsWithWord(stepAsString, andWord);
 54  
     }
 55  
 
 56  
     private String stripStartingWord(final String stepAsString) {
 57  3
         String startingWord = findStartingWord(stepAsString);
 58  3
         return trimStartingWord(startingWord, stepAsString);
 59  
     }
 60  
 
 61  
     private StepType findStepType(final String stepAsString) throws StartingWordNotFound {
 62  3
         for (StepType stepType : startingWordsByType.keySet()) {
 63  13
             String wordForType = startingWordFor(stepType);
 64  13
             if (stepStartsWithWord(stepAsString, wordForType)) {
 65  3
                 return stepType;
 66  
             }
 67  10
         }
 68  0
         throw new StartingWordNotFound(stepAsString, startingWordsByType);
 69  
     }
 70  
 
 71  
     private String findStartingWord(final String stepAsString) throws StartingWordNotFound {
 72  3
         for (StepType stepType : startingWordsByType.keySet()) {
 73  12
             String wordForType = startingWordFor(stepType);
 74  12
             if (stepStartsWithWord(stepAsString, wordForType)) {
 75  3
                 return wordForType;
 76  
             }
 77  9
         }
 78  0
         String andWord = startingWordFor(StepType.AND);
 79  0
         if (stepStartsWithWord(stepAsString, andWord)) {
 80  0
             return andWord;
 81  
         }
 82  0
         throw new StartingWordNotFound(stepAsString, startingWordsByType);
 83  
     }
 84  
 
 85  
     private boolean stepStartsWithWord(String step, String word) {
 86  28
         return step.startsWith(word + " "); // space after qualifies it as word
 87  
     }
 88  
 
 89  
     private String trimStartingWord(String word, String step) {
 90  3
         return step.substring(word.length() + 1); // 1 for the space after
 91  
     }
 92  
 
 93  
     private String startingWordFor(StepType stepType) {
 94  28
         String startingWord = startingWordsByType.get(stepType);
 95  28
         if (startingWord == null) {
 96  0
             throw new StartingWordNotFound(stepType, startingWordsByType);
 97  
         }
 98  28
         return startingWord;
 99  
     }
 100  
 
 101  
     @SuppressWarnings("serial")
 102  
     public static class StartingWordNotFound extends RuntimeException {
 103  
 
 104  
         public StartingWordNotFound(String step, Map<StepType, String> startingWordsByType) {
 105  0
             super("No starting word found for step '" + step + "' amongst '" + startingWordsByType + "'");
 106  0
         }
 107  
 
 108  
         public StartingWordNotFound(StepType stepType, Map<StepType, String> startingWordsByType) {
 109  0
             super("No starting word found of type '" + stepType + "' amongst '" + startingWordsByType + "'");
 110  0
         }
 111  
 
 112  
     }
 113  
 
 114  
 }