Search in sources :

Example 1 with BasicPath

use of org.jboss.shrinkwrap.impl.base.path.BasicPath in project camel by apache.

the class ArquillianPackager method addSpringbootPackage.

private static JavaArchive addSpringbootPackage(JavaArchive ark, String... packageNames) throws Exception {
    Iterable<ClassLoader> classLoaders = Collections.singleton(Thread.currentThread().getContextClassLoader());
    for (String packageName : packageNames) {
        for (final ClassLoader classLoader : classLoaders) {
            final URLPackageScanner.Callback callback = new URLPackageScanner.Callback() {

                @Override
                public void classFound(String className) {
                    ArchivePath classNamePath = AssetUtil.getFullPathForClassResource(className);
                    Asset asset = new ClassLoaderAsset(classNamePath.get().substring(1), classLoader);
                    ArchivePath location = new BasicPath(CLASSES_FOLDER + "/", classNamePath);
                    ark.add(asset, location);
                }
            };
            final URLPackageScanner scanner = URLPackageScanner.newInstance(true, classLoader, callback, packageName);
            scanner.scanPackage();
        }
    }
    return ark;
}
Also used : ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) URLPackageScanner(org.jboss.shrinkwrap.impl.base.URLPackageScanner) Asset(org.jboss.shrinkwrap.api.asset.Asset) FileAsset(org.jboss.shrinkwrap.api.asset.FileAsset) ClassLoaderAsset(org.jboss.shrinkwrap.api.asset.ClassLoaderAsset) BasicPath(org.jboss.shrinkwrap.impl.base.path.BasicPath) ClassLoaderAsset(org.jboss.shrinkwrap.api.asset.ClassLoaderAsset)

Example 2 with BasicPath

use of org.jboss.shrinkwrap.impl.base.path.BasicPath in project wildfly-swarm by wildfly-swarm.

the class DefaultDeploymentScenarioGenerator method generate.

