AnonymousOdsFileWriter { public void save(final OutputStream out) throws IOException { final ZipUTF8Writer writer = ZipUTF8WriterImpl.builder().build(out); this.save(writer); writer.finish(); writer.flush(); } AnonymousOdsFileWriter(final Logger logger, final AnonymousOdsDocument document); OdsDocument document(); void save(final OutputStream out); @Deprecated void save(final OutputStream out, final ZipUTF8WriterBuilderImpl builder); void save(final ZipUTF8Writer writer); void saveAs(final String filename); void saveAs(final File file); void saveAs(final String filename, final ZipUTF8WriterBuilder builder); void saveAs(final File file, final ZipUTF8WriterBuilder builder);  }
@Test public final void testSaveEmptyDocumentToStream() throws IOException { final AnonymousOdsFileWriter writer = this.odsFactory.createWriter(); PowerMock.resetAll(); PowerMock.replayAll(); try { writer.save(this.os); } finally { this.os.close(); } PowerMock.verifyAll(); final byte[] buf = this.os.toByteArray(); final InputStream is = new ByteArrayInputStream(buf); final ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = zis.getNextEntry(); final Collection<String> names = new HashSet<String>(); while (entry != null) { names.add(entry.getName()); entry = zis.getNextEntry(); } if (Math.abs(EMPTY_DOCUMENT_SIZE - buf.length) > DELTA) { System.out.println( String.format("Expected size: %d, actual size: %d", EMPTY_DOCUMENT_SIZE, buf.length)); Assert.fail(); } Assert.assertEquals( Sets.newHashSet("settings.xml", "Configurations2/images/Bitmaps/", "Configurations2/toolbar/", "META-INF/manifest.xml", "Thumbnails/", "Configurations2/floater/", "Configurations2/menubar/", "mimetype", "meta.xml", "Configurations2/accelerator/current.xml", "Configurations2/popupmenu/", "styles.xml", "content.xml", "Configurations2/progressbar/", "Configurations2/statusbar/"), names); }
@Test public final void testSaveEmptyDocumentToStreamAndAddPrePostamble() throws IOException { final AnonymousOdsFileWriter writer = this.odsFactory.createWriter(); PowerMock.resetAll(); PowerMock.replayAll(); try { this.os.write("preamble".getBytes()); writer.save(this.os); this.os.write("postamble".getBytes()); } finally { this.os.close(); } PowerMock.verifyAll(); final byte[] buf = this.os.toByteArray(); final byte[] pre = Arrays.copyOfRange(buf, 0, 8); final byte[] body = Arrays.copyOfRange(buf, 8, buf.length - 9); final byte[] post = Arrays.copyOfRange(buf, buf.length - 9, buf.length); final Charset charset = Charset.forName("US-ASCII"); Assert.assertArrayEquals(pre, "preamble".getBytes(charset)); Assert.assertArrayEquals(post, "postamble".getBytes(charset)); final InputStream is = new ByteArrayInputStream(body); final ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = zis.getNextEntry(); final Collection<String> names = new HashSet<String>(); while (entry != null) { names.add(entry.getName()); entry = zis.getNextEntry(); } Assert.assertEquals( Sets.newHashSet("settings.xml", "Configurations2/images/Bitmaps/", "Configurations2/toolbar/", "META-INF/manifest.xml", "Thumbnails/", "Configurations2/floater/", "Configurations2/menubar/", "mimetype", "meta.xml", "Configurations2/accelerator/current.xml", "Configurations2/popupmenu/", "styles.xml", "content.xml", "Configurations2/progressbar/", "Configurations2/statusbar/"), names); }
@Test public final void testSaveEmptyDocumentToWriterAndAddEntry() throws IOException { final AnonymousOdsFileWriter writer = this.odsFactory.createWriter(); final ZipUTF8Writer zw = ZipUTF8WriterImpl.builder().build(this.os); PowerMock.resetAll(); PowerMock.replayAll(); try { writer.save(zw); zw.putAndRegisterNextEntry(new StandardManifestEntry("last", null, null)); zw.append("last content"); zw.closeEntry(); } finally { zw.finish(); zw.close(); } PowerMock.verifyAll(); final InputStream is = new ByteArrayInputStream(this.os.toByteArray()); final ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = zis.getNextEntry(); final Collection<String> names = new HashSet<String>(); while (entry != null) { names.add(entry.getName()); entry = zis.getNextEntry(); } Assert.assertEquals( Sets.newHashSet("settings.xml", "last", "Configurations2/images/Bitmaps/", "Configurations2/toolbar/", "META-INF/manifest.xml", "Thumbnails/", "Configurations2/floater/", "Configurations2/menubar/", "mimetype", "meta.xml", "Configurations2/accelerator/current.xml", "Configurations2/popupmenu/", "styles.xml", "content.xml", "Configurations2/progressbar/", "Configurations2/statusbar/"), names); }
@Test public final void testSaveTwiceEmptyDocumentToStream() throws IOException { final AnonymousOdsFileWriter writer = this.odsFactory.createWriter(); PowerMock.resetAll(); PowerMock.replayAll(); try { writer.save(this.os); writer.save(this.os); } finally { this.os.close(); } PowerMock.verifyAll(); final byte[] buf = this.os.toByteArray(); final InputStream is = new ByteArrayInputStream(buf); final ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = zis.getNextEntry(); final Set<String> names = new HashSet<String>(); while (entry != null) { names.add(entry.getName()); entry = zis.getNextEntry(); } if (Math.abs(EMPTY_DOCUMENT_SIZE * 2 - buf.length) > 2*DELTA) { System.out.println( String.format("Expected size: %d, actual size: %d", EMPTY_DOCUMENT_SIZE * 2, buf.length)); Assert.fail(); } Assert.assertEquals( new HashSet<String>(Arrays.asList("Configurations2/accelerator/current.xml", "Configurations2/floater/", "Configurations2/images/Bitmaps/", "Configurations2/menubar/", "Configurations2/popupmenu/", "Configurations2/progressbar/", "Configurations2/statusbar/", "Configurations2/toolbar/", "META-INF/manifest.xml", "Thumbnails/", "content.xml", "meta.xml", "mimetype", "settings.xml", "styles.xml")), names); }
@Test public final void testSaveWriter() throws IOException { final ZipUTF8WriterBuilderImpl zb = PowerMock.createMock(ZipUTF8WriterBuilderImpl.class); final ZipUTF8Writer z = PowerMock.createMock(ZipUTF8Writer.class); final File temp = File.createTempFile("tempfile", ".tmp"); PowerMock.resetAll(); TestHelper.initMockDocument(this.odsElements); EasyMock.expect(zb.build(EasyMock.isA(FileOutputStream.class))).andReturn(z); this.odsElements.saveAsync(); PowerMock.replayAll(); final NamedOdsDocument document = this.getNamedDocument(); final NamedOdsFileWriter writer = new OdsFileWriterBuilder(this.logger, document).zipBuilder(zb) .file(temp.getAbsolutePath()).build(); writer.save(); PowerMock.verifyAll(); }
@Test public final void testSaveWriterWithException() throws IOException { final OutputStream outputStream = PowerMock.createMock(OutputStream.class); PowerMock.resetAll(); TestHelper.initMockDocument(this.odsElements); this.odsElements .createEmptyElements(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class)); this.odsElements .writeMimeType(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class)); this.odsElements.writeMeta(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class)); this.odsElements.writeStyles(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class)); this.odsElements.writeContent(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class)); this.odsElements .writeSettings(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class)); this.odsElements.writeExtras(EasyMock.eq(this.xmlUtil), EasyMock.isA(ZipUTF8Writer.class)); outputStream.write(EasyMock.isA(byte[].class), EasyMock.anyInt(), EasyMock.anyInt()); EasyMock.expectLastCall().anyTimes(); outputStream.flush(); EasyMock.expectLastCall().anyTimes(); outputStream.close(); PowerMock.replayAll(); final AnonymousOdsDocument document = this.getAnonymousDocument(); try { new AnonymousOdsFileWriter(this.logger, document).save(outputStream); } finally { outputStream.close(); } PowerMock.verifyAll(); }