001: /*
002: * Contact Streams Regression test.
003: * Copyright (C) 2004 Stephen Ostermiller
004: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * See COPYING.TXT for details.
017: */
018: package com.Ostermiller.util;
019:
020: import java.io.*;
021:
022: /**
023: * Regression test for Concatenation Streams.
024: * More information about this class is available from <a target="_top" href=
025: * "http://ostermiller.org/utils/Conact.html">ostermiller.org</a>.
026: *
027: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
028: * @since ostermillerutils 1.04.00
029: */
030: class ConcatTests {
031:
032: /**
033: * Main method for tests
034: * @param args command line arguments (ignored)
035: */
036: public static void main(String[] args) {
037: try {
038: ConcatReader cr = new ConcatReader(new Reader[] {
039: new StringReader("1"), new StringReader("two"),
040: new StringReader(""), new StringReader("4"),
041: new StringReader("five"), new StringReader("six"),
042: new StringReader("seven"), });
043: if (!cr.ready())
044: throw new Exception("Not Ready");
045: read(cr, '1');
046: read(cr, 't');
047: read(cr, 'w');
048: read(cr, 'o');
049: if (!cr.ready())
050: throw new Exception("Not Ready");
051: read(cr, '4');
052: read(cr, "fiv");
053: skip(cr, 2);
054: read(cr, "i");
055: read(cr, "xseven");
056: if (cr.read() != -1)
057: throw new Exception("Read did not terminate");
058: if (cr.read() != -1)
059: throw new Exception("Didn't stay closed");
060: cr.close();
061:
062: final ConcatReader cr1 = new ConcatReader();
063: if (cr1.ready())
064: throw new Exception("Ready");
065: cr1.addReader(new StringReader("one"));
066: read(cr1, 'o');
067: cr1.addReader(new StringReader("two"));
068: read(cr1, "netwo");
069: new Thread() {
070: @Override
071: public void run() {
072: try {
073: Thread.sleep(1000);
074: } catch (InterruptedException ix) {
075: throw new RuntimeException(ix);
076: }
077: cr1.addReader(new StringReader("three"));
078: }
079: }.start();
080: read(cr1, "three");
081: cr1.lastReaderAdded();
082: if (cr1.read() != -1)
083: throw new Exception("Read did not terminate");
084:
085: ConcatInputStream cis = new ConcatInputStream(
086: new InputStream[] {
087: new ByteArrayInputStream(new byte[] { '1' }),
088: new ByteArrayInputStream(new byte[] { 't',
089: 'w', 'o' }),
090: new ByteArrayInputStream(new byte[] {}),
091: new ByteArrayInputStream(new byte[] { '4' }),
092: new ByteArrayInputStream(new byte[] { 'f',
093: 'i', 'v', 'e' }),
094: new ByteArrayInputStream(new byte[] { 's',
095: 'i', 'x' }),
096: new ByteArrayInputStream(new byte[] { 's',
097: 'e', 'v', 'e', 'n' }), });
098: if (cis.available() <= 0)
099: throw new Exception("Not Ready");
100: read(cis, '1');
101: read(cis, 't');
102: read(cis, 'w');
103: read(cis, 'o');
104: read(cis, '4');
105: read(cis, "fivesi");
106: if (cis.available() <= 0)
107: throw new Exception("Not Ready");
108: read(cis, "xseven");
109: if (cis.read() != -1)
110: throw new Exception("Read did not terminate");
111: if (cis.read() != -1)
112: throw new Exception("Didn't stay closed");
113:
114: final ConcatInputStream cis1 = new ConcatInputStream();
115: if (cis.available() != 0)
116: throw new Exception("Ready");
117: cis1.addInputStream(new ByteArrayInputStream("one"
118: .getBytes("ASCII")));
119: read(cis1, 'o');
120: cis1.addInputStream(new ByteArrayInputStream("two"
121: .getBytes("ASCII")));
122: read(cis1, "netwo");
123: new Thread() {
124: @Override
125: public void run() {
126: try {
127: Thread.sleep(1000);
128: } catch (InterruptedException ix) {
129: throw new RuntimeException(ix);
130: }
131: try {
132: cis1.addInputStream(new ByteArrayInputStream(
133: "three".getBytes("ASCII")));
134: } catch (Exception x) {
135: System.err.println(x.getMessage());
136: x.printStackTrace();
137: System.exit(1);
138: }
139: }
140: }.start();
141: read(cis1, "three");
142: cis1.lastInputStreamAdded();
143: if (cis1.read() != -1)
144: throw new Exception("Read did not terminate");
145:
146: } catch (Exception x) {
147: System.err.println(x.getMessage());
148: x.printStackTrace();
149: System.exit(1);
150: }
151: System.exit(0);
152: }
153:
154: private static void skip(Reader in, int n) throws Exception {
155: int s = 0;
156: while (s < n) {
157: s += in.skip(n - s);
158: }
159: }
160:
161: private static void read(Reader in, char expected) throws Exception {
162: int c = in.read();
163: if (c != expected)
164: throw new Exception("Expected to read " + expected
165: + " but read " + (char) c);
166: }
167:
168: private static void read(InputStream in, char expected)
169: throws Exception {
170: int c = in.read();
171: if (c != expected)
172: throw new Exception("Expected to read " + expected
173: + " but read " + (char) c);
174: }
175:
176: private static void read(Reader in, String expected)
177: throws Exception {
178: int totalRead = 0;
179: while (totalRead < expected.length()) {
180: char[] buffer = new char[expected.length() - totalRead];
181: int read = in.read(buffer);
182: if (read == -1)
183: throw new Exception("Read terminated early");
184: if (!expected.substring(totalRead, totalRead + read)
185: .equals(new String(buffer, 0, read))) {
186: throw new Exception("Expected to read "
187: + expected.substring(totalRead, totalRead
188: + read) + " but read "
189: + new String(buffer, 0, read));
190: }
191: totalRead += read;
192: }
193: }
194:
195: private static void read(InputStream in, String expected)
196: throws Exception {
197: int totalRead = 0;
198: while (totalRead < expected.length()) {
199: byte[] buffer = new byte[expected.length() - totalRead];
200: int read = in.read(buffer);
201: if (read == -1)
202: throw new Exception("Read terminated early");
203: if (!expected.substring(totalRead, totalRead + read)
204: .equals(new String(buffer, 0, read, "ASCII"))) {
205: throw new Exception("Expected to read "
206: + expected.substring(totalRead, totalRead
207: + read) + " but read "
208: + new String(buffer, 0, read, "ASCII"));
209: }
210: totalRead += read;
211: }
212: }
213:
214: }
|