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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 java.io.ByteArrayOutputStream;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.nio.charset.Charset;
  22. import java.util.Calendar;
  23. import java.util.Date;
  24. import java.util.GregorianCalendar;
  25. import junit.framework.TestCase;
  26. import org.apache.poi.hssf.HSSFTestDataSamples;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.usermodel.CellStyle;
  29. import org.apache.poi.ss.usermodel.DataFormatter;
  30. import org.apache.poi.ss.usermodel.DateUtil;
  31. import org.apache.poi.ss.usermodel.FormulaError;
  32. import org.apache.poi.ss.usermodel.RichTextString;
  33. import org.apache.poi.ss.usermodel.Row;
  34. import org.apache.poi.ss.usermodel.Sheet;
  35. import org.apache.poi.ss.usermodel.Workbook;
  36. import org.apache.poi.ss.util.CellRangeAddress;
  37. import org.apache.poi.ss.util.CellUtil;
  38. import org.apache.poi.ss.util.RegionUtil;
  39. import org.apache.poi.xssf.SXSSFITestDataProvider;
  40. import org.apache.poi.xssf.XSSFTestDataSamples;
  41. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  42. import org.junit.Test;
  43. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
  44. /**
  45. * @author centic
  46. *
  47. * This testcase contains tests for bugs that are yet to be fixed. Therefore,
  48. * the standard ant test target does not run these tests. Run this testcase with
  49. * the single-test target. The names of the tests usually correspond to the
  50. * Bugzilla id's PLEASE MOVE tests from this class to TestBugs once the bugs are
  51. * fixed, so that they are then run automatically.
  52. */
  53. public final class TestUnfixedBugs extends TestCase {
  54. public void testBug54084Unicode() throws IOException {
  55. // sample XLSX with the same text-contents as the text-file above
  56. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("54084 - Greek - beyond BMP.xlsx");
  57. verifyBug54084Unicode(wb);
  58. // OutputStream baos = new FileOutputStream("/tmp/test.xlsx");
  59. // try {
  60. // wb.write(baos);
  61. // } finally {
  62. // baos.close();
  63. // }
  64. // now write the file and read it back in
  65. XSSFWorkbook wbWritten = XSSFTestDataSamples.writeOutAndReadBack(wb);
  66. verifyBug54084Unicode(wbWritten);
  67. // finally also write it out via the streaming interface and verify that we still can read it back in
  68. Workbook wbStreamingWritten = SXSSFITestDataProvider.instance.writeOutAndReadBack(new SXSSFWorkbook(wb));
  69. verifyBug54084Unicode(wbStreamingWritten);
  70. }
  71. private void verifyBug54084Unicode(Workbook wb) {
  72. // expected data is stored in UTF-8 in a text-file
  73. byte data[] = HSSFTestDataSamples.getTestDataFileContent("54084 - Greek - beyond BMP.txt");
  74. String testData = new String(data, Charset.forName("UTF-8")).trim();
  75. Sheet sheet = wb.getSheetAt(0);
  76. Row row = sheet.getRow(0);
  77. Cell cell = row.getCell(0);
  78. String value = cell.getStringCellValue();
  79. //System.out.println(value);
  80. assertEquals("The data in the text-file should exactly match the data that we read from the workbook", testData, value);
  81. }
  82. public void test54071() {
  83. Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("54071.xlsx");
  84. Sheet sheet = workbook.getSheetAt(0);
  85. int rows = sheet.getPhysicalNumberOfRows();
  86. System.out.println(">> file rows is:"+(rows-1)+" <<");
  87. Row title = sheet.getRow(0);
  88. Date prev = null;
  89. for (int row = 1; row < rows; row++) {
  90. Row rowObj = sheet.getRow(row);
  91. for (int col = 0; col < 1; col++) {
  92. String titleName = title.getCell(col).toString();
  93. Cell cell = rowObj.getCell(col);
  94. if (titleName.startsWith("time")) {
  95. // here the output will produce ...59 or ...58 for the rows, probably POI is
  96. // doing some different rounding or some other small difference...
  97. System.out.println("==Time:"+cell.getDateCellValue());
  98. if(prev != null) {
  99. assertEquals(prev, cell.getDateCellValue());
  100. }
  101. prev = cell.getDateCellValue();
  102. }
  103. }
  104. }
  105. }
  106. public void test54071Simple() {
  107. double value1 = 41224.999988425923;
  108. double value2 = 41224.999988368058;
  109. int wholeDays1 = (int)Math.floor(value1);
  110. int millisecondsInDay1 = (int)((value1 - wholeDays1) * DateUtil.DAY_MILLISECONDS + 0.5);
  111. int wholeDays2 = (int)Math.floor(value2);
  112. int millisecondsInDay2 = (int)((value2 - wholeDays2) * DateUtil.DAY_MILLISECONDS + 0.5);
  113. assertEquals(wholeDays1, wholeDays2);
  114. // here we see that the time-value is 5 milliseconds apart, one is 86399000 and the other is 86398995,
  115. // thus one is one second higher than the other
  116. assertEquals("The time-values are 5 milliseconds apart",
  117. millisecondsInDay1, millisecondsInDay2);
  118. // when we do the calendar-stuff, there is a boolean which determines if
  119. // the milliseconds are rounded or not, having this at "false" causes the
  120. // second to be different here!
  121. int startYear = 1900;
  122. int dayAdjust = -1; // Excel thinks 2/29/1900 is a valid date, which it isn't
  123. Calendar calendar1 = new GregorianCalendar();
  124. calendar1.set(startYear,0, wholeDays1 + dayAdjust, 0, 0, 0);
  125. calendar1.set(Calendar.MILLISECOND, millisecondsInDay1);
  126. // this is the rounding part:
  127. calendar1.add(Calendar.MILLISECOND, 500);
  128. calendar1.clear(Calendar.MILLISECOND);
  129. Calendar calendar2 = new GregorianCalendar();
  130. calendar2.set(startYear,0, wholeDays2 + dayAdjust, 0, 0, 0);
  131. calendar2.set(Calendar.MILLISECOND, millisecondsInDay2);
  132. // this is the rounding part:
  133. calendar2.add(Calendar.MILLISECOND, 500);
  134. calendar2.clear(Calendar.MILLISECOND);
  135. // now the calendars are equal
  136. assertEquals(calendar1, calendar2);
  137. assertEquals(DateUtil.getJavaDate(value1, false), DateUtil.getJavaDate(value2, false));
  138. }
  139. public void test57236() {
  140. // Having very small numbers leads to different formatting, Excel uses the scientific notation, but POI leads to "0"
  141. /*
  142. DecimalFormat format = new DecimalFormat("#.##########", new DecimalFormatSymbols(Locale.getDefault()));
  143. double d = 3.0E-104;
  144. assertEquals("3.0E-104", format.format(d));
  145. */
  146. DataFormatter formatter = new DataFormatter(true);
  147. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57236.xlsx");
  148. for(int sheetNum = 0;sheetNum < wb.getNumberOfSheets();sheetNum++) {
  149. Sheet sheet = wb.getSheetAt(sheetNum);
  150. for(int rowNum = sheet.getFirstRowNum();rowNum < sheet.getLastRowNum();rowNum++) {
  151. Row row = sheet.getRow(rowNum);
  152. for(int cellNum = row.getFirstCellNum();cellNum < row.getLastCellNum();cellNum++) {
  153. Cell cell = row.getCell(cellNum);
  154. String fmtCellValue = formatter.formatCellValue(cell);
  155. System.out.println("Cell: " + fmtCellValue);
  156. assertNotNull(fmtCellValue);
  157. assertFalse(fmtCellValue.equals("0"));
  158. }
  159. }
  160. }
  161. }
  162. // When this is fixed, the test case should go to BaseTestXCell with
  163. // adjustments to use _testDataProvider to also verify this for XSSF
  164. public void testBug57294() throws IOException {
  165. Workbook wb = SXSSFITestDataProvider.instance.createWorkbook();
  166. Sheet sheet = wb.createSheet();
  167. Row row = sheet.createRow(0);
  168. Cell cell = row.createCell(0);
  169. RichTextString str = new XSSFRichTextString("Test rich text string");
  170. str.applyFont(2, 4, (short)0);
  171. assertEquals(3, str.numFormattingRuns());
  172. cell.setCellValue(str);
  173. Workbook wbBack = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb);
  174. wb.close();
  175. // re-read after serializing and reading back
  176. Cell cellBack = wbBack.getSheetAt(0).getRow(0).getCell(0);
  177. assertNotNull(cellBack);
  178. RichTextString strBack = cellBack.getRichStringCellValue();
  179. assertNotNull(strBack);
  180. assertEquals(3, strBack.numFormattingRuns());
  181. assertEquals(0, strBack.getIndexOfFormattingRun(0));
  182. assertEquals(2, strBack.getIndexOfFormattingRun(1));
  183. assertEquals(4, strBack.getIndexOfFormattingRun(2));
  184. }
  185. @Test
  186. public void testBug56655() {
  187. XSSFWorkbook wb = new XSSFWorkbook();
  188. Sheet sheet = wb.createSheet();
  189. setCellFormula(sheet, 0, 0, "#VALUE!");
  190. setCellFormula(sheet, 0, 1, "SUMIFS(A:A,A:A,#VALUE!)");
  191. XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  192. assertEquals(XSSFCell.CELL_TYPE_ERROR, getCell(sheet, 0,0).getCachedFormulaResultType());
  193. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0,0).getErrorCellValue());
  194. assertEquals(XSSFCell.CELL_TYPE_ERROR, getCell(sheet, 0,1).getCachedFormulaResultType());
  195. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0,1).getErrorCellValue());
  196. }
  197. @Test
  198. public void testBug56655a() {
  199. XSSFWorkbook wb = new XSSFWorkbook();
  200. Sheet sheet = wb.createSheet();
  201. setCellFormula(sheet, 0, 0, "B1*C1");
  202. sheet.getRow(0).createCell(1).setCellValue("A");
  203. setCellFormula(sheet, 1, 0, "B1*C1");
  204. sheet.getRow(1).createCell(1).setCellValue("A");
  205. setCellFormula(sheet, 0, 3, "SUMIFS(A:A,A:A,A2)");
  206. XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  207. assertEquals(XSSFCell.CELL_TYPE_ERROR, getCell(sheet, 0, 0).getCachedFormulaResultType());
  208. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 0).getErrorCellValue());
  209. assertEquals(XSSFCell.CELL_TYPE_ERROR, getCell(sheet, 1, 0).getCachedFormulaResultType());
  210. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 1, 0).getErrorCellValue());
  211. assertEquals(XSSFCell.CELL_TYPE_ERROR, getCell(sheet, 0, 3).getCachedFormulaResultType());
  212. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 3).getErrorCellValue());
  213. }
  214. /**
  215. * @param row 0-based
  216. * @param column 0-based
  217. */
  218. private void setCellFormula(Sheet sheet, int row, int column, String formula) {
  219. Row r = sheet.getRow(row);
  220. if (r == null) {
  221. r = sheet.createRow(row);
  222. }
  223. Cell cell = r.getCell(column);
  224. if (cell == null) {
  225. cell = r.createCell(column);
  226. }
  227. cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
  228. cell.setCellFormula(formula);
  229. }
  230. /**
  231. * @param rowNo 0-based
  232. * @param column 0-based
  233. */
  234. private Cell getCell(Sheet sheet, int rowNo, int column) {
  235. return sheet.getRow(rowNo).getCell(column);
  236. }
  237. @Test
  238. public void testBug55752() throws IOException {
  239. Workbook wb = new XSSFWorkbook();
  240. try {
  241. Sheet sheet = wb.createSheet("test");
  242. for (int i = 0; i < 4; i++) {
  243. Row row = sheet.createRow(i);
  244. for (int j = 0; j < 2; j++) {
  245. Cell cell = row.createCell(j);
  246. cell.setCellStyle(wb.createCellStyle());
  247. }
  248. }
  249. // set content
  250. Row row1 = sheet.getRow(0);
  251. row1.getCell(0).setCellValue("AAA");
  252. Row row2 = sheet.getRow(1);
  253. row2.getCell(0).setCellValue("BBB");
  254. Row row3 = sheet.getRow(2);
  255. row3.getCell(0).setCellValue("CCC");
  256. Row row4 = sheet.getRow(3);
  257. row4.getCell(0).setCellValue("DDD");
  258. // merge cells
  259. CellRangeAddress range1 = new CellRangeAddress(0, 0, 0, 1);
  260. sheet.addMergedRegion(range1);
  261. CellRangeAddress range2 = new CellRangeAddress(1, 1, 0, 1);
  262. sheet.addMergedRegion(range2);
  263. CellRangeAddress range3 = new CellRangeAddress(2, 2, 0, 1);
  264. sheet.addMergedRegion(range3);
  265. assertEquals(0, range3.getFirstColumn());
  266. assertEquals(1, range3.getLastColumn());
  267. assertEquals(2, range3.getLastRow());
  268. CellRangeAddress range4 = new CellRangeAddress(3, 3, 0, 1);
  269. sheet.addMergedRegion(range4);
  270. // set border
  271. RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range1, sheet, wb);
  272. row2.getCell(0).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);
  273. row2.getCell(1).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);
  274. Cell cell0 = CellUtil.getCell(row3, 0);
  275. CellUtil.setCellStyleProperty(cell0, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);
  276. Cell cell1 = CellUtil.getCell(row3, 1);
  277. CellUtil.setCellStyleProperty(cell1, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);
  278. RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range4, sheet, wb);
  279. // write to file
  280. OutputStream stream = new FileOutputStream(new File("C:/temp/55752.xlsx"));
  281. try {
  282. wb.write(stream);
  283. } finally {
  284. stream.close();
  285. }
  286. } finally {
  287. wb.close();
  288. }
  289. }
  290. @Test
  291. public void test57423() throws IOException {
  292. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57423.xlsx");
  293. Sheet testSheet = wb.getSheetAt(0);
  294. // row shift (negative or positive) causes corrupted output xlsx file when the shift value is bigger
  295. // than the number of rows being shifted
  296. // Excel 2010 on opening the output file says:
  297. // "Excel found unreadable content" and offers recovering the file by removing the unreadable content
  298. // This can be observed in cases like the following:
  299. // negative shift of 1 row by less than -1
  300. // negative shift of 2 rows by less than -2
  301. // positive shift of 1 row by 2 or more
  302. // positive shift of 2 rows by 3 or more
  303. //testSheet.shiftRows(4, 5, -3);
  304. testSheet.shiftRows(10, 10, 2);
  305. checkRows57423(testSheet);
  306. Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  307. /*FileOutputStream stream = new FileOutputStream("C:\\temp\\57423.xlsx");
  308. try {
  309. wb.write(stream);
  310. } finally {
  311. stream.close();
  312. }*/
  313. wb.close();
  314. checkRows57423(wbBack.getSheetAt(0));
  315. wbBack.close();
  316. }
  317. private void checkRows57423(Sheet testSheet) throws IOException {
  318. checkRow57423(testSheet, 0, "0");
  319. checkRow57423(testSheet, 1, "1");
  320. checkRow57423(testSheet, 2, "2");
  321. checkRow57423(testSheet, 3, "3");
  322. checkRow57423(testSheet, 4, "4");
  323. checkRow57423(testSheet, 5, "5");
  324. checkRow57423(testSheet, 6, "6");
  325. checkRow57423(testSheet, 7, "7");
  326. checkRow57423(testSheet, 8, "8");
  327. checkRow57423(testSheet, 9, "9");
  328. assertNull("Row number 10 should be gone after the shift",
  329. testSheet.getRow(10));
  330. checkRow57423(testSheet, 11, "11");
  331. checkRow57423(testSheet, 12, "10");
  332. checkRow57423(testSheet, 13, "13");
  333. checkRow57423(testSheet, 14, "14");
  334. checkRow57423(testSheet, 15, "15");
  335. checkRow57423(testSheet, 16, "16");
  336. checkRow57423(testSheet, 17, "17");
  337. checkRow57423(testSheet, 18, "18");
  338. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  339. try {
  340. ((XSSFSheet)testSheet).write(stream);
  341. } finally {
  342. stream.close();
  343. }
  344. // verify that the resulting XML has the rows in correct order as required by Excel
  345. String xml = new String(stream.toByteArray());
  346. int posR12 = xml.indexOf("<row r=\"12\"");
  347. int posR13 = xml.indexOf("<row r=\"13\"");
  348. // both need to be found
  349. assertTrue(posR12 != -1);
  350. assertTrue(posR13 != -1);
  351. assertTrue("Need to find row 12 before row 13 after the shifting, but had row 12 at " + posR12 + " and row 13 at " + posR13,
  352. posR12 < posR13);
  353. }
  354. private void checkRow57423(Sheet testSheet, int rowNum, String contents) {
  355. Row row = testSheet.getRow(rowNum);
  356. assertNotNull("Expecting row at rownum " + rowNum, row);
  357. CTRow ctRow = ((XSSFRow)row).getCTRow();
  358. assertEquals(rowNum+1, ctRow.getR());
  359. Cell cell = row.getCell(0);
  360. assertNotNull("Expecting cell at rownum " + rowNum, cell);
  361. assertEquals("Did not have expected contents at rownum " + rowNum,
  362. contents + ".0", cell.toString());
  363. }
  364. }