view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupportTest.java @ 1027:10dc26b32c35

Update code according to new Java
author nikolai.kulakov@DESKTOP-Q4QCGIH
date Wed, 05 Aug 2020 13:19:53 +0300
parents 7bf4d943d5bb
children
line wrap: on
line source
/*
 * Copyright 2000-2018 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.AuthSettings;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.TestCommandSettingsFactory;
import jetbrains.buildServer.util.TestFor;
import jetbrains.buildServer.vcs.*;
import org.jetbrains.annotations.NotNull;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static jetbrains.buildServer.buildTriggers.vcs.mercurial.MercurialSupportBuilder.mercurialSupport;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.Util.getHgPath;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.VcsChangeMatcher.vcsChange;
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.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;

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

  private ServerPluginConfig myPluginConfig;
  private MercurialVcsSupport myVcs;
  private MercurialCommitSupport myCommitSupport;
  private File myRepo;

  @Override
  @BeforeMethod
  public void setUp() throws Exception {
    super.setUp();

    myRepo = createRepo("mercurial-tests/testData/commit");
    myPluginConfig = new ServerPluginConfigBuilder()
            .cachesDir(myTempFiles.createTempDir())
            .build();
    MercurialSupportBuilder mercurialBuilder = mercurialSupport().withConfig(myPluginConfig);
    myVcs = mercurialBuilder.build();
    myCommitSupport = new MercurialCommitSupport(myVcs, myVcs.getMirrorManager(), myPluginConfig,
            mercurialBuilder.getHgRootFactory(), mercurialBuilder.getHgRepoFactory());
  }


  public void test_commit(@NotNull HgVersion version) throws Exception {
    VcsRoot root = vcsRoot().withUrl(myRepo).build();
    RepositoryStateData beforeCommit = myVcs.getCollectChangesPolicy().getCurrentState(root);

    String description = "Test commit support";
    String author = "Joe Doe <joe@some.org>";
    CommitPatchBuilder patchBuilder = myCommitSupport.getCommitPatchBuilder(root);
    patchBuilder.deleteDirectory("dir");
    patchBuilder.deleteFile("x");
    patchBuilder.createFile("a/b/c", stream("test"));
    patchBuilder.renameFile("y", "z", stream("rename"));
    CommitResult result = patchBuilder.commit(new CommitSettingsImpl(author, description));
    patchBuilder.dispose();

    RepositoryStateData afterCommit = myVcs.getCollectChangesPolicy().getCurrentState(root);
    assertEquals(result.getCreatedRevision(), afterCommit.getBranchRevisions().get(afterCommit.getDefaultBranchName()));

    List<ModificationData> changes = myVcs.getCollectChangesPolicy().collectChanges(root, beforeCommit, afterCommit, CheckoutRules.DEFAULT);
    ModificationData m = changes.get(0);
    assertEquals(description, m.getDescription());
    assertEquals(author, m.getUserName());

    assertThat(m.getChanges(), hasItem(vcsChange().file("x").type(VcsChangeInfo.Type.REMOVED)));
    assertThat(m.getChanges(), hasItem(vcsChange().file("dir/x").type(VcsChangeInfo.Type.REMOVED)));
    assertThat(m.getChanges(), hasItem(vcsChange().file("a/b/c").type(VcsChangeInfo.Type.ADDED)));
    assertThat(m.getChanges(), hasItem(vcsChange().file("y").type(VcsChangeInfo.Type.REMOVED)));
    assertThat(m.getChanges(), hasItem(vcsChange().file("z").type(VcsChangeInfo.Type.ADDED)));
  }


  @TestFor(issues = "TW-39095")
  public void commit_with_non_ascii_commit_message(@NotNull HgVersion version) throws Exception {
    for (String msg : new String[] {"Комит месадж", "Über Alles"}) {
      commit(msg);
    }
  }


  @TestFor(issues = "TW-39321")
  public void should_throw_meaningful_error_if_destination_branch_doesnt_exist(@NotNull HgVersion version) throws Exception {
    String nonExistingBranch = "nonExisting";
    VcsRoot root = vcsRoot().withUrl(myRepo).withBranch(nonExistingBranch).build();
    String description = "msg";
    String author = "Joe Doe <joe@some.org>";
    try {
      CommitPatchBuilder patchBuilder = myCommitSupport.getCommitPatchBuilder(root);
      patchBuilder.createFile("a/b/c", stream("test"));
      patchBuilder.commit(new CommitSettingsImpl(author, description));
      patchBuilder.dispose();
    } catch (VcsException e) {
      assertEquals("The '" + nonExistingBranch + "' destination branch doesn't exist", e.getMessage());
    }
  }


  @TestFor(issues = "TW-39321")
  public void should_create_branch_if_repository_has_no_branches(@NotNull HgVersion version) throws Exception {
    File emptyRemoteRepo = myTempFiles.createTempDir();
    ServerHgRepo repo = new ServerHgRepo(new TestCommandSettingsFactory(), myPluginConfig, emptyRemoteRepo, getHgPath(), new AuthSettings());
    repo.init().call();

    String nonExistingBranch = "nonExisting";
    VcsRoot root = vcsRoot().withUrl(emptyRemoteRepo).withBranch(nonExistingBranch).build();
    String description = "msg";
    String author = "Joe Doe <joe@some.org>";
    CommitPatchBuilder patchBuilder = myCommitSupport.getCommitPatchBuilder(root);
    patchBuilder.createFile("a/b/c", stream("test"));
    patchBuilder.commit(new CommitSettingsImpl(author, description));
    patchBuilder.dispose();

    RepositoryStateData state = myVcs.getCollectChangesPolicy().getCurrentState(root);
    assertNotNull(state.getBranchRevisions().get(nonExistingBranch));
  }


  @NotNull
  private InputStream stream(@NotNull String content) {
    return new ByteArrayInputStream(content.getBytes());
  }

  private static class CommitSettingsImpl implements CommitSettings {
    private final String myUserName;
    private final String myDescription;

    public CommitSettingsImpl(@NotNull String userName, @NotNull String description) {
      myUserName = userName;
      myDescription = description;
    }

    @NotNull
    public String getUserName() {
      return myUserName;
    }

    @NotNull
    public String getDescription() {
      return myDescription;
    }
  }

  private void commit(String description) throws IOException, VcsException {
    VcsRoot root = vcsRoot().withUrl(myRepo).build();
    String author = "Joe Doe <joe@some.org>";
    CommitPatchBuilder patchBuilder = myCommitSupport.getCommitPatchBuilder(root);
    patchBuilder.createFile("a/b/c", stream("test"));
    patchBuilder.commit(new CommitSettingsImpl(author, description));
    patchBuilder.dispose();
  }
}