Search in sources :

Example 1 with NullOutputStream

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();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) OpenXML4JRuntimeException(org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException) NullOutputStream(org.apache.poi.util.NullOutputStream)

Example 2 with NullOutputStream

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);
        }
    }
}
Also used : InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) NullOutputStream(org.apache.poi.util.NullOutputStream)

Example 3 with NullOutputStream

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();
}
Also used : PrintStream(java.io.PrintStream) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) NullOutputStream(org.apache.poi.util.NullOutputStream) Test(org.junit.Test)

Example 4 with NullOutputStream

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();
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) NullOutputStream(org.apache.poi.util.NullOutputStream) NullOutputStream(org.apache.poi.util.NullOutputStream) Test(org.junit.Test)

Aggregations

NullOutputStream (org.apache.poi.util.NullOutputStream)4 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)2 Test (org.junit.Test)2 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1 OpenXML4JRuntimeException (org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException)1 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)1 TempFile (org.apache.poi.util.TempFile)1