KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > ConcatTests


1 /*
2  * Contact Streams Regression test.
3  * Copyright (C) 2004 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.io.*;
21
22 /**
23  * Regression test for Concatenation Streams.
24  * More information about this class is available from <a target="_top" HREF=
25  * "http://ostermiller.org/utils/Conact.html">ostermiller.org</a>.
26  *
27  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
28  * @since ostermillerutils 1.04.00
29  */

30 class ConcatTests {
31     public static void main(String JavaDoc[] args){
32         try {
33             ConcatReader cr = new ConcatReader(
34                 new Reader[]{
35                     new StringReader("1"),
36                     new StringReader("two"),
37                     new StringReader(""),
38                     new StringReader("4"),
39                     new StringReader("five"),
40                     new StringReader("six"),
41                     new StringReader("seven"),
42                 }
43             );
44             if (!cr.ready()) throw new Exception JavaDoc ("Not Ready");
45             read(cr, '1');
46             read(cr, 't');
47             read(cr, 'w');
48             read(cr, 'o');
49             if (!cr.ready()) throw new Exception JavaDoc ("Not Ready");
50             read(cr, '4');
51             read(cr, "fiv");
52             skip(cr, 2);
53             read(cr, "i");
54             read(cr, "xseven");
55             if (cr.read() != -1) throw new Exception JavaDoc ("Read did not terminate");
56             if (cr.read() != -1) throw new Exception JavaDoc ("Didn't stay closed");
57             cr.close();
58
59             final ConcatReader cr1 = new ConcatReader();
60             if (cr1.ready()) throw new Exception JavaDoc ("Ready");
61             cr1.addReader(new StringReader("one"));
62             read(cr1, 'o');
63             cr1.addReader(new StringReader("two"));
64             read(cr1, "netwo");
65             new Thread JavaDoc(){
66                 public void run(){
67                     try {
68                         Thread.sleep(1000);
69                     } catch (InterruptedException JavaDoc ix){
70                     }
71                     cr1.addReader(new StringReader("three"));
72                 }
73             }.start();
74             read(cr1, "three");
75             cr1.lastReaderAdded();
76             if (cr1.read() != -1) throw new Exception JavaDoc ("Read did not terminate");
77
78             ConcatInputStream cis = new ConcatInputStream(
79                 new InputStream[]{
80                     new ByteArrayInputStream(new byte[]{'1'}),
81                     new ByteArrayInputStream(new byte[]{'t','w','o'}),
82                     new ByteArrayInputStream(new byte[]{}),
83                     new ByteArrayInputStream(new byte[]{'4'}),
84                     new ByteArrayInputStream(new byte[]{'f','i','v','e'}),
85                     new ByteArrayInputStream(new byte[]{'s','i','x'}),
86                     new ByteArrayInputStream(new byte[]{'s','e','v','e','n'}),
87                 }
88             );
89             if (cis.available() <= 0) throw new Exception JavaDoc ("Not Ready");
90             read(cis, '1');
91             read(cis, 't');
92             read(cis, 'w');
93             read(cis, 'o');
94             read(cis, '4');
95             read(cis, "fivesi");
96             if (cis.available() <= 0) throw new Exception JavaDoc ("Not Ready");
97             read(cis, "xseven");
98             if (cis.read() != -1) throw new Exception JavaDoc ("Read did not terminate");
99             if (cis.read() != -1) throw new Exception JavaDoc ("Didn't stay closed");
100
101             final ConcatInputStream cis1 = new ConcatInputStream();
102             if (cis.available() != 0) throw new Exception JavaDoc ("Ready");
103             cis1.addInputStream(new ByteArrayInputStream("one".getBytes("ASCII")));
104             read(cis1, 'o');
105             cis1.addInputStream(new ByteArrayInputStream("two".getBytes("ASCII")));
106             read(cis1, "netwo");
107             new Thread JavaDoc(){
108                 public void run(){
109                     try {
110                         Thread.sleep(1000);
111                     } catch (InterruptedException JavaDoc ix){
112                     }
113                     try {
114                         cis1.addInputStream(new ByteArrayInputStream("three".getBytes("ASCII")));
115                     } catch (Exception JavaDoc x){
116                         System.err.println(x.getMessage());
117                         x.printStackTrace();
118                         System.exit(1);
119                     }
120                 }
121             }.start();
122             read(cis1, "three");
123             cis1.lastInputStreamAdded();
124             if (cis1.read() != -1) throw new Exception JavaDoc ("Read did not terminate");
125
126         } catch (Exception JavaDoc x){
127             System.err.println(x.getMessage());
128             x.printStackTrace();
129             System.exit(1);
130         }
131         System.exit(0);
132     }
133
134     private static void skip(Reader in, int n) throws Exception JavaDoc {
135         int s = 0;
136         while (s<n){
137             s+=in.skip(n-s);
138         }
139     }
140
141
142     private static void read(Reader in, char expected) throws Exception JavaDoc {
143         int c = in.read();
144         if (c != (int)expected) throw new Exception JavaDoc ("Expected to read " + expected + " but read " + (char)c);
145     }
146
147     private static void read(InputStream in, char expected) throws Exception JavaDoc {
148         int c = in.read();
149         if (c != (int)expected) throw new Exception JavaDoc ("Expected to read " + expected + " but read " + (char)c);
150     }
151
152     private static void read(Reader in, String JavaDoc expected) throws Exception JavaDoc {
153         int totalRead = 0;
154         while (totalRead < expected.length()){
155             char[] buffer = new char[expected.length()-totalRead];
156             int read = in.read(buffer);
157             if (read == -1) throw new Exception JavaDoc("Read terminated early");
158             if (!expected.substring(totalRead, totalRead+read).equals(new String JavaDoc(buffer,0,read))){
159                 throw new Exception JavaDoc ("Expected to read " + expected.substring(totalRead, totalRead+read) + " but read " + new String JavaDoc(buffer,0,read));
160             }
161             totalRead+=read;
162         }
163     }
164
165      private static void read(InputStream in, String JavaDoc expected) throws Exception JavaDoc {
166         int totalRead = 0;
167         while (totalRead < expected.length()){
168             byte[] buffer = new byte[expected.length()-totalRead];
169             int read = in.read(buffer);
170             if (read == -1) throw new Exception JavaDoc("Read terminated early");
171             if (!expected.substring(totalRead, totalRead+read).equals(new String JavaDoc(buffer,0,read,"ASCII"))){
172                 throw new Exception JavaDoc ("Expected to read " + expected.substring(totalRead, totalRead+read) + " but read " + new String JavaDoc(buffer,0,read,"ASCII"));
173             }
174             totalRead+=read;
175         }
176     }
177
178 }
Popular Tags