use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestPOIXMLDocument method testVSDXPart.
@Test
public void testVSDXPart() throws IOException {
POIDataSamples pds = POIDataSamples.getDiagramInstance();
OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
POIXMLDocumentPart part = new POIXMLDocumentPart(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT);
assertNotNull(part);
assertEquals(0, part.getRelationCounter());
open.close();
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestPOIXMLDocument method testGetNextPartNumber.
@Test
public void testGetNextPartNumber() throws Exception {
POIDataSamples pds = POIDataSamples.getDocumentInstance();
@SuppressWarnings("resource") OPCPackage pkg = PackageHelper.open(pds.openResourceAsStream("WordWithAttachments.docx"));
OPCParser doc = new OPCParser(pkg);
try {
doc.parse(new TestFactory());
// Non-indexed parts: Word is taken, Excel is not
assertEquals(-1, doc.getNextPartNumber(XWPFRelation.DOCUMENT, 0));
assertEquals(-1, doc.getNextPartNumber(XWPFRelation.DOCUMENT, -1));
assertEquals(-1, doc.getNextPartNumber(XWPFRelation.DOCUMENT, 99));
assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKBOOK, 0));
assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKBOOK, -1));
assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKBOOK, 99));
// Indexed parts:
// Has 2 headers
assertEquals(0, doc.getNextPartNumber(XWPFRelation.HEADER, 0));
assertEquals(3, doc.getNextPartNumber(XWPFRelation.HEADER, -1));
assertEquals(3, doc.getNextPartNumber(XWPFRelation.HEADER, 1));
assertEquals(8, doc.getNextPartNumber(XWPFRelation.HEADER, 8));
// Has no Excel Sheets
assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKSHEET, 0));
assertEquals(1, doc.getNextPartNumber(XSSFRelation.WORKSHEET, -1));
assertEquals(1, doc.getNextPartNumber(XSSFRelation.WORKSHEET, 1));
} finally {
doc.close();
}
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestPOIXMLDocument method testVSDX.
@Test
public void testVSDX() throws Exception {
POIDataSamples pds = POIDataSamples.getDiagramInstance();
@SuppressWarnings("resource") OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
POIXMLDocument part = new OPCParser(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT);
assertNotNull(part);
assertEquals(0, part.getRelationCounter());
part.close();
}
use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.
the class TestXSSFBugs method bug54764.
@Test
public void bug54764() throws IOException, OpenXML4JException, XmlException {
OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("54764.xlsx");
// Check the core properties - will be found but empty, due
// to the expansion being too much to be considered valid
POIXMLProperties props = new POIXMLProperties(pkg);
assertEquals(null, props.getCoreProperties().getTitle());
assertEquals(null, props.getCoreProperties().getSubject());
assertEquals(null, props.getCoreProperties().getDescription());
// Now check the spreadsheet itself
try {
new XSSFWorkbook(pkg).close();
fail("Should fail as too much expansion occurs");
} catch (POIXMLException e) {
// Expected
}
pkg.close();
// Try with one with the entities in the Content Types
try {
XSSFTestDataSamples.openSamplePackage("54764-2.xlsx").close();
fail("Should fail as too much expansion occurs");
} catch (Exception e) {
// Expected
}
// Check we can still parse valid files after all that
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
assertEquals(3, wb.getNumberOfSheets());
wb.close();
}
use of org.apache.poi.openxml4j.opc.OPCPackage 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