1 16 17 18 package org.apache.poi.hpsf.basic; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.File ; 23 import java.io.FileFilter ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.io.PrintWriter ; 30 import java.io.StringWriter ; 31 import java.io.UnsupportedEncodingException ; 32 import java.nio.charset.Charset ; 33 import java.util.Date ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 import junit.framework.Assert; 38 import junit.framework.TestCase; 39 40 import org.apache.poi.hpsf.ClassID; 41 import org.apache.poi.hpsf.Constants; 42 import org.apache.poi.hpsf.HPSFRuntimeException; 43 import org.apache.poi.hpsf.IllegalPropertySetDataException; 44 import org.apache.poi.hpsf.MutableProperty; 45 import org.apache.poi.hpsf.MutablePropertySet; 46 import org.apache.poi.hpsf.MutableSection; 47 import org.apache.poi.hpsf.NoFormatIDException; 48 import org.apache.poi.hpsf.NoPropertySetStreamException; 49 import org.apache.poi.hpsf.PropertySet; 50 import org.apache.poi.hpsf.PropertySetFactory; 51 import org.apache.poi.hpsf.ReadingNotSupportedException; 52 import org.apache.poi.hpsf.Section; 53 import org.apache.poi.hpsf.SummaryInformation; 54 import org.apache.poi.hpsf.UnsupportedVariantTypeException; 55 import org.apache.poi.hpsf.Variant; 56 import org.apache.poi.hpsf.VariantSupport; 57 import org.apache.poi.hpsf.WritingNotSupportedException; 58 import org.apache.poi.hpsf.wellknown.PropertyIDMap; 59 import org.apache.poi.hpsf.wellknown.SectionIDMap; 60 import org.apache.poi.poifs.eventfilesystem.POIFSReader; 61 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent; 62 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener; 63 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 64 import org.apache.poi.util.LittleEndian; 65 import org.apache.poi.util.TempFile; 66 67 68 69 76 public class TestWrite extends TestCase 77 { 78 79 static final String POI_FS = "TestHPSFWritingFunctionality.doc"; 80 81 static final int BYTE_ORDER = 0xfffe; 82 static final int FORMAT = 0x0000; 83 static final int OS_VERSION = 0x00020A04; 84 static final int[] SECTION_COUNT = {1, 2}; 85 static final boolean[] IS_SUMMARY_INFORMATION = {true, false}; 86 static final boolean[] IS_DOCUMENT_SUMMARY_INFORMATION = {false, true}; 87 88 final String IMPROPER_DEFAULT_CHARSET_MESSAGE = 89 "Your default character set is " + getDefaultCharsetName() + 90 ". However, this testcase must be run in an environment " + 91 "with a default character set supporting at least " + 92 "8-bit-characters. You can achieve this by setting the " + 93 "LANG environment variable to a proper value, e.g. " + 94 "\"de_DE\"."; 95 96 POIFile[] poiFiles; 97 98 99 100 105 public TestWrite(final String name) 106 { 107 super(name); 108 } 109 110 111 112 115 public void setUp() 116 { 117 VariantSupport.setLogUnsupportedTypes(false); 118 } 119 120 121 122 130 public void testNoFormatID() 131 throws IOException , UnsupportedVariantTypeException 132 { 133 final String dataDirName = System.getProperty("HPSF.testdata.path"); 134 final File dataDir = new File (dataDirName); 135 final File filename = new File (dataDir, POI_FS); 136 filename.deleteOnExit(); 137 138 140 final OutputStream out = new FileOutputStream (filename); 141 final POIFSFileSystem poiFs = new POIFSFileSystem(); 142 final MutablePropertySet ps = new MutablePropertySet(); 143 ps.clearSections(); 144 ps.addSection(new MutableSection()); 145 146 147 try 148 { 149 final ByteArrayOutputStream psStream = new ByteArrayOutputStream (); 150 ps.write(psStream); 151 psStream.close(); 152 final byte[] streamData = psStream.toByteArray(); 153 poiFs.createDocument(new ByteArrayInputStream (streamData), 154 SummaryInformation.DEFAULT_STREAM_NAME); 155 poiFs.writeFilesystem(out); 156 out.close(); 157 Assert.fail("Should have thrown a NoFormatIDException."); 158 } 159 catch (Exception ex) 160 { 161 Assert.assertTrue(ex instanceof NoFormatIDException); 162 } 163 finally 164 { 165 out.close(); 166 } 167 } 168 169 170 171 179 public void testWriteEmptyPropertySet() 180 throws IOException , UnsupportedVariantTypeException 181 { 182 final File dataDir = 183 new File (System.getProperty("HPSF.testdata.path")); 184 final File filename = new File (dataDir, POI_FS); 185 filename.deleteOnExit(); 186 187 188 final OutputStream out = new FileOutputStream (filename); 189 final POIFSFileSystem poiFs = new POIFSFileSystem(); 190 final MutablePropertySet ps = new MutablePropertySet(); 191 final MutableSection s = (MutableSection) ps.getSections().get(0); 192 s.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID); 193 194 final ByteArrayOutputStream psStream = new ByteArrayOutputStream (); 195 ps.write(psStream); 196 psStream.close(); 197 final byte[] streamData = psStream.toByteArray(); 198 poiFs.createDocument(new ByteArrayInputStream (streamData), 199 SummaryInformation.DEFAULT_STREAM_NAME); 200 poiFs.writeFilesystem(out); 201 out.close(); 202 203 204 final POIFSReader r = new POIFSReader(); 205 r.registerListener(new MyPOIFSReaderListener(), 206 SummaryInformation.DEFAULT_STREAM_NAME); 207 r.read(new FileInputStream (filename)); 208 } 209 210 211 212 220 public void testWriteSimplePropertySet() 221 throws IOException , UnsupportedVariantTypeException 222 { 223 final String AUTHOR = "Rainer Klute"; 224 final String TITLE = "Test Document"; 225 final File dataDir = 226 new File (System.getProperty("HPSF.testdata.path")); 227 final File filename = new File (dataDir, POI_FS); 228 filename.deleteOnExit(); 229 final OutputStream out = new FileOutputStream (filename); 230 final POIFSFileSystem poiFs = new POIFSFileSystem(); 231 232 final MutablePropertySet ps = new MutablePropertySet(); 233 final MutableSection si = new MutableSection(); 234 si.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID); 235 ps.getSections().set(0, si); 236 237 final MutableProperty p = new MutableProperty(); 238 p.setID(PropertyIDMap.PID_AUTHOR); 239 p.setType(Variant.VT_LPWSTR); 240 p.setValue(AUTHOR); 241 si.setProperty(p); 242 si.setProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPSTR, TITLE); 243 244 poiFs.createDocument(ps.toInputStream(), 245 SummaryInformation.DEFAULT_STREAM_NAME); 246 poiFs.writeFilesystem(out); 247 out.close(); 248 249 250 final PropertySet[] psa = new PropertySet[1]; 251 final POIFSReader r = new POIFSReader(); 252 r.registerListener(new POIFSReaderListener() 253 { 254 public void processPOIFSReaderEvent 255 (final POIFSReaderEvent event) 256 { 257 try 258 { 259 psa[0] = PropertySetFactory.create(event.getStream()); 260 } 261 catch (Exception ex) 262 { 263 fail(org.apache.poi.hpsf.Util.toString(ex)); 264 } 265 } 266 267 }, 268 SummaryInformation.DEFAULT_STREAM_NAME); 269 r.read(new FileInputStream (filename)); 270 Assert.assertNotNull(psa[0]); 271 Assert.assertTrue(psa[0].isSummaryInformation()); 272 273 final Section s = (Section) (psa[0].getSections().get(0)); 274 Object p1 = s.getProperty(PropertyIDMap.PID_AUTHOR); 275 Object p2 = s.getProperty(PropertyIDMap.PID_TITLE); 276 Assert.assertEquals(AUTHOR, p1); 277 Assert.assertEquals(TITLE, p2); 278 } 279 280 281 282 290 public void testWriteTwoSections() 291 throws WritingNotSupportedException, IOException 292 { 293 final String STREAM_NAME = "PropertySetStream"; 294 final String SECTION1 = "Section 1"; 295 final String SECTION2 = "Section 2"; 296 297 final File dataDir = 298 new File (System.getProperty("HPSF.testdata.path")); 299 final File filename = new File (dataDir, POI_FS); 300 filename.deleteOnExit(); 301 final OutputStream out = new FileOutputStream (filename); 302 303 final POIFSFileSystem poiFs = new POIFSFileSystem(); 304 final MutablePropertySet ps = new MutablePropertySet(); 305 ps.clearSections(); 306 307 final ClassID formatID = new ClassID(); 308 formatID.setBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 309 8, 9, 10, 11, 12, 13, 14, 15}); 310 final MutableSection s1 = new MutableSection(); 311 s1.setFormatID(formatID); 312 s1.setProperty(2, SECTION1); 313 ps.addSection(s1); 314 315 final MutableSection s2 = new MutableSection(); 316 s2.setFormatID(formatID); 317 s2.setProperty(2, SECTION2); 318 ps.addSection(s2); 319 320 poiFs.createDocument(ps.toInputStream(), STREAM_NAME); 321 poiFs.writeFilesystem(out); 322 out.close(); 323 324 325 final PropertySet[] psa = new PropertySet[1]; 326 final POIFSReader r = new POIFSReader(); 327 r.registerListener(new POIFSReaderListener() 328 { 329 public void processPOIFSReaderEvent 330 (final POIFSReaderEvent event) 331 { 332 try 333 { 334 psa[0] = PropertySetFactory.create(event.getStream()); 335 } 336 catch (Exception ex) 337 { 338 ex.printStackTrace(); 339 throw new RuntimeException (ex.toString()); 340 342 } 344 } 345 }, 346 STREAM_NAME); 347 r.read(new FileInputStream (filename)); 348 Assert.assertNotNull(psa[0]); 349 Section s = (Section) (psa[0].getSections().get(0)); 350 assertEquals(s.getFormatID(), formatID); 351 Object p = s.getProperty(2); 352 Assert.assertEquals(SECTION1, p); 353 s = (Section) (psa[0].getSections().get(1)); 354 p = s.getProperty(2); 355 Assert.assertEquals(SECTION2, p); 356 } 357 358 359 360 static class MyPOIFSReaderListener implements POIFSReaderListener 361 { 362 public void processPOIFSReaderEvent(final POIFSReaderEvent event) 363 { 364 try 365 { 366 PropertySetFactory.create(event.getStream()); 367 } 368 catch (Exception ex) 369 { 370 fail(org.apache.poi.hpsf.Util.toString(ex)); 371 } 372 } 373 } 374 375 376 377 private static final int CODEPAGE_DEFAULT = -1; 378 private static final int CODEPAGE_1252 = 1252; 379 private static final int CODEPAGE_UTF8 = Constants.CP_UTF8; 380 private static final int CODEPAGE_UTF16 = Constants.CP_UTF16; 381 382 383 384 388 public void testVariantTypes() 389 { 390 Throwable t = null; 391 final int codepage = CODEPAGE_DEFAULT; 392 if (!hasProperDefaultCharset()) 393 { 394 System.err.println(IMPROPER_DEFAULT_CHARSET_MESSAGE + 395 " This testcase is skipped."); 396 return; 397 } 398 399 try 400 { 401 check(Variant.VT_EMPTY, null, codepage); 402 check(Variant.VT_BOOL, new Boolean (true), codepage); 403 check(Variant.VT_BOOL, new Boolean (false), codepage); 404 check(Variant.VT_CF, new byte[]{0}, codepage); 405 check(Variant.VT_CF, new byte[]{0, 1}, codepage); 406 check(Variant.VT_CF, new byte[]{0, 1, 2}, codepage); 407 check(Variant.VT_CF, new byte[]{0, 1, 2, 3}, codepage); 408 check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4}, codepage); 409 check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5}, codepage); 410 check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5, 6}, codepage); 411 check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5, 6, 7}, codepage); 412 check(Variant.VT_I2, new Integer (27), codepage); 413 check(Variant.VT_I4, new Long (28), codepage); 414 check(Variant.VT_FILETIME, new Date (), codepage); 415 416 check(Variant.VT_LPSTR, 417 "", codepage); 418 check(Variant.VT_LPSTR, 419 "\u00e4", codepage); 420 check(Variant.VT_LPSTR, 421 "\u00e4\u00f6", codepage); 422 check(Variant.VT_LPSTR, 423 "\u00e4\u00f6\u00fc", codepage); 424 check(Variant.VT_LPSTR, 425 "\u00e4\u00f6\u00fc\u00df", codepage); 426 check(Variant.VT_LPSTR, 427 "\u00e4\u00f6\u00fc\u00df\u00c4", codepage); 428 check(Variant.VT_LPSTR, 429 "\u00e4\u00f6\u00fc\u00df\u00c4\u00d6", codepage); 430 check(Variant.VT_LPSTR, 431 "\u00e4\u00f6\u00fc\u00df\u00c4\u00d6\u00dc", codepage); 432 433 check(Variant.VT_LPWSTR, 434 "", codepage); 435 check(Variant.VT_LPWSTR, 436 "\u00e4", codepage); 437 check(Variant.VT_LPWSTR, 438 "\u00e4\u00f6", codepage); 439 check(Variant.VT_LPWSTR, 440 "\u00e4\u00f6\u00fc", codepage); 441 check(Variant.VT_LPWSTR, 442 "\u00e4\u00f6\u00fc\u00df", codepage); 443 check(Variant.VT_LPWSTR, 444 "\u00e4\u00f6\u00fc\u00df\u00c4", codepage); 445 check(Variant.VT_LPWSTR, 446 "\u00e4\u00f6\u00fc\u00df\u00c4\u00d6", codepage); 447 check(Variant.VT_LPWSTR, 448 "\u00e4\u00f6\u00fc\u00df\u00c4\u00d6\u00dc", codepage); 449 } 450 catch (Exception ex) 451 { 452 t = ex; 453 } 454 catch (Error ex) 455 { 456 t = ex; 457 } 458 if (t != null) 459 fail(org.apache.poi.hpsf.Util.toString(t)); 460 } 461 462 463 464 469 public void testCodepages() 470 { 471 Throwable thr = null; 472 final int[] validCodepages = new int[] 473 {CODEPAGE_DEFAULT, CODEPAGE_UTF8, CODEPAGE_UTF16, CODEPAGE_1252}; 474 for (int i = 0; i < validCodepages.length; i++) 475 { 476 final int cp = validCodepages[i]; 477 if (cp == -1 && !hasProperDefaultCharset()) 478 { 479 System.err.println(IMPROPER_DEFAULT_CHARSET_MESSAGE + 480 " This testcase is skipped for the default codepage."); 481 continue; 482 } 483 484 final long t = cp == CODEPAGE_UTF16 ? Variant.VT_LPWSTR 485 : Variant.VT_LPSTR; 486 try 487 { 488 check(t, "", cp); 489 check(t, "\u00e4", cp); 490 check(t, "\u00e4\u00f6", cp); 491 check(t, "\u00e4\u00f6\u00fc", cp); 492 check(t, "\u00e4\u00f6\u00fc\u00c4", cp); 493 check(t, "\u00e4\u00f6\u00fc\u00c4\u00d6", cp); 494 check(t, "\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc", cp); 495 check(t, "\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df", cp); 496 if (cp == Constants.CP_UTF16 || cp == Constants.CP_UTF8) 497 check(t, "\u79D1\u5B78", cp); 498 } 499 catch (Exception ex) 500 { 501 thr = ex; 502 } 503 catch (Error ex) 504 { 505 thr = ex; 506 } 507 if (thr != null) 508 fail(org.apache.poi.hpsf.Util.toString(thr) + 509 " with codepage " + cp); 510 } 511 512 final int[] invalidCodepages = new int[] {0, 1, 2, 4711, 815}; 513 for (int i = 0; i < invalidCodepages.length; i++) 514 { 515 int cp = invalidCodepages[i]; 516 final long type = cp == CODEPAGE_UTF16 ? Variant.VT_LPWSTR 517 : Variant.VT_LPSTR; 518 try 519 { 520 check(type, "", cp); 521 check(type, "\u00e4", cp); 522 check(type, "\u00e4\u00f6", cp); 523 check(type, "\u00e4\u00f6\u00fc", cp); 524 check(type, "\u00e4\u00f6\u00fc\u00c4", cp); 525 check(type, "\u00e4\u00f6\u00fc\u00c4\u00d6", cp); 526 check(type, "\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc", cp); 527 check(type, "\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df", cp); 528 fail("UnsupportedEncodingException for codepage " + cp + 529 " expected."); 530 } 531 catch (UnsupportedEncodingException ex) 532 { 533 534 } 535 catch (Exception ex) 536 { 537 thr = ex; 538 } 539 catch (Error ex) 540 { 541 thr = ex; 542 } 543 if (thr != null) 544 fail(org.apache.poi.hpsf.Util.toString(thr)); 545 } 546 547 } 548 549 550 551 555 public void testUnicodeWrite8Bit() 556 { 557 final String TITLE = "This is a sample title"; 558 final MutablePropertySet mps = new MutablePropertySet(); 559 final MutableSection ms = (MutableSection) mps.getSections().get(0); 560 ms.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID); 561 final MutableProperty p = new MutableProperty(); 562 p.setID(PropertyIDMap.PID_TITLE); 563 p.setType(Variant.VT_LPSTR); 564 p.setValue(TITLE); 565 ms.setProperty(p); 566 567 Throwable t = null; 568 try 569 { 570 ByteArrayOutputStream out = new ByteArrayOutputStream (); 571 mps.write(out); 572 out.close(); 573 byte[] bytes = out.toByteArray(); 574 575 PropertySet psr = new PropertySet(bytes); 576 assertTrue(psr.isSummaryInformation()); 577 Section sr = (Section) psr.getSections().get(0); 578 String title = (String ) sr.getProperty(PropertyIDMap.PID_TITLE); 579 assertEquals(TITLE, title); 580 } 581 catch (WritingNotSupportedException e) 582 { 583 t = e; 584 } 585 catch (IOException e) 586 { 587 t = e; 588 } 589 catch (NoPropertySetStreamException e) 590 { 591 t = e; 592 } 593 if (t != null) 594 fail(t.getMessage()); 595 } 596 597 598 599 607 private void check(final long variantType, final Object value, 608 final int codepage) 609 throws UnsupportedVariantTypeException, IOException , 610 ReadingNotSupportedException, UnsupportedEncodingException 611 { 612 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 613 VariantSupport.write(out, variantType, value, codepage); 614 out.close(); 615 final byte[] b = out.toByteArray(); 616 final Object objRead = 617 VariantSupport.read(b, 0, b.length + LittleEndian.INT_SIZE, 618 variantType, codepage); 619 if (objRead instanceof byte[]) 620 { 621 final int diff = diff((byte[]) value, (byte[]) objRead); 622 if (diff >= 0) 623 fail("Byte arrays are different. First different byte is at " + 624 "index " + diff + "."); 625 } 626 else 627 if (value != null && !value.equals(objRead)) 628 { 629 fail("Expected: \"" + value + "\" but was: \"" + objRead + 630 "\". Codepage: " + codepage + 631 (codepage == -1 ? 632 " (" + System.getProperty("file.encoding") + ")." : ".")); 633 } 634 else 635 assertEquals(value, objRead); 636 } 637 638 639 640 648 private int diff(final byte[] a, final byte[] b) 649 { 650 final int min = Math.min(a.length, b.length); 651 for (int i = 0; i < min; i++) 652 if (a[i] != b[i]) 653 return i; 654 if (a.length != b.length) 655 return min; 656 return -1; 657 } 658 659 660 661 681 public void testRecreate() 682 { 683 final File dataDir = 684 new File (System.getProperty("HPSF.testdata.path")); 685 final File [] fileList = dataDir.listFiles(new FileFilter () 686 { 687 public boolean accept(final File f) 688 { 689 return f.getName().startsWith("Test"); 690 } 691 }); 692 for (int i = 0; i < fileList.length; i++) 693 testRecreate(fileList[i]); 694 } 695 696 697 698 704 private void testRecreate(final File f) 705 { 706 System.out.println("Recreating file \"" + f + "\""); 707 try 708 { 709 710 final POIFile[] psf1 = Util.readPropertySets(f); 711 712 714 final File copy = TempFile.createTempFile(f.getName(), ""); 715 copy.deleteOnExit(); 716 final OutputStream out = new FileOutputStream (copy); 717 final POIFSFileSystem poiFs = new POIFSFileSystem(); 718 for (int i = 0; i < psf1.length; i++) 719 { 720 final InputStream in = 721 new ByteArrayInputStream (psf1[i].getBytes()); 722 final PropertySet psIn = PropertySetFactory.create(in); 723 final MutablePropertySet psOut = new MutablePropertySet(psIn); 724 final ByteArrayOutputStream psStream = 725 new ByteArrayOutputStream (); 726 psOut.write(psStream); 727 psStream.close(); 728 final byte[] streamData = psStream.toByteArray(); 729 poiFs.createDocument(new ByteArrayInputStream (streamData), 730 psf1[i].getName()); 731 poiFs.writeFilesystem(out); 732 } 733 out.close(); 734 735 736 738 final POIFile[] psf2 = Util.readPropertySets(copy); 739 for (int i = 0; i < psf2.length; i++) 740 { 741 final byte[] bytes1 = psf1[i].getBytes(); 742 final byte[] bytes2 = psf2[i].getBytes(); 743 final InputStream in1 = new ByteArrayInputStream (bytes1); 744 final InputStream in2 = new ByteArrayInputStream (bytes2); 745 final PropertySet ps1 = PropertySetFactory.create(in1); 746 final PropertySet ps2 = PropertySetFactory.create(in2); 747 748 750 assertEquals("Equality for file " + f.getName(), ps1, ps2); 751 } 752 } 753 catch (Exception ex) 754 { 755 handle(ex); 756 } 757 } 758 759 760 761 764 public void testDictionary() 765 { 766 try 767 { 768 final File copy = TempFile.createTempFile("Test-HPSF", "ole2"); 769 copy.deleteOnExit(); 770 771 772 final OutputStream out = new FileOutputStream (copy); 773 final POIFSFileSystem poiFs = new POIFSFileSystem(); 774 final MutablePropertySet ps1 = new MutablePropertySet(); 775 final MutableSection s = (MutableSection) ps1.getSections().get(0); 776 final Map m = new HashMap (3, 1.0f); 777 m.put(new Long (1), "String 1"); 778 m.put(new Long (2), "String 2"); 779 m.put(new Long (3), "String 3"); 780 s.setDictionary(m); 781 s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID); 782 int codepage = Constants.CP_UNICODE; 783 s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, 784 new Integer (codepage)); 785 poiFs.createDocument(ps1.toInputStream(), "Test"); 786 poiFs.writeFilesystem(out); 787 out.close(); 788 789 790 final POIFile[] psf = Util.readPropertySets(copy); 791 Assert.assertEquals(1, psf.length); 792 final byte[] bytes = psf[0].getBytes(); 793 final InputStream in = new ByteArrayInputStream (bytes); 794 final PropertySet ps2 = PropertySetFactory.create(in); 795 796 798 assertTrue(ps2.isDocumentSummaryInformation()); 799 800 802 assertEquals(ps1, ps2); 803 } 804 catch (Exception ex) 805 { 806 handle(ex); 807 } 808 } 809 810 811 812 816 public void testDictionaryWithInvalidCodepage() 817 { 818 try 819 { 820 final File copy = TempFile.createTempFile("Test-HPSF", "ole2"); 821 copy.deleteOnExit(); 822 823 824 final OutputStream out = new FileOutputStream (copy); 825 final POIFSFileSystem poiFs = new POIFSFileSystem(); 826 final MutablePropertySet ps1 = new MutablePropertySet(); 827 final MutableSection s = (MutableSection) ps1.getSections().get(0); 828 final Map m = new HashMap (3, 1.0f); 829 m.put(new Long (1), "String 1"); 830 m.put(new Long (2), "String 2"); 831 m.put(new Long (3), "String 3"); 832 s.setDictionary(m); 833 s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID); 834 int codepage = 12345; 835 s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, 836 new Integer (codepage)); 837 poiFs.createDocument(ps1.toInputStream(), "Test"); 838 poiFs.writeFilesystem(out); 839 out.close(); 840 fail("This testcase did not detect the invalid codepage value."); 841 } 842 catch (IllegalPropertySetDataException ex) 843 { 844 assertTrue(true); 845 } 846 catch (Exception ex) 847 { 848 handle(ex); 849 } 850 } 851 852 853 854 859 private void handle(final Exception ex) 860 { 861 final StringWriter sw = new StringWriter (); 862 final PrintWriter pw = new PrintWriter (sw); 863 Throwable t = ex; 864 while (t != null) 865 { 866 t.printStackTrace(pw); 867 if (t instanceof HPSFRuntimeException) 868 t = ((HPSFRuntimeException) t).getReason(); 869 else 870 t = null; 871 if (t != null) 872 pw.println("Caused by:"); 873 } 874 pw.close(); 875 try 876 { 877 sw.close(); 878 } 879 catch (IOException ex2) 880 { 881 ex.printStackTrace(); 882 } 883 fail(sw.toString()); 884 } 885 886 887 888 893 private String getDefaultCharsetName() 894 { 895 final String charSetName = System.getProperty("file.encoding"); 896 final Charset charSet = Charset.forName(charSetName); 897 return charSet.displayName(); 898 } 899 900 901 902 907 private boolean hasProperDefaultCharset() 908 { 909 final String charSetName = System.getProperty("file.encoding"); 910 final Charset charSet = Charset.forName(charSetName); 911 return charSet.newEncoder().canEncode('\u00e4'); 912 } 913 914 915 916 919 public static void main(final String [] args) throws Throwable 920 { 921 System.setProperty("HPSF.testdata.path", 922 "./src/testcases/org/apache/poi/hpsf/data"); 923 junit.textui.TestRunner.run(TestWrite.class); 924 } 925 926 } 927 | Popular Tags |