- Revision
- 690
- Author
- mauro
- Date
- 2007-02-26 11:58:54 -0600 (Mon, 26 Feb 2007)
Log Message
Added StoryLoader to abstract the loading of a Story - both via StoryParser and from class name. Refactored StoryPrinter to have StoryLoader and Renderer as injectable dependendencies.
Modified Paths
- trunk/core/src/behaviour/org/jbehave/core/story/StoryPrinterBehaviour.java
- trunk/core/src/java/org/jbehave/core/story/StoryPrinter.java
- trunk/plugins/maven/src/main/java/org/jbehave/mojo/StoryRunnerMojo.java
Added Paths
Diff
Modified: trunk/core/src/behaviour/org/jbehave/core/story/StoryPrinterBehaviour.java (689 => 690)
--- trunk/core/src/behaviour/org/jbehave/core/story/StoryPrinterBehaviour.java 2007-02-26 17:32:51 UTC (rev 689) +++ trunk/core/src/behaviour/org/jbehave/core/story/StoryPrinterBehaviour.java 2007-02-26 17:58:54 UTC (rev 690) @@ -4,16 +4,19 @@ import java.io.PrintStream; import org.jbehave.core.mock.UsingMatchers; +import org.jbehave.core.story.codegen.parser.TextStoryParser; +import org.jbehave.core.story.renderer.PlainTextRenderer; public class StoryPrinterBehaviour extends UsingMatchers { - - + public void shouldSpecifyAndPrintStoryAsPlainText() throws Exception { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(byteStream); - StoryPrinter printer = new StoryPrinter(printStream); + StoryPrinter printer = new StoryPrinter( + new StoryLoader(new TextStoryParser(), Thread.currentThread().getContextClassLoader()), + new PlainTextRenderer(System.out)); printer.print(SimpleStory.class.getName()); String result = byteStream.toString();
Added: trunk/core/src/java/org/jbehave/core/story/StoryLoader.java (0 => 690)
--- trunk/core/src/java/org/jbehave/core/story/StoryLoader.java (rev 0) +++ trunk/core/src/java/org/jbehave/core/story/StoryLoader.java 2007-02-26 17:58:54 UTC (rev 690) @@ -0,0 +1,45 @@ +package org.jbehave.core.story; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.MalformedURLException; +import java.util.NoSuchElementException; + +import org.jbehave.core.story.codegen.domain.StoryDetails; +import org.jbehave.core.story.codegen.parser.StoryParser; +import org.jbehave.core.story.domain.Story; + +/** + * StoryLoader parses story details from a resource in the classpath and build a Story via the StoryBuilder. + * + * @author Mauro Talevi + * @see StoryBuilder + */ +public class StoryLoader { + + private ClassLoader classLoader; + private StoryParser storyParser; + + public StoryLoader(StoryParser storyParser, ClassLoader classLoader) { + this.classLoader = classLoader; + this.storyParser = storyParser; + } + + public Story loadStory(String storyPath, String storyPackage) throws MalformedURLException { + StoryDetails storyDetails = storyParser.parseStory(getReader(storyPath, classLoader)); + return new StoryBuilder(storyDetails, storyPackage).story(); + } + + public Story loadStory(String storyClassName) throws InstantiationException, IllegalAccessException, ClassNotFoundException { + return (Story) classLoader.loadClass(storyClassName).newInstance(); + } + + protected Reader getReader(String resource, ClassLoader classLoader) { + InputStream is = classLoader.getResourceAsStream(resource); + if ( is == null ){ + throw new NoSuchElementException("Resource "+resource+" not found in ClassLoader "+classLoader.getClass()); + } + return new InputStreamReader(is); + } +}
Modified: trunk/core/src/java/org/jbehave/core/story/StoryPrinter.java (689 => 690)
--- trunk/core/src/java/org/jbehave/core/story/StoryPrinter.java 2007-02-26 17:32:51 UTC (rev 689) +++ trunk/core/src/java/org/jbehave/core/story/StoryPrinter.java 2007-02-26 17:58:54 UTC (rev 690) @@ -6,50 +6,37 @@ * See license.txt for license details */ package org.jbehave.core.story; -import java.io.PrintStream; +import java.net.MalformedURLException; import org.jbehave.core.story.domain.Story; -import org.jbehave.core.story.renderer.PlainTextRenderer; +import org.jbehave.core.story.renderer.Renderer; - - /** - * TODO Introduce StoryLoader + * A StoryPrinter loads a story and narrates it to a given renderer. * * @author <a href="" PROTECTED]">Dan North</a> * @author Mauro Talevi */ public class StoryPrinter { - private ClassLoader classLoader; - private final PrintStream stream; + private StoryLoader storyLoader; + private Renderer renderer; - public StoryPrinter(){ - this(System.out); + public StoryPrinter(StoryLoader storyLoader, Renderer renderer) { + this.storyLoader = storyLoader; + this.renderer = renderer; } - - public StoryPrinter(PrintStream stream) { - this(Thread.currentThread().getContextClassLoader(), stream); - } - private StoryPrinter(ClassLoader classLoader, PrintStream stream) { - this.classLoader = classLoader; - this.stream = stream; + public void print(String storyPath, String storyPackage) throws MalformedURLException { + Story story = storyLoader.loadStory(storyPath, storyPackage); + story.specify(); + story.narrateTo(renderer); } - + public void print(String storyClassName) throws InstantiationException, IllegalAccessException, ClassNotFoundException{ - Story story = (Story) classLoader.loadClass(storyClassName).newInstance(); + Story story = storyLoader.loadStory(storyClassName); story.specify(); - story.narrateTo(new PlainTextRenderer(stream)); + story.narrateTo(renderer); } - public static void main(String[] args) { - try { - StoryPrinter printer = new StoryPrinter(); - printer.print(args[0]); - } catch (Exception e) { - e.printStackTrace(); - } - } - }
Modified: trunk/plugins/maven/src/main/java/org/jbehave/mojo/StoryRunnerMojo.java (689 => 690)
--- trunk/plugins/maven/src/main/java/org/jbehave/mojo/StoryRunnerMojo.java 2007-02-26 17:32:51 UTC (rev 689) +++ trunk/plugins/maven/src/main/java/org/jbehave/mojo/StoryRunnerMojo.java 2007-02-26 17:58:54 UTC (rev 690) @@ -1,18 +1,12 @@ package org.jbehave.mojo; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.net.MalformedURLException; import java.util.List; -import java.util.NoSuchElementException; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.jbehave.core.story.StoryBuilder; +import org.jbehave.core.story.StoryLoader; import org.jbehave.core.story.StoryRunner; -import org.jbehave.core.story.codegen.domain.StoryDetails; import org.jbehave.core.story.codegen.parser.StoryParser; import org.jbehave.core.story.codegen.parser.TextStoryParser; import org.jbehave.core.story.domain.Story; @@ -55,28 +49,13 @@ public void execute() throws MojoExecutionException, MojoFailureException { try { getLog().debug("Running story "+ storyPath); - Story story = buildStory(storyPath, storyPackage); + StoryLoader loader = new StoryLoader(storyParser, new BehavioursClassLoader(classpathElements)); + Story story = loader.loadStory(storyPath, storyPackage); storyRunner.run(story); } catch (Exception e) { throw new MojoExecutionException("Failed to run story "+storyPath+" with package "+storyPackage, e); } } - private Story buildStory(String storyPath, String storyPackage) throws MalformedURLException { - StoryDetails storyDetails = storyParser.parseStory(getReader(storyPath)); - return new StoryBuilder(storyDetails, storyPackage).story(); - } - - private Reader getReader(String resource) throws MalformedURLException { - BehavioursClassLoader cl = new BehavioursClassLoader(classpathElements); - return getReader(resource, cl); - } - - protected Reader getReader(String resource, ClassLoader classLoader) { - InputStream is = classLoader.getResourceAsStream(resource); - if ( is == null ){ - throw new NoSuchElementException("Resource "+resource+" not found in ClassLoader "+classLoader.getClass()); - } - return new InputStreamReader(is); - } + }
To unsubscribe from this list please visit:
