From 60dc4f42954b6cdf5e23a8c1ee0fb86632209e52 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 16 Sep 2020 15:48:27 +0000 Subject: Bug 57857 POI OSGi bundle git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1881771 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/osgi/OSGiSpreadsheetIT.java | 117 +++++++++++++++++++++ .../java/org/apache/poi/osgi/TestOSGiBundle.java | 84 --------------- 2 files changed, 117 insertions(+), 84 deletions(-) create mode 100644 osgi/src/test/java/org/apache/poi/osgi/OSGiSpreadsheetIT.java delete mode 100644 osgi/src/test/java/org/apache/poi/osgi/TestOSGiBundle.java (limited to 'osgi/src') diff --git a/osgi/src/test/java/org/apache/poi/osgi/OSGiSpreadsheetIT.java b/osgi/src/test/java/org/apache/poi/osgi/OSGiSpreadsheetIT.java new file mode 100644 index 0000000000..ea139e7e50 --- /dev/null +++ b/osgi/src/test/java/org/apache/poi/osgi/OSGiSpreadsheetIT.java @@ -0,0 +1,117 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.osgi; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.ops4j.pax.exam.CoreOptions.bundle; +import static org.ops4j.pax.exam.CoreOptions.junitBundles; +import static org.ops4j.pax.exam.CoreOptions.options; + +import javax.inject.Inject; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.util.Arrays; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.Configuration; +import org.ops4j.pax.exam.Option; +import org.ops4j.pax.exam.junit.PaxExam; +import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; +import org.ops4j.pax.exam.spi.reactors.PerMethod; +import org.osgi.framework.BundleContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test to ensure that all our main formats can create, write + * and read back in, when running under OSGi + */ +@RunWith(PaxExam.class) +@ExamReactorStrategy(PerMethod.class) +public class OSGiSpreadsheetIT { + private final static Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + public static final String POI_BUNDLE_SYMBOLIC_NAME = "org.apache.poi.bundle"; + + @Inject + private BundleContext bc; + + @Configuration + public Option[] configuration() throws IOException { + String bundlePath = System.getProperty("bundle.filename"); + return options( + junitBundles(), + bundle(new File(bundlePath).toURI().toURL().toString())); + } + + @Test + public void testBundleLoaded() { + boolean hasBundle = Arrays.stream(bc.getBundles()).anyMatch(b -> + POI_BUNDLE_SYMBOLIC_NAME.equals(b.getSymbolicName())); + assertTrue(POI_BUNDLE_SYMBOLIC_NAME + " not found", hasBundle); + } + + // create a workbook, validate and write back + void testWorkbook(Workbook wb) throws Exception { + logger.info("testing " + wb.getClass().getSimpleName()); + + Sheet s = wb.createSheet("OSGi"); + s.createRow(0).createCell(0).setCellValue("With OSGi"); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + wb.write(baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + + if(wb instanceof HSSFWorkbook) + wb = new HSSFWorkbook(bais); + else + wb = new XSSFWorkbook(bais); + assertEquals(1, wb.getNumberOfSheets()); + + s = wb.getSheet("OSGi"); + assertEquals("With OSGi", s.getRow(0).getCell(0).toString()); + + + } + + @Test + public void testHSSF() throws Exception { + testWorkbook(new HSSFWorkbook()); + } + + @Test + public void testXSSF() throws Exception { + testWorkbook(new XSSFWorkbook()); + } + + @Test + public void testSXSSF() throws Exception { + testWorkbook(new SXSSFWorkbook()); + } +} diff --git a/osgi/src/test/java/org/apache/poi/osgi/TestOSGiBundle.java b/osgi/src/test/java/org/apache/poi/osgi/TestOSGiBundle.java deleted file mode 100644 index 738d96b994..0000000000 --- a/osgi/src/test/java/org/apache/poi/osgi/TestOSGiBundle.java +++ /dev/null @@ -1,84 +0,0 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -==================================================================== */ - -package org.apache.poi.osgi; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.ops4j.pax.exam.CoreOptions.bundle; -import static org.ops4j.pax.exam.CoreOptions.junitBundles; -import static org.ops4j.pax.exam.CoreOptions.options; - -import javax.inject.Inject; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; - -import org.apache.poi.hssf.usermodel.HSSFSheet; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.ops4j.pax.exam.Configuration; -import org.ops4j.pax.exam.Option; -import org.ops4j.pax.exam.junit.PaxExam; -import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; -import org.ops4j.pax.exam.spi.reactors.PerMethod; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; - -/** - * Test to ensure that all our main formats can create, write - * and read back in, when running under OSGi - */ -@RunWith(PaxExam.class) -@ExamReactorStrategy(PerMethod.class) -public class TestOSGiBundle { - - private final File TARGET = new File("target"); - - @Inject - private BundleContext bc; - - @Configuration - public Option[] configuration() throws IOException, URISyntaxException { - File base = new File(TARGET, "test-bundles"); - return options( - junitBundles(), - bundle(new File(base, "poi-bundle.jar").toURI().toURL().toString())); - } - - @Test - public void testHSSF() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet("OSGi"); - s.createRow(0).createCell(0).setCellValue("With OSGi"); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - wb.write(baos); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); - - wb = new HSSFWorkbook(bais); - assertEquals(1, wb.getNumberOfSheets()); - - s = wb.getSheet("OSGi"); - assertEquals("With OSGi", s.getRow(0).getCell(0).toString()); - } -} -- cgit v1.2.3