PageRenderTime 50ms CodeModel.GetById 10ms app.highlight 33ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://bitbucket.org/mmeinhold/amps
Java | 350 lines | 247 code | 53 blank | 50 comment | 41 complexity | 30823250cf5f47c8e80368cfec6e2044 MD5 | raw file
  1package com.atlassian.maven.plugins.amps;
  2
  3import java.io.File;
  4import java.util.*;
  5
  6import com.atlassian.maven.plugins.amps.product.AmpsDefaults;
  7import com.atlassian.maven.plugins.amps.product.ProductHandler;
  8
  9import org.apache.commons.lang.StringUtils;
 10import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
 11import org.apache.maven.plugin.MojoExecutionException;
 12import org.apache.maven.plugins.annotations.Component;
 13import org.apache.maven.plugins.annotations.Mojo;
 14import org.apache.maven.plugins.annotations.Parameter;
 15import org.apache.maven.plugins.annotations.ResolutionScope;
 16import org.apache.maven.project.MavenProject;
 17
 18/**
 19 * Run the integration tests against the webapp
 20 */
 21@Mojo(name = "integration-test", requiresDependencyResolution = ResolutionScope.TEST)
 22public class IntegrationTestMojo extends AbstractTestGroupsHandlerMojo
 23{
 24
 25    /**
 26     * Pattern for to use to find integration tests.  Only used if no test groups are defined.
 27     */
 28    @Parameter(property = "functional.test.pattern")
 29    private String functionalTestPattern = "it/**";
 30
 31    /**
 32     * The directory containing generated test classes of the project being tested.
 33     */
 34    @Parameter(property = "project.build.testOutputDirectory", required = true)
 35    private File testClassesDirectory;
 36
 37    /**
 38     * A comma separated list of test groups to run.  If not specified, all
 39     * test groups are run.
 40     */
 41    @Parameter(property = "testGroups")
 42    private String configuredTestGroupsToRun;
 43
 44    /**
 45     * Whether the reference application will not be started or not
 46     */
 47    @Parameter(property = "no.webapp", defaultValue = "false")
 48    private boolean noWebapp = false;
 49
 50    @Component
 51    private ArtifactHandlerManager artifactHandlerManager;
 52
 53    @Parameter(property = "maven.test.skip", defaultValue = "false")
 54    private boolean testsSkip = false;
 55
 56    @Parameter(property = "skipTests", defaultValue = "false")
 57    private boolean skipTests = false;
 58
 59    /**
 60     * Skip the integration tests along with any product startups
 61     */
 62    @Parameter(property = "skipITs", defaultValue = "false")
 63    private boolean skipITs = false;
 64
 65    /**
 66     * port for debugging
 67     */
 68    @Parameter(property = "jvm.debug.port", defaultValue = "0")
 69    protected int jvmDebugPort;
 70
 71    /**
 72     * Suspend when debugging
 73     */
 74    @Parameter(property = "jvm.debug.suspend")
 75    protected boolean jvmDebugSuspend = false;
 76
 77    protected void doExecute() throws MojoExecutionException
 78    {
 79        final MavenProject project = getMavenContext().getProject();
 80
 81        // workaround for MNG-1682/MNG-2426: force maven to install artifact using the "jar" handler
 82        project.getArtifact().setArtifactHandler(artifactHandlerManager.getArtifactHandler("jar"));
 83
 84        if (!new File(testClassesDirectory, "it").exists())
 85        {
 86            getLog().info("No integration tests found");
 87            return;
 88        }
 89
 90        if (skipTests || testsSkip || skipITs)
 91        {
 92            getLog().info("Integration tests skipped");
 93            return;
 94        }
 95
 96        final MavenGoals goals = getMavenGoals();
 97        final String pluginJar = targetDirectory.getAbsolutePath() + "/" + finalName + ".jar";
 98
 99        final Set<String> configuredTestGroupIds = getTestGroupIds();
100        if (configuredTestGroupIds.isEmpty())
101        {
102            runTestsForTestGroup(NO_TEST_GROUP, goals, pluginJar, copy(systemPropertyVariables));
103        }
104        else if (configuredTestGroupsToRun != null)
105        {
106            String[] testGroupIdsToRun = configuredTestGroupsToRun.split(",");
107
108            // now run the tests
109            for (String testGroupId : testGroupIdsToRun)
110            {
111                if (!configuredTestGroupIds.contains(testGroupId))
112                {
113                    getLog().warn("Test group " + testGroupId + " does not exist");
114                }
115                else
116                {
117                    runTestsForTestGroup(testGroupId, goals, pluginJar, copy(systemPropertyVariables));
118                }
119            }
120        }
121        else
122        {
123            for (String testGroupId : configuredTestGroupIds)
124            {
125                runTestsForTestGroup(testGroupId, goals, pluginJar, copy(systemPropertyVariables));
126            }
127        }
128    }
129
130    private Map<String,Object> copy(Map<String,Object> systemPropertyVariables)
131    {
132        return new HashMap<String,Object>(systemPropertyVariables);
133    }
134
135    /**
136     * Returns product-specific properties to pass to the container during
137     * integration testing. Default implementation does nothing.
138     * @param product the {@code Product} object to use
139     * @return a {@code Map} of properties to add to the system properties passed
140     * to the container
141     */
142    protected Map<String, String> getProductFunctionalTestProperties(Product product)
143    {
144        return Collections.emptyMap();
145    }
146
147    private Set<String> getTestGroupIds() throws MojoExecutionException
148    {
149        Set<String> ids = new HashSet<String>();
150
151        //ids.addAll(ProductHandlerFactory.getIds());
152        for (TestGroup group : getTestGroups())
153        {
154            ids.add(group.getId());
155        }
156
157        return ids;
158    }
159
160
161
162    private void runTestsForTestGroup(String testGroupId, MavenGoals goals, String pluginJar, Map<String,Object> systemProperties) throws MojoExecutionException
163    {
164        List<String> includes = getIncludesForTestGroup(testGroupId);
165        List<String> excludes = getExcludesForTestGroup(testGroupId);
166
167        List<ProductExecution> executionsDeclaredInPom = getTestGroupProductExecutions(testGroupId);
168        List<ProductExecution> productExecutions = includeStudioDependentProducts(executionsDeclaredInPom, goals);
169        setParallelMode(productExecutions);
170
171        int counter = 0;
172        // Install the plugin in each product and start it
173        for (ProductExecution productExecution : productExecutions)
174        {
175            ProductHandler productHandler = productExecution.getProductHandler();
176            Product product = productExecution.getProduct();
177            if (product.isInstallPlugin() == null)
178            {
179                product.setInstallPlugin(installPlugin);
180            }
181            
182            if(shouldBuildTestPlugin())
183            {
184                List<ProductArtifact> plugins = product.getBundledArtifacts();
185                plugins.addAll(testFrameworkPlugins);
186            }
187
188            int actualHttpPort = 0;
189            if (!noWebapp)
190            {
191                if(jvmDebugPort > 0)
192                {
193                    if (product.getJvmDebugPort() == 0)
194                    {
195                        product.setJvmDebugPort(jvmDebugPort + counter++);
196                    }
197                    final int debugPort = product.getJvmDebugPort();
198                    String debugArgs = " -Xdebug -Xrunjdwp:transport=dt_socket,address=" +
199                            String.valueOf(debugPort) + ",suspend=" + (jvmDebugSuspend ? "y" : "n") + ",server=y ";
200    
201                    if (product.getJvmArgs() == null)
202                    {
203                        product.setJvmArgs(StringUtils.defaultString(jvmArgs));
204                    }
205    
206                    product.setJvmArgs(product.getJvmArgs() + debugArgs);
207                }
208                
209                actualHttpPort = productHandler.start(product);
210            }
211
212            if (productExecutions.size() == 1)
213            {
214                systemProperties.put("http.port", String.valueOf(actualHttpPort));
215                systemProperties.put("context.path", product.getContextPath());
216            }
217
218            String baseUrl = MavenGoals.getBaseUrl(product, actualHttpPort);
219            // hard coded system properties...
220            systemProperties.put("http." + product.getInstanceId() + ".port", String.valueOf(actualHttpPort));
221            systemProperties.put("context." + product.getInstanceId() + ".path", product.getContextPath());
222            systemProperties.put("http." + product.getInstanceId() + ".url", MavenGoals.getBaseUrl(product, actualHttpPort));
223
224            systemProperties.put("baseurl." + product.getInstanceId(), baseUrl);
225            systemProperties.put("plugin.jar", pluginJar);
226
227            // yes, this means you only get one base url if multiple products, but that is what selenium would expect
228            if (!systemProperties.containsKey("baseurl"))
229            {
230                systemProperties.put("baseurl", baseUrl);
231            }
232
233            systemProperties.put("homedir." + product.getInstanceId(), productHandler.getHomeDirectory(product).getAbsolutePath());
234            if (!systemProperties.containsKey("homedir"))
235            {
236                systemProperties.put("homedir", productHandler.getHomeDirectory(product).getAbsolutePath());
237            }
238
239            systemProperties.putAll(getProductFunctionalTestProperties(product));
240        }
241        systemProperties.put("testGroup", testGroupId);
242        systemProperties.putAll(getTestGroupSystemProperties(testGroupId));
243
244        /*
245        Commenting out the parallel check so we ALWAYS wait until the products are started
246         */
247        /*if (parallel)
248        {
249            waitForProducts(productExecutions, true);
250        }*/
251
252        waitForProducts(productExecutions, true);
253
254        // Actually run the tests
255        goals.runIntegrationTests("group-" + testGroupId, getClassifier(testGroupId), includes, excludes, systemProperties, targetDirectory);
256
257        // Shut all products down
258        if (!noWebapp)
259        {
260            stopProducts(productExecutions);
261        }
262    }
263
264    /**
265     * Returns the classifier of the test group. Unless specified, this is "tomcat6x", the default container.
266     */
267    private String getClassifier(String testGroupId)
268    {
269        for (TestGroup group : getTestGroups())
270        {
271            if (group.getId().equals(testGroupId))
272            {
273                if (group.getClassifier() != null)
274                {
275                    return group.getClassifier();
276                }
277                else
278                {
279                    return AmpsDefaults.DEFAULT_CONTAINER;
280                }
281            }
282        }
283        return AmpsDefaults.DEFAULT_CONTAINER;
284    }
285
286
287    private Map<String, String> getTestGroupSystemProperties(String testGroupId)
288    {
289        if (NO_TEST_GROUP.equals(testGroupId))
290        {
291            return Collections.emptyMap();
292        }
293
294        for (TestGroup group : getTestGroups())
295        {
296            if (group.getId().equals(testGroupId))
297            {
298                return group.getSystemProperties();
299            }
300        }
301        return Collections.emptyMap();
302    }
303
304    private List<String> getIncludesForTestGroup(String testGroupId)
305    {
306        if (NO_TEST_GROUP.equals(testGroupId))
307        {
308            return Collections.singletonList(functionalTestPattern);
309        }
310        else
311        {
312            for (TestGroup group : getTestGroups())
313            {
314                if (group.getId().equals(testGroupId))
315                {
316                    List<String> groupIncludes =  group.getIncludes();
317                    if(groupIncludes.isEmpty())
318                    {
319                        return Collections.singletonList(functionalTestPattern);
320                    }
321                    else
322                    {
323                        return groupIncludes;
324                    }
325                }
326            }
327        }
328        return Collections.singletonList(functionalTestPattern);
329    }
330
331
332    private List<String> getExcludesForTestGroup(String testGroupId)
333    {
334        if (NO_TEST_GROUP.equals(testGroupId))
335        {
336            return Collections.emptyList();
337        }
338        else
339        {
340            for (TestGroup group : getTestGroups())
341            {
342                if (group.getId().equals(testGroupId))
343                {
344                    return group.getExcludes();
345                }
346            }
347        }
348        return Collections.emptyList();
349    }
350}