@Override
public List<DeploymentDescription> generate(TestClass testClass) {
    DefaultDeployment anno = testClass.getAnnotation(DefaultDeployment.class);
    if (anno == null) {
        return super.generate(testClass);
    }
    String classPrefix = (anno.type() == DefaultDeployment.Type.JAR ? "" : "WEB-INF/classes");
    Archive<?> archive;
    if (DefaultDeployment.Type.WAR.equals(anno.type())) {
        WebArchive webArchive = ShrinkWrap.create(WebArchive.class, testClass.getJavaClass().getSimpleName() + ".war");
        // Add the marker to also include the project dependencies
        MarkerContainer.addMarker(webArchive, DependenciesContainer.ALL_DEPENDENCIES_MARKER);
        archive = webArchive;
    } else {
        archive = ShrinkWrap.create(JavaArchive.class, testClass.getJavaClass().getSimpleName() + ".jar");
    }
    ClassLoader cl = testClass.getJavaClass().getClassLoader();
    Set<CodeSource> codeSources = new HashSet<>();
    URLPackageScanner.Callback callback = (className, asset) -> {
        ArchivePath classNamePath = AssetUtil.getFullPathForClassResource(className);
        ArchivePath location = new BasicPath(classPrefix, classNamePath);
        archive.add(asset, location);
        try {
            Class<?> cls = cl.loadClass(className);
            codeSources.add(cls.getProtectionDomain().getCodeSource());
        } catch (ClassNotFoundException | NoClassDefFoundError e) {
        // e.printStackTrace();
        }
    };
    URLPackageScanner scanner = URLPackageScanner.newInstance(true, cl, callback, testClass.getJavaClass().getPackage().getName());
    scanner.scanPackage();
    Set<String> prefixes = codeSources.stream().map(e -> e.getLocation().toExternalForm()).collect(Collectors.toSet());
    try {
        List<URL> resources = Collections.list(cl.getResources(""));
        resources.stream().filter(e -> {
            for (String prefix : prefixes) {
                if (e.toExternalForm().startsWith(prefix)) {
                    return true;
                }
            }
            return false;
        }).filter(e -> e.getProtocol().equals("file")).map(e -> getPlatformPath(e.getPath())).map(e -> Paths.get(e)).filter(e -> Files.isDirectory(e)).forEach(e -> {
            try {
                Files.walkFileTree(e, new SimpleFileVisitor<Path>() {

                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        if (!file.toString().endsWith(".class")) {
                            String location = javaSlashize(handleExceptionalCases(archive, e.relativize(file)));
                            archive.add(new FileAsset(file.toFile()), location);
                        }
                        return super.visitFile(file, attrs);
                    }
                });
            } catch (IOException e1) {
            }
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    /*
        Map<ArchivePath, Node> content = archive.getContent();
        for (ArchivePath each : content.keySet()) {
            System.err.println(" --> " + each);
        }
        */
    DeploymentModules deploymentModules = testClass.getAnnotation(DeploymentModules.class);
    if (deploymentModules != null) {
        for (DeploymentModule each : deploymentModules.value()) {
            archive.as(JARArchive.class).addModule(each.name(), each.slot());
        }
    }
    DeploymentModule deploymentModule = testClass.getAnnotation(DeploymentModule.class);
    if (deploymentModule != null) {
        archive.as(JARArchive.class).addModule(deploymentModule.name(), deploymentModule.slot());
    }
    DeploymentDescription description = new DeploymentDescription(testClass.getName(), archive);
    Class<?> mainClass = anno.main();
    if (mainClass != Void.class) {
        archive.add(new StringAsset(mainClass.getName()), "META-INF/arquillian-main-class");
    }
    description.shouldBeTestable(anno.testable());
    return Collections.singletonList(description);
}
Also used : URL(java.net.URL) AnnotationDeploymentScenarioGenerator(org.jboss.arquillian.container.test.impl.client.deployment.AnnotationDeploymentScenarioGenerator) DependenciesContainer(org.wildfly.swarm.spi.api.DependenciesContainer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) AssetUtil(org.jboss.shrinkwrap.impl.base.asset.AssetUtil) URI(java.net.URI) DeploymentModule(org.wildfly.swarm.spi.api.annotations.DeploymentModule) Path(java.nio.file.Path) TestClass(org.jboss.arquillian.test.spi.TestClass) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) MarkerContainer(org.wildfly.swarm.spi.api.MarkerContainer) DefaultDeployment(org.wildfly.swarm.arquillian.DefaultDeployment) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) Files(java.nio.file.Files) DeploymentModules(org.wildfly.swarm.spi.api.annotations.DeploymentModules) Set(java.util.Set) JARArchive(org.wildfly.swarm.spi.api.JARArchive) IOException(java.io.IOException) Archive(org.jboss.shrinkwrap.api.Archive) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) DeploymentDescription(org.jboss.arquillian.container.spi.client.deployment.DeploymentDescription) Collectors(java.util.stream.Collectors) StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) URLPackageScanner(org.jboss.shrinkwrap.impl.base.URLPackageScanner) Paths(java.nio.file.Paths) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) FileAsset(org.jboss.shrinkwrap.api.asset.FileAsset) CodeSource(java.security.CodeSource) Collections(java.util.Collections) BasicPath(org.jboss.shrinkwrap.impl.base.path.BasicPath) StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) DefaultDeployment(org.wildfly.swarm.arquillian.DefaultDeployment) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) URL(java.net.URL) DeploymentModules(org.wildfly.swarm.spi.api.annotations.DeploymentModules) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) URLPackageScanner(org.jboss.shrinkwrap.impl.base.URLPackageScanner) DeploymentDescription(org.jboss.arquillian.container.spi.client.deployment.DeploymentDescription) JARArchive(org.wildfly.swarm.spi.api.JARArchive) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) Path(java.nio.file.Path) BasicPath(org.jboss.shrinkwrap.impl.base.path.BasicPath) FileAsset(org.jboss.shrinkwrap.api.asset.FileAsset) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) CodeSource(java.security.CodeSource) DeploymentModule(org.wildfly.swarm.spi.api.annotations.DeploymentModule) BasicPath(org.jboss.shrinkwrap.impl.base.path.BasicPath) TestClass(org.jboss.arquillian.test.spi.TestClass)

Example 3 with BasicPath

use of org.jboss.shrinkwrap.impl.base.path.BasicPath in project wildfly by wildfly.

the class ServletResourceOverlaysTestCase method single.

@Deployment
public static WebArchive single() {
    WebArchive war = ShrinkWrap.create(WebArchive.class, "single.war");
    war.addAsWebResource(new StringAsset("a"), "a.txt");
    war.addAsWebResource(new StringAsset("b"), "b.txt");
    war.addClass(PathAccessCheckServlet.class);
    war.addAsManifestResource(createPermissionsXmlAsset(new FilePermission("/-", "read"), new PropertyPermission("java.io.tmpdir", "read"), new VirtualFilePermission(Paths.get(System.getProperty("java.io.tmpdir"), "noaccess.txt").toFile().getAbsolutePath(), "read")), "permissions.xml");
    JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "test.jar");
    jar.addAsManifestResource(new StringAsset("b - overlay"), new BasicPath("resources", "b.txt"));
    jar.addAsManifestResource(new StringAsset("c - overlay"), new BasicPath("resources", "c.txt"));
    war.addAsLibrary(jar);
    return war;
}
Also used : StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) PropertyPermission(java.util.PropertyPermission) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) VirtualFilePermission(org.jboss.vfs.VirtualFilePermission) BasicPath(org.jboss.shrinkwrap.impl.base.path.BasicPath) FilePermission(java.io.FilePermission) VirtualFilePermission(org.jboss.vfs.VirtualFilePermission) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) Deployment(org.jboss.arquillian.container.test.api.Deployment)

