Update of /cvsroot/mockobjects/mockobjects-java/src/jdk/common/com/mockobjects/io
In directory usw-pr-cvs1:/tmp/cvs-serv17107/src/jdk/common/com/mockobjects/io
Added Files:
MockWriter.java
Log Message:
new class
--- NEW FILE: MockWriter.java ---
package com.mockobjects.io;
import com.mockobjects.ExpectationCounter;
import com.mockobjects.ExpectationSegment;
import com.mockobjects.Verifiable;
import com.mockobjects.util.Verifier;
import java.io.IOException;
import java.io.Writer;
/**
* A mock {@link java.io.Writer Writer}.
* <h3>Example usage</h3>
* <p>
* You may use the <code>MockWriter</code> like this:
* <pre>
* public void testSomething() throws IOException {
* MockWriter out = new MockWriter();
* out.setExpectedSegment("some string");
* out.setExpectedFlushCalls(1);
* out.setExpectedCloseCalls(1);
*
* ObjectUnderTest testee = new ObjectUnderTest(out);
* out.verify();
*
* // If we get here, the mock's flush() and close() methods were
* // called exactly once each (order cannot be determined) and
* // the write() method was called with the string "some string" in it.
* }</pre>
* </p>
* @author Francois Beausoleil, fb...@us...
*/
public class MockWriter extends Writer implements Verifiable {
private ExpectationSegment mySegment = new ExpectationSegment("String segment");
private ExpectationCounter myFlushCalls = new ExpectationCounter("flush calls");
private ExpectationCounter myCloseCalls = new ExpectationCounter("close calls");
private boolean writeShouldThrowException = false;
/**
* Instantiates a new mock writer which will act as a data sink.
* Once instantiated, mocks of this class do not expect anything special.
* Once the object is instantiated, you should set your expectations using
* the provided methods.
*/
public MockWriter() {
}
/**
* Sets the mocks behavior when writing.
* When the {@link #write(char[],int,int) write(char[], int, int)} method
* is called, if this method was called with <code>true</code>, the mock
* will throw an {@link java.io.IOException IOException} instead of
* asserting a string segment.
* @param state Set to <code>true</code> if you want the mock to throw an
* exception, <code>false</code> otherwise.
*/
public void setWriteShouldThrowException(boolean state) {
writeShouldThrowException = state;
}
/**
* Sets the expected number of times that the {@link #flush() flush()}
* method will be called.
* @see #flush()
*/
public void setExpectedFlushCalls(int calls) {
myFlushCalls.setExpected(calls);
}
/**
* Sets the expected number of times that the {@link #close() close()}
* method will be called.
* @see #close()
*/
public void setExpectedCloseCalls(int calls) {
myCloseCalls.setExpected(calls);
}
/**
* Sets the value of the expected string segment.
* When the {@link #write(char[], int, int) write(char[], int, int)} method
* is called, a string is instantiated with the passed array and compared
* to the <code>aString</code> parameter of this method. If the two strings
* differ, an {@link junit.framework.AssertionFailedError} will be thrown.
* @see com.mockobjects.ExpectationSegment
* @see #write(char[], int, int)
*/
public void setExpectedSegment(String aString) {
mySegment.setExpected(aString);
}
/**
* Either throws an exception or asserts a string segment for equality.
* @see com.mockobjects.ExpectationSegment
* @see #setWriteShouldThrowException(boolean)
*/
public void write(char cbuf[], int off, int len) throws IOException {
if (writeShouldThrowException) {
throw new IOException("Exception, as requested");
}
mySegment.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 {
myFlushCalls.inc();
}
/**
* Increments the close counter and asserts that this method was not
* called too many times.
* @see #setExpectedCloseCalls(int)
*/
public void close() {
myCloseCalls.inc();
}
public void verify() {
mySegment.verify();
myFlushCalls.verify();
myCloseCalls.verify();
}
}
|