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.poifs.common.POIFSConstants; 28 import org.apache.poi.util.LittleEndian; 29 import org.apache.poi.util.LittleEndianConsts; 30 31 36 37 public class TestBlockAllocationTableWriter 38 extends TestCase 39 { 40 41 46 47 public TestBlockAllocationTableWriter(String name) 48 { 49 super(name); 50 } 51 52 55 56 public void testAllocateSpace() 57 { 58 BlockAllocationTableWriter table = 59 new BlockAllocationTableWriter(); 60 int[] blockSizes = 61 { 62 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 63 }; 64 int expectedIndex = 0; 65 66 for (int j = 0; j < blockSizes.length; j++) 67 { 68 assertEquals(expectedIndex, table.allocateSpace(blockSizes[ j ])); 69 expectedIndex += blockSizes[ j ]; 70 } 71 } 72 73 78 79 public void testCreateBlocks() 80 throws IOException 81 { 82 BlockAllocationTableWriter table = new BlockAllocationTableWriter(); 83 84 table.allocateSpace(127); 85 table.createBlocks(); 86 verifyBlocksCreated(table, 1); 87 table = new BlockAllocationTableWriter(); 88 table.allocateSpace(128); 89 table.createBlocks(); 90 verifyBlocksCreated(table, 2); 91 table = new BlockAllocationTableWriter(); 92 table.allocateSpace(254); 93 table.createBlocks(); 94 verifyBlocksCreated(table, 2); 95 table = new BlockAllocationTableWriter(); 96 table.allocateSpace(255); 97 table.createBlocks(); 98 verifyBlocksCreated(table, 3); 99 table = new BlockAllocationTableWriter(); 100 table.allocateSpace(13843); 101 table.createBlocks(); 102 verifyBlocksCreated(table, 109); 103 table = new BlockAllocationTableWriter(); 104 table.allocateSpace(13844); 105 table.createBlocks(); 106 verifyBlocksCreated(table, 110); 107 table = new BlockAllocationTableWriter(); 108 table.allocateSpace(13969); 109 table.createBlocks(); 110 verifyBlocksCreated(table, 110); 111 table = new BlockAllocationTableWriter(); 112 table.allocateSpace(13970); 113 table.createBlocks(); 114 verifyBlocksCreated(table, 111); 115 } 116 117 122 123 public void testProduct() 124 throws IOException 125 { 126 BlockAllocationTableWriter table = new BlockAllocationTableWriter(); 127 128 for (int k = 1; k <= 22; k++) 129 { 130 table.allocateSpace(k); 131 } 132 table.createBlocks(); 133 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 134 135 table.writeBlocks(stream); 136 byte[] output = stream.toByteArray(); 137 138 assertEquals(1024, output.length); 139 byte[] expected = new byte[ 1024 ]; 140 141 Arrays.fill(expected, ( byte ) 0xFF); 142 int offset = 0; 143 int block_index = 1; 144 145 for (int k = 1; k <= 22; k++) 146 { 147 int limit = k - 1; 148 149 for (int j = 0; j < limit; j++) 150 { 151 LittleEndian.putInt(expected, offset, block_index++); 152 offset += LittleEndianConsts.INT_SIZE; 153 } 154 LittleEndian.putInt(expected, offset, 155 POIFSConstants.END_OF_CHAIN); 156 offset += 4; 157 block_index++; 158 } 159 160 LittleEndian.putInt(expected, offset, block_index++); 162 offset += LittleEndianConsts.INT_SIZE; 163 LittleEndian.putInt(expected, offset, POIFSConstants.END_OF_CHAIN); 164 for (int k = 0; k < expected.length; k++) 165 { 166 assertEquals("At offset " + k, expected[ k ], output[ k ]); 167 } 168 } 169 170 private void verifyBlocksCreated(BlockAllocationTableWriter table, 171 int count) 172 throws IOException 173 { 174 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 175 176 table.writeBlocks(stream); 177 byte[] output = stream.toByteArray(); 178 179 assertEquals(count * 512, output.length); 180 } 181 182 187 188 public static void main(String [] ignored_args) 189 { 190 System.out.println( 191 "Testing org.apache.poi.poifs.storage.BlockAllocationTableWriter"); 192 junit.textui.TestRunner.run(TestBlockAllocationTableWriter.class); 193 } 194 } 195 | Popular Tags |