// Path: src/main/java/uk/co/pols/bamboo/gitplugin/client/git/commands/CommandExecutor.java
// public interface CommandExecutor {
//     String execute(String[] commandLine, File sourceCodeDirectory) throws IOException;
// }
// 
// Path: src/main/java/uk/co/pols/bamboo/gitplugin/client/git/commands/ExecutorGitPullCommand.java
// public class ExecutorGitPullCommand implements GitPullCommand {
//     private static final Log log = LogFactory.getLog(ExecutorGitPullCommand.class);
// 
//     private String gitExe;
//     private File sourceCodeDirectory;
//     private CommandExecutor commandExecutor;
// 
//     public ExecutorGitPullCommand(String gitExe, File sourceCodeDirectory, CommandExecutor commandExecutor) {
//         this.gitExe = gitExe;
//         this.sourceCodeDirectory = sourceCodeDirectory;
//         this.commandExecutor = commandExecutor;
//     }
// 
//     public void pullUpdatesFromRemoteRepository(BuildLogger buildLogger, String repositoryUrl, String branch) throws IOException {
//         log.info(buildLogger.addBuildLogEntry("Pulling source from branch '" + branch + "' @ '" + repositoryUrl + "' into '" + sourceCodeDirectory.getAbsolutePath() + "'."));
// 
//         String pullOutput = commandExecutor.execute(new String[]{gitExe, "pull", repositoryUrl, branch + ":" + branch}, sourceCodeDirectory);
//         checkCommandOutput(buildLogger, repositoryUrl, pullOutput);
// 
//         // git submodule commands will silently do nothing if no submodules are configured
//         String submoduleInitOutput = commandExecutor.execute(new String[]{gitExe, "submodule", "init"}, sourceCodeDirectory);
//         checkCommandOutput(buildLogger, repositoryUrl, submoduleInitOutput);
// 
//         String submoduleUpdateOutput = commandExecutor.execute(new String[]{gitExe, "submodule", "update"}, sourceCodeDirectory);
//         checkCommandOutput(buildLogger, repositoryUrl, submoduleUpdateOutput);
//     }
// 
//     private void checkCommandOutput(BuildLogger buildLogger, String repositoryUrl, String output) throws IOException {
//         if (output.contains("fatal:")) {
//             throw new IOException("Could not pull from '" + repositoryUrl + "'. git-pull: " + output);
//         } else {
//             log.info(buildLogger.addBuildLogEntry(output));
//         }
//     }
// }


