PendingStepMethodGenerator.java

  1. package org.jbehave.core.steps;

  2. import static java.text.MessageFormat.format;

  3. import org.apache.commons.lang3.StringUtils;
  4. import org.apache.commons.text.StringEscapeUtils;
  5. import org.apache.commons.text.WordUtils;
  6. import org.jbehave.core.annotations.Pending;
  7. import org.jbehave.core.configuration.Keywords;
  8. import org.jbehave.core.steps.StepCreator.PendingStep;

  9. public class PendingStepMethodGenerator {

  10.     private static final String METHOD_SOURCE = "@{0}(\"{1}\")\n@{2}\npublic void {3}() '{'\n  // {4}\n'}'\n";

  11.     private final Keywords keywords;

  12.     public PendingStepMethodGenerator(Keywords keywords) {
  13.         this.keywords = keywords;
  14.     }

  15.     public String generateMethod(PendingStep step) {
  16.         String stepAsString = step.stepAsString();
  17.         String previousNonAndStepAsString = step.previousNonAndStepAsString();
  18.         StepType stepType = null;
  19.         if (keywords.isAndStep(stepAsString) && previousNonAndStepAsString != null) {
  20.             stepType = keywords.stepTypeFor(previousNonAndStepAsString);
  21.         } else {
  22.             stepType = keywords.stepTypeFor(stepAsString);
  23.         }
  24.         String stepPattern = keywords.stepWithoutStartingWord(stepAsString, stepType);
  25.         String stepAnnotation = StringUtils.capitalize(stepType.name().toLowerCase());
  26.         String methodName = methodName(stepType, stepPattern);
  27.         String pendingAnnotation = Pending.class.getSimpleName();
  28.         return format(METHOD_SOURCE, stepAnnotation, StringEscapeUtils.escapeJava(stepPattern), pendingAnnotation,
  29.                 methodName, keywords.pending());
  30.     }

  31.     private String methodName(StepType stepType, String stepPattern) {
  32.         String name = stepType.name().toLowerCase() + WordUtils.capitalize(stepPattern);
  33.         char[] filteredName = new char[name.length()];
  34.         int index = 0;
  35.         for (int i = 0; i < name.length(); i++) {
  36.             char ch = name.charAt(i);
  37.             if (Character.isJavaIdentifierPart(ch) && ch != '$' && ch != 127) {
  38.                 filteredName[index++] = ch;
  39.             }
  40.         }
  41.         return new String(filteredName,0,index);
  42.     }

  43. }