All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jayway.maven.plugins.android.AbstractIntegrationtestMojo Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-11
Show newest version
/*
 * Copyright (C) 2009 Jayway AB
 *
 * 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.
 */
package com.jayway.maven.plugins.android;

import com.jayway.maven.plugins.android.asm.AndroidTestFinder;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import java.io.File;
import java.util.Set;

/**
 * For integrationtest related Mojos.
 *
 * @author [email protected]
 */
public abstract class AbstractIntegrationtestMojo extends AbstractAndroidMojo {
    /**
     * -Dmaven.test.skip is commonly used with Maven to skip tests. We honor it too.
     *
     * @parameter expression="${maven.test.skip}" default-value=false
     * @readonly
     */
    private boolean mavenTestSkip;

    /**
     * Enables or disables integration test related goals. If true they will be run; if false,
     * they will be skipped. If auto, they will run if any of the classes inherit from any class in
     * junit.framework.** or android.test.**.
     *
     * @parameter expression="${android.enableIntegrationTest}" default-value="auto"
     */
    private String enableIntegrationTest;

    /**
     * Whether or not to execute integration test related goals. Reads from configuration parameter
     * enableIntegrationTest, but can be overridden with -Dmaven.test.skip.
     *
     * @return true if integration test goals should be executed, false otherwise.
     */
    protected boolean isEnableIntegrationTest() throws MojoFailureException, MojoExecutionException {
        if (mavenTestSkip) {
            return false;
        }

        if ("false".equalsIgnoreCase(enableIntegrationTest)) {
            return false;
        }

        if ("true".equalsIgnoreCase(enableIntegrationTest)) {
            return true;
        }

        if ("auto".equalsIgnoreCase(enableIntegrationTest)) {
            if (extractInstrumentationRunnerFromAndroidManifest(androidManifestFile) == null) {
                return false;
            }
            return AndroidTestFinder.containsAndroidTests(new File(project.getBuild().getDirectory(), "android-classes"));
        }

        throw new MojoFailureException("enableIntegrationTest must be configured as 'true', 'false' or 'auto'.");

    }

    protected void deployDependencies() throws MojoExecutionException {
        Set directDependentArtifacts = project.getDependencyArtifacts();
        if (directDependentArtifacts != null) {
            for (Artifact artifact : directDependentArtifacts) {
                String type = artifact.getType();
                if (type.equals("apk")) {
                    getLog().debug("Detected apk dependency " + artifact + ". Will resolve and deploy to device...");
                    final File targetApkFile = resolveArtifactToFile(artifact);
                    if (undeployBeforeDeploy) {
                        getLog().debug("Attempting undeploy of " + targetApkFile + " from device...");
                        undeployApk(targetApkFile);
                    }
                    getLog().debug("Deploying " + targetApkFile + " to device...");
                    deployApk(targetApkFile);
                }
            }
        }
    }

    protected void deployBuiltApk() throws MojoExecutionException {
        File apkFile = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName()
                + ANDROID_PACKAGE_EXTENSTION);
        deployApk(apkFile);
    }

    /**
     * Deploy an apk file, but undeploy first if {@link #undeployBeforeDeploy}{@code == true}.
     * @param apkFile the apk file to deploy
     * @throws MojoExecutionException
     */
    protected void deployFile(File apkFile) throws MojoExecutionException {
        if (undeployBeforeDeploy) {
            undeployApk(apkFile);
        }
        deployApk(apkFile);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy