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.

SpreadsheetHandler.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 static org.junit.jupiter.api.Assertions.assertNotNull;
  17. import java.io.IOException;
  18. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  19. import org.apache.poi.ss.extractor.EmbeddedData;
  20. import org.apache.poi.ss.extractor.EmbeddedExtractor;
  21. import org.apache.poi.ss.usermodel.Cell;
  22. import org.apache.poi.ss.usermodel.Name;
  23. import org.apache.poi.ss.usermodel.Row;
  24. import org.apache.poi.ss.usermodel.Sheet;
  25. import org.apache.poi.ss.usermodel.Workbook;
  26. import org.apache.poi.ss.usermodel.WorkbookFactory;
  27. import org.apache.poi.util.RecordFormatException;
  28. import org.apache.poi.xssf.usermodel.XSSFChartSheet;
  29. public abstract class SpreadsheetHandler extends AbstractFileHandler {
  30. public void handleWorkbook(Workbook wb) throws IOException {
  31. // try to access some of the content
  32. readContent(wb);
  33. // write out the file
  34. writeToArray(wb);
  35. // access some more content (we had cases where writing corrupts the data in memory)
  36. readContent(wb);
  37. // write once more
  38. UnsynchronizedByteArrayOutputStream out = writeToArray(wb);
  39. // read in the written file
  40. Workbook read = WorkbookFactory.create(out.toInputStream());
  41. assertNotNull(read);
  42. readContent(read);
  43. extractEmbedded(read);
  44. modifyContent(read);
  45. read.close();
  46. }
  47. private UnsynchronizedByteArrayOutputStream writeToArray(Workbook wb) throws IOException {
  48. UnsynchronizedByteArrayOutputStream stream = new UnsynchronizedByteArrayOutputStream();
  49. wb.write(stream);
  50. return stream;
  51. }
  52. private void readContent(Workbook wb) {
  53. for(int i = 0;i < wb.getNumberOfSheets();i++) {
  54. Sheet sheet = wb.getSheetAt(i);
  55. assertNotNull(wb.getSheet(sheet.getSheetName()));
  56. sheet.groupColumn((short) 4, (short) 5);
  57. sheet.setColumnGroupCollapsed(4, true);
  58. sheet.setColumnGroupCollapsed(4, false);
  59. // don't do this for very large sheets as it will take a long time
  60. if(sheet.getPhysicalNumberOfRows() > 1000) {
  61. continue;
  62. }
  63. for(Row row : sheet) {
  64. for(Cell cell : row) {
  65. assertNotNull(cell.toString());
  66. }
  67. }
  68. }
  69. for (Name name : wb.getAllNames()) {
  70. // this sometimes caused exceptions
  71. if(!name.isFunctionName()) {
  72. name.getRefersToFormula();
  73. }
  74. }
  75. }
  76. private void extractEmbedded(Workbook wb) throws IOException {
  77. EmbeddedExtractor ee = new EmbeddedExtractor();
  78. for (Sheet s : wb) {
  79. for (EmbeddedData ed : ee.extractAll(s)) {
  80. assertNotNull(ed.getFilename());
  81. assertNotNull(ed.getEmbeddedData());
  82. assertNotNull(ed.getShape());
  83. }
  84. }
  85. }
  86. private void modifyContent(Workbook wb) {
  87. /* a number of file fail because of various things: udf, unimplemented functions, ...
  88. we would need quite a list of excludes and the large regression tests would probably
  89. take a lot longer to run...
  90. try {
  91. // try to re-compute all formulas to find cases where parsing fails
  92. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  93. } catch (RuntimeException e) {
  94. // only allow a specific exception which indicates that an external
  95. // reference was not found
  96. if(!e.getMessage().contains("Could not resolve external workbook name")) {
  97. throw e;
  98. }
  99. }*/
  100. for (int i=wb.getNumberOfSheets()-1; i>=0; i--) {
  101. if(wb.getSheetAt(i) instanceof XSSFChartSheet) {
  102. // clone for chart-sheets is not supported
  103. continue;
  104. }
  105. try {
  106. wb.cloneSheet(i);
  107. } catch (RecordFormatException e) {
  108. if (e.getCause() instanceof CloneNotSupportedException) {
  109. // ignore me
  110. continue;
  111. }
  112. throw e;
  113. } catch (RuntimeException e) {
  114. if ("Could not find 'internal references' EXTERNALBOOK".equals(e.getMessage()) ||
  115. "CountryRecord not found".equals(e.getMessage()) ||
  116. "CountryRecord or SSTRecord not found".equals(e.getMessage()) ||
  117. "Cannot add more than 65535 shapes".equals(e.getMessage()) ) {
  118. // ignore these here for now
  119. continue;
  120. }
  121. throw e;
  122. }
  123. }
  124. }
  125. }