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

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