KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > services > T_MarkedLimitInputStream


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.services.T_MarkedLimitInputStream
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.services;
23
24 import org.apache.derbyTesting.unitTests.harness.T_Generic;
25 import org.apache.derbyTesting.unitTests.harness.T_Fail;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.DataInputStream JavaDoc;
32 import java.io.DataOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 /**
36   A simple unit test for a MarkedLimitInputStream.
37   */

38 public class T_MarkedLimitInputStream extends T_Generic
39 {
40
41     private static final int TEST_SIZE = 10000;
42     private static final int BLOCK_SIZE = 256;
43
44
45     private static MarkedLimitInputStream setup(byte[] data)
46         throws Exception JavaDoc
47     {
48         // make an InputStream on top of an array
49
InputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(data);
50
51         // make an OutputStream on top of an empty array
52
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(TEST_SIZE + 200);
53         // make it into a DataOutputStream
54
DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(baos);
55         // fill it with data in the correct (block) format
56
writeDos(inputStream,dos);
57
58         // make a MarkedLimitInputStream
59
return makeMLIS(baos.toByteArray());
60
61     }
62
63     private static void writeDos(InputStream JavaDoc x, DataOutputStream JavaDoc out)
64         throws Exception JavaDoc
65     {
66         boolean isLastBlock = false;
67         byte[] b = new byte[BLOCK_SIZE];
68
69         while (isLastBlock == false)
70         {
71             int len = x.read(b);
72             if (len != BLOCK_SIZE)
73             {
74                 isLastBlock = true;
75                 if (len < 0)
76                 {
77                     len = 0;
78                 }
79             }
80             out.writeBoolean(isLastBlock);
81             out.writeInt(len);
82             for (int i = 0; i < len; i++)
83             {
84                 out.writeByte(b[i]);
85             }
86         }
87     }
88
89
90     private static MarkedLimitInputStream makeMLIS(byte[] b)
91         throws Exception JavaDoc
92     {
93         // make an InputStream
94
InputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(b);
95         // make a DataInputStream
96
DataInputStream JavaDoc dataInputStream = new DataInputStream JavaDoc(inputStream);
97         // make a MarkedLimitInputStream
98
return new MarkedLimitInputStream(dataInputStream);
99     }
100
101
102     private static boolean readAndCompare(MarkedLimitInputStream mlis, byte[] x)
103         throws Exception JavaDoc
104     {
105         int b;
106         int i = 0;
107         while ((b = mlis.read()) != -1)
108         {
109             if (x[i] != (byte) b)
110             {
111                 System.out.println("Stream and array differ at position " + i);
112                 return false;
113             }
114             i++;
115         }
116         // read to end of stream, check array size
117
if (i != x.length)
118         {
119             System.out.println("array size and stream size differ");
120             return false;
121         }
122         return true;
123
124     }
125
126
127     private static boolean readAndCompareChunks(MarkedLimitInputStream mlis,
128         byte[] x)
129         throws Exception JavaDoc
130     {
131         int chunkSize = 10;
132         byte[] chunk = new byte[chunkSize];
133         int c = 0;
134         int base = 0;
135         while ((c = mlis.read(chunk)) > 0)
136         {
137             for (int offset = 0; offset < c; offset++)
138             {
139                 if (x[base + offset] != chunk[offset])
140                 {
141                     System.out.println("Stream and array differ at position " +
142                         (base + offset));
143                     System.out.println("Array : x[" + (base + offset) + "] = " + x[base+offset]);
144                     System.out.println("Stream : chunk[" + offset + "] = " + chunk[offset]);
145                     return false;
146                 }
147             }
148             base += c;
149         }
150
151         // read to end of stream, check array size
152
if (base != x.length)
153         {
154             System.out.println("array size ( " + x.length +
155                 " ) and stream size ( " + base + " ) differ");
156             return false;
157         }
158         return true;
159
160     }
161
162
163     private static boolean skipAndCompare(MarkedLimitInputStream mlis, byte[] x,
164         long skipTo)
165         throws Exception JavaDoc
166     {
167         long c = mlis.skip(skipTo);
168         T_Fail.T_ASSERT(c == skipTo);
169         byte[] y = new byte[x.length - (int) c];
170         System.arraycopy(x,(int) skipTo, y, 0, x.length - (int) c);
171         return readAndCompare(mlis,y);
172     }
173
174
175     /** Methods required by T_Generic
176     */

177     public String JavaDoc getModuleToTestProtocolName()
178     {
179         return "internalUtils.MarkedLimitInputStream";
180     }
181
182
183     protected void runTests()
184         throws Exception JavaDoc
185     {
186         boolean success = true;
187         // create and initialize array
188
byte[] data = new byte[TEST_SIZE];
189         for (int i = 0; i < data.length; i++)
190         {
191             data[i] = (byte)(i & 0xFF);
192         }
193
194         MarkedLimitInputStream mlis = setup(data);
195         // compare MarkedLimitInputStream with original byte array
196
if (readAndCompare(mlis, data))
197         {
198             PASS("test1");
199         }
200         else
201         {
202             FAIL("test1");
203             success = false;
204         }
205
206         MarkedLimitInputStream mlis2 = setup(data);
207         // compare MarkedLimitInputStream with original byte array
208
// read in chunks
209
if (readAndCompareChunks(mlis2, data))
210         {
211             PASS("test2");
212         }
213         else
214         {
215             FAIL("test2");
216             success = false;
217         }
218
219         MarkedLimitInputStream mlis3 = setup(data);
220         // skip and compare MarkedLimitInputStream with original byte array
221
if (skipAndCompare(mlis3, data, TEST_SIZE/2))
222         {
223             PASS("test3");
224         }
225         else
226         {
227             FAIL("test3");
228             success = false;
229         }
230
231         MarkedLimitInputStream mlis4 = setup(data);
232         // skip and compare MarkedLimitInputStream with original byte array
233
if (skipAndCompare(mlis4, data, TEST_SIZE-1))
234         {
235             PASS("test4");
236         }
237         else
238         {
239             FAIL("test4");
240             success = false;
241         }
242
243         if (!success)
244         {
245             throw T_Fail.testFail();
246         }
247
248
249         // create and initialize array with size BLOCK_SIZE
250
byte[] data2 = new byte[BLOCK_SIZE];
251         for (int i = 0; i < data.length; i++)
252         {
253             data[i] = (byte)(i & 0xFF);
254         }
255         MarkedLimitInputStream mlis5 = setup(data2);
256         // skip and compare MarkedLimitInputStream with original byte array
257
if (readAndCompare(mlis5, data2))
258         {
259             PASS("test5");
260         }
261         else
262         {
263             FAIL("test5");
264             success = false;
265         }
266
267         if (!success)
268         {
269             throw T_Fail.testFail();
270         }
271
272     }
273
274 }
275
Popular Tags