- Revision
- 788
- Author
- mauro
- Date
- 2007-09-05 09:28:57 -0500 (Wed, 05 Sep 2007)
Log Message
Refactored StoryRunner to use StoryLoader to load stories.
Modified Paths
- trunk/core/src/behaviour/org/jbehave/core/story/StoryPrinterBehaviour.java
- trunk/core/src/behaviour/org/jbehave/core/story/StoryRunnerBehaviour.java
- trunk/core/src/java/org/jbehave/core/story/StoryLoader.java
- trunk/core/src/java/org/jbehave/core/story/StoryRunner.java
Diff
Modified: trunk/core/src/behaviour/org/jbehave/core/story/StoryPrinterBehaviour.java (787 => 788)
--- trunk/core/src/behaviour/org/jbehave/core/story/StoryPrinterBehaviour.java 2007-09-05 10:10:39 UTC (rev 787) +++ trunk/core/src/behaviour/org/jbehave/core/story/StoryPrinterBehaviour.java 2007-09-05 14:28:57 UTC (rev 788) @@ -15,7 +15,7 @@ PrintStream printStream = new PrintStream(byteStream); StoryPrinter printer = new StoryPrinter( - new StoryLoader(new TextStoryParser(), Thread.currentThread().getContextClassLoader()), + new StoryLoader(new TextStoryParser()), new PlainTextRenderer(printStream)); printer.print(SimpleStory.class); String result = byteStream.toString();
Modified: trunk/core/src/behaviour/org/jbehave/core/story/StoryRunnerBehaviour.java (787 => 788)
--- trunk/core/src/behaviour/org/jbehave/core/story/StoryRunnerBehaviour.java 2007-09-05 10:10:39 UTC (rev 787) +++ trunk/core/src/behaviour/org/jbehave/core/story/StoryRunnerBehaviour.java 2007-09-05 14:28:57 UTC (rev 788) @@ -14,65 +14,64 @@ import org.jbehave.core.story.listener.PlainTextScenarioListener; import org.jbehave.core.story.renderer.Renderer; - public class StoryRunnerBehaviour extends UsingMiniMock { - public void shouldSpecifyAndRunStoryAndOutputResults() throws Exception { - final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - - PrintStream stream = new PrintStream(buffer); - - new StoryRunner().run(SimpleStory.class.getName(), stream); + public void shouldSpecifyAndRunStoryAndOutputResults() throws Exception { + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + PrintStream stream = new PrintStream(buffer); + + new StoryRunner().run(SimpleStory.class.getName(), stream); + Ensure.that(buffer.toString(), contains("..")); - Ensure.that(buffer.toString(), contains("Total: 2.")); - - verifyMocks(); - } - - public void shouldOutputPendingExeptionsWithoutFailingTheStory() throws Exception { - final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - final PrintStream stream = new PrintStream(buffer); - - Exception exception = runAndCatch(Exception.class, new Block() { - public void run() throws Exception { - new StoryRunner().run(PendingStory.class.getName(), stream); - } - }); + Ensure.that(buffer.toString(), contains("Total: 2.")); - Ensure.that(exception, isNull()); + verifyMocks(); + } + + public void shouldOutputPendingExeptionsWithoutFailingTheStory() throws Exception { + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + final PrintStream stream = new PrintStream(buffer); + + Exception exception = runAndCatch(Exception.class, new Block() { + public void run() throws Exception { + new StoryRunner().run(PendingStory.class.getName(), stream); + } + }); + + Ensure.that(exception, isNull()); Ensure.that(buffer.toString(), contains("P")); - Ensure.that(buffer.toString(), contains("Total: 1. Pending: 1.")); - - verifyMocks(); - } - - public static class PendingStory extends UsingMiniMock implements Story { - private Mock mock; - - public PendingStory() { - mock = mock(Story.class); + Ensure.that(buffer.toString(), contains("Total: 1. Pending: 1.")); + + verifyMocks(); + } + + public static class PendingStory extends UsingMiniMock implements Story { + private Mock mock; + + public PendingStory() { + mock = mock(Story.class); mock.expects("addListener").with(isA(PlainTextScenarioListener.class)); - mock.expects("run").will(throwException(new PendingException("TODO"))); - } + mock.expects("run").will(throwException(new PendingException("TODO"))); + } - public Narrative narrative() { - return ((Story)mock).narrative(); - } + public Narrative narrative() { + return ((Story) mock).narrative(); + } - public void run() { - ((Story)mock).run(); - } + public void run() { + ((Story) mock).run(); + } public void addListener(BehaviourListener listener) { - ((Story)mock).addListener(listener); + ((Story) mock).addListener(listener); } public void narrateTo(Renderer renderer) { throw new UnsupportedOperationException("Should not be called"); } - + public void specify() { } - } + } }
Modified: trunk/core/src/java/org/jbehave/core/story/StoryLoader.java (787 => 788)
--- trunk/core/src/java/org/jbehave/core/story/StoryLoader.java 2007-09-05 10:10:39 UTC (rev 787) +++ trunk/core/src/java/org/jbehave/core/story/StoryLoader.java 2007-09-05 14:28:57 UTC (rev 788) @@ -7,10 +7,12 @@ 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; /** - * StoryLoader parses story details from a resource in the classpath and build a Story via the StoryBuilder. + * StoryLoader parses story details from a resource in the classpath and build a + * Story via the StoryBuilder. * * @author Mauro Talevi * @see StoryBuilder @@ -20,43 +22,71 @@ private ClassLoader classLoader; private StoryParser storyParser; + /** + * Creates a story loader with default story parser and classloader + */ + public StoryLoader() { + this(new TextStoryParser()); + } + + /** + * Creates a story loader with a given story parser and default classloader + * + * @param storyParser the StoryParser + */ + public StoryLoader(StoryParser storyParser) { + this(storyParser, Thread.currentThread().getContextClassLoader()); + } + + /** + * Creates a story loader with a given story parser and classloader + * + * @param storyParser the StoryParser + * @param classLoader the ClassLoader + */ public StoryLoader(StoryParser storyParser, ClassLoader classLoader) { this.classLoader = classLoader; this.storyParser = storyParser; } public StoryDetails loadStoryDetails(String storyPath) { - return storyParser.parseStory(getReader(storyPath, classLoader)); + return storyParser.parseStory(getResourceReader(storyPath, classLoader)); } - + public Story loadStory(String storyPath) { StoryDetails storyDetails = loadStoryDetails(storyPath); return new StoryBuilder(storyDetails, classLoader).story(); } public Story loadStory(Class storyClass) { - return loadStoryClass(storyClass.getName()); + return loadStoryClass(storyClass.getName()); } public Story loadStoryClass(String storyClassName) { try { return (Story) classLoader.loadClass(storyClassName).newInstance(); - } catch ( Exception e) { - throw new InvalidStoryClassException("Failed to load story for class "+storyClassName, e); + } catch (Exception e) { + throw new InvalidStoryClassException("Failed to load story for class " + storyClassName, e); } } - - 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()); + + private Reader getResourceReader(String path, ClassLoader classLoader) { + InputStream is = classLoader.getResourceAsStream(path); + if (is == null) { + throw new NoSuchElementException("Resource " + path + " not found in ClassLoader " + + classLoader.getClass()); } return new InputStreamReader(is); } - + + /** + * Runtime exception thrown when a story class cannot be instantiated + * + * @author Mauro Talevi + */ static class InvalidStoryClassException extends RuntimeException { public InvalidStoryClassException(String message, Exception cause) { super(message, cause); - } + } } }
Modified: trunk/core/src/java/org/jbehave/core/story/StoryRunner.java (787 => 788)
--- trunk/core/src/java/org/jbehave/core/story/StoryRunner.java 2007-09-05 10:10:39 UTC (rev 787) +++ trunk/core/src/java/org/jbehave/core/story/StoryRunner.java 2007-09-05 14:28:57 UTC (rev 788) @@ -1,61 +1,61 @@ /* - * Created on 25-Aug-2004 - * - * (c) 2003-2004 ThoughtWorks Ltd - * - * See license.txt for license details + * Created on 25-Aug-2004 (c) 2003-2004 ThoughtWorks Ltd See license.txt for + * license details */ package org.jbehave.core.story; + +import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.io.PrintStream; import org.jbehave.core.exception.PendingException; import org.jbehave.core.result.Result; +import org.jbehave.core.story.codegen.parser.TextStoryParser; import org.jbehave.core.story.domain.Story; import org.jbehave.core.story.listener.PlainTextScenarioListener; import org.jbehave.core.util.CamelCaseConverter; - - - /** * @author <a href="" PROTECTED]">Dan North</a> * @author Mauro Talevi */ public class StoryRunner { - - private ClassLoader classLoader; + + private StoryLoader storyLoader; private boolean succeeded = true; + + /** + * Creates a story runner with default story loader + */ + public StoryRunner() { + this(new StoryLoader(new TextStoryParser())); + } - public StoryRunner(){ - this(Thread.currentThread().getContextClassLoader()); + /** + * Creates a story runner with a given story loader + * + * @param storyLoader the StoryLoader + */ + public StoryRunner(StoryLoader storyLoader) { + this.storyLoader = storyLoader; } - public StoryRunner(ClassLoader classLoader) { - this.classLoader = classLoader; + public void run(Story story) { + run(story, System.out); } - public void run(String storyClassName, PrintStream printStream) - throws InstantiationException, IllegalAccessException, ClassNotFoundException { - Story story = loadStory(storyClassName, classLoader); + public void run(String storyClassName, OutputStream outputStream) { + Story story = storyLoader.loadStoryClass(storyClassName); story.specify(); - run(story, printStream); + run(story, outputStream); } - - public void run(Story story) { - run(story, System.out); - } - public void run(Story story, PrintStream printStream) { - PlainTextScenarioListener listener = new PlainTextScenarioListener(new OutputStreamWriter(printStream)); + public void run(Story story, OutputStream outputStream) { + PlainTextScenarioListener listener = new PlainTextScenarioListener(new OutputStreamWriter(outputStream)); story.addListener(listener); try { - story.run(); - } catch (PendingException pe) { - listener.gotResult( - new Result("", - new CamelCaseConverter(story.getClass()).toPhrase(), - pe)); + story.run(); + } catch (PendingException e) { + listener.gotResult(new Result("Pending", new CamelCaseConverter(story.getClass()).toPhrase(), e)); } listener.printReport(); succeeded = succeeded && !listener.hasBehaviourFailures(); @@ -64,12 +64,8 @@ private boolean succeeded() { return succeeded; } - - private Story loadStory(String className, ClassLoader classLoader) throws InstantiationException, IllegalAccessException, ClassNotFoundException { - return (Story) classLoader.loadClass(className).newInstance(); - } - - public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { + + public static void main(String[] args) { StoryRunner runner = new StoryRunner(); for (int i = 0; i < args.length; i++) { runner.run(args[i], System.out); @@ -77,8 +73,4 @@ System.exit(runner.succeeded() ? 0 : 1); } - - - } - \ No newline at end of file
To unsubscribe from this list please visit:
