01: package com.mockrunner.test.util;
02:
03: import java.io.ByteArrayInputStream;
04: import java.io.InputStream;
05: import java.io.Reader;
06: import java.io.StringReader;
07: import java.util.List;
08:
09: import junit.framework.TestCase;
10:
11: import com.mockrunner.util.common.StreamUtil;
12:
13: public class StreamUtilTest extends TestCase {
14: public void testGetStreamAsByteArray() throws Exception {
15: String expectedString = "This is a simple test string";
16: byte[] testData = StreamUtil
17: .getStreamAsByteArray(new ByteArrayInputStream(
18: expectedString.getBytes("ISO-8859-1")));
19: String testString = new String(testData);
20: assertEquals(expectedString, testString);
21: testData = StreamUtil.getStreamAsByteArray(
22: new ByteArrayInputStream(expectedString
23: .getBytes("ISO-8859-1")), 3);
24: testString = new String(testData);
25: assertEquals("Thi", testString);
26: }
27:
28: public void testGetReaderAsString() {
29: String expectedString = "This is a simple test string";
30: String testString = StreamUtil
31: .getReaderAsString(new StringReader(expectedString));
32: assertEquals(expectedString, testString);
33: testString = StreamUtil.getReaderAsString(new StringReader(
34: expectedString), 0);
35: assertEquals("", testString);
36: }
37:
38: public void testCompareStreamsAndReader() throws Exception {
39: byte[] sourceArray = new byte[] { 1, 2, 3, 4, 5 };
40: byte[] targetArray = new byte[] { 1, 2, 3, 4, 5 };
41: InputStream sourceStream = new ByteArrayInputStream(sourceArray);
42: assertTrue(StreamUtil.compareStreams(sourceStream,
43: new ByteArrayInputStream(targetArray)));
44: assertTrue(StreamUtil.compareStreams(sourceStream,
45: new ByteArrayInputStream(targetArray)));
46: assertTrue(1 == sourceStream.read());
47: assertFalse(StreamUtil.compareStreams(new ByteArrayInputStream(
48: new byte[] { 1, 2, 3, 4, 6 }),
49: new ByteArrayInputStream(targetArray)));
50: assertFalse(StreamUtil.compareStreams(new ByteArrayInputStream(
51: new byte[0]), new ByteArrayInputStream(targetArray)));
52: String sourceString = "This is a test";
53: String targetString = "This is a test";
54: StringReader sourceReader = new StringReader(sourceString);
55: assertTrue(StreamUtil.compareReaders(sourceReader,
56: new StringReader(targetString)));
57: assertTrue('T' == (char) sourceReader.read());
58: assertTrue(StreamUtil.compareReaders(sourceReader,
59: new StringReader("his is a test")));
60: assertFalse(StreamUtil.compareReaders(sourceReader,
61: new StringReader("This is a test")));
62: assertFalse(StreamUtil.compareReaders(new StringReader(
63: "this is a test"), new StringReader("This is a test")));
64: }
65:
66: public void testCopyStreamAndReader() throws Exception {
67: InputStream sourceStream = new ByteArrayInputStream(new byte[] {
68: 1, 2, 3, 4, 5 });
69: InputStream copyStream = StreamUtil.copyStream(sourceStream);
70: assertTrue(StreamUtil.compareStreams(sourceStream, copyStream));
71: assertEquals(1, copyStream.read());
72: assertFalse(StreamUtil.compareStreams(sourceStream, copyStream));
73: Reader sourceReader = new StringReader("This is a String");
74: Reader copyReader = StreamUtil.copyReader(sourceReader);
75: assertTrue(StreamUtil.compareReaders(sourceReader, copyReader));
76: assertEquals('T', copyReader.read());
77: }
78:
79: public void testGetLinesFromReader() {
80: String testString = "This is line1\nThis is line2\nThis is line3";
81: StringReader reader = new StringReader(testString);
82: List lines = StreamUtil.getLinesFromReader(reader);
83: assertEquals(3, lines.size());
84: assertEquals("This is line1", lines.get(0));
85: assertEquals("This is line2", lines.get(1));
86: assertEquals("This is line3", lines.get(2));
87: }
88: }
|