/** * Helper method to run a specified task and automatically handle the closing of the stream. * * @param stream * @param task */ public static <S extends Closeable> void closeOnComplete(S stream, StreamTask<S> task) { closeOnComplete(stream, task, DEFAULT_ERROR_HANDLER); }
/** * Helper method to run a specified task and automatically handle the closing of the stream. * * @param <S> * @param task * @param errorHandler */ public static <S extends Closeable> void closeOnComplete(S stream, StreamTask<S> task, StreamErrorHandler errorHandler) { try { task.execute(stream); } catch (Throwable t) { errorHandler.handle(t); } finally { if (stream != null) { try { stream.close(); } catch (final IOException ignore) { if (log.isLoggable(Level.FINER)) { log.finer("Could not close stream due to: " + ignore.getMessage() + "; ignoring"); } } } } } }
/** * Performs copy operation between currentNodeStream and outputStream using buffer length. * * @throws IOException */ private void doCopy() throws IOException { int copied = IOUtil.copy(currentNodeStream, outputStream, BUFFER_LENGTH); if (copied == -1) { currentNodeStream.close(); currentNodeStream = null; endAsset(); } }
/** * {@inheritDoc} */ @Override public void writeTo(final OutputStream outputStream, final Formatter formatter) throws IllegalArgumentException { try { IOUtil.bufferedWriteWithFlush(outputStream, toString(formatter).getBytes()); } catch (IOException ioe) { throw new IllegalArgumentException("Could not write Archive contents to specified OutputStream", ioe); } }
/** * Assert an asset is actually in the file * * @throws IOException * @throws IllegalArgumentException */ private void assertAssetInTar(final File archive, final ArchivePath path, final Asset asset) throws IllegalArgumentException, IOException { final InputStream in = this.getEntryFromTarFile(archive, path); byte[] expectedContents = IOUtil.asByteArray(asset.openStream()); byte[] actualContents = IOUtil.asByteArray(in); Assert.assertArrayEquals(expectedContents, actualContents); }
@Test(expected = ArchiveExportException.class) public void testExportThrowsArchiveExceptionOnAssetWriteFailure() throws IOException { log.info("testExportThrowsArchiveExceptionOnAssetWriteFailure"); Archive<?> archive = createArchiveWithAssets(); // Check if a the path already contains a node so we remove it from the parent's children if (archive.contains(PATH_ONE)) { archive.delete(PATH_ONE); } archive.add(new Asset() { @Override public InputStream openStream() { throw new RuntimeException("Mock Exception from an Asset write"); } }, PATH_ONE); // Export final InputStream in = this.exportAsInputStream(archive); // Read in the full content (to in turn empty the underlying buffer and ensure we complete) final OutputStream sink = new OutputStream() { @Override public void write(int b) throws IOException { } }; IOUtil.copyWithClose(in, sink); }
/** * Performs copy operation between currentNodeStream and outputStream using buffer length. * * @throws IOException */ private void doCopy() throws IOException { int copied = IOUtil.copy(currentNodeStream, outputStream, BUFFER_LENGTH); if (copied == -1) { currentNodeStream.close(); currentNodeStream = null; endAsset(); } }
/** * {@inheritDoc} */ @Override public void writeTo(final OutputStream outputStream, final Formatter formatter) throws IllegalArgumentException { try { IOUtil.bufferedWriteWithFlush(outputStream, toString(formatter).getBytes()); } catch (IOException ioe) { throw new IllegalArgumentException("Could not write Archive contents to specified OutputStream", ioe); } }
/** * Assert an asset is actually in the Zip file * * @throws IOException * @throws IllegalArgumentException */ private void assertAssetInZip(ZipFile expectedZip, ArchivePath path, Asset asset) throws IllegalArgumentException, IOException { final ZipEntry entry = this.getEntryFromZip(expectedZip, path); final byte[] expectedContents = IOUtil.asByteArray(asset.openStream()); final byte[] actualContents = IOUtil.asByteArray(expectedZip.getInputStream(entry)); Assert.assertArrayEquals(expectedContents, actualContents); }
/** * Helper method to run a specified task and automatically handle the closing of the stream. * * @param <S> * @param task * @param errorHandler */ public static <S extends Closeable> void closeOnComplete(S stream, StreamTask<S> task, StreamErrorHandler errorHandler) { try { task.execute(stream); } catch (Throwable t) { errorHandler.handle(t); } finally { if (stream != null) { try { stream.close(); } catch (final IOException ignore) { if (log.isLoggable(Level.FINER)) { log.finer("Could not close stream due to: " + ignore.getMessage() + "; ignoring"); } } } } } }
/** * Helper method to run a specified task and automatically handle the closing of the stream. * * @param stream * @param task */ public static <S extends Closeable> void closeOnComplete(S stream, StreamTask<S> task) { closeOnComplete(stream, task, DEFAULT_ERROR_HANDLER); }
@Test @InSequence(1) public void shouldBeAbleToInjectURL(@ArquillianResource URL base) throws Exception { System.out.println(base); Assert.assertNotNull(base); IOUtil.copy(base.openStream(), System.out); }
@Test public void shouldBufferWritesCorrectly() throws IOException { MockOutputStream outputStream = new MockOutputStream(); byte[] content = new byte[9202]; for (int i = 0; i < content.length; i++) { content[i] = (i + "").getBytes()[0]; } IOUtil.bufferedWriteWithFlush(outputStream, content); Assert.assertArrayEquals("Inconsistent writes?", content, outputStream.getContents()); }
/** * Assert an asset is actually in the Zip file * * @throws IOException * @throws IllegalArgumentException */ private void assertAssetInZip(ZipFile expectedZip, ArchivePath path, Asset asset) throws IllegalArgumentException, IOException { final ZipEntry entry = this.getEntryFromZip(expectedZip, path); final byte[] expectedContents = IOUtil.asByteArray(asset.openStream()); final byte[] actualContents = IOUtil.asByteArray(expectedZip.getInputStream(entry)); Assert.assertArrayEquals(expectedContents, actualContents); }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#ensureAssetInExportedFile(java.io.File, * org.jboss.shrinkwrap.api.ArchivePath, org.jboss.shrinkwrap.api.asset.Asset) */ protected final void ensureAssetInExportedFile(final File file, final ArchivePath path, final Asset asset) throws IOException { // Precondition checks assert file != null : "file must be specified"; assert path != null : "path must be specified"; assert asset != null : "asset must be specified"; // Get as Exported File final InputStream actualStream = this.getContentsFromExportedFile(file, path); assert actualStream != null : "No contents found at path " + path + " in " + file.getAbsolutePath(); byte[] actualContents = IOUtil.asByteArray(actualStream); byte[] expectedContents = IOUtil.asByteArray(asset.openStream()); Assert.assertArrayEquals(expectedContents, actualContents); } }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#getContentsFromExportedFile(java.io.File, * org.jboss.shrinkwrap.api.ArchivePath) */ @Override protected InputStream getContentsFromExportedFile(final File file, final ArchivePath path) throws IOException { // Precondition checks assert file != null : "file must be specified"; assert path != null : "path must be specified"; // Get as Zip File final ZipFile zipFile = new ZipFile(file); final ZipEntry entry = zipFile.getEntry(PathUtil.optionallyRemovePrecedingSlash(path.get())); if (entry == null) { return null; } final byte[] actualContents = IOUtil.asByteArray(zipFile.getInputStream(entry)); return new ByteArrayInputStream(actualContents); }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#getContentsFromExportedFile(java.io.File, * org.jboss.shrinkwrap.api.ArchivePath) */ @Override protected InputStream getContentsFromExportedFile(final File file, final ArchivePath path) throws IOException { // Precondition checks assert file != null : "file must be specified"; assert path != null : "path must be specified"; // Get as Zip File final ZipFile zipFile = new ZipFile(file); final ZipEntry entry = zipFile.getEntry(PathUtil.optionallyRemovePrecedingSlash(path.get())); if (entry == null) { return null; } final byte[] actualContents = IOUtil.asByteArray(zipFile.getInputStream(entry)); return new ByteArrayInputStream(actualContents); }
/** * Compare two Asset with each other. <br/> * Does not check instances but content. * * @param one * Asset to compare * @param two * Asset to compare * @return true if they are equal * @throws IllegalArgumentException * If either asset is not specified */ private boolean compareAssets(final Asset one, final Asset two) throws IllegalArgumentException { // Precondition check Validate.notNull(one, "Asset one must be specified"); Validate.notNull(two, "Asset two must be specified"); byte[] oneData = IOUtil.asByteArray(one.openStream()); byte[] twoData = IOUtil.asByteArray(two.openStream()); return Arrays.equals(oneData, twoData); }
/** * Assert an asset is actually in the exploded directory * * @throws FileNotFoundException * @throws IOException * @throws IllegalArgumentException */ private void assertAssetInExploded(File explodedDirectory, ArchivePath path, Asset asset) throws FileNotFoundException { File assetFile = new File(explodedDirectory, path.get()); Assert.assertNotNull(assetFile); Assert.assertTrue(assetFile.exists()); byte[] expectedContents = IOUtil.asByteArray(asset.openStream()); InputStream inputStream = new FileInputStream(assetFile); byte[] actualContents = IOUtil.asByteArray(inputStream); Assert.assertArrayEquals(expectedContents, actualContents); } }
@Test public void shouldCreateServiceProviderFile() throws Exception { Asset asset = new ServiceProviderAsset(TestImpl1.class, TestImpl2.class); byte[] expectedContent = (TestImpl1.class.getName() + "\n" + TestImpl2.class.getName() + "\n").getBytes(); byte[] content = IOUtil.asByteArray(asset.openStream()); Assert.assertArrayEquals(expectedContent, content); }