package hudson.plugins.git;

import hudson.EnvVars;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.slaves.DumbSlave;
import hudson.tools.ToolDescriptor;
import hudson.util.StreamTaskListener;
import java.io.IOException;
import java.util.List;
import org.apache.commons.lang.SystemUtils;
import org.jenkinsci.plugins.gitclient.JGitApacheTool;
import org.jenkinsci.plugins.gitclient.JGitTool;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class GitToolTest {

    @ClassRule public static JenkinsRule j = new JenkinsRule();

    private GitTool gitTool;

    @Before
    public void setUp() throws IOException {
        GitTool.onLoaded();
        gitTool = GitTool.getDefaultInstallation();
    }

    @Test
    public void testGetGitExe() {
        assertEquals(SystemUtils.IS_OS_WINDOWS ? "git.exe" : "git", gitTool.getGitExe());
    }

    @Test
    public void testForNode() throws Exception {
        DumbSlave slave = j.createSlave();
        slave.setMode(Node.Mode.EXCLUSIVE);
        TaskListener log = StreamTaskListener.fromStdout();
        GitTool newTool = gitTool.forNode(slave, log);
        assertEquals(gitTool.getGitExe(), newTool.getGitExe());
    }

    @Test
    public void testForEnvironment() {
        EnvVars environment = new EnvVars();
        GitTool newTool = gitTool.forEnvironment(environment);
        assertEquals(gitTool.getGitExe(), newTool.getGitExe());
    }

    @Test
    public void testGetDescriptor() {
        GitTool.DescriptorImpl descriptor = gitTool.getDescriptor();
        assertEquals("Git", descriptor.getDisplayName());
    }

    @Test
    public void testGetInstallationFromDescriptor() {
        GitTool.DescriptorImpl descriptor = gitTool.getDescriptor();
        assertEquals(null, descriptor.getInstallation(""));
        assertEquals(null, descriptor.getInstallation("not-a-valid-git-install"));
    }

    @Test
    public void testGetApplicableFromDescriptor() {
        GitTool.DescriptorImpl gitDescriptor = gitTool.getDescriptor();
        GitTool.DescriptorImpl jgitDescriptor = (new JGitTool()).getDescriptor();
        GitTool.DescriptorImpl jgitApacheDescriptor = (new JGitApacheTool()).getDescriptor();
        List<ToolDescriptor<? extends GitTool>> toolDescriptors = gitDescriptor.getApplicableDescriptors();
        assertTrue("git tool descriptor not found in " + toolDescriptors, toolDescriptors.contains(gitDescriptor));
        assertTrue("jgit tool descriptor not found in " + toolDescriptors, toolDescriptors.contains(jgitDescriptor));
        assertTrue("jgitapache tool descriptor not found in " + toolDescriptors, toolDescriptors.contains(jgitApacheDescriptor));
        assertEquals("Wrong tool descriptor count in " + toolDescriptors, 3, toolDescriptors.size());
    }

}

@@ -137,16 +137,97 @@
 bSlave;%0A
+import hudson.tools.CommandInstaller;%0Aimport hudson.tools.InstallSourceProperty;%0A
 import h
@@ -254,16 +254,16 @@
 riptor;%0A
-
 import h
@@ -321,16 +321,46 @@
 eption;%0A
+import java.util.Collections;%0A
 import j
@@ -370,24 +370,25 @@
 .util.List;%0A
+%0A
 import org.a
@@ -556,16 +556,42 @@
 ssert.*;
+%0A%0Aimport org.junit.Assume;
 %0Aimport 
@@ -1371,16 +1371,16 @@
 , log);%0A
-
         
@@ -1434,32 +1434,800 @@
 tExe());%0A    %7D%0A%0A
+    @Test%0A    public void shouldResolveToolsOnMaster() throws Exception %7B%0A        Assume.assumeTrue(!hudson.remoting.Launcher.isWindows());%0A        TaskListener log = StreamTaskListener.fromStdout();%0A        GitTool t = new GitTool(%22myGit%22, null, Collections.singletonList(%0A                new InstallSourceProperty(Collections.singletonList(%0A                        new CommandInstaller(%22master%22, %22echo Hello%22, %22TOOL_HOME%22)%0A                ))));%0A        t.getDescriptor().setInstallations(t);%0A%0A        GitTool defaultTool = GitTool.getDefaultInstallation();%0A        GitTool resolved = (GitTool) defaultTool.translate(j.jenkins, new EnvVars(), TaskListener.NULL);%0A        assertThat(resolved.getGitExe(), org.hamcrest.CoreMatchers.containsString(%22TOOL_HOME%22));%0A    %7D%0A%0A
     @Test%0A    pu

Add test that verifies invocation of ToolInstallers on master