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.

TestLinkTable.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertNull;
  20. import static org.junit.jupiter.api.Assertions.assertSame;
  21. import static org.junit.jupiter.api.Assertions.assertTrue;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.LinkedHashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import org.apache.poi.hssf.HSSFTestDataSamples;
  28. import org.apache.poi.hssf.record.BOFRecord;
  29. import org.apache.poi.hssf.record.CountryRecord;
  30. import org.apache.poi.hssf.record.EOFRecord;
  31. import org.apache.poi.hssf.record.ExternSheetRecord;
  32. import org.apache.poi.hssf.record.ExternalNameRecord;
  33. import org.apache.poi.hssf.record.NameCommentRecord;
  34. import org.apache.poi.hssf.record.NameRecord;
  35. import org.apache.poi.hssf.record.Record;
  36. import org.apache.poi.hssf.record.SSTRecord;
  37. import org.apache.poi.hssf.record.SupBookRecord;
  38. import org.apache.poi.hssf.usermodel.HSSFCell;
  39. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  40. import org.apache.poi.ss.formula.ptg.NameXPtg;
  41. import org.junit.jupiter.api.Test;
  42. /**
  43. * Tests for {@link LinkTable}
  44. */
  45. final class TestLinkTable {
  46. /**
  47. * The example file attached to bugzilla 45046 is a clear example of Name records being present
  48. * without an External Book (SupBook) record. Excel has no trouble reading this file.<br>
  49. * TODO get OOO documentation updated to reflect this (that EXTERNALBOOK is optional).
  50. *
  51. * It's not clear what exact steps need to be taken in Excel to create such a workbook
  52. */
  53. @Test
  54. void testLinkTableWithoutExternalBookRecord_bug45046() {
  55. // Bug 45046 b: DEFINEDNAME is part of LinkTable
  56. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex45046-21984.xls");
  57. // some other sanity checks
  58. assertEquals(3, wb.getNumberOfSheets());
  59. String formula = wb.getSheetAt(0).getRow(4).getCell(13).getCellFormula();
  60. // The reported symptom of this bugzilla is an earlier bug (already fixed)
  61. // This is observable in version 3.0
  62. assertNotEquals("ipcSummenproduktIntern($P5,N$6,$A$9,N$5)", formula);
  63. assertEquals("ipcSummenproduktIntern($C5,N$2,$A$9,N$1)", formula);
  64. }
  65. @Test
  66. void testMultipleExternSheetRecords_bug45698() {
  67. // Bug: Extern sheet is part of LinkTable
  68. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex45698-22488.xls");
  69. // some other sanity checks
  70. assertEquals(7, wb.getNumberOfSheets());
  71. }
  72. @Test
  73. void testExtraSheetRefs_bug45978() {
  74. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex45978-extraLinkTableSheets.xls");
  75. /*
  76. ex45978-extraLinkTableSheets.xls is a cut-down version of attachment 22561.
  77. The original file produces the same error.
  78. This bug was caused by a combination of invalid sheet indexes in the EXTERNSHEET
  79. record, and eager initialisation of the extern sheet references. Note - the workbook
  80. has 2 sheets, but the EXTERNSHEET record refers to sheet indexes 0, 1 and 2.
  81. Offset 0x3954 (14676)
  82. recordid = 0x17, size = 32
  83. [EXTERNSHEET]
  84. numOfRefs = 5
  85. refrec #0: extBook=0 firstSheet=0 lastSheet=0
  86. refrec #1: extBook=1 firstSheet=2 lastSheet=2
  87. refrec #2: extBook=2 firstSheet=1 lastSheet=1
  88. refrec #3: extBook=0 firstSheet=-1 lastSheet=-1
  89. refrec #4: extBook=0 firstSheet=1 lastSheet=1
  90. [/EXTERNSHEET]
  91. As it turns out, the formula in question doesn't even use externSheetIndex #1 - it
  92. uses #4, which resolves to sheetIndex 1 -> 'Data'.
  93. It is not clear exactly what externSheetIndex #4 would refer to. Excel seems to
  94. display such a formula as "''!$A2", but then complains of broken link errors.
  95. */
  96. HSSFCell cell = wb.getSheetAt(0).getRow(1).getCell(1);
  97. // Bug: IndexOutOfBoundsException - Index: 2, Size: 2
  98. String cellFormula = cell.getCellFormula();
  99. assertEquals("Data!$A2", cellFormula);
  100. }
  101. /**
  102. * This problem was visible in POI svn r763332
  103. * when reading the workbook of attachment 23468 from bugzilla 47001
  104. */
  105. @Test
  106. void testMissingExternSheetRecord_bug47001b() {
  107. Record[] recs = {
  108. SupBookRecord.createAddInFunctions(),
  109. new SSTRecord(),
  110. };
  111. List<org.apache.poi.hssf.record.Record> recList = Arrays.asList(recs);
  112. WorkbookRecordList wrl = new WorkbookRecordList();
  113. // Bug 47001b: Expected an EXTERNSHEET record but got (org.apache.poi.hssf.record.SSTRecord)
  114. LinkTable lt = new LinkTable(recList, 0, wrl, Collections.emptyMap());
  115. assertNotNull(lt);
  116. }
  117. @Test
  118. void testNameCommentRecordBetweenNameRecords() {
  119. final Record[] recs = {
  120. new NameRecord(),
  121. new NameCommentRecord("name1", "comment1"),
  122. new NameRecord(),
  123. new NameCommentRecord("name2", "comment2"),
  124. };
  125. final List<org.apache.poi.hssf.record.Record> recList = Arrays.asList(recs);
  126. final WorkbookRecordList wrl = new WorkbookRecordList();
  127. final Map<String, NameCommentRecord> commentRecords = new LinkedHashMap<>();
  128. final LinkTable lt = new LinkTable(recList, 0, wrl, commentRecords);
  129. assertNotNull(lt);
  130. assertEquals(2, commentRecords.size());
  131. assertSame(recs[1], commentRecords.get("name1")); //== is intentionally not .equals()!
  132. assertSame(recs[3], commentRecords.get("name2")); //== is intentionally not .equals()!
  133. assertEquals(2, lt.getNumNames());
  134. }
  135. @Test
  136. void testAddNameX(){
  137. WorkbookRecordList wrl = new WorkbookRecordList();
  138. wrl.add(0, new BOFRecord());
  139. wrl.add(1, new CountryRecord());
  140. wrl.add(2, EOFRecord.instance);
  141. int numberOfSheets = 3;
  142. LinkTable tbl = new LinkTable(numberOfSheets, wrl);
  143. // creation of a new LinkTable insert two new records: SupBookRecord followed by ExternSheetRecord
  144. // assure they are in place:
  145. // [BOFRecord]
  146. // [CountryRecord]
  147. // [SUPBOOK Internal References nSheets= 3]
  148. // [EXTERNSHEET]
  149. // [EOFRecord]
  150. assertEquals(5, wrl.getRecords().size());
  151. assertTrue(wrl.get(2) instanceof SupBookRecord);
  152. SupBookRecord sup1 = (SupBookRecord)wrl.get(2);
  153. assertEquals(numberOfSheets, sup1.getNumberOfSheets());
  154. assertTrue(wrl.get(3) instanceof ExternSheetRecord);
  155. ExternSheetRecord extSheet = (ExternSheetRecord)wrl.get(3);
  156. assertEquals(0, extSheet.getNumOfRefs());
  157. assertNull(tbl.getNameXPtg("ISODD", -1));
  158. assertEquals(5, wrl.getRecords().size()); //still have five records
  159. NameXPtg namex1 = tbl.addNameXPtg("ISODD"); // adds two new rercords
  160. assertEquals(0, namex1.getSheetRefIndex());
  161. assertEquals(0, namex1.getNameIndex());
  162. NameXPtg act = tbl.getNameXPtg("ISODD", -1);
  163. assertNotNull(act);
  164. assertEquals(namex1.toString(), act.toString());
  165. // Can only find on the right sheet ref, if restricting
  166. act = tbl.getNameXPtg("ISODD", 0);
  167. assertNotNull(act);
  168. assertEquals(namex1.toString(), act.toString());
  169. assertNull(tbl.getNameXPtg("ISODD", 1));
  170. assertNull(tbl.getNameXPtg("ISODD", 2));
  171. // assure they are in place:
  172. // [BOFRecord]
  173. // [CountryRecord]
  174. // [SUPBOOK Internal References nSheets= 3]
  175. // [SUPBOOK Add-In Functions nSheets= 1]
  176. // [EXTERNALNAME .name = ISODD]
  177. // [EXTERNSHEET]
  178. // [EOFRecord]
  179. assertEquals(7, wrl.getRecords().size());
  180. assertTrue(wrl.get(3) instanceof SupBookRecord);
  181. SupBookRecord sup2 = (SupBookRecord)wrl.get(3);
  182. assertTrue(sup2.isAddInFunctions());
  183. assertTrue(wrl.get(4) instanceof ExternalNameRecord);
  184. ExternalNameRecord ext1 = (ExternalNameRecord)wrl.get(4);
  185. assertEquals("ISODD", ext1.getText());
  186. assertTrue(wrl.get(5) instanceof ExternSheetRecord);
  187. assertEquals(1, extSheet.getNumOfRefs());
  188. //check that
  189. assertEquals(0, tbl.resolveNameXIx(namex1.getSheetRefIndex(), namex1.getNameIndex()));
  190. assertEquals("ISODD", tbl.resolveNameXText(namex1.getSheetRefIndex(), namex1.getNameIndex(), null));
  191. assertNull(tbl.getNameXPtg("ISEVEN", -1));
  192. NameXPtg namex2 = tbl.addNameXPtg("ISEVEN"); // adds two new rercords
  193. assertEquals(0, namex2.getSheetRefIndex());
  194. assertEquals(1, namex2.getNameIndex()); // name index increased by one
  195. act = tbl.getNameXPtg("ISEVEN", -1);
  196. assertNotNull(act);
  197. assertEquals(namex2.toString(), act.toString());
  198. assertEquals(8, wrl.getRecords().size());
  199. // assure they are in place:
  200. // [BOFRecord]
  201. // [CountryRecord]
  202. // [SUPBOOK Internal References nSheets= 3]
  203. // [SUPBOOK Add-In Functions nSheets= 1]
  204. // [EXTERNALNAME .name = ISODD]
  205. // [EXTERNALNAME .name = ISEVEN]
  206. // [EXTERNSHEET]
  207. // [EOFRecord]
  208. assertTrue(wrl.get(3) instanceof SupBookRecord);
  209. assertTrue(wrl.get(4) instanceof ExternalNameRecord);
  210. assertTrue(wrl.get(5) instanceof ExternalNameRecord);
  211. assertEquals("ISODD", ((ExternalNameRecord)wrl.get(4)).getText());
  212. assertEquals("ISEVEN", ((ExternalNameRecord)wrl.get(5)).getText());
  213. assertTrue(wrl.get(6) instanceof ExternSheetRecord);
  214. assertTrue(wrl.get(7) instanceof EOFRecord);
  215. assertEquals(0, tbl.resolveNameXIx(namex2.getSheetRefIndex(), namex2.getNameIndex()));
  216. assertEquals("ISEVEN", tbl.resolveNameXText(namex2.getSheetRefIndex(), namex2.getNameIndex(), null));
  217. }
  218. }