]> source.dussan.org Git - poi.git/commitdiff
Add reproducing unit tests to TestUnfixedBugs for Bug 53798 and Bug 54071
authorDominik Stadler <centic@apache.org>
Tue, 9 Jul 2013 13:55:54 +0000 (13:55 +0000)
committerDominik Stadler <centic@apache.org>
Tue, 9 Jul 2013 13:55:54 +0000 (13:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1501263 13f79535-47bb-0310-9956-ffa450edef68

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

index ce3fc2d0457e8fff7605adba763fa74930da3c62..cdeeaec3124c7ce9f6f9374f21eb960fafb11fba 100644 (file)
 \r
 package org.apache.poi.xssf.usermodel;\r
 \r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
 import java.io.IOException;\r
 import java.io.UnsupportedEncodingException;\r
 \r
-import junit.framework.AssertionFailedError;\r
 import junit.framework.TestCase;\r
 \r
 import org.apache.poi.hssf.HSSFTestDataSamples;\r
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
 import org.apache.poi.ss.usermodel.Cell;\r
+import org.apache.poi.ss.usermodel.Font;\r
 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.util.TempFile;\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.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontImpl;\r
 \r
 /**\r
  * @author centic\r
- * \r
+ *\r
  * This testcase contains tests for bugs that are yet to be fixed. Therefore,\r
  * the standard ant test target does not run these tests. Run this testcase with\r
  * the single-test target. The names of the tests usually correspond to the\r
@@ -46,7 +51,7 @@ public final class TestUnfixedBugs extends TestCase {
     public void testBug54084Unicode() throws IOException {\r
         // sample XLSX with the same text-contents as the text-file above\r
         XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("54084 - Greek - beyond BMP.xlsx");\r
-        \r
+\r
         verifyBug54084Unicode(wb);\r
 \r
 //        OutputStream baos = new FileOutputStream("/tmp/test.xlsx");\r
@@ -55,7 +60,7 @@ public final class TestUnfixedBugs extends TestCase {
 //        } finally {\r
 //            baos.close();\r
 //        }\r
-        \r
+\r
         // now write the file and read it back in\r
         XSSFWorkbook wbWritten = XSSFTestDataSamples.writeOutAndReadBack(wb);\r
         verifyBug54084Unicode(wbWritten);\r
@@ -68,14 +73,134 @@ public final class TestUnfixedBugs extends TestCase {
     private void verifyBug54084Unicode(Workbook wb) throws UnsupportedEncodingException {\r
         // expected data is stored in UTF-8 in a text-file\r
         String testData = new String(HSSFTestDataSamples.getTestDataFileContent("54084 - Greek - beyond BMP.txt"), "UTF-8").trim();\r
-        \r
+\r
         Sheet sheet = wb.getSheetAt(0);\r
         Row row = sheet.getRow(0);\r
         Cell cell = row.getCell(0);\r
-        \r
+\r
         String value = cell.getStringCellValue();\r
         //System.out.println(value);\r
-        \r
+\r
         assertEquals("The data in the text-file should exactly match the data that we read from the workbook", testData, value);\r
-    }   \r
+    }\r
+\r
+       public void test54071() {\r
+               Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("54071.xlsx");\r
+               Sheet sheet = workbook.getSheetAt(0);\r
+               int rows = sheet.getPhysicalNumberOfRows();\r
+               System.out.println(">> file rows is:"+(rows-1)+" <<");\r
+               Row title = sheet.getRow(0);\r
+\r
+               for (int row = 1; row < rows; row++) {\r
+                       Row rowObj = sheet.getRow(row);\r
+                       for (int col = 0; col < 1; col++) {\r
+                               String titleName = title.getCell(col).toString();\r
+                               Cell cell = rowObj.getCell(col);\r
+                               if (titleName.startsWith("time")) {\r
+                                       // here the output will produce ...59 or ...58 for the rows, probably POI is\r
+                                       // doing some different rounding or some other small difference...\r
+                                       System.out.println("==Time:"+cell.getDateCellValue());\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       public void testBug53798XLSX() throws IOException {\r
+       XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xlsx");\r
+        File xlsOutput = TempFile.createTempFile("testBug53798", ".xlsx");\r
+        bug53798Work(wb, xlsOutput);\r
+       }\r
+\r
+       // Disabled because shift rows is not yet implemented for SXSSFWorkbook\r
+       public void disabled_testBug53798XLSXStream() throws IOException {\r
+       XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xlsx");\r
+        File xlsOutput = TempFile.createTempFile("testBug53798", ".xlsx");\r
+        bug53798Work(new SXSSFWorkbook(wb), xlsOutput);\r
+       }\r
+\r
+       public void testBug53798XLS() throws IOException {\r
+       Workbook wb = HSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xls");\r
+        File xlsOutput = TempFile.createTempFile("testBug53798", ".xls");\r
+        bug53798Work(wb, xlsOutput);\r
+       }\r
+\r
+       private void bug53798Work(Workbook wb, File xlsOutput) throws IOException {\r
+               Sheet testSheet = wb.getSheetAt(0);\r
+\r
+               testSheet.shiftRows(2, 2, 1);\r
+\r
+               saveAndReloadReport(wb, xlsOutput);\r
+\r
+               // 1) corrupted xlsx (unreadable data in the first row of a shifted group) already comes about\r
+               // when shifted by less than -1 negative amount (try -2)\r
+               testSheet.shiftRows(3, 3, -1);\r
+\r
+               saveAndReloadReport(wb, xlsOutput);\r
+\r
+               testSheet.shiftRows(2, 2, 1);\r
+\r
+               saveAndReloadReport(wb, xlsOutput);\r
+\r
+               Row newRow = null;\r
+               Cell newCell = null;\r
+               // 2) attempt to create a new row IN PLACE of a removed row by a negative shift causes corrupted\r
+               // xlsx file with  unreadable data in the negative shifted row.\r
+               // NOTE it's ok to create any other row.\r
+               newRow = testSheet.createRow(3);\r
+\r
+               saveAndReloadReport(wb, xlsOutput);\r
+\r
+               newCell = newRow.createCell(0);\r
+\r
+               saveAndReloadReport(wb, xlsOutput);\r
+\r
+               newCell.setCellValue("new Cell in row "+newRow.getRowNum());\r
+\r
+               saveAndReloadReport(wb, xlsOutput);\r
+\r
+               // 3) once a negative shift has been made any attempt to shift another group of rows\r
+               // (note: outside of previously negative shifted rows) by a POSITIVE amount causes POI exception:\r
+               // org.apache.xmlbeans.impl.values.XmlValueDisconnectedException.\r
+               // NOTE: another negative shift on another group of rows is successful, provided no new rows in\r
+               // place of previously shifted rows were attempted to be created as explained above.\r
+               testSheet.shiftRows(6, 7, 1);   // -- CHANGE the shift to positive once the behaviour of\r
+                                                                               // the above has been tested\r
+\r
+               saveAndReloadReport(wb, xlsOutput);\r
+       }\r
+\r
+       private void saveAndReloadReport(Workbook wb, File outFile) throws IOException {\r
+               // run some method on the font to verify if it is "disconnected" already\r
+               //for(short i = 0;i < 256;i++)\r
+               {\r
+                       Font font = wb.getFontAt((short)0);\r
+                       if(font instanceof XSSFFont) {\r
+                               XSSFFont xfont = (XSSFFont) wb.getFontAt((short)0);\r
+                               CTFontImpl ctFont = (CTFontImpl) xfont.getCTFont();\r
+                               assertEquals(0, ctFont.sizeOfBArray());\r
+                       }\r
+               }\r
+\r
+               FileOutputStream fileOutStream = new FileOutputStream(outFile);\r
+               wb.write(fileOutStream);\r
+               fileOutStream.close();\r
+               //System.out.println("File \""+outFile.getName()+"\" has been saved successfully");\r
+\r
+               FileInputStream is = new FileInputStream(outFile);\r
+               try {\r
+                       final Workbook newWB;\r
+                       if(wb instanceof XSSFWorkbook) {\r
+                               newWB = new XSSFWorkbook(is);\r
+                       } else if(wb instanceof HSSFWorkbook) {\r
+                               newWB = new HSSFWorkbook(is);\r
+                       } else if(wb instanceof SXSSFWorkbook) {\r
+                               newWB = new SXSSFWorkbook(new XSSFWorkbook(is));\r
+                       } else {\r
+                               throw new IllegalStateException("Unknown workbook: " + wb);\r
+                       }\r
+                       assertNotNull(newWB.getSheet("test"));\r
+               } finally {\r
+                       is.close();\r
+               }\r
+       }\r
 }\r
diff --git a/test-data/spreadsheet/53798_shiftNegative_TMPL.xls b/test-data/spreadsheet/53798_shiftNegative_TMPL.xls
new file mode 100644 (file)
index 0000000..1f9f48f
Binary files /dev/null and b/test-data/spreadsheet/53798_shiftNegative_TMPL.xls differ
diff --git a/test-data/spreadsheet/53798_shiftNegative_TMPL.xlsx b/test-data/spreadsheet/53798_shiftNegative_TMPL.xlsx
new file mode 100644 (file)
index 0000000..f273308
Binary files /dev/null and b/test-data/spreadsheet/53798_shiftNegative_TMPL.xlsx differ
diff --git a/test-data/spreadsheet/54071.xlsx b/test-data/spreadsheet/54071.xlsx
new file mode 100644 (file)
index 0000000..be3a3e9
Binary files /dev/null and b/test-data/spreadsheet/54071.xlsx differ