PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/maven-amps-plugin/src/main/java/com/atlassian/maven/plugins/amps/AbstractTestGroupsHandlerMojo.java

https://bitbucket.org/mmeinhold/amps
Java | 104 lines | 77 code | 14 blank | 13 comment | 11 complexity | 3bdd5dbdc48f164d2beaa76ebbb796e3 MD5 | raw file
  1package com.atlassian.maven.plugins.amps;
  2
  3import java.util.ArrayList;
  4import java.util.Arrays;
  5import java.util.HashSet;
  6import java.util.List;
  7import java.util.Map;
  8import java.util.Set;
  9
 10import com.atlassian.maven.plugins.amps.product.ProductHandler;
 11import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
 12
 13import org.apache.maven.plugin.MojoExecutionException;
 14import org.apache.maven.plugins.annotations.Parameter;
 15
 16public abstract class AbstractTestGroupsHandlerMojo extends AbstractProductHandlerMojo
 17{
 18
 19    /**
 20     * The list of configured test groups
 21     */
 22    @Parameter
 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();
 37        for (String instanceId : getTestGroupInstanceIds(testGroupId))
 38        {
 39            Product ctx = productContexts.get(instanceId);
 40            if (ctx == null)
 41            {
 42                throw new MojoExecutionException("The test group '" + testGroupId + "' refers to a product '" + instanceId
 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(instanceId))
 49            {
 50                ctx.setInstanceId(instanceId + "-" + dupCounter++);
 51            }
 52            else
 53            {
 54                uniqueProductIds.add(instanceId);
 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> getTestGroupInstanceIds(String testGroupId) throws MojoExecutionException
 71    {
 72        List<String> instanceIds = new ArrayList<String>();
 73        if (NO_TEST_GROUP.equals(testGroupId))
 74        {
 75            instanceIds.add(getProductId());
 76        }
 77
 78        for (TestGroup group : testGroups)
 79        {
 80            if (group.getId().equals(testGroupId))
 81            {
 82                instanceIds.addAll(group.getInstanceIds());
 83            }
 84        }
 85        if (ProductHandlerFactory.getIds().contains(testGroupId) && !instanceIds.contains(testGroupId))
 86        {
 87            instanceIds.add(testGroupId);
 88        }
 89
 90        if (instanceIds.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 instanceIds;
102    }
103
104}