package hudson.plugins.groovy;

import hudson.model.FreeStyleProject;

import java.io.File;

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

public class ClassPathTest {
    
    @Rule
    public JenkinsRule j = new JenkinsRule();
    
    /**
     * Tests that groovy build step accepts wild cards on class path
     */
    @Issue("JENKINS-26070")
    @Test
    public void testWildcartOnClassPath() throws Exception {
        final String testJar = "groovy-cp-test.jar";
        final ScriptSource script = new StringScriptSource(
                "def printCP(classLoader){\n "
                + "  classLoader.getURLs().each {println \"$it\"}\n"
                + "  if(classLoader.parent) {printCP(classLoader.parent)}\n"
                + "}\n"
                + "printCP(this.class.classLoader)");
        Groovy g = new Groovy(script,"(Default)", "", "","", "", this.getClass().getResource("/lib").getPath() + "/*");
        FreeStyleProject p = j.createFreeStyleProject();
        p.getBuildersList().add(g);
        j.assertLogContains(testJar, j.buildAndAssertSuccess(p));
    }
    
    @Issue("JENKINS-29577")
    @Test
    public void testClassPathAndProperties() throws Exception {
        final String testJar = "groovy-cp-test.jar";
        StringBuilder sb = new StringBuilder();
        final ScriptSource script = new StringScriptSource(
                sb.append("import App\n")
                .append("println System.getProperty(\"aaa\")")
                .toString()
                );
        Groovy g = new Groovy(script,"(Default)", "", "","aaa=\"bbb\"", "", this.getClass().getResource("/lib").getPath() + File.separator + testJar);
        FreeStyleProject p = j.createFreeStyleProject();
        p.getBuildersList().add(g);
        j.assertLogContains("bbb", j.buildAndAssertSuccess(p));
    }
    
}

@@ -25,16 +25,40 @@
 roovy;%0A%0A
+import hudson.FilePath;%0A
 import h
@@ -87,17 +87,16 @@
 roject;%0A
-%0A
 import j
@@ -108,16 +108,40 @@
 o.File;%0A
+import org.junit.Before;
 %0Aimport 
@@ -347,16 +347,404 @@
 sRule();
+%0A%0A    @Before%0A    public void setUpGroovy() throws Exception %7B%0A        FilePath home = j.jenkins.getRootPath();%0A        home.unzipFrom(ClassPathTest.class.getResourceAsStream(%22/groovy-binary-2.4.13.zip%22));%0A        j.jenkins.getDescriptorByType(GroovyInstallation.DescriptorImpl.class).setInstallations(new GroovyInstallation(%222.4.x%22, home.child(%22groovy-2.4.13%22).getRemote(), null));%0A    %7D
 %0A    %0A  
@@ -1342,34 +1342,31 @@
 oovy(script,
-%22(Default)
+ %222.4.x
 %22, %22%22, %22%22,%22%22
@@ -1363,16 +1363,17 @@
  %22%22, %22%22,
+ 
 %22%22, %22%22, 
@@ -2053,18 +2053,15 @@
 ipt,
-%22(Default)
+ %222.4.x
 %22, %22
@@ -2066,16 +2066,17 @@
  %22%22, %22%22,
+ 
 %22aaa=%5C%22b

Allow ClassPathTest to run even on a CI system without Groovy in the default path.