
org.bluestemsoftware.open.eoa.plugin.compile.IntegrationTestMojo Maven / Gradle / Ivy
/**
* Copyright 2008 Bluestem Software LLC. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
package org.bluestemsoftware.open.eoa.plugin.compile;
/*
* Adapted from org.apache.maven.plugin.CompilerMojo which was released under the following
* license:
*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.resolver.ArtifactCollector;
import org.apache.maven.plugin.MojoExecutionException;
import org.bluestemsoftware.open.eoa.plugin.util.ClasspathHelper;
import org.bluestemsoftware.open.eoa.plugin.util.DependencyHelper;
import org.bluestemsoftware.open.eoa.plugin.util.FileFilterImpl;
/**
* For each integration test suite, copies resources to output folder and then compiles sources
* to output folder. Note that plexus component descriptor included within plugin dist binds
* pre-integration-test phase within default lifecycle to this mojo's it-test-compile goal.
*
* By convention this mojo requires that the directory be structured as follows:
*
*
src
* -- it
* ---- suite1 (or name of your choice)
* ------ system
* -------- java
* -------- resources
* ------ client (optional)
* -------- java
* -------- resources
* ---- suite2 (or name of your choice)
* ------ system
* -------- java
* -------- resources
* ------ client (optional)
* -------- java
* -------- resources
*
*
* @goal it-test-compile
* @phase pre-integration-test
*/
public class IntegrationTestMojo extends AbstractCompilerMojo {
static final String LINE_BREAK = System.getProperty("line.separator");
/**
* Identifies suite(s) to be processed. Default is 'all'.
*
* @parameter expression="${maven.surefire.it.suite}" default-value="all"
*/
private String suiteName;
/**
* Collects artifacts.
*
* @component
*/
private ArtifactCollector artifactCollector;
/**
* For retrieval of artifact's metadata.
*
* @component
*/
private ArtifactMetadataSource metadataSource;
private File current;
private boolean isClientClasspath;
public void execute() throws MojoExecutionException, CompilationFailureException {
if (skip) {
getLog().info("tests skipped. not compiling sources");
return;
}
File it = new File(basedir, "src/it/");
if (!it.exists() || !it.isDirectory()) {
getLog().info("no sources to compile");
return;
}
if (suiteName.equals("all")) {
File[] suites = it.listFiles(new FileFilterImpl());
for (int i = 0; i < suites.length; i++) {
File suite = suites[i];
if (!suite.isDirectory()) {
throw new MojoExecutionException("Invalid directory structure.");
}
getLog().info("processing test suite " + suite.getName());
File system = new File(suite, "system/");
if (!system.exists() || !system.isDirectory()) {
throw new MojoExecutionException("Invalid directory structure.");
}
current = system;
isClientClasspath = false;
getLog().info("compiling sources for system tests");
super.execute();
File client = new File(suite, "client/");
if (!client.exists()) {
continue;
}
if (!client.isDirectory()) {
throw new MojoExecutionException("Invalid directory structure.");
}
current = client;
isClientClasspath = true;
getLog().info("compiling sources for client tests");
super.execute();
}
} else {
File suite = new File(it, suiteName);
if (!suite.isDirectory()) {
throw new MojoExecutionException("Invalid directory structure.");
}
getLog().info("processing test suite " + suite.getName());
File system = new File(suite, "system/");
if (!system.exists() || !system.isDirectory()) {
throw new MojoExecutionException("Invalid directory structure.");
}
current = system;
isClientClasspath = false;
getLog().info("compiling sources for system tests");
super.execute();
File client = new File(suite, "client/");
if (client.exists()) {
if (!client.isDirectory()) {
throw new MojoExecutionException("Invalid directory structure.");
}
current = client;
isClientClasspath = true;
getLog().info("compiling sources for client tests");
super.execute();
}
}
}
protected List getCompileSourceRoots() {
File sourceRoot = new File(current, "java");
getLog().debug("source root " + sourceRoot.getAbsolutePath());
return Collections.singletonList(sourceRoot.getAbsolutePath());
}
protected File getOutputDirectory() {
String suite = current.getParentFile().getName();
String relativePath = "target/it/" + suite + "/" + current.getName() + "/test-classes/";
File outputDirectory = new File(basedir, relativePath);
try {
copyDir(new File(current, "resources/"), outputDirectory);
} catch (Exception ex) {
throw new RuntimeException("Error configuring output directory. " + ex);
}
return outputDirectory;
}
protected List getClasspathElements() throws MojoExecutionException {
Set pd = null;
try {
pd = DependencyHelper.getProjectDependencies(project, artifactFactory);
} catch (Exception ex) {
throw new MojoExecutionException("Error retrieving project dependencies. " + ex);
}
ClasspathHelper classpathHelper = new ClasspathHelper(logger, basedir, project, false);
classpathHelper.setArtifactCollector(artifactCollector);
classpathHelper.setArtifactFactory(artifactFactory);
classpathHelper.setArtifactResolver(artifactResolver);
classpathHelper.setLocalRepository(localRepository);
classpathHelper.setMetadataSource(metadataSource);
classpathHelper.setRemoteRepositories(remoteRepositories);
classpathHelper.setTest(test);
Map urls = new HashMap();
if (!isClientClasspath) {
classpathHelper.setSystemTestClassesDirectory(getOutputDirectory());
Set versionlessKeys = new HashSet();
urls.putAll(classpathHelper.getParentClasspath(pd, versionlessKeys));
urls.putAll(classpathHelper.getSharedClasspath(pd, versionlessKeys, true));
urls.putAll(classpathHelper.getDeploymentClasspath(pd, versionlessKeys));
urls.putAll(classpathHelper.getSystemTestClasspath(pd, versionlessKeys));
} else {
classpathHelper.setClientTestClassesDirectory(getOutputDirectory());
Set versionlessKeys = new HashSet();
urls.putAll(classpathHelper.getClientTestClasspath(pd, versionlessKeys));
}
if (logger.isDebugEnabled()) {
logger.debug(LINE_BREAK);
logger.debug("classpath: ");
for (Map.Entry entry : urls.entrySet()) {
logger.debug("constituent: "
+ new File(entry.getKey()).getName()
+ " dependency trail: "
+ entry.getValue());
}
logger.debug("end classpath");
}
// strip scheme and decode each url before returning as
// an element within -classpath argument
List constituents = new ArrayList();
for (String url : urls.keySet()) {
try {
constituents.add(new URL(url).toURI().getPath());
} catch (Exception fatchance) {
}
}
return constituents;
}
}