From: Steve F. <sm...@us...> - 2002-08-22 13:21:58
|
Update of /cvsroot/mockobjects/mockobjects-java/src/jdk/common/com/mockobjects/io In directory usw-pr-cvs1:/tmp/cvs-serv22461/src/jdk/common/com/mockobjects/io Modified Files: MockWriter.java Log Message: added new exception throwing methods fixed infinite loop in verify Index: MockWriter.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/jdk/common/com/mockobjects/io/MockWriter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- MockWriter.java 22 Aug 2002 09:48:14 -0000 1.2 +++ MockWriter.java 22 Aug 2002 13:21:50 -0000 1.3 @@ -31,11 +31,16 @@ * @author Francois Beausoleil, fb...@us... */ public class MockWriter extends Writer implements Verifiable { - private ExpectationSegment segment = new ExpectationSegment("String segment"); - private ExpectationCounter flushCallsCount = new ExpectationCounter("flush calls"); - private ExpectationCounter closeCallsCount = new ExpectationCounter("close calls"); + private ExpectationSegment segment = + new ExpectationSegment("String segment"); + private ExpectationCounter flushCallsCount = + new ExpectationCounter("flush calls"); + 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,20 +49,42 @@ * 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()); } /** * Sets the mock's behavior when writing. - * If this method has been called, then - * {@link #write(char[],int,int) write(char[], int, int)} will throw an - * {@link java.io.IOException IOException}. + * If this method has been called, then + * {@link #write(char[],int,int) write(char[], int, int)} will throw an + * {@link java.io.IOException IOException}. */ public void setWriteShouldThrowException() { writeShouldThrowException = true; } /** + * 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() @@ -101,25 +128,46 @@ segment.setActual(new String(cbuf, off, len)); } - /** - * Increments the flush counter and asserts that this method was not - * called too many times. - * @see #setExpectedFlushCalls(int) - */ - public void flush() throws IOException { - flushCallsCount.inc(); - } - - /** - * Increments the close counter and asserts that this method was not - * called too many times. - * @see #setExpectedCloseCalls(int) - */ - public void close() { - closeCallsCount.inc(); - } - + /** + * 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() throws IOException { + closeCallsCount.inc(); + + if (closeShouldThrowException) { + throw new IOException("Mock Exception"); + } + } + public void verify() { - Verifier.verifyObject(this); + // WARNING: If the MockWriter calls its parent no-arg constructor, + // this call will fail with a StackOverflowError. See constructor + // for details. + Verifier.verifyObject(this); } } |