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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 junit.framework.TestCase;
  17. import org.apache.poi.ss.ITestDataProvider;
  18. import org.apache.poi.ss.SpreadsheetVersion;
  19. import org.apache.poi.ss.util.CellRangeAddress;
  20. /**
  21. * A base class for bugzilla issues that can be described in terms of common ss interfaces.
  22. *
  23. * @author Yegor Kozlov
  24. */
  25. public abstract class BaseTestBugzillaIssues extends TestCase {
  26. protected abstract ITestDataProvider getTestDataProvider();
  27. /**
  28. *
  29. * Test writing a hyperlink
  30. * Open resulting sheet in Excel and check that A1 contains a hyperlink
  31. *
  32. * Also tests bug 15353 (problems with hyperlinks to Google)
  33. */
  34. public void test23094() {
  35. Workbook wb = getTestDataProvider().createWorkbook();
  36. Sheet s = wb.createSheet();
  37. Row r = s.createRow(0);
  38. r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")");
  39. r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
  40. wb = getTestDataProvider().writeOutAndReadBack(wb);
  41. r = wb.getSheetAt(0).getRow(0);
  42. Cell cell_0 = r.getCell(0);
  43. assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula());
  44. Cell cell_1 = r.getCell(1);
  45. assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula());
  46. }
  47. /**
  48. * test writing a file with large number of unique strings,
  49. * open resulting file in Excel to check results!
  50. * @param num the number of strings to generate
  51. */
  52. public void baseTest15375(int num) {
  53. Workbook wb = getTestDataProvider().createWorkbook();
  54. Sheet sheet = wb.createSheet();
  55. CreationHelper factory = wb.getCreationHelper();
  56. String tmp1 = null;
  57. String tmp2 = null;
  58. String tmp3 = null;
  59. for (int i = 0; i < num; i++) {
  60. tmp1 = "Test1" + i;
  61. tmp2 = "Test2" + i;
  62. tmp3 = "Test3" + i;
  63. Row row = sheet.createRow(i);
  64. Cell cell = row.createCell(0);
  65. cell.setCellValue(factory.createRichTextString(tmp1));
  66. cell = row.createCell(1);
  67. cell.setCellValue(factory.createRichTextString(tmp2));
  68. cell = row.createCell(2);
  69. cell.setCellValue(factory.createRichTextString(tmp3));
  70. }
  71. wb = getTestDataProvider().writeOutAndReadBack(wb);
  72. for (int i = 0; i < num; i++) {
  73. tmp1 = "Test1" + i;
  74. tmp2 = "Test2" + i;
  75. tmp3 = "Test3" + i;
  76. Row row = sheet.getRow(i);
  77. assertEquals(tmp1, row.getCell(0).getStringCellValue());
  78. assertEquals(tmp2, row.getCell(1).getStringCellValue());
  79. assertEquals(tmp3, row.getCell(2).getStringCellValue());
  80. }
  81. }
  82. /**
  83. * Merged regions were being removed from the parent in cloned sheets
  84. */
  85. public void test22720() {
  86. Workbook workBook = getTestDataProvider().createWorkbook();
  87. workBook.createSheet("TEST");
  88. Sheet template = workBook.getSheetAt(0);
  89. template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
  90. template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));
  91. Sheet clone = workBook.cloneSheet(0);
  92. int originalMerged = template.getNumMergedRegions();
  93. assertEquals("2 merged regions", 2, originalMerged);
  94. //remove merged regions from clone
  95. for (int i=template.getNumMergedRegions()-1; i>=0; i--) {
  96. clone.removeMergedRegion(i);
  97. }
  98. assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());
  99. //check if template's merged regions are OK
  100. if (template.getNumMergedRegions()>0) {
  101. // fetch the first merged region...EXCEPTION OCCURS HERE
  102. template.getMergedRegion(0);
  103. }
  104. //make sure we dont exception
  105. }
  106. public void test28031() {
  107. Workbook wb = getTestDataProvider().createWorkbook();
  108. Sheet sheet = wb.createSheet();
  109. wb.setSheetName(0, "Sheet1");
  110. Row row = sheet.createRow(0);
  111. Cell cell = row.createCell(0);
  112. String formulaText =
  113. "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))";
  114. cell.setCellFormula(formulaText);
  115. assertEquals(formulaText, cell.getCellFormula());
  116. wb = getTestDataProvider().writeOutAndReadBack(wb);
  117. cell = wb.getSheetAt(0).getRow(0).getCell(0);
  118. assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula());
  119. }
  120. /**
  121. * Bug 21334: "File error: data may have been lost" with a file
  122. * that contains macros and this formula:
  123. * {=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))}
  124. */
  125. public void test21334() {
  126. Workbook wb = getTestDataProvider().createWorkbook();
  127. Sheet sh = wb.createSheet();
  128. Cell cell = sh.createRow(0).createCell(0);
  129. 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))";
  130. cell.setCellFormula(formula);
  131. Workbook wb_sv = getTestDataProvider().writeOutAndReadBack(wb);
  132. Cell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell(0);
  133. assertEquals(formula, cell_sv.getCellFormula());
  134. }
  135. /** another test for the number of unique strings issue
  136. *test opening the resulting file in Excel*/
  137. public void test22568() {
  138. int r=2000;int c=3;
  139. Workbook wb = getTestDataProvider().createWorkbook();
  140. Sheet sheet = wb.createSheet("ExcelTest") ;
  141. int col_cnt=0, rw_cnt=0 ;
  142. col_cnt = c;
  143. rw_cnt = r;
  144. Row rw ;
  145. rw = sheet.createRow(0) ;
  146. //Header row
  147. for(int j=0; j<col_cnt; j++){
  148. Cell cell = rw.createCell(j) ;
  149. cell.setCellValue("Col " + (j+1));
  150. }
  151. for(int i=1; i<rw_cnt; i++){
  152. rw = sheet.createRow(i) ;
  153. for(int j=0; j<col_cnt; j++){
  154. Cell cell = rw.createCell(j) ;
  155. cell.setCellValue("Row:" + (i+1) + ",Column:" + (j+1));
  156. }
  157. }
  158. sheet.setDefaultColumnWidth(18) ;
  159. wb = getTestDataProvider().writeOutAndReadBack(wb);
  160. sheet = wb.getSheetAt(0);
  161. rw = sheet.getRow(0);
  162. //Header row
  163. for(int j=0; j<col_cnt; j++){
  164. Cell cell = rw.getCell(j) ;
  165. assertEquals("Col " + (j+1), cell.getStringCellValue());
  166. }
  167. for(int i=1; i<rw_cnt; i++){
  168. rw = sheet.getRow(i) ;
  169. for(int j=0; j<col_cnt; j++){
  170. Cell cell = rw.getCell(j) ;
  171. assertEquals("Row:" + (i+1) + ",Column:" + (j+1), cell.getStringCellValue());
  172. }
  173. }
  174. }
  175. /**
  176. * Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69
  177. */
  178. public void test42448(){
  179. Workbook wb = getTestDataProvider().createWorkbook();
  180. Cell cell = wb.createSheet().createRow(0).createCell(0);
  181. cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69");
  182. assertTrue("no errors parsing formula", true);
  183. }
  184. public void test18800() {
  185. Workbook book = getTestDataProvider().createWorkbook();
  186. book.createSheet("TEST");
  187. Sheet sheet = book.cloneSheet(0);
  188. book.setSheetName(1,"CLONE");
  189. sheet.createRow(0).createCell(0).setCellValue("Test");
  190. book = getTestDataProvider().writeOutAndReadBack(book);
  191. sheet = book.getSheet("CLONE");
  192. Row row = sheet.getRow(0);
  193. Cell cell = row.getCell(0);
  194. assertEquals("Test", cell.getRichStringCellValue().getString());
  195. }
  196. private static void addNewSheetWithCellsA1toD4(Workbook book, int sheet) {
  197. Sheet sht = book .createSheet("s" + sheet);
  198. for (int r=0; r < 4; r++) {
  199. Row row = sht.createRow (r);
  200. for (int c=0; c < 4; c++) {
  201. Cell cel = row.createCell(c);
  202. cel.setCellValue(sheet*100 + r*10 + c);
  203. }
  204. }
  205. }
  206. public void testBug43093() {
  207. Workbook xlw = getTestDataProvider().createWorkbook();
  208. addNewSheetWithCellsA1toD4(xlw, 1);
  209. addNewSheetWithCellsA1toD4(xlw, 2);
  210. addNewSheetWithCellsA1toD4(xlw, 3);
  211. addNewSheetWithCellsA1toD4(xlw, 4);
  212. Sheet s2 = xlw.getSheet("s2");
  213. Row s2r3 = s2.getRow(3);
  214. Cell s2E4 = s2r3.createCell(4);
  215. s2E4.setCellFormula("SUM(s3!B2:C3)");
  216. FormulaEvaluator eva = xlw.getCreationHelper().createFormulaEvaluator();
  217. double d = eva.evaluate(s2E4).getNumberValue();
  218. assertEquals(d, (311+312+321+322), 0.0000001);
  219. }
  220. public void testMaxFunctionArguments_bug46729(){
  221. String[] func = {"COUNT", "AVERAGE", "MAX", "MIN", "OR", "SUBTOTAL", "SKEW"};
  222. SpreadsheetVersion ssVersion = getTestDataProvider().getSpreadsheetVersion();
  223. Workbook wb = getTestDataProvider().createWorkbook();
  224. Cell cell = wb.createSheet().createRow(0).createCell(0);
  225. String fmla;
  226. for (String name : func) {
  227. fmla = createFunction(name, 5);
  228. cell.setCellFormula(fmla);
  229. fmla = createFunction(name, ssVersion.getMaxFunctionArgs());
  230. cell.setCellFormula(fmla);
  231. try {
  232. fmla = createFunction(name, ssVersion.getMaxFunctionArgs() + 1);
  233. cell.setCellFormula(fmla);
  234. fail("Expected FormulaParseException");
  235. } catch (RuntimeException e){
  236. assertTrue(e.getMessage().startsWith("Too many arguments to function '"+name+"'"));
  237. }
  238. }
  239. }
  240. private String createFunction(String name, int maxArgs){
  241. StringBuffer fmla = new StringBuffer();
  242. fmla.append(name);
  243. fmla.append("(");
  244. for(int i=0; i < maxArgs; i++){
  245. if(i > 0) fmla.append(',');
  246. fmla.append("A1");
  247. }
  248. fmla.append(")");
  249. return fmla.toString();
  250. }
  251. }