/** * SHRINKWRAP-237: Reading the same resource multiple times cause IOException */ @Test public void shouldBeAbleToLoadAResourceFromArchiveMultipleTimes() throws Exception { String resourceName = getResourceNameOfClass(applicationClassLoaderClass); // Load the class as a resource URL resource = shrinkWrapClassLoader.getResource(resourceName); // Assertions Assert.assertNotNull(resource); // Read the stream until EOF IOUtil.copyWithClose(resource.openStream(), new ByteArrayOutputStream()); // Load the class as a resource for the second time resource = shrinkWrapClassLoader.getResource(resourceName); // Assertions Assert.assertNotNull(resource); // SHRINKWRAP-237: This throws IOException: Stream closed IOUtil.copyWithClose(resource.openStream(), new ByteArrayOutputStream()); }
IOUtil.copyWithClose(assetInputStream, assetBufferedOutputStream); } catch (final Exception e) {
IOUtil.copyWithClose(assetInputStream, assetBufferedOutputStream); } catch (final Exception e) {
/** * https://jira.jboss.org/jira/browse/SHRINKWRAP-86 Ensure an IllegalArgumentException is thrown when output * directory is a file */ @Test(expected = IllegalArgumentException.class) public void testExportExplodedOutpuDirIsAFile() throws Exception { log.info("testExportExplodedOutpuDirIsAFile"); final File directory = createTempDirectory("testExportExplodedOutpuDirIsAFile"); // Will cause the creation of Archive directory to fail final File existingFile = new File(directory, NAME_ARCHIVE + this.getArchiveExtension()); final boolean created = existingFile.createNewFile(); IOUtil.copyWithClose(new ByteArrayInputStream("test-test".getBytes()), new FileOutputStream(existingFile)); Assert.assertEquals("Could not create test file", true, created); createArchiveWithAssets().as(ExplodedExporter.class).exportExploded(directory); }
/** * Test to make sue an archive can be exported and all contents are correctly located. * * @throws Exception */ @Test public void testExport() throws Exception { log.info("testExport"); // Get an archive instance Archive<?> archive = createArchiveWithAssets(); // Export as InputStream final InputStream exportStream = this.exportAsInputStream(archive); // Validate final File tempDirectory = createTempDirectory("testExport"); final File serialized = new File(tempDirectory, archive.getName()); final FileOutputStream out = new FileOutputStream(serialized); IOUtil.copyWithClose(exportStream, out); ensureInExpectedForm(serialized); }
/** * Test to ensure that the export process accepts an archive with only directories, no assets. * * @throws Exception */ @Test public void testExportArchiveWithOnlyDirectories() throws IOException { // Create an archive with directories final ArchivePath path = ArchivePaths.create("/test/game"); final Archive<?> archive = ShrinkWrap.create(JavaArchive.class, NAME_ARCHIVE).addAsDirectories(path); // Fully export by reading all content (export is on-demand) final InputStream content = this.exportAsInputStream(archive); final ByteArrayOutputStream exportedContents = new ByteArrayOutputStream(); IOUtil.copyWithClose(content, exportedContents); final GenericArchive roundtrip = ShrinkWrap.create(this.getImporterClass(), "roundtrip.zip") .importFrom(new ByteArrayInputStream(exportedContents.toByteArray())).as(GenericArchive.class); log.info(roundtrip.toString(true)); Assert.assertTrue(roundtrip.contains(path)); }
IOUtil.copyWithClose(in, out);
IOUtil.copyWithClose(exportStream, exportedOut); IOUtil.copyWithClose(nestedArchiveStream, nestedOut); IOUtil.copyWithClose(nestedArchiveTwoStream, nestedTwoOut);
/** * Ensures that we may import an archive, add content to it, export, and that the added content has been reflected * to the exported view. * * @throws Exception */ @Test public void shouldBeAbleToImportAddAndExport() throws Exception { // Get the delegate final ContentAssertionDelegateBase delegate = this.getDelegate(); assert delegate != null : "Delegate must be specified by implementations"; final File testFile = delegate.getExistingResource(); // Import from file final Class<? extends StreamImporter<?>> importerClass = this.getImporterClass(); assert importerClass != null : "Importer class must be specified by implementations"; final Archive<?> archive = ShrinkWrap.create(importerClass, "test.jar").importFrom(testFile) .as(JavaArchive.class); Assert.assertNotNull("Should not return a null archive", archive); // Add a new resource archive.add(new ClassLoaderAsset(EXISTING_RESOURCE), ArchivePaths.create("test.properties")); // Export File tempFile = new File("target/testOutput"); tempFile.deleteOnExit(); final Class<? extends StreamExporter> exporterClass = this.getExporterClass(); Assert.assertNotNull("Exporter class must be specified by implementations", exporterClass); final InputStream stream = archive.as(exporterClass).exportAsInputStream(); IOUtil.copyWithClose(stream, new FileOutputStream(tempFile)); // Ensure the exported view matches that of the archive delegate.assertContent(archive, tempFile); }
@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); }