use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class TestPOIXMLDocument method assertReadWrite.
public void assertReadWrite(OPCPackage pkg1) throws Exception {
OPCParser doc = new OPCParser(pkg1);
doc.parse(new TestFactory());
traverse(doc);
File tmp = TempFile.createTempFile("poi-ooxml", ".tmp");
FileOutputStream out = new FileOutputStream(tmp);
doc.write(out);
out.close();
// Should not be able to write to an output stream that has been closed
try {
doc.write(out);
fail("Should not be able to write to an output stream that has been closed.");
} catch (final OpenXML4JRuntimeException e) {
// see {@link org.apache.poi.openxml4j.opc.ZipPackage#saveImpl(java.io.OutputStream)}
if (e.getMessage().matches("Fail to save: an error occurs while saving the package : The part .+ failed to be saved in the stream with marshaller .+")) {
// expected
} else {
throw e;
}
}
// Should not be able to write a document that has been closed
doc.close();
try {
doc.write(new NullOutputStream());
fail("Should not be able to write a document that has been closed.");
} catch (final IOException e) {
if (e.getMessage().equals("Cannot write data, document seems to have been closed already")) {
// expected
} else {
throw e;
}
}
// Should be able to close a document multiple times, though subsequent closes will have no effect.
doc.close();
@SuppressWarnings("resource") OPCPackage pkg2 = OPCPackage.open(tmp.getAbsolutePath());
doc = new OPCParser(pkg1);
try {
doc.parse(new TestFactory());
traverse(doc);
assertEquals(pkg1.getRelationships().size(), pkg2.getRelationships().size());
ArrayList<PackagePart> l1 = pkg1.getParts();
ArrayList<PackagePart> l2 = pkg2.getParts();
assertEquals(l1.size(), l2.size());
for (int i = 0; i < l1.size(); i++) {
PackagePart p1 = l1.get(i);
PackagePart p2 = l2.get(i);
assertEquals(p1.getContentType(), p2.getContentType());
assertEquals(p1.hasRelationships(), p2.hasRelationships());
if (p1.hasRelationships()) {
assertEquals(p1.getRelationships().size(), p2.getRelationships().size());
}
assertEquals(p1.getPartName(), p2.getPartName());
}
} finally {
doc.close();
pkg1.close();
pkg2.close();
}
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class TestXSLFSlideShow method testContainsMainContentType.
@Test
public void testContainsMainContentType() throws Exception {
boolean found = false;
for (PackagePart part : pack.getParts()) {
if (part.getContentType().equals(XSLFRelation.MAIN.getContentType())) {
found = true;
}
}
assertTrue(found);
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class TestXMLSlideShow method testContainsMainContentType.
@Test
public void testContainsMainContentType() throws Exception {
boolean found = false;
for (PackagePart part : pack.getParts()) {
if (part.getContentType().equals(XSLFRelation.MAIN.getContentType())) {
found = true;
}
}
assertTrue(found);
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class TestXSSFWorkbook method bug47668.
/**
* Verify that the attached test data was not modified. If this test method
* fails, the test data is not working properly.
*/
@Test
public void bug47668() throws Exception {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("47668.xlsx");
List<XSSFPictureData> allPictures = workbook.getAllPictures();
assertEquals(1, allPictures.size());
PackagePartName imagePartName = PackagingURIHelper.createPartName("/xl/media/image1.jpeg");
PackagePart imagePart = workbook.getPackage().getPart(imagePartName);
assertNotNull(imagePart);
for (XSSFPictureData pictureData : allPictures) {
PackagePart picturePart = pictureData.getPackagePart();
assertSame(imagePart, picturePart);
}
XSSFSheet sheet0 = workbook.getSheetAt(0);
XSSFDrawing drawing0 = sheet0.createDrawingPatriarch();
XSSFPictureData pictureData0 = (XSSFPictureData) drawing0.getRelations().get(0);
byte[] data0 = pictureData0.getData();
CRC32 crc0 = new CRC32();
crc0.update(data0);
XSSFSheet sheet1 = workbook.getSheetAt(1);
XSSFDrawing drawing1 = sheet1.createDrawingPatriarch();
XSSFPictureData pictureData1 = (XSSFPictureData) drawing1.getRelations().get(0);
byte[] data1 = pictureData1.getData();
CRC32 crc1 = new CRC32();
crc1.update(data1);
assertEquals(crc0.getValue(), crc1.getValue());
workbook.close();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class TestXWPFDocument method testContainsMainContentType.
@Test
public void testContainsMainContentType() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
OPCPackage pack = doc.getPackage();
boolean found = false;
for (PackagePart part : pack.getParts()) {
if (part.getContentType().equals(XWPFRelation.DOCUMENT.getContentType())) {
found = true;
}
// if (false) {
// // successful tests should be silent
// System.out.println(part);
// }
}
assertTrue(found);
pack.close();
doc.close();
}
Aggregations