package org.jenkinsci.plugins.gitclient;

import hudson.FilePath;
import hudson.model.Computer;
import hudson.model.StreamBuildListener;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
import hudson.slaves.DumbSlave;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.jvnet.hudson.test.HudsonTestCase;

import java.io.File;
import java.io.IOException;
import org.jenkinsci.remoting.RoleChecker;

/**
 * @author Kohsuke Kawaguchi
 */
public class RemotingTest extends HudsonTestCase {
    /**
     * Makes sure {@link GitClient} is remotable.
     */
    public void testRemotability() throws Exception {
        DumbSlave s = createSlave();

        File dir = createTmpDir();
        final GitClient jgit = new JGitAPIImpl(dir,StreamBuildListener.fromStdout());

        Computer c = s.toComputer();
        c.connect(false).get();
        VirtualChannel channel = c.getChannel();
        channel.call(new Work(jgit));
        channel.close();
    }

    private static class Work implements Callable<Void,IOException> {

        private static final long serialVersionUID = 1L;

        private final GitClient git;

        private static boolean cliGitDefaultsSet = false;

        private void setCliGitDefaults() throws Exception {
            if (!cliGitDefaultsSet) {
                CliGitCommand gitCmd = new CliGitCommand(null);
                gitCmd.setDefaults();
            }
            cliGitDefaultsSet = true;
        }

        private Work(GitClient git) throws Exception {
            setCliGitDefaults();
            this.git = git;
        }

        @Override
        public Void call() throws IOException {
            try {
                git.init();
                git.getWorkTree().child("foo").touch(0);
                git.add("foo");
                PersonIdent alice = new PersonIdent("alice", "alice@jenkins-ci.org");
                git.commit("committing changes", alice, alice);

                FilePath ws = git.withRepository(new RepositoryCallableImpl());
                assertEquals(ws,git.getWorkTree());

                return null;
            } catch (InterruptedException e) {
                throw new Error(e);
            }
        }

        @Override
        public void checkRoles(RoleChecker rc) throws SecurityException {
            throw new UnsupportedOperationException("unexpected call to checkRoles in private static Work class");
        }
    }

    private static class RepositoryCallableImpl implements RepositoryCallback<FilePath> {

        private static final long serialVersionUID = 1L;

        @Override
        public FilePath invoke(Repository repo, VirtualChannel channel) {
            assertNotNull(repo);
            return new FilePath(repo.getWorkTree());
        }
    }
}

@@ -35,16 +35,51 @@
 lient;%0A%0A
+import static org.junit.Assert.*;%0A%0A
 import h
@@ -366,42 +366,129 @@
 rg.j
-vnet.hudson.test.H
+unit.ClassRule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder;%0Aimport org.jvnet.h
 udson
-TestCas
+.test.JenkinsRul
 e;%0A
-%0A
 impo
@@ -643,32 +643,165 @@
 est 
-extends HudsonTestCase %7B
+%7B%0A%0A    @ClassRule%0A    public static JenkinsRule j = new JenkinsRule();%0A%0A    @ClassRule%0A    public static TemporaryFolder tempFolder = new TemporaryFolder();%0A
 %0A   
@@ -863,16 +863,26 @@
      */%0A
+    @Test%0A
     publ
@@ -949,16 +949,18 @@
 ave s = 
+j.
 createSl
@@ -979,49 +979,8 @@
     
-File dir = createTmpDir();%0A        final 
 GitC
@@ -1012,12 +1012,30 @@
 mpl(
-dir,
+tempFolder.getRoot(), 
 Stre
@@ -1309,16 +1309,17 @@
 le%3CVoid,
+ 
 IOExcept
@@ -2154,24 +2154,103 @@
 s-ci.org%22);%0A
+                git.setAuthor(alice);%0A                git.setCommitter(alice);%0A
             
@@ -2288,22 +2288,8 @@
 ges%22
-, alice, alice
 );%0A%0A
@@ -2400,16 +2400,17 @@
 uals(ws,
+ 
 git.getW

Refactor test to use JenkinsRule instead of deprecated HudsonTestCase