KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > io > MockWriter


1 package com.mockobjects.io;
2
3 import com.mockobjects.ExpectationCounter;
4 import com.mockobjects.ExpectationSegment;
5 import com.mockobjects.Verifiable;
6 import com.mockobjects.util.Verifier;
7
8 import java.io.IOException JavaDoc;
9 import java.io.Writer JavaDoc;
10
11 /**
12   * A mock {@link java.io.Writer Writer}.
13   * <h3>Example usage</h3>
14   * <p>
15   * You may use the <code>MockWriter</code> like this:
16   * <pre>
17   * public void testSomething() throws IOException {
18   * MockWriter out = new MockWriter();
19   * out.setExpectedSegment("some string");
20   * out.setExpectedFlushCalls(1);
21   * out.setExpectedCloseCalls(1);
22   *
23   * ObjectUnderTest testee = new ObjectUnderTest(out);
24   * out.verify();
25   *
26   * // If we get here, the mock's flush() and close() methods were
27   * // called exactly once each (order cannot be determined) and
28   * // the write() method was called with the string "some string" in it.
29   * }</pre>
30   * </p>
31   * @author Francois Beausoleil, fbos@users.sourceforge.net
32   */

33 public class MockWriter extends Writer JavaDoc implements Verifiable {
34     private ExpectationSegment segment =
35         new ExpectationSegment("String segment");
36     private ExpectationCounter flushCallsCount =
37         new ExpectationCounter("flush calls");
38     private ExpectationCounter closeCallsCount =
39         new ExpectationCounter("close calls");
40
41     private boolean writeShouldThrowException = false;
42     private boolean flushShouldThrowException = false;
43     private boolean closeShouldThrowException = false;
44
45     /**
46      * Instantiates a new mock writer which will act as a data sink.
47      * Once instantiated, mocks of this class do not expect anything special.
48      * Once the object is instantiated, you should set your expectations using
49      * the provided methods.
50      */

51     public MockWriter() {
52         // NOTE: We *have* to use the Writer(Object lock) constructor because
53
// Writer() will use this as a lock. When we call
54
// Verifier.verifyObject(MockWriter), it will find the lock field,
55
// which is Verifiable, resulting in an infinite recursion loop...
56
super(new Object JavaDoc());
57     }
58
59     /**
60      * Sets the mock's behavior when writing.
61      * If this method has been called, then
62      * {@link #write(char[],int,int) write(char[], int, int)} will throw an
63      * {@link java.io.IOException IOException}.
64      */

65     public void setWriteShouldThrowException() {
66         writeShouldThrowException = true;
67     }
68
69     /**
70      * Sets the mock's behavior when flushing. If this method has been called,
71      * then {@link #flush() flush()} will throw
72      * an {@link java.io.IOException IOException}.
73      */

74     public void setFlushShouldThrowException() {
75         flushShouldThrowException = true;
76     }
77
78     /**
79      * Sets the mock's behavior when closing.
80      * If this method has been called, then {@link #close() close()} will
81      * throw an {@link java.io.IOException IOException}.
82      */

83     public void setCloseShouldThrowException() {
84         closeShouldThrowException = true;
85     }
86     
87     /**
88      * Sets the expected number of times that the {@link #flush() flush()}
89      * method will be called.
90      * @see #flush()
91      */

92     public void setExpectedFlushCalls(int calls) {
93         flushCallsCount.setExpected(calls);
94     }
95
96     /**
97      * Sets the expected number of times that the {@link #close() close()}
98      * method will be called.
99      * @see #close()
100      */

101     public void setExpectedCloseCalls(int calls) {
102         closeCallsCount.setExpected(calls);
103     }
104
105     /**
106      * Sets the value of the expected string segment.
107      * When the {@link #write(char[], int, int) write(char[], int, int)} method
108      * is called, a string is instantiated with the passed array and compared
109      * to the <code>aString</code> parameter of this method. If the two strings
110      * differ, an {@link junit.framework.AssertionFailedError} will be thrown.
111      * @see com.mockobjects.ExpectationSegment
112      * @see #write(char[], int, int)
113      */

114     public void setExpectedSegment(String JavaDoc aString) {
115         segment.setExpected(aString);
116     }
117
118     /**
119      * Either throws an exception or asserts a string segment for equality.
120      * @see com.mockobjects.ExpectationSegment
121      * @see #setWriteShouldThrowException(boolean)
122      */

123     public void write(char cbuf[], int off, int len) throws IOException JavaDoc {
124         if (writeShouldThrowException) {
125             throw new IOException JavaDoc("Mock Exception");
126         }
127
128         segment.setActual(new String JavaDoc(cbuf, off, len));
129     }
130
131     /**
132      * This method will also throw an {@link java.io.IOException IOException}
133      * if asked to do so by calling
134      * {@link #setFlushShouldThrowException() setFlushShouldThrowException()}.
135      * Please note that the call count will be incremented <em>before</em> the
136      * check for the exception is done.
137      * @see #setExpectedFlushCalls(int)
138      * @see #setFlushShouldThrowException()
139      */

140     public void flush() throws IOException JavaDoc {
141         flushCallsCount.inc();
142     
143         if (flushShouldThrowException) {
144             throw new IOException JavaDoc("Mock Exception");
145         }
146     }
147
148     /**
149      * Increments the close counter and asserts that this method was not
150      * called too many times.
151      * This method will also throw an {@link java.io.IOException IOException}
152      * if asked to do so by calling
153      * {@link #setCloseShouldThrowException() setCloseShouldThrowException()}.
154      * Please note that the call count will be incremented <em>before</em> the
155      * check for the exception is done.
156      * @see #setExpectedCloseCalls(int)
157      * @see #setCloseShouldThrowException()
158      */

159     public void close() throws IOException JavaDoc {
160         closeCallsCount.inc();
161     
162         if (closeShouldThrowException) {
163             throw new IOException JavaDoc("Mock Exception");
164         }
165     }
166     
167     public void verify() {
168         // WARNING: If the MockWriter calls its parent no-arg constructor,
169
// this call will fail with a StackOverflowError. See constructor
170
// for details.
171
Verifier.verifyObject(this);
172     }
173 }
174
Popular Tags