package hudson.plugins.groovy;

import static org.junit.Assert.assertTrue;
import hudson.model.FreeStyleProject;
import hudson.tasks.Builder;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class PluginTest {

    @Rule
    public JenkinsRule j = new JenkinsRule();
    
    @Test
    public void testAllowTokenMacro() throws Exception {
        HtmlPage page = j.createWebClient().goTo("configure");
        HtmlCheckBoxInput allowMacroCheckBox = page.getElementByName("allowMacro");
        allowMacroCheckBox.setChecked(true);
        j.submit(page.getFormByName("config"));
        Groovy.DescriptorImpl descriptor = (Groovy.DescriptorImpl) j.jenkins.getDescriptor(Groovy.class);
        assertTrue(descriptor.getAllowMacro());
    }

    @Test
    public void roundtripTestSystemGroovyStringScript() throws Exception {
        SystemGroovy before = new SystemGroovy(new StringScriptSource("println 'Test'"),"TEST=45","test.jar");
        SystemGroovy after = doRoundtrip(before, SystemGroovy.class);

        j.assertEqualBeans(before, after, "bindings,classpath");
        j.assertEqualBeans(before.getScriptSource(), after.getScriptSource(), "command");
    }

    @Test
    public void roundtripTestSystemGroovyFileScript() throws Exception {
        SystemGroovy before = new SystemGroovy(new FileScriptSource("test.groovy"),"TEST=45","test.jar");
        SystemGroovy after = doRoundtrip(before, SystemGroovy.class);

        j.assertEqualBeans(before, after, "bindings,classpath");
        j.assertEqualBeans(before.getScriptSource(), after.getScriptSource(), "scriptFile");
    }

    @Test
    public void roundtripTestGroovyFileScript() throws Exception {
        Groovy before = new Groovy(new FileScriptSource("test.groovy"),"(Default)", "-Xmx1024m", "TEST=45","some.property=true", "-Xmx1024m", "test.jar");
        Groovy after = doRoundtrip(before, Groovy.class);

        j.assertEqualBeans(before, after, "groovyName,parameters,scriptParameters,properties,javaOpts,classPath");
        j.assertEqualBeans(before.getScriptSource(), after.getScriptSource(), "scriptFile");
    }

    @Test
    public void roundtripTestGroovyStringScript() throws Exception {
        Groovy before = new Groovy(new StringScriptSource("println 'Test'"),"(Default)", "-Xmx1024m", "TEST=45","some.property=true", "-Xmx1024m", "test.jar");
        Groovy after = doRoundtrip(before, Groovy.class);

        j.assertEqualBeans(before, after, "groovyName,parameters,scriptParameters,properties,javaOpts,classPath");
        j.assertEqualBeans(before.getScriptSource(), after.getScriptSource(), "command");
    }
    
    @Test
    public void roundtripTestGroovyParams() throws Exception {
        Groovy before = new Groovy(new StringScriptSource("println 'Test'"),"(Default)", "some 'param with spaces' and other params", "some other 'param with spaces' and other params", "some.property=true", "-Xmx1024m", "test.jar");
        Groovy after = doRoundtrip(before, Groovy.class);
        j.assertEqualBeans(before, after, "parameters,scriptParameters");
    }
    
    @Test
    public void roundtripTestGroovyParamsSlashes() throws Exception {
        Groovy before = new Groovy(new StringScriptSource("println 'Test'"),"(Default)", "some 'param with spaces' and http://slashes/and c:\\backslashes", "some other 'param with spaces' and http://slashes/and c:\\backslashes", "some.property=true", "-Xmx1024m", "test.jar");
        Groovy after = doRoundtrip(before, Groovy.class);
        j.assertEqualBeans(before, after, "parameters,scriptParameters");
    }

    private <T extends Builder> T doRoundtrip(T before, Class<T> clazz) throws Exception {
        FreeStyleProject p = j.createFreeStyleProject();
        p.getBuildersList().add(before);

        j.submit(j.createWebClient().getPage(p,"configure").getFormByName("config"));
        T after = p.getBuildersList().get(clazz);
        return after;
    }

}

@@ -68,16 +68,89 @@
 rtTrue;%0A
+import static org.junit.Assert.assertEquals;%0Aimport hudson.model.Result;%0A
 import h
@@ -958,32 +958,480 @@
 wMacro());%0A    %7D
+%0A    %0A    //JENKINS-25392%0A    @Test%0A    public void testFailWhenScriptThrowsException() throws Exception %7B%0A        ScriptSource script = new StringScriptSource(%22throw new Exception(%5C%22test%5C%22)%22);%0A        Groovy g = new Groovy(script,%22(Default)%22, %22%22, %22%22,%22%22, %22%22, %22%22);%0A        FreeStyleProject p = j.createFreeStyleProject();%0A        p.getBuildersList().add(g);%0A        assertEquals(Result.FAILURE, p.scheduleBuild2(0).get().getResult());%0A        %0A    %7D
 %0A%0A    @Test%0A    

Add test which verifies that build fails when exception is thrown from script