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.

TestXSSFSheetMergeRegions.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.xssf.usermodel;
  16. import static org.apache.logging.log4j.util.Unbox.box;
  17. import static org.junit.jupiter.api.Assertions.*;
  18. import java.io.IOException;
  19. import java.util.List;
  20. import org.apache.logging.log4j.LogManager;
  21. import org.apache.logging.log4j.Logger;
  22. import org.apache.poi.ss.util.CellRangeAddress;
  23. import org.apache.poi.xssf.XSSFTestDataSamples;
  24. import org.junit.jupiter.api.Test;
  25. class TestXSSFSheetMergeRegions {
  26. private static final Logger LOG = LogManager.getLogger(TestXSSFSheetMergeRegions.class);
  27. @Test
  28. void testMergeRegionsSpeed() throws IOException {
  29. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57893-many-merges.xlsx")) {
  30. long millis = Long.MAX_VALUE;
  31. // in order to reduce the number of false positives we run it a few times before we fail,
  32. // sometimes it fails on machines that are busy at the moment.
  33. for (int i = 0; i < 5; i++) {
  34. millis = runTest(wb);
  35. if (millis < 2000) {
  36. break;
  37. }
  38. LOG.atInfo().log("Retry {} because run-time is too high: {}", box(i),box(millis));
  39. }
  40. boolean inGump = false;
  41. String version = System.getProperty("version.id");
  42. if (version != null && version.startsWith("gump-")) {
  43. inGump = true;
  44. }
  45. // This time is typically ~800ms, versus ~7800ms to iterate getMergedRegion(int).
  46. // when running in Gump, the VM is very slow, so we should allow much more time
  47. assertTrue(inGump ? millis < 8000 : millis < 2000,
  48. "Should have taken <2000 ms to iterate 50k merged regions but took " + millis);
  49. }
  50. }
  51. private long runTest(final XSSFWorkbook wb) {
  52. final long start = System.currentTimeMillis();
  53. final XSSFSheet sheet = wb.getSheetAt(0);
  54. final List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
  55. assertEquals(50000, mergedRegions.size());
  56. for (CellRangeAddress cellRangeAddress : mergedRegions) {
  57. assertEquals(cellRangeAddress.getFirstRow(), cellRangeAddress.getLastRow());
  58. assertEquals(2, cellRangeAddress.getNumberOfCells());
  59. }
  60. return System.currentTimeMillis() - start;
  61. }
  62. }