001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.util;
004:
005: import java.io.ByteArrayOutputStream;
006: import java.io.PipedInputStream;
007: import java.io.PipedOutputStream;
008:
009: import fitnesse.testutil.AbstractRegex;
010:
011: public class StreamReaderTest extends AbstractRegex {
012: private PipedOutputStream output;
013:
014: StreamReader reader;
015:
016: String readResult;
017:
018: byte[] byteResult;
019:
020: private Thread thread;
021:
022: public void setUp() throws Exception {
023: output = new PipedOutputStream();
024: reader = new StreamReader(new PipedInputStream(output));
025: }
026:
027: public void tearDown() throws Exception {
028: output.close();
029: reader.close();
030: }
031:
032: private void writeToPipe(String value) throws Exception {
033: byte[] bytes = value.getBytes();
034: output.write(bytes);
035: }
036:
037: public void testReadLine() throws Exception {
038: startReading(new ReadLine());
039: writeToPipe("a line\r\n");
040: finishReading();
041: assertEquals("a line", readResult);
042: }
043:
044: public void testReadLineBytes() throws Exception {
045: startReading(new ReadLineBytes());
046: writeToPipe("a line\r\n");
047: finishReading();
048: assertEquals("a line", new String(byteResult));
049: }
050:
051: public void testBufferCanGrow() throws Exception {
052: startReading(new ReadLine());
053: for (int i = 0; i < 1001; i++)
054: writeToPipe(i + ",");
055: writeToPipe("\r\n");
056:
057: finishReading();
058: assertHasRegexp("1000", readResult);
059: }
060:
061: public void testReadNumberOfBytesAsString() throws Exception {
062: startReading(new ReadCount(100));
063: StringBuffer buffer = new StringBuffer();
064: for (int i = 0; i < 100; i++) {
065: buffer.append("*");
066: writeToPipe("*");
067: }
068: finishReading();
069:
070: assertEquals(buffer.toString(), readResult);
071: }
072:
073: public void testReadNumberOfBytes() throws Exception {
074: startReading(new ReadCountBytes(100));
075: StringBuffer buffer = new StringBuffer();
076: for (int i = 0; i < 100; i++) {
077: buffer.append("*");
078: writeToPipe("*");
079: }
080: finishReading();
081:
082: assertEquals(buffer.toString(), new String(byteResult));
083: }
084:
085: public void testReadNumberOfBytesWithClosedInput() throws Exception {
086: startReading(new ReadCountBytes(100));
087:
088: for (int i = 0; i < 50; i++)
089: writeToPipe("*");
090: output.close();
091: finishReading();
092:
093: assertEquals("bytes consumed", 50, reader
094: .numberOfBytesConsumed());
095: assertEquals("bytes returned", 50, byteResult.length);
096: }
097:
098: public void testReadingZeroBytes() throws Exception {
099: startReading(new ReadCount(0));
100: finishReading();
101: assertEquals("", readResult);
102: }
103:
104: public void testReadUpTo() throws Exception {
105: checkReadUoTo("--boundary", "some bytes--boundary",
106: "some bytes");
107: }
108:
109: public void testReadUpToNonEnd() throws Exception {
110: checkReadUoTo("--bound", "some bytes--boundary", "some bytes");
111: }
112:
113: public void testReadBytesUpTo() throws Exception {
114: startReading(new ReadUpToBytes("--boundary"));
115: writeToPipe("some bytes--boundary");
116: finishReading();
117:
118: assertEquals("some bytes", new String(byteResult));
119: }
120:
121: public void testReadUpTo2() throws Exception {
122: checkReadUoTo("--bob", "----bob\r\n", "--");
123: }
124:
125: public void testReadUpTo3() throws Exception {
126: checkReadUoTo("12345", "112123123412345", "1121231234");
127: }
128:
129: private void checkReadUoTo(String boundary, String input,
130: String expected) throws Exception {
131: startReading(new ReadUpTo(boundary));
132: writeToPipe(input);
133: finishReading();
134:
135: assertEquals(expected, readResult);
136: }
137:
138: public void testCopyBytesUpTo() throws Exception {
139: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
140: writeToPipe("some bytes--boundary");
141: reader.copyBytesUpTo("--boundary", outputStream);
142: assertEquals("some bytes", outputStream.toString());
143: }
144:
145: public void testEofReadCount() throws Exception {
146: writeToPipe("abcdefghijklmnopqrstuvwxyz");
147: output.close();
148: assertFalse(reader.isEof());
149: reader.read(10);
150: assertFalse(reader.isEof());
151: reader.read(16);
152: assertFalse(reader.isEof());
153: reader.read(1);
154: assertTrue(reader.isEof());
155: }
156:
157: public void testEofReadLine() throws Exception {
158: writeToPipe("one line\ntwo lines\nthree lines");
159: output.close();
160: assertFalse(reader.isEof());
161: reader.readLine();
162: assertFalse(reader.isEof());
163: reader.readLine();
164: assertFalse(reader.isEof());
165: reader.readLine();
166: assertTrue(reader.isEof());
167: }
168:
169: public void testEofReadUpTo() throws Exception {
170: writeToPipe("mark one, mark two, the end");
171: output.close();
172: assertFalse(reader.isEof());
173: reader.readUpTo("one");
174: assertFalse(reader.isEof());
175: reader.readUpTo("two");
176: assertFalse(reader.isEof());
177: reader.readUpTo("three");
178: assertTrue(reader.isEof());
179: }
180:
181: public void testBytesConsumed() throws Exception {
182: writeToPipe("One line\r\n12345abc-boundary");
183: assertEquals(0, reader.numberOfBytesConsumed());
184:
185: reader.readLine();
186: assertEquals(10, reader.numberOfBytesConsumed());
187:
188: reader.read(5);
189: assertEquals(15, reader.numberOfBytesConsumed());
190:
191: reader.readUpTo("-boundary");
192: assertEquals(27, reader.numberOfBytesConsumed());
193: }
194:
195: public void testEarlyClosingStream() throws Exception {
196: startReading(new ReadCount(10));
197: output.close();
198: finishReading();
199: assertEquals("", readResult);
200: }
201:
202: private void startReading(ReadThread thread) {
203: this .thread = thread;
204: this .thread.start();
205: }
206:
207: private void finishReading() throws Exception {
208: thread.join();
209: }
210:
211: abstract class ReadThread extends Thread {
212: public void run() {
213: try {
214: doRead();
215: } catch (Exception e) {
216: }
217: }
218:
219: public abstract void doRead() throws Exception;
220: }
221:
222: class ReadLine extends ReadThread {
223: public void doRead() throws Exception {
224: readResult = reader.readLine();
225: }
226: }
227:
228: class ReadCount extends ReadThread {
229: private int amount;
230:
231: public ReadCount(int amount) {
232: this .amount = amount;
233: }
234:
235: public void doRead() throws Exception {
236: readResult = reader.read(amount);
237: }
238: }
239:
240: class ReadUpTo extends ReadThread {
241: private String boundary;
242:
243: public ReadUpTo(String b) {
244: boundary = b;
245: }
246:
247: public void doRead() throws Exception {
248: readResult = reader.readUpTo(boundary);
249: }
250: }
251:
252: class ReadLineBytes extends ReadThread {
253: public void doRead() throws Exception {
254: byteResult = reader.readLineBytes();
255: }
256: }
257:
258: class ReadCountBytes extends ReadThread {
259: private int amount;
260:
261: public ReadCountBytes(int amount) {
262: this .amount = amount;
263: }
264:
265: public void doRead() throws Exception {
266: byteResult = reader.readBytes(amount);
267: }
268: }
269:
270: class ReadUpToBytes extends ReadThread {
271: private String boundary;
272:
273: public ReadUpToBytes(String b) {
274: boundary = b;
275: }
276:
277: public void doRead() throws Exception {
278: byteResult = reader.readBytesUpTo(boundary);
279: }
280: }
281:
282: }
|