Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 Red Hat, Inc. and/or its affiliates.
   *
   * Licensed under the Eclipse Public License version 1.0, available at
   * http://www.eclipse.org/legal/epl-v10.html
   */
  package org.jboss.forge.arquillian;
  
  import java.io.File;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
 import  org.jboss.arquillian.container.spi.client.deployment.DeploymentDescription;
 import  org.jboss.arquillian.container.spi.client.deployment.TargetDescription;
 import  org.jboss.arquillian.container.spi.client.protocol.ProtocolDescription;
 import  org.jboss.arquillian.container.test.api.Deployment;
 import  org.jboss.arquillian.container.test.api.OverProtocol;
 import  org.jboss.arquillian.container.test.api.ShouldThrowException;
 import  org.jboss.arquillian.container.test.api.TargetsContainer;
 import  org.jboss.arquillian.container.test.spi.client.deployment.DeploymentScenarioGenerator;
 import  org.jboss.arquillian.test.spi.TestClass;
 import  org.jboss.shrinkwrap.api.Archive;
 import  org.jboss.shrinkwrap.api.ShrinkWrap;
 import  org.jboss.shrinkwrap.descriptor.api.Descriptor;
 
 public class ForgeDeploymentScenarioGenerator implements DeploymentScenarioGenerator
 {
 
    @Override
    public List<DeploymentDescription> generate(TestClass testClass)
    {
       List<DeploymentDescription> deployments = new ArrayList<DeploymentDescription>();
       Method[] deploymentMethods = testClass.getMethods(Deployment.class);
       for (Method deploymentMethod : deploymentMethods)
       {
          validate(deploymentMethod);
          if (deploymentMethod.isAnnotationPresent(Dependencies.class))
             deployments.addAll(generateDependencyDeployments(testClass.getJavaClass(), deploymentMethod));
          deployments.add(generateDeployment(deploymentMethod));
       }
 
       return deployments;
    }
 
    private Collection<DeploymentDescription> generateDependencyDeployments(Class<?> classUnderTest,
             Method deploymentMethod)
    {
       Dependencies dependency = deploymentMethod.getAnnotation(Dependencies.class);
       Collection<DeploymentDescription> deployments = new ArrayList<DeploymentDescription>();
 
       if (dependency.value() != null)
          for (AddonDependency addon : dependency.value())
          {
             String version;
             if (addon.version().isEmpty())
             {
                version = resolveVersionFromPOM(classUnderTestaddon.name());
                if (version == null)
                {
                   throw new IllegalStateException("Could not resolve the version for [" + addon.name()
                            + "]. Either specify the version for this @" + AddonDependency.class.getSimpleName()
                            + " in [" + classUnderTest.getName() + "] or add it to pom.xml located at ["
                            + getPomFileFor(classUnderTest) + "]");
                }
             }
             else
             {
                version = addon.version();
             }
             AddonId id = AddonId.from(addon.name(), version);
             ForgeRemoteAddon remoteAddon = ShrinkWrap.create(ForgeRemoteAddon.class).setAddonId(id);
 
             if (Annotations.isAnnotationPresent(deploymentMethodDeployToRepository.class))
                remoteAddon.setAddonRepository(Annotations.getAnnotation(deploymentMethodDeployToRepository.class)
                         .value());
 
             DeploymentDescription deploymentDescription = new DeploymentDescription(id.toCoordinates(), remoteAddon);
             deploymentDescription.shouldBeTestable(false);
             deployments.add(deploymentDescription);
          }
 
       return deployments;
    }

   
Read the pom.xml of the project containing the class under test

Parameters:
classUnderTest
name
Returns:
   private String resolveVersionFromPOM(Class<?> classUnderTestString name)
   {
      if ( == null)
      {
         ProjectHelper projectHelper = new ProjectHelper();
          = new HashMap<StringString>();
         File pomFile = getPomFileFor(classUnderTest);
         try
         {
            // Needed for single-project addons
            Model model = projectHelper.loadPomFromFile(pomFile);
            String thisAddonName = (model.getGroupId() == null) ? model.getParent().getGroupId() : model.getGroupId()
                     + ":" + model.getArtifactId();
            String thisVersion = model.getVersion();
            .put(thisAddonNamethisVersion);
            List<Dependencydependencies = projectHelper.resolveDependenciesFromPOM(pomFile);
            for (Dependency dependency : dependencies)
            {
               Artifact artifact = dependency.getArtifact();
               String addonName = artifact.getGroupId() + ":" + artifact.getArtifactId();
               String version = artifact.getBaseVersion();
               .put(addonNameversion);
            }
         }
         catch (Exception e)
         {
            // TODO log this instead?
            e.printStackTrace();
         }
      }
      return .get(name);
   }
   private File getPomFileFor(Class<?> classUnderTest)
   {
      URL resource = classUnderTest.getClassLoader().getResource("");
      if (resource == null)
      {
         throw new IllegalStateException("Could not find the pom.xml for class " + classUnderTest.getName());
      }
      String directory = resource.getFile();
      File pomFile = findBuildDescriptor(directory);
      return pomFile;
   }
   private File findBuildDescriptor(String classLocation)
   {
      File pom = null;
      File dir = new File(classLocation);
      while (dir != null)
      {
         File testPom = new File(dir"pom.xml");
         if (testPom.isFile())
         {
            pom = testPom;
            break;
         }
         dir = dir.getParentFile();
      }
      return pom;
   }
   private void validate(Method deploymentMethod)
   {
      if (!Modifier.isStatic(deploymentMethod.getModifiers()))
      {
         throw new IllegalArgumentException("Method annotated with " + Deployment.class.getName() + " is not static. "
                  + deploymentMethod);
      }
      if (!Archive.class.isAssignableFrom(deploymentMethod.getReturnType())
               && !Descriptor.class.isAssignableFrom(deploymentMethod.getReturnType()))
      {
         throw new IllegalArgumentException(
                  "Method annotated with " + Deployment.class.getName() +
                           " must have return type " + Archive.class.getName() + " or " + Descriptor.class.getName()
                           + ". " + deploymentMethod);
      }
      if (deploymentMethod.getParameterTypes().length != 0)
      {
         throw new IllegalArgumentException("Method annotated with " + Deployment.class.getName()
                  + " can not accept parameters. " + deploymentMethod);
      }
      String name = deploymentMethod.getAnnotation(Deployment.class).name();
      try
      {
         if (!Strings.isNullOrEmpty(name) && !"_DEFAULT_".equals(name))
            AddonId.fromCoordinates(name);
      }
      catch (IllegalArgumentException e)
      {
         throw new IllegalArgumentException("@" + Deployment.class.getName()
                  + " requires name in the format \"name,version\", but was \"" + name + "\". ");
      }
   }

   

Parameters:
deploymentMethod
Returns:
   private DeploymentDescription generateDeployment(Method deploymentMethod)
   {
      TargetDescription target = generateTarget(deploymentMethod);
      ProtocolDescription protocol = generateProtocol(deploymentMethod);
      Deployment deploymentAnnotation = deploymentMethod.getAnnotation(Deployment.class);
      DeploymentDescription description = null;
      if (Archive.class.isAssignableFrom(deploymentMethod.getReturnType()))
      {
         Archive<?> archive = invoke(Archive.classdeploymentMethod);
         if (archive instanceof RepositoryForgeArchive)
         {
            if (Annotations.isAnnotationPresent(deploymentMethodDeployToRepository.class))
               ((RepositoryForgeArchivearchive).setAddonRepository(Annotations.getAnnotation(deploymentMethod,
                        DeployToRepository.class).value());
         }
         description = new DeploymentDescription(deploymentAnnotation.name(), archive);
         description.shouldBeTestable(deploymentAnnotation.testable());
      }
      else if (Descriptor.class.isAssignableFrom(deploymentMethod.getReturnType()))
      {
         description = new DeploymentDescription(deploymentAnnotation.name(),
                  invoke(Descriptor.classdeploymentMethod));
      }
      description.shouldBeManaged(deploymentAnnotation.managed());
      description.setOrder(deploymentAnnotation.order());
      if (target != null)
      {
         description.setTarget(target);
      }
      if (protocol != null)
      {
         description.setProtocol(protocol);
      }
      if (deploymentMethod.isAnnotationPresent(ShouldThrowException.class))
      {
         description.setExpectedException(deploymentMethod.getAnnotation(ShouldThrowException.class).value());
      }
      return description;
   }

   

Parameters:
deploymentMethod
Returns:
   private TargetDescription generateTarget(Method deploymentMethod)
   {
      if (deploymentMethod.isAnnotationPresent(TargetsContainer.class))
      {
         return new TargetDescription(deploymentMethod.getAnnotation(TargetsContainer.class).value());
      }
      return TargetDescription.DEFAULT;
   }

   

Parameters:
deploymentMethod
Returns:
   private ProtocolDescription generateProtocol(Method deploymentMethod)
   {
      if (deploymentMethod.isAnnotationPresent(OverProtocol.class))
      {
         return new ProtocolDescription(deploymentMethod.getAnnotation(OverProtocol.class).value());
      }
      return ProtocolDescription.DEFAULT;
   }

   

Parameters:
deploymentMethod
Returns:
   private <T> T invoke(Class<T> typeMethod deploymentMethod)
   {
      try
      {
         return type.cast(deploymentMethod.invoke(null));
      }
      catch (Exception e)
      {
         throw new RuntimeException("Could not invoke deployment method: " + deploymentMethode);
      }
   }
New to GrepCode? Check out our FAQ X