view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTestCase.java @ 178:d314f06e6ee3 remote-run/dmitry.neverov/agent_caches3

Agent caches. Settings contains only root settings and nothing more. Commands use settings and a directory where they should be run.
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 25 Feb 2011 18:48:44 +0300
parents 5198b02fc5e9
children 643fa1236f4e
line wrap: on
line source
/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;

import jetbrains.buildServer.BaseTestCase;
import jetbrains.buildServer.TempFiles;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.Constants;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.LocalRepositoryUtil;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.MirrorManager;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.Util;
import jetbrains.buildServer.vcs.VcsException;
import jetbrains.buildServer.vcs.VcsRoot;
import jetbrains.buildServer.vcs.impl.VcsRootImpl;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BaseCommandTestCase extends BaseTestCase {
  private String myRepository;
  private String myUsername;
  private String myPassword;
  private boolean myCloneRequired;

  public BaseCommandTestCase() {
  }

  protected void setRepository(final String repository, boolean cloneRequired) {
    myRepository = repository;
    myCloneRequired = cloneRequired;
  }

  protected void setUsername(final String username) {
    myUsername = username;
  }

  protected void setPassword(final String password) {
    myPassword = password;
  }

  protected <T> T runCommand(CommandExecutor<T> executor) throws IOException, VcsException {
    Map<String, String> vcsRootProps = new HashMap<String, String>();

    vcsRootProps.put(Constants.REPOSITORY_PROP, myRepository);

    if (myCloneRequired) {
      File repository = LocalRepositoryUtil.prepareRepository(new File(myRepository).getAbsolutePath());
      vcsRootProps.put(Constants.REPOSITORY_PROP, repository.getAbsolutePath());
    }

    vcsRootProps.put(Constants.HG_COMMAND_PATH_PROP, Util.getHgPath());
    if (myUsername != null) {
      vcsRootProps.put(Constants.USERNAME, myUsername);
    }
    if (myPassword != null) {
      vcsRootProps.put(Constants.PASSWORD, myPassword);
    }

    TempFiles tf = new TempFiles();
    File parentDir = tf.createTempDir();

    MirrorManager mirrorManager = new MirrorManager(parentDir);
    VcsRoot vcsRoot = new VcsRootImpl(1, vcsRootProps);
    Settings settings = new Settings(vcsRoot);
    final File workingDir = mirrorManager.getMirrorDir(settings.getRepositoryUrl());
    settings.setCustomWorkingDir(workingDir);
    try {
      if (myCloneRequired) {
        new CloneCommand(settings, workingDir).execute();
      }

      return executor.execute(settings, workingDir);
    } finally {
      tf.cleanup();
    }
  }

  public interface CommandExecutor<T> {
    T execute(@NotNull Settings settings, File workingDir) throws VcsException;
  }
}