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.

TestRVA.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.hssf.model;
  16. import static org.junit.jupiter.api.Assertions.assertFalse;
  17. import static org.junit.jupiter.api.Assertions.assertTrue;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.stream.Stream;
  21. import org.apache.poi.hssf.HSSFTestDataSamples;
  22. import org.apache.poi.hssf.usermodel.FormulaExtractor;
  23. import org.apache.poi.hssf.usermodel.HSSFCell;
  24. import org.apache.poi.hssf.usermodel.HSSFRow;
  25. import org.apache.poi.hssf.usermodel.HSSFSheet;
  26. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  27. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  28. import org.apache.poi.ss.formula.ptg.AttrPtg;
  29. import org.apache.poi.ss.formula.ptg.Ptg;
  30. import org.apache.poi.ss.usermodel.CellType;
  31. import org.junit.jupiter.api.AfterAll;
  32. import org.junit.jupiter.params.ParameterizedTest;
  33. import org.junit.jupiter.params.provider.Arguments;
  34. import org.junit.jupiter.params.provider.MethodSource;
  35. /**
  36. * Tests 'operand class' transformation performed by
  37. * {@code OperandClassTransformer} by comparing its results with those
  38. * directly produced by Excel (in a sample spreadsheet).
  39. */
  40. final class TestRVA {
  41. private static final String NEW_LINE = System.getProperty("line.separator");
  42. private static POIFSFileSystem poifs;
  43. private static HSSFWorkbook workbook;
  44. @AfterAll
  45. public static void closeResource() throws Exception {
  46. workbook.close();
  47. poifs.close();
  48. }
  49. public static Stream<Arguments> data() throws Exception {
  50. poifs = new POIFSFileSystem(HSSFTestDataSamples.getSampleFile("testRVA.xls"), true);
  51. workbook = new HSSFWorkbook(poifs);
  52. HSSFSheet sheet = workbook.getSheetAt(0);
  53. List<Arguments> data = new ArrayList<>();
  54. for (int rowIdx = 0; true; rowIdx++) {
  55. HSSFRow row = sheet.getRow(rowIdx);
  56. if (row == null) {
  57. break;
  58. }
  59. HSSFCell cell = row.getCell(0);
  60. if (cell == null || cell.getCellType() == CellType.BLANK) {
  61. break;
  62. }
  63. String formula = cell.getCellFormula();
  64. data.add(Arguments.of(cell,formula));
  65. }
  66. return data.stream();
  67. }
  68. @ParameterizedTest
  69. @MethodSource("data")
  70. void confirmCell(HSSFCell formulaCell, String formula) {
  71. Ptg[] excelPtgs = FormulaExtractor.getPtgs(formulaCell);
  72. Ptg[] poiPtgs = HSSFFormulaParser.parse(formula, workbook);
  73. int nExcelTokens = excelPtgs.length;
  74. int nPoiTokens = poiPtgs.length;
  75. if (nExcelTokens != nPoiTokens) {
  76. assertTrue(nExcelTokens == nPoiTokens + 1 && excelPtgs[0].getClass() == AttrPtg.class,
  77. "Expected " + nExcelTokens + " tokens but got " + nPoiTokens);
  78. // compensate for missing tAttrVolatile, which belongs in any formula
  79. // involving OFFSET() et al. POI currently does not insert where required
  80. Ptg[] temp = new Ptg[nExcelTokens];
  81. temp[0] = excelPtgs[0];
  82. System.arraycopy(poiPtgs, 0, temp, 1, nPoiTokens);
  83. poiPtgs = temp;
  84. }
  85. boolean hasMismatch = false;
  86. StringBuilder sb = new StringBuilder();
  87. for (int i = 0; i < nExcelTokens; i++) {
  88. Ptg poiPtg = poiPtgs[i];
  89. Ptg excelPtg = excelPtgs[i];
  90. if (excelPtg.getClass() != poiPtg.getClass()) {
  91. hasMismatch = true;
  92. sb.append(" mismatch token type[").append(i).append("] ").append(getShortClassName(excelPtg)).append(" ").append(excelPtg.getRVAType()).append(" - ").append(getShortClassName(poiPtg)).append(" ").append(poiPtg.getRVAType());
  93. sb.append(NEW_LINE);
  94. continue;
  95. }
  96. if (poiPtg.isBaseToken()) {
  97. continue;
  98. }
  99. sb.append(" token[").append(i).append("] ").append(excelPtg).append(" ").append(excelPtg.getRVAType());
  100. if (excelPtg.getPtgClass() != poiPtg.getPtgClass()) {
  101. hasMismatch = true;
  102. sb.append(" - was ").append(poiPtg.getRVAType());
  103. }
  104. sb.append(NEW_LINE);
  105. }
  106. assertFalse(hasMismatch);
  107. }
  108. private String getShortClassName(Object o) {
  109. String cn = o.getClass().getName();
  110. int pos = cn.lastIndexOf('.');
  111. return cn.substring(pos + 1);
  112. }
  113. }