KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > derbynet > ByteArrayCombinerStreamTest


1 /*
2     Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.ByteArrayCombinerStreamTest
3
4     Licensed to the Apache Software Foundation (ASF) under one
5     or more contributor license agreements. See the NOTICE file
6     distributed with this work for additional information
7     regarding copyright ownership. The ASF licenses this file
8     to you under the Apache License, Version 2.0 (the
9     "License"); you may not use this file except in compliance
10     with 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,
15     software distributed under the License is distributed on an
16     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17     KIND, either express or implied. See the License for the
18     specific language governing permissions and limitations
19     under the License.
20 */

21 package org.apache.derbyTesting.functionTests.tests.derbynet;
22
23 import org.apache.derbyTesting.junit.BaseTestCase;
24
25 import org.apache.derby.client.am.ByteArrayCombinerStream;
26
27 import java.io.*;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30
31 /**
32  * Test functionality of <code>ByteArrayCombinerStream</code>.
33  */

34 public class ByteArrayCombinerStreamTest
35     extends BaseTestCase {
36
37     private static final byte[] defaultArray = {
38             65,66,67,68,69,70,71,72,73,74,75,76,
39             77,78,79,80,81,82,83,84,85,86,87,88};
40
41     private ByteArrayCombinerStream combiner;
42
43     public ByteArrayCombinerStreamTest(String JavaDoc name) {
44         super(name);
45     }
46
47     public void testCombineNullRead()
48             throws IOException {
49         combiner = new ByteArrayCombinerStream(null, 0);
50         assertEquals(-1, combiner.read());
51     }
52
53     public void testCombineNullReadArray()
54             throws IOException {
55         combiner = new ByteArrayCombinerStream(null, 0);
56         assertEquals(-1, combiner.read(new byte[10], 0, 10));
57     }
58
59     public void testCombineAvailableNull()
60             throws IOException {
61         combiner = new ByteArrayCombinerStream(null, 0);
62         assertEquals(0, combiner.available());
63     }
64
65     public void testCombineAvailable4bytes()
66             throws IOException {
67         byte[] array = {65,66,77,79};
68         ArrayList JavaDoc list = new ArrayList JavaDoc(1);
69         list.add(array);
70         combiner = new ByteArrayCombinerStream(list, 4);
71         assertEquals(4, combiner.available());
72     }
73
74     /**
75      * Make sure an extra "empty" array doesn't cause errors.
76      * This test is based on knowledge of the implementation, where an extra
77      * byte array was not removed during the reducation process. This can
78      * cause <code>nextArray</code> to not return <code>null</code> when it
79      * should, causing an <code>ArrayIndexOutOfBoundsException</code>.
80      * This bug was corrected by DERBY-1417.
81      */

82     public void testCombineWithExtraEmptyByteArray()
83             throws IOException {
84         byte[] array = {65,66,77,79};
85         ArrayList JavaDoc list = new ArrayList JavaDoc(2);
86         list.add(array);
87         list.add(new byte[4]);
88         combiner = new ByteArrayCombinerStream(list, array.length);
89         byte[] resArray = new byte[array.length];
90         assertEquals(array.length,
91                      combiner.read(resArray, 0, resArray.length));
92         assertTrue(combiner.read() == -1);
93     }
94
95     public void testCombineOneArray()
96             throws IOException {
97         ArrayList JavaDoc list = new ArrayList JavaDoc(1);
98         list.add(defaultArray);
99         combiner = new ByteArrayCombinerStream(list, defaultArray.length);
100         byte[] resArray = new byte[defaultArray.length];
101         assertEquals(defaultArray.length,
102                      combiner.read(resArray,0, resArray.length));
103         assertTrue(combiner.read() == -1);
104         assertTrue(Arrays.equals(defaultArray, resArray));
105     }
106
107     public void testCominbe100SmallArrays()
108             throws IOException {
109         int arrays = 100;
110         byte[] array = {65,66,77,79};
111         ArrayList JavaDoc list = new ArrayList JavaDoc(arrays);
112         long length = 0;
113         for (int i=0; i < arrays; i++) {
114             list.add(array);
115             length += array.length;
116         }
117         byte[] targetArray = new byte[(int)length];
118         int offset = 0;
119         for (int i=0; i < arrays; i++) {
120             System.arraycopy(array, 0, targetArray, offset, array.length);
121             offset += array.length;
122         }
123         combiner = new ByteArrayCombinerStream(list, length);
124         byte[] resArray = new byte[(int)length];
125         assertEquals(length, combiner.read(resArray, 0, resArray.length));
126         assertTrue(combiner.read() == -1);
127         assertTrue(combiner.read() == -1);
128         assertTrue(Arrays.equals(targetArray, resArray));
129     }
130
131     public void testTruncateDataFromOneArray()
132             throws IOException {
133         int length = defaultArray.length -5;
134         ArrayList JavaDoc list = new ArrayList JavaDoc(1);
135         list.add(defaultArray);
136         byte[] targetArray = new byte[length];
137         System.arraycopy(defaultArray, 0,
138                          targetArray, 0, length);
139         byte[] resArray = new byte[length];
140         combiner = new ByteArrayCombinerStream(list, length);
141         assertEquals(length, combiner.read(resArray, 0, length));
142         assertTrue(combiner.read() == -1);
143         assertTrue(Arrays.equals(targetArray, resArray));
144     }
145
146     public void testTruncateDataFromTwoArrays()
147             throws IOException {
148         int length = (defaultArray.length *2) -7;
149         ArrayList JavaDoc list = new ArrayList JavaDoc(2);
150         list.add(defaultArray);
151         list.add(defaultArray);
152         byte[] targetArray = new byte[length];
153         System.arraycopy(defaultArray, 0,
154                          targetArray, 0, defaultArray.length);
155         System.arraycopy(defaultArray, 0,
156                          targetArray, defaultArray.length,
157                          length - defaultArray.length);
158         byte[] resArray = new byte[length];
159         combiner = new ByteArrayCombinerStream(list, length);
160         assertEquals(length, combiner.read(resArray, 0, length));
161         assertTrue(combiner.read() == -1);
162         assertTrue(Arrays.equals(targetArray, resArray));
163     }
164
165     /**
166      * Make sure an exception is thrown if there is less data available than
167      * the specified length.
168      */

169     public void testTooLittleDataNoCombine() {
170         ArrayList JavaDoc list = new ArrayList JavaDoc(1);
171         list.add(new byte[5]);
172         try {
173             combiner = new ByteArrayCombinerStream(list, 10);
174             fail("An IllegalArgumentException singalling too little data " +
175                     "should have been thrown");
176         } catch (IllegalArgumentException JavaDoc iae) {
177             // This should happen, continue.
178
}
179     }
180
181     /**
182      * Make sure an exception is thrown if there is less data available than
183      * the specified length.
184      */

185     public void testTooLittleDataWithCombine() {
186         ArrayList JavaDoc list = new ArrayList JavaDoc(3);
187         byte[] data = {65,66,67,68,69};
188         list.add(data);
189         list.add(data);
190         list.add(data);
191         try {
192             combiner = new ByteArrayCombinerStream(list, data.length *3 + 1);
193             fail("An IllegalArgumentException singalling too little data " +
194                     "should have been thrown");
195         } catch (IllegalArgumentException JavaDoc iae) {
196             // This should happen, continue.
197
}
198     }
199
200     /**
201      * Make sure an exception is thrown if a negative length is specified.
202      */

203     public void testNegativeLengthArgument() {
204         ArrayList JavaDoc list = new ArrayList JavaDoc(1);
205         list.add(new byte[1234]);
206         try {
207             combiner = new ByteArrayCombinerStream(list, -54);
208             fail("An IllegalArgumentException singalling negative length " +
209                     "should have been thrown");
210         } catch (IllegalArgumentException JavaDoc iae) {
211             // This should happen, continue.
212

213         }
214     }
215
216     /**
217      * Demonstrate that the stream does not change negative values in the
218      * underlying data.
219      * This can cause code to believe the stream is exhausted even though it is
220      * not.
221      */

222     public void testNegativeValueInDataCausesEndOfStream()
223             throws IOException {
224         byte[] data = {66,67,-123,68,69};
225         byte[] targetData = {66,67,0,0,0};
226         byte[] resData = new byte[5];
227         ArrayList JavaDoc list = new ArrayList JavaDoc(1);
228         list.add(data);
229         combiner = new ByteArrayCombinerStream(list, data.length);
230         byte b;
231         int index = 0;
232         while ((b = (byte)combiner.read()) > 0) {
233             resData[index++] = b;
234         }
235         assertTrue(Arrays.equals(targetData, resData));
236         // Even though we think the stream is exhausted, it is not...
237
assertEquals(data[3], (byte)combiner.read());
238         assertEquals(data[4], (byte)combiner.read());
239         assertEquals(-1, (byte)combiner.read());
240     }
241 } // End class ByteArrayCombinerStreamTest
242
Popular Tags