View Javadoc
1   /*
2    * Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com> 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  package org.eclipse.jgit.revwalk;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertNull;
15  
16  import org.eclipse.jgit.api.Git;
17  import org.eclipse.jgit.junit.RepositoryTestCase;
18  import org.eclipse.jgit.lib.Constants;
19  import org.eclipse.jgit.lib.ObjectId;
20  import org.junit.Test;
21  
22  public class RevCommitListTest extends RepositoryTestCase {
23  
24  	private RevCommitList<RevCommit> list;
25  
26  	public void setup(int count) throws Exception {
27  		try (Git git = new Git(db);
28  				RevWalk w = new RevWalk(db);) {
29  			for (int i = 0; i < count; i++)
30  				git.commit().setCommitter(committer).setAuthor(author)
31  						.setMessage("commit " + i).call();
32  			list = new RevCommitList<>();
33  			w.markStart(w.lookupCommit(db.resolve(Constants.HEAD)));
34  			list.source(w);
35  		}
36  	}
37  
38  	@Test
39  	public void testFillToHighMark2() throws Exception {
40  		setup(3);
41  		list.fillTo(1);
42  		assertEquals(2, list.size());
43  		assertEquals("commit 2", list.get(0).getFullMessage());
44  		assertEquals("commit 1", list.get(1).getFullMessage());
45  		assertNull("commit 0 shouldn't be loaded", list.get(2));
46  	}
47  
48  	@Test
49  	public void testFillToHighMarkAll() throws Exception {
50  		setup(3);
51  		list.fillTo(2);
52  		assertEquals(3, list.size());
53  		assertEquals("commit 2", list.get(0).getFullMessage());
54  		assertEquals("commit 0", list.get(2).getFullMessage());
55  	}
56  
57  	@Test
58  	public void testFillToHighMark4() throws Exception {
59  		setup(3);
60  		list.fillTo(3);
61  		assertEquals(3, list.size());
62  		assertEquals("commit 2", list.get(0).getFullMessage());
63  		assertEquals("commit 0", list.get(2).getFullMessage());
64  		assertNull("commit 3 can't be loaded", list.get(3));
65  	}
66  
67  	@Test
68  	public void testFillToHighMarkMulitpleBlocks() throws Exception {
69  		setup(258);
70  		list.fillTo(257);
71  		assertEquals(258, list.size());
72  	}
73  
74  	@Test
75  	public void testFillToCommit() throws Exception {
76  		setup(3);
77  
78  		try (RevWalk w = new RevWalk(db)) {
79  			w.markStart(w.lookupCommit(db.resolve(Constants.HEAD)));
80  
81  			w.next();
82  			RevCommit c = w.next();
83  			assertNotNull("should have found 2. commit", c);
84  
85  			list.fillTo(c, 5);
86  			assertEquals(2, list.size());
87  			assertEquals("commit 1", list.get(1).getFullMessage());
88  			assertNull(list.get(3));
89  		}
90  	}
91  
92  	@Test
93  	public void testFillToUnknownCommit() throws Exception {
94  		setup(258);
95  		RevCommit c = new RevCommit(
96  				ObjectId.fromString("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
97  
98  		list.fillTo(c, 300);
99  		assertEquals("loading to unknown commit should load all commits", 258,
100 				list.size());
101 	}
102 
103 	@Test
104 	public void testFillToNullCommit() throws Exception {
105 		setup(3);
106 		list.fillTo(null, 1);
107 		assertNull(list.get(0));
108 	}
109 }