KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > io > Test


1 package com.quadcap.io;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.*;
42 import com.quadcap.util.*;
43
44 /**
45  * Test cases for various io classes.
46  *
47  * @author Stan Bailes
48  */

49 public class Test extends com.quadcap.util.Test {
50     void testDot(String JavaDoc s) throws IOException {
51     ByteArrayOutputStream b1 = new ByteArrayOutputStream();
52     DotStuffOutputStream os = new DotStuffOutputStream(b1);
53     os.write(s.getBytes());
54     os.close();
55     String JavaDoc dotrep = b1.toString();
56
57     ByteArrayInputStream b2 = new ByteArrayInputStream(dotrep.getBytes());
58     DotStuffInputStream is = new DotStuffInputStream(b2);
59     ByteArrayOutputStream b3 = new ByteArrayOutputStream();
60     int c;
61     while ((c = is.read()) >= 0) { b3.write(c); }
62     String JavaDoc act = b3.toString();
63         testAssert(s.equals(act));
64     }
65
66     public void testDotStuffStreams(String JavaDoc[] args) throws IOException {
67     testDot("a\r\n.b\n");
68     testDot(".\r\n");
69     testDot(
70         "hello\r\n.goodbye\r\nlala\r\n\r\n..\r\n\r\n..ar\r\nfoo\r\n.\r\n"
71         );
72
73     testDot("");
74     testDot(".");
75     testDot(".\n");
76     testDot("\n");
77     testDot("\r\n");
78         // XXX This test can't be made to work if we want DotStuffInputStream
79
// XXX to be able to handle non-compliant newline-only implementations.
80
// XXX pffhht.
81
// testDot("\n.\n.\n.\n.");
82
}
83     
84     public void testLimitedOutputStream(String JavaDoc[] args) {
85     ByteArrayOutputStream bos = new ByteArrayOutputStream();
86     ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
87     LimitedOutputStream los = new LimitedOutputStream(bos, 25);
88     boolean hitLimit = false;
89     try {
90         for (int i = 0; i < 10; i++) {
91         bos2.write(Util.bytes(" " + i));
92         los.write(Util.bytes(" " + i));
93         }
94     } catch (IOException e) {
95         if (e instanceof LimitExceededException) {
96         hitLimit = true;
97         String JavaDoc b1 = bos.toString();
98         if (b1.length() != 25) {
99             testError("wrong length: " + b1.length());
100         }
101         String JavaDoc b2 = bos2.toString().substring(0, 25);
102         if (!b1.equals(b2)) {
103             testError("'" + b1 + "' != '" + b2 + "'");
104         }
105         } else {
106         testError("wrong exception: " + e.toString());
107         Debug.print(e);
108         }
109     }
110     if (!hitLimit) {
111         testError("no exception");
112     }
113     
114     }
115
116     public void testNullOutputStream(String JavaDoc[] args) {
117     for (int i = 0; i < 10; i++) {
118         NullOutputStream.Null.println(" " + i);
119     }
120     }
121
122     public void testBase64(String JavaDoc[] args) throws IOException {
123     for (int i = 0; i < 5; i++) {
124         SaveRestoreStream sr = new SaveRestoreStream();
125         OutputStream out = sr.getOutputStream();
126         Base64OutputStream bout = new Base64OutputStream(out);
127         for (int j = 0; j < i; j++) {
128         bout.write(j + 16);
129         }
130         bout.finish();
131         out.close();
132         InputStream is = sr.getInputStream();
133         Base64InputStream bin = new Base64InputStream(is);
134         for (int j = 0; j <= i; j++) {
135         writer.println("" + i + ": " + j + ": " + bin.read());
136         }
137         bin.close();
138         sr.close();
139     }
140     }
141
142     public void testSaveRestore(String JavaDoc[] args) throws IOException {
143         for (int size = 1; size < (1 << 16); size <<= 1) {
144             SaveRestoreStream so = new SaveRestoreStream(1000);
145             OutputStream os = so.getOutputStream();
146             for (int i = 0; i < size; i += 4) {
147                 os.write((i >> 24) & 0xff);
148                 os.write((i >> 16) & 0xff);
149                 os.write((i >> 8) & 0xff);
150                 os.write(i & 0xff);
151             }
152             InputStream is = so.getInputStream();
153             for (int i = 0; i < size; i += 4) {
154                 int t = is.read();
155                 t <<= 8; t |= (is.read() & 0xff);
156                 t <<= 8; t |= (is.read() & 0xff);
157                 t <<= 8; t |= (is.read() & 0xff);
158                 testAssert(t == i);
159             }
160         }
161     }
162
163     public void testRecursiveFileIterator(String JavaDoc[] args)
164         throws Exception JavaDoc
165     {
166         FileFilter filter = new FileFilter() {
167                 public boolean accept(File f) {
168                     if (f.isDirectory()) {
169                         return !f.getName().equals("CVS");
170                     } else {
171                         return (f.getName().endsWith(".java"));
172                     }
173                 }
174             };
175         RecursiveFileIterator f =
176             new RecursiveFileIterator(new File("."), filter);
177         while (f.hasNext()) {
178             File fil = (File)f.next();
179             writer.println(fil.getName());
180         }
181     }
182     
183     public static void main(String JavaDoc args[]) {
184     Test t = new Test();
185     t.test(args);
186     }
187
188 }
189
190
Popular Tags