Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RenameBranchCommandTest.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2012, GitHub Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.junit.Assert.assertArrayEquals;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertNull;
  49. import static org.junit.Assert.assertTrue;
  50. import org.eclipse.jgit.junit.RepositoryTestCase;
  51. import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
  52. import org.eclipse.jgit.lib.ConfigConstants;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.StoredConfig;
  55. import org.eclipse.jgit.revwalk.RevCommit;
  56. import org.junit.Before;
  57. import org.junit.Test;
  58. /**
  59. * Unit tests of {@link RenameBranchCommand}
  60. */
  61. public class RenameBranchCommandTest extends RepositoryTestCase {
  62. private static final String PATH = "file.txt";
  63. private RevCommit head;
  64. private Git git;
  65. @Before
  66. public void setUp() throws Exception {
  67. super.setUp();
  68. git = Git.wrap(db);
  69. writeTrashFile(PATH, "content");
  70. git.add().addFilepattern(PATH).call();
  71. head = git.commit().setMessage("add file").call();
  72. assertNotNull(head);
  73. }
  74. @Test
  75. public void renameBranchNoConfigValues() throws Exception {
  76. StoredConfig config = git.getRepository().getConfig();
  77. config.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
  78. Constants.MASTER);
  79. config.save();
  80. String branch = "b1";
  81. assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
  82. Constants.MASTER).isEmpty());
  83. assertNotNull(git.branchRename().setNewName(branch).call());
  84. config = git.getRepository().getConfig();
  85. assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
  86. Constants.MASTER).isEmpty());
  87. assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
  88. branch).isEmpty());
  89. }
  90. @Test
  91. public void renameBranchSingleConfigValue() throws Exception {
  92. StoredConfig config = git.getRepository().getConfig();
  93. config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  94. ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
  95. config.save();
  96. String branch = "b1";
  97. assertEquals(BranchRebaseMode.REBASE,
  98. config.getEnum(BranchRebaseMode.values(),
  99. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  100. ConfigConstants.CONFIG_KEY_REBASE,
  101. BranchRebaseMode.NONE));
  102. assertNull(config.getEnum(BranchRebaseMode.values(),
  103. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  104. ConfigConstants.CONFIG_KEY_REBASE, null));
  105. assertNotNull(git.branchRename().setNewName(branch).call());
  106. config = git.getRepository().getConfig();
  107. assertNull(config.getEnum(BranchRebaseMode.values(),
  108. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  109. ConfigConstants.CONFIG_KEY_REBASE, null));
  110. assertEquals(BranchRebaseMode.REBASE,
  111. config.getEnum(BranchRebaseMode.values(),
  112. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  113. ConfigConstants.CONFIG_KEY_REBASE,
  114. BranchRebaseMode.NONE));
  115. }
  116. @Test
  117. public void renameBranchExistingSection() throws Exception {
  118. String branch = "b1";
  119. StoredConfig config = git.getRepository().getConfig();
  120. config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  121. ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
  122. config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
  123. Constants.MASTER, "a", "a");
  124. config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a",
  125. "b");
  126. config.save();
  127. assertNotNull(git.branchRename().setNewName(branch).call());
  128. config = git.getRepository().getConfig();
  129. assertArrayEquals(new String[] { "b", "a" }, config.getStringList(
  130. ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a"));
  131. }
  132. @Test
  133. public void renameBranchMultipleConfigValues() throws Exception {
  134. StoredConfig config = git.getRepository().getConfig();
  135. config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  136. ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
  137. config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  138. Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true);
  139. config.save();
  140. String branch = "b1";
  141. assertEquals(BranchRebaseMode.REBASE,
  142. config.getEnum(BranchRebaseMode.values(),
  143. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  144. ConfigConstants.CONFIG_KEY_REBASE,
  145. BranchRebaseMode.NONE));
  146. assertNull(config.getEnum(BranchRebaseMode.values(),
  147. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  148. ConfigConstants.CONFIG_KEY_REBASE, null));
  149. assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  150. Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true));
  151. assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  152. branch, ConfigConstants.CONFIG_KEY_MERGE, false));
  153. assertNotNull(git.branchRename().setNewName(branch).call());
  154. config = git.getRepository().getConfig();
  155. assertNull(config.getEnum(BranchRebaseMode.values(),
  156. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  157. ConfigConstants.CONFIG_KEY_REBASE, null));
  158. assertEquals(BranchRebaseMode.REBASE,
  159. config.getEnum(BranchRebaseMode.values(),
  160. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  161. ConfigConstants.CONFIG_KEY_REBASE,
  162. BranchRebaseMode.NONE));
  163. assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  164. Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, false));
  165. assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  166. branch, ConfigConstants.CONFIG_KEY_MERGE, false));
  167. }
  168. }