Format.java

  1. package org.jbehave.core.reporters;

  2. import java.io.PrintStream;

  3. import org.jbehave.core.reporters.StoryReporterBuilder.ProvidedFormat;

  4. /**
  5.  * A Format is a {@link StoryReporter} factory, allowing named-based pre-defined
  6.  * story reporters to be configured via the
  7.  * {@link StoryReporterBuilder#withFormats(Format...)} method. Users wanting to
  8.  * configure their custom defined story reporters, can do so via the
  9.  * {@link StoryReporterBuilder#withReporters(StoryReporter...)} method, or use
  10.  * the {@link ProvidedFormat} wrapper.
  11.  */
  12. public abstract class Format {

  13.     public static final Format CONSOLE = new Format("CONSOLE") {
  14.         @Override
  15.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  16.                 StoryReporterBuilder storyReporterBuilder) {
  17.             return configureFailureTraces(storyReporterBuilder, new ConsoleOutput(storyReporterBuilder.keywords()));
  18.         }
  19.     };

  20.     public static final Format ANSI_CONSOLE = new Format("ANSI_CONSOLE") {
  21.         @Override
  22.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  23.                 StoryReporterBuilder storyReporterBuilder) {
  24.             return configureFailureTraces(storyReporterBuilder,
  25.                     new ANSIConsoleOutput(storyReporterBuilder.keywords()).withCodes(storyReporterBuilder.codes()));
  26.         }
  27.     };

  28.     public static final Format IDE_CONSOLE = new Format("IDE_CONSOLE") {
  29.         @Override
  30.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  31.                 StoryReporterBuilder storyReporterBuilder) {
  32.             return configureFailureTraces(storyReporterBuilder,
  33.                     new IdeOnlyConsoleOutput(storyReporterBuilder.keywords()));
  34.         }
  35.     };

  36.     public static final Format TEAMCITY_CONSOLE = new Format("TEAMCITY_CONSOLE") {
  37.         @Override
  38.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  39.                                                  StoryReporterBuilder storyReporterBuilder) {
  40.             return configureFailureTraces(storyReporterBuilder,
  41.                     new TeamCityConsoleOutput(storyReporterBuilder.keywords()));
  42.         }
  43.     };

  44.     public static final Format TXT = new Format("TXT") {
  45.         @Override
  46.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  47.                 StoryReporterBuilder storyReporterBuilder) {
  48.             factory.useConfiguration(storyReporterBuilder.fileConfiguration("txt"));
  49.             return configureFailureTraces(storyReporterBuilder,
  50.                     new TxtOutput(factory.createPrintStream(), storyReporterBuilder.keywords()));
  51.         }
  52.     };

  53.     public static final Format HTML = new Format("HTML") {

  54.         @Override
  55.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  56.                 StoryReporterBuilder storyReporterBuilder) {
  57.             factory.useConfiguration(storyReporterBuilder.fileConfiguration("html"));
  58.             return configureFailureTraces(storyReporterBuilder,
  59.                     new HtmlOutput(factory.createPrintStream(), storyReporterBuilder.keywords()));
  60.         }
  61.     };

  62.     public static final Format XML = new Format("XML") {
  63.         @Override
  64.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  65.                                                  StoryReporterBuilder storyReporterBuilder) {
  66.             factory.useConfiguration(storyReporterBuilder.fileConfiguration("xml"));
  67.             return configureFailureTraces(storyReporterBuilder,
  68.                     new XmlOutput(factory.createPrintStream(), storyReporterBuilder.keywords()));
  69.         }
  70.     };

  71.     public static final Format JSON = new Format("JSON") {
  72.         @Override
  73.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  74.                                                  StoryReporterBuilder storyReporterBuilder) {
  75.             factory.useConfiguration(storyReporterBuilder.fileConfiguration("json"));
  76.             return configureFailureTraces(storyReporterBuilder,
  77.                     new JsonOutput(factory.createPrintStream(), storyReporterBuilder.keywords()));
  78.         }
  79.     };

  80.     public static final Format HTML_TEMPLATE = new Format("HTML") {
  81.         @Override
  82.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  83.                 StoryReporterBuilder storyReporterBuilder) {
  84.             factory.useConfiguration(storyReporterBuilder.fileConfiguration("html"));
  85.             return new HtmlTemplateOutput(factory.getOutputFile(), storyReporterBuilder.keywords());
  86.         }
  87.     };

  88.     public static final Format XML_TEMPLATE = new Format("XML") {
  89.         @Override
  90.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  91.                 StoryReporterBuilder storyReporterBuilder) {
  92.             factory.useConfiguration(storyReporterBuilder.fileConfiguration("xml"));
  93.             return new XmlTemplateOutput(factory.getOutputFile(), storyReporterBuilder.keywords());
  94.         }
  95.     };

  96.     public static final Format JSON_TEMPLATE = new Format("JSON") {
  97.         @Override
  98.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  99.                                                  StoryReporterBuilder storyReporterBuilder) {
  100.             factory.useConfiguration(storyReporterBuilder.fileConfiguration("json"));
  101.             return new JsonTemplateOutput(factory.getOutputFile(), storyReporterBuilder.keywords());
  102.         }
  103.     };

  104.     /**
  105.      * STATS is needed by the final reports.html summary page.
  106.      */
  107.     public static final Format STATS = new Format("STATS") {
  108.         @Override
  109.         public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  110.                 StoryReporterBuilder storyReporterBuilder) {
  111.             factory.useConfiguration(storyReporterBuilder.fileConfiguration("stats"));
  112.             return new PostStoryStatisticsCollector(factory.createPrintStream());
  113.         }
  114.     };

  115.     private final String name;

  116.     public Format(String name) {
  117.         this.name = name;
  118.     }

  119.     public String name() {
  120.         return name;
  121.     }

  122.     public abstract StoryReporter createStoryReporter(FilePrintStreamFactory factory,
  123.             StoryReporterBuilder storyReporterBuilder);

  124.     public static void println(PrintStream writer, String format, Object... args) {
  125.         writer.printf(format + "%n", args);
  126.     }

  127.     @Override
  128.     public String toString() {
  129.         return name;
  130.     }

  131.     private static PrintStreamOutput configureFailureTraces(StoryReporterBuilder storyReporterBuilder,
  132.                                                             PrintStreamOutput output) {
  133.         return output
  134.                 .doReportFailureTrace(storyReporterBuilder.reportFailureTrace())
  135.                 .doCompressFailureTrace(storyReporterBuilder.compressFailureTrace());
  136.     }
  137. }