View Javadoc
1   /*
2    * Copyright (C) 2011, Robin Stocker <robin@nibor.org> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.lib;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNull;
15  
16  import org.eclipse.jgit.junit.RepositoryTestCase;
17  import org.eclipse.jgit.junit.TestRepository;
18  import org.eclipse.jgit.revwalk.RevCommit;
19  import org.eclipse.jgit.revwalk.RevWalk;
20  import org.junit.Test;
21  
22  public class BranchTrackingStatusTest extends RepositoryTestCase {
23  	private TestRepository<Repository> util;
24  
25  	protected RevWalk rw;
26  
27  	@Override
28  	public void setUp() throws Exception {
29  		super.setUp();
30  		util = new TestRepository<>(db);
31  		StoredConfig config = util.getRepository().getConfig();
32  		config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, "master",
33  				ConfigConstants.CONFIG_KEY_REMOTE, "origin");
34  		config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, "master",
35  				ConfigConstants.CONFIG_KEY_MERGE, "refs/heads/master");
36  		config.setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
37  				"fetch", "+refs/heads/*:refs/remotes/origin/*");
38  	}
39  
40  	@Test
41  	public void shouldWorkInNormalCase() throws Exception {
42  		RevCommit remoteTracking = util.branch("refs/remotes/origin/master")
43  				.commit().create();
44  		util.branch("master").commit().parent(remoteTracking).create();
45  		util.branch("master").commit().create();
46  
47  		BranchTrackingStatus status = BranchTrackingStatus.of(
48  				util.getRepository(), "master");
49  		assertEquals(2, status.getAheadCount());
50  		assertEquals(0, status.getBehindCount());
51  		assertEquals("refs/remotes/origin/master",
52  				status.getRemoteTrackingBranch());
53  	}
54  
55  	@Test
56  	public void shouldWorkWithoutMergeBase() throws Exception {
57  		util.branch("refs/remotes/origin/master").commit().create();
58  		util.branch("master").commit().create();
59  
60  		BranchTrackingStatus status = BranchTrackingStatus.of(util.getRepository(), "master");
61  		assertEquals(1, status.getAheadCount());
62  		assertEquals(1, status.getBehindCount());
63  	}
64  
65  	@Test
66  	public void shouldReturnNullWhenBranchDoesntExist() throws Exception {
67  		BranchTrackingStatus status = BranchTrackingStatus.of(
68  				util.getRepository(), "doesntexist");
69  
70  		assertNull(status);
71  	}
72  }