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.

TestXMLPropertiesTextExtractor.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.ooxml;
  16. import org.apache.poi.POIDataSamples;
  17. import org.apache.poi.ooxml.extractor.POIXMLPropertiesTextExtractor;
  18. import org.apache.poi.ooxml.util.PackageHelper;
  19. import org.apache.poi.openxml4j.opc.OPCPackage;
  20. import org.apache.poi.xslf.usermodel.XSLFSlideShow;
  21. import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
  22. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  23. import org.junit.jupiter.api.Test;
  24. import static org.apache.poi.POITestCase.assertContains;
  25. import static org.junit.jupiter.api.Assertions.assertEquals;
  26. import static org.junit.jupiter.api.Assertions.assertFalse;
  27. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  28. import static org.junit.jupiter.api.Assertions.assertNotNull;
  29. import static org.junit.jupiter.api.Assertions.assertTrue;
  30. public final class TestXMLPropertiesTextExtractor {
  31. private static final POIDataSamples _ssSamples = POIDataSamples.getSpreadSheetInstance();
  32. private static final POIDataSamples _slSamples = POIDataSamples.getSlideShowInstance();
  33. @Test
  34. void testGetFromMainExtractor() throws Exception {
  35. OPCPackage pkg = PackageHelper.open(_ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm"));
  36. XSSFWorkbook wb = new XSSFWorkbook(pkg);
  37. XSSFExcelExtractor ext = new XSSFExcelExtractor(wb);
  38. POIXMLPropertiesTextExtractor textExt = ext.getMetadataTextExtractor();
  39. // Check basics
  40. assertNotNull(textExt);
  41. assertTrue(textExt.getText().length() > 0);
  42. // Check some of the content
  43. String text = textExt.getText();
  44. String cText = textExt.getCorePropertiesText();
  45. assertContains(text, "LastModifiedBy = Yury Batrakov");
  46. assertContains(cText, "LastModifiedBy = Yury Batrakov");
  47. textExt.close();
  48. ext.close();
  49. }
  50. @Test
  51. void testCore() throws Exception {
  52. OPCPackage pkg = PackageHelper.open(
  53. _ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm")
  54. );
  55. XSSFWorkbook wb = new XSSFWorkbook(pkg);
  56. POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(wb);
  57. ext.getText();
  58. // Now check
  59. String text = ext.getText();
  60. String cText = ext.getCorePropertiesText();
  61. assertContains(text, "LastModifiedBy = Yury Batrakov");
  62. assertContains(cText, "LastModifiedBy = Yury Batrakov");
  63. ext.close();
  64. }
  65. @Test
  66. void testExtended() throws Exception {
  67. OPCPackage pkg = OPCPackage.open(
  68. _ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm")
  69. );
  70. XSSFWorkbook wb = new XSSFWorkbook(pkg);
  71. POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(wb);
  72. ext.getText();
  73. // Now check
  74. String text = ext.getText();
  75. String eText = ext.getExtendedPropertiesText();
  76. assertContains(text, "Application = Microsoft Excel");
  77. assertContains(text, "Company = Mera");
  78. assertContains(eText, "Application = Microsoft Excel");
  79. assertContains(eText, "Company = Mera");
  80. ext.close();
  81. }
  82. @Test
  83. void testCustom() throws Exception {
  84. OPCPackage pkg = OPCPackage.open(
  85. _ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm")
  86. );
  87. XSSFWorkbook wb = new XSSFWorkbook(pkg);
  88. POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(wb);
  89. ext.getText();
  90. // Now check
  91. String text = ext.getText();
  92. String cText = ext.getCustomPropertiesText();
  93. assertContains(text, "description = another value");
  94. assertContains(cText, "description = another value");
  95. ext.close();
  96. }
  97. /**
  98. * Bug #49386 - some properties, especially
  99. * dates can be null
  100. */
  101. @Test
  102. void testWithSomeNulls() throws Exception {
  103. try (
  104. OPCPackage pkg = OPCPackage.open(
  105. _slSamples.openResourceAsStream("49386-null_dates.pptx")
  106. );
  107. XSLFSlideShow sl = new XSLFSlideShow(pkg);
  108. POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(sl)
  109. ) {
  110. String text = ext.getText();
  111. assertFalse(text.contains("Created =")); // With date is null
  112. assertContains(text, "CreatedString = "); // Via string is blank
  113. assertContains(text, "LastModifiedBy = IT Client Services");
  114. }
  115. }
  116. /**
  117. * Bug #65946 - a bug led to Category being added twice
  118. */
  119. @Test
  120. void testCategoryProperty() throws Exception {
  121. try (
  122. OPCPackage pkg = OPCPackage.open(
  123. _slSamples.openResourceAsStream("rain.pptx")
  124. );
  125. XSLFSlideShow sl = new XSLFSlideShow(pkg);
  126. POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(sl)
  127. ) {
  128. String text = ext.getText();
  129. int idx0 = text.indexOf("Category =");
  130. assertNotEquals(-1, idx0);
  131. int idx1 = text.indexOf("Category =", idx0 + 1);
  132. assertEquals(-1, idx1);
  133. assertContains(text, "Category = rain"); // Via string is blank
  134. }
  135. }
  136. }