/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

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