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.

XSSFBFileHandler.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.stress;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import org.apache.poi.openxml4j.opc.OPCPackage;
  22. import org.apache.poi.openxml4j.opc.PackageAccess;
  23. import org.apache.poi.util.IOUtils;
  24. import org.apache.poi.xssf.XLSBUnsupportedException;
  25. import org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor;
  26. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  27. import org.junit.jupiter.api.Test;
  28. class XSSFBFileHandler extends AbstractFileHandler {
  29. static {
  30. //add expected failures here:
  31. AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.add("spreadsheet/protected_passtika.xlsb");
  32. }
  33. @Override
  34. public void handleFile(InputStream stream, String path) throws Exception {
  35. byte[] bytes = IOUtils.toByteArray(stream);
  36. try (OPCPackage opcPackage = OPCPackage.open(new ByteArrayInputStream(bytes))) {
  37. testOne(opcPackage);
  38. }
  39. testNotHandledByWorkbookException(OPCPackage.open(new ByteArrayInputStream(bytes)));
  40. }
  41. private void testNotHandledByWorkbookException(OPCPackage pkg) throws IOException {
  42. try {
  43. new XSSFWorkbook(pkg).close();
  44. } catch (XLSBUnsupportedException e) {
  45. //this is what we'd expect
  46. //swallow
  47. }
  48. }
  49. @Override
  50. public void handleExtracting(File file) throws Exception {
  51. OPCPackage pkg = OPCPackage.open(file, PackageAccess.READ);
  52. try {
  53. testOne(pkg);
  54. } finally {
  55. pkg.close();
  56. }
  57. pkg = OPCPackage.open(file, PackageAccess.READ);
  58. try {
  59. testNotHandledByWorkbookException(pkg);
  60. } finally {
  61. pkg.close();
  62. }
  63. }
  64. private void testOne(OPCPackage pkg) throws Exception {
  65. XSSFBEventBasedExcelExtractor ex = new XSSFBEventBasedExcelExtractor(pkg);
  66. String txt = ex.getText();
  67. if (txt.length() < 1) {
  68. throw new RuntimeException("Should have gotten some text.");
  69. }
  70. }
  71. @Test
  72. void testLocal() throws Exception {
  73. File file = new File("test-data/spreadsheet/Simple.xlsb");
  74. try (FileInputStream stream = new FileInputStream(file)) {
  75. handleFile(stream, file.getPath());
  76. }
  77. handleExtracting(file);
  78. }
  79. }