001: /*
002: Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.ByteArrayCombinerStreamTest
003:
004: Licensed to the Apache Software Foundation (ASF) under one
005: or more contributor license agreements. See the NOTICE file
006: distributed with this work for additional information
007: regarding copyright ownership. The ASF licenses this file
008: to you under the Apache License, Version 2.0 (the
009: "License"); you may not use this file except in compliance
010: with the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing,
015: software distributed under the License is distributed on an
016: "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: KIND, either express or implied. See the License for the
018: specific language governing permissions and limitations
019: under the License.
020: */
021: package org.apache.derbyTesting.functionTests.tests.derbynet;
022:
023: import org.apache.derbyTesting.junit.BaseTestCase;
024:
025: import org.apache.derby.client.am.ByteArrayCombinerStream;
026:
027: import java.io.*;
028: import java.util.ArrayList;
029: import java.util.Arrays;
030:
031: /**
032: * Test functionality of <code>ByteArrayCombinerStream</code>.
033: */
034: public class ByteArrayCombinerStreamTest extends BaseTestCase {
035:
036: private static final byte[] defaultArray = { 65, 66, 67, 68, 69,
037: 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
038: 85, 86, 87, 88 };
039:
040: private ByteArrayCombinerStream combiner;
041:
042: public ByteArrayCombinerStreamTest(String name) {
043: super (name);
044: }
045:
046: public void testCombineNullRead() throws IOException {
047: combiner = new ByteArrayCombinerStream(null, 0);
048: assertEquals(-1, combiner.read());
049: }
050:
051: public void testCombineNullReadArray() throws IOException {
052: combiner = new ByteArrayCombinerStream(null, 0);
053: assertEquals(-1, combiner.read(new byte[10], 0, 10));
054: }
055:
056: public void testCombineAvailableNull() throws IOException {
057: combiner = new ByteArrayCombinerStream(null, 0);
058: assertEquals(0, combiner.available());
059: }
060:
061: public void testCombineAvailable4bytes() throws IOException {
062: byte[] array = { 65, 66, 77, 79 };
063: ArrayList list = new ArrayList(1);
064: list.add(array);
065: combiner = new ByteArrayCombinerStream(list, 4);
066: assertEquals(4, combiner.available());
067: }
068:
069: /**
070: * Make sure an extra "empty" array doesn't cause errors.
071: * This test is based on knowledge of the implementation, where an extra
072: * byte array was not removed during the reducation process. This can
073: * cause <code>nextArray</code> to not return <code>null</code> when it
074: * should, causing an <code>ArrayIndexOutOfBoundsException</code>.
075: * This bug was corrected by DERBY-1417.
076: */
077: public void testCombineWithExtraEmptyByteArray() throws IOException {
078: byte[] array = { 65, 66, 77, 79 };
079: ArrayList list = new ArrayList(2);
080: list.add(array);
081: list.add(new byte[4]);
082: combiner = new ByteArrayCombinerStream(list, array.length);
083: byte[] resArray = new byte[array.length];
084: assertEquals(array.length, combiner.read(resArray, 0,
085: resArray.length));
086: assertTrue(combiner.read() == -1);
087: }
088:
089: public void testCombineOneArray() throws IOException {
090: ArrayList list = new ArrayList(1);
091: list.add(defaultArray);
092: combiner = new ByteArrayCombinerStream(list,
093: defaultArray.length);
094: byte[] resArray = new byte[defaultArray.length];
095: assertEquals(defaultArray.length, combiner.read(resArray, 0,
096: resArray.length));
097: assertTrue(combiner.read() == -1);
098: assertTrue(Arrays.equals(defaultArray, resArray));
099: }
100:
101: public void testCominbe100SmallArrays() throws IOException {
102: int arrays = 100;
103: byte[] array = { 65, 66, 77, 79 };
104: ArrayList list = new ArrayList(arrays);
105: long length = 0;
106: for (int i = 0; i < arrays; i++) {
107: list.add(array);
108: length += array.length;
109: }
110: byte[] targetArray = new byte[(int) length];
111: int offset = 0;
112: for (int i = 0; i < arrays; i++) {
113: System.arraycopy(array, 0, targetArray, offset,
114: array.length);
115: offset += array.length;
116: }
117: combiner = new ByteArrayCombinerStream(list, length);
118: byte[] resArray = new byte[(int) length];
119: assertEquals(length, combiner
120: .read(resArray, 0, resArray.length));
121: assertTrue(combiner.read() == -1);
122: assertTrue(combiner.read() == -1);
123: assertTrue(Arrays.equals(targetArray, resArray));
124: }
125:
126: public void testTruncateDataFromOneArray() throws IOException {
127: int length = defaultArray.length - 5;
128: ArrayList list = new ArrayList(1);
129: list.add(defaultArray);
130: byte[] targetArray = new byte[length];
131: System.arraycopy(defaultArray, 0, targetArray, 0, length);
132: byte[] resArray = new byte[length];
133: combiner = new ByteArrayCombinerStream(list, length);
134: assertEquals(length, combiner.read(resArray, 0, length));
135: assertTrue(combiner.read() == -1);
136: assertTrue(Arrays.equals(targetArray, resArray));
137: }
138:
139: public void testTruncateDataFromTwoArrays() throws IOException {
140: int length = (defaultArray.length * 2) - 7;
141: ArrayList list = new ArrayList(2);
142: list.add(defaultArray);
143: list.add(defaultArray);
144: byte[] targetArray = new byte[length];
145: System.arraycopy(defaultArray, 0, targetArray, 0,
146: defaultArray.length);
147: System.arraycopy(defaultArray, 0, targetArray,
148: defaultArray.length, length - defaultArray.length);
149: byte[] resArray = new byte[length];
150: combiner = new ByteArrayCombinerStream(list, length);
151: assertEquals(length, combiner.read(resArray, 0, length));
152: assertTrue(combiner.read() == -1);
153: assertTrue(Arrays.equals(targetArray, resArray));
154: }
155:
156: /**
157: * Make sure an exception is thrown if there is less data available than
158: * the specified length.
159: */
160: public void testTooLittleDataNoCombine() {
161: ArrayList list = new ArrayList(1);
162: list.add(new byte[5]);
163: try {
164: combiner = new ByteArrayCombinerStream(list, 10);
165: fail("An IllegalArgumentException singalling too little data "
166: + "should have been thrown");
167: } catch (IllegalArgumentException iae) {
168: // This should happen, continue.
169: }
170: }
171:
172: /**
173: * Make sure an exception is thrown if there is less data available than
174: * the specified length.
175: */
176: public void testTooLittleDataWithCombine() {
177: ArrayList list = new ArrayList(3);
178: byte[] data = { 65, 66, 67, 68, 69 };
179: list.add(data);
180: list.add(data);
181: list.add(data);
182: try {
183: combiner = new ByteArrayCombinerStream(list,
184: data.length * 3 + 1);
185: fail("An IllegalArgumentException singalling too little data "
186: + "should have been thrown");
187: } catch (IllegalArgumentException iae) {
188: // This should happen, continue.
189: }
190: }
191:
192: /**
193: * Make sure an exception is thrown if a negative length is specified.
194: */
195: public void testNegativeLengthArgument() {
196: ArrayList list = new ArrayList(1);
197: list.add(new byte[1234]);
198: try {
199: combiner = new ByteArrayCombinerStream(list, -54);
200: fail("An IllegalArgumentException singalling negative length "
201: + "should have been thrown");
202: } catch (IllegalArgumentException iae) {
203: // This should happen, continue.
204:
205: }
206: }
207:
208: /**
209: * Demonstrate that the stream does not change negative values in the
210: * underlying data.
211: * This can cause code to believe the stream is exhausted even though it is
212: * not.
213: */
214: public void testNegativeValueInDataCausesEndOfStream()
215: throws IOException {
216: byte[] data = { 66, 67, -123, 68, 69 };
217: byte[] targetData = { 66, 67, 0, 0, 0 };
218: byte[] resData = new byte[5];
219: ArrayList list = new ArrayList(1);
220: list.add(data);
221: combiner = new ByteArrayCombinerStream(list, data.length);
222: byte b;
223: int index = 0;
224: while ((b = (byte) combiner.read()) > 0) {
225: resData[index++] = b;
226: }
227: assertTrue(Arrays.equals(targetData, resData));
228: // Even though we think the stream is exhausted, it is not...
229: assertEquals(data[3], (byte) combiner.read());
230: assertEquals(data[4], (byte) combiner.read());
231: assertEquals(-1, (byte) combiner.read());
232: }
233: } // End class ByteArrayCombinerStreamTest
|