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