From: Francois B. <fbe...@ft...> - 2002-08-22 13:02:14
|
Hi guys ! Just found a problem with the MockWriter as committed by Steve. I had the problem yesterday, but did not have the energy to find why. Anyway, the diff below corrects this problem. Check the comments in the no-arg constructor for details. I also implemented two new functions: close() and flush() will cause IOExceptions if requested to do so. Have a nice day ! -- Francois Beausoleil (fra...@ya...) Developper of Java Gui Builder - http://jgb.sourceforge.net/ Index: src/jdk/common/com/mockobjects/io/MockWriter.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/jdk/common/com/mockobjects/io/MockWriter.java,v retrieving revision 1.2 diff -u -r1.2 MockWriter.java --- src/jdk/common/com/mockobjects/io/MockWriter.java 22 Aug 2002 09:48:14 -0000 1.2 +++ src/jdk/common/com/mockobjects/io/MockWriter.java 22 Aug 2002 12:57:52 -0000 @@ -36,6 +36,8 @@ private ExpectationCounter closeCallsCount = new ExpectationCounter("close calls"); private boolean writeShouldThrowException = false; + private boolean flushShouldThrowException = false; + private boolean closeShouldThrowException = false; /** * Instantiates a new mock writer which will act as a data sink. @@ -44,7 +46,11 @@ * the provided methods. */ public MockWriter() { - super(); + // NOTE: We *have* to use the Writer(Object lock) constructor because + // Writer() will use this as a lock. When we call + // Verifier.verifyObject(MockWriter), it will find the lock field, + // which is Verifiable, resulting in an infinite recursion loop... + super(new Object()); } /** @@ -58,6 +64,26 @@ } /** + * Sets the mock's behavior when flushing. + * If this method has been called, then + * {@link #flush() flush()} will throw an + * {@link java.io.IOException IOException}. + */ + public void setFlushShouldThrowException() { + flushShouldThrowException = true; + } + + /** + * Sets the mock's behavior when closing. + * If this method has been called, then + * {@link #close() close()} will throw an + * {@link java.io.IOException IOException}. + */ + public void setCloseShouldThrowException() { + closeShouldThrowException = true; + } + + /** * Sets the expected number of times that the {@link #flush() flush()} * method will be called. * @see #flush() @@ -104,22 +130,45 @@ /** * Increments the flush counter and asserts that this method was not * called too many times. + * This method will also throw an {@link java.io.IOException IOException} + * if asked to do so by calling + * {@link #setFlushShouldThrowException() setFlushShouldThrowException()}. + * Please note that the call count will be incremented <em>before</em> the + * check for the exception is done. * @see #setExpectedFlushCalls(int) + * @see #setFlushShouldThrowException() */ public void flush() throws IOException { flushCallsCount.inc(); + + if (flushShouldThrowException) { + throw new IOException("Mock Exception"); + } } /** * Increments the close counter and asserts that this method was not * called too many times. + * This method will also throw an {@link java.io.IOException IOException} + * if asked to do so by calling + * {@link #setCloseShouldThrowException() setCloseShouldThrowException()}. + * Please note that the call count will be incremented <em>before</em> the + * check for the exception is done. * @see #setExpectedCloseCalls(int) + * @see #setCloseShouldThrowException() */ - public void close() { + public void close() throws IOException { closeCallsCount.inc(); + + if (closeShouldThrowException) { + throw new IOException("Mock Exception"); + } } public void verify() { + // WARNING: If the MockWriter calls it's no-arg parent constructor, + // this call will fail with a StackOverflowError. See constructor + // for details. Verifier.verifyObject(this); } } |