You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RenameBranchCommandTest.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. @Override
  66. @Before
  67. public void setUp() throws Exception {
  68. super.setUp();
  69. git = Git.wrap(db);
  70. writeTrashFile(PATH, "content");
  71. git.add().addFilepattern(PATH).call();
  72. head = git.commit().setMessage("add file").call();
  73. assertNotNull(head);
  74. }
  75. @Test
  76. public void renameBranchNoConfigValues() throws Exception {
  77. StoredConfig config = git.getRepository().getConfig();
  78. config.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
  79. Constants.MASTER);
  80. config.save();
  81. String branch = "b1";
  82. assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
  83. Constants.MASTER).isEmpty());
  84. assertNotNull(git.branchRename().setNewName(branch).call());
  85. config = git.getRepository().getConfig();
  86. assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
  87. Constants.MASTER).isEmpty());
  88. assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
  89. branch).isEmpty());
  90. }
  91. @Test
  92. public void renameBranchSingleConfigValue() throws Exception {
  93. StoredConfig config = git.getRepository().getConfig();
  94. config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  95. ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
  96. config.save();
  97. String branch = "b1";
  98. assertEquals(BranchRebaseMode.REBASE,
  99. config.getEnum(BranchRebaseMode.values(),
  100. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  101. ConfigConstants.CONFIG_KEY_REBASE,
  102. BranchRebaseMode.NONE));
  103. assertNull(config.getEnum(BranchRebaseMode.values(),
  104. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  105. ConfigConstants.CONFIG_KEY_REBASE, null));
  106. assertNotNull(git.branchRename().setNewName(branch).call());
  107. config = git.getRepository().getConfig();
  108. assertNull(config.getEnum(BranchRebaseMode.values(),
  109. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  110. ConfigConstants.CONFIG_KEY_REBASE, null));
  111. assertEquals(BranchRebaseMode.REBASE,
  112. config.getEnum(BranchRebaseMode.values(),
  113. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  114. ConfigConstants.CONFIG_KEY_REBASE,
  115. BranchRebaseMode.NONE));
  116. }
  117. @Test
  118. public void renameBranchExistingSection() throws Exception {
  119. String branch = "b1";
  120. StoredConfig config = git.getRepository().getConfig();
  121. config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  122. ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
  123. config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
  124. Constants.MASTER, "a", "a");
  125. config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a",
  126. "b");
  127. config.save();
  128. assertNotNull(git.branchRename().setNewName(branch).call());
  129. config = git.getRepository().getConfig();
  130. assertArrayEquals(new String[] { "b", "a" }, config.getStringList(
  131. ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a"));
  132. }
  133. @Test
  134. public void renameBranchMultipleConfigValues() throws Exception {
  135. StoredConfig config = git.getRepository().getConfig();
  136. config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  137. ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
  138. config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  139. Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true);
  140. config.save();
  141. String branch = "b1";
  142. assertEquals(BranchRebaseMode.REBASE,
  143. config.getEnum(BranchRebaseMode.values(),
  144. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  145. ConfigConstants.CONFIG_KEY_REBASE,
  146. BranchRebaseMode.NONE));
  147. assertNull(config.getEnum(BranchRebaseMode.values(),
  148. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  149. ConfigConstants.CONFIG_KEY_REBASE, null));
  150. assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  151. Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true));
  152. assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  153. branch, ConfigConstants.CONFIG_KEY_MERGE, false));
  154. assertNotNull(git.branchRename().setNewName(branch).call());
  155. config = git.getRepository().getConfig();
  156. assertNull(config.getEnum(BranchRebaseMode.values(),
  157. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  158. ConfigConstants.CONFIG_KEY_REBASE, null));
  159. assertEquals(BranchRebaseMode.REBASE,
  160. config.getEnum(BranchRebaseMode.values(),
  161. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  162. ConfigConstants.CONFIG_KEY_REBASE,
  163. BranchRebaseMode.NONE));
  164. assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  165. Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, false));
  166. assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  167. branch, ConfigConstants.CONFIG_KEY_MERGE, false));
  168. }
  169. }