1 2 17 18 19 package org.apache.poi.poifs.property; 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 29 34 35 public class TestDirectoryProperty 36 extends TestCase 37 { 38 private DirectoryProperty _property; 39 private byte[] _testblock; 40 41 46 47 public TestDirectoryProperty(String name) 48 { 49 super(name); 50 } 51 52 57 58 public void testConstructor() 59 throws IOException 60 { 61 createBasicDirectoryProperty(); 62 verifyProperty(); 63 } 64 65 70 71 public void testPreWrite() 72 throws IOException 73 { 74 createBasicDirectoryProperty(); 75 _property.preWrite(); 76 77 verifyProperty(); 79 verifyChildren(0); 80 81 createBasicDirectoryProperty(); 83 _property.addChild(new LocalProperty(1)); 84 _property.preWrite(); 85 86 _testblock[ 0x4C ] = 1; 88 _testblock[ 0x4D ] = 0; 89 _testblock[ 0x4E ] = 0; 90 _testblock[ 0x4F ] = 0; 91 verifyProperty(); 92 verifyChildren(1); 93 94 createBasicDirectoryProperty(); 96 _property.addChild(new LocalProperty(1)); 97 _property.addChild(new LocalProperty(2)); 98 _property.preWrite(); 99 100 _testblock[ 0x4C ] = 2; 102 _testblock[ 0x4D ] = 0; 103 _testblock[ 0x4E ] = 0; 104 _testblock[ 0x4F ] = 0; 105 verifyProperty(); 106 verifyChildren(2); 107 108 for (int count = 1; count < 100; count++) 110 { 111 createBasicDirectoryProperty(); 112 for (int j = 1; j < (count + 1); j++) 113 { 114 _property.addChild(new LocalProperty(j)); 115 } 116 _property.preWrite(); 117 verifyChildren(count); 118 } 119 } 120 121 private void verifyChildren(int count) 122 throws IOException 123 { 124 Iterator iter = _property.getChildren(); 125 List children = new ArrayList(); 126 127 while (iter.hasNext()) 128 { 129 children.add(iter.next()); 130 } 131 assertEquals(count, children.size()); 132 if (count != 0) 133 { 134 boolean[] found = new boolean[ count ]; 135 136 found[ _property.getChildIndex() - 1 ] = true; 137 int total_found = 1; 138 139 Arrays.fill(found, false); 140 iter = children.iterator(); 141 while (iter.hasNext()) 142 { 143 Property child = ( Property ) iter.next(); 144 Child next = child.getNextChild(); 145 146 if (next != null) 147 { 148 int index = (( Property ) next).getIndex(); 149 150 if (index != -1) 151 { 152 assertTrue("found index " + index + " twice", 153 !found[ index - 1 ]); 154 found[ index - 1 ] = true; 155 total_found++; 156 } 157 } 158 Child previous = child.getPreviousChild(); 159 160 if (previous != null) 161 { 162 int index = (( Property ) previous).getIndex(); 163 164 if (index != -1) 165 { 166 assertTrue("found index " + index + " twice", 167 !found[ index - 1 ]); 168 found[ index - 1 ] = true; 169 total_found++; 170 } 171 } 172 } 173 assertEquals(count, total_found); 174 } 175 } 176 177 private void createBasicDirectoryProperty() 178 { 179 String name = "MyDirectory"; 180 181 _property = new DirectoryProperty(name); 182 _testblock = new byte[ 128 ]; 183 int index = 0; 184 185 for (; index < 0x40; index++) 186 { 187 _testblock[ index ] = ( byte ) 0; 188 } 189 int limit = Math.min(31, name.length()); 190 191 _testblock[ index++ ] = ( byte ) (2 * (limit + 1)); 192 _testblock[ index++ ] = ( byte ) 0; 193 _testblock[ index++ ] = ( byte ) 1; 194 _testblock[ index++ ] = ( byte ) 1; 195 for (; index < 0x50; index++) 196 { 197 _testblock[ index ] = ( byte ) 0xff; 198 } 199 for (; index < 0x80; index++) 200 { 201 _testblock[ index ] = ( byte ) 0; 202 } 203 byte[] name_bytes = name.getBytes(); 204 205 for (index = 0; index < limit; index++) 206 { 207 _testblock[ index * 2 ] = name_bytes[ index ]; 208 } 209 } 210 211 private void verifyProperty() 212 throws IOException 213 { 214 ByteArrayOutputStream stream = new ByteArrayOutputStream(512); 215 216 _property.writeData(stream); 217 byte[] output = stream.toByteArray(); 218 219 assertEquals(_testblock.length, output.length); 220 for (int j = 0; j < _testblock.length; j++) 221 { 222 assertEquals("mismatch at offset " + j, _testblock[ j ], 223 output[ j ]); 224 } 225 } 226 227 232 233 public void testAddChild() 234 throws IOException 235 { 236 createBasicDirectoryProperty(); 237 _property.addChild(new LocalProperty(1)); 238 _property.addChild(new LocalProperty(2)); 239 try 240 { 241 _property.addChild(new LocalProperty(1)); 242 fail("should have caught IOException"); 243 } 244 catch (IOException ignored) 245 { 246 247 } 249 try 250 { 251 _property.addChild(new LocalProperty(2)); 252 fail("should have caught IOException"); 253 } 254 catch (IOException ignored) 255 { 256 257 } 259 _property.addChild(new LocalProperty(3)); 260 } 261 262 267 268 public void testDeleteChild() 269 throws IOException 270 { 271 createBasicDirectoryProperty(); 272 Property p1 = new LocalProperty(1); 273 274 _property.addChild(p1); 275 try 276 { 277 _property.addChild(new LocalProperty(1)); 278 fail("should have caught IOException"); 279 } 280 catch (IOException ignored) 281 { 282 283 } 285 assertTrue(_property.deleteChild(p1)); 286 assertTrue(!_property.deleteChild(p1)); 287 _property.addChild(new LocalProperty(1)); 288 } 289 290 295 296 public void testChangeName() 297 throws IOException 298 { 299 createBasicDirectoryProperty(); 300 Property p1 = new LocalProperty(1); 301 String originalName = p1.getName(); 302 303 _property.addChild(p1); 304 assertTrue(_property.changeName(p1, "foobar")); 305 assertEquals("foobar", p1.getName()); 306 assertTrue(!_property.changeName(p1, "foobar")); 307 assertEquals("foobar", p1.getName()); 308 Property p2 = new LocalProperty(1); 309 310 _property.addChild(p2); 311 assertTrue(!_property.changeName(p1, originalName)); 312 assertTrue(_property.changeName(p2, "foo")); 313 assertTrue(_property.changeName(p1, originalName)); 314 } 315 316 321 322 public void testReadingConstructor() 323 throws IOException 324 { 325 byte[] input = 326 { 327 ( byte ) 0x42, ( byte ) 0x00, ( byte ) 0x6F, ( byte ) 0x00, 328 ( byte ) 0x6F, ( byte ) 0x00, ( byte ) 0x74, ( byte ) 0x00, 329 ( byte ) 0x20, ( byte ) 0x00, ( byte ) 0x45, ( byte ) 0x00, 330 ( byte ) 0x6E, ( byte ) 0x00, ( byte ) 0x74, ( byte ) 0x00, 331 ( byte ) 0x72, ( byte ) 0x00, ( byte ) 0x79, ( byte ) 0x00, 332 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 333 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 334 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 335 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 336 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 337 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 338 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 339 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 340 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 341 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 342 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 343 ( byte ) 0x16, ( byte ) 0x00, ( byte ) 0x01, ( byte ) 0x01, 344 ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, 345 ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, 346 ( byte ) 0x02, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 347 ( byte ) 0x20, ( byte ) 0x08, ( byte ) 0x02, ( byte ) 0x00, 348 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 349 ( byte ) 0xC0, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 350 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x46, 351 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 352 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 353 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 354 ( byte ) 0xC0, ( byte ) 0x5C, ( byte ) 0xE8, ( byte ) 0x23, 355 ( byte ) 0x9E, ( byte ) 0x6B, ( byte ) 0xC1, ( byte ) 0x01, 356 ( byte ) 0xFE, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, 357 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, 358 ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00 359 }; 360 361 verifyReadingProperty(0, input, 0, "Boot Entry"); 362 } 363 364 private void verifyReadingProperty(int index, byte [] input, int offset, 365 String name) 366 throws IOException 367 { 368 DirectoryProperty property = new DirectoryProperty(index, input, 369 offset); 370 ByteArrayOutputStream stream = new ByteArrayOutputStream(128); 371 byte[] expected = new byte[ 128 ]; 372 373 System.arraycopy(input, offset, expected, 0, 128); 374 property.writeData(stream); 375 byte[] output = stream.toByteArray(); 376 377 assertEquals(128, output.length); 378 for (int j = 0; j < 128; j++) 379 { 380 assertEquals("mismatch at offset " + j, expected[ j ], 381 output[ j ]); 382 } 383 assertEquals(index, property.getIndex()); 384 assertEquals(name, property.getName()); 385 assertTrue(!property.getChildren().hasNext()); 386 } 387 388 393 394 public static void main(String [] ignored_args) 395 { 396 System.out.println( 397 "Testing org.apache.poi.poifs.property.DirectoryProperty"); 398 junit.textui.TestRunner.run(TestDirectoryProperty.class); 399 } 400 } 401 | Popular Tags |