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();
}
}
}
Aggregations