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.

TestReferencePtg.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.assertArrayEquals;
  17. import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
  18. import static org.junit.jupiter.api.Assertions.assertEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  20. import java.io.IOException;
  21. import org.apache.poi.hssf.HSSFTestDataSamples;
  22. import org.apache.poi.hssf.record.TestcaseRecordInputStream;
  23. import org.apache.poi.hssf.usermodel.HSSFSheet;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.apache.poi.util.LittleEndianInput;
  26. import org.junit.jupiter.api.Test;
  27. /**
  28. * Tests for {@link RefPtg}.
  29. */
  30. final class TestReferencePtg {
  31. /**
  32. * Tests reading a file containing this ptg.
  33. */
  34. @Test
  35. void testReading() {
  36. HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("ReferencePtg.xls");
  37. HSSFSheet sheet = workbook.getSheetAt(0);
  38. // First row
  39. assertEquals(55.0, sheet.getRow(0).getCell(0).getNumericCellValue(), 0.0,
  40. "Wrong numeric value for original number");
  41. assertEquals(55.0, sheet.getRow(0).getCell(1).getNumericCellValue(), 0.0,
  42. "Wrong numeric value for referemce");
  43. assertEquals("A1", sheet.getRow(0).getCell(1).getCellFormula(), "Wrong formula string for reference");
  44. // Now moving over the 2**15 boundary
  45. // (Remember that excel row (n) is poi row (n-1)
  46. assertEquals(32767.0, sheet.getRow(32766).getCell(0).getNumericCellValue(), 0.0, "Wrong numeric value for original number");
  47. assertEquals(32767.0, sheet.getRow(32766).getCell(1).getNumericCellValue(), 0.0, "Wrong numeric value for referemce");
  48. assertEquals("A32767", sheet.getRow(32766).getCell(1).getCellFormula(), "Wrong formula string for reference");
  49. assertEquals(32768.0, sheet.getRow(32767).getCell(0).getNumericCellValue(), 0.0, "Wrong numeric value for original number");
  50. assertEquals(32768.0, sheet.getRow(32767).getCell(1).getNumericCellValue(), 0.0, "Wrong numeric value for referemce");
  51. assertEquals("A32768", sheet.getRow(32767).getCell(1).getCellFormula(), "Wrong formula string for reference");
  52. assertEquals(32769.0, sheet.getRow(32768).getCell(0).getNumericCellValue(), 0.0, "Wrong numeric value for original number");
  53. assertEquals(32769.0, sheet.getRow(32768).getCell(1).getNumericCellValue(), 0.0, "Wrong numeric value for referemce");
  54. assertEquals("A32769", sheet.getRow(32768).getCell(1).getCellFormula(), "Wrong formula string for reference");
  55. assertEquals(32770.0, sheet.getRow(32769).getCell(0).getNumericCellValue(), 0.0, "Wrong numeric value for original number");
  56. assertEquals(32770.0, sheet.getRow(32769).getCell(1).getNumericCellValue(), 0.0, "Wrong numeric value for referemce");
  57. assertEquals("A32770", sheet.getRow(32769).getCell(1).getCellFormula(), "Wrong formula string for reference");
  58. }
  59. @Test
  60. void testBug44921() throws IOException {
  61. try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex44921-21902.xls")) {
  62. assertDoesNotThrow(() -> HSSFTestDataSamples.writeOutAndReadBack(wb));
  63. }
  64. }
  65. private static final byte[] tRefN_data = {
  66. 0x2C, 33, 44, 55, 66,
  67. };
  68. @Test
  69. void testReadWrite_tRefN_bug45091() {
  70. LittleEndianInput in = TestcaseRecordInputStream.createLittleEndian(tRefN_data);
  71. Ptg[] ptgs = Ptg.readTokens(tRefN_data.length, in);
  72. byte[] outData = new byte[5];
  73. Ptg.serializePtgs(ptgs, outData, 0);
  74. assertNotEquals(0x24, outData[0], "Identified bug 45091");
  75. assertArrayEquals(tRefN_data, outData);
  76. }
  77. /**
  78. * test that RefPtgBase can handle references with column index greater than 255,
  79. * see Bugzilla 50096
  80. */
  81. @Test
  82. void testColumnGreater255() {
  83. RefPtgBase ptg;
  84. ptg = new RefPtg("IW1");
  85. assertEquals(256, ptg.getColumn());
  86. assertEquals("IW1", ptg.formatReferenceAsString());
  87. ptg = new RefPtg("JA1");
  88. assertEquals(260, ptg.getColumn());
  89. assertEquals("JA1", ptg.formatReferenceAsString());
  90. }
  91. }