import org.jmock.integration.junit3.MockObjectTestCase;
import org.jmock.Expectations;
import java.io.File;
import java.io.IOException;
import com.atlassian.bamboo.build.logger.BuildLogger;
import uk.co.pols.bamboo.gitplugin.client.git.commands.CommandExecutor;
import uk.co.pols.bamboo.gitplugin.client.git.commands.ExecutorGitPullCommand;
            assertEquals("Could not pull from 'gitRepositoryUrl'. git-pull: fatal: something bad happened", e.getMessage());
        }
    }

    public void testThrowsAnIOExceptionIfGitSubmoduleInitReturnsAnError() throws IOException {
        checking(new Expectations() {
            {
                one(buildLogger).addBuildLogEntry("Pulling source from branch 'some-branch' @ 'gitRepositoryUrl' into '" + SOURCE_CODE_DIRECTORY.getAbsolutePath() + "'.");
                one(commandExecutor).execute(new String[]{GIT_EXE, "pull", "gitRepositoryUrl", "some-branch:some-branch"}, SOURCE_CODE_DIRECTORY);
                will(returnValue("PULL COMMAND OUTPUT"));
                one(buildLogger).addBuildLogEntry("PULL COMMAND OUTPUT");
                one(commandExecutor).execute(new String[]{GIT_EXE, "submodule", "init"}, SOURCE_CODE_DIRECTORY);
                will(returnValue("fatal: could not submodule init"));
            }
        });

        ExecutorGitPullCommand gitPullCommand = new ExecutorGitPullCommand(GIT_EXE, SOURCE_CODE_DIRECTORY, commandExecutor);
        try {
            gitPullCommand.pullUpdatesFromRemoteRepository(buildLogger, "gitRepositoryUrl", "some-branch");
            fail("Should throw an IOException");
        } catch (IOException e) {
            assertEquals("Could not pull from 'gitRepositoryUrl'. git-pull: fatal: could not submodule init", e.getMessage());
        }
    }

    public void testThrowsAnIOExceptionIfGitSubmoduleUpdateReturnsAnError() throws IOException {
        checking(new Expectations() {
            {
                one(buildLogger).addBuildLogEntry("Pulling source from branch 'some-branch' @ 'gitRepositoryUrl' into '" + SOURCE_CODE_DIRECTORY.getAbsolutePath() + "'.");
                one(commandExecutor).execute(new String[]{GIT_EXE, "pull", "gitRepositoryUrl", "some-branch:some-branch"}, SOURCE_CODE_DIRECTORY);
// Path: src/main/java/uk/co/pols/bamboo/gitplugin/client/git/commands/CommandExecutor.java
// public interface CommandExecutor {
//     String execute(String[] commandLine, File sourceCodeDirectory) throws IOException;
// }
// 
// Path: src/main/java/uk/co/pols/bamboo/gitplugin/client/git/commands/ExecutorGitPullCommand.java
// public class ExecutorGitPullCommand implements GitPullCommand {
//     private static final Log log = LogFactory.getLog(ExecutorGitPullCommand.class);
// 
//     private String gitExe;
//     private File sourceCodeDirectory;
//     private CommandExecutor commandExecutor;
// 
//     public ExecutorGitPullCommand(String gitExe, File sourceCodeDirectory, CommandExecutor commandExecutor) {
//         this.gitExe = gitExe;
//         this.sourceCodeDirectory = sourceCodeDirectory;
//         this.commandExecutor = commandExecutor;
//     }
// 
//     public void pullUpdatesFromRemoteRepository(BuildLogger buildLogger, String repositoryUrl, String branch) throws IOException {
//         log.info(buildLogger.addBuildLogEntry("Pulling source from branch '" + branch + "' @ '" + repositoryUrl + "' into '" + sourceCodeDirectory.getAbsolutePath() + "'."));
// 
//         String pullOutput = commandExecutor.execute(new String[]{gitExe, "pull", repositoryUrl, branch + ":" + branch}, sourceCodeDirectory);
//         checkCommandOutput(buildLogger, repositoryUrl, pullOutput);
// 
//         // git submodule commands will silently do nothing if no submodules are configured
//         String submoduleInitOutput = commandExecutor.execute(new String[]{gitExe, "submodule", "init"}, sourceCodeDirectory);
//         checkCommandOutput(buildLogger, repositoryUrl, submoduleInitOutput);
// 
//         String submoduleUpdateOutput = commandExecutor.execute(new String[]{gitExe, "submodule", "update"}, sourceCodeDirectory);
//         checkCommandOutput(buildLogger, repositoryUrl, submoduleUpdateOutput);
//     }
// 
//     private void checkCommandOutput(BuildLogger buildLogger, String repositoryUrl, String output) throws IOException {
//         if (output.contains("fatal:")) {
//             throw new IOException("Could not pull from '" + repositoryUrl + "'. git-pull: " + output);
//         } else {
//             log.info(buildLogger.addBuildLogEntry(output));
//         }
//     }
// }


// Path: src/test/java/uk/co/pols/bamboo/gitplugin/client/git/commands/ExecutorGitPullCommandTest.java
import org.jmock.integration.junit3.MockObjectTestCase;
import org.jmock.Expectations;
import java.io.File;
import java.io.IOException;
import com.atlassian.bamboo.build.logger.BuildLogger;
import uk.co.pols.bamboo.gitplugin.client.git.commands.CommandExecutor;
import uk.co.pols.bamboo.gitplugin.client.git.commands.ExecutorGitPullCommand;

            assertEquals("Could not pull from 'gitRepositoryUrl'. git-pull: fatal: something bad happened", e.getMessage());
        }
    }

    public void testThrowsAnIOExceptionIfGitSubmoduleInitReturnsAnError() throws IOException {
        checking(new Expectations() {
            {
                one(buildLogger).addBuildLogEntry("Pulling source from branch 'some-branch' @ 'gitRepositoryUrl' into '" + SOURCE_CODE_DIRECTORY.getAbsolutePath() + "'.");
                one(commandExecutor).execute(new String[]{GIT_EXE, "pull", "gitRepositoryUrl", "some-branch:some-branch"}, SOURCE_CODE_DIRECTORY);
                will(returnValue("PULL COMMAND OUTPUT"));
                one(buildLogger).addBuildLogEntry("PULL COMMAND OUTPUT");
                one(commandExecutor).execute(new String[]{GIT_EXE, "submodule", "init"}, SOURCE_CODE_DIRECTORY);
                will(returnValue("fatal: could not submodule init"));
            }
        });

        ExecutorGitPullCommand gitPullCommand = new ExecutorGitPullCommand(GIT_EXE, SOURCE_CODE_DIRECTORY, commandExecutor);
        try {
            gitPullCommand.pullUpdatesFromRemoteRepository(buildLogger, "gitRepositoryUrl", "some-branch");
            fail("Should throw an IOException");
        } catch (IOException e) {
            assertEquals("Could not pull from 'gitRepositoryUrl'. git-pull: fatal: could not submodule init", e.getMessage());
        }
    }

    public void testThrowsAnIOExceptionIfGitSubmoduleUpdateReturnsAnError() throws IOException {
        checking(new Expectations() {
            {
                one(buildLogger).addBuildLogEntry("Pulling source from branch 'some-branch' @ 'gitRepositoryUrl' into '" + SOURCE_CODE_DIRECTORY.getAbsolutePath() + "'.");
                one(commandExecutor).execute(new String[]{GIT_EXE, "pull", "gitRepositoryUrl", "some-branch:some-branch"}, SOURCE_CODE_DIRECTORY);
                will(returnValue("PULL COMMAND OUTPUT"));