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.Map;
13  import java.util.Set;
14  
15  public abstract class AbstractTestGroupsHandlerMojo extends AbstractProductHandlerMojo
16  {
17      protected static final String NO_TEST_GROUP = "__no_test_group__";
18  
19      /**
20       * The list of configured test groups
21       */
22      @MojoParameter
23      private List<TestGroup> testGroups = new ArrayList<TestGroup>();
24  
25      protected final List<TestGroup> getTestGroups()
26      {
27          return testGroups;
28      }
29  
30      protected final List<ProductExecution> getTestGroupProductExecutions(String testGroupId) throws MojoExecutionException
31      {
32          // Create a container object to hold product-related stuff
33          List<ProductExecution> products = new ArrayList<ProductExecution>();
34          int dupCounter = 0;
35          Set<String> uniqueProductIds = new HashSet<String>();
36          Map<String, Product> productContexts = getProductContexts(getMavenGoals());
37          for (String productId : getTestGroupProductIds(testGroupId))
38          {
39              Product ctx = productContexts.get(productId);
40              if (ctx == null)
41              {
42                  throw new MojoExecutionException("The test group '" + testGroupId + "' refers to a product '" + productId
43                      + "' that doesn't have an associated <product> configuration.");
44              }
45              ProductHandler productHandler = createProductHandler(ctx.getId());
46  
47              // Give unique ids to duplicate product instances
48              if (uniqueProductIds.contains(productId))
49              {
50                  ctx.setInstanceId(productId + "-" + dupCounter++);
51              }
52              else
53              {
54                  uniqueProductIds.add(productId);
55              }
56              products.add(new ProductExecution(ctx, productHandler));
57          }
58  
59          return products;
60      }
61  
62      /**
63       * Returns the products in the test group:
64       * <ul>
65       * <li>If a {@literal <testGroup>} is defined, all the products of this test group</li>
66       * <li>If testGroupId is __no_test_group__, adds it</li>
67       * <li>If testGroupId is a product instanceId, adds it</li>
68       * </ul>
69       */
70      private List<String> getTestGroupProductIds(String testGroupId) throws MojoExecutionException
71      {
72          List<String> productIds = new ArrayList<String>();
73          if (NO_TEST_GROUP.equals(testGroupId))
74          {
75              productIds.add(getProductId());
76          }
77  
78          for (TestGroup group : testGroups)
79          {
80              if (group.getId().equals(testGroupId))
81              {
82                  productIds.addAll(group.getProductIds());
83              }
84          }
85          if (ProductHandlerFactory.getIds().contains(testGroupId) && !productIds.contains(testGroupId))
86          {
87              productIds.add(testGroupId);
88          }
89  
90          if (productIds.isEmpty())
91          {
92              List<String> validTestGroups = new ArrayList<String>();
93              for (TestGroup group: testGroups)
94              {
95                  validTestGroups.add(group.getId());
96              }
97              throw new MojoExecutionException("Unknown test group ID: " + testGroupId
98                  + " Detected IDs: " + Arrays.toString(validTestGroups.toArray()));
99          }
100 
101         return productIds;
102     }
103 
104 }