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.

OSGiSpreadsheetIT.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertTrue;
  18. import static org.ops4j.pax.exam.CoreOptions.bundle;
  19. import static org.ops4j.pax.exam.CoreOptions.junitBundles;
  20. import static org.ops4j.pax.exam.CoreOptions.options;
  21. import javax.inject.Inject;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.lang.invoke.MethodHandles;
  27. import java.util.Arrays;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.ss.usermodel.Sheet;
  30. import org.apache.poi.ss.usermodel.Workbook;
  31. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  32. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  33. import org.junit.Test;
  34. import org.junit.runner.RunWith;
  35. import org.ops4j.pax.exam.Configuration;
  36. import org.ops4j.pax.exam.Option;
  37. import org.ops4j.pax.exam.junit.PaxExam;
  38. import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
  39. import org.ops4j.pax.exam.spi.reactors.PerMethod;
  40. import org.osgi.framework.BundleContext;
  41. import org.slf4j.Logger;
  42. import org.slf4j.LoggerFactory;
  43. /**
  44. * Test to ensure that all our main formats can create, write
  45. * and read back in, when running under OSGi
  46. */
  47. @RunWith(PaxExam.class)
  48. @ExamReactorStrategy(PerMethod.class)
  49. public class OSGiSpreadsheetIT {
  50. private final static Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
  51. public static final String POI_BUNDLE_SYMBOLIC_NAME = "org.apache.poi.bundle";
  52. @Inject
  53. private BundleContext bc;
  54. @Configuration
  55. public Option[] configuration() throws IOException {
  56. String bundlePath = System.getProperty("bundle.filename");
  57. return options(
  58. junitBundles(),
  59. bundle(new File(bundlePath).toURI().toURL().toString()));
  60. }
  61. @Test
  62. public void testBundleLoaded() {
  63. boolean hasBundle = Arrays.stream(bc.getBundles()).anyMatch(b ->
  64. POI_BUNDLE_SYMBOLIC_NAME.equals(b.getSymbolicName()));
  65. assertTrue(POI_BUNDLE_SYMBOLIC_NAME + " not found", hasBundle);
  66. }
  67. // create a workbook, validate and write back
  68. void testWorkbook(Workbook wb) throws Exception {
  69. logger.info("testing " + wb.getClass().getSimpleName());
  70. Sheet s = wb.createSheet("OSGi");
  71. s.createRow(0).createCell(0).setCellValue("With OSGi");
  72. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  73. wb.write(baos);
  74. ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  75. if(wb instanceof HSSFWorkbook)
  76. wb = new HSSFWorkbook(bais);
  77. else
  78. wb = new XSSFWorkbook(bais);
  79. assertEquals(1, wb.getNumberOfSheets());
  80. s = wb.getSheet("OSGi");
  81. assertEquals("With OSGi", s.getRow(0).getCell(0).toString());
  82. }
  83. @Test
  84. public void testHSSF() throws Exception {
  85. testWorkbook(new HSSFWorkbook());
  86. }
  87. @Test
  88. public void testXSSF() throws Exception {
  89. testWorkbook(new XSSFWorkbook());
  90. }
  91. @Test
  92. public void testSXSSF() throws Exception {
  93. testWorkbook(new SXSSFWorkbook());
  94. }
  95. }