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.

TestAreaPtg.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.ss.formula.ptg;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import org.apache.poi.hssf.model.HSSFFormulaParser;
  18. import org.apache.poi.ss.SpreadsheetVersion;
  19. import org.apache.poi.ss.util.AreaReference;
  20. import org.junit.jupiter.api.BeforeEach;
  21. import org.junit.jupiter.api.Test;
  22. /**
  23. * Tests for {@link AreaPtg}.
  24. */
  25. final class TestAreaPtg {
  26. AreaPtg relative;
  27. AreaPtg absolute;
  28. @BeforeEach
  29. void setUp() {
  30. short firstRow=5;
  31. short lastRow=13;
  32. short firstCol=7;
  33. short lastCol=17;
  34. relative = new AreaPtg(firstRow,lastRow,firstCol,lastCol,true,true,true,true);
  35. absolute = new AreaPtg(firstRow,lastRow,firstCol,lastCol,false,false,false,false);
  36. }
  37. @Test
  38. void testSortTopLeftToBottomRight() {
  39. AreaPtg ptg = new AreaPtg(new AreaReference("A$1:$B5", SpreadsheetVersion.EXCEL2007));
  40. assertEquals("A$1:$B5", ptg.toFormulaString());
  41. ptg.setFirstColumn(3);
  42. assertEquals("D$1:$B5", ptg.toFormulaString(),
  43. "Area Ptg should not implicitly re-sort itself (except during construction)");
  44. ptg.sortTopLeftToBottomRight();
  45. assertEquals("$B$1:D5", ptg.toFormulaString(),
  46. "Area Ptg should restore itself to top-left to lower-right order when explicitly asked");
  47. }
  48. @Test
  49. void testSetColumnsAbsolute() {
  50. resetColumns(absolute);
  51. validateReference(true, absolute);
  52. }
  53. @Test
  54. void testSetColumnsRelative() {
  55. resetColumns(relative);
  56. validateReference(false, relative);
  57. }
  58. private void validateReference(boolean abs, AreaPtg ref) {
  59. String isWrong = " reference is not "+(abs?"absolute":"relative");
  60. assertEquals(abs,!ref.isFirstColRelative(),"First column" + isWrong);
  61. assertEquals(abs,!ref.isLastColRelative(), "Last column" + isWrong);
  62. assertEquals(abs,!ref.isFirstRowRelative(),"First row" + isWrong);
  63. assertEquals(abs,!ref.isLastRowRelative(), "Last row" + isWrong);
  64. }
  65. private static void resetColumns(AreaPtg aptg) {
  66. int fc = aptg.getFirstColumn();
  67. int lc = aptg.getLastColumn();
  68. aptg.setFirstColumn(fc);
  69. aptg.setLastColumn(lc);
  70. assertEquals(fc , aptg.getFirstColumn() );
  71. assertEquals(lc , aptg.getLastColumn() );
  72. }
  73. @Test
  74. void testAbsoluteRelativeRefs() {
  75. AreaPtg sca1 = new AreaPtg(4, 5, 6, 7, true, false, true, false);
  76. AreaPtg sca2 = new AreaPtg(4, 5, 6, 7, false, true, false, true);
  77. AreaPtg sca3 = new AreaPtg(5, 5, 7, 7, true, false, true, false);
  78. AreaPtg sca4 = new AreaPtg(5, 5, 7, 7, false, true, false, true);
  79. assertEquals("G5:$H$6", sca1.toFormulaString(), "first rel., last abs.");
  80. assertEquals("$G$5:H6", sca2.toFormulaString(), "first abs., last rel.");
  81. assertEquals("H6:$H$6", sca3.toFormulaString(), "first rel., last abs.");
  82. assertEquals("$H$6:H6", sca4.toFormulaString(), "first abs., last rel.");
  83. AreaPtg cla1 = cloneArea(sca1);
  84. AreaPtg cla2 = cloneArea(sca2);
  85. AreaPtg cla3 = cloneArea(sca3);
  86. AreaPtg cla4 = cloneArea(sca4);
  87. assertEquals("G5:$H$6", cla1.toFormulaString(), "first rel., last abs.");
  88. assertEquals("$G$5:H6", cla2.toFormulaString(), "first abs., last rel.");
  89. assertEquals("H6:$H$6", cla3.toFormulaString(), "first rel., last abs.");
  90. assertEquals("$H$6:H6", cla4.toFormulaString(), "first abs., last rel.");
  91. }
  92. private AreaPtg cloneArea(AreaPtg a) {
  93. return new AreaPtg(
  94. a.getFirstRow(), a.getLastRow(), a.getFirstColumn(), a.getLastColumn(),
  95. a.isFirstRowRelative(), a.isLastRowRelative(), a.isFirstColRelative(), a.isLastColRelative()
  96. );
  97. }
  98. @Test
  99. void testFormulaParser() {
  100. String formula1="SUM($E$5:$E$6)";
  101. String expectedFormula1="SUM($F$5:$F$6)";
  102. String newFormula1 = shiftAllColumnsBy1(formula1);
  103. assertEquals(expectedFormula1, newFormula1, "Absolute references changed");
  104. String formula2="SUM(E5:E6)";
  105. String expectedFormula2="SUM(F5:F6)";
  106. String newFormula2 = shiftAllColumnsBy1(formula2);
  107. assertEquals(expectedFormula2, newFormula2, "Relative references changed");
  108. }
  109. private static String shiftAllColumnsBy1(String formula) {
  110. int letUsShiftColumn1By1Column=1;
  111. Ptg[] ptgs = HSSFFormulaParser.parse(formula, null);
  112. for (Ptg ptg : ptgs) {
  113. if (ptg instanceof AreaPtg )
  114. {
  115. AreaPtg aptg = (AreaPtg)ptg;
  116. aptg.setFirstColumn((short)(aptg.getFirstColumn()+letUsShiftColumn1By1Column));
  117. aptg.setLastColumn((short)(aptg.getLastColumn()+letUsShiftColumn1By1Column));
  118. }
  119. }
  120. return HSSFFormulaParser.toFormulaString(null, ptgs);
  121. }
  122. }