PrintingEmbedderMonitor.java

  1. package org.jbehave.core.embedder;

  2. import java.io.File;
  3. import java.util.List;
  4. import java.util.Properties;
  5. import java.util.concurrent.ExecutorService;

  6. import org.apache.commons.lang3.builder.ToStringBuilder;
  7. import org.apache.commons.lang3.builder.ToStringStyle;
  8. import org.jbehave.core.ConfigurableEmbedder;
  9. import org.jbehave.core.failures.BatchFailures;
  10. import org.jbehave.core.model.Meta;
  11. import org.jbehave.core.model.Scenario;
  12. import org.jbehave.core.model.Story;
  13. import org.jbehave.core.model.StoryDuration;
  14. import org.jbehave.core.model.StoryMaps;
  15. import org.jbehave.core.reporters.ReportsCount;

  16. /**
  17.  * Abstract monitor that reports to output which should be defined in child implementations.
  18.  */
  19. public abstract class PrintingEmbedderMonitor implements EmbedderMonitor {

  20.     @Override
  21.     public void batchFailed(BatchFailures failures) {
  22.         print("Failed to run batch %s", failures);
  23.     }

  24.     @Override
  25.     public void beforeOrAfterStoriesFailed() {
  26.         print("Failed to run before or after stories steps");
  27.     }

  28.     @Override
  29.     public void embeddableFailed(String name, Throwable cause) {
  30.         print("Failed to run embeddable %s", name);
  31.         printStackTrace(cause);
  32.     }

  33.     @Override
  34.     public void embeddableNotConfigurable(String name) {
  35.         print("Embeddable %s must be an instance of %s", name,  ConfigurableEmbedder.class);
  36.     }

  37.     @Override
  38.     public void embeddablesSkipped(List<String> classNames) {
  39.         print("Skipped embeddables %s", classNames);
  40.     }

  41.     @Override
  42.     public void metaExcluded(Meta meta, MetaFilter filter) {
  43.         print("%s excluded by filter '%s'", meta, filter.asString());
  44.     }

  45.     @Override
  46.     public void runningEmbeddable(String name) {
  47.         print("Running embeddable %s", name);
  48.     }

  49.     @Override
  50.     public void runningStory(String path) {
  51.         print("Running story %s", path);
  52.     }

  53.     @Override
  54.     public void storyFailed(String path, Throwable cause) {
  55.         print("Failed to run story %s", path);
  56.         printStackTrace(cause);
  57.     }

  58.     @Override
  59.     public void storiesSkipped(List<String> storyPaths) {
  60.         print("Skipped stories %s", storyPaths);
  61.     }

  62.     @Override
  63.     public void storiesExcluded(List<Story> excluded, MetaFilter filter, boolean verbose) {
  64.         StringBuilder format = new StringBuilder("%d stories excluded by filter: %s%n");
  65.         if (verbose) {
  66.             for (Story story : excluded) {
  67.                 format.append(story.getPath()).append("%n");
  68.             }
  69.         }
  70.         print(format.toString(), excluded.size(), filter.asString());
  71.     }

  72.     @Override
  73.     public void scenarioExcluded(Scenario scenario, MetaFilter filter) {
  74.         print("Scenario '%s' excluded by filter: %s%n", scenario.getTitle(), filter.asString());
  75.     }

  76.     @Override
  77.     public void runningWithAnnotatedEmbedderRunner(String className) {
  78.         print("Running with AnnotatedEmbedderRunner '%s'", className);
  79.     }

  80.     @Override
  81.     public void annotatedInstanceNotOfType(Object annotatedInstance, Class<?> type) {
  82.         print("Annotated instance %s if not of type %s", annotatedInstance, type);
  83.     }

  84.     @Override
  85.     public void generatingReportsView(File outputDirectory, List<String> formats, Properties viewProperties) {
  86.         print("Generating reports view to '%s' using formats '%s' and view properties '%s'", outputDirectory, formats,
  87.                 viewProperties);
  88.     }

  89.     @Override
  90.     public void reportsViewGenerationFailed(File outputDirectory, List<String> formats, Properties viewProperties,
  91.             Throwable cause) {
  92.         print("Failed to generate reports view to '%s' using formats '%s' and view properties '%s'", outputDirectory,
  93.                 formats, viewProperties);
  94.     }

  95.     @Override
  96.     public void reportsViewGenerated(ReportsCount count) {
  97.         print("Reports view generated with %d stories (of which %d pending) containing %d scenarios (of which %d "
  98.                         + "pending)",
  99.                 count.getStories(), count.getStoriesPending(), count.getScenarios(), count.getScenariosPending());
  100.         if (count.getStoriesExcluded() > 0 || count.getScenariosExcluded() > 0) {
  101.             print("Meta filters excluded %d stories and  %d scenarios", count.getStoriesExcluded(),
  102.                     count.getScenariosExcluded());
  103.         }
  104.     }

  105.     @Override
  106.     public void reportsViewFailures(ReportsCount count) {
  107.         print("Failures in reports view: %d scenarios failed", count.getScenariosFailed());
  108.     }

  109.     @Override
  110.     public void reportsViewNotGenerated() {
  111.         print("Reports view not generated");
  112.     }

  113.     @Override
  114.     public void mappingStory(String storyPath, List<String> metaFilters) {
  115.         print("Mapping story %s with meta filters %s", storyPath, metaFilters);
  116.     }

  117.     @Override
  118.     public void generatingMapsView(File outputDirectory, StoryMaps storyMaps, Properties viewProperties) {
  119.         print("Generating maps view to '%s' using story maps '%s' and view properties '%s'", outputDirectory, storyMaps,
  120.                 viewProperties);
  121.     }

  122.     @Override
  123.     public void mapsViewGenerationFailed(File outputDirectory, StoryMaps storyMaps, Properties viewProperties,
  124.             Throwable cause) {
  125.         print("Failed to generating maps view to '%s' using story maps '%s' and view properties '%s'", outputDirectory,
  126.                 storyMaps, viewProperties);
  127.         printStackTrace(cause);
  128.     }

  129.     @Override
  130.     public void processingSystemProperties(Properties properties) {
  131.         print("Processing system properties %s", properties);
  132.     }

  133.     @Override
  134.     public void systemPropertySet(String name, String value) {
  135.         print("System property '%s' set to '%s'", name, value);
  136.     }

  137.     @Override
  138.     public void storyTimeout(Story story, StoryDuration storyDuration) {
  139.         print("Story %s duration of %d seconds has exceeded timeout of %d seconds", story.getPath(),
  140.                 storyDuration.getDurationInSecs(), storyDuration.getTimeoutInSecs());
  141.     }

  142.     @Override
  143.     public void usingThreads(int threads) {
  144.         print("Using %d threads", threads);
  145.     }

  146.     @Override
  147.     public void usingExecutorService(ExecutorService executorService) {
  148.         print("Using executor service %s", executorService);
  149.     }

  150.     @Override
  151.     public void usingControls(EmbedderControls embedderControls) {
  152.         print("Using controls %s", embedderControls);
  153.     }
  154.    
  155.    
  156.     @Override
  157.     public void invalidTimeoutFormat(String path) {
  158.         print("Failed to set specific story timeout for story %s because 'storyTimeoutInSecsByPath' has incorrect "
  159.                 + "format", path);
  160.         print("'storyTimeoutInSecsByPath' must be a CSV of regex expressions matching story paths. E.g. \"*/long/*"
  161.                 + ".story:5000,*/short/*.story:200\"");
  162.     }

  163.     @Override
  164.     public void usingTimeout(String path, long timeout) {
  165.         print("Using timeout for story %s of %d secs.", path, timeout);
  166.     }

  167.     @Override
  168.     public String toString() {
  169.         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  170.     }

  171.     protected abstract void print(String format, Object... args);

  172.     protected abstract void printStackTrace(Throwable e);
  173. }