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 TestBATBlock 34 extends TestCase 35 { 36 37 42 43 public TestBATBlock(String name) 44 { 45 super(name); 46 } 47 48 56 57 public void testCreateBATBlocks() 58 throws IOException 59 { 60 61 BATBlock[] rvalue = BATBlock.createBATBlocks(createTestArray(0)); 63 64 assertEquals(0, rvalue.length); 65 66 rvalue = BATBlock.createBATBlocks(createTestArray(1)); 68 assertEquals(1, rvalue.length); 69 verifyContents(rvalue, 1); 70 71 rvalue = BATBlock.createBATBlocks(createTestArray(127)); 73 assertEquals(1, rvalue.length); 74 verifyContents(rvalue, 127); 75 76 rvalue = BATBlock.createBATBlocks(createTestArray(128)); 78 assertEquals(1, rvalue.length); 79 verifyContents(rvalue, 128); 80 81 rvalue = BATBlock.createBATBlocks(createTestArray(129)); 83 assertEquals(2, rvalue.length); 84 verifyContents(rvalue, 129); 85 } 86 87 private int [] createTestArray(int count) 88 { 89 int[] rvalue = new int[ count ]; 90 91 for (int j = 0; j < count; j++) 92 { 93 rvalue[ j ] = j; 94 } 95 return rvalue; 96 } 97 98 private void verifyContents(BATBlock [] blocks, int entries) 99 throws IOException 100 { 101 byte[] expected = new byte[ 512 * blocks.length ]; 102 103 Arrays.fill(expected, ( byte ) 0xFF); 104 int offset = 0; 105 106 for (int j = 0; j < entries; j++) 107 { 108 expected[ offset++ ] = ( byte ) j; 109 expected[ offset++ ] = 0; 110 expected[ offset++ ] = 0; 111 expected[ offset++ ] = 0; 112 } 113 ByteArrayOutputStream stream = new ByteArrayOutputStream(512 114 * blocks.length); 115 116 for (int j = 0; j < blocks.length; j++) 117 { 118 blocks[ j ].writeBlocks(stream); 119 } 120 byte[] actual = stream.toByteArray(); 121 122 assertEquals(expected.length, actual.length); 123 for (int j = 0; j < expected.length; j++) 124 { 125 assertEquals(expected[ j ], actual[ j ]); 126 } 127 } 128 129 134 135 public void testCreateXBATBlocks() 136 throws IOException 137 { 138 139 BATBlock[] rvalue = BATBlock.createXBATBlocks(createTestArray(0), 1); 141 142 assertEquals(0, rvalue.length); 143 144 rvalue = BATBlock.createXBATBlocks(createTestArray(1), 1); 146 assertEquals(1, rvalue.length); 147 verifyXBATContents(rvalue, 1, 1); 148 149 rvalue = BATBlock.createXBATBlocks(createTestArray(127), 1); 151 assertEquals(1, rvalue.length); 152 verifyXBATContents(rvalue, 127, 1); 153 154 rvalue = BATBlock.createXBATBlocks(createTestArray(128), 1); 156 assertEquals(2, rvalue.length); 157 verifyXBATContents(rvalue, 128, 1); 158 159 rvalue = BATBlock.createXBATBlocks(createTestArray(254), 1); 161 assertEquals(2, rvalue.length); 162 verifyXBATContents(rvalue, 254, 1); 163 164 rvalue = BATBlock.createXBATBlocks(createTestArray(255), 1); 166 assertEquals(3, rvalue.length); 167 verifyXBATContents(rvalue, 255, 1); 168 } 169 170 private void verifyXBATContents(BATBlock [] blocks, int entries, 171 int start_block) 172 throws IOException 173 { 174 byte[] expected = new byte[ 512 * blocks.length ]; 175 176 Arrays.fill(expected, ( byte ) 0xFF); 177 int offset = 0; 178 179 for (int j = 0; j < entries; j++) 180 { 181 if ((j % 127) == 0) 182 { 183 if (j != 0) 184 { 185 offset += 4; 186 } 187 } 188 expected[ offset++ ] = ( byte ) j; 189 expected[ offset++ ] = 0; 190 expected[ offset++ ] = 0; 191 expected[ offset++ ] = 0; 192 } 193 for (int j = 0; j < (blocks.length - 1); j++) 194 { 195 offset = 508 + (j * 512); 196 expected[ offset++ ] = ( byte ) (start_block + j + 1); 197 expected[ offset++ ] = 0; 198 expected[ offset++ ] = 0; 199 expected[ offset++ ] = 0; 200 } 201 offset = (blocks.length * 512) - 4; 202 expected[ offset++ ] = ( byte ) -2; 203 expected[ offset++ ] = ( byte ) -1; 204 expected[ offset++ ] = ( byte ) -1; 205 expected[ offset++ ] = ( byte ) -1; 206 ByteArrayOutputStream stream = new ByteArrayOutputStream(512 207 * blocks.length); 208 209 for (int j = 0; j < blocks.length; j++) 210 { 211 blocks[ j ].writeBlocks(stream); 212 } 213 byte[] actual = stream.toByteArray(); 214 215 assertEquals(expected.length, actual.length); 216 for (int j = 0; j < expected.length; j++) 217 { 218 assertEquals("offset " + j, expected[ j ], actual[ j ]); 219 } 220 } 221 222 225 226 public void testCalculateXBATStorageRequirements() 227 { 228 int[] blockCounts = 229 { 230 0, 1, 127, 128 231 }; 232 int[] requirements = 233 { 234 0, 1, 1, 2 235 }; 236 237 for (int j = 0; j < blockCounts.length; j++) 238 { 239 assertEquals( 240 "requirement for " + blockCounts[ j ], requirements[ j ], 241 BATBlock.calculateXBATStorageRequirements(blockCounts[ j ])); 242 } 243 } 244 245 248 249 public void testEntriesPerBlock() 250 { 251 assertEquals(128, BATBlock.entriesPerBlock()); 252 } 253 254 257 258 public void testEntriesPerXBATBlock() 259 { 260 assertEquals(127, BATBlock.entriesPerXBATBlock()); 261 } 262 263 266 267 public void testGetXBATChainOffset() 268 { 269 assertEquals(508, BATBlock.getXBATChainOffset()); 270 } 271 272 277 278 public static void main(String [] ignored_args) 279 { 280 System.out.println("Testing org.apache.poi.poifs.storage.BATBlock"); 281 junit.textui.TestRunner.run(TestBATBlock.class); 282 } 283 } 284 | Popular Tags |