You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestHPSFPropertiesExtractor.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hpsf.extractor;
  16. import java.io.IOException;
  17. import junit.framework.TestCase;
  18. import org.apache.poi.POIDataSamples;
  19. import org.apache.poi.hpsf.Thumbnail;
  20. import org.apache.poi.hssf.HSSFTestDataSamples;
  21. import org.apache.poi.hssf.extractor.ExcelExtractor;
  22. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  23. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  24. public final class TestHPSFPropertiesExtractor extends TestCase {
  25. private static final POIDataSamples _samples = POIDataSamples.getHPSFInstance();
  26. public void testNormalProperties() throws Exception {
  27. POIFSFileSystem fs = new POIFSFileSystem(_samples.openResourceAsStream("TestMickey.doc"));
  28. HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs);
  29. ext.getText();
  30. // Check each bit in turn
  31. String sinfText = ext.getSummaryInformationText();
  32. String dinfText = ext.getDocumentSummaryInformationText();
  33. assertTrue(sinfText.indexOf("TEMPLATE = Normal") > -1);
  34. assertTrue(sinfText.indexOf("SUBJECT = sample subject") > -1);
  35. assertTrue(dinfText.indexOf("MANAGER = sample manager") > -1);
  36. assertTrue(dinfText.indexOf("COMPANY = sample company") > -1);
  37. // Now overall
  38. String text = ext.getText();
  39. assertTrue(text.indexOf("TEMPLATE = Normal") > -1);
  40. assertTrue(text.indexOf("SUBJECT = sample subject") > -1);
  41. assertTrue(text.indexOf("MANAGER = sample manager") > -1);
  42. assertTrue(text.indexOf("COMPANY = sample company") > -1);
  43. }
  44. public void testNormalUnicodeProperties() throws Exception {
  45. POIFSFileSystem fs = new POIFSFileSystem(_samples.openResourceAsStream("TestUnicode.xls"));
  46. HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs);
  47. ext.getText();
  48. // Check each bit in turn
  49. String sinfText = ext.getSummaryInformationText();
  50. String dinfText = ext.getDocumentSummaryInformationText();
  51. assertTrue(sinfText.indexOf("AUTHOR = marshall") > -1);
  52. assertTrue(sinfText.indexOf("TITLE = Titel: \u00c4h") > -1);
  53. assertTrue(dinfText.indexOf("COMPANY = Schreiner") > -1);
  54. assertTrue(dinfText.indexOf("SCALE = false") > -1);
  55. // Now overall
  56. String text = ext.getText();
  57. assertTrue(text.indexOf("AUTHOR = marshall") > -1);
  58. assertTrue(text.indexOf("TITLE = Titel: \u00c4h") > -1);
  59. assertTrue(text.indexOf("COMPANY = Schreiner") > -1);
  60. assertTrue(text.indexOf("SCALE = false") > -1);
  61. }
  62. public void testCustomProperties() throws Exception {
  63. POIFSFileSystem fs = new POIFSFileSystem(
  64. _samples.openResourceAsStream("TestMickey.doc")
  65. );
  66. HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs);
  67. // Custom properties are part of the document info stream
  68. String dinfText = ext.getDocumentSummaryInformationText();
  69. assertTrue(dinfText.indexOf("Client = sample client") > -1);
  70. assertTrue(dinfText.indexOf("Division = sample division") > -1);
  71. String text = ext.getText();
  72. assertTrue(text.indexOf("Client = sample client") > -1);
  73. assertTrue(text.indexOf("Division = sample division") > -1);
  74. }
  75. public void testConstructors() {
  76. POIFSFileSystem fs;
  77. HSSFWorkbook wb;
  78. try {
  79. fs = new POIFSFileSystem(_samples.openResourceAsStream("TestUnicode.xls"));
  80. wb = new HSSFWorkbook(fs);
  81. } catch (IOException e) {
  82. throw new RuntimeException(e);
  83. }
  84. ExcelExtractor excelExt = new ExcelExtractor(wb);
  85. String fsText = (new HPSFPropertiesExtractor(fs)).getText();
  86. String hwText = (new HPSFPropertiesExtractor(wb)).getText();
  87. String eeText = (new HPSFPropertiesExtractor(excelExt)).getText();
  88. assertEquals(fsText, hwText);
  89. assertEquals(fsText, eeText);
  90. assertTrue(fsText.indexOf("AUTHOR = marshall") > -1);
  91. assertTrue(fsText.indexOf("TITLE = Titel: \u00c4h") > -1);
  92. }
  93. public void test42726() {
  94. HPSFPropertiesExtractor ex = new HPSFPropertiesExtractor(HSSFTestDataSamples.openSampleWorkbook("42726.xls"));
  95. String txt = ex.getText();
  96. assertTrue(txt.indexOf("PID_AUTHOR") != -1);
  97. assertTrue(txt.indexOf("PID_EDITTIME") != -1);
  98. assertTrue(txt.indexOf("PID_REVNUMBER") != -1);
  99. assertTrue(txt.indexOf("PID_THUMBNAIL") != -1);
  100. }
  101. public void testThumbnail() throws Exception {
  102. POIFSFileSystem fs = new POIFSFileSystem(_samples.openResourceAsStream("TestThumbnail.xls"));
  103. HSSFWorkbook wb = new HSSFWorkbook(fs);
  104. Thumbnail thumbnail = new Thumbnail(wb.getSummaryInformation().getThumbnail());
  105. assertEquals(-1, thumbnail.getClipboardFormatTag());
  106. assertEquals(3, thumbnail.getClipboardFormat());
  107. assertNotNull(thumbnail.getThumbnailAsWMF());
  108. wb.close();
  109. }
  110. }