1 2 17 18 19 package org.apache.poi.poifs.storage; 20 21 import java.io.*; 22 23 import java.util.*; 24 25 import junit.framework.*; 26 27 32 33 public class TestDocumentBlock 34 extends TestCase 35 { 36 static final private byte[] _testdata; 37 38 static 39 { 40 _testdata = new byte[ 2000 ]; 41 for (int j = 0; j < _testdata.length; j++) 42 { 43 _testdata[ j ] = ( byte ) j; 44 } 45 } 46 ; 47 48 53 54 public TestDocumentBlock(String name) 55 { 56 super(name); 57 } 58 59 64 65 public void testConstructor() 66 throws IOException 67 { 68 ByteArrayInputStream input = new ByteArrayInputStream(_testdata); 69 int index = 0; 70 int size = 0; 71 72 while (true) 73 { 74 byte[] data = new byte[ Math.min(_testdata.length - index, 512) ]; 75 76 System.arraycopy(_testdata, index, data, 0, data.length); 77 DocumentBlock block = new DocumentBlock(input); 78 79 verifyOutput(block, data); 80 size += block.size(); 81 if (block.partiallyRead()) 82 { 83 break; 84 } 85 index += 512; 86 } 87 assertEquals(_testdata.length, size); 88 } 89 90 95 96 public void testRead() 97 throws IOException 98 { 99 DocumentBlock[] blocks = new DocumentBlock[ 4 ]; 100 ByteArrayInputStream input = new ByteArrayInputStream(_testdata); 101 102 for (int j = 0; j < 4; j++) 103 { 104 blocks[ j ] = new DocumentBlock(input); 105 } 106 for (int j = 1; j <= 2000; j += 17) 107 { 108 byte[] buffer = new byte[ j ]; 109 int offset = 0; 110 111 for (int k = 0; k < (2000 / j); k++) 112 { 113 DocumentBlock.read(blocks, buffer, offset); 114 for (int n = 0; n < buffer.length; n++) 115 { 116 assertEquals("checking byte " + (k * j) + n, 117 _testdata[ (k * j) + n ], buffer[ n ]); 118 } 119 offset += j; 120 } 121 } 122 } 123 124 129 130 public void testReadingConstructor() 131 throws IOException 132 { 133 RawDataBlock input = 134 new RawDataBlock(new ByteArrayInputStream(_testdata)); 135 136 verifyOutput(new DocumentBlock(input), input.getData()); 137 } 138 139 private void verifyOutput(DocumentBlock block, byte [] input) 140 throws IOException 141 { 142 assertEquals(input.length, block.size()); 143 if (input.length < 512) 144 { 145 assertTrue(block.partiallyRead()); 146 } 147 else 148 { 149 assertTrue(!block.partiallyRead()); 150 } 151 ByteArrayOutputStream output = new ByteArrayOutputStream(512); 152 153 block.writeBlocks(output); 154 byte[] copy = output.toByteArray(); 155 int j = 0; 156 157 for (; j < input.length; j++) 158 { 159 assertEquals(input[ j ], copy[ j ]); 160 } 161 for (; j < 512; j++) 162 { 163 assertEquals(( byte ) 0xFF, copy[ j ]); 164 } 165 } 166 167 172 173 public static void main(String [] ignored_args) 174 { 175 System.out 176 .println("Testing org.apache.poi.poifs.storage.DocumentBlock"); 177 junit.textui.TestRunner.run(TestDocumentBlock.class); 178 } 179 } 180 | Popular Tags |