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.

TestXSLFPowerPointExtractor.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.xslf.extractor;
  16. import org.apache.poi.POIDataSamples;
  17. import org.apache.poi.openxml4j.opc.OPCPackage;
  18. import org.apache.poi.xslf.XSLFSlideShow;
  19. import junit.framework.TestCase;
  20. /**
  21. * Tests for HXFPowerPointExtractor
  22. */
  23. public class TestXSLFPowerPointExtractor extends TestCase {
  24. /**
  25. * A simple file
  26. */
  27. private XSLFSlideShow xmlA;
  28. private OPCPackage pkg;
  29. protected void setUp() throws Exception {
  30. POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  31. pkg = OPCPackage.open(slTests.openResourceAsStream("sample.pptx"));
  32. xmlA = new XSLFSlideShow(pkg);
  33. }
  34. /**
  35. * Get text out of the simple file
  36. */
  37. public void testGetSimpleText() throws Exception {
  38. new XSLFPowerPointExtractor(xmlA);
  39. new XSLFPowerPointExtractor(pkg);
  40. XSLFPowerPointExtractor extractor =
  41. new XSLFPowerPointExtractor(xmlA);
  42. extractor.getText();
  43. String text = extractor.getText();
  44. assertTrue(text.length() > 0);
  45. // Check Basics
  46. assertTrue(text.startsWith("Lorem ipsum dolor sit amet\n"));
  47. assertTrue(text.endsWith("amet\n\n"));
  48. // Just slides, no notes
  49. text = extractor.getText(true, false);
  50. assertEquals(
  51. "Lorem ipsum dolor sit amet\n" +
  52. "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
  53. "\n" +
  54. "Lorem ipsum dolor sit amet\n" +
  55. "Lorem\n" +
  56. "ipsum\n" +
  57. "dolor\n" +
  58. "sit\n" +
  59. "amet\n" +
  60. "\n", text
  61. );
  62. // Just notes, no slides
  63. text = extractor.getText(false, true);
  64. assertEquals(
  65. "\n\n\n\n", text
  66. );
  67. // Both
  68. text = extractor.getText(true, true);
  69. assertEquals(
  70. "Lorem ipsum dolor sit amet\n" +
  71. "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
  72. "\n\n\n" +
  73. "Lorem ipsum dolor sit amet\n" +
  74. "Lorem\n" +
  75. "ipsum\n" +
  76. "dolor\n" +
  77. "sit\n" +
  78. "amet\n" +
  79. "\n\n\n", text
  80. );
  81. // Via set defaults
  82. extractor.setSlidesByDefault(false);
  83. extractor.setNotesByDefault(true);
  84. text = extractor.getText();
  85. assertEquals(
  86. "\n\n\n\n", text
  87. );
  88. }
  89. public void testGetComments() throws Exception {
  90. POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  91. xmlA = new XSLFSlideShow(OPCPackage.open(slTests.openResourceAsStream("45545_Comment.pptx")));
  92. XSLFPowerPointExtractor extractor =
  93. new XSLFPowerPointExtractor(xmlA);
  94. String text = extractor.getText();
  95. assertTrue(text.length() > 0);
  96. // Check comments are there
  97. assertTrue("Unable to find expected word in text\n" + text, text.contains("testdoc"));
  98. }
  99. public void testTable() throws Exception {
  100. POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  101. xmlA = new XSLFSlideShow(OPCPackage.open(slTests.openResourceAsStream("present1.pptx")));
  102. XSLFPowerPointExtractor extractor =
  103. new XSLFPowerPointExtractor(xmlA);
  104. String text = extractor.getText();
  105. assertTrue(text.length() > 0);
  106. // Check comments are there
  107. assertTrue("Unable to find expected word in text\n" + text, text.contains("TEST"));
  108. }
  109. }