- Revision
- 792
- Author
- mauro
- Date
- 2007-09-06 08:02:08 -0500 (Thu, 06 Sep 2007)
Log Message
Deprecated BehaviourRunner ctors with OutputStream in favour of Writer. Renamed behaviour class to reflect name of class.
Modified Paths
Added Paths
Removed Paths
Diff
Copied: trunk/core/src/behaviour/org/jbehave/core/BehaviourRunnerBehaviour.java (from rev 789, trunk/core/src/behaviour/org/jbehave/core/RunBehaviour.java) (0 => 792)
--- trunk/core/src/behaviour/org/jbehave/core/BehaviourRunnerBehaviour.java (rev 0) +++ trunk/core/src/behaviour/org/jbehave/core/BehaviourRunnerBehaviour.java 2007-09-06 13:02:08 UTC (rev 792) @@ -0,0 +1,111 @@ +package org.jbehave.core; + +import java.io.IOException; +import java.io.Writer; + +import org.jbehave.core.mock.UsingMatchers; + +/** + * @author <a href="" PROTECTED]">Elizabeth Keogh</a> + * @author Mauro Talevi + */ +public class BehaviourRunnerBehaviour extends UsingMatchers { + + private static int runCount; + private static String methodCalled; + + public void shouldRunClassFromArgumentSuccessfully() { + runCount = 0; + new BehaviourRunner(new NullWriter()).verifyBehaviour(RunnableBehaviour.class); + Ensure.that(runCount == 1); + } + + public void shouldRunTwoClassesFromArgumentsSuccessfully() { + runCount = 0; + BehaviourRunner run = new BehaviourRunner(new NullWriter()); + run.verifyBehaviour(RunnableBehaviour.class); + run.verifyBehaviour(RunnableBehaviour.class); + Ensure.that(runCount == 2); + } + + public void shouldRunClassNameFromArgumentSuccessfully() throws ClassNotFoundException { + runCount = 0; + new BehaviourRunner(new NullWriter()).verifyBehaviour(RunnableBehaviour.class.getName()); + Ensure.that(runCount == 1); + } + + public void shouldRunClassNamePlusOneMethodSuccessfully() throws ClassNotFoundException { + runCount = 0; + BehaviourRunner run = new BehaviourRunner(new NullWriter()); + run.verifyBehaviour(RunnableBehaviourWithTwoMethods.class.getName() + ":" + "shouldRunNumberOne"); + ensureThat(run.succeeded(), eq(true)); + ensureThat(runCount, eq(1)); + ensureThat(methodCalled, eq("shouldRunNumberOne")); + } + + public void shouldRunClassNameUsingCustomClassLoader() throws ClassNotFoundException { + runCount = 0; + new BehaviourRunner(new NullWriter(), new CustomClassLoader(false)) + .verifyBehaviour(RunnableBehaviour.class.getName()); + Ensure.that(runCount == 1); + } + + public void shouldFailToRunClassNameUsingInvalidClassLoader() throws ClassNotFoundException { + try { + new BehaviourRunner(new NullWriter(), new CustomClassLoader(true)).verifyBehaviour(RunnableBehaviour.class + .getName()); + } catch (ClassNotFoundException e) { + Ensure.that(runCount == 0); + } + } + + public static class RunnableBehaviour { + public void shouldBeRunByRunClass() { + runCount++; + } + } + + public static class RunnableBehaviourWithTwoMethods { + public void shouldRunNumberOne() { + runCount++; + methodCalled = "shouldRunNumberOne"; + } + + public void shouldRunNumberTwo() { + runCount++; + methodCalled = "shouldRunNumberTwo"; + } + } + + + private static class NullWriter extends Writer { + public void close() throws IOException { + // no-op + } + + public void flush() throws IOException { + // no-op + } + + public void write(char[] cbuf, int off, int len) throws IOException { + // no-op + } + }; + + + private static class CustomClassLoader extends ClassLoader { + + boolean invalid; + + public CustomClassLoader(boolean invalid) { + this.invalid = invalid; + } + + public Class findClass(String name) throws ClassNotFoundException { + if (invalid) { + throw new ClassNotFoundException(); + } + return BehaviourRunner.class.getClassLoader().loadClass(name); + } + } +}
Deleted: trunk/core/src/behaviour/org/jbehave/core/RunBehaviour.java (791 => 792)
--- trunk/core/src/behaviour/org/jbehave/core/RunBehaviour.java 2007-09-06 11:23:13 UTC (rev 791) +++ trunk/core/src/behaviour/org/jbehave/core/RunBehaviour.java 2007-09-06 13:02:08 UTC (rev 792) @@ -1,103 +0,0 @@ -package org.jbehave.core; - - - -import java.io.OutputStream; -import java.io.PrintStream; - -import org.jbehave.core.mock.UsingMatchers; - - -/** - * @author <a href="" PROTECTED]">Elizabeth Keogh</a> - * @author Mauro Talevi - */ -public class RunBehaviour extends UsingMatchers { - - private static int runCount; - private static String methodCalled; - - public static class RunnableBehaviour { - public void shouldBeRunByRunClass() { - runCount++; - } - } - - public static class RunnableBehaviourWithTwoMethods { - public void shouldRunNumberOne() { - runCount ++; - methodCalled = "shouldRunNumberOne"; - } - - public void shouldRunNumberTwo() { - runCount ++; - methodCalled = "shouldRunNumberTwo"; - } - } - - private PrintStream nullPrintStream = new PrintStream(new OutputStream() { - public void write(int b) { - // do nothing - } - }); - - public void shouldRunClassFromArgumentSuccessfully() { - runCount = 0; - new BehaviourRunner(nullPrintStream).verifyBehaviour(RunnableBehaviour.class); - Ensure.that(runCount == 1); - } - - public void shouldRunTwoClassesFromArgumentsSuccessfully() { - runCount = 0; - BehaviourRunner run = new BehaviourRunner(nullPrintStream); - run.verifyBehaviour(RunnableBehaviour.class); - run.verifyBehaviour(RunnableBehaviour.class); - Ensure.that(runCount == 2); - } - - public void shouldRunClassNameFromArgumentSuccessfully() throws ClassNotFoundException { - runCount = 0; - new BehaviourRunner(nullPrintStream).verifyBehaviour(RunnableBehaviour.class.getName()); - Ensure.that(runCount == 1); - } - - public void shouldRunClassNamePlusOneMethodSuccessfully() throws ClassNotFoundException { - runCount = 0; - BehaviourRunner run = new BehaviourRunner(nullPrintStream); - run.verifyBehaviour(RunnableBehaviourWithTwoMethods.class.getName() + ":" + "shouldRunNumberOne"); - ensureThat(run.succeeded(), eq(true)); - ensureThat(runCount, eq(1)); - ensureThat(methodCalled, eq("shouldRunNumberOne")); - } - - public void shouldRunClassNameUsingCustomClassLoader() throws ClassNotFoundException { - runCount = 0; - new BehaviourRunner(nullPrintStream, new CustomClassLoader(false)).verifyBehaviour(RunnableBehaviour.class.getName()); - Ensure.that(runCount == 1); - } - - public void shouldFailToRunClassNameUsingInvalidClassLoader() throws ClassNotFoundException { - try { - new BehaviourRunner(nullPrintStream, new CustomClassLoader(true)) - .verifyBehaviour(RunnableBehaviour.class.getName()); - } catch (ClassNotFoundException e) { - Ensure.that(runCount == 0); - } - } - - private static class CustomClassLoader extends ClassLoader { - - boolean invalid; - - public CustomClassLoader(boolean invalid) { - this.invalid = invalid; - } - - public Class findClass(String name) throws ClassNotFoundException{ - if ( invalid ){ - throw new ClassNotFoundException(); - } - return BehaviourRunner.class.getClassLoader().loadClass(name); - } - } -}
Modified: trunk/core/src/java/org/jbehave/core/BehaviourRunner.java (791 => 792)
--- trunk/core/src/java/org/jbehave/core/BehaviourRunner.java 2007-09-06 11:23:13 UTC (rev 791) +++ trunk/core/src/java/org/jbehave/core/BehaviourRunner.java 2007-09-06 13:02:08 UTC (rev 792) @@ -1,33 +1,54 @@ package org.jbehave.core; -import java.io.PrintStream; +import java.io.OutputStream; import java.io.PrintWriter; +import java.io.Writer; import org.jbehave.core.behaviour.BehaviourClass; import org.jbehave.core.behaviour.BehaviourVerifier; +import org.jbehave.core.listener.BehaviourListener; import org.jbehave.core.listener.PlainTextListener; import org.jbehave.core.util.Timer; - /** - * This is the entry point to run one or more [EMAIL PROTECTED] BehaviourClass}. - * Classes are loaded using the ClassLoader injected, which defaults to the current context - * ClassLoader. + * This is the entry point to verify one or more [EMAIL PROTECTED] BehaviourClass}. Classes + * are loaded using the ClassLoader injected, which defaults to the current + * context ClassLoader. * * @author <a href="" PROTECTED]">Dan North</a> * @author Mauro Talevi + * @see BehaviourListener */ public class BehaviourRunner { + private static final String EMPTY = ""; private boolean succeeded = true; - private final PrintWriter writer; - private ClassLoader classLoader; + private final Writer writer; + private final ClassLoader classLoader; - public BehaviourRunner(PrintStream out) { - this(out, Thread.currentThread().getContextClassLoader()); + public BehaviourRunner() { + this(new PrintWriter(System.out)); } - - public BehaviourRunner(PrintStream out, ClassLoader classLoader) { - this.writer = new PrintWriter(out); + + /** + * @deprecated Use BehaviourRunner(new PrintWriter(OutputStream)) + */ + public BehaviourRunner(OutputStream out) { + this(new PrintWriter(out), Thread.currentThread().getContextClassLoader()); + } + + /** + * @deprecated Use BehaviourRunner(new PrintWriter(OutputStream), ClassLoader) + */ + public BehaviourRunner(OutputStream out, ClassLoader classLoader) { + this(new PrintWriter(out), classLoader); + } + + public BehaviourRunner(Writer writer) { + this(writer, Thread.currentThread().getContextClassLoader()); + } + + public BehaviourRunner(Writer writer, ClassLoader classLoader) { + this.writer = writer; this.classLoader = classLoader; } @@ -37,7 +58,7 @@ public void verifyBehaviour(String behaviourLocator) throws ClassNotFoundException { String className = behaviourLocator; - String methodName = ""; + String methodName = EMPTY; int index = behaviourLocator.indexOf(':'); if (index >= 0) { className = behaviourLocator.substring(0, index); @@ -45,31 +66,30 @@ } verifyBehaviour(classLoader.loadClass(className), methodName); } - + public void verifyBehaviour(Class classToVerify) { - verifyBehaviour(classToVerify, ""); + verifyBehaviour(classToVerify, EMPTY); } public void verifyBehaviour(Class classToVerify, String methodName) { - PlainTextListener textListener = new PlainTextListener(new PrintWriter(writer), new Timer()); - BehaviourVerifier verifier = new BehaviourVerifier(textListener); + BehaviourListener listener = new PlainTextListener(writer, new Timer()); + BehaviourVerifier verifier = new BehaviourVerifier(listener); verifier.verifyBehaviour(new BehaviourClass(classToVerify, methodName, verifier)); - textListener.printReport(); - succeeded = succeeded && !textListener.hasBehaviourFailures(); + listener.printReport(); + succeeded = succeeded && !listener.hasBehaviourFailures(); } public static void main(String[] args) { try { - BehaviourRunner run = new BehaviourRunner(System.out); + BehaviourRunner runner = new BehaviourRunner(); for (int i = 0; i < args.length; i++) { - run.verifyBehaviour(args[i]); + runner.verifyBehaviour(args[i]); } - System.exit(run.succeeded() ? 0 : 1); + System.exit(runner.succeeded() ? 0 : 1); } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(1); } } - }
To unsubscribe from this list please visit:
