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.

BaseTestBugzillaIssues.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertTrue;
  18. import static org.junit.Assert.fail;
  19. import org.apache.poi.hssf.util.PaneInformation;
  20. import org.apache.poi.ss.ITestDataProvider;
  21. import org.apache.poi.ss.SpreadsheetVersion;
  22. import org.apache.poi.ss.util.CellRangeAddress;
  23. import org.junit.Test;
  24. /**
  25. * A base class for bugzilla issues that can be described in terms of common ss interfaces.
  26. *
  27. * @author Yegor Kozlov
  28. */
  29. public abstract class BaseTestBugzillaIssues {
  30. private final ITestDataProvider _testDataProvider;
  31. protected BaseTestBugzillaIssues(ITestDataProvider testDataProvider) {
  32. _testDataProvider = testDataProvider;
  33. }
  34. /**
  35. * Test writing a hyperlink
  36. * Open resulting sheet in Excel and check that A1 contains a hyperlink
  37. *
  38. * Also tests bug 15353 (problems with hyperlinks to Google)
  39. */
  40. @Test
  41. public final void bug23094() {
  42. Workbook wb = _testDataProvider.createWorkbook();
  43. Sheet s = wb.createSheet();
  44. Row r = s.createRow(0);
  45. r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")");
  46. r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
  47. wb = _testDataProvider.writeOutAndReadBack(wb);
  48. r = wb.getSheetAt(0).getRow(0);
  49. Cell cell_0 = r.getCell(0);
  50. assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula());
  51. Cell cell_1 = r.getCell(1);
  52. assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula());
  53. }
  54. /**
  55. * test writing a file with large number of unique strings,
  56. * open resulting file in Excel to check results!
  57. * @param num the number of strings to generate
  58. */
  59. public final void bug15375(int num) {
  60. Workbook wb = _testDataProvider.createWorkbook();
  61. Sheet sheet = wb.createSheet();
  62. CreationHelper factory = wb.getCreationHelper();
  63. String tmp1 = null;
  64. String tmp2 = null;
  65. String tmp3 = null;
  66. for (int i = 0; i < num; i++) {
  67. tmp1 = "Test1" + i;
  68. tmp2 = "Test2" + i;
  69. tmp3 = "Test3" + i;
  70. Row row = sheet.createRow(i);
  71. Cell cell = row.createCell(0);
  72. cell.setCellValue(factory.createRichTextString(tmp1));
  73. cell = row.createCell(1);
  74. cell.setCellValue(factory.createRichTextString(tmp2));
  75. cell = row.createCell(2);
  76. cell.setCellValue(factory.createRichTextString(tmp3));
  77. }
  78. wb = _testDataProvider.writeOutAndReadBack(wb);
  79. for (int i = 0; i < num; i++) {
  80. tmp1 = "Test1" + i;
  81. tmp2 = "Test2" + i;
  82. tmp3 = "Test3" + i;
  83. Row row = sheet.getRow(i);
  84. assertEquals(tmp1, row.getCell(0).getStringCellValue());
  85. assertEquals(tmp2, row.getCell(1).getStringCellValue());
  86. assertEquals(tmp3, row.getCell(2).getStringCellValue());
  87. }
  88. }
  89. /**
  90. * Merged regions were being removed from the parent in cloned sheets
  91. */
  92. @Test
  93. public final void bug22720() {
  94. Workbook workBook = _testDataProvider.createWorkbook();
  95. workBook.createSheet("TEST");
  96. Sheet template = workBook.getSheetAt(0);
  97. template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
  98. template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));
  99. Sheet clone = workBook.cloneSheet(0);
  100. int originalMerged = template.getNumMergedRegions();
  101. assertEquals("2 merged regions", 2, originalMerged);
  102. //remove merged regions from clone
  103. for (int i=template.getNumMergedRegions()-1; i>=0; i--) {
  104. clone.removeMergedRegion(i);
  105. }
  106. assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());
  107. //check if template's merged regions are OK
  108. if (template.getNumMergedRegions()>0) {
  109. // fetch the first merged region...EXCEPTION OCCURS HERE
  110. template.getMergedRegion(0);
  111. }
  112. //make sure we dont exception
  113. }
  114. @Test
  115. public final void bug28031() {
  116. Workbook wb = _testDataProvider.createWorkbook();
  117. Sheet sheet = wb.createSheet();
  118. wb.setSheetName(0, "Sheet1");
  119. Row row = sheet.createRow(0);
  120. Cell cell = row.createCell(0);
  121. String formulaText =
  122. "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))";
  123. cell.setCellFormula(formulaText);
  124. assertEquals(formulaText, cell.getCellFormula());
  125. wb = _testDataProvider.writeOutAndReadBack(wb);
  126. cell = wb.getSheetAt(0).getRow(0).getCell(0);
  127. assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula());
  128. }
  129. /**
  130. * Bug 21334: "File error: data may have been lost" with a file
  131. * that contains macros and this formula:
  132. * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))}
  133. */
  134. @Test
  135. public final void bug21334() {
  136. Workbook wb = _testDataProvider.createWorkbook();
  137. Sheet sh = wb.createSheet();
  138. Cell cell = sh.createRow(0).createCell(0);
  139. String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))";
  140. cell.setCellFormula(formula);
  141. Workbook wb_sv = _testDataProvider.writeOutAndReadBack(wb);
  142. Cell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell(0);
  143. assertEquals(formula, cell_sv.getCellFormula());
  144. }
  145. /** another test for the number of unique strings issue
  146. *test opening the resulting file in Excel*/
  147. @Test
  148. public final void bug22568() {
  149. int r=2000;int c=3;
  150. Workbook wb = _testDataProvider.createWorkbook();
  151. Sheet sheet = wb.createSheet("ExcelTest") ;
  152. int col_cnt=0, rw_cnt=0 ;
  153. col_cnt = c;
  154. rw_cnt = r;
  155. Row rw ;
  156. rw = sheet.createRow(0) ;
  157. //Header row
  158. for(int j=0; j<col_cnt; j++){
  159. Cell cell = rw.createCell(j) ;
  160. cell.setCellValue("Col " + (j+1));
  161. }
  162. for(int i=1; i<rw_cnt; i++){
  163. rw = sheet.createRow(i) ;
  164. for(int j=0; j<col_cnt; j++){
  165. Cell cell = rw.createCell(j) ;
  166. cell.setCellValue("Row:" + (i+1) + ",Column:" + (j+1));
  167. }
  168. }
  169. sheet.setDefaultColumnWidth(18) ;
  170. wb = _testDataProvider.writeOutAndReadBack(wb);
  171. sheet = wb.getSheetAt(0);
  172. rw = sheet.getRow(0);
  173. //Header row
  174. for(int j=0; j<col_cnt; j++){
  175. Cell cell = rw.getCell(j) ;
  176. assertEquals("Col " + (j+1), cell.getStringCellValue());
  177. }
  178. for(int i=1; i<rw_cnt; i++){
  179. rw = sheet.getRow(i) ;
  180. for(int j=0; j<col_cnt; j++){
  181. Cell cell = rw.getCell(j) ;
  182. assertEquals("Row:" + (i+1) + ",Column:" + (j+1), cell.getStringCellValue());
  183. }
  184. }
  185. }
  186. /**
  187. * Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69
  188. */
  189. @Test
  190. public final void bug42448(){
  191. Workbook wb = _testDataProvider.createWorkbook();
  192. Cell cell = wb.createSheet().createRow(0).createCell(0);
  193. cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69");
  194. assertTrue("no errors parsing formula", true);
  195. }
  196. @Test
  197. public final void bug18800() {
  198. Workbook book = _testDataProvider.createWorkbook();
  199. book.createSheet("TEST");
  200. Sheet sheet = book.cloneSheet(0);
  201. book.setSheetName(1,"CLONE");
  202. sheet.createRow(0).createCell(0).setCellValue("Test");
  203. book = _testDataProvider.writeOutAndReadBack(book);
  204. sheet = book.getSheet("CLONE");
  205. Row row = sheet.getRow(0);
  206. Cell cell = row.getCell(0);
  207. assertEquals("Test", cell.getRichStringCellValue().getString());
  208. }
  209. private static void addNewSheetWithCellsA1toD4(Workbook book, int sheet) {
  210. Sheet sht = book .createSheet("s" + sheet);
  211. for (int r=0; r < 4; r++) {
  212. Row row = sht.createRow (r);
  213. for (int c=0; c < 4; c++) {
  214. Cell cel = row.createCell(c);
  215. cel.setCellValue(sheet*100 + r*10 + c);
  216. }
  217. }
  218. }
  219. @Test
  220. public final void bug43093() {
  221. Workbook xlw = _testDataProvider.createWorkbook();
  222. addNewSheetWithCellsA1toD4(xlw, 1);
  223. addNewSheetWithCellsA1toD4(xlw, 2);
  224. addNewSheetWithCellsA1toD4(xlw, 3);
  225. addNewSheetWithCellsA1toD4(xlw, 4);
  226. Sheet s2 = xlw.getSheet("s2");
  227. Row s2r3 = s2.getRow(3);
  228. Cell s2E4 = s2r3.createCell(4);
  229. s2E4.setCellFormula("SUM(s3!B2:C3)");
  230. FormulaEvaluator eva = xlw.getCreationHelper().createFormulaEvaluator();
  231. double d = eva.evaluate(s2E4).getNumberValue();
  232. assertEquals(d, (311+312+321+322), 0.0000001);
  233. }
  234. @Test
  235. public final void bug46729_testMaxFunctionArguments(){
  236. String[] func = {"COUNT", "AVERAGE", "MAX", "MIN", "OR", "SUBTOTAL", "SKEW"};
  237. SpreadsheetVersion ssVersion = _testDataProvider.getSpreadsheetVersion();
  238. Workbook wb = _testDataProvider.createWorkbook();
  239. Cell cell = wb.createSheet().createRow(0).createCell(0);
  240. String fmla;
  241. for (String name : func) {
  242. fmla = createFunction(name, 5);
  243. cell.setCellFormula(fmla);
  244. fmla = createFunction(name, ssVersion.getMaxFunctionArgs());
  245. cell.setCellFormula(fmla);
  246. try {
  247. fmla = createFunction(name, ssVersion.getMaxFunctionArgs() + 1);
  248. cell.setCellFormula(fmla);
  249. fail("Expected FormulaParseException");
  250. } catch (RuntimeException e){
  251. assertTrue(e.getMessage().startsWith("Too many arguments to function '"+name+"'"));
  252. }
  253. }
  254. }
  255. private static String createFunction(String name, int maxArgs){
  256. StringBuffer fmla = new StringBuffer();
  257. fmla.append(name);
  258. fmla.append("(");
  259. for(int i=0; i < maxArgs; i++){
  260. if(i > 0) fmla.append(',');
  261. fmla.append("A1");
  262. }
  263. fmla.append(")");
  264. return fmla.toString();
  265. }
  266. @Test
  267. public final void bug506819_testAutoSize() {
  268. Workbook wb = _testDataProvider.createWorkbook();
  269. Sheet sheet = wb.createSheet("Sheet1");
  270. Row row = sheet.createRow(0);
  271. Cell cell0 = row.createCell(0);
  272. String longValue = "www.hostname.com, www.hostname.com, " +
  273. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  274. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  275. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  276. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  277. "www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com";
  278. cell0.setCellValue(longValue);
  279. sheet.autoSizeColumn(0);
  280. assertEquals(255*256, sheet.getColumnWidth(0)); // maximum column width is 255 characters
  281. sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 506819 reports exception at this point
  282. }
  283. /**
  284. * CreateFreezePane column/row order check
  285. */
  286. @Test
  287. public void bug49381() throws Exception {
  288. Workbook wb = _testDataProvider.createWorkbook();
  289. int colSplit = 1;
  290. int rowSplit = 2;
  291. int leftmostColumn = 3;
  292. int topRow = 4;
  293. Sheet s = wb.createSheet();
  294. // Populate
  295. for(int rn=0; rn<= topRow; rn++) {
  296. Row r = s.createRow(rn);
  297. for(int cn=0; cn<leftmostColumn; cn++) {
  298. Cell c = r.createCell(cn, Cell.CELL_TYPE_NUMERIC);
  299. c.setCellValue(100*rn + cn);
  300. }
  301. }
  302. // Create the Freeze Pane
  303. s.createFreezePane(colSplit, rowSplit, leftmostColumn, topRow);
  304. PaneInformation paneInfo = s.getPaneInformation();
  305. // Check it
  306. assertEquals(colSplit, paneInfo.getVerticalSplitPosition());
  307. assertEquals(rowSplit, paneInfo.getHorizontalSplitPosition());
  308. assertEquals(leftmostColumn, paneInfo.getVerticalSplitLeftColumn());
  309. assertEquals(topRow, paneInfo.getHorizontalSplitTopRow());
  310. // Now a row only freezepane
  311. s.createFreezePane(0, 3);
  312. paneInfo = s.getPaneInformation();
  313. assertEquals(0, paneInfo.getVerticalSplitPosition());
  314. assertEquals(3, paneInfo.getHorizontalSplitPosition());
  315. assertEquals(0, paneInfo.getVerticalSplitLeftColumn());
  316. assertEquals(3, paneInfo.getHorizontalSplitTopRow());
  317. // Now a column only freezepane
  318. s.createFreezePane(4, 0);
  319. paneInfo = s.getPaneInformation();
  320. assertEquals(4, paneInfo.getVerticalSplitPosition());
  321. assertEquals(0, paneInfo.getHorizontalSplitPosition());
  322. assertEquals(4 , paneInfo.getVerticalSplitLeftColumn());
  323. assertEquals(0, paneInfo.getHorizontalSplitTopRow());
  324. }
  325. }