View Javadoc
1   /*
2    * Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com>
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.revwalk;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNotNull;
47  import static org.junit.Assert.assertNull;
48  
49  import org.eclipse.jgit.api.Git;
50  import org.eclipse.jgit.junit.RepositoryTestCase;
51  import org.eclipse.jgit.lib.Constants;
52  import org.eclipse.jgit.lib.ObjectId;
53  import org.junit.Test;
54  
55  public class RevCommitListTest extends RepositoryTestCase {
56  
57  	private RevCommitList<RevCommit> list;
58  
59  	public void setup(int count) throws Exception {
60  		try (Git git = new Git(db);
61  				RevWalk w = new RevWalk(db);) {
62  			for (int i = 0; i < count; i++)
63  				git.commit().setCommitter(committer).setAuthor(author)
64  						.setMessage("commit " + i).call();
65  			list = new RevCommitList<>();
66  			w.markStart(w.lookupCommit(db.resolve(Constants.HEAD)));
67  			list.source(w);
68  		}
69  	}
70  
71  	@Test
72  	public void testFillToHighMark2() throws Exception {
73  		setup(3);
74  		list.fillTo(1);
75  		assertEquals(2, list.size());
76  		assertEquals("commit 2", list.get(0).getFullMessage());
77  		assertEquals("commit 1", list.get(1).getFullMessage());
78  		assertNull("commit 0 shouldn't be loaded", list.get(2));
79  	}
80  
81  	@Test
82  	public void testFillToHighMarkAll() throws Exception {
83  		setup(3);
84  		list.fillTo(2);
85  		assertEquals(3, list.size());
86  		assertEquals("commit 2", list.get(0).getFullMessage());
87  		assertEquals("commit 0", list.get(2).getFullMessage());
88  	}
89  
90  	@Test
91  	public void testFillToHighMark4() throws Exception {
92  		setup(3);
93  		list.fillTo(3);
94  		assertEquals(3, list.size());
95  		assertEquals("commit 2", list.get(0).getFullMessage());
96  		assertEquals("commit 0", list.get(2).getFullMessage());
97  		assertNull("commit 3 can't be loaded", list.get(3));
98  	}
99  
100 	@Test
101 	public void testFillToHighMarkMulitpleBlocks() throws Exception {
102 		setup(258);
103 		list.fillTo(257);
104 		assertEquals(258, list.size());
105 	}
106 
107 	@Test
108 	public void testFillToCommit() throws Exception {
109 		setup(3);
110 
111 		try (RevWalk w = new RevWalk(db)) {
112 			w.markStart(w.lookupCommit(db.resolve(Constants.HEAD)));
113 
114 			w.next();
115 			RevCommit c = w.next();
116 			assertNotNull("should have found 2. commit", c);
117 
118 			list.fillTo(c, 5);
119 			assertEquals(2, list.size());
120 			assertEquals("commit 1", list.get(1).getFullMessage());
121 			assertNull(list.get(3));
122 		}
123 	}
124 
125 	@Test
126 	public void testFillToUnknownCommit() throws Exception {
127 		setup(258);
128 		RevCommit c = new RevCommit(
129 				ObjectId.fromString("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
130 
131 		list.fillTo(c, 300);
132 		assertEquals("loading to unknown commit should load all commits", 258,
133 				list.size());
134 	}
135 
136 	@Test
137 	public void testFillToNullCommit() throws Exception {
138 		setup(3);
139 		list.fillTo(null, 1);
140 		assertNull(list.get(0));
141 	}
142 }