package hudson.plugins.git;

import hudson.EnvVars;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.slaves.DumbSlave;
import hudson.util.StreamTaskListener;
import java.io.IOException;
import org.apache.commons.lang.SystemUtils;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class GitToolTest {

    @Rule public 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"));
    }

}

@@ -137,16 +137,52 @@
 bSlave;%0A
+import hudson.tools.ToolDescriptor;%0A
 import h
@@ -240,16 +240,39 @@
 eption;%0A
+import java.util.List;%0A
 import o
@@ -307,16 +307,65 @@
 mUtils;%0A
+import org.jenkinsci.plugins.gitclient.JGitTool;%0A
 import s
@@ -1925,10 +1925,678 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testGetApplicableFromDescriptor() %7B%0A        GitTool.DescriptorImpl gitDescriptor = gitTool.getDescriptor();%0A        GitTool.DescriptorImpl jgitDescriptor = (new JGitTool()).getDescriptor();%0A        List%3CToolDescriptor%3C? extends GitTool%3E%3E toolDescriptors = gitDescriptor.getApplicableDesccriptors();%0A        assertTrue(%22git tool descriptor not found in %22 + toolDescriptors, toolDescriptors.contains(gitDescriptor));%0A        assertTrue(%22jgit tool descriptor not found in %22 + toolDescriptors, toolDescriptors.contains(jgitDescriptor));%0A        assertEquals(%22Wrong tool descriptor count in %22 + toolDescriptors, 2, toolDescriptors.size());%0A    %7D%0A%0A
 %7D%0A

Add GitTool.getApplicableDesccriptors test