PropertyBasedEmbedderControls.java

  1. package org.jbehave.core.embedder;

  2. import org.apache.commons.lang3.builder.ToStringBuilder;
  3. import org.apache.commons.lang3.builder.ToStringStyle;

  4. public class PropertyBasedEmbedderControls extends EmbedderControls {

  5.     public static final String BATCH = "BATCH";
  6.     public static final String IGNORE_FAILURE_IN_VIEW = "IGNORE_FAILURE_IN_VIEW";
  7.     public static final String IGNORE_FAILURE_IN_STORIES = "IGNORE_FAILURE_IN_STORIES";
  8.     public static final String GENERATE_VIEW_AFTER_STORIES = "GENERATE_VIEW_AFTER_STORIES";
  9.     public static final String SKIP = "SKIP";
  10.     public static final String VERBOSE_FAILURES = "VERBOSE_FAILURES";
  11.     public static final String VERBOSE_FILTERING = "VERBOSE_FILTERING";
  12.     public static final String STORY_TIMEOUTS = "STORY_TIMEOUTS";
  13.     public static final String STORY_TIMEOUT_IN_SECS = "STORY_TIMEOUT_IN_SECS";
  14.     public static final String STORY_TIMEOUT_IN_SECS_BY_PATH = "STORY_TIMEOUT_IN_SECS_BY_PATH";
  15.     public static final String FAIL_ON_STORY_TIMEOUT = "FAIL_ON_STORY_TIMEOUT";
  16.     public static final String THREADS = "THREADS";

  17.     @Override
  18.     public boolean batch() {
  19.         return propertyAs(BATCH, Boolean.class, super.batch());
  20.     }

  21.     @Override
  22.     public boolean ignoreFailureInView() {
  23.         return propertyAs(IGNORE_FAILURE_IN_VIEW, Boolean.class, super.ignoreFailureInView());
  24.     }

  25.     @Override
  26.     public boolean ignoreFailureInStories() {
  27.         return propertyAs(IGNORE_FAILURE_IN_STORIES, Boolean.class, super.ignoreFailureInStories());
  28.     }

  29.     @Override
  30.     public boolean generateViewAfterStories() {
  31.         return propertyAs(GENERATE_VIEW_AFTER_STORIES, Boolean.class, super.generateViewAfterStories());
  32.     }

  33.     @Override
  34.     public boolean skip() {
  35.         return propertyAs(SKIP, Boolean.class, super.skip());
  36.     }
  37.    
  38.     @Override
  39.     public boolean verboseFailures() {
  40.         return propertyAs(VERBOSE_FAILURES, Boolean.class, super.verboseFailures());
  41.     }

  42.     @Override
  43.     public boolean verboseFiltering() {
  44.         return propertyAs(VERBOSE_FILTERING, Boolean.class, super.verboseFiltering());
  45.     }
  46.    
  47.     @Override
  48.     public String storyTimeouts() {
  49.         return propertyAs(STORY_TIMEOUTS, String.class, super.storyTimeouts());
  50.     }

  51.     @Override
  52.     public boolean failOnStoryTimeout() {
  53.         return propertyAs(FAIL_ON_STORY_TIMEOUT, Boolean.class, super.failOnStoryTimeout());
  54.     }

  55.     @Override
  56.     public int threads() {
  57.         return propertyAs(THREADS, Integer.class, super.threads());
  58.     }
  59.    
  60.     @SuppressWarnings("unchecked")
  61.     private <T> T propertyAs(String name, Class<T> type, T defaultValue) {
  62.         String property = System.getProperty(name);
  63.         if (property == null) {
  64.             return defaultValue;
  65.         }
  66.         if (type == String.class) {
  67.             return (T) property;
  68.         }
  69.         if (type == Boolean.class) {
  70.             return (T) Boolean.valueOf(property);
  71.         }
  72.         if (type == Long.class) {
  73.             return (T) Long.valueOf(property);
  74.         }
  75.         if (type == Integer.class) {
  76.             return (T) Integer.valueOf(property);
  77.         }
  78.         throw new IllegalArgumentException("Unsupported type: " + type);
  79.     }

  80.     @Override
  81.     public String toString() {
  82.         // Calling accessor methods to show the expected system based values
  83.         // rather than the object values
  84.         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
  85.         .append("batch", batch())
  86.         .append("skip", skip())
  87.         .append("generateViewAfterStories", generateViewAfterStories())
  88.         .append("ignoreFailureInStories", ignoreFailureInStories())
  89.         .append("ignoreFailureInView", ignoreFailureInView())
  90.         .append("verboseFailures", verboseFailures())
  91.         .append("verboseFiltering", verboseFiltering())
  92.         .append("storyTimeouts", storyTimeouts())
  93.         .append("threads", threads())
  94.         .toString();        
  95.     }

  96. }