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.

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