view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/VcsRootBuilder.java @ 916:40124b503933

Merge branch Hajipur-9.0.x
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 02 Jan 2015 12:37:50 +0100
parents 80ae3dc66685
children c0f3096cfff6 38adef4f1b8f
line wrap: on
line source
/*
 * Copyright 2000-2014 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;

import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.HgVcsRoot;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildServer.vcs.SVcsRoot;
import jetbrains.buildServer.vcs.impl.VcsRootImpl;
import org.jetbrains.annotations.NotNull;
import org.jmock.Expectations;
import org.jmock.Mockery;

import java.io.File;
import java.io.IOException;

/**
 * @author dmitry.neverov
 */
public class VcsRootBuilder {

  private String myExtensions;
  private String myRepository;
  private String myUsername;
  private String myPassword;
  private String myBranch;
  private long myRootId = 1L;
  private String myHgPath;
  private String myUserForTag;
  private boolean myUncompressed = true;
  private File myCloneRepositoryTo;
  private boolean myDetectSubrepoChanges = false;
  private boolean myTagsAsBranches = false;
  private boolean myIncludeSubreposInPatch = true;
  private boolean myUseArchiveForPatch = false;
  private HgVcsRoot.PurgePolicy myPurgePolicy;

  public static VcsRootBuilder vcsRoot() {
    return new VcsRootBuilder();
  }

  public VcsRootImpl build() throws IOException {
    VcsRootImpl vcsRoot = new VcsRootImpl(myRootId, Constants.VCS_NAME);
    vcsRoot.addProperty(Constants.REPOSITORY_PROP, myRepository);
    vcsRoot.addProperty(Constants.HG_COMMAND_PATH_PROP, myHgPath != null ? myHgPath : Util.getHgPath());
    vcsRoot.addProperty(Constants.USERNAME, myUsername);
    vcsRoot.addProperty(Constants.PASSWORD, myPassword);
    vcsRoot.addProperty(Constants.BRANCH_NAME_PROP, myBranch);
    vcsRoot.addProperty(Constants.USER_FOR_TAG, myUserForTag);
    vcsRoot.addProperty(Constants.UNCOMPRESSED_TRANSFER, String.valueOf(myUncompressed));
    vcsRoot.addProperty(Constants.DETECT_SUBREPO_CHANGES, String.valueOf(myDetectSubrepoChanges));
    vcsRoot.addProperty(Constants.INCLUDE_SUBREPOS_IN_PATCH, String.valueOf(myIncludeSubreposInPatch));
    vcsRoot.addProperty(Constants.USE_ARCHIVE_FOR_PATCH, String.valueOf(myUseArchiveForPatch));
    vcsRoot.addProperty(Constants.HG_EXTENSIONS, myExtensions);
    if (myCloneRepositoryTo != null)
      vcsRoot.addProperty(Constants.SERVER_CLONE_PATH_PROP, String.valueOf(myCloneRepositoryTo.getAbsolutePath()));
    vcsRoot.addProperty(Constants.USE_TAGS_AS_BRANCHES, String.valueOf(myTagsAsBranches));
    if (myPurgePolicy != null)
      vcsRoot.addProperty(Constants.PURGE_POLICY, myPurgePolicy.name());
    return vcsRoot;
  }


  public SVcsRoot build(Mockery context) {
    final SVcsRoot root = context.mock(SVcsRoot.class, "SVcsRoot" + myRootId);
    context.checking(new Expectations() {{
      allowing(root).describe(false); will(returnValue("toString"));
      allowing(root).getVcsName(); will(returnValue(Constants.VCS_NAME));
      allowing(root).getProperty(with(Constants.REPOSITORY_PROP)); will(returnValue(myRepository));
      allowing(root).getProperty(with(Constants.HG_COMMAND_PATH_PROP)); will(returnValue(myHgPath));
      allowing(root).getProperty(with(Constants.BRANCH_NAME_PROP)); will(returnValue(myBranch));
      allowing(root).getProperty(with(Constants.SERVER_CLONE_PATH_PROP)); will(returnValue(null));
      allowing(root).getProperty(with(Constants.USERNAME)); will(returnValue(myUsername));
      allowing(root).getProperty(with(Constants.PASSWORD)); will(returnValue(myPassword));
      allowing(root).getProperty(with(Constants.UNCOMPRESSED_TRANSFER)); will(returnValue(null));
      allowing(root).getProperty(with(Constants.USER_FOR_TAG)); will(returnValue(myUserForTag));
      allowing(root).getProperty(with(Constants.DETECT_SUBREPO_CHANGES)); will(returnValue(String.valueOf(myDetectSubrepoChanges)));
      allowing(root).getProperty(with(Constants.USE_TAGS_AS_BRANCHES)); will(returnValue(String.valueOf(myTagsAsBranches)));
      allowing(root).getProperty(with(Constants.HG_EXTENSIONS)); will(returnValue(myExtensions));
    }});
    if (myCloneRepositoryTo != null) {
      context.checking(new Expectations() {{
        allowing(root).getProperty(with(Constants.SERVER_CLONE_PATH_PROP)); will(returnValue(myCloneRepositoryTo.getAbsolutePath()));
      }});
    }
    return root;
  }


  public VcsRootBuilder withUrl(@NotNull String repository) {
    myRepository = repository;
    return this;
  }

  @NotNull
  public VcsRootBuilder withExtensions(@NotNull String... extensions) {
    myExtensions = StringUtil.join(extensions, "\n");
    return this;
  }


  public VcsRootBuilder withLocalRepository(@NotNull final File repo) {
    return withUrl(repo.getPath()).withCloneRepositoryTo(repo.getParentFile());
  }


  public VcsRootBuilder withUrl(@NotNull File repository) {
    myRepository = repository.getAbsolutePath();
    return this;
  }


  public VcsRootBuilder withUserName(@NotNull String username) {
    myUsername = username;
    return this;
  }


  public VcsRootBuilder withPassword(@NotNull String password) {
    myPassword = password;
    return this;
  }


  public VcsRootBuilder withBranch(@NotNull String branch) {
    myBranch = branch;
    return this;
  }


  public VcsRootBuilder withId(long rootId) {
    myRootId = rootId;
    return this;
  }


  public VcsRootBuilder withHgPath(String hgPath) {
    myHgPath = hgPath;
    return this;
  }


  public VcsRootBuilder withUserForTag(String username) {
    myUserForTag = username;
    return this;
  }


  public VcsRootBuilder withCloneRepositoryTo(File cloneTo) {
    myCloneRepositoryTo = cloneTo;
    return this;
  }


  public VcsRootBuilder withSubrepoChanges(boolean detectSubrepoChanges) {
    myDetectSubrepoChanges = detectSubrepoChanges;
    return this;
  }


  public VcsRootBuilder withSubreposInPatch(boolean includeSubreposInPatch) {
    myIncludeSubreposInPatch = includeSubreposInPatch;
    return this;
  }


  public VcsRootBuilder withArchiveForPatch(boolean useArchiveForPatch) {
    myUseArchiveForPatch = useArchiveForPatch;
    return this;
  }


  public VcsRootBuilder withTagsEnabled(boolean useTagsAsBranches) {
    myTagsAsBranches = useTagsAsBranches;
    return this;
  }


  public VcsRootBuilder withPurgePolicy(HgVcsRoot.PurgePolicy policy) {
    myPurgePolicy = policy;
    return this;
  }
}