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 TestSmallDocumentBlock 34 extends TestCase 35 { 36 static final private byte[] _testdata; 37 static final private int _testdata_size = 2999; 38 39 static 40 { 41 _testdata = new byte[ _testdata_size ]; 42 for (int j = 0; j < _testdata.length; j++) 43 { 44 _testdata[ j ] = ( byte ) j; 45 } 46 } 47 ; 48 49 54 55 public TestSmallDocumentBlock(String name) 56 { 57 super(name); 58 } 59 60 65 66 public void testConvert1() 67 throws IOException 68 { 69 ByteArrayInputStream stream = new ByteArrayInputStream(_testdata); 70 List documents = new ArrayList(); 71 72 while (true) 73 { 74 DocumentBlock block = new DocumentBlock(stream); 75 76 documents.add(block); 77 if (block.partiallyRead()) 78 { 79 break; 80 } 81 } 82 SmallDocumentBlock[] results = 83 SmallDocumentBlock 84 .convert(( BlockWritable [] ) documents 85 .toArray(new DocumentBlock[ 0 ]), _testdata_size); 86 87 assertEquals("checking correct result size: ", 88 (_testdata_size + 63) / 64, results.length); 89 ByteArrayOutputStream output = new ByteArrayOutputStream(); 90 91 for (int j = 0; j < results.length; j++) 92 { 93 results[ j ].writeBlocks(output); 94 } 95 byte[] output_array = output.toByteArray(); 96 97 assertEquals("checking correct output size: ", 64 * results.length, 98 output_array.length); 99 int index = 0; 100 101 for (; index < _testdata_size; index++) 102 { 103 assertEquals("checking output " + index, _testdata[ index ], 104 output_array[ index ]); 105 } 106 for (; index < output_array.length; index++) 107 { 108 assertEquals("checking output " + index, ( byte ) 0xff, 109 output_array[ index ]); 110 } 111 } 112 113 120 121 public void testConvert2() 122 throws IOException 123 { 124 for (int j = 0; j < 320; j++) 125 { 126 byte[] array = new byte[ j ]; 127 128 for (int k = 0; k < j; k++) 129 { 130 array[ k ] = ( byte ) k; 131 } 132 SmallDocumentBlock[] blocks = SmallDocumentBlock.convert(array, 133 319); 134 135 assertEquals(5, blocks.length); 136 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 137 138 for (int k = 0; k < blocks.length; k++) 139 { 140 blocks[ k ].writeBlocks(stream); 141 } 142 stream.close(); 143 byte[] output = stream.toByteArray(); 144 145 for (int k = 0; k < array.length; k++) 146 { 147 assertEquals(String.valueOf(k), array[ k ], output[ k ]); 148 } 149 for (int k = array.length; k < 320; k++) 150 { 151 assertEquals(String.valueOf(k), ( byte ) 0xFF, output[ k ]); 152 } 153 } 154 } 155 156 161 162 public void testRead() 163 throws IOException 164 { 165 ByteArrayInputStream stream = new ByteArrayInputStream(_testdata); 166 List documents = new ArrayList(); 167 168 while (true) 169 { 170 DocumentBlock block = new DocumentBlock(stream); 171 172 documents.add(block); 173 if (block.partiallyRead()) 174 { 175 break; 176 } 177 } 178 SmallDocumentBlock[] blocks = 179 SmallDocumentBlock 180 .convert(( BlockWritable [] ) documents 181 .toArray(new DocumentBlock[ 0 ]), _testdata_size); 182 183 for (int j = 1; j <= _testdata_size; j += 38) 184 { 185 byte[] buffer = new byte[ j ]; 186 int offset = 0; 187 188 for (int k = 0; k < (_testdata_size / j); k++) 189 { 190 SmallDocumentBlock.read(blocks, buffer, offset); 191 for (int n = 0; n < buffer.length; n++) 192 { 193 assertEquals("checking byte " + (k * j) + n, 194 _testdata[ (k * j) + n ], buffer[ n ]); 195 } 196 offset += j; 197 } 198 } 199 } 200 201 206 207 public void testFill() 208 throws IOException 209 { 210 for (int j = 0; j <= 8; j++) 211 { 212 List foo = new ArrayList(); 213 214 for (int k = 0; k < j; k++) 215 { 216 foo.add(new Object ()); 217 } 218 int result = SmallDocumentBlock.fill(foo); 219 220 assertEquals("correct big block count: ", (j + 7) / 8, result); 221 assertEquals("correct small block count: ", 8 * result, 222 foo.size()); 223 for (int m = j; m < foo.size(); m++) 224 { 225 BlockWritable block = ( BlockWritable ) foo.get(m); 226 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 227 228 block.writeBlocks(stream); 229 byte[] output = stream.toByteArray(); 230 231 assertEquals("correct output size (block[ " + m + " ]): ", 232 64, output.length); 233 for (int n = 0; n < 64; n++) 234 { 235 assertEquals("correct value (block[ " + m + " ][ " + n 236 + " ]): ", ( byte ) 0xff, output[ n ]); 237 } 238 } 239 } 240 } 241 242 245 246 public void testCalcSize() 247 { 248 for (int j = 0; j < 10; j++) 249 { 250 assertEquals("testing " + j, j * 64, 251 SmallDocumentBlock.calcSize(j)); 252 } 253 } 254 255 260 261 public void testExtract() 262 throws IOException 263 { 264 byte[] data = new byte[ 512 ]; 265 int offset = 0; 266 267 for (int j = 0; j < 8; j++) 268 { 269 for (int k = 0; k < 64; k++) 270 { 271 data[ offset++ ] = ( byte ) (k + j); 272 } 273 } 274 RawDataBlock[] blocks = 275 { 276 new RawDataBlock(new ByteArrayInputStream(data)) 277 }; 278 List output = SmallDocumentBlock.extract(blocks); 279 Iterator iter = output.iterator(); 280 281 offset = 0; 282 while (iter.hasNext()) 283 { 284 byte[] out_data = (( SmallDocumentBlock ) iter.next()).getData(); 285 286 assertEquals("testing block at offset " + offset, 64, 287 out_data.length); 288 for (int j = 0; j < out_data.length; j++) 289 { 290 assertEquals("testing byte at offset " + offset, 291 data[ offset ], out_data[ j ]); 292 offset++; 293 } 294 } 295 } 296 297 302 303 public static void main(String [] ignored_args) 304 { 305 System.out.println( 306 "Testing org.apache.poi.poifs.storage.SmallDocumentBlock"); 307 junit.textui.TestRunner.run(TestSmallDocumentBlock.class); 308 } 309 } 310 | Popular Tags |