/*
 * The MIT License
 *
 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jvnet.hudson.test;

import hudson.cli.CLICommand;
import junit.framework.TestSuite;

import java.io.File;
import java.util.Map;

/**
 * Called by the code generated by maven-hpi-plugin to build tests for plugins.
 *
 * @author Kohsuke Kawaguchi
 */
public class PluginAutomaticTestBuilder {
    /**
     * @param params
     *      Various information about the plugin that maven-hpi-plugin adds.
     *      As of 1.52, this includes the followings:
     *
     *      basedir (String) : directory that contains pom.xml
     *      artifactId (String) : artifact ID of the plugin
     *      outputDirectory (String) : target/classes dir where class files and resources can be found
     *      testOutputDirectory (String) : target/test-classes.
     */
    public static TestSuite build(Map<String,?> params) throws Exception {
        TestSuite suite = new TestSuite();

        // automatic Jelly tests
        if (params.containsKey("outputDirectory") // shouldn't happen, but be defensive
        ||  notSkipTests("JellyTest")) {
            File outputDirectory = new File((String)params.get("outputDirectory"));
            suite.addTest(JellyTestSuiteBuilder.build(outputDirectory,toBoolean(params.get("requirePI"))));
        }

        suite.addTestSuite(CliSanityTest.class);

        return suite;
    }

    private static boolean toBoolean(Object requirePI) {
        if (requirePI==null)    return false;
        if (requirePI instanceof Boolean)   return (Boolean)requirePI;
        return Boolean.parseBoolean(requirePI.toString());
    }

    /**
     * Provides an escape hatch for plugin developers to skip auto-generated tests.
     */
    private static boolean notSkipTests(String propertyName) {
        return !Boolean.getBoolean("hpiTest.skip"+propertyName);
    }

    public static class CliSanityTest extends HudsonTestCase {
        public void testCliSanity() {
            CLICommand.clone("help");
        }
    }
}

@@ -1210,16 +1210,59 @@
 ommand;%0A
+import java.io.File;%0Aimport java.util.Map;%0A
 import j
@@ -1284,24 +1284,22 @@
 Test
-Suit
+Cas
 e;%0A
-%0A
 import j
 ava.
@@ -1298,41 +1298,32 @@
 rt j
-ava.io.File;%0Aimport java.util.Map
+unit.framework.TestSuite
 ;%0A%0A/
@@ -1483,16 +1483,175 @@
 ilder %7B%0A
+%0A    /** %7B@code InjectedTest%7D mistakenly calls %7B@link #build%7D as an instance method, and uses this constructor. */%0A    public PluginAutomaticTestBuilder() %7B%7D%0A%0A
     /**%0A
@@ -2186,52 +2186,8 @@
 n %7B%0A
-        TestSuite suite = new TestSuite();%0A%0A
     
@@ -2343,16 +2343,97 @@
 est%22)) %7B
+ // TODO this is probably obsolete given -Dmaven-hpi-plugin.disabledTestInjection
 %0A       
@@ -2525,22 +2525,26 @@
     
-suite.addTest(
+TestSuite suite = 
 Jell
@@ -2622,22 +2622,14 @@
 %22)))
-)
 ;%0A    
-    %7D%0A%0A
     
@@ -2649,58 +2649,131 @@
 Test
-Suite(CliSanityTest.class);%0A%0A        return suite;
+(new OtherTests(%22testCliSanity%22));%0A            return suite;%0A        %7D else %7B%0A            return new TestSuite();%0A        %7D
 %0A   
@@ -3279,21 +3279,18 @@
 ass 
-CliSanity
+Other
 Test
+s
  ext
@@ -3298,14 +3298,8 @@
 nds 
-Hudson
 Test
@@ -3301,24 +3301,102 @@
  TestCase %7B%0A
+%0A        public OtherTests(String name) %7B%0A            super(name);%0A        %7D%0A%0A
         publ
@@ -3465,20 +3465,21 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%7D%0A

Consolidate testCliSanity into the same HudsonTestCase run as the Jelly tests, to avoid overhead.