Quadcap Embeddable Server

com/quadcap/io/Test.java

Go to the documentation of this file.
00001 package com.quadcap.io; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.*; 00042 import com.quadcap.util.*; 00043 00044 /** 00045 * Test cases for various io classes. 00046 * 00047 * @author Stan Bailes 00048 */ 00049 public class Test extends com.quadcap.util.Test { 00050 void testDot(String s) throws IOException { 00051 ByteArrayOutputStream b1 = new ByteArrayOutputStream(); 00052 DotStuffOutputStream os = new DotStuffOutputStream(b1); 00053 os.write(s.getBytes()); 00054 os.close(); 00055 String dotrep = b1.toString(); 00056 00057 ByteArrayInputStream b2 = new ByteArrayInputStream(dotrep.getBytes()); 00058 DotStuffInputStream is = new DotStuffInputStream(b2); 00059 ByteArrayOutputStream b3 = new ByteArrayOutputStream(); 00060 int c; 00061 while ((c = is.read()) >= 0) { b3.write(c); } 00062 String act = b3.toString(); 00063 testAssert(s.equals(act)); 00064 } 00065 00066 public void testDotStuffStreams(String[] args) throws IOException { 00067 testDot("a\r\n.b\n"); 00068 testDot(".\r\n"); 00069 testDot( 00070 "hello\r\n.goodbye\r\nlala\r\n\r\n..\r\n\r\n..ar\r\nfoo\r\n.\r\n" 00071 ); 00072 00073 testDot(""); 00074 testDot("."); 00075 testDot(".\n"); 00076 testDot("\n"); 00077 testDot("\r\n"); 00078 // XXX This test can't be made to work if we want DotStuffInputStream 00079 // XXX to be able to handle non-compliant newline-only implementations. 00080 // XXX pffhht. 00081 // testDot("\n.\n.\n.\n."); 00082 } 00083 00084 public void testLimitedOutputStream(String[] args) { 00085 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 00086 ByteArrayOutputStream bos2 = new ByteArrayOutputStream(); 00087 LimitedOutputStream los = new LimitedOutputStream(bos, 25); 00088 boolean hitLimit = false; 00089 try { 00090 for (int i = 0; i < 10; i++) { 00091 bos2.write(Util.bytes(" " + i)); 00092 los.write(Util.bytes(" " + i)); 00093 } 00094 } catch (IOException e) { 00095 if (e instanceof LimitExceededException) { 00096 hitLimit = true; 00097 String b1 = bos.toString(); 00098 if (b1.length() != 25) { 00099 testError("wrong length: " + b1.length()); 00100 } 00101 String b2 = bos2.toString().substring(0, 25); 00102 if (!b1.equals(b2)) { 00103 testError("'" + b1 + "' != '" + b2 + "'"); 00104 } 00105 } else { 00106 testError("wrong exception: " + e.toString()); 00107 Debug.print(e); 00108 } 00109 } 00110 if (!hitLimit) { 00111 testError("no exception"); 00112 } 00113 00114 } 00115 00116 public void testNullOutputStream(String[] args) { 00117 for (int i = 0; i < 10; i++) { 00118 NullOutputStream.Null.println(" " + i); 00119 } 00120 } 00121 00122 public void testBase64(String[] args) throws IOException { 00123 for (int i = 0; i < 5; i++) { 00124 SaveRestoreStream sr = new SaveRestoreStream(); 00125 OutputStream out = sr.getOutputStream(); 00126 Base64OutputStream bout = new Base64OutputStream(out); 00127 for (int j = 0; j < i; j++) { 00128 bout.write(j + 16); 00129 } 00130 bout.finish(); 00131 out.close(); 00132 InputStream is = sr.getInputStream(); 00133 Base64InputStream bin = new Base64InputStream(is); 00134 for (int j = 0; j <= i; j++) { 00135 writer.println("" + i + ": " + j + ": " + bin.read()); 00136 } 00137 bin.close(); 00138 sr.close(); 00139 } 00140 } 00141 00142 public void testSaveRestore(String[] args) throws IOException { 00143 for (int size = 1; size < (1 << 16); size <<= 1) { 00144 SaveRestoreStream so = new SaveRestoreStream(1000); 00145 OutputStream os = so.getOutputStream(); 00146 for (int i = 0; i < size; i += 4) { 00147 os.write((i >> 24) & 0xff); 00148 os.write((i >> 16) & 0xff); 00149 os.write((i >> 8) & 0xff); 00150 os.write(i & 0xff); 00151 } 00152 InputStream is = so.getInputStream(); 00153 for (int i = 0; i < size; i += 4) { 00154 int t = is.read(); 00155 t <<= 8; t |= (is.read() & 0xff); 00156 t <<= 8; t |= (is.read() & 0xff); 00157 t <<= 8; t |= (is.read() & 0xff); 00158 testAssert(t == i); 00159 } 00160 } 00161 } 00162 00163 public void testRecursiveFileIterator(String[] args) 00164 throws Exception 00165 { 00166 FileFilter filter = new FileFilter() { 00167 public boolean accept(File f) { 00168 if (f.isDirectory()) { 00169 return !f.getName().equals("CVS"); 00170 } else { 00171 return (f.getName().endsWith(".java")); 00172 } 00173 } 00174 }; 00175 RecursiveFileIterator f = 00176 new RecursiveFileIterator(new File("."), filter); 00177 while (f.hasNext()) { 00178 File fil = (File)f.next(); 00179 writer.println(fil.getName()); 00180 } 00181 } 00182 00183 public static void main(String args[]) { 00184 Test t = new Test(); 00185 t.test(args); 00186 } 00187 00188 } 00189