]> source.dussan.org Git - poi.git/commitdiff
Add verifying testcase for Bug 57423 and Bug 55752, both are still not solved, though.
authorDominik Stadler <centic@apache.org>
Thu, 30 Apr 2015 11:54:13 +0000 (11:54 +0000)
committerDominik Stadler <centic@apache.org>
Thu, 30 Apr 2015 11:54:13 +0000 (11:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1676965 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestUnfixedBugs.java
test-data/spreadsheet/57423.xlsx [new file with mode: 0644]

index 0e5864b365f9256d868a902dfb7f88affa27d089..b40c4ac1eab7a57a6e6302b2f0deff7653a1867d 100644 (file)
 \r
 package org.apache.poi.xssf.usermodel;\r
 \r
+import java.io.ByteArrayOutputStream;\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
 import java.io.IOException;\r
+import java.io.OutputStream;\r
 import java.nio.charset.Charset;\r
 import java.util.Calendar;\r
 import java.util.Date;\r
@@ -27,6 +31,7 @@ import junit.framework.TestCase;
 \r
 import org.apache.poi.hssf.HSSFTestDataSamples;\r
 import org.apache.poi.ss.usermodel.Cell;\r
+import org.apache.poi.ss.usermodel.CellStyle;\r
 import org.apache.poi.ss.usermodel.DataFormatter;\r
 import org.apache.poi.ss.usermodel.DateUtil;\r
 import org.apache.poi.ss.usermodel.FormulaError;\r
@@ -34,10 +39,14 @@ import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.ss.usermodel.Row;\r
 import org.apache.poi.ss.usermodel.Sheet;\r
 import org.apache.poi.ss.usermodel.Workbook;\r
+import org.apache.poi.ss.util.CellRangeAddress;\r
+import org.apache.poi.ss.util.CellUtil;\r
+import org.apache.poi.ss.util.RegionUtil;\r
 import org.apache.poi.xssf.SXSSFITestDataProvider;\r
 import org.apache.poi.xssf.XSSFTestDataSamples;\r
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;\r
 import org.junit.Test;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;\r
 \r
 /**\r
  * @author centic\r
@@ -251,7 +260,7 @@ public final class TestUnfixedBugs extends TestCase {
     * @param row 0-based\r
     * @param column 0-based\r
     */\r
-   void setCellFormula(Sheet sheet, int row, int column, String formula) {\r
+   private void setCellFormula(Sheet sheet, int row, int column, String formula) {\r
        Row r = sheet.getRow(row);\r
        if (r == null) {\r
            r = sheet.createRow(row);\r
@@ -268,7 +277,163 @@ public final class TestUnfixedBugs extends TestCase {
     * @param rowNo 0-based\r
     * @param column 0-based\r
     */\r
-   Cell getCell(Sheet sheet, int rowNo, int column) {\r
+   private Cell getCell(Sheet sheet, int rowNo, int column) {\r
        return sheet.getRow(rowNo).getCell(column);\r
    }\r
+\r
+   @Test\r
+   public void testBug55752() throws IOException {\r
+       Workbook wb = new XSSFWorkbook();\r
+       try {\r
+           Sheet sheet = wb.createSheet("test");\r
+    \r
+           for (int i = 0; i < 4; i++) {\r
+               Row row = sheet.createRow(i);\r
+               for (int j = 0; j < 2; j++) {\r
+                   Cell cell = row.createCell(j);\r
+                   cell.setCellStyle(wb.createCellStyle());\r
+               }\r
+           }\r
+    \r
+           // set content\r
+           Row row1 = sheet.getRow(0);\r
+           row1.getCell(0).setCellValue("AAA");\r
+           Row row2 = sheet.getRow(1);\r
+           row2.getCell(0).setCellValue("BBB");\r
+           Row row3 = sheet.getRow(2);\r
+           row3.getCell(0).setCellValue("CCC");\r
+           Row row4 = sheet.getRow(3);\r
+           row4.getCell(0).setCellValue("DDD");\r
+    \r
+           // merge cells\r
+           CellRangeAddress range1 = new CellRangeAddress(0, 0, 0, 1);\r
+           sheet.addMergedRegion(range1);\r
+           CellRangeAddress range2 = new CellRangeAddress(1, 1, 0, 1);\r
+           sheet.addMergedRegion(range2);\r
+           CellRangeAddress range3 = new CellRangeAddress(2, 2, 0, 1);\r
+           sheet.addMergedRegion(range3);\r
+           assertEquals(0, range3.getFirstColumn());\r
+           assertEquals(1, range3.getLastColumn());\r
+           assertEquals(2, range3.getLastRow());\r
+           CellRangeAddress range4 = new CellRangeAddress(3, 3, 0, 1);\r
+           sheet.addMergedRegion(range4);\r
+           \r
+           // set border\r
+           RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range1, sheet, wb);\r
+           \r
+           row2.getCell(0).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);\r
+           row2.getCell(1).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);\r
+\r
+           Cell cell0 = CellUtil.getCell(row3, 0);\r
+           CellUtil.setCellStyleProperty(cell0, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);\r
+           Cell cell1 = CellUtil.getCell(row3, 1);\r
+           CellUtil.setCellStyleProperty(cell1, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);\r
+\r
+           RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range4, sheet, wb);\r
+    \r
+           // write to file\r
+           OutputStream stream = new FileOutputStream(new File("C:/temp/55752.xlsx"));\r
+           try {\r
+               wb.write(stream);\r
+           } finally {\r
+               stream.close();\r
+           }\r
+       } finally {\r
+           wb.close();\r
+       }\r
+   }\r
+\r
+   @Test\r
+   public void test57423() throws IOException {        \r
+       Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57423.xlsx");\r
+       \r
+       Sheet testSheet = wb.getSheetAt(0);\r
+\r
+       // row shift (negative or positive) causes corrupted output xlsx file when the shift value is bigger \r
+       // than the number of rows being shifted \r
+       // Excel 2010 on opening the output file says:\r
+       // "Excel found unreadable content" and offers recovering the file by removing the unreadable content\r
+       // This can be observed in cases like the following:\r
+       // negative shift of 1 row by less than -1\r
+       // negative shift of 2 rows by less than -2\r
+       // positive shift of 1 row by 2 or more \r
+       // positive shift of 2 rows by 3 or more\r
+       \r
+       //testSheet.shiftRows(4, 5, -3);\r
+       testSheet.shiftRows(10, 10, 2);\r
+       \r
+       checkRows57423(testSheet);\r
+       \r
+       Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);\r
+\r
+       /*FileOutputStream stream = new FileOutputStream("C:\\temp\\57423.xlsx");\r
+       try {\r
+           wb.write(stream);\r
+       } finally {\r
+           stream.close();\r
+       }*/\r
+\r
+       wb.close();\r
+       \r
+       checkRows57423(wbBack.getSheetAt(0));\r
+       \r
+       wbBack.close();\r
+   }\r
+\r
+   private void checkRows57423(Sheet testSheet) throws IOException {\r
+       checkRow57423(testSheet, 0, "0");\r
+       checkRow57423(testSheet, 1, "1");\r
+       checkRow57423(testSheet, 2, "2");\r
+       checkRow57423(testSheet, 3, "3");\r
+       checkRow57423(testSheet, 4, "4");\r
+       checkRow57423(testSheet, 5, "5");\r
+       checkRow57423(testSheet, 6, "6");\r
+       checkRow57423(testSheet, 7, "7");\r
+       checkRow57423(testSheet, 8, "8");\r
+       checkRow57423(testSheet, 9, "9");\r
+       \r
+       assertNull("Row number 10 should be gone after the shift", \r
+               testSheet.getRow(10));\r
+       \r
+       checkRow57423(testSheet, 11, "11");\r
+       checkRow57423(testSheet, 12, "10");\r
+       checkRow57423(testSheet, 13, "13");\r
+       checkRow57423(testSheet, 14, "14");\r
+       checkRow57423(testSheet, 15, "15");\r
+       checkRow57423(testSheet, 16, "16");\r
+       checkRow57423(testSheet, 17, "17");\r
+       checkRow57423(testSheet, 18, "18");\r
+       \r
+       ByteArrayOutputStream stream = new ByteArrayOutputStream();\r
+       try {\r
+           ((XSSFSheet)testSheet).write(stream);\r
+       } finally {\r
+           stream.close();\r
+       }\r
+       \r
+       // verify that the resulting XML has the rows in correct order as required by Excel\r
+       String xml = new String(stream.toByteArray());\r
+       int posR12 = xml.indexOf("<row r=\"12\"");\r
+       int posR13 = xml.indexOf("<row r=\"13\"");\r
+       \r
+       // both need to be found\r
+       assertTrue(posR12 != -1);\r
+       assertTrue(posR13 != -1);\r
+       \r
+       assertTrue("Need to find row 12 before row 13 after the shifting, but had row 12 at " + posR12 + " and row 13 at " + posR13, \r
+               posR12 < posR13);\r
+   }\r
+\r
+   private void checkRow57423(Sheet testSheet, int rowNum, String contents) {\r
+       Row row = testSheet.getRow(rowNum);\r
+       assertNotNull("Expecting row at rownum " + rowNum, row);\r
+       \r
+       CTRow ctRow = ((XSSFRow)row).getCTRow();\r
+       assertEquals(rowNum+1, ctRow.getR());\r
+       \r
+       Cell cell = row.getCell(0);\r
+       assertNotNull("Expecting cell at rownum " + rowNum, cell);\r
+       assertEquals("Did not have expected contents at rownum " + rowNum, \r
+               contents + ".0", cell.toString());\r
+   }\r
 }\r
diff --git a/test-data/spreadsheet/57423.xlsx b/test-data/spreadsheet/57423.xlsx
new file mode 100644 (file)
index 0000000..54c65be
Binary files /dev/null and b/test-data/spreadsheet/57423.xlsx differ