View Javadoc
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  
45  import static org.junit.Assert.assertArrayEquals;
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertNotNull;
49  import static org.junit.Assert.assertNull;
50  import static org.junit.Assert.assertTrue;
51  
52  import org.eclipse.jgit.junit.RepositoryTestCase;
53  import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
54  import org.eclipse.jgit.lib.ConfigConstants;
55  import org.eclipse.jgit.lib.Constants;
56  import org.eclipse.jgit.lib.StoredConfig;
57  import org.eclipse.jgit.revwalk.RevCommit;
58  import org.junit.Before;
59  import org.junit.Test;
60  
61  /**
62   * Unit tests of {@link RenameBranchCommand}
63   */
64  public class RenameBranchCommandTest extends RepositoryTestCase {
65  
66  	private static final String PATH = "file.txt";
67  
68  	private RevCommit head;
69  
70  	private Git git;
71  
72  	@Override
73  	@Before
74  	public void setUp() throws Exception {
75  		super.setUp();
76  		git = Git.wrap(db);
77  		writeTrashFile(PATH, "content");
78  		git.add().addFilepattern(PATH).call();
79  		head = git.commit().setMessage("add file").call();
80  		assertNotNull(head);
81  	}
82  
83  	@Test
84  	public void renameBranchNoConfigValues() throws Exception {
85  		StoredConfig config = git.getRepository().getConfig();
86  		config.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
87  				Constants.MASTER);
88  		config.save();
89  
90  		String branch = "b1";
91  		assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
92  				Constants.MASTER).isEmpty());
93  		assertNotNull(git.branchRename().setNewName(branch).call());
94  
95  		config = git.getRepository().getConfig();
96  		assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
97  				Constants.MASTER).isEmpty());
98  		assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
99  				branch).isEmpty());
100 	}
101 
102 	@Test
103 	public void renameBranchSingleConfigValue() throws Exception {
104 		StoredConfig config = git.getRepository().getConfig();
105 		config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
106 				ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
107 		config.save();
108 
109 		String branch = "b1";
110 
111 		assertEquals(BranchRebaseMode.REBASE,
112 				config.getEnum(BranchRebaseMode.values(),
113 						ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
114 						ConfigConstants.CONFIG_KEY_REBASE,
115 						BranchRebaseMode.NONE));
116 		assertNull(config.getEnum(BranchRebaseMode.values(),
117 				ConfigConstants.CONFIG_BRANCH_SECTION, branch,
118 				ConfigConstants.CONFIG_KEY_REBASE, null));
119 
120 		assertNotNull(git.branchRename().setNewName(branch).call());
121 
122 		config = git.getRepository().getConfig();
123 		assertNull(config.getEnum(BranchRebaseMode.values(),
124 				ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
125 				ConfigConstants.CONFIG_KEY_REBASE, null));
126 		assertEquals(BranchRebaseMode.REBASE,
127 				config.getEnum(BranchRebaseMode.values(),
128 						ConfigConstants.CONFIG_BRANCH_SECTION, branch,
129 						ConfigConstants.CONFIG_KEY_REBASE,
130 						BranchRebaseMode.NONE));
131 	}
132 
133 	@Test
134 	public void renameBranchExistingSection() throws Exception {
135 		String branch = "b1";
136 		StoredConfig config = git.getRepository().getConfig();
137 		config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
138 				ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
139 		config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
140 				Constants.MASTER, "a", "a");
141 		config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a",
142 				"b");
143 		config.save();
144 
145 		assertNotNull(git.branchRename().setNewName(branch).call());
146 
147 		config = git.getRepository().getConfig();
148 		assertArrayEquals(new String[] { "b", "a" }, config.getStringList(
149 				ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a"));
150 	}
151 
152 	@Test
153 	public void renameBranchMultipleConfigValues() throws Exception {
154 		StoredConfig config = git.getRepository().getConfig();
155 		config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
156 				ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
157 		config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
158 				Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true);
159 		config.save();
160 
161 		String branch = "b1";
162 
163 		assertEquals(BranchRebaseMode.REBASE,
164 				config.getEnum(BranchRebaseMode.values(),
165 						ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
166 						ConfigConstants.CONFIG_KEY_REBASE,
167 						BranchRebaseMode.NONE));
168 		assertNull(config.getEnum(BranchRebaseMode.values(),
169 				ConfigConstants.CONFIG_BRANCH_SECTION, branch,
170 				ConfigConstants.CONFIG_KEY_REBASE, null));
171 		assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
172 				Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true));
173 		assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
174 				branch, ConfigConstants.CONFIG_KEY_MERGE, false));
175 
176 		assertNotNull(git.branchRename().setNewName(branch).call());
177 
178 		config = git.getRepository().getConfig();
179 		assertNull(config.getEnum(BranchRebaseMode.values(),
180 				ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
181 				ConfigConstants.CONFIG_KEY_REBASE, null));
182 		assertEquals(BranchRebaseMode.REBASE,
183 				config.getEnum(BranchRebaseMode.values(),
184 						ConfigConstants.CONFIG_BRANCH_SECTION, branch,
185 						ConfigConstants.CONFIG_KEY_REBASE,
186 						BranchRebaseMode.NONE));
187 		assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
188 				Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, false));
189 		assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
190 				branch, ConfigConstants.CONFIG_KEY_MERGE, false));
191 	}
192 }