Coverage Report - org.jbehave.mojo.AbstractEmbedderMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEmbedderMojo
94%
70/74
87%
21/24
1.457
AbstractEmbedderMojo$MavenEmbedderMonitor
39%
34/87
12%
1/8
1.457
 
 1  
 package org.jbehave.mojo;
 2  
 
 3  
 import static org.apache.commons.lang.ArrayUtils.isNotEmpty;
 4  
 
 5  
 import java.io.File;
 6  
 import java.net.MalformedURLException;
 7  
 import java.net.URL;
 8  
 import java.util.ArrayList;
 9  
 import java.util.List;
 10  
 import java.util.Properties;
 11  
 import java.util.concurrent.ExecutorService;
 12  
 
 13  
 import org.apache.maven.plugin.AbstractMojo;
 14  
 import org.jbehave.core.ConfigurableEmbedder;
 15  
 import org.jbehave.core.InjectableEmbedder;
 16  
 import org.jbehave.core.embedder.Embedder;
 17  
 import org.jbehave.core.embedder.EmbedderClassLoader;
 18  
 import org.jbehave.core.embedder.EmbedderControls;
 19  
 import org.jbehave.core.embedder.EmbedderMonitor;
 20  
 import org.jbehave.core.embedder.MetaFilter;
 21  
 import org.jbehave.core.embedder.NullEmbedderMonitor;
 22  
 import org.jbehave.core.embedder.UnmodifiableEmbedderControls;
 23  
 import org.jbehave.core.embedder.executors.ExecutorServiceFactory;
 24  
 import org.jbehave.core.failures.BatchFailures;
 25  
 import org.jbehave.core.io.StoryFinder;
 26  
 import org.jbehave.core.junit.AnnotatedEmbedderRunner;
 27  
 import org.jbehave.core.model.Meta;
 28  
 import org.jbehave.core.model.Scenario;
 29  
 import org.jbehave.core.model.Story;
 30  
 import org.jbehave.core.model.StoryDuration;
 31  
 import org.jbehave.core.model.StoryMaps;
 32  
 import org.jbehave.core.reporters.ReportsCount;
 33  
 
 34  
 /**
 35  
  * Abstract mojo that holds all the configuration parameters to specify and load
 36  
  * stories.
 37  
  * 
 38  
  * @requiresDependencyResolution test
 39  
  */
 40  28
 public abstract class AbstractEmbedderMojo extends AbstractMojo {
 41  
 
 42  
     static final String TEST_SCOPE = "test";
 43  
 
 44  
     /**
 45  
      * @parameter expression="${project.build.sourceDirectory}"
 46  
      * @required
 47  
      */
 48  
     String sourceDirectory;
 49  
 
 50  
     /**
 51  
      * @parameter expression="${project.build.testSourceDirectory}"
 52  
      * @required
 53  
      */
 54  
     String testSourceDirectory;
 55  
 
 56  
     /**
 57  
      * @parameter expression="${project.build.outputDirectory}"
 58  
      * @required
 59  
      */
 60  
     String outputDirectory;
 61  
 
 62  
     /**
 63  
      * @parameter expression="${project.build.testOutputDirectory}"
 64  
      * @required
 65  
      */
 66  
     String testOutputDirectory;
 67  
 
 68  
     /**
 69  
      * The scope of the mojo classpath, either "compile" or "test"
 70  
      * 
 71  
      * @parameter default-value="compile"
 72  
      */
 73  
     String scope;
 74  
 
 75  
     /**
 76  
      * Include filters, relative to the root source directory determined by the
 77  
      * scope
 78  
      * 
 79  
      * @parameter
 80  
      */
 81  
     List<String> includes;
 82  
 
 83  
     /**
 84  
      * Exclude filters, relative to the root source directory determined by the
 85  
      * scope
 86  
      * 
 87  
      * @parameter
 88  
      */
 89  
     List<String> excludes;
 90  
 
 91  
     /**
 92  
      * Compile classpath.
 93  
      * 
 94  
      * @parameter expression="${project.compileClasspathElements}"
 95  
      * @required
 96  
      * @readonly
 97  
      */
 98  
     List<String> compileClasspathElements;
 99  
 
 100  
     /**
 101  
      * Test classpath.
 102  
      * 
 103  
      * @parameter expression="${project.testClasspathElements}"
 104  
      * @required
 105  
      * @readonly
 106  
      */
 107  
     List<String> testClasspathElements;
 108  
 
 109  
     /**
 110  
      * The boolean flag to skip stories
 111  
      * 
 112  
      * @parameter default-value="false"
 113  
      */
 114  28
     boolean skip = false;
 115  
 
 116  
     /**
 117  
      * The boolean flag to run in batch mode
 118  
      * 
 119  
      * @parameter default-value="false"
 120  
      */
 121  28
     boolean batch = false;
 122  
 
 123  
     /**
 124  
      * The boolean flag to ignore failure in stories
 125  
      * 
 126  
      * @parameter default-value="false"
 127  
      */
 128  28
     boolean ignoreFailureInStories = false;
 129  
 
 130  
     /**
 131  
      * The boolean flag to ignore failure in view
 132  
      * 
 133  
      * @parameter default-value="false"
 134  
      */
 135  28
     boolean ignoreFailureInView = false;
 136  
 
 137  
     /**
 138  
      * The boolean flag to generate view after stories are run
 139  
      * 
 140  
      * @parameter default-value="true"
 141  
      */
 142  28
     boolean generateViewAfterStories = true;
 143  
 
 144  
     /**
 145  
      * The boolean flag to output failures in verbose mode
 146  
      * 
 147  
      * @parameter default-value="false"
 148  
      */
 149  28
     boolean verboseFailures = false;
 150  
 
 151  
     /**
 152  
      * The boolean flag to output filtering in verbose mode
 153  
      * 
 154  
      * @parameter default-value="false"
 155  
      */
 156  28
     boolean verboseFiltering = false;
 157  
 
 158  
     /**
 159  
      * The story timeout in secs
 160  
      * 
 161  
      * @parameter default-value="300"
 162  
      */
 163  28
     long storyTimeoutInSecs = 300;
 164  
     
 165  
     /**
 166  
      * The story timeout in secs by path
 167  
      * 
 168  
      * @parameter default-value=""
 169  
      */
 170  28
     String storyTimeoutInSecsByPath = "";
 171  
 
 172  
     /**
 173  
      * The boolean flag to fail on story timeout
 174  
      * 
 175  
      * @parameter default-value="false"
 176  
      */
 177  28
     boolean failOnStoryTimeout = false;
 178  
 
 179  
     /**
 180  
      * The number of threads
 181  
      * 
 182  
      * @parameter default-value="1"
 183  
      */
 184  28
     int threads = 1;
 185  
 
 186  
     /**
 187  
      * The embedder class
 188  
      * 
 189  
      * @parameter default-value="org.jbehave.core.embedder.Embedder"
 190  
      */
 191  28
     String embedderClass = Embedder.class.getName();
 192  
 
 193  
     /**
 194  
      * The implementation class of the {@link ExecutorServiceFactory}
 195  
      * 
 196  
      * @parameter
 197  
      */
 198  
     String executorsClass;
 199  
 
 200  
     /**
 201  
      * The class that is injected with the embedder
 202  
      * 
 203  
      * @parameter
 204  
      */
 205  
     String injectableEmbedderClass;
 206  
 
 207  
     /**
 208  
      * The annotated embedder runner class
 209  
      * 
 210  
      * @parameter default-value="org.jbehave.core.junit.AnnotatedEmbedderRunner"
 211  
      * @deprecated Obsolete
 212  
      */
 213  28
     String annotatedEmbedderRunnerClass = AnnotatedEmbedderRunner.class.getName();
 214  
 
 215  
     /**
 216  
      * Used to find story paths and class names
 217  
      * 
 218  
      * @parameter
 219  
      */
 220  28
     String storyFinderClass = StoryFinder.class.getName();
 221  
 
 222  
     /**
 223  
      * The meta filter
 224  
      * 
 225  
      * @parameter
 226  
      */
 227  
     String[] metaFilters;
 228  
 
 229  
     /**
 230  
      * The system properties
 231  
      * 
 232  
      * @parameter
 233  
      */
 234  28
     Properties systemProperties = new Properties();
 235  
 
 236  
     /**
 237  
      * The class loader
 238  
      */
 239  
     private EmbedderClassLoader classLoader;
 240  
 
 241  
     /**
 242  
      * Determines if the scope of the mojo classpath is "test"
 243  
      * 
 244  
      * @return A boolean <code>true</code> if test scoped
 245  
      */
 246  
     boolean isTestScope() {
 247  32
         return TEST_SCOPE.equals(scope);
 248  
     }
 249  
 
 250  
     String searchDirectory() {
 251  11
         if (isTestScope()) {
 252  1
             return testSourceDirectory;
 253  
         }
 254  10
         return sourceDirectory;
 255  
     }
 256  
 
 257  
     String outputDirectory() {
 258  10
         if (isTestScope()) {
 259  0
             return testOutputDirectory;
 260  
         }
 261  10
         return outputDirectory;
 262  
     }
 263  
 
 264  
     URL codeLocation() {
 265  10
         String outputDirectory = outputDirectory();
 266  
         try {
 267  10
             return outputDirectory != null ? new File(outputDirectory).toURI().toURL() : null;
 268  0
         } catch (MalformedURLException e) {
 269  0
             throw new IllegalArgumentException("Failed to create code location from " + outputDirectory, e);
 270  
         }
 271  
     }
 272  
 
 273  
     /**
 274  
      * Returns the EmbedderClassLoader with the classpath element of the
 275  
      * selected scope.
 276  
      * 
 277  
      * @return An EmbedderClassLoader
 278  
      */
 279  
     protected EmbedderClassLoader classLoader() {
 280  10
         if (classLoader == null) {
 281  10
             classLoader = new EmbedderClassLoader(classpathElements());
 282  
         }
 283  10
         return classLoader;
 284  
     }
 285  
 
 286  
     List<String> classpathElements() {
 287  11
         List<String> classpathElements = compileClasspathElements;
 288  11
         if (isTestScope()) {
 289  1
             classpathElements = testClasspathElements;
 290  
         }
 291  11
         return classpathElements;
 292  
     }
 293  
 
 294  
     /**
 295  
      * Finds story paths, using the {@link #newStoryFinder()}, in the
 296  
      * {@link #searchDirectory()} given specified {@link #includes} and
 297  
      * {@link #excludes}.
 298  
      * 
 299  
      * @return A List of story paths found
 300  
      */
 301  
     protected List<String> storyPaths() {
 302  4
         getLog().debug("Searching for story paths including " + includes + " and excluding " + excludes);
 303  4
         List<String> storyPaths = newStoryFinder().findPaths(searchDirectory(), includes, excludes);
 304  4
         getLog().info("Found story paths: " + storyPaths);
 305  4
         return storyPaths;
 306  
     }
 307  
 
 308  
     /**
 309  
      * Finds class names, using the {@link #newStoryFinder()}, in the
 310  
      * {@link #searchDirectory()} given specified {@link #includes} and
 311  
      * {@link #excludes}.
 312  
      * 
 313  
      * @return A List of class names found
 314  
      */
 315  
     protected List<String> classNames() {
 316  6
         getLog().debug("Searching for class names including " + includes + " and excluding " + excludes);
 317  6
         List<String> classNames = newStoryFinder().findClassNames(searchDirectory(), includes, excludes);
 318  6
         getLog().info("Found class names: " + classNames);
 319  6
         return classNames;
 320  
     }
 321  
 
 322  
     /**
 323  
      * Creates an instance of StoryFinder, using the {@link #storyFinderClass}
 324  
      * 
 325  
      * @return A StoryFinder
 326  
      */
 327  
     protected StoryFinder newStoryFinder() {
 328  11
         return classLoader().newInstance(StoryFinder.class, storyFinderClass);
 329  
     }
 330  
 
 331  
     /**
 332  
      * Creates an instance of Embedder, either using
 333  
      * {@link #injectableEmbedderClass} (if set) or defaulting to
 334  
      * {@link #embedderClass}.
 335  
      * 
 336  
      * @return An Embedder
 337  
      */
 338  
     protected Embedder newEmbedder() {
 339  9
         Embedder embedder = null;
 340  9
         EmbedderClassLoader classLoader = classLoader();
 341  9
         if (injectableEmbedderClass != null) {
 342  1
             embedder = classLoader.newInstance(InjectableEmbedder.class, injectableEmbedderClass).injectedEmbedder();
 343  
         } else {
 344  8
             embedder = classLoader.newInstance(Embedder.class, embedderClass);
 345  
         }
 346  
         
 347  9
         URL codeLocation = codeLocation();
 348  9
         if (codeLocation != null) {
 349  0
             embedder.configuration().storyReporterBuilder().withCodeLocation(codeLocation);
 350  
         }
 351  
 
 352  9
         embedder.useClassLoader(classLoader);
 353  9
         embedder.useEmbedderControls(embedderControls());
 354  9
         if ( executorsClass != null ){
 355  1
             ExecutorServiceFactory executorServiceFactory = classLoader.newInstance(ExecutorServiceFactory.class, executorsClass);
 356  1
             embedder.useExecutorService(executorServiceFactory.create(embedder.embedderControls()));
 357  
         }
 358  9
         embedder.useEmbedderMonitor(embedderMonitor());
 359  9
         if (isNotEmpty(metaFilters)) {
 360  2
             List<String> filters = new ArrayList<String>();
 361  6
             for (String filter : metaFilters) {
 362  4
                 if (filter != null) {
 363  3
                     filters.add(filter);
 364  
                 }
 365  
             }
 366  2
             embedder.useMetaFilters(filters);
 367  
         }
 368  9
         if (!systemProperties.isEmpty()) {
 369  1
             embedder.useSystemProperties(systemProperties);
 370  
         }
 371  9
         return embedder;
 372  
     }
 373  
 
 374  
     protected EmbedderMonitor embedderMonitor() {
 375  9
         return new MavenEmbedderMonitor();
 376  
     }
 377  
 
 378  
     protected EmbedderControls embedderControls() {
 379  9
         return new UnmodifiableEmbedderControls(new EmbedderControls().doBatch(batch).doSkip(skip)
 380  9
                 .doGenerateViewAfterStories(generateViewAfterStories).doIgnoreFailureInStories(ignoreFailureInStories)
 381  9
                 .doIgnoreFailureInView(ignoreFailureInView).doVerboseFailures(verboseFailures)
 382  9
                 .doVerboseFiltering(verboseFiltering).useStoryTimeoutInSecs(storyTimeoutInSecs)
 383  9
                 .useStoryTimeoutInSecsByPath(storyTimeoutInSecsByPath)
 384  9
                 .doFailOnStoryTimeout(failOnStoryTimeout).useThreads(threads));
 385  
     }
 386  
 
 387  28
     protected class MavenEmbedderMonitor extends NullEmbedderMonitor {
 388  
 
 389  
         public void batchFailed(BatchFailures failures) {
 390  1
             getLog().warn("Failed to run batch " + failures);
 391  1
         }
 392  
 
 393  
         public void beforeOrAfterStoriesFailed() {
 394  0
             getLog().warn("Failed to run before or after stories steps");
 395  0
         }
 396  
 
 397  
         public void embeddableFailed(String name, Throwable cause) {
 398  1
             getLog().warn("Failed to run embeddable " + name, cause);
 399  1
         }
 400  
 
 401  
         public void embeddableNotConfigurable(String name) {
 402  0
             getLog().warn("Embeddable " + name + " must be an instance of " + ConfigurableEmbedder.class);
 403  0
         }
 404  
 
 405  
         public void embeddablesSkipped(List<String> classNames) {
 406  1
             getLog().info("Skipped embeddables " + classNames);
 407  1
         }
 408  
 
 409  
         public void metaNotAllowed(Meta meta, MetaFilter filter) {
 410  0
             getLog().debug(meta + " excluded by filter '" + filter.asString() + "'");
 411  0
         }
 412  
 
 413  
         public void runningEmbeddable(String name) {
 414  1
             getLog().info("Running embeddable " + name);
 415  1
         }
 416  
 
 417  
         public void runningStory(String path) {
 418  1
             getLog().info("Running story " + path);
 419  1
         }
 420  
 
 421  
         public void storyFailed(String path, Throwable cause) {
 422  1
             getLog().warn("Failed to run story " + path, cause);
 423  1
         }
 424  
 
 425  
         public void storiesSkipped(List<String> storyPaths) {
 426  1
             getLog().info("Skipped stories " + storyPaths);
 427  1
         }
 428  
 
 429  
         public void storiesNotAllowed(List<Story> stories, MetaFilter filter, boolean verbose) {
 430  0
             StringBuffer sb = new StringBuffer();
 431  0
             sb.append(stories.size() + " stories excluded by filter: " + filter.asString() + "\n");
 432  0
             if (verbose) {
 433  0
                 for (Story story : stories) {
 434  0
                     sb.append(story.getPath()).append("\n");
 435  0
                 }
 436  
             }
 437  0
             getLog().info(sb.toString());
 438  0
         }
 439  
 
 440  
             public void scenarioNotAllowed(Scenario scenario, MetaFilter filter) {
 441  0
             StringBuffer sb = new StringBuffer();
 442  0
             sb.append("Scenario '"+scenario.getTitle()+"' excluded by filter: " + filter.asString() + "\n");
 443  0
             getLog().info(sb.toString());
 444  0
             }
 445  
 
 446  
         public void runningWithAnnotatedEmbedderRunner(String className) {
 447  0
             getLog().info("Running with AnnotatedEmbedderRunner '" + className + "'");
 448  0
         }
 449  
 
 450  
         public void annotatedInstanceNotOfType(Object annotatedInstance, Class<?> type) {
 451  1
             getLog().warn("Annotated instance " + annotatedInstance + " not of type " + type);
 452  1
         }
 453  
 
 454  
         public void generatingReportsView(File outputDirectory, List<String> formats, Properties viewProperties) {
 455  1
             getLog().info(
 456  
                     "Generating reports view to '" + outputDirectory + "' using formats '" + formats + "'"
 457  
                             + " and view properties '" + viewProperties + "'");
 458  1
         }
 459  
 
 460  
         public void reportsViewGenerationFailed(File outputDirectory, List<String> formats, Properties viewProperties,
 461  
                 Throwable cause) {
 462  1
             String message = "Failed to generate reports view to '" + outputDirectory + "' using formats '" + formats
 463  
                     + "'" + " and view properties '" + viewProperties + "'";
 464  1
             getLog().warn(message, cause);
 465  1
         }
 466  
 
 467  
         public void reportsViewGenerated(ReportsCount count) {
 468  2
             getLog().info(
 469  1
                     "Reports view generated with " + count.getStories() + " stories (of which "
 470  1
                             + count.getStoriesPending() + " pending) containing " + count.getScenarios()
 471  1
                             + " scenarios (of which " + count.getScenariosPending() + " pending)");
 472  1
             if (count.getStoriesNotAllowed() > 0 || count.getScenariosNotAllowed() > 0) {
 473  2
                 getLog().info(
 474  1
                         "Meta filters excluded " + count.getStoriesNotAllowed() + " stories and  "
 475  1
                                 + count.getScenariosNotAllowed() + " scenarios");
 476  
             }
 477  1
         }
 478  
 
 479  
         public void reportsViewFailures(ReportsCount count) {
 480  0
             getLog().warn("Failures in reports view: " + count.getScenariosFailed() + " scenarios failed");
 481  0
         }
 482  
 
 483  
         public void reportsViewNotGenerated() {
 484  1
             getLog().info("Reports view not generated");
 485  1
         }
 486  
 
 487  
         public void mappingStory(String storyPath, List<String> metaFilters) {
 488  0
             getLog().info("Mapping story " + storyPath + " with meta filters " + metaFilters);
 489  0
         }
 490  
 
 491  
         public void generatingMapsView(File outputDirectory, StoryMaps storyMaps, Properties viewProperties) {
 492  0
             getLog().info(
 493  
                     "Generating maps view to '" + outputDirectory + "' using story maps '" + storyMaps + "'"
 494  
                             + " and view properties '" + viewProperties + "'");
 495  0
         }
 496  
 
 497  
         public void mapsViewGenerationFailed(File outputDirectory, StoryMaps storyMaps, Properties viewProperties,
 498  
                 Throwable cause) {
 499  0
             getLog().warn(
 500  
                     "Failed to generate maps view to '" + outputDirectory + "' using story maps '" + storyMaps + "'"
 501  
                             + " and view properties '" + viewProperties + "'", cause);
 502  0
         }
 503  
 
 504  
         public void generatingNavigatorView(File outputDirectory, Properties viewProperties) {
 505  0
             getLog().info(
 506  
                     "Generating navigator view to '" + outputDirectory + "' using view properties '" + viewProperties
 507  
                             + "'");
 508  0
         }
 509  
 
 510  
         public void navigatorViewGenerationFailed(File outputDirectory, Properties viewProperties, Throwable cause) {
 511  0
             getLog().warn(
 512  
                     "Failed to generate navigator view to '" + outputDirectory + "' using view properties '"
 513  
                             + viewProperties + "'", cause);
 514  0
         }
 515  
 
 516  
         public void navigatorViewNotGenerated() {
 517  0
             getLog().warn(
 518  
                     "Navigator view not generated, as the CrossReference has not been declared in the StoryReporterBuilder");
 519  0
         }
 520  
 
 521  
         public void processingSystemProperties(Properties properties) {
 522  0
             getLog().info("Processing system properties " + properties);
 523  0
         }
 524  
 
 525  
         public void systemPropertySet(String name, String value) {
 526  0
             getLog().info("System property '" + name + "' set to '" + value + "'");
 527  0
         }
 528  
 
 529  
         public void storyTimeout(Story story, StoryDuration storyDuration) {
 530  0
             getLog().warn(
 531  0
                     "Story " + story.getPath() + " duration of " + storyDuration.getDurationInSecs()
 532  0
                             + " seconds has exceeded timeout of " + storyDuration.getTimeoutInSecs() + " seconds");
 533  0
         }
 534  
 
 535  
         public void usingThreads(int threads) {
 536  0
             getLog().info("Using " + threads + " threads");
 537  0
         }
 538  
 
 539  
         public void usingExecutorService(ExecutorService executorService) {
 540  0
             getLog().info("Using executor service " + executorService);
 541  0
         }
 542  
 
 543  
         public void usingControls(EmbedderControls embedderControls) {
 544  0
             getLog().info("Using controls " + embedderControls);
 545  0
         }
 546  
         
 547  
         public void invalidTimeoutFormat(String path) {
 548  0
                 getLog().warn("Failed to set specific story timeout for story " + path + " because 'storyTimeoutInSecsByPath' has incorrect format");
 549  0
                 getLog().warn("'storyTimeoutInSecsByPath' must be a CSV of regex expressions matching story paths. E.g. \"*/long/*.story:5000,*/short/*.story:200\"");
 550  0
             }
 551  
 
 552  
             public void usingTimeout(String path, long timeout) {
 553  0
                 getLog().info("Using timeout for story " + path + " of "+timeout+" secs.");
 554  0
             }
 555  
 
 556  
         @Override
 557  
         public String toString() {
 558  1
             return this.getClass().getSimpleName();
 559  
         }
 560  
 
 561  
     }
 562  
 }