Example 4 with BasicPath

use of org.jboss.shrinkwrap.impl.base.path.BasicPath in project tomee by apache.

the class MicroProfileRestClientTCKArchiveProcessor method process.

@Override
public void process(final Archive<?> archive, final TestClass testClass) {
    if (archive instanceof WebArchive) {
        WebArchive webArchive = (WebArchive) archive;
        final Map<ArchivePath, Node> content = webArchive.getContent();
        final Node node = content.get(new BasicPath("META-INF/certificates-dir.txt"));
        if (node != null) {
            webArchive.addAsResource(node.getAsset(), "META-INF/certificates-dir.txt");
        }
    }
}
Also used : ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) Node(org.jboss.shrinkwrap.api.Node) BasicPath(org.jboss.shrinkwrap.impl.base.path.BasicPath)

Example 5 with BasicPath

use of org.jboss.shrinkwrap.impl.base.path.BasicPath in project eap-additional-testsuite by jboss-set.

the class ArchiveTestCase method before.

@BeforeClass
public static void before() throws Exception {
    String tempDir = TestSuiteEnvironment.getTmpDir();
    WebArchive[] wars = new WebArchive[3];
    // deployment1
    wars[0] = ShrinkWrap.create(WebArchive.class, "deployment0.war");
    wars[0].addClass(SimpleServlet.class);
    wars[0].addAsWebResource(new StringAsset("Version0"), "page.html");
    // deployment2
    wars[1] = ShrinkWrap.create(WebArchive.class, "deployment1.war");
    wars[1].addClass(SimpleServlet.class);
    wars[1].addAsWebResource(new StringAsset("Version1"), "page.html");
    // deployment3 is included but not deployed
    wars[2] = ShrinkWrap.create(WebArchive.class, "deployment2.war");
    wars[2].addClass(SimpleServlet.class);
    wars[2].addAsWebResource(new StringAsset("Version2"), "page.html");
    // build cli archive
    EnterpriseArchive cliArchive = ShrinkWrap.create(EnterpriseArchive.class, "archive.cli");
    String deploy = "deploy deployment0.war\ndeploy deployment1.war";
    String undeploy = "undeploy deployment0.war\nundeploy deployment1.war";
    cliArchive.add(new StringAsset(deploy), new BasicPath("/", "install.scr"));
    // add the default script which shouldn't be picked up
    cliArchive.add(new StringAsset("deploy deployment0.war\ndeploy deployment1.war\ndeploy deployment2.war"), new BasicPath("/", "deploy.scr"));
    cliArchive.add(new StringAsset(undeploy), new BasicPath("/", "uninstall.scr"));
    cliArchive.add(new StringAsset("undeploy deployment0.war\nundeploy deployment1.war\nundeploy deployment2.war"), new BasicPath("/", "undeploy.scr"));
    for (WebArchive war : wars) {
        cliArchive.add(war, new BasicPath("/"), ZipExporter.class);
    }
    cliArchiveFile = new File(tempDir + File.separator + "archive.cli");
    new ZipExporterImpl(cliArchive).exportTo(cliArchiveFile, true);
}
Also used : EnterpriseArchive(org.jboss.shrinkwrap.api.spec.EnterpriseArchive) StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) BasicPath(org.jboss.shrinkwrap.impl.base.path.BasicPath) File(java.io.File) ZipExporterImpl(org.jboss.shrinkwrap.impl.base.exporter.zip.ZipExporterImpl) BeforeClass(org.junit.BeforeClass)

Aggregations

BasicPath (org.jboss.shrinkwrap.impl.base.path.BasicPath)13 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)11 StringAsset (org.jboss.shrinkwrap.api.asset.StringAsset)10 File (java.io.File)8 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)8 ZipExporterImpl (org.jboss.shrinkwrap.impl.base.exporter.zip.ZipExporterImpl)8 BeforeClass (org.junit.BeforeClass)8 ArchivePath (org.jboss.shrinkwrap.api.ArchivePath)4 Node (org.jboss.shrinkwrap.api.Node)2 FileAsset (org.jboss.shrinkwrap.api.asset.FileAsset)2 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)2 URLPackageScanner (org.jboss.shrinkwrap.impl.base.URLPackageScanner)2 FilePermission (java.io.FilePermission)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URL (java.net.URL)1 FileVisitResult (java.nio.file.FileVisitResult)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1