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 import org.apache.poi.util.LittleEndian; 28 import org.apache.poi.util.LittleEndianConsts; 29 30 35 36 public class TestBlockListImpl 37 extends TestCase 38 { 39 40 45 46 public TestBlockListImpl(String name) 47 { 48 super(name); 49 } 50 51 56 57 public void testZap() 58 throws IOException 59 { 60 BlockListImpl list = new BlockListImpl(); 61 62 for (int j = -2; j < 10; j++) 64 { 65 list.zap(j); 66 } 67 RawDataBlock[] blocks = new RawDataBlock[ 5 ]; 68 69 for (int j = 0; j < 5; j++) 70 { 71 blocks[ j ] = 72 new RawDataBlock(new ByteArrayInputStream(new byte[ 512 ])); 73 } 74 list.setBlocks(blocks); 75 for (int j = -2; j < 10; j++) 76 { 77 list.zap(j); 78 } 79 80 for (int j = 0; j < 5; j++) 82 { 83 try 84 { 85 list.remove(j); 86 fail("removing item " + j + " should not have succeeded"); 87 } 88 catch (IOException ignored) 89 { 90 } 91 } 92 } 93 94 99 100 public void testRemove() 101 throws IOException 102 { 103 BlockListImpl list = new BlockListImpl(); 104 RawDataBlock[] blocks = new RawDataBlock[ 5 ]; 105 byte[] data = new byte[ 512 * 5 ]; 106 107 for (int j = 0; j < 5; j++) 108 { 109 Arrays.fill(data, j * 512, (j * 512) + 512, ( byte ) j); 110 } 111 ByteArrayInputStream stream = new ByteArrayInputStream(data); 112 113 for (int j = 0; j < 5; j++) 114 { 115 blocks[ j ] = new RawDataBlock(stream); 116 } 117 list.setBlocks(blocks); 118 119 for (int j = -2; j < 10; j++) 121 { 122 if ((j < 0) || (j >= 5)) 123 { 124 try 125 { 126 list.remove(j); 127 fail("removing item " + j + " should have failed"); 128 } 129 catch (IOException ignored) 130 { 131 } 132 } 133 } 134 135 for (int j = 0; j < 5; j++) 137 { 138 byte[] output = list.remove(j).getData(); 139 140 for (int k = 0; k < 512; k++) 141 { 142 assertEquals("testing block " + j + ", index " + k, 143 data[ (j * 512) + k ], output[ k ]); 144 } 145 } 146 147 for (int j = 0; j < 5; j++) 149 { 150 try 151 { 152 list.remove(j); 153 fail("removing item " + j + " should not have succeeded"); 154 } 155 catch (IOException ignored) 156 { 157 } 158 } 159 } 160 161 166 167 public void testSetBAT() 168 throws IOException 169 { 170 BlockListImpl list = new BlockListImpl(); 171 172 list.setBAT(null); 173 list.setBAT(new BlockAllocationTableReader()); 174 try 175 { 176 list.setBAT(new BlockAllocationTableReader()); 177 fail("second attempt should have failed"); 178 } 179 catch (IOException ignored) 180 { 181 } 182 } 183 184 189 190 public void testFetchBlocks() 191 throws IOException 192 { 193 194 BlockListImpl list = new BlockListImpl(); 207 List raw_blocks = new ArrayList(); 208 byte[] data = new byte[ 512 ]; 209 int offset = 0; 210 211 LittleEndian.putInt(data, offset, -3); offset += LittleEndianConsts.INT_SIZE; 213 214 LittleEndian.putInt(data, offset, -2); 217 offset += LittleEndianConsts.INT_SIZE; 218 219 LittleEndian.putInt(data, offset, 2); 221 offset += LittleEndianConsts.INT_SIZE; 222 223 LittleEndian.putInt(data, offset, 4); 225 offset += LittleEndianConsts.INT_SIZE; 226 LittleEndian.putInt(data, offset, 1); 227 offset += LittleEndianConsts.INT_SIZE; 228 229 LittleEndian.putInt(data, offset, 6); 231 offset += LittleEndianConsts.INT_SIZE; 232 LittleEndian.putInt(data, offset, -1); 233 offset += LittleEndianConsts.INT_SIZE; 234 235 LittleEndian.putInt(data, offset, 8); 237 offset += LittleEndianConsts.INT_SIZE; 238 LittleEndian.putInt(data, offset, 0); 239 offset += LittleEndianConsts.INT_SIZE; 240 241 LittleEndian.putInt(data, offset, 10); 243 offset += LittleEndianConsts.INT_SIZE; 244 LittleEndian.putInt(data, offset, -4); 245 offset += LittleEndianConsts.INT_SIZE; 246 247 LittleEndian.putInt(data, offset, 1000); 249 offset += LittleEndianConsts.INT_SIZE; 250 251 int index = 13; 253 254 for (; offset < 508; offset += LittleEndianConsts.INT_SIZE) 255 { 256 LittleEndian.putInt(data, offset, index++); 257 } 258 LittleEndian.putInt(data, offset, -2); 259 raw_blocks.add(new RawDataBlock(new ByteArrayInputStream(data))); 260 for (int j = raw_blocks.size(); j < 128; j++) 261 { 262 raw_blocks.add( 263 new RawDataBlock(new ByteArrayInputStream(new byte[ 0 ]))); 264 } 265 list.setBlocks(( RawDataBlock [] ) raw_blocks 266 .toArray(new RawDataBlock[ 0 ])); 267 int[] blocks = 268 { 269 0 270 }; 271 BlockAllocationTableReader table = 272 new BlockAllocationTableReader(1, blocks, 0, -2, list); 273 int[] start_blocks = 274 { 275 -2, 1, 2, 3, 5, 7, 9, 11, 12 276 }; 277 int[] expected_length = 278 { 279 0, 1, -1, -1, -1, -1, -1, -1, 116 280 }; 281 282 for (int j = 0; j < start_blocks.length; j++) 283 { 284 try 285 { 286 ListManagedBlock[] dataBlocks = 287 list.fetchBlocks(start_blocks[ j ]); 288 289 if (expected_length[ j ] == -1) 290 { 291 fail("document " + j + " should have failed"); 292 } 293 else 294 { 295 assertEquals(expected_length[ j ], dataBlocks.length); 296 } 297 } 298 catch (IOException e) 299 { 300 if (expected_length[ j ] == -1) 301 { 302 303 } 305 else 306 { 307 throw e; 308 } 309 } 310 } 311 } 312 313 318 319 public static void main(String [] ignored_args) 320 { 321 System.out 322 .println("Testing org.apache.poi.poifs.storage.BlockListImpl"); 323 junit.textui.TestRunner.run(TestBlockListImpl.class); 324 } 325 } 326 | Popular Tags |