View Javadoc
1   /*
2    * Copyright (C) 2011, Robin Stocker <robin@nibor.org>
3    * Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.lib;
46  
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertFalse;
49  import static org.junit.Assert.assertNull;
50  import static org.junit.Assert.assertTrue;
51  
52  import org.eclipse.jgit.errors.ConfigInvalidException;
53  import org.junit.Test;
54  
55  public class BranchConfigTest {
56  
57  	@Test
58  	public void getRemoteTrackingBranchShouldHandleNormalCase() {
59  		Config c = parse("" //
60  				+ "[remote \"origin\"]\n"
61  				+ "  fetch = +refs/heads/*:refs/remotes/origin/*\n"
62  				+ "[branch \"master\"]\n"
63  				+ "  remote = origin\n"
64  				+ "  merge = refs/heads/master\n");
65  
66  		BranchConfig branchConfig = new BranchConfig(c, "master");
67  		assertEquals("refs/remotes/origin/master",
68  				branchConfig.getRemoteTrackingBranch());
69  	}
70  
71  	@Test
72  	public void getRemoteTrackingBranchShouldHandleOtherMapping() {
73  		Config c = parse("" //
74  				+ "[remote \"test\"]\n"
75  				+ "  fetch = +refs/foo/*:refs/remotes/origin/foo/*\n"
76  				+ "  fetch = +refs/heads/*:refs/remotes/origin/*\n"
77  				+ "  fetch = +refs/other/*:refs/remotes/origin/other/*\n"
78  				+ "[branch \"master\"]\n"
79  				+ "  remote = test\n"
80  				+ "  merge = refs/foo/master\n" + "\n");
81  
82  		BranchConfig branchConfig = new BranchConfig(c, "master");
83  		assertEquals("refs/remotes/origin/foo/master",
84  				branchConfig.getRemoteTrackingBranch());
85  	}
86  
87  	@Test
88  	public void getRemoteTrackingBranchShouldReturnNullWithoutFetchSpec() {
89  		Config c = parse("" //
90  				+ "[remote \"origin\"]\n"
91  				+ "  fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
92  				+ "[branch \"master\"]\n"
93  				+ "  remote = origin\n"
94  				+ "  merge = refs/heads/master\n");
95  		BranchConfig branchConfig = new BranchConfig(c, "master");
96  		assertNull(branchConfig.getRemoteTrackingBranch());
97  	}
98  
99  	@Test
100 	public void getRemoteTrackingBranchShouldReturnNullWithoutMergeBranch() {
101 		Config c = parse("" //
102 				+ "[remote \"origin\"]\n"
103 				+ "  fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
104 				+ "[branch \"master\"]\n"
105 				+ "  remote = origin\n");
106 		BranchConfig branchConfig = new BranchConfig(c, "master");
107 		assertNull(branchConfig.getRemoteTrackingBranch());
108 	}
109 
110 	@Test
111 	public void getTrackingBranchShouldReturnMergeBranchForLocalBranch() {
112 		Config c = parse("" //
113 				+ "[remote \"origin\"]\n"
114 				+ "  fetch = +refs/heads/*:refs/remotes/origin/*\n"
115 				+ "[branch \"master\"]\n"
116 				+ "  remote = .\n"
117 				+ "  merge = refs/heads/master\n");
118 
119 		BranchConfig branchConfig = new BranchConfig(c, "master");
120 		assertEquals("refs/heads/master",
121 				branchConfig.getTrackingBranch());
122 	}
123 
124 	@Test
125 	public void getTrackingBranchShouldReturnNullWithoutMergeBranchForLocalBranch() {
126 		Config c = parse("" //
127 				+ "[remote \"origin\"]\n"
128 				+ "  fetch = +refs/heads/onlyone:refs/remotes/origin/onlyone\n"
129 				+ "[branch \"master\"]\n" //
130 				+ "  remote = .\n");
131 		BranchConfig branchConfig = new BranchConfig(c, "master");
132 		assertNull(branchConfig.getTrackingBranch());
133 	}
134 
135 	@Test
136 	public void getTrackingBranchShouldHandleNormalCaseForRemoteTrackingBranch() {
137 		Config c = parse("" //
138 				+ "[remote \"origin\"]\n"
139 				+ "  fetch = +refs/heads/*:refs/remotes/origin/*\n"
140 				+ "[branch \"master\"]\n"
141 				+ "  remote = origin\n"
142 				+ "  merge = refs/heads/master\n");
143 
144 		BranchConfig branchConfig = new BranchConfig(c, "master");
145 		assertEquals("refs/remotes/origin/master",
146 				branchConfig.getTrackingBranch());
147 	}
148 
149 	@Test
150 	public void isRebase() {
151 		Config c = parse("" //
152 				+ "[branch \"undefined\"]\n"
153 				+ "[branch \"false\"]\n"
154 				+ "  rebase = false\n"
155 				+ "[branch \"true\"]\n"
156 				+ "  rebase = true\n");
157 
158 		assertFalse(new BranchConfig(c, "undefined").isRebase());
159 		assertFalse(new BranchConfig(c, "false").isRebase());
160 		assertTrue(new BranchConfig(c, "true").isRebase());
161 	}
162 
163 	private static Config parse(final String content) {
164 		final Config c = new Config(null);
165 		try {
166 			c.fromText(content);
167 		} catch (ConfigInvalidException e) {
168 			throw new RuntimeException(e);
169 		}
170 		return c;
171 	}
172 }