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.3KB

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