Commit 24500024 authored by Ole Hejlskov's avatar Ole Hejlskov
Browse files

Add integration-test module as a dependency for the platform war and include...

Add integration-test module as a dependency for the platform war and include it to allow remote execution
parent 85c7e11b
Loading
Loading
Loading
Loading
+15 −9
Original line number Diff line number Diff line
@@ -31,6 +31,21 @@

    <build>
        <plugins>

            <!-- Make sure we attach the tests so we can include them when running -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Filter the test resource files in the IT project, and do property substitutions.
                We need this config so this is done before the Alfresco Maven Plugin 'it' is executed. -->
            <plugin>
@@ -103,15 +118,6 @@
                        </goals>
                        <phase>pre-integration-test</phase>
                    </execution>
                    <!-- No need to call a shutdown mojo, it shuts down after the tests finishes
                    <execution>
                        <id>stop-alfresco</id>
                        <goals>
                            <goal>shutdown</goal>
                        </goals>
                        <phase>post-integration-test</phase>
                    </execution>
                    -->
                </executions>
            </plugin>
        </plugins>
+9 −0
Original line number Diff line number Diff line
@@ -307,6 +307,15 @@
                            <artifactId>${artifactId}-platform-jar</artifactId>
                            <version>${project.version}</version>
                        </moduleDependency>

                        <!-- Bring in the integration tests -->
                        <moduleDependency>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>integration-tests</artifactId>
                            <version>${project.version}</version>
                            <classifier>tests</classifier>
                        </moduleDependency>

                    </platformModules>

                    <!--
