package org.wetator;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.DirectoryScanner;
import org.wetator.core.WetatorEngine;

import java.io.File;

/**
 * This is the main Mojo for executing wetator tests.
 */
@Mojo(name = "execute")
public class WetatorMojo extends AbstractMojo {

    /**
     * File pattern for the weator test files.
     */
    private static final String INCLUDE_FILE_PATTERN = "**\\*.wet";

    /**
     * Path with filename to the config file in file system.
     */
    @Parameter(property = "execute.configFile")
    private String configFile;

    /**
     * Directory where the test files resides.
     */
    @Parameter(property = "execute.testFileDir")
    private String testFileDir;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("start running wetator tests ...");
        getLog().info("using config file: " + configFile);
        getLog().info("using wetator test file directory: " + testFileDir);

        try {
            final WetatorEngine wetatorEngine = new WetatorEngine();
            wetatorEngine.setConfigFileName(configFile);

            wetatorEngine.init();

            // add all files
            final DirectoryScanner directoryScanner = new DirectoryScanner();
            directoryScanner.setBasedir(testFileDir);
            directoryScanner.setIncludes(new String[]{INCLUDE_FILE_PATTERN});
            directoryScanner.scan();

            final String[] weatorTestFilenames = directoryScanner.getIncludedFiles();

            for (int i = 0; i < weatorTestFilenames.length; i++) {
                final String filename = weatorTestFilenames[i];
                getLog().info("adding test file: " + filename);
                wetatorEngine.addTestCase(filename, new File(directoryScanner.getBasedir(), filename));
            }

            wetatorEngine.executeTests();

            getLog().info("wetator test execution complete!");
        } catch (Exception e) {
            getLog().error(e.getMessage(), e);
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
}

@@ -1422,285 +1422,8 @@
 );%0A%0A
-            // add all files%0A            final DirectoryScanner directoryScanner = new DirectoryScanner();%0A            directoryScanner.setBasedir(testFileDir);%0A            directoryScanner.setIncludes(new String%5B%5D%7BINCLUDE_FILE_PATTERN%7D);%0A            directoryScanner.scan();%0A%0A
     
@@ -1471,36 +1471,26 @@
 s = 
-directoryScanner.getIncluded
+scanForWetatorTest
 File
@@ -1490,25 +1490,24 @@
 estFiles();%0A
-%0A
             
@@ -1515,22 +1515,14 @@
 or (
-int i = 0; i %3C
+String
  wea
@@ -1540,64 +1540,10 @@
 name
-s.length; i++) %7B%0A                final String filename =
+ :
  wea
@@ -1562,12 +1562,11 @@
 ames
-%5Bi%5D;
+) %7B
 %0A   
@@ -1615,17 +1615,27 @@
 le: %22 + 
-f
+weatorTestF
 ilename)
@@ -1678,17 +1678,27 @@
 estCase(
-f
+weatorTestF
 ilename,
@@ -1711,40 +1711,32 @@
 ile(
-directoryScanner.getBasedir(), f
+testFileDir, weatorTestF
 ilen
@@ -1796,24 +1796,154 @@
 teTests();%0A%0A
+            getLog().info(%22find wetator test results in: %22 + wetatorEngine.getConfiguration().getOutputDir().getCanonicalPath());%0A
             
@@ -2152,11 +2152,351 @@
  %7D%0A    %7D
+%0A%0A    private String%5B%5D scanForWetatorTestFiles() %7B%0A        final DirectoryScanner directoryScanner = new DirectoryScanner();%0A        directoryScanner.setBasedir(testFileDir);%0A        directoryScanner.setIncludes(new String%5B%5D%7BINCLUDE_FILE_PATTERN%7D);%0A        directoryScanner.scan();%0A%0A        return directoryScanner.getIncludedFiles();%0A    %7D
 %0A%7D%0A

update logging