view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MergeSupportTest.java @ 953:ef995b1ed5ec

update dsl attributes
author Dmitry Neverov <dmitry.neverov@gmail.com>
date Wed, 22 Jun 2016 21:33:59 +0200
parents 31a1aca3305c
children 7bf4d943d5bb
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.vcs.*;
import org.jetbrains.annotations.NotNull;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.MercurialSupportBuilder.mercurialSupport;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.ModificationDataMatcher.modificationData;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.Util.copyRepository;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.VcsRootBuilder.vcsRoot;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;


@RequiredHgVersion(min = "1.7.0")
@Test(dataProviderClass = HgVersionConstraint.class, dataProvider = "installedHgVersion")
public class MergeSupportTest extends BaseMercurialTestCase {

  private File myRemoteRepo;
  private MercurialVcsSupport myVcs;
  private MercurialMergeSupport myMergeSupport;

  @Override
  @BeforeMethod
  public void setUp() throws Exception {
    super.setUp();
    ServerPluginConfig pluginConfig = new ServerPluginConfigBuilder()
            .cachesDir(myTempFiles.createTempDir())
            .build();
    MercurialSupportBuilder mercurialBuilder = mercurialSupport().withConfig(pluginConfig);
    myVcs = mercurialBuilder.build();
    myMergeSupport = new MercurialMergeSupport(myVcs, myVcs.getMirrorManager(), pluginConfig, mercurialBuilder.getHgRootFactory(), mercurialBuilder.getHgRepoFactory());
    myRemoteRepo = myTempFiles.createTempDir();
    copyRepository(new File("mercurial-tests/testData/merge/no.subrepos"), myRemoteRepo);
  }


  public void should_return_succesful_result_when_merge_succeeds(@NotNull HgVersion _) throws Exception {
    VcsRoot root = vcsRoot().withUrl(myRemoteRepo).build();
    MergeResult mergeResult = myMergeSupport.merge(root, "2742914d19b2", "default", "merge topic1 into default", new MergeOptions());
    assertTrue(mergeResult.isSuccess());
  }


  public void result_of_succesful_merge_should_appear_in_remote_repository(@NotNull HgVersion _) throws Exception {
    final String mergeDestinationBranch = "default";
    final String mergeCommitMessage = "merge topic1 into default";

    VcsRoot root = vcsRoot().withUrl(myRemoteRepo).build();
    RepositoryStateData stateBeforeMerge = myVcs.getCollectChangesPolicy().getCurrentState(root);
    String dstBranchRevBefore = stateBeforeMerge.getBranchRevisions().get(mergeDestinationBranch);

    myMergeSupport.merge(root, "2742914d19b2", mergeDestinationBranch, mergeCommitMessage, new MergeOptions());
    RepositoryStateData stateAfterMerge = myVcs.getCollectChangesPolicy().getCurrentState(root);
    String dstBranchRevAfter = stateAfterMerge.getBranchRevisions().get(mergeDestinationBranch);

    assertFalse("Revision of the destination branch is not changed", dstBranchRevAfter.equals(dstBranchRevBefore));

    List<ModificationData> changes = myVcs.getCollectChangesPolicy().collectChanges(root, stateBeforeMerge, stateAfterMerge, CheckoutRules.DEFAULT);
    assertThat(changes, hasItem(modificationData().withDescription(mergeCommitMessage)));
  }


  public void should_return_unsuccesful_result_when_merge_fails(@NotNull HgVersion _) throws Exception {
    VcsRoot root = vcsRoot().withUrl(myRemoteRepo).build();
    MergeResult mergeResult = myMergeSupport.merge(root, "79d836707416", "default", "merge topic2 into default", new MergeOptions());
    assertFalse(mergeResult.isSuccess());
    assertThat(mergeResult.getConflicts(), hasItem("b/c"));
  }


  public void result_of_failed_merge_should_not_appear_in_remote_repository(@NotNull HgVersion _) throws Exception {
    final String mergeDestinationBranch = "default";

    VcsRoot root = vcsRoot().withUrl(myRemoteRepo).build();
    RepositoryStateData stateBeforeMerge = myVcs.getCollectChangesPolicy().getCurrentState(root);
    String dstBranchRevBefore = stateBeforeMerge.getBranchRevisions().get(mergeDestinationBranch);

    myMergeSupport.merge(root, "79d836707416", mergeDestinationBranch, "merge topic2 into default", new MergeOptions());
    RepositoryStateData stateAfterMerge = myVcs.getCollectChangesPolicy().getCurrentState(root);
    String dstBranchRevAfter = stateAfterMerge.getBranchRevisions().get(mergeDestinationBranch);

    assertTrue("Revision of the destination branch changed after failed merge", dstBranchRevAfter.equals(dstBranchRevBefore));
  }


  public void try_merge_returns_correct_result_for_every_task(@NotNull HgVersion _) throws Exception {
    VcsRoot root = vcsRoot().withUrl(myRemoteRepo).build();

    final MergeTask topic1ToDefault = new MergeTask("2742914d19b2", "09dd527b77ec");
    final MergeTask topic2ToDefault = new MergeTask("79d836707416", "09dd527b77ec");
    final MergeTask topic1ToTopic2 = new MergeTask("2742914d19b2", "79d836707416");
    Map<MergeTask, MergeResult> results = myMergeSupport.tryMerge(root, asList(topic1ToDefault, topic2ToDefault, topic1ToTopic2), new MergeOptions());

    assertTrue(results.get(topic1ToDefault).isSuccess());
    assertFalse(results.get(topic2ToDefault).isSuccess());
    assertThat(results.get(topic2ToDefault).getConflicts(), hasItem("b/c"));
    assertTrue(results.get(topic1ToTopic2).isSuccess());
  }


  public void merge_into_same_branch(@NotNull HgVersion _) throws Exception {
    VcsRoot root = vcsRoot().withUrl(myRemoteRepo).build();
    MergeResult result = myMergeSupport.merge(root, "6370ce18689a", "default", "merge into same branch", new MergeOptions());
    assertTrue(result.isSuccess());
  }
}