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.

TestUnfixedBugs.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.xssf.usermodel;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotNull;
  18. import static org.junit.jupiter.api.Assertions.assertNull;
  19. import static org.junit.jupiter.api.Assertions.assertTrue;
  20. import static org.junit.jupiter.api.Assertions.fail;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.Calendar;
  25. import java.util.Date;
  26. import java.util.Hashtable;
  27. import org.apache.poi.ss.formula.OperationEvaluationContext;
  28. import org.apache.poi.ss.formula.eval.NumberEval;
  29. import org.apache.poi.ss.formula.eval.ValueEval;
  30. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  31. import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
  32. import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
  33. import org.apache.poi.ss.formula.udf.UDFFinder;
  34. import org.apache.poi.ss.usermodel.BorderStyle;
  35. import org.apache.poi.ss.usermodel.Cell;
  36. import org.apache.poi.ss.usermodel.DateUtil;
  37. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  38. import org.apache.poi.ss.usermodel.RichTextString;
  39. import org.apache.poi.ss.usermodel.Row;
  40. import org.apache.poi.ss.usermodel.Sheet;
  41. import org.apache.poi.ss.usermodel.Workbook;
  42. import org.apache.poi.ss.util.CellAddress;
  43. import org.apache.poi.ss.util.CellRangeAddress;
  44. import org.apache.poi.ss.util.CellUtil;
  45. import org.apache.poi.ss.util.RegionUtil;
  46. import org.apache.poi.util.LocaleUtil;
  47. import org.apache.poi.xssf.SXSSFITestDataProvider;
  48. import org.apache.poi.xssf.XSSFTestDataSamples;
  49. import org.junit.jupiter.api.Test;
  50. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
  51. /**
  52. * @author centic
  53. *
  54. * This testcase contains tests for bugs that are yet to be fixed. Therefore,
  55. * the standard ant test target does not run these tests. Run this testcase with
  56. * the single-test target. The names of the tests usually correspond to the
  57. * Bugzilla id's PLEASE MOVE tests from this class to TestBugs once the bugs are
  58. * fixed, so that they are then run automatically.
  59. */
  60. public final class TestUnfixedBugs {
  61. @Test
  62. public void test54071() throws Exception {
  63. Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("54071.xlsx");
  64. Sheet sheet = workbook.getSheetAt(0);
  65. int rows = sheet.getPhysicalNumberOfRows();
  66. Row title = sheet.getRow(0);
  67. Date prev = null;
  68. for (int row = 1; row < rows; row++) {
  69. Row rowObj = sheet.getRow(row);
  70. for (int col = 0; col < 1; col++) {
  71. String titleName = title.getCell(col).toString();
  72. Cell cell = rowObj.getCell(col);
  73. if (titleName.startsWith("time")) {
  74. // here the output will produce ...59 or ...58 for the rows, probably POI is
  75. // doing some different rounding or some other small difference...
  76. //System.out.println("==Time:"+cell.getDateCellValue());
  77. if(prev != null) {
  78. assertEquals(prev, cell.getDateCellValue());
  79. }
  80. prev = cell.getDateCellValue();
  81. }
  82. }
  83. }
  84. workbook.close();
  85. }
  86. @Test
  87. public void test54071Simple() {
  88. double value1 = 41224.999988425923;
  89. double value2 = 41224.999988368058;
  90. int wholeDays1 = (int)Math.floor(value1);
  91. int millisecondsInDay1 = (int)((value1 - wholeDays1) * DateUtil.DAY_MILLISECONDS + 0.5);
  92. int wholeDays2 = (int)Math.floor(value2);
  93. int millisecondsInDay2 = (int)((value2 - wholeDays2) * DateUtil.DAY_MILLISECONDS + 0.5);
  94. assertEquals(wholeDays1, wholeDays2);
  95. // here we see that the time-value is 5 milliseconds apart, one is 86399000 and the other is 86398995,
  96. // thus one is one second higher than the other
  97. assertEquals(millisecondsInDay1, millisecondsInDay2, "The time-values are 5 milliseconds apart");
  98. // when we do the calendar-stuff, there is a boolean which determines if
  99. // the milliseconds are rounded or not, having this at "false" causes the
  100. // second to be different here!
  101. int startYear = 1900;
  102. int dayAdjust = -1; // Excel thinks 2/29/1900 is a valid date, which it isn't
  103. Calendar calendar1 = LocaleUtil.getLocaleCalendar(startYear,0, wholeDays1 + dayAdjust);
  104. calendar1.set(Calendar.MILLISECOND, millisecondsInDay1);
  105. // this is the rounding part:
  106. calendar1.add(Calendar.MILLISECOND, 500);
  107. calendar1.clear(Calendar.MILLISECOND);
  108. Calendar calendar2 = LocaleUtil.getLocaleCalendar(startYear,0, wholeDays2 + dayAdjust);
  109. calendar2.set(Calendar.MILLISECOND, millisecondsInDay2);
  110. // this is the rounding part:
  111. calendar2.add(Calendar.MILLISECOND, 500);
  112. calendar2.clear(Calendar.MILLISECOND);
  113. // now the calendars are equal
  114. assertEquals(calendar1, calendar2);
  115. assertEquals(DateUtil.getJavaDate(value1, false), DateUtil.getJavaDate(value2, false));
  116. }
  117. // When this is fixed, the test case should go to BaseTestXCell with
  118. // adjustments to use _testDataProvider to also verify this for XSSF
  119. @Test
  120. public void testBug57294() throws IOException {
  121. Workbook wb = SXSSFITestDataProvider.instance.createWorkbook();
  122. Sheet sheet = wb.createSheet();
  123. Row row = sheet.createRow(0);
  124. Cell cell = row.createCell(0);
  125. RichTextString str = new XSSFRichTextString("Test rich text string");
  126. str.applyFont(2, 4, (short)0);
  127. assertEquals(3, str.numFormattingRuns());
  128. cell.setCellValue(str);
  129. Workbook wbBack = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb);
  130. wb.close();
  131. // re-read after serializing and reading back
  132. Cell cellBack = wbBack.getSheetAt(0).getRow(0).getCell(0);
  133. assertNotNull(cellBack);
  134. RichTextString strBack = cellBack.getRichStringCellValue();
  135. assertNotNull(strBack);
  136. assertEquals(3, strBack.numFormattingRuns());
  137. assertEquals(0, strBack.getIndexOfFormattingRun(0));
  138. assertEquals(2, strBack.getIndexOfFormattingRun(1));
  139. assertEquals(4, strBack.getIndexOfFormattingRun(2));
  140. wbBack.close();
  141. }
  142. // This test will run green, but the resulting file is formatted incorrectly,
  143. // see the bug at https://bz.apache.org/bugzilla/show_bug.cgi?id=55752
  144. @Test
  145. public void testBug55752() throws IOException {
  146. try (Workbook wb = new XSSFWorkbook()) {
  147. Sheet sheet = wb.createSheet("test");
  148. for (int i = 0; i < 4; i++) {
  149. Row row = sheet.createRow(i);
  150. for (int j = 0; j < 2; j++) {
  151. Cell cell = row.createCell(j);
  152. cell.setCellStyle(wb.createCellStyle());
  153. }
  154. }
  155. // set content
  156. Row row1 = sheet.getRow(0);
  157. row1.getCell(0).setCellValue("AAA");
  158. Row row2 = sheet.getRow(1);
  159. row2.getCell(0).setCellValue("BBB");
  160. Row row3 = sheet.getRow(2);
  161. row3.getCell(0).setCellValue("CCC");
  162. Row row4 = sheet.getRow(3);
  163. row4.getCell(0).setCellValue("DDD");
  164. // merge cells
  165. CellRangeAddress range1 = new CellRangeAddress(0, 0, 0, 1);
  166. assertEquals(0, sheet.addMergedRegion(range1));
  167. CellRangeAddress range2 = new CellRangeAddress(1, 1, 0, 1);
  168. assertEquals(1, sheet.addMergedRegion(range2));
  169. CellRangeAddress range3 = new CellRangeAddress(2, 2, 0, 1);
  170. assertEquals(2, sheet.addMergedRegion(range3));
  171. assertEquals(0, range3.getFirstColumn());
  172. assertEquals(1, range3.getLastColumn());
  173. assertEquals(2, range3.getLastRow());
  174. CellRangeAddress range4 = new CellRangeAddress(3, 3, 0, 1);
  175. assertEquals(3, sheet.addMergedRegion(range4));
  176. // set border
  177. RegionUtil.setBorderBottom(BorderStyle.THIN, range1, sheet);
  178. row2.getCell(0).getCellStyle().setBorderBottom(BorderStyle.THIN);
  179. row2.getCell(1).getCellStyle().setBorderBottom(BorderStyle.THIN);
  180. Cell cell0 = CellUtil.getCell(row3, 0);
  181. CellUtil.setCellStyleProperty(cell0, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);
  182. Cell cell1 = CellUtil.getCell(row3, 1);
  183. CellUtil.setCellStyleProperty(cell1, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);
  184. RegionUtil.setBorderBottom(BorderStyle.THIN, range4, sheet);
  185. // write to file for manual inspection
  186. XSSFTestDataSamples.writeOut(wb, "bug 55752 for review");
  187. }
  188. fail("Test runs ok, but the resulting file is incorrectly formatted");
  189. }
  190. @Test
  191. public void test57423() throws IOException {
  192. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57423.xlsx");
  193. Sheet testSheet = wb.getSheetAt(0);
  194. // row shift (negative or positive) causes corrupted output xlsx file when the shift value is bigger
  195. // than the number of rows being shifted
  196. // Excel 2010 on opening the output file says:
  197. // "Excel found unreadable content" and offers recovering the file by removing the unreadable content
  198. // This can be observed in cases like the following:
  199. // negative shift of 1 row by less than -1
  200. // negative shift of 2 rows by less than -2
  201. // positive shift of 1 row by 2 or more
  202. // positive shift of 2 rows by 3 or more
  203. //testSheet.shiftRows(4, 5, -3);
  204. testSheet.shiftRows(10, 10, 2);
  205. checkRows57423(testSheet);
  206. Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  207. /* XSSFTestDataSamples.writeOut(wb, "bug 57423 for manual review"); */
  208. wb.close();
  209. checkRows57423(wbBack.getSheetAt(0));
  210. wbBack.close();
  211. fail("Excel reports that the workbook is corrupt");
  212. }
  213. private void checkRows57423(Sheet testSheet) throws IOException {
  214. checkRow57423(testSheet, 0, "0");
  215. checkRow57423(testSheet, 1, "1");
  216. checkRow57423(testSheet, 2, "2");
  217. checkRow57423(testSheet, 3, "3");
  218. checkRow57423(testSheet, 4, "4");
  219. checkRow57423(testSheet, 5, "5");
  220. checkRow57423(testSheet, 6, "6");
  221. checkRow57423(testSheet, 7, "7");
  222. checkRow57423(testSheet, 8, "8");
  223. checkRow57423(testSheet, 9, "9");
  224. assertNull(testSheet.getRow(10), "Row number 10 should be gone after the shift");
  225. checkRow57423(testSheet, 11, "11");
  226. checkRow57423(testSheet, 12, "10");
  227. checkRow57423(testSheet, 13, "13");
  228. checkRow57423(testSheet, 14, "14");
  229. checkRow57423(testSheet, 15, "15");
  230. checkRow57423(testSheet, 16, "16");
  231. checkRow57423(testSheet, 17, "17");
  232. checkRow57423(testSheet, 18, "18");
  233. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  234. try {
  235. ((XSSFSheet)testSheet).write(stream);
  236. } finally {
  237. stream.close();
  238. }
  239. // verify that the resulting XML has the rows in correct order as required by Excel
  240. String xml = new String(stream.toByteArray(), StandardCharsets.UTF_8);
  241. int posR12 = xml.indexOf("<row r=\"12\"");
  242. int posR13 = xml.indexOf("<row r=\"13\"");
  243. // both need to be found
  244. assertTrue(posR12 != -1);
  245. assertTrue(posR13 != -1);
  246. assertTrue(posR12 < posR13, "Need to find row 12 before row 13 after the shifting, but had row 12 at " + posR12 + " and row 13 at " + posR13);
  247. }
  248. private void checkRow57423(Sheet testSheet, int rowNum, String contents) {
  249. Row row = testSheet.getRow(rowNum);
  250. assertNotNull(row, "Expecting row at rownum " + rowNum);
  251. CTRow ctRow = ((XSSFRow)row).getCTRow();
  252. assertEquals(rowNum+1, ctRow.getR());
  253. Cell cell = row.getCell(0);
  254. assertNotNull(cell, "Expecting cell at rownum " + rowNum);
  255. assertEquals( contents + ".0", cell.toString(), "Did not have expected contents at rownum " + rowNum );
  256. }
  257. @Test
  258. public void bug57423_shiftRowsByLargeOffset() throws IOException {
  259. try (
  260. XSSFWorkbook wb = new XSSFWorkbook()
  261. //OutputStream out = new FileOutputStream("/tmp/57423." + wb.getClass().getName() + ".xlsx"));
  262. ) {
  263. Sheet sh = wb.createSheet();
  264. sh.createRow(0).createCell(0).setCellValue("a");
  265. sh.createRow(1).createCell(0).setCellValue("b");
  266. sh.createRow(2).createCell(0).setCellValue("c");
  267. sh.shiftRows(0, 1, 3);
  268. XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  269. assertThatRowsInAscendingOrder(wb);
  270. assertThatRowsInAscendingOrder(wbBack);
  271. //wbBack.write(out);
  272. // Excel reports that the workbook is corrupt because the rows are not in ascending order
  273. // LibreOffice doesn't complain when rows are not in ascending order
  274. wbBack.close();
  275. fail("Excel reports that the workbook is corrupt, LibreOffice can read it");
  276. }
  277. }
  278. private void assertThatRowsInAscendingOrder(final XSSFWorkbook wb) {
  279. // Check that CTRows are stored in ascending order of row index
  280. long maxSeenRowNum = 0; //1-based
  281. for (final CTRow ctRow : wb.getSheetAt(0).getCTWorksheet().getSheetData().getRowList()) {
  282. final long rowNum = ctRow.getR(); //1-based
  283. assertTrue(rowNum > maxSeenRowNum, "Row " + rowNum + " (1-based) is not in ascending order; previously saw " + maxSeenRowNum);
  284. maxSeenRowNum = rowNum;
  285. }
  286. }
  287. @Test
  288. public void testBug60355() throws IOException {
  289. try (Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("HsGetVal.xlsx")){
  290. Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
  291. System.out.println("cell_4_1 formula:" + sheet.getRow(4).getCell(1).getCellFormula());
  292. System.out.println("cell_4_2 formula:" + sheet.getRow(4).getCell(2).getCellFormula());
  293. // hard code HsGetValue test values for formulas on the sheet
  294. Hashtable<CellAddress, String> cellToValueTable = new Hashtable<>();
  295. CellAddress cell4_1 = new CellAddress(4, 1);
  296. cellToValueTable.put(cell4_1, "678.0");
  297. CellAddress cell4_2 = new CellAddress(4, 2);
  298. cellToValueTable.put(cell4_2, "123.0");
  299. String[] functionNames = {HsGetValue.name};
  300. FreeRefFunction[] functionImpls = {new HsGetValue(cellToValueTable)};
  301. UDFFinder udfs = new DefaultUDFFinder(functionNames, functionImpls);
  302. UDFFinder udfToolpack = new AggregatingUDFFinder(udfs);
  303. workbook.addToolPack(udfToolpack);
  304. FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
  305. formulaEvaluator.setIgnoreMissingWorkbooks(true);
  306. formulaEvaluator.evaluateAll();
  307. }
  308. }
  309. public static class HsGetValue implements FreeRefFunction {
  310. public static final String name = "HsGetValue";
  311. private final Hashtable<CellAddress, String> cellValues;
  312. public HsGetValue(Hashtable<CellAddress, String> cellValues) {
  313. super();
  314. this.cellValues = cellValues;
  315. }
  316. @Override
  317. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext evaluationContext) {
  318. int row = evaluationContext.getRowIndex();
  319. int column = evaluationContext.getColumnIndex();
  320. CellAddress cell = new CellAddress(row, column);
  321. String value = cellValues.get(cell);
  322. return new NumberEval( Double.parseDouble(value) );
  323. }
  324. }
  325. @Test
  326. public void testBug64759() throws IOException {
  327. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("64759.xlsx")) {
  328. Sheet cloned = wb.cloneSheet(0);
  329. assertNotNull(cloned);
  330. }
  331. }
  332. }