/*
 * Copyright 2012 akquinet
 * 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 de.akquinet.innovation.play.maven;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.IOException;

/**
 * Run the test of the Play application.
 * The tests are run using <tt>play test</tt>
 *
 * @goal test
 * @phase test
 */
public class Play2TestMojo
        extends AbstractPlay2Mojo {

    /**
     * Set this to "true" to skip running tests, but still compile them. Its use is NOT RECOMMENDED, but quite
     * convenient on occasion.
     *
     * @parameter default-value="false" expression="${skipTests}"
     */
    private boolean skipTests;

    /**
     * Set this to "true" to bypass unit tests entirely. Its use is NOT RECOMMENDED, especially if you enable it using
     * the "maven.test.skip" property, because maven.test.skip disables both running the tests and compiling the tests.
     * Consider using the <code>skipTests</code> parameter instead.
     *
     * @parameter default-value="false" expression="${maven.test.skip}"
     */
    private boolean skip;

    /**
     * Set this to "true" to ignore a failure during testing. Its use is NOT RECOMMENDED, but quite convenient on
     * occasion.
     *
     * @parameter default-value="false" expression="${maven.test.failure.ignore}"
     */
    private boolean testFailureIgnore;

    public void execute()
            throws MojoExecutionException {

        if (isSkipExecution()) {
            getLog().info("Test phase skipped");
            return;
        }

        String line = getPlay2().getAbsolutePath();

        CommandLine cmdLine = CommandLine.parse(line);
        cmdLine.addArguments(getPlay2SystemPropertiesArguments(), false);
        cmdLine.addArgument("test");
        DefaultExecutor executor = new DefaultExecutor();

        if (timeout > 0) {
            ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
            executor.setWatchdog(watchdog);
        }

        executor.setWorkingDirectory(project.getBasedir());

        executor.setExitValue(0);
        try {
            executor.execute(cmdLine, getEnvironment());
        } catch (IOException e) {
            if (testFailureIgnore) {
                getLog().error("Test execution failures ignored");
            } else {
                throw new MojoExecutionException("Error during compilation", e);
            }
        }
    }

    protected boolean isSkipExecution() {
        return skip || skipTests;
    }
}

@@ -817,24 +817,45 @@
 Exception;%0A%0A
+import java.io.File;%0A
 import java.
@@ -870,16 +870,16 @@
 eption;%0A
-
 %0A/**%0A * 
@@ -1324,17 +1324,16 @@
 pTests;%0A
-%0A
     /**%0A
@@ -1752,17 +1752,16 @@
 n skip;%0A
-%0A
     /**%0A
@@ -2204,32 +2204,158 @@
 urn;%0A        %7D%0A%0A
+        if (noTestFound()) %7B%0A            getLog().info(%22Test phase skipped - no tests found%22);%0A            return;%0A        %7D%0A%0A
         String l
@@ -3183,24 +3183,24 @@
           %7D%0A
-
         %7D%0A  
@@ -3200,24 +3200,1053 @@
    %7D%0A    %7D%0A%0A
+    /**%0A     * Does an educated guess on the existence of tests.%0A     * This methods checks that the %60test%60 directory is existing and not empty,%0A     * as well as the %60src/test/main%60 directory.%0A     *%0A     * @return %3Ccode%3Etrue%3C/code%3E if no tests are found.%0A     */%0A    private boolean noTestFound() %7B%0A        File mavenTestDirectory = new File(project.getBuild().getTestSourceDirectory());%0A        File playTest = new File(baseDirectory, %22test%22);%0A%0A        getLog().debug(%22Searching tests in %22 + playTest.getAbsolutePath()%0A                + %22 and %22 + mavenTestDirectory.getAbsolutePath());%0A%0A        int count = 0;%0A        if (playTest.isDirectory()) %7B%0A            String%5B%5D names = playTest.list();%0A            if (names != null) %7B%0A                count += names.length;%0A            %7D%0A        %7D%0A%0A        if (mavenTestDirectory.isDirectory()) %7B%0A            String%5B%5D names = mavenTestDirectory.list();%0A            if (names != null) %7B%0A                count += names.length;%0A            %7D%0A        %7D%0A%0A        return count == 0;%0A    %7D%0A%0A
     protecte

Fix issue #39