\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
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
// } 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
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