AbstractStepResult.java

  1. package org.jbehave.core.steps;

  2. import java.lang.reflect.Method;

  3. import org.apache.commons.lang3.builder.ToStringBuilder;
  4. import org.apache.commons.lang3.builder.ToStringStyle;
  5. import org.jbehave.core.failures.PendingStepFound;
  6. import org.jbehave.core.failures.UUIDExceptionWrapper;
  7. import org.jbehave.core.model.OutcomesTable.OutcomesFailed;
  8. import org.jbehave.core.reporters.StoryReporter;
  9. import org.jbehave.core.steps.StepCreator.PendingStep;

  10. /**
  11.  * Represents the possible step results:
  12.  * <ul>
  13.  * <li>Failed</li>
  14.  * <li>NotPerformed</li>
  15.  * <li>Pending</li>
  16.  * <li>Successful</li>
  17.  * <li>Ignorable</li>
  18.  * <li>Skipped</li>
  19.  * </ul>
  20.  */
  21. public abstract class AbstractStepResult implements StepResult {

  22.     public static class Failed extends AbstractStepResult {

  23.         public Failed(String step, UUIDExceptionWrapper throwable) {
  24.             super(step, Type.FAILED, throwable);
  25.         }

  26.         public Failed(Method method, UUIDExceptionWrapper throwable) {
  27.             super(asString(method), Type.FAILED, throwable);
  28.         }

  29.         @Override
  30.         public void describeTo(StoryReporter reporter) {
  31.             if (throwable.getCause() instanceof OutcomesFailed) {
  32.                 reporter.failedOutcomes(parametrisedStep(), ((OutcomesFailed) throwable.getCause()).outcomesTable());
  33.             } else {
  34.                 reporter.failed(parametrisedStep(), throwable);
  35.             }
  36.         }
  37.     }

  38.     public static class NotPerformed extends AbstractStepResult {

  39.         public NotPerformed(String step) {
  40.             super(Type.NOT_PERFORMED, step);
  41.         }

  42.         @Override
  43.         public void describeTo(StoryReporter reporter) {
  44.             reporter.notPerformed(parametrisedStep());
  45.         }
  46.     }

  47.     public static class Pending extends AbstractStepResult {
  48.         private PendingStep pendingStep;

  49.         public Pending(PendingStep step) {
  50.             this(step, new PendingStepFound(step.stepAsString()));
  51.             pendingStep = step;
  52.         }

  53.         public Pending(PendingStep step, PendingStepFound e) {
  54.             super(step.getStepAsString(), Type.PENDING, e);
  55.             pendingStep = step;
  56.         }

  57.         @Override
  58.         public void describeTo(StoryReporter reporter) {
  59.             reporter.pending(parametrisedStep());
  60.             reporter.pending(pendingStep);
  61.         }
  62.     }

  63.     public static class Successful extends AbstractStepResult {

  64.         public Successful(String step) {
  65.             super(Type.SUCCESSFUL, step);
  66.         }

  67.         public Successful(Method method) {
  68.             super(Type.SUCCESSFUL, asString(method));
  69.         }

  70.         @Override
  71.         public void describeTo(StoryReporter reporter) {
  72.             reporter.successful(parametrisedStep());
  73.         }

  74.     }

  75.     public static class Ignorable extends AbstractStepResult {
  76.         public Ignorable(String step) {
  77.             super(Type.IGNORABLE, step);
  78.         }

  79.         @Override
  80.         public void describeTo(StoryReporter reporter) {
  81.             reporter.ignorable(step);
  82.         }
  83.     }

  84.     public static class Comment extends AbstractStepResult {
  85.         public Comment(String step) {
  86.             super(Type.COMMENT, step);
  87.         }

  88.         @Override
  89.         public void describeTo(StoryReporter reporter) {
  90.             reporter.comment(step);
  91.         }
  92.     }

  93.     public static class Skipped extends AbstractStepResult {

  94.         public Skipped() {
  95.             super(Type.SKIPPED, "");
  96.         }

  97.         @Override
  98.         public void describeTo(StoryReporter reporter) {
  99.             // do not report
  100.         }
  101.     }

  102.     protected final String step;
  103.     protected final Type type;
  104.     protected final UUIDExceptionWrapper throwable;
  105.     private Timing timing = new Timing();
  106.     private String parametrisedStep;

  107.     public AbstractStepResult(Type type, String step) {
  108.         this(step, type, null);
  109.     }

  110.     public AbstractStepResult(String step, Type type, UUIDExceptionWrapper throwable) {
  111.         this.step = step;
  112.         this.type = type;
  113.         this.throwable = throwable;
  114.     }

  115.     @Override
  116.     public String parametrisedStep() {
  117.         return parametrisedStep != null ? parametrisedStep : step;
  118.     }

  119.     @Override
  120.     public StepResult withParameterValues(String parametrisedStep) {
  121.         this.parametrisedStep = parametrisedStep;
  122.         return this;
  123.     }

  124.     public Timing getTiming() {
  125.         return timing;
  126.     }
  127.    
  128.     @Override
  129.     public StepResult setTimings(Timer timer) {
  130.         this.timing = new Timing(timer);
  131.         return this;
  132.     }
  133.    
  134.     @Override
  135.     public UUIDExceptionWrapper getFailure() {
  136.         return throwable;
  137.     }

  138.     @Override
  139.     public String toString() {
  140.         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(parametrisedStep()).append(
  141.                 getTiming()).toString();
  142.     }

  143.     public static StepResult successful(String step) {
  144.         return new Successful(step);
  145.     }

  146.     public static StepResult successful(Method method) {
  147.         return new Successful(method);
  148.     }

  149.     public static StepResult ignorable(String step) {
  150.         return new Ignorable(step);
  151.     }

  152.     public static StepResult comment(String step) {
  153.         return new Comment(step);
  154.     }

  155.     public static StepResult pending(PendingStep step) {
  156.         return new Pending(step);
  157.     }

  158.     public static StepResult pending(PendingStep step, PendingStepFound e) {
  159.         return new Pending(step, e);
  160.     }

  161.     public static StepResult notPerformed(String step) {
  162.         return new NotPerformed(step);
  163.     }

  164.     public static StepResult failed(String step, UUIDExceptionWrapper e) {
  165.         return new Failed(step, e);
  166.     }

  167.     public static StepResult failed(Method method, UUIDExceptionWrapper e) {
  168.         return new Failed(method, e);
  169.     }

  170.     public static StepResult skipped() {
  171.         return new Skipped();
  172.     }
  173.    
  174.     private static String asString(Method method) {
  175.         if (method == null) {
  176.             return "";
  177.         }
  178.         StringBuilder sb = new StringBuilder()
  179.                 .append(method.getDeclaringClass().getName()).append(".")
  180.                 .append(method.getName()).append("(");
  181.         Class<?>[] types = method.getParameterTypes();
  182.         for (int i = 0; i < types.length; i++) {
  183.             Class<?> type = types[i];
  184.             sb.append(type.getName());
  185.             if (i + 1 < types.length) {
  186.                 sb.append(",");
  187.             }
  188.         }
  189.         return sb.append(")").toString();
  190.     }
  191. }