Title: [1615] trunk/core/examples/trader: JBEHAVE-277: Allowed specification of output directory in ScenarioReporterBuilder as absolute to workaround the issue with Ant's classloading that specifies the class code source location as $ANT_HOME/lib.
Revision
1615
Author
mauro
Date
2010-04-24 08:05:39 -0500 (Sat, 24 Apr 2010)

Log Message

JBEHAVE-277: Allowed specification of output directory in ScenarioReporterBuilder as absolute to workaround the issue with Ant's classloading that specifies the class code source location as $ANT_HOME/lib.  

Modified Paths

Diff

Modified: trunk/core/examples/trader/build.xml (1614 => 1615)

--- trunk/core/examples/trader/build.xml	2010-04-21 18:11:27 UTC (rev 1614)
+++ trunk/core/examples/trader/build.xml	2010-04-24 13:05:39 UTC (rev 1615)
@@ -3,7 +3,7 @@
     Note: Use of <artifact:dependencies/> requires maven-ant-tasks-2.1.jar in $ANT_HOME/lib
           Download it from http://maven.apache.org/ant-tasks/download.html
   -->
-<project name="trader" default="all"
+<project name="trader" default="build"
   xmlns:artifact="antlib:org.apache.maven.artifact.ant">
 
   <artifact:pom id="pom" file="pom.xml" />
@@ -25,6 +25,7 @@
       <fileset dir="src/main/java">
         <include name="**/*.scenario" />
         <include name="**/*.properties" />
+        <include name="**/*.xml" />        
       </fileset>
     </copy>
 
@@ -105,7 +106,7 @@
       scenarioExcludes="**/i18n/scenarios/*.java" classLoaderInjected="false" />
   </target>
 
-  <target name="all" depends="run-scenarios,render-reports,stepdoc">
+  <target name="build" depends="run-scenarios,render-reports,stepdoc">
   </target>
 
 </project> 
\ No newline at end of file

Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java (1614 => 1615)

--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java	2010-04-21 18:11:27 UTC (rev 1614)
+++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/TraderScenario.java	2010-04-24 13:05:39 UTC (rev 1615)
@@ -42,7 +42,12 @@
 
             @Override
             public ScenarioReporter forReportingScenarios() {
+                // configure reports output to absolute directory (output specification in builder are usually optional)
+                // specifying output allows a workaround for Ant classloader that return 
+                // $ANT_HOME/lib as the default code source location (cf JBEHAVE-277)
                 return new ScenarioReporterBuilder(new FilePrintStreamFactory(scenarioClass, resolver))
+                            .outputTo("target/jbehave-reports") 
+                            .outputAsAbsolute(true)
                             .with(CONSOLE)
                             .with(TXT)
                             .with(HTML)

Modified: trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/reporters/ScenarioReporterBuilderBehaviour.java (1614 => 1615)

--- trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/reporters/ScenarioReporterBuilderBehaviour.java	2010-04-21 18:11:27 UTC (rev 1614)
+++ trunk/core/jbehave-core/src/behaviour/java/org/jbehave/scenario/reporters/ScenarioReporterBuilderBehaviour.java	2010-04-24 13:05:39 UTC (rev 1615)
@@ -49,7 +49,7 @@
         builder.outputTo(fileDirectory);
         
         // Then
-        ensureThat(builder.fileConfiguration("").getDirectory().endsWith(fileDirectory));
+        ensureThat(builder.fileConfiguration("").getOutputDirectory().endsWith(fileDirectory));
     }
 
     @Test

Modified: trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/reporters/FilePrintStreamFactory.java (1614 => 1615)

--- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/reporters/FilePrintStreamFactory.java	2010-04-21 18:11:27 UTC (rev 1614)
+++ trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/reporters/FilePrintStreamFactory.java	2010-04-24 13:05:39 UTC (rev 1615)
@@ -64,11 +64,17 @@
     }
 
     protected File outputDirectory(Class<? extends RunnableScenario> scenarioClass, FileConfiguration configuration) {
-        String classesDir = scenarioClass.getProtectionDomain().getCodeSource().getLocation().getFile();        
-        File targetDirectory = new File(classesDir).getParentFile();
-        return new File(targetDirectory, configuration.getDirectory());
+        if ( configuration.isOutputDirectoryAbsolute() ){
+            return new File(configuration.getOutputDirectory());
+        }        
+        return buildOutputDirectoryFromCodeLocation(scenarioClass, configuration);
     }
 
