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  		Git git = new Git(db);
61  		for (int i = 0; i < count; i++)
62  			git.commit().setCommitter(committer).setAuthor(author)
63  					.setMessage("commit " + i).call();
64  		list = new RevCommitList<RevCommit>();
65  		RevWalk w = new RevWalk(db);
66  		w.markStart(w.lookupCommit(db.resolve(Constants.HEAD)));
67  		list.source(w);
68  	}
69  
70  	@Test
71  	public void testFillToHighMark2() throws Exception {
72  		setup(3);
73  		list.fillTo(1);
74  		assertEquals(2, list.size());
75  		assertEquals("commit 2", list.get(0).getFullMessage());
76  		assertEquals("commit 1", list.get(1).getFullMessage());
77  		assertNull("commit 0 shouldn't be loaded", list.get(2));
78  	}
79  
80  	@Test
81  	public void testFillToHighMarkAll() throws Exception {
82  		setup(3);
83  		list.fillTo(2);
84  		assertEquals(3, list.size());
85  		assertEquals("commit 2", list.get(0).getFullMessage());
86  		assertEquals("commit 0", list.get(2).getFullMessage());
87  	}
88  
89  	@Test
90  	public void testFillToHighMark4() throws Exception {
91  		setup(3);
92  		list.fillTo(3);
93  		assertEquals(3, list.size());
94  		assertEquals("commit 2", list.get(0).getFullMessage());
95  		assertEquals("commit 0", list.get(2).getFullMessage());
96  		assertNull("commit 3 can't be loaded", list.get(3));
97  	}
98  
99  	@Test
100 	public void testFillToHighMarkMulitpleBlocks() throws Exception {
101 		setup(258);
102 		list.fillTo(257);
103 		assertEquals(258, list.size());
104 	}
105 
106 	@Test
107 	public void testFillToCommit() throws Exception {
108 		setup(3);
109 
110 		RevWalk w = new RevWalk(db);
111 		w.markStart(w.lookupCommit(db.resolve(Constants.HEAD)));
112 
113 		w.next();
114 		RevCommit c = w.next();
115 		assertNotNull("should have found 2. commit", c);
116 
117 		list.fillTo(c, 5);
118 		assertEquals(2, list.size());
119 		assertEquals("commit 1", list.get(1).getFullMessage());
120 		assertNull(list.get(3));
121 	}
122 
123 	@Test
124 	public void testFillToUnknownCommit() throws Exception {
125 		setup(258);
126 		RevCommit c = new RevCommit(
127 				ObjectId.fromString("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
128 
129 		list.fillTo(c, 300);
130 		assertEquals("loading to unknown commit should load all commits", 258,
131 				list.size());
132 	}
133 
134 	@Test
135 	public void testFillToNullCommit() throws Exception {
136 		setup(3);
137 		list.fillTo(null, 1);
138 		assertNull(list.get(0));
139 	}
140 }