1 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 ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.DataInputStream ; 32 import java.io.DataOutputStream ; 33 import java.io.IOException ; 34 35 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 47 { 48 InputStream inputStream = new ByteArrayInputStream (data); 50 51 ByteArrayOutputStream baos = new ByteArrayOutputStream (TEST_SIZE + 200); 53 DataOutputStream dos = new DataOutputStream (baos); 55 writeDos(inputStream,dos); 57 58 return makeMLIS(baos.toByteArray()); 60 61 } 62 63 private static void writeDos(InputStream x, DataOutputStream out) 64 throws Exception 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 92 { 93 InputStream inputStream = new ByteArrayInputStream (b); 95 DataInputStream dataInputStream = new DataInputStream (inputStream); 97 return new MarkedLimitInputStream(dataInputStream); 99 } 100 101 102 private static boolean readAndCompare(MarkedLimitInputStream mlis, byte[] x) 103 throws Exception 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 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 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 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 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 177 public String getModuleToTestProtocolName() 178 { 179 return "internalUtils.MarkedLimitInputStream"; 180 } 181 182 183 protected void runTests() 184 throws Exception 185 { 186 boolean success = true; 187 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 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 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 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 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 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 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 |