+35 −3
Original line number Diff line number Diff line
@@ -21,6 +21,11 @@ import org.alfresco.maven.plugin.config.ModuleDependency;
import org.alfresco.maven.plugin.config.TomcatDependency;
import org.alfresco.maven.plugin.config.TomcatWebapp;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
@@ -33,6 +38,10 @@ import org.codehaus.plexus.util.FileUtils;
import org.zeroturnaround.zip.ZipUtil;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -272,7 +281,7 @@ public abstract class AbstractRunMojo extends AbstractMojo {
    /**
     * Legacy to be compatible with maven.tomcat.port
     */
    @Parameter(property = "maven.tomcat.port", defaultValue = "8080")
    @Parameter(property = "maven.tomcat.port", defaultValue = "")
    protected String mavenTomcatPort;


@@ -345,7 +354,7 @@ public abstract class AbstractRunMojo extends AbstractMojo {
     */
    protected String getPort() {
        String port = tomcatPort;
        if (mavenTomcatPort.toString() != tomcatPort.toString()) {
        if (mavenTomcatPort != null) {
            port = mavenTomcatPort;
            getLog().info( "Tomcat Port overridden by property maven.tomcat.port");
        }
@@ -353,6 +362,28 @@ public abstract class AbstractRunMojo extends AbstractMojo {
        return port;
    }

    /**
     *
     */
    protected boolean tomcatIsRunning()  {

        CloseableHttpClient client= HttpClients.createDefault();
        CloseableHttpResponse response = null;

        try {
            HttpGet httpget = new HttpGet("http://localhost:" + getPort() + "/alfresco");
            response = client.execute(httpget);
            getLog().info("Tomcat is running on port "+ getPort());
            return true;

        } catch (Exception ex) {
            getLog().info("Tomcat is not running on port " + getPort() );
            return false;
        }



    }
    /**
     * Download and unpack the Solr 4 configuration as we don't have it in the project.
     * It will reside under /alf_data_dev/solr
@@ -877,6 +908,7 @@ public abstract class AbstractRunMojo extends AbstractMojo {
                        element(name("groupId"), moduleDep.getGroupId()),
                        element(name("artifactId"), moduleDep.getArtifactId()),
                        element(name("version"), moduleDep.getVersion()),
                        element(name("classifier"), moduleDep.getClassifier()),
                        element(name("type"), moduleDep.getType()),
                        element(name("overWrite"), "true"));

@@ -1143,7 +1175,7 @@ public abstract class AbstractRunMojo extends AbstractMojo {

        // Set up the system properties that should be set for Tomcat
        ArrayList systemProps = new ArrayList<Element>();
        systemProps.add(element(name("java.io.tmpdir"), "${project.build.directory}"));
        systemProps.add(element(name("java.io.tmpdir"), "${project.build.directory}/tmp"));
        if (enableSolr) {
            systemProps.add(element(name("solr.solr.home"), solrHome));
        }
+32 −203
Original line number Diff line number Diff line
@@ -79,9 +79,8 @@ public class IntegrationTestMojo extends AbstractRunMojo {
            return;
        }

//        execEnv.getMavenSession().getGoals().add("alfresco:it");

        getLog().info("Checking if Tomcat is already running on port " + "");
        if ( ! tomcatIsRunning() ) {

            if (enableSolr) {
                unpackSolrConfig();
@@ -105,18 +104,6 @@ public class IntegrationTestMojo extends AbstractRunMojo {
                                "alfresco-rad",
                                "${alfresco.sdk.version}",
                                ModuleDependency.TYPE_JAR));

            // Create a JAR with all tests and add to module dependencies for platform WAR
            // So we can run tests on the server
            // TODO: remove    different approach at the moment with separate integration-tests module   copyTestClassesFromSubModules2Parent();
            // TODO: remove    testJarArtifactId = packageAndInstallTestsJar();
            // TODO: remove platformModules.add(
            // TODO:         new ModuleDependency(
            // TODO:                 project.getGroupId(),
            // TODO:                 testJarArtifactId,
            // TODO:                 project.getVersion(),
            // TODO:                 ModuleDependency.TYPE_JAR));
            // Now build the platform WAR
                buildPlatformWar();
            }

@@ -131,167 +118,9 @@ public class IntegrationTestMojo extends AbstractRunMojo {
            if (startTomcat) {
                boolean fork = true;
                startTomcat(fork);
            // TODO: remove    different approach at the moment with separate integration-tests module   runIntegrationTests(testJarArtifactId);
            // TODO: remove    stopTomcat();
        }
    }

    /**
     * In an AIO project copy all integration test (IT) test-classes from sub projects/modules
     * to the parent target/test-classes
     *
     * @throws MojoExecutionException
     */
    protected void copyTestClassesFromSubModules2Parent() throws MojoExecutionException {
        // Get sub-module names, so we can see where to copy test classes from
        List<String> childModules = project.getModules();
        if (childModules == null || childModules.size() <= 0) {
            // Running in a single JAR module, nothing to copy,
            // all test classes are already in top level target/test-classes
            return;
        }

        for (String module : childModules) {
            getLog().info("Copying integration test-classes (*IT.class) from module '" + module +
                    "' to parent target/test-classes");

            executeMojo(
                    plugin(
                            groupId("org.apache.maven.plugins"),
                            artifactId("maven-resources-plugin"),
                            version(MAVEN_RESOURCE_PLUGIN_VERSION)
                    ),
                    goal("copy-resources"),
                    configuration(
                            element(name("outputDirectory"), "${project.build.testOutputDirectory}"),
                            element(name("resources"),
                                    element(name("resource"),
                                            element(name("directory"), module + "/target/test-classes"),
                                            element(name("includes"),
                                                    element(name("include"), "**/*.class")
                                            ),
                                            element(name("filtering"), "false")
                                    )
                            )
                    ),
                    execEnv
            );
        }
    }

    /**
     * Package all IT Tests in JAR file and install it in local maven repo.
     *
     * @return the customized JAR file artifactId, to be used by the failsafe plugin
     * @throws MojoExecutionException
     */
    protected String packageAndInstallTestsJar() throws MojoExecutionException {
        final String jarArtifactId = project.getArtifactId() + "-IT-classes";

        // Package the JAR file with all the Integration Tests (IT)
        // Note. don't use the maven-war-plugin here as it will package the module files twice, once from the
        // target/classes dir and once via the JAR
        final String jarPath = project.getBuild().getDirectory() + "/" + jarArtifactId + ".jar";
        final String jarSourceDir = project.getBuild().getDirectory() + "/test-classes";

        ZipUtil.pack(new File(jarSourceDir), new File(jarPath));

        getLog().info("Installing integration test JAR into local Maven repo.");

        // Install the Test JAR file in the local maven repo
        executeMojo(
                plugin(
                        groupId("org.apache.maven.plugins"),
                        artifactId("maven-install-plugin"),
                        version(MAVEN_INSTALL_PLUGIN_VERSION)
                ),
                goal("install-file"),
                configuration(
                        element(name("file"), jarPath),
                        element(name("groupId"), "${project.groupId}"),
                        element(name("artifactId"), jarArtifactId),
                        element(name("version"), "${project.version}"),
                        element(name("packaging"), "jar") // Don't forget, otherwise installed as .POM
                )
                , execEnv
        );

        return jarArtifactId;
    }

    /**
     * Run all IT tests contained in JAR with passed in artifact ID. Group ID and Version will be the same
     * as the project.
     *
     * @param testJarArtifactId
     * @throws MojoExecutionException
     */
    protected void runIntegrationTests(String testJarArtifactId) throws MojoExecutionException {
        getLog().info("Executing integration tests (*IT.class)...");

        // JUnit runner
        List<Dependency> failsafePluginDependencies = new ArrayList<Dependency>();
        failsafePluginDependencies.add(dependency("org.apache.maven.surefire", "surefire-junit47", "2.19.1"));

        // Add dependencies to classes under test
        List<Element> classpathElements = new ArrayList<>();
        List<String> childModules = project.getModules();
        if (childModules != null && childModules.size() > 0) {
            // Get sub-module names, so we can see what libs to include
            for (String module : childModules) {
                String classpathElement = project.getBasedir() + "/" + module + "/target/" + module + "-1.0-SNAPSHOT.jar";
                getLog().info("Adding module '" + classpathElement + "' to test classpath");
                classpathElements.add(element(name("additionalClasspathElement"), classpathElement));
            }
        }

        // Execute Failsafe Mojo
        Plugin failsafePlugin = plugin(
                groupId("org.apache.maven.plugins"),
                artifactId("maven-failsafe-plugin"),
                version("2.19.1"),
                failsafePluginDependencies
        );
        getLog().info("Start executing failsafe Mojo...");

        // This might be ugly, the MojoExecuter will only accept Element[] and we need this list to be dynamic
        // to avoid NPEs. If there's a better way to do this, then feel free to change it!
        Element[] classpathElementArray = new Element[classpathElements.size()];
        classpathElements.toArray(classpathElementArray);

        executeMojo(failsafePlugin,
                goal("integration-test"),
                configuration(
                        element(name("includes"),
                                element(name("include"), "**/*IT.class")
                        ),
                        element(name("additionalClasspathElements"),
                                classpathElementArray
                        ),
                        // IT Test dependency to scan
                        element(name("dependenciesToScan"),
                                element(name("dependency"), project.getGroupId() + ":" + testJarArtifactId)
                        )
                ),
                execEnv
        );

        getLog().info("Stopped executing failsafe Mojo");
    }

    protected void stopTomcat() throws MojoExecutionException {
        getLog().info("Stopping Tomcat...");

        Plugin tomcatPlugin = plugin(
                groupId("org.apache.tomcat.maven"),
                artifactId("tomcat7-maven-plugin"),
                version(MAVEN_TOMCAT7_PLUGIN_VERSION)
        );

        executeMojo(tomcatPlugin,
                goal("shutdown") ,
                configuration(),
                execEnv
        );
    }
}
 No newline at end of file
+19 −3
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ public abstract class MavenDependency {
    private String groupId;
    private String artifactId;
    private String version;
    private String classifier;

    public MavenDependency() {}

@@ -38,6 +39,14 @@ public abstract class MavenDependency {
        this.version = v;
    }

    public MavenDependency(String g, String a, String v, String c) {
        this.groupId = g;
        this.artifactId = a;
        this.version = v;
        this.classifier = c;
    }


    public String getGroupId() {
        return groupId;
    }
@@ -54,14 +63,20 @@ public abstract class MavenDependency {
        this.artifactId = artifactId;
    }

    public String getVersion() {
        return version;
    }
    public String getVersion() { return version; }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getClassifier() {
        return classifier;
    }

    public void setClassifier(String c) { this.classifier = c; }



    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
@@ -71,6 +86,7 @@ public abstract class MavenDependency {

        if (!groupId.equals(that.groupId)) return false;
        if (!artifactId.equals(that.artifactId)) return false;
        if (!classifier.equals(that.classifier)) return false;
        return version.equals(that.version);

    }