use of org.apache.poi.util.NullOutputStream 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.util.NullOutputStream in project poi by apache.
the class TestSXSSFWorkbook method saveTwice.
private static void saveTwice(Workbook wb) throws Exception {
for (int i = 0; i < 2; i++) {
try {
NullOutputStream out = new NullOutputStream();
wb.write(out);
out.close();
} catch (Exception e) {
throw new Exception("ERROR: failed on " + (i + 1) + "th time calling " + wb.getClass().getName() + ".write() with exception " + e.getMessage(), e);
}
}
}
use of org.apache.poi.util.NullOutputStream in project poi by apache.
the class TestOOXMLLister method testWithPrintStream.
@Test
public void testWithPrintStream() throws IOException, InvalidFormatException {
File file = XSSFTestDataSamples.getSampleFile("Formatting.xlsx");
PrintStream nullStream = new PrintStream(new NullOutputStream(), true, "UTF-8");
OPCPackage opc = OPCPackage.open(file.getAbsolutePath(), PackageAccess.READ);
OOXMLLister lister = new OOXMLLister(opc, nullStream);
lister.displayParts();
lister.displayRelations();
lister.close();
opc.close();
nullStream.close();
}
use of org.apache.poi.util.NullOutputStream in project poi by apache.
the class BaseTestWorkbook method test58499.
@Test
public void test58499() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet();
for (int i = 0; i < 900; i++) {
Row r = sheet.createRow(i);
Cell c = r.createCell(0);
CellStyle cs = workbook.createCellStyle();
c.setCellStyle(cs);
c.setCellValue("AAA");
}
OutputStream os = new NullOutputStream();
try {
workbook.write(os);
} finally {
os.close();
}
//workbook.dispose();
workbook.close();
}
Aggregations