public static File toExploded(Archive archive, String ext) { File tmp = assertTmpFile(ext); if (tmp.delete()) { ExplodedExporter exporter = archive.as(ExplodedExporter.class); exporter.exportExploded(tmp.getParentFile(), tmp.getName()); tmp.deleteOnExit(); return tmp; } else { throw failure("Could not delete tmp file " + tmp.getAbsolutePath()); } } }
Map<ArchivePath, Node> archiveContents = archive.getContent(); for (Map.Entry<ArchivePath, Node> entry : archiveContents.entrySet()) { ArchivePath path = entry.getKey(); Node n = entry.getValue(); Path f = subpath(deployDir, path.get()); if (n.getAsset() == null) { Files.createDirectories(f); } else { Files.createDirectories(parent); Files.copy(n.getAsset().openStream(), f, StandardCopyOption.REPLACE_EXISTING); String p = n.getPath().get(); if (callback != null) { callback.accept(p);
@Test public void shouldGenerateDependencies() throws Exception { Archive<?> archive = new JUnitDeploymentAppender().createAuxiliaryArchive(); Assert.assertTrue( "Should have added Extension", archive.contains( ArchivePaths.create("/META-INF/services/org.jboss.arquillian.container.test.spi.TestRunner"))); Assert.assertTrue( "Should have added TestRunner Impl", archive.contains(ArchivePaths.create("/org/jboss/arquillian/junit/container/JUnitTestRunner.class"))); } }
@Test public void existsTrue() throws IOException { final Archive<?> archive = this.getArchive(); final String pathString = "file"; archive.add(EmptyAsset.INSTANCE, pathString); final Path path = fs.getPath(pathString); final boolean exists = Files.exists(path); Assert.assertTrue("Should report exists", exists); }
public void repackageWar(File file) throws IOException { this.log.info("Repackaging .war: " + file); Path backupPath = get(file); move(file, backupPath, this.log); Archive original = ShrinkWrap.create(JavaArchive.class); try (InputStream inputStream = Files.newInputStream(backupPath)) { original.as(ZipImporter.class).importFrom(inputStream); } WebInfLibFilteringArchive repackaged = new WebInfLibFilteringArchive(original, this.dependencyManager); repackaged.as(ZipExporter.class).exportTo(file, true); this.log.info("Repackaged .war: " + file); }
@Test public void copyFromInputStreamToPath() throws IOException { final String contents = "Hello, testing content writing!"; final byte[] bytes = contents.getBytes("UTF-8"); final InputStream in = new ByteArrayInputStream(bytes); final String pathName = "content"; final Path path = fs.getPath(pathName); final long bytesCopied = Files.copy(in, path); final String roundtrip = new BufferedReader(new InputStreamReader(this.getArchive().get(pathName).getAsset() .openStream())).readLine(); Assert.assertEquals("Contents after copy were not as expected", contents, roundtrip); Assert.assertEquals(bytes.length, bytesCopied); }
@Test public void readAllBytes() throws IOException { final String path = "path"; final String contents = "contents"; this.getArchive().add(new StringAsset(contents), path); final byte[] bytes = Files.readAllBytes(fs.getPath(path)); final String roundtrip = new String(bytes); Assert.assertEquals("Contents not read as expected from the readAllBytes", contents, roundtrip); }
@Test @ArchiveType(ManifestContainer.class) public void testAddManifestPackagePathTarget() throws Exception { ArchivePath targetPath = ArchivePaths.create("Test.txt"); getManifestContainer().addAsManifestResource(AssetUtil.class.getPackage(), "Test.properties", targetPath); ArchivePath testPath = new BasicPath(getManifestPath(), targetPath); Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath)); }
/** * Asserts that the archive recursively contains the specified file in the target starting from the base position. */ private void assertArchiveContainsFolderRecursively(File file, ArchivePath base, String target) throws Exception { ArchivePath testPath = new BasicPath(base, target); Assert.assertTrue("Archive should contain " + testPath, this.getArchive().contains(testPath)); if (file.isDirectory()) { for (File child : file.listFiles()) { assertArchiveContainsFolderRecursively(child, base, target + "/" + child.getName()); } int folderInArchiveSize = this.getArchive().get(testPath).getChildren().size(); Assert.assertEquals("Wrong number of files in the archive folder: " + testPath.get(), file.listFiles().length, folderInArchiveSize); } }
/** * SHRINKWRAP-275 */ @Test @ArchiveType(WebContainer.class) public void testAddWebStringTargetResourceFromJar() throws Exception { // Causing NPE getWebContainer().addAsWebResource("java/lang/String.class", "String.class"); ArchivePath testPath = new BasicPath(getWebPath(), "String.class"); Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath)); }
@Test public void newInputStream() throws IOException { final String path = "path"; final String contents = "contents"; this.getArchive().add(new StringAsset(contents), path); final InputStream in = Files.newInputStream(fs.getPath(path), StandardOpenOption.READ); final byte[] buffer = new byte[contents.length()]; in.read(buffer); in.close(); final String roundtrip = new String(buffer); Assert.assertEquals("Contents not read as expected from the instream", contents, roundtrip); }
@Test public void newOutputStream() throws IOException { final String path = "path"; final String contents = "contents"; final OutputStream outStream = Files.newOutputStream(fs.getPath(path), StandardOpenOption.WRITE); outStream.write(contents.getBytes()); outStream.close(); final String roundtrip = new BufferedReader(new InputStreamReader(this.getArchive().get(path).getAsset() .openStream())).readLine(); Assert.assertEquals("Contents not read as expected from the outstream", contents, roundtrip); }
@Test @ArchiveType(LibraryContainer.class) public void testAddLibrariesArchive() throws Exception { Archive<?> archive = createNewArchive(); Archive<?> archive2 = createNewArchive(); getLibraryContainer().addAsLibraries(archive, archive2); ArchivePath testPath = new BasicPath(getLibraryPath(), archive.getName()); ArchivePath testPath2 = new BasicPath(getLibraryPath(), archive.getName()); Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath)); Assert.assertTrue("Archive should contain " + testPath2, getArchive().contains(testPath2)); }
@Test public void newBufferedWriter() throws IOException { final String path = "path"; final String contents = "contents"; final BufferedWriter writer = Files.newBufferedWriter(fs.getPath(path), Charset.defaultCharset(), (OpenOption) null); writer.write(contents); writer.close(); final String roundtrip = new BufferedReader(new InputStreamReader(this.getArchive().get(path).getAsset() .openStream())).readLine(); Assert.assertEquals("Contents not written as expected from the buffered writer", contents, roundtrip); }
@Test public void moveDirectory() throws IOException { final String source = "dir"; this.getArchive().addAsDirectory(source); final String dest = "newPath"; final Path src = fs.getPath(source); final Path dst = fs.getPath(dest); final Path moved = Files.move(src, dst); Assert.assertEquals(dest, moved.toString()); Assert.assertNull("Directory expected after move", this.getArchive().get(dest).getAsset()); }
@Test public void isRegularFile() throws IOException { final String path = "path"; this.getArchive().add(EmptyAsset.INSTANCE, path); Assert.assertTrue(Files.isRegularFile(fs.getPath(path))); }
@Test public void testImportArchiveAsTypeFromFilterUsingDefaultFormat() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); Collection<JavaArchive> jars = archive.getAsType(JavaArchive.class, Filters.include(".*jar")); Assert.assertEquals("Unexpected result found", 1, jars.size()); JavaArchive jar = jars.iterator().next().add(new StringAsset("test file content"), "test.txt"); Assert.assertEquals("JAR imported with wrong name", resourcePath, jar.getName()); Assert.assertNotNull("Class in JAR not imported", jar.get("test/classloader/DummyClass.class")); Assert.assertNotNull("Inner Class in JAR not imported", jar.get("test/classloader/DummyClass$DummyInnerClass.class")); Assert.assertNotNull("Should contain a new asset", ((ArchiveAsset) archive.get(resourcePath).getAsset()) .getArchive().get("test.txt")); }
@Test(expected = UnsupportedOperationException.class) public void getLastModifiedTime() throws IOException { this.getArchive().add(EmptyAsset.INSTANCE, "path"); Files.getLastModifiedTime(fs.getPath("path"), (LinkOption) null); }
protected static Stream<Archive> archives(Collection<Path> paths) { return paths.stream() .map(path -> { String simpleName = path.getFileName().toString(); Archive archive = ShrinkWrap.create(JavaArchive.class, simpleName); archive.as(ZipImporter.class).importFrom(path.toFile()); return archive; }); }