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.

OSGiExtractorsIT.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.osgi;
  16. import org.apache.poi.extractor.ExtractorFactory;
  17. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.sl.usermodel.Slide;
  20. import org.apache.poi.sl.usermodel.SlideShow;
  21. import org.apache.poi.sl.usermodel.TextBox;
  22. import org.apache.poi.sl.usermodel.TextShape;
  23. import org.apache.poi.ss.usermodel.Sheet;
  24. import org.apache.poi.ss.usermodel.Workbook;
  25. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  26. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import org.ops4j.pax.exam.junit.PaxExam;
  30. import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
  31. import org.ops4j.pax.exam.spi.reactors.PerClass;
  32. import java.awt.*;
  33. import java.io.ByteArrayInputStream;
  34. import java.io.ByteArrayOutputStream;
  35. import static org.junit.Assert.assertEquals;
  36. /**
  37. * Test to ensure that all our main formats can create, write
  38. * and read back in, when running under OSGi
  39. */
  40. @RunWith(PaxExam.class)
  41. @ExamReactorStrategy(PerClass.class)
  42. public class OSGiExtractorsIT extends BaseOSGiTestCase {
  43. byte[] createSlideShow(SlideShow ppt) throws Exception {
  44. Slide<?, ?> slide = ppt.createSlide();
  45. TextBox<?, ?> box = slide.createTextBox();
  46. box.setTextPlaceholder(TextShape.TextPlaceholder.TITLE);
  47. box.setText("Hello, World!");
  48. box.setAnchor(new Rectangle(36, 15, 648, 65));
  49. ByteArrayOutputStream out = new ByteArrayOutputStream();
  50. ppt.write(out);
  51. return out.toByteArray();
  52. }
  53. byte[] createWorkbook(Workbook wb) throws Exception {
  54. Sheet s = wb.createSheet("OSGi");
  55. s.createRow(0).createCell(0).setCellValue("Hello, World!");
  56. ByteArrayOutputStream out = new ByteArrayOutputStream();
  57. wb.write(out);
  58. return out.toByteArray();
  59. }
  60. /**
  61. * this should invoke OLE2ScratchpadExtractorFactory
  62. */
  63. @Test
  64. public void testHSLF() throws Exception {
  65. byte[] bytes = createSlideShow(new HSLFSlideShow());
  66. ByteArrayInputStream is = new ByteArrayInputStream(bytes);
  67. String text = ExtractorFactory.createExtractor(is).getText().trim();
  68. assertEquals("Hello, World!", text);
  69. }
  70. /**
  71. * this should invoke POIXMLExtractorFactory
  72. */
  73. @Test
  74. public void testXSLF() throws Exception {
  75. byte[] bytes = createSlideShow(new XMLSlideShow());
  76. ByteArrayInputStream is = new ByteArrayInputStream(bytes);
  77. String text = ExtractorFactory.createExtractor(is).getText().trim();
  78. assertEquals("Hello, World!", text);
  79. }
  80. @Test
  81. public void testHSSF() throws Exception {
  82. byte[] bytes = createWorkbook(new HSSFWorkbook());
  83. ByteArrayInputStream is = new ByteArrayInputStream(bytes);
  84. String text = ExtractorFactory.createExtractor(is).getText().trim();
  85. assertEquals("OSGi\nHello, World!", text);
  86. }
  87. /**
  88. * this should invoke POIXMLExtractorFactory
  89. */
  90. @Test
  91. public void testXSSF() throws Exception {
  92. byte[] bytes = createWorkbook(new XSSFWorkbook());
  93. ByteArrayInputStream is = new ByteArrayInputStream(bytes);
  94. String text = ExtractorFactory.createExtractor(is).getText().trim();
  95. assertEquals("OSGi\nHello, World!", text);
  96. }
  97. }