Search in sources :

Example 1 with PackageAccess

use of org.apache.poi.openxml4j.opc.PackageAccess in project poi by apache.

the class TestXSSFBugs method bug57482.

/**
     * A .xlsx file with no Shared Strings table should open fine
     * in read-only mode
     */
@SuppressWarnings("resource")
@Test
public void bug57482() throws IOException, InvalidFormatException {
    for (PackageAccess access : new PackageAccess[] { PackageAccess.READ_WRITE, PackageAccess.READ }) {
        File file = HSSFTestDataSamples.getSampleFile("57482-OnlyNumeric.xlsx");
        OPCPackage pkg = OPCPackage.open(file, access);
        try {
            // Try to open it and read the contents
            XSSFWorkbook wb1 = new XSSFWorkbook(pkg);
            assertNotNull(wb1.getSharedStringSource());
            assertEquals(0, wb1.getSharedStringSource().getCount());
            DataFormatter fmt = new DataFormatter();
            XSSFSheet s = wb1.getSheetAt(0);
            assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
            assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
            assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
            // Add a text cell
            s.getRow(0).createCell(3).setCellValue("Testing");
            assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
            // Try to write-out and read again, should only work
            //  in read-write mode, not read-only mode
            XSSFWorkbook wb2 = null;
            try {
                wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
                if (access == PackageAccess.READ) {
                    fail("Shouln't be able to write from read-only mode");
                }
                // Check again
                s = wb2.getSheetAt(0);
                assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
                assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
                assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
                assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
            } catch (InvalidOperationException e) {
                if (access == PackageAccess.READ_WRITE) {
                    // Shouldn't occur in write-mode
                    throw e;
                }
            } finally {
                if (wb2 != null) {
                    wb2.getPackage().revert();
                }
            }
            wb1.getPackage().revert();
        } finally {
            pkg.revert();
        }
    }
}
Also used : PackageAccess(org.apache.poi.openxml4j.opc.PackageAccess) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) TempFile(org.apache.poi.util.TempFile) ZipSecureFile(org.apache.poi.openxml4j.util.ZipSecureFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Aggregations

File (java.io.File)1 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)1 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)1 PackageAccess (org.apache.poi.openxml4j.opc.PackageAccess)1 ZipSecureFile (org.apache.poi.openxml4j.util.ZipSecureFile)1 TempFile (org.apache.poi.util.TempFile)1 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)1 Test (org.junit.Test)1