Exemple #1
0
  @Test
  public void testEar() {
    EnterpriseArchive archive =
        ShrinkWrap.create(MavenImporter.class, "test.ear")
            .loadEffectivePom("../ear-sample/pom.xml")
            .importBuildOutput()
            .as(EnterpriseArchive.class);

    Assert.assertNotNull("Archive is not null", archive);
    Assert.assertTrue("Archive contains test.xml", archive.contains("test.xml"));
    Assert.assertTrue(
        "Archive contains application.xml", archive.contains("META-INF/application.xml"));

    Assert.assertEquals("Archive contains one library", 1, getLibraries(archive).size());
  }
  private Archive<?> handleArchive(
      EnterpriseArchive applicationArchive,
      Collection<Archive<?>> auxiliaryArchives,
      JavaArchive protocol,
      Processor processor) {
    Map<ArchivePath, Node> applicationArchiveWars =
        applicationArchive.getContent(Filters.include(".*\\.war"));
    if (applicationArchiveWars.size() == 1) {
      ArchivePath warPath = applicationArchiveWars.keySet().iterator().next();
      try {
        handleArchive(
            applicationArchive.getAsType(WebArchive.class, warPath),
            new ArrayList<
                Archive<
                    ?>>(), // reuse the War handling, but Auxiliary Archives should be added to the
            // EAR, not the WAR
            protocol,
            processor);
      } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
            "Can not manipulate war's that are not of type " + WebArchive.class, e);
      }
    } else if (applicationArchiveWars.size() > 1) {
      // TODO: fetch the TestDeployment.getArchiveForEnrichment
      throw new UnsupportedOperationException(
          "Multiple WebArchives found in "
              + applicationArchive.getName()
              + ". Can not determine which to enrich");
    } else {
      // reuse handle(JavaArchive, ..) logic
      Archive<?> wrappedWar = handleArchive(protocol, new ArrayList<Archive<?>>(), null, processor);
      applicationArchive.addAsModule(wrappedWar);

      if (applicationArchive.contains(APPLICATION_XML_PATH)) {
        ApplicationDescriptor applicationXml =
            Descriptors.importAs(ApplicationDescriptor.class)
                .from(applicationArchive.get(APPLICATION_XML_PATH).getAsset().openStream());

        applicationXml.webModule(wrappedWar.getName(), wrappedWar.getName());

        // SHRINKWRAP-187, to eager on not allowing overrides, delete it first
        applicationArchive.delete(APPLICATION_XML_PATH);
        applicationArchive.setApplicationXML(new StringAsset(applicationXml.exportAsString()));
      }
    }

    applicationArchive.addAsLibraries(auxiliaryArchives.toArray(new Archive<?>[0]));

    return applicationArchive;
  }
  private Archive<?> handleArchive(
      EnterpriseArchive applicationArchive,
      Collection<Archive<?>> auxiliaryArchives,
      WebArchive protocol,
      Processor processor,
      TestDeployment testDeployment) {
    Map<ArchivePath, Node> applicationArchiveWars =
        applicationArchive.getContent(Filters.include(".*\\.war"));
    if (applicationArchiveWars.size() == 1) {
      ArchivePath warPath = applicationArchiveWars.keySet().iterator().next();
      try {
        handleArchive(
            applicationArchive.getAsType(WebArchive.class, warPath),
            new ArrayList<
                Archive<
                    ?>>(), // reuse the War handling, but Auxiliary Archives should be added to the
                           // EAR, not the WAR
            protocol,
            processor);
      } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
            "Can not manipulate war's that are not of type " + WebArchive.class, e);
      }
    } else if (applicationArchiveWars.size() > 1) {
      Archive<?> archiveToTest = testDeployment.getArchiveForEnrichment();
      if (archiveToTest == null) {
        throw new UnsupportedOperationException(
            "Multiple WebArchives found in "
                + applicationArchive.getName()
                + ". Can not determine which to enrich");
      } else if (!archiveToTest.getName().endsWith(".war")) {
        // TODO: Removed throwing an exception when EJB modules are supported as well
        throw new UnsupportedOperationException("Archive to test is not a WebArchive!");
      } else {
        handleArchive(
            archiveToTest.as(WebArchive.class),
            new ArrayList<
                Archive<
                    ?>>(), // reuse the War handling, but Auxiliary Archives should be added to the
                           // EAR, not the WAR
            protocol,
            processor);
      }
    } else {
      // SHRINKWRAP-187, to eager on not allowing overrides, delete it first
      protocol.delete(WEB_XML_PATH);
      applicationArchive.addAsModule(
          protocol.setWebXML(new StringAsset(WebUtils.createNewDescriptor().exportAsString())));

      if (applicationArchive.contains(APPLICATION_XML_PATH)) {
        ApplicationDescriptor applicationXml =
            Descriptors.importAs(ApplicationDescriptor.class)
                .from(applicationArchive.get(APPLICATION_XML_PATH).getAsset().openStream());

        applicationXml.webModule(
            protocol.getName(), ServletUtil.calculateContextRoot(protocol.getName()));

        // SHRINKWRAP-187, to eager on not allowing overrides, delete it first
        applicationArchive.delete(APPLICATION_XML_PATH);
        applicationArchive.setApplicationXML(new StringAsset(applicationXml.exportAsString()));
      }

      processor.process(protocol);
    }
    applicationArchive.addAsLibraries(auxiliaryArchives.toArray(new Archive<?>[0]));
    return applicationArchive;
  }