1 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 ; 29 import java.util.Arrays ; 30 31 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 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 list = new ArrayList (1); 69 list.add(array); 70 combiner = new ByteArrayCombinerStream(list, 4); 71 assertEquals(4, combiner.available()); 72 } 73 74 82 public void testCombineWithExtraEmptyByteArray() 83 throws IOException { 84 byte[] array = {65,66,77,79}; 85 ArrayList list = new ArrayList (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 list = new ArrayList (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 list = new ArrayList (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 list = new ArrayList (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 list = new ArrayList (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 169 public void testTooLittleDataNoCombine() { 170 ArrayList list = new ArrayList (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 iae) { 177 } 179 } 180 181 185 public void testTooLittleDataWithCombine() { 186 ArrayList list = new ArrayList (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 iae) { 196 } 198 } 199 200 203 public void testNegativeLengthArgument() { 204 ArrayList list = new ArrayList (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 iae) { 211 213 } 214 } 215 216 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 list = new ArrayList (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 assertEquals(data[3], (byte)combiner.read()); 238 assertEquals(data[4], (byte)combiner.read()); 239 assertEquals(-1, (byte)combiner.read()); 240 } 241 } | Popular Tags |