Java Code Examples for org.jboss.shrinkwrap.api.spec.JavaArchive#add()

The following examples show how to use org.jboss.shrinkwrap.api.spec.JavaArchive#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeploymentIndexTest.java    From thorntail with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndexAttached() throws IOException {
    WebArchive war = ShrinkWrap.create(WebArchive.class);
    war.addClass(Foo.class);
    war.add(createIndexAsset(Foo.class, Baz.class), DeploymentProducer.INDEX_LOCATION);
    JavaArchive lib1 = ShrinkWrap.create(JavaArchive.class).addClass(Bar.class);
    lib1.add(createIndexAsset(Delta.class), DeploymentProducer.INDEX_LOCATION);
    war.addAsLibraries(lib1);
    IndexView index = new DeploymentProducer().createDeploymentIndex(war);
    assertContains(index, Foo.class);
    // Baz should be found in the attached index
    assertContains(index, Baz.class);
    assertContains(index, Delta.class);
    // Bar is not in the attached index
    assertDoesNotContain(index, Bar.class);
}
 
Example 2
Source File: QuarkusUnitTest.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private void exportArchive(Path deploymentDir, Class<?> testClass) {
    try {
        JavaArchive archive = getArchiveProducerOrDefault();
        Class<?> c = testClass;
        while (c != Object.class) {
            archive.addClass(c);
            c = c.getSuperclass();
        }
        if (customApplicationProperties != null) {
            archive.add(new PropertiesAsset(customApplicationProperties), "application.properties");
        }
        archive.as(ExplodedExporter.class).exportExplodedInto(deploymentDir.toFile());

        //debugging code
        ExportUtil.exportToQuarkusDeploymentPath(archive);
    } catch (Exception e) {
        throw new RuntimeException("Unable to create the archive", e);
    }
}
 
Example 3
Source File: FractionUsageAnalyzerTest.java    From thorntail with Apache License 2.0 6 votes vote down vote up
@Test
public void testDetectEmptyWarAsUndertow() throws Exception {
    JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.war");
    archive.add(EmptyAsset.INSTANCE, "nothing");
    FractionUsageAnalyzer analyzer = new FractionUsageAnalyzer();

    final File out = Files.createTempFile(archive.getName(), ".war").toFile();
    archive.as(ZipExporter.class).exportTo(out, true);

    analyzer.source(out);
    assertThat(analyzer.detectNeededFractions()
                       .stream()
                       .filter(fd -> fd.getArtifactId().equals("undertow"))
                       .count())
            .isEqualTo(1);

    out.delete();
}
 
Example 4
Source File: SwaggerArchiveTest.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocateSwaggerConfInWar() throws Exception {
    JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "myapp.war");
    archive.add(basicSwaggerConf(), "WEB-INF/classes/META-INF/swarm.swagger.conf");

    String[] packages = archive.as(SwaggerArchive.class).getResourcePackages();
    assertThat(packages).contains("com.foo.mystuff");
}
 
Example 5
Source File: FullReplaceUndeployTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testWFCORE1577() throws Exception {
    final String name = "WFCORE-1577.jar";
    // Create a deployment archive
    final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, name);
    archive.add(EmptyAsset.INSTANCE, "META-INF/MANIFEST.MF");

    testDeployment(archive);
}
 
Example 6
Source File: MainIT.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
@Test
public void testRunMainWithError() throws Throwable {
    JavaArchive archive = createBootstrapArchive(MyMainThatThrows.class.getName(), "_bootstrap/myapp.jar");

    JavaArchive app = ShrinkWrap.create(JavaArchive.class);
    app.addClass(MyMainThatThrows.class);
    archive.add(new ArchiveAsset(app, ZipExporter.class), "_bootstrap/myapp.jar");

    ClassLoader cl = createClassLoader(archive);

    Class<?> mainClass = cl.loadClass(Main.class.getName());

    Constructor<?> ctor = mainClass.getConstructor(String[].class);

    Object main = ctor.newInstance((Object) new String[]{});

    Method run = mainClass.getMethod("run");


    boolean exceptionFound = false;
    try {
        run.invoke(main);
        fail("should have thrown");
    } catch (Throwable t) {
        while (t != null) {
            if (t.getMessage() != null && t.getMessage().equals("expected to throw")) {
                exceptionFound = true;
                break;
            }
            t = t.getCause();
        }
    }

    assertThat(exceptionFound).isTrue();
}
 
