Coverage Report - org.jbehave.core.reporters.StoryReporterBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
StoryReporterBuilder
100%
61/61
93%
14/15
1.682
StoryReporterBuilder$1
100%
1/1
N/A
1.682
StoryReporterBuilder$Format
100%
2/2
N/A
1.682
 
 1  
 package org.jbehave.core.reporters;
 2  
 
 3  
 import static java.util.Arrays.asList;
 4  
 
 5  
 import java.io.File;
 6  
 import java.net.URL;
 7  
 import java.util.ArrayList;
 8  
 import java.util.HashMap;
 9  
 import java.util.List;
 10  
 import java.util.Map;
 11  
 import java.util.Properties;
 12  
 
 13  
 import org.jbehave.core.configuration.Keywords;
 14  
 import org.jbehave.core.i18n.LocalizedKeywords;
 15  
 import org.jbehave.core.io.CodeLocations;
 16  
 import org.jbehave.core.io.StoryLocation;
 17  
 import org.jbehave.core.reporters.FilePrintStreamFactory.FileConfiguration;
 18  
 import org.jbehave.core.reporters.FilePrintStreamFactory.FilePathResolver;
 19  
 
 20  
 /**
 21  
  * <p>
 22  
  * A <a href="http://en.wikipedia.org/wiki/Builder_pattern">Builder</a> for
 23  
  * {@link StoryReporter}s. It builds a {@link DelegatingStoryReporter} with
 24  
  * delegates for a number of formats - mostly file-based ones except
 25  
  * {@Format.CONSOLE}. It requires a
 26  
  * {@link FilePrintStreamFactory} and provides default delegate instances for
 27  
  * each format.
 28  
  * </p>
 29  
  * <p>
 30  
  * To build a reporter for a single story path with default and given formats:
 31  
  * 
 32  
  * <pre>
 33  
  * Class&lt;MyStory&gt; storyClass = MyStory.class;
 34  
  * StoryPathResolver resolver = new UnderscoredCamelCaseResolver();
 35  
  * String storyPath = resolver.resolve(storyClass);
 36  
  * StoryReporter reporter = new StoryReporterBuilder().withCodeLocation(StoryLocation.codeLocationFromClass(storyClass))
 37  
  *         .withDefaultFormats().withFormats(TXT, HTML, XML).build(storyPath);
 38  
  * </pre>
 39  
  * 
 40  
  * </p>
 41  
  * <p>
 42  
  * The builder is configured to build with the {@link Format#STATS} as default
 43  
  * format. To change the default formats the user can override the method:
 44  
  * 
 45  
  * <pre>
 46  
  * new StoryReporterBuilder() {
 47  
  *     protected StoryReporterBuilder withDefaultFormats() {
 48  
  *         return withFormats(STATS);
 49  
  *     }
 50  
  * }
 51  
  * </pre>
 52  
  * 
 53  
  * </p>
 54  
  * <p>
 55  
  * The builder configures the file-based reporters to output to the default file
 56  
  * directory {@link FileConfiguration#DIRECTORY} as relative to the code
 57  
  * location. In some case, e.g. with Ant class loader, the code source location
 58  
  * from class may not be properly set. In this case, we may specify it from a
 59  
  * file:
 60  
  * 
 61  
  * <pre>
 62  
  * new StoryReporterBuilder().withCodeLocation(StoryLocation.codeLocationFromFile(new File(&quot;target/classes&quot;)))
 63  
  *         .withDefaultFormats().withFormats(TXT, HTML, XML).build(storyPath);
 64  
  * </pre>
 65  
  * 
 66  
  * </p>
 67  
  * <p>
 68  
  * By default, the reporters will output minimal failure information, the single
 69  
  * line describing the failure cause and the outcomes if failures occur. To
 70  
  * configure the failure trace to be reported as well:
 71  
  * 
 72  
  * <pre>
 73  
  * new StoryReporterBuilder().withFailureTrace(true)
 74  
  * </pre>
 75  
  * 
 76  
  * </p>
 77  
  * 
 78  
  * <p>
 79  
  * To specify the use of keywords for a given locale:
 80  
  * 
 81  
  * <pre>
 82  
  * new StoryReporterBuilder().withKeywords(new LocalisedKeywords(Locale.IT)
 83  
  * </pre>
 84  
  * 
 85  
  * </p>
 86  
  * 
 87  
  * <p>
 88  
  * The builder provides default instances for all reporters, using the default
 89  
  * output patterns. To change the reporter for a specific instance, e.g. to
 90  
  * report format <b>TXT</b> to <b>.text</b> files and to inject other
 91  
  * non-default parameters, such as the custom output patterns:
 92  
  * 
 93  
  * <pre>
 94  
  * new StoryReporterBuilder(){
 95  
  *   public StoryReporter reporterFor(String storyPath, Format format){
 96  
  *       FilePrintStreamFactory factory = new FilePrintStreamFactory(new StoryLocation(storyPath, codeLocation));
 97  
  *       switch (format) {
 98  
  *           case TXT:
 99  
  *               factory.useConfiguration(new FileConfiguration("text"));
 100  
  *               Properties customPatterns = new Properties();
 101  
  *               customPatterns.setProperty("successful", "{0}(YEAH!!!)\n");
 102  
  *               return new TxtOutput(factory.createPrintStream(), customPatterns, keywords);
 103  
  *            default:
 104  
  *               return super.reporterFor(format);
 105  
  *   }
 106  
  * }
 107  
  * </pre>
 108  
  * 
 109  
  * </p>
 110  
  */
 111  213
 public class StoryReporterBuilder {
 112  
 
 113  10
     public enum Format {
 114  1
         CONSOLE, IDE_CONSOLE, TXT, HTML, XML, STATS
 115  
     }
 116  
 
 117  213
     private List<Format> formats = new ArrayList<Format>();
 118  213
     private String relativeDirectory = new FileConfiguration().getRelativeDirectory();
 119  213
     private FilePathResolver pathResolver = new FileConfiguration().getPathResolver();
 120  213
     private URL codeLocation = CodeLocations.codeLocationFromPath("target/classes");
 121  213
     private Properties viewResources = FreemarkerViewGenerator.defaultViewProperties();
 122  213
     private boolean reportFailureTrace = false;
 123  213
     private Keywords keywords = new LocalizedKeywords();
 124  
 
 125  
     public File outputDirectory() {
 126  17
         return filePrintStreamFactory("").outputDirectory();
 127  
     }
 128  
 
 129  
     public String relativeDirectory(){
 130  1
         return relativeDirectory;
 131  
     }
 132  
     
 133  
     public FilePathResolver pathResolver(){
 134  2
         return pathResolver;
 135  
     }
 136  
     
 137  
     public URL codeLocation() {
 138  1
         return codeLocation;
 139  
     }
 140  
 
 141  
     public List<Format> formats() {
 142  1
         return formats;
 143  
     }
 144  
 
 145  
     public List<String> formatNames(boolean toLowerCase) {
 146  19
         List<String> names = new ArrayList<String>();
 147  19
         for (Format format : formats) {
 148  14
             String name = format.name();
 149  14
             if (toLowerCase) {
 150  7
                 name = name.toLowerCase();
 151  
             }
 152  14
             names.add(name);
 153  14
         }
 154  19
         return names;
 155  
     }
 156  
 
 157  
     public Keywords keywords() {
 158  1
         return keywords;
 159  
     }
 160  
 
 161  
     public boolean reportFailureTrace() {
 162  1
         return reportFailureTrace;
 163  
     }
 164  
 
 165  
     public Properties viewResources() {
 166  18
         return viewResources;
 167  
     }
 168  
 
 169  
     public StoryReporterBuilder withRelativeDirectory(String relativeDirectory) {
 170  3
         this.relativeDirectory = relativeDirectory;
 171  3
         return this;
 172  
     }
 173  
 
 174  
     public StoryReporterBuilder withPathResolver(FilePathResolver pathResolver){
 175  1
         this.pathResolver = pathResolver;
 176  1
         return this;
 177  
     }
 178  
 
 179  
     public StoryReporterBuilder withCodeLocation(URL codeLocation) {
 180  1
         this.codeLocation = codeLocation;
 181  1
         return this;
 182  
     }
 183  
 
 184  
     public StoryReporterBuilder withDefaultFormats() {
 185  6
         return withFormats(Format.STATS);
 186  
     }
 187  
 
 188  
     public StoryReporterBuilder withFormats(Format... formats) {
 189  15
         this.formats.addAll(asList(formats));
 190  15
         return this;
 191  
     }
 192  
 
 193  
     public StoryReporterBuilder withFailureTrace(boolean reportFailureTrace) {
 194  1
         this.reportFailureTrace = reportFailureTrace;
 195  1
         return this;
 196  
     }
 197  
 
 198  
     public StoryReporterBuilder withKeywords(Keywords keywords) {
 199  1
         this.keywords = keywords;
 200  1
         return this;
 201  
     }
 202  
 
 203  
     public StoryReporterBuilder withViewResources(Properties resources) {
 204  1
         this.viewResources = resources;
 205  1
         return this;
 206  
     }
 207  
 
 208  
     public StoryReporter build(String storyPath) {
 209  36
         Map<Format, StoryReporter> delegates = new HashMap<Format, StoryReporter>();
 210  36
         for (Format format : formats) {
 211  21
             delegates.put(format, reporterFor(storyPath, format));
 212  
         }
 213  36
         return new DelegatingStoryReporter(delegates.values());
 214  
     }
 215  
 
 216  
     public Map<String, StoryReporter> build(List<String> storyPaths) {
 217  17
         Map<String, StoryReporter> reporters = new HashMap<String, StoryReporter>();
 218  17
         for (String storyPath : storyPaths) {
 219  24
             reporters.put(storyPath, build(storyPath));
 220  
         }
 221  17
         return reporters;
 222  
     }
 223  
 
 224  
     public StoryReporter reporterFor(String storyPath, Format format) {
 225  19
         FilePrintStreamFactory factory = filePrintStreamFactory(storyPath);
 226  19
         switch (format) {
 227  
         case CONSOLE:
 228  
         default:
 229  1
             return new ConsoleOutput(keywords).doReportFailureTrace(reportFailureTrace);
 230  
         case IDE_CONSOLE:
 231  1
             return new IdeOnlyConsoleOutput(keywords).doReportFailureTrace(reportFailureTrace);
 232  
         case TXT:
 233  7
             factory.useConfiguration(fileConfiguration("txt"));
 234  7
             return new TxtOutput(factory.createPrintStream(), keywords).doReportFailureTrace(reportFailureTrace);
 235  
         case HTML:
 236  3
             factory.useConfiguration(fileConfiguration("html"));
 237  3
             return new HtmlOutput(factory.createPrintStream(), keywords).doReportFailureTrace(reportFailureTrace);
 238  
         case XML:
 239  1
             factory.useConfiguration(fileConfiguration("xml"));
 240  1
             return new XmlOutput(factory.createPrintStream(), keywords).doReportFailureTrace(reportFailureTrace);
 241  
         case STATS:
 242  6
             factory.useConfiguration(fileConfiguration("stats"));
 243  6
             return new PostStoryStatisticsCollector(factory.createPrintStream());
 244  
         }
 245  
     }
 246  
 
 247  
     protected FilePrintStreamFactory filePrintStreamFactory(String storyPath) {
 248  35
         return new FilePrintStreamFactory(new StoryLocation(codeLocation, storyPath), fileConfiguration(""));
 249  
     }
 250  
 
 251  
     protected FileConfiguration fileConfiguration(String extension) {
 252  54
         return new FileConfiguration(relativeDirectory, extension, pathResolver);
 253  
     }
 254  
 
 255  
 }