001: package com.quadcap.io;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.*;
042: import com.quadcap.util.*;
043:
044: /**
045: * Test cases for various io classes.
046: *
047: * @author Stan Bailes
048: */
049: public class Test extends com.quadcap.util.Test {
050: void testDot(String s) throws IOException {
051: ByteArrayOutputStream b1 = new ByteArrayOutputStream();
052: DotStuffOutputStream os = new DotStuffOutputStream(b1);
053: os.write(s.getBytes());
054: os.close();
055: String dotrep = b1.toString();
056:
057: ByteArrayInputStream b2 = new ByteArrayInputStream(dotrep
058: .getBytes());
059: DotStuffInputStream is = new DotStuffInputStream(b2);
060: ByteArrayOutputStream b3 = new ByteArrayOutputStream();
061: int c;
062: while ((c = is.read()) >= 0) {
063: b3.write(c);
064: }
065: String act = b3.toString();
066: testAssert(s.equals(act));
067: }
068:
069: public void testDotStuffStreams(String[] args) throws IOException {
070: testDot("a\r\n.b\n");
071: testDot(".\r\n");
072: testDot("hello\r\n.goodbye\r\nlala\r\n\r\n..\r\n\r\n..ar\r\nfoo\r\n.\r\n");
073:
074: testDot("");
075: testDot(".");
076: testDot(".\n");
077: testDot("\n");
078: testDot("\r\n");
079: // XXX This test can't be made to work if we want DotStuffInputStream
080: // XXX to be able to handle non-compliant newline-only implementations.
081: // XXX pffhht.
082: // testDot("\n.\n.\n.\n.");
083: }
084:
085: public void testLimitedOutputStream(String[] args) {
086: ByteArrayOutputStream bos = new ByteArrayOutputStream();
087: ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
088: LimitedOutputStream los = new LimitedOutputStream(bos, 25);
089: boolean hitLimit = false;
090: try {
091: for (int i = 0; i < 10; i++) {
092: bos2.write(Util.bytes(" " + i));
093: los.write(Util.bytes(" " + i));
094: }
095: } catch (IOException e) {
096: if (e instanceof LimitExceededException) {
097: hitLimit = true;
098: String b1 = bos.toString();
099: if (b1.length() != 25) {
100: testError("wrong length: " + b1.length());
101: }
102: String b2 = bos2.toString().substring(0, 25);
103: if (!b1.equals(b2)) {
104: testError("'" + b1 + "' != '" + b2 + "'");
105: }
106: } else {
107: testError("wrong exception: " + e.toString());
108: Debug.print(e);
109: }
110: }
111: if (!hitLimit) {
112: testError("no exception");
113: }
114:
115: }
116:
117: public void testNullOutputStream(String[] args) {
118: for (int i = 0; i < 10; i++) {
119: NullOutputStream.Null.println(" " + i);
120: }
121: }
122:
123: public void testBase64(String[] args) throws IOException {
124: for (int i = 0; i < 5; i++) {
125: SaveRestoreStream sr = new SaveRestoreStream();
126: OutputStream out = sr.getOutputStream();
127: Base64OutputStream bout = new Base64OutputStream(out);
128: for (int j = 0; j < i; j++) {
129: bout.write(j + 16);
130: }
131: bout.finish();
132: out.close();
133: InputStream is = sr.getInputStream();
134: Base64InputStream bin = new Base64InputStream(is);
135: for (int j = 0; j <= i; j++) {
136: writer.println("" + i + ": " + j + ": " + bin.read());
137: }
138: bin.close();
139: sr.close();
140: }
141: }
142:
143: public void testSaveRestore(String[] args) throws IOException {
144: for (int size = 1; size < (1 << 16); size <<= 1) {
145: SaveRestoreStream so = new SaveRestoreStream(1000);
146: OutputStream os = so.getOutputStream();
147: for (int i = 0; i < size; i += 4) {
148: os.write((i >> 24) & 0xff);
149: os.write((i >> 16) & 0xff);
150: os.write((i >> 8) & 0xff);
151: os.write(i & 0xff);
152: }
153: InputStream is = so.getInputStream();
154: for (int i = 0; i < size; i += 4) {
155: int t = is.read();
156: t <<= 8;
157: t |= (is.read() & 0xff);
158: t <<= 8;
159: t |= (is.read() & 0xff);
160: t <<= 8;
161: t |= (is.read() & 0xff);
162: testAssert(t == i);
163: }
164: }
165: }
166:
167: public void testRecursiveFileIterator(String[] args)
168: throws Exception {
169: FileFilter filter = new FileFilter() {
170: public boolean accept(File f) {
171: if (f.isDirectory()) {
172: return !f.getName().equals("CVS");
173: } else {
174: return (f.getName().endsWith(".java"));
175: }
176: }
177: };
178: RecursiveFileIterator f = new RecursiveFileIterator(new File(
179: "."), filter);
180: while (f.hasNext()) {
181: File fil = (File) f.next();
182: writer.println(fil.getName());
183: }
184: }
185:
186: public static void main(String args[]) {
187: Test t = new Test();
188: t.test(args);
189: }
190:
191: }
|