assertTrue(tmp.delete()); war.as(ZipExporter.class).exportTo(tmp); final byte[] bytes = Utils.readAsBytes(jar.get("foo/A.class").getAsset().openStream()); return new ClassLoader(Thread.currentThread().getContextClassLoader()) { Class<?> aClass = null;
javaArchive.get(MANIFEST_PATH).getAsset().openStream());
@Test public void testImportArchiveAsTypeFromFilter() 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"), ArchiveFormat.ZIP); 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")); }
@Override public Node get(ArchivePath path) throws IllegalArgumentException { return delegate.get(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")); }
@Override public Node get(String path) throws IllegalArgumentException { return delegate.get(path); }
@Test public void testImportArchiveAsTypeFromStringUsingDefaultFormat() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); JavaArchive jar = archive.getAsType(JavaArchive.class, resourcePath).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")); }
@Override public Manifest getManifest(JavaArchive javaArchive) throws IOException { Node manifestNode = javaArchive.get(JarFile.MANIFEST_NAME); Asset manifestAsset = manifestNode.getAsset(); return new Manifest(manifestAsset.openStream()); }
@Test public void testImportArchiveAsTypeFromString() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); JavaArchive jar = archive.getAsType(JavaArchive.class, resourcePath, ArchiveFormat.ZIP).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")); }
private void unpack(File destination, JavaArchive file) throws IOException { final Node rootDir = file.get("/"); final Set<Node> contractFiles = rootDir.getChildren(); for (Node contractFile : contractFiles) { final String filename = contractFile.getPath().get().substring(1); final Asset asset = contractFile.getAsset(); try (final InputStream in = asset.openStream()) { Files.copy(in, new File(destination, filename).toPath()); } } }
@Test public void testImportArchiveAsTypeFromArchivePath() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); JavaArchive jar = archive.getAsType(JavaArchive.class, ArchivePaths.create(resourcePath), ArchiveFormat.ZIP) .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 an archive asset", ((ArchiveAsset) archive.get(resourcePath).getAsset()) .getArchive().get("test.txt")); }
private void assertJar(URL url) throws IOException { URLConnection conn = url.openConnection(); conn.connect(); InputStream in = conn.getInputStream(); JavaArchive archive = ShrinkWrap.create(ZipImporter.class).importFrom(in).as(JavaArchive.class); Node node = archive.get("/" + ReadFileSystemTestCase.class.getName().replace('.', '/') + ".class"); assertNotNull(node); }
@Test public void testImportArchiveAsTypeFromArchivePathUsingDefaultFormat() throws Exception { String resourcePath = "/test/cl-test.jar"; GenericArchive archive = ShrinkWrap.create(GenericArchive.class).add( new FileAsset(TestIOUtil.createFileFromResourceName("cl-test.jar")), resourcePath); JavaArchive jar = archive.getAsType(JavaArchive.class, ArchivePaths.create(resourcePath)).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 an archive asset", ((ArchiveAsset) archive.get(resourcePath).getAsset()) .getArchive().get("test.txt")); }
private void assertJar(URL url) throws IOException { URLConnection conn = url.openConnection(); conn.connect(); InputStream in = conn.getInputStream(); JavaArchive archive = ShrinkWrap.create(ZipImporter.class).importFrom(in).as(JavaArchive.class); Node node = archive.get("/" + ReadFileSystemTestCase.class.getName().replace('.', '/') + ".class"); assertNotNull(node); }
@Test public void testAddPackage() { // This archive's classloaders should be the app classloader (without `donotchange`) // and an .war-based classloader including the `donotchange` within WEB-INF/classes/... JavaArchive jar = this.domain.getArchiveFactory().create(JavaArchive.class); jar.addPackage("org.jboss.shrinkwrap.impl.base.spec.donotchange" ); Assert.assertNotNull( jar.get( "/" + DummyClassA.class.getName().replace( '.', '/' ) + ".class" ) ); }
@Test public void testAddPackage() { // This archive's classloaders should be the app classloader (without `donotchange`) // and an .war-based classloader including the `donotchange` within WEB-INF/classes/... JavaArchive jar = this.domain.getArchiveFactory().create(JavaArchive.class); jar.addPackage("org.jboss.shrinkwrap.impl.base.spec.donotchange" ); Assert.assertNotNull( jar.get( "/" + DummyClassA.class.getName().replace( '.', '/' ) + ".class" ) ); } }
/** * Explodes kumuluzee-loader library to root of the archive. * * @param javaArchive Archive with all jars in /WEB-INF/lib (war) */ private static void explodeLoaderArchiveToRoot(JavaArchive javaArchive) { for (Node n : javaArchive.get("/WEB-INF/lib").getChildren()) { if (n.getAsset() instanceof ArchiveAsset && ((ArchiveAsset) n.getAsset()).getArchive().getName().startsWith("kumuluzee-loader-")) { Archive<?> dependencyJar = ((ArchiveAsset) n.getAsset()).getArchive(); LOG.fine("Found kumuluzee-loader archive: " + dependencyJar.getName()); javaArchive.merge(dependencyJar); javaArchive.delete(n.getPath()); break; } } }
Node node = jar.get(item.getSource()); if (node == null) { throw new PackagingException("Unable to handle the file item " + item.getSource() + ", " +
/** * Explodes archive marked with {@link ApplicationArchiveMarker} to root of the archive. * * @param javaArchive Archive with all jars in /WEB-INF/lib (war) */ private static void explodeAppArchiveToRoot(JavaArchive javaArchive) { for (Node n : javaArchive.get("/WEB-INF/lib").getChildren()) { if (n.getAsset() instanceof ArchiveAsset) { Archive<?> dependencyJar = ((ArchiveAsset) n.getAsset()).getArchive(); if (dependencyJar.contains(ApplicationArchiveMarker.MARKER_FILENAME)) { LOG.fine("Found application archive: " + dependencyJar.getName()); dependencyJar.delete(ApplicationArchiveMarker.MARKER_FILENAME); javaArchive.merge(dependencyJar); javaArchive.delete(n.getPath()); break; } } } }
for (Node n : javaArchive.get("/").getChildren()) { if (n.getAsset() == null && !n.getPath().get().endsWith("WEB-INF")) { dirsToMove.add(n.getPath().get());