+    private File buildOutputDirectoryFromCodeLocation(Class<? extends RunnableScenario> scenarioClass, FileConfiguration configuration) {
+        File targetDirectory = new File(scenarioClass.getProtectionDomain().getCodeSource().getLocation().getFile()).getParentFile();
+        return new File(targetDirectory, configuration.getOutputDirectory());
+    }
+
     protected String fileName(Class<? extends RunnableScenario> scenarioClass, ScenarioNameResolver scenarioNameResolver,
             FileConfiguration configuration) {
         String scenarioName = scenarioNameResolver.resolve(scenarioClass).replace('/', '.');
@@ -77,39 +83,45 @@
     }
 
     /**
-     * Configuration class for file print streams. Allows specification of the
-     * file directory (relative to the scenario class code source location) and
-     * the file extension. Provides as defaults {...@link #DIRECTORY} and
-     * {...@link #HTML}.
+     * Configuration class for file print streams. Allows specification the 
+     * output directory (either absolute or relative to the code location) and
+     * the file extension. Provides as defaults {...@link #OUTPUT_DIRECTORY} (relative
+     * to class code location) and {...@link #HTML}. 
      */
     public static class FileConfiguration {
-        public static final String DIRECTORY = "jbehave-reports";
+        public static final String OUTPUT_DIRECTORY = "jbehave-reports";
         public static final String HTML = "html";
 
-        private final String directory;
+        private final String outputDirectory;
         private final String extension;
-
+        private final boolean outputAbsolute;
+        
         public FileConfiguration() {
-            this(DIRECTORY, HTML);
+            this(HTML);
         }
 
         public FileConfiguration(String extension) {
-            this(DIRECTORY, extension);
+            this(OUTPUT_DIRECTORY, false, extension);
         }
 
-        public FileConfiguration(String directory, String extension) {
-            this.directory = directory;
+        public FileConfiguration(String outputDirectory, boolean outputAbsolute, String extension) {
+            this.outputDirectory = outputDirectory;
+            this.outputAbsolute = outputAbsolute;
             this.extension = extension;
         }
 
-        public String getDirectory() {
-            return directory;
+        public String getOutputDirectory() {
+            return outputDirectory;
         }
 
         public String getExtension() {
             return extension;
         }
 
+        public boolean isOutputDirectoryAbsolute() {
+            return outputAbsolute;
+        }
+        
     }
 
 }

Modified: trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/reporters/ScenarioReporterBuilder.java (1614 => 1615)

--- trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/reporters/ScenarioReporterBuilder.java	2010-04-21 18:11:27 UTC (rev 1614)
+++ trunk/core/jbehave-core/src/main/java/org/jbehave/scenario/reporters/ScenarioReporterBuilder.java	2010-04-24 13:05:39 UTC (rev 1615)
@@ -34,7 +34,7 @@
  *  }
  * </pre>
  * </p>
- * <p>The builder configures the file-based reporters to output to the default file directory {...@link FileConfiguration#DIRECTORY}.
+ * <p>The builder configures the file-based reporters to output to the default file directory {...@link FileConfiguration#OUTPUT_DIRECTORY}.
  * To change the default:
  * <pre>
  * new ScenarioReporterBuilder(printStreamFactory).outputTo("my-reports").with(HTML).with(TXT).build();
@@ -64,7 +64,8 @@
 
     protected final FilePrintStreamFactory factory;
     protected Map<Format, ScenarioReporter> delegates = new HashMap<Format, ScenarioReporter>();
-    private String fileDirectory = new FileConfiguration().getDirectory();
+    private String outputDirectory = new FileConfiguration().getOutputDirectory();
+    private boolean outputAbsolute = new FileConfiguration().isOutputDirectoryAbsolute();
 
     public ScenarioReporterBuilder(FilePrintStreamFactory factory) {
         this.factory = factory;
@@ -79,10 +80,15 @@
         return new DelegatingScenarioReporter(delegates.values());
     }
 
-    public ScenarioReporterBuilder outputTo(String fileDirectory){        
-        this.fileDirectory = fileDirectory;
+    public ScenarioReporterBuilder outputTo(String outputDirector){        
+        this.outputDirectory = outputDirector;
         return this;
     }
+
+    public ScenarioReporterBuilder outputAsAbsolute(boolean outputAbsolute) {
+        this.outputAbsolute = outputAbsolute;
+        return this;
+    }
     
     public ScenarioReporterBuilder with(Format format) {
         delegates.put(format, reporterFor(format));
@@ -115,7 +121,7 @@
     }
 
     protected FileConfiguration fileConfiguration(String extension) {
-        return new FileConfiguration(fileDirectory, extension);
+        return new FileConfiguration(outputDirectory, outputAbsolute, extension);
     }
 
     @SuppressWarnings("serial")
@@ -127,4 +133,5 @@
 
     }
 
+
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to