1 2 17 18 19 package org.apache.poi.poifs.filesystem; 20 21 import java.io.*; 22 23 import java.util.*; 24 25 import junit.framework.*; 26 27 import org.apache.poi.poifs.property.DirectoryProperty; 28 import org.apache.poi.poifs.property.DocumentProperty; 29 import org.apache.poi.poifs.storage.RawDataBlock; 30 31 36 37 public class TestDocumentInputStream 38 extends TestCase 39 { 40 41 48 49 public TestDocumentInputStream(String name) 50 throws IOException 51 { 52 super(name); 53 int blocks = (_workbook_size + 511) / 512; 54 55 _workbook_data = new byte[ 512 * blocks ]; 56 Arrays.fill(_workbook_data, ( byte ) -1); 57 for (int j = 0; j < _workbook_size; j++) 58 { 59 _workbook_data[ j ] = ( byte ) (j * j); 60 } 61 RawDataBlock[] rawBlocks = new RawDataBlock[ blocks ]; 62 ByteArrayInputStream stream = 63 new ByteArrayInputStream(_workbook_data); 64 65 for (int j = 0; j < blocks; j++) 66 { 67 rawBlocks[ j ] = new RawDataBlock(stream); 68 } 69 POIFSDocument document = new POIFSDocument("Workbook", rawBlocks, 70 _workbook_size); 71 72 _workbook = new DocumentNode( 73 document.getDocumentProperty(), 74 new DirectoryNode( 75 new DirectoryProperty("Root Entry"), null, null)); 76 } 77 78 private DocumentNode _workbook; 79 private byte[] _workbook_data; 80 private static final int _workbook_size = 5000; 81 82 private static final int _buffer_size = 6; 85 86 91 92 public void testConstructor() 93 throws IOException 94 { 95 DocumentInputStream stream = new DocumentInputStream(_workbook); 96 97 assertEquals(_workbook_size, stream.available()); 98 } 99 100 105 106 public void testAvailable() 107 throws IOException 108 { 109 DocumentInputStream stream = new DocumentInputStream(_workbook); 110 111 assertEquals(_workbook_size, stream.available()); 112 stream.close(); 113 try 114 { 115 stream.available(); 116 fail("Should have caught IOException"); 117 } 118 catch (IOException ignored) 119 { 120 121 } 123 } 124 125 130 131 public void testMarkFunctions() 132 throws IOException 133 { 134 DocumentInputStream stream = new DocumentInputStream(_workbook); 135 byte[] buffer = new byte[ _workbook_size / 5 ]; 136 137 stream.read(buffer); 138 for (int j = 0; j < buffer.length; j++) 139 { 140 assertEquals("checking byte " + j, _workbook_data[ j ], 141 buffer[ j ]); 142 } 143 assertEquals(_workbook_size - buffer.length, stream.available()); 144 stream.reset(); 145 assertEquals(_workbook_size, stream.available()); 146 stream.read(buffer); 147 stream.mark(12); 148 stream.read(buffer); 149 assertEquals(_workbook_size - (2 * buffer.length), 150 stream.available()); 151 for (int j = buffer.length; j < (2 * buffer.length); j++) 152 { 153 assertEquals("checking byte " + j, _workbook_data[ j ], 154 buffer[ j - buffer.length ]); 155 } 156 stream.reset(); 157 assertEquals(_workbook_size - buffer.length, stream.available()); 158 stream.read(buffer); 159 assertEquals(_workbook_size - (2 * buffer.length), 160 stream.available()); 161 for (int j = buffer.length; j < (2 * buffer.length); j++) 162 { 163 assertEquals("checking byte " + j, _workbook_data[ j ], 164 buffer[ j - buffer.length ]); 165 } 166 assertTrue(stream.markSupported()); 167 } 168 169 174 175 public void testReadSingleByte() 176 throws IOException 177 { 178 DocumentInputStream stream = new DocumentInputStream(_workbook); 179 int remaining = _workbook_size; 180 181 for (int j = 0; j < _workbook_size; j++) 182 { 183 int b = stream.read(); 184 assertTrue("checking sign of " + j, b >= 0); 185 assertEquals("validating byte " + j, _workbook_data[ j ], 186 ( byte ) b); 187 remaining--; 188 assertEquals("checking remaining after reading byte " + j, 189 remaining, stream.available()); 190 } 191 assertEquals(-1, stream.read()); 192 stream.close(); 193 try 194 { 195 stream.read(); 196 fail("Should have caught IOException"); 197 } 198 catch (IOException ignored) 199 { 200 201 } 203 } 204 205 210 211 public void testBufferRead() 212 throws IOException 213 { 214 DocumentInputStream stream = new DocumentInputStream(_workbook); 215 216 try 217 { 218 stream.read(null); 219 fail("Should have caught NullPointerException"); 220 } 221 catch (NullPointerException ignored) 222 { 223 224 } 226 227 assertEquals(0, stream.read(new byte[ 0 ])); 229 assertEquals(_workbook_size, stream.available()); 230 byte[] buffer = new byte[ _buffer_size ]; 231 int offset = 0; 232 233 while (stream.available() >= buffer.length) 234 { 235 assertEquals(_buffer_size, stream.read(buffer)); 236 for (int j = 0; j < buffer.length; j++) 237 { 238 assertEquals("in main loop, byte " + offset, 239 _workbook_data[ offset ], buffer[ j ]); 240 offset++; 241 } 242 assertEquals("offset " + offset, _workbook_size - offset, 243 stream.available()); 244 } 245 assertEquals(_workbook_size % _buffer_size, stream.available()); 246 Arrays.fill(buffer, ( byte ) 0); 247 int count = stream.read(buffer); 248 249 assertEquals(_workbook_size % _buffer_size, count); 250 for (int j = 0; j < count; j++) 251 { 252 assertEquals("past main loop, byte " + offset, 253 _workbook_data[ offset ], buffer[ j ]); 254 offset++; 255 } 256 assertEquals(_workbook_size, offset); 257 for (int j = count; j < buffer.length; j++) 258 { 259 assertEquals("checking remainder, byte " + j, 0, buffer[ j ]); 260 } 261 assertEquals(-1, stream.read(buffer)); 262 stream.close(); 263 try 264 { 265 stream.read(buffer); 266 fail("Should have caught IOException"); 267 } 268 catch (IOException ignored) 269 { 270 271 } 273 } 274 275 280 281 public void testComplexBufferRead() 282 throws IOException 283 { 284 DocumentInputStream stream = new DocumentInputStream(_workbook); 285 286 try 287 { 288 stream.read(null, 0, 1); 289 fail("Should have caught NullPointerException"); 290 } 291 catch (NullPointerException ignored) 292 { 293 294 } 296 297 try 299 { 300 stream.read(new byte[ 5 ], -4, 0); 301 fail("Should have caught IndexOutOfBoundsException"); 302 } 303 catch (IndexOutOfBoundsException ignored) 304 { 305 306 } 308 try 309 { 310 stream.read(new byte[ 5 ], 0, -4); 311 fail("Should have caught IndexOutOfBoundsException"); 312 } 313 catch (IndexOutOfBoundsException ignored) 314 { 315 316 } 318 try 319 { 320 stream.read(new byte[ 5 ], 0, 6); 321 fail("Should have caught IndexOutOfBoundsException"); 322 } 323 catch (IndexOutOfBoundsException ignored) 324 { 325 326 } 328 329 assertEquals(0, stream.read(new byte[ 5 ], 0, 0)); 331 assertEquals(_workbook_size, stream.available()); 332 byte[] buffer = new byte[ _workbook_size ]; 333 int offset = 0; 334 335 while (stream.available() >= _buffer_size) 336 { 337 Arrays.fill(buffer, ( byte ) 0); 338 assertEquals(_buffer_size, 339 stream.read(buffer, offset, _buffer_size)); 340 for (int j = 0; j < offset; j++) 341 { 342 assertEquals("checking byte " + j, 0, buffer[ j ]); 343 } 344 for (int j = offset; j < (offset + _buffer_size); j++) 345 { 346 assertEquals("checking byte " + j, _workbook_data[ j ], 347 buffer[ j ]); 348 } 349 for (int j = offset + _buffer_size; j < buffer.length; j++) 350 { 351 assertEquals("checking byte " + j, 0, buffer[ j ]); 352 } 353 offset += _buffer_size; 354 assertEquals("offset " + offset, _workbook_size - offset, 355 stream.available()); 356 } 357 assertEquals(_workbook_size % _buffer_size, stream.available()); 358 Arrays.fill(buffer, ( byte ) 0); 359 int count = stream.read(buffer, offset, 360 _workbook_size % _buffer_size); 361 362 assertEquals(_workbook_size % _buffer_size, count); 363 for (int j = 0; j < offset; j++) 364 { 365 assertEquals("checking byte " + j, 0, buffer[ j ]); 366 } 367 for (int j = offset; j < buffer.length; j++) 368 { 369 assertEquals("checking byte " + j, _workbook_data[ j ], 370 buffer[ j ]); 371 } 372 assertEquals(_workbook_size, offset + count); 373 for (int j = count; j < offset; j++) 374 { 375 assertEquals("byte " + j, 0, buffer[ j ]); 376 } 377 assertEquals(-1, stream.read(buffer, 0, 1)); 378 stream.close(); 379 try 380 { 381 stream.read(buffer, 0, 1); 382 fail("Should have caught IOException"); 383 } 384 catch (IOException ignored) 385 { 386 387 } 389 } 390 391 396 397 public void testSkip() 398 throws IOException 399 { 400 DocumentInputStream stream = new DocumentInputStream(_workbook); 401 402 assertEquals(_workbook_size, stream.available()); 403 int count = stream.available(); 404 405 while (stream.available() >= _buffer_size) 406 { 407 assertEquals(_buffer_size, stream.skip(_buffer_size)); 408 count -= _buffer_size; 409 assertEquals(count, stream.available()); 410 } 411 assertEquals(_workbook_size % _buffer_size, 412 stream.skip(_buffer_size)); 413 assertEquals(0, stream.available()); 414 stream.reset(); 415 assertEquals(_workbook_size, stream.available()); 416 assertEquals(_workbook_size, stream.skip(_workbook_size * 2)); 417 assertEquals(0, stream.available()); 418 stream.reset(); 419 assertEquals(_workbook_size, stream.available()); 420 assertEquals(_workbook_size, 421 stream.skip(2 + ( long ) Integer.MAX_VALUE)); 422 assertEquals(0, stream.available()); 423 } 424 425 430 431 public static void main(String [] ignored_args) 432 { 433 System.out.println( 434 "Testing org.apache.poi.poifs.filesystem.DocumentInputStream"); 435 junit.textui.TestRunner.run(TestDocumentInputStream.class); 436 } 437 } 438 | Popular Tags |