Example 7
Source File: ArquillianPackager.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
private static JavaArchive addDependencies(JavaArchive ark, Collection<File> deps) {
    Set<File> dependencySet = new HashSet<>(deps);
    for (File d : dependencySet) {
        debug("Adding spring-boot dependency: " + d.getName());
        ark = ark.add(new FileAsset(d), LIB_FOLDER + "/" + d.getName());
    }

    return ark;
}
 
Example 8
Source File: SwaggerArchiveTest.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocateSwaggerConfInJar() throws Exception {
    JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "myapp.war");
    archive.add(basicSwaggerConf(), "META-INF/swarm.swagger.conf");

    String[] packages = archive.as(SwaggerArchive.class).getResourcePackages();
    assertThat(packages).contains("com.foo.mystuff");
}
 
Example 9
Source File: QuarkusProdModeTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void exportArchive(Path deploymentDir, Class<?> testClass) {
    try {
        JavaArchive archive = getArchiveProducerOrDefault();
        if (customApplicationProperties != null) {
            archive.add(new PropertiesAsset(customApplicationProperties), "application.properties");
        }
        archive.as(ExplodedExporter.class).exportExplodedInto(deploymentDir.toFile());

        String exportPath = System.getProperty("quarkus.deploymentExportPath");
        if (exportPath != null) {
            File exportDir = new File(exportPath);
            if (exportDir.exists()) {
                if (!exportDir.isDirectory()) {
                    throw new IllegalStateException("Export path is not a directory: " + exportPath);
                }
                try (Stream<Path> stream = Files.walk(exportDir.toPath())) {
                    stream.sorted(Comparator.reverseOrder()).map(Path::toFile)
                            .forEach(File::delete);
                }
            } else if (!exportDir.mkdirs()) {
                throw new IllegalStateException("Export path could not be created: " + exportPath);
            }
            File exportFile = new File(exportDir, archive.getName());
            archive.as(ZipExporter.class).exportTo(exportFile);
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to create the archive", e);
    }
}
 
Example 10
Source File: DependencyManagerTest.java    From thorntail with Apache License 2.0 4 votes vote down vote up
private static ArtifactSpec fraction(String ga, Consumer<MockArtifactResolver.Entry> config) {
    String moduleName = ga.replace(':', '.');
    String gav = ga + ":" + System.getProperty("project.version");
    ArtifactSpec spec = ArtifactSpec.fromMscGav(gav);

    JavaArchive jar = ShrinkWrap.create( JavaArchive.class );

    StringBuilder yaml = new StringBuilder();

    yaml.append( "module: " + moduleName );

    jar.add(new StringAsset(yaml.toString()), FractionManifest.CLASSPATH_LOCATION );

    RESOLVER.add(spec, jar, config);

    return spec;
}
 
Example 11
Source File: PackagingIT.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testDuplicateManagement() throws VerificationException, IOException {
    File testDir = initProject(PACKAGING_DUPLICATE);
    assertThat(testDir).isDirectory();
    initVerifier(testDir);
    prepareProject(testDir, verifier);

    File A = new File("target/A-1.0.jar");
    File B = new File("target/B-1.0.jar");
    JavaArchive jarArchive1 = ShrinkWrap.create(JavaArchive.class);
    jarArchive1.add(new StringAsset("A1"), "/res/A");
    jarArchive1.add(new StringAsset("B1"), "/res/B");
    jarArchive1.as(ZipExporter.class).exportTo(A, true);

    JavaArchive jarArchive2 = ShrinkWrap.create(JavaArchive.class);
    jarArchive2.add(new StringAsset("A2"), "/res/A");
    jarArchive2.add(new StringAsset("B2"), "/res/B");
    jarArchive2.add(new StringAsset("C2"), "/res/C");
    jarArchive2.as(ZipExporter.class).exportTo(B, true);

    installJarToLocalRepository(verifier.getLocalRepository(), "A", A);
    installJarToLocalRepository(verifier.getLocalRepository(), "B", B);

    runPackage(verifier);

    File out = new File(testDir, "target/vertx-demo-start-0.0.1.BUILD-SNAPSHOT.jar");
    assertThat(out).isFile();
    JavaArchive archive = ShrinkWrap.createFromZipFile(JavaArchive.class, out);
    assertNotNull(archive);

    Asset a = archive.get( "/res/A").getAsset();
    Asset b = archive.get( "/res/B").getAsset();
    Asset c = archive.get( "/res/C").getAsset();

    String content_a = IOUtils.toString(a.openStream(), "UTF-8");
    String content_b = IOUtils.toString(b.openStream(), "UTF-8");
    String content_c = IOUtils.toString(c.openStream(), "UTF-8");

    assertThat(content_a).isEqualToIgnoringCase("A3\n");
    assertThat(content_b).isEqualToIgnoringCase("B1");
    assertThat(content_c).isEqualToIgnoringCase("C2");
}
 
Example 12
Source File: SPICombineTest.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testCombineWithSpringDescriptors() throws Exception {
    File jar1 = new File("target/testCombine1Spring.jar");
    File jar2 = new File("target/testCombine2Spring.jar");
    File jar3 = new File("target/testCombine3Spring.jar");

    JavaArchive jarArchive1 = ShrinkWrap.create(JavaArchive.class);
    jarArchive1.add(new StringAsset("com.test.demo.DemoSPI.impl.DemoSPIImpl"),
        "/META-INF/spring.foo");

    jarArchive1.as(ZipExporter.class).exportTo(jar1, true);


    JavaArchive jarArchive2 = ShrinkWrap.create(JavaArchive.class);
    jarArchive2.add(new StringAsset("com.test.demo.DemoSPI.impl.DemoSPIImpl2"),
        "/META-INF/spring.foo");
    jarArchive2.add(new StringAsset("com.test.demo.DemoSPI2.impl.DemoSPI2Impl2"),
        "/META-INF/spring.bar");
    jarArchive2.as(ZipExporter.class).exportTo(jar2, true);

    JavaArchive jarArchive3 = ShrinkWrap.create(JavaArchive.class);
    jarArchive3.addClass(SPICombineTest.class);
    jarArchive3.as(ZipExporter.class).exportTo(jar3, true);

    Set<Artifact> artifacts = new LinkedHashSet<>();
    Artifact a1 = new DefaultArtifact("org.acme", "a1", "1.0",
        "compile", "jar", "", null);
    a1.setFile(jar1);
    Artifact a2 = new DefaultArtifact("org.acme", "a2", "1.0",
        "compile", "jar", "", null);
    a2.setFile(jar2);
    Artifact a3 = new DefaultArtifact("org.acme", "a3", "1.0",
        "compile", "jar", "", null);
    a3.setFile(jar3);

    artifacts.add(a1);
    artifacts.add(a2);
    artifacts.add(a3);

    MavenProject project = new MavenProject();
    project.setVersion("1.0");
    project.setArtifactId("foo");

    AbstractVertxMojo mojo = new AbstractVertxMojo() {
        @Override
        public void execute() throws MojoExecutionException, MojoFailureException {

        }
    };

    mojo.setLog(new SystemStreamLog());
    Build build = new Build();
    build.setOutputDirectory("target/junk");
    project.setBuild(build);

    ServiceFileCombinationConfig config = new ServiceFileCombinationConfig()
        .setProject(project)
        .setArtifacts(artifacts)
        .setArchive(ServiceUtils.getDefaultFatJar())
        .setMojo(mojo);

    combiner.doCombine(config);

    File merged = new File("target/junk/META-INF/spring.foo");
    assertThat(merged).isFile();

    List<String> lines = FileUtils.readLines(merged, "UTF-8");
    assertThat(lines).containsExactly("com.test.demo.DemoSPI.impl.DemoSPIImpl",
        "com.test.demo.DemoSPI.impl.DemoSPIImpl2");
    Stream.of(jar1, jar2, jar3, new File("target/junk")).forEach(FileUtils::deleteQuietly);
}
 
Example 13
Source File: MainIT.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoUserMain() throws Throwable {
    JavaArchive archive = createBootstrapArchive();

    JavaArchive app = ShrinkWrap.create(JavaArchive.class);
    app.addClass(MyMain.class);
    archive.add(new ArchiveAsset(app, ZipExporter.class), "_bootstrap/myapp.jar");

    ClassLoader cl = createClassLoader(archive);

    Class<?> mainClass = cl.loadClass(Main.class.getName());

    Constructor<?> ctor = mainClass.getConstructor(String[].class);

    Object main = ctor.newInstance((Object) new String[]{});

    Method getMainClassName = mainClass.getMethod("getMainClassName");

    String mainClassName = (String) getMainClassName.invoke(main);

    assertThat(mainClassName).isEqualTo(Main.DEFAULT_MAIN_CLASS_NAME);
}
 
Example 14
Source File: MainIT.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithUserMain() throws Throwable {
    JavaArchive archive = createBootstrapArchive(MyMain.class.getName());

    JavaArchive app = ShrinkWrap.create(JavaArchive.class);
    app.addClass(MyMain.class);
    archive.add(new ArchiveAsset(app, ZipExporter.class), "_bootstrap/myapp.jar");

    ClassLoader cl = createClassLoader(archive);

    Class<?> mainClass = cl.loadClass(Main.class.getName());

    Constructor<?> ctor = mainClass.getConstructor(String[].class);

    Object main = ctor.newInstance((Object) new String[]{});

    Method getMainClassName = mainClass.getMethod("getMainClassName");

    String mainClassName = (String) getMainClassName.invoke(main);

    assertThat(mainClassName).isEqualTo(MyMain.class.getName());
}
 
Example 15
Source File: MainIT.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
@Test
public void testRunWithoutError() throws Throwable {
    JavaArchive archive = createBootstrapArchive(MyMain.class.getName(), "_bootstrap/myapp.jar");

    JavaArchive app = ShrinkWrap.create(JavaArchive.class);
    app.addClass(MyMain.class);
    archive.add(new ArchiveAsset(app, ZipExporter.class), "_bootstrap/myapp.jar");

    ClassLoader cl = createClassLoader(archive);

    Class<?> mainClass = cl.loadClass(Main.class.getName());

    Constructor<?> ctor = mainClass.getConstructor(String[].class);

    Object main = ctor.newInstance((Object) new String[]{});

    Method run = mainClass.getMethod("run");

    run.invoke(main);
}
 
Example 16
Source File: PackagingIT.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testDuplicateManagement() throws VerificationException, IOException {
    File testDir = initProject(PACKAGING_DUPLICATE);
    assertThat(testDir).isDirectory();
    initVerifier(testDir);
    prepareProject(testDir, verifier);

    File A = new File("target/A-1.0.jar");
    File B = new File("target/B-1.0.jar");
    JavaArchive jarArchive1 = ShrinkWrap.create(JavaArchive.class);
    jarArchive1.add(new StringAsset("A1"), "/res/A");
    jarArchive1.add(new StringAsset("B1"), "/res/B");
    jarArchive1.as(ZipExporter.class).exportTo(A, true);

    JavaArchive jarArchive2 = ShrinkWrap.create(JavaArchive.class);
    jarArchive2.add(new StringAsset("A2"), "/res/A");
    jarArchive2.add(new StringAsset("B2"), "/res/B");
    jarArchive2.add(new StringAsset("C2"), "/res/C");
    jarArchive2.as(ZipExporter.class).exportTo(B, true);

    installJarToLocalRepository(verifier.getLocalRepository(), "A", A);
    installJarToLocalRepository(verifier.getLocalRepository(), "B", B);

    runPackage(verifier);

    File out = new File(testDir, "target/vertx-demo-start-0.0.1.BUILD-SNAPSHOT.jar");
    assertThat(out).isFile();
    JavaArchive archive = ShrinkWrap.createFromZipFile(JavaArchive.class, out);
    assertNotNull(archive);

    Asset a = archive.get( "/res/A").getAsset();
    Asset b = archive.get( "/res/B").getAsset();
    Asset c = archive.get( "/res/C").getAsset();

    String content_a = IOUtils.toString(a.openStream(), "UTF-8");
    String content_b = IOUtils.toString(b.openStream(), "UTF-8");
    String content_c = IOUtils.toString(c.openStream(), "UTF-8");

    assertThat(content_a).isEqualToIgnoringCase("A3\n");
    assertThat(content_b).isEqualToIgnoringCase("B1");
    assertThat(content_c).isEqualToIgnoringCase("C2");
}
 
Example 17
Source File: DeploymentContentRemovalTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void createDeployment(final File file) throws IOException {
    final JavaArchive archive = ShrinkWrap.create(JavaArchive.class);
    archive.add(new StringAsset("ArchiveName: " + file.getName()), "META-INF/MANIFEST.MF"); // something unique for each deployment
    archive.as(ZipExporter.class).exportTo(file);
}
 
Example 18
Source File: AbstractDeploymentScannerBasedTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected Archive createDeploymentArchive(final String dependency) {
    final JavaArchive archive = ShrinkWrap.create(JavaArchive.class);
    final String dependencies = "Dependencies: " + dependency;
    archive.add(new StringAsset(dependencies), "META-INF/MANIFEST.MF");
    return archive;
}
 
Example 19
Source File: PatchingTestUtil.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
static void updateJar(final File source, final File target) throws IOException {
    final JavaArchive archive = ShrinkWrap.createFromZipFile(JavaArchive.class, source);
    archive.add(new StringAsset("test " + randomString()), "testFile");
    archive.as(ZipExporter.class).exportTo(target);
}
 
Example 20
Source File: SPICombineTest.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testCombineWithSpringDescriptors() throws Exception {
    File jar1 = new File("target/testCombine1Spring.jar");
    File jar2 = new File("target/testCombine2Spring.jar");
    File jar3 = new File("target/testCombine3Spring.jar");

    JavaArchive jarArchive1 = ShrinkWrap.create(JavaArchive.class);
    jarArchive1.add(new StringAsset("com.test.demo.DemoSPI.impl.DemoSPIImpl"),
        "/META-INF/spring.foo");

    jarArchive1.as(ZipExporter.class).exportTo(jar1, true);


    JavaArchive jarArchive2 = ShrinkWrap.create(JavaArchive.class);
    jarArchive2.add(new StringAsset("com.test.demo.DemoSPI.impl.DemoSPIImpl2"),
        "/META-INF/spring.foo");
    jarArchive2.add(new StringAsset("com.test.demo.DemoSPI2.impl.DemoSPI2Impl2"),
        "/META-INF/spring.bar");
    jarArchive2.as(ZipExporter.class).exportTo(jar2, true);

    JavaArchive jarArchive3 = ShrinkWrap.create(JavaArchive.class);
    jarArchive3.addClass(SPICombineTest.class);
    jarArchive3.as(ZipExporter.class).exportTo(jar3, true);

    Set<Artifact> artifacts = new LinkedHashSet<>();
    Artifact a1 = new DefaultArtifact("org.acme", "a1", "1.0",
        "compile", "jar", "", null);
    a1.setFile(jar1);
    Artifact a2 = new DefaultArtifact("org.acme", "a2", "1.0",
        "compile", "jar", "", null);
    a2.setFile(jar2);
    Artifact a3 = new DefaultArtifact("org.acme", "a3", "1.0",
        "compile", "jar", "", null);
    a3.setFile(jar3);

    artifacts.add(a1);
    artifacts.add(a2);
    artifacts.add(a3);

    MavenProject project = new MavenProject();
    project.setVersion("1.0");
    project.setArtifactId("foo");

    AbstractVertxMojo mojo = new AbstractVertxMojo() {
        @Override
        public void execute() throws MojoExecutionException, MojoFailureException {

        }
    };

    mojo.setLog(new SystemStreamLog());
    Build build = new Build();
    build.setOutputDirectory("target/junk");
    project.setBuild(build);

    ServiceFileCombinationConfig config = new ServiceFileCombinationConfig()
        .setProject(project)
        .setArtifacts(artifacts)
        .setArchive(ServiceUtils.getDefaultFatJar())
        .setMojo(mojo);

    combiner.doCombine(config);

    File merged = new File("target/junk/META-INF/spring.foo");
    assertThat(merged).isFile();

    List<String> lines = FileUtils.readLines(merged, "UTF-8");
    assertThat(lines).containsExactly("com.test.demo.DemoSPI.impl.DemoSPIImpl",
        "com.test.demo.DemoSPI.impl.DemoSPIImpl2");
    Stream.of(jar1, jar2, jar3, new File("target/junk")).forEach(FileUtils::deleteQuietly);
}