1   package com.atlassian.maven.plugins.amps;
2   
3   import com.atlassian.maven.plugins.amps.product.ProductHandler;
4   import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
5   import org.apache.maven.plugin.MojoExecutionException;
6   import org.jfrog.maven.annomojo.annotations.MojoParameter;
7   
8   import java.util.ArrayList;
9   import java.util.Arrays;
10  import java.util.HashSet;
11  import java.util.List;
12  import java.util.Set;
13  
14  public abstract class AbstractTestGroupsHandlerMojo extends AbstractProductHandlerMojo
15  {
16      protected static final String NO_TEST_GROUP = "__no_test_group__";
17  
18      /**
19       * The list of configured test groups
20       */
21      @MojoParameter
22      private List<TestGroup> testGroups = new ArrayList<TestGroup>();
23  
24      protected final List<TestGroup> getTestGroups()
25      {
26          return testGroups;
27      }
28  
29      protected final List<ProductExecution> getTestGroupProductExecutions(String testGroupId) throws MojoExecutionException
30      {
31          // Create a container object to hold product-related stuff
32          List<ProductExecution> products = new ArrayList<ProductExecution>();
33          int dupCounter = 0;
34          Set<String> uniqueProductIds = new HashSet<String>();
35          for (String productId : getTestGroupProductIds(testGroupId))
36          {
37              Product ctx = getProductContexts(getMavenGoals()).get(productId);
38              if (ctx == null)
39              {
40                  throw new MojoExecutionException("The test group '" + testGroupId + "' refers to a product '" + productId
41                      + "' that doesn't have an associated <product> configuration.");
42              }
43              ProductHandler productHandler = createProductHandler(ctx.getId());
44  
45              // Give unique ids to duplicate product instances
46              if (uniqueProductIds.contains(productId))
47              {
48                  ctx.setInstanceId(productId + "-" + dupCounter++);
49              }
50              else
51              {
52                  uniqueProductIds.add(productId);
53              }
54              products.add(new ProductExecution(ctx, productHandler));
55          }
56  
57          return products;
58      }
59  
60      private List<String> getTestGroupProductIds(String testGroupId) throws MojoExecutionException
61      {
62          List<String> productIds = new ArrayList<String>();
63          if (NO_TEST_GROUP.equals(testGroupId))
64          {
65              productIds.add(getProductId());
66          }
67  
68          for (TestGroup group : testGroups)
69          {
70              if (group.getId().equals(testGroupId))
71              {
72                  productIds.addAll(group.getProductIds());
73              }
74          }
75          if (ProductHandlerFactory.getIds().contains(testGroupId) && !productIds.contains(testGroupId))
76          {
77              productIds.add(testGroupId);
78          }
79  
80          if (productIds.isEmpty())
81          {
82              List<String> validTestGroups = new ArrayList<String>();
83              for (TestGroup group: testGroups)
84              {
85                  validTestGroups.add(group.getId());
86              }
87              throw new MojoExecutionException("Unknown test group ID: " + testGroupId
88                  + " Detected IDs: " + Arrays.toString(validTestGroups.toArray()));
89          }
90  
91          return productIds;
92      }
93  
94  }