View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
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  
44  package org.eclipse.jgit.revwalk;
45  
46  import static org.junit.Assert.assertNull;
47  import static org.junit.Assert.assertTrue;
48  
49  import org.junit.Test;
50  
51  public class RevWalkSortTest extends RevWalkTestCase {
52  	@Test
53  	public void testSort_Default() throws Exception {
54  		final RevCommit a = commit();
55  		final RevCommit b = commit(1, a);
56  		final RevCommit c = commit(1, b);
57  		final RevCommit d = commit(1, c);
58  
59  		markStart(d);
60  		assertCommit(d, rw.next());
61  		assertCommit(c, rw.next());
62  		assertCommit(b, rw.next());
63  		assertCommit(a, rw.next());
64  		assertNull(rw.next());
65  	}
66  
67  	@Test
68  	public void testSort_COMMIT_TIME_DESC() throws Exception {
69  		final RevCommit a = commit();
70  		final RevCommit b = commit(a);
71  		final RevCommit c = commit(b);
72  		final RevCommit d = commit(c);
73  
74  		rw.sort(RevSort.COMMIT_TIME_DESC);
75  		markStart(d);
76  		assertCommit(d, rw.next());
77  		assertCommit(c, rw.next());
78  		assertCommit(b, rw.next());
79  		assertCommit(a, rw.next());
80  		assertNull(rw.next());
81  	}
82  
83  	@Test
84  	public void testSort_REVERSE() throws Exception {
85  		final RevCommit a = commit();
86  		final RevCommit b = commit(a);
87  		final RevCommit c = commit(b);
88  		final RevCommit d = commit(c);
89  
90  		rw.sort(RevSort.REVERSE);
91  		markStart(d);
92  		assertCommit(a, rw.next());
93  		assertCommit(b, rw.next());
94  		assertCommit(c, rw.next());
95  		assertCommit(d, rw.next());
96  		assertNull(rw.next());
97  	}
98  
99  	@Test
100 	public void testSort_COMMIT_TIME_DESC_OutOfOrder1() throws Exception {
101 		// Despite being out of order time-wise, a strand-of-pearls must
102 		// still maintain topological order.
103 		//
104 		final RevCommit a = commit();
105 		final RevCommit b = commit(a);
106 		final RevCommit c = commit(-5, b);
107 		final RevCommit d = commit(10, c);
108 		assertTrue(parseBody(a).getCommitTime() < parseBody(d).getCommitTime());
109 		assertTrue(parseBody(c).getCommitTime() < parseBody(b).getCommitTime());
110 
111 		rw.sort(RevSort.COMMIT_TIME_DESC);
112 		markStart(d);
113 		assertCommit(d, rw.next());
114 		assertCommit(c, rw.next());
115 		assertCommit(b, rw.next());
116 		assertCommit(a, rw.next());
117 		assertNull(rw.next());
118 	}
119 
120 	@Test
121 	public void testSort_COMMIT_TIME_DESC_OutOfOrder2() throws Exception {
122 		// c1 is back dated before its parent.
123 		//
124 		final RevCommit a = commit();
125 		final RevCommit b = commit(a);
126 		final RevCommit c1 = commit(-5, b);
127 		final RevCommit c2 = commit(10, b);
128 		final RevCommit d = commit(c1, c2);
129 
130 		rw.sort(RevSort.COMMIT_TIME_DESC);
131 		markStart(d);
132 		assertCommit(d, rw.next());
133 		assertCommit(c2, rw.next());
134 		assertCommit(b, rw.next());
135 		assertCommit(a, rw.next());
136 		assertCommit(c1, rw.next());
137 		assertNull(rw.next());
138 	}
139 
140 	@Test
141 	public void testSort_TOPO() throws Exception {
142 		// c1 is back dated before its parent.
143 		//
144 		final RevCommit a = commit();
145 		final RevCommit b = commit(a);
146 		final RevCommit c1 = commit(-5, b);
147 		final RevCommit c2 = commit(10, b);
148 		final RevCommit d = commit(c1, c2);
149 
150 		rw.sort(RevSort.TOPO);
151 		markStart(d);
152 		assertCommit(d, rw.next());
153 		assertCommit(c2, rw.next());
154 		assertCommit(c1, rw.next());
155 		assertCommit(b, rw.next());
156 		assertCommit(a, rw.next());
157 		assertNull(rw.next());
158 	}
159 
160 	@Test
161 	public void testSort_TOPO_REVERSE() throws Exception {
162 		// c1 is back dated before its parent.
163 		//
164 		final RevCommit a = commit();
165 		final RevCommit b = commit(a);
166 		final RevCommit c1 = commit(-5, b);
167 		final RevCommit c2 = commit(10, b);
168 		final RevCommit d = commit(c1, c2);
169 
170 		rw.sort(RevSort.TOPO);
171 		rw.sort(RevSort.REVERSE, true);
172 		markStart(d);
173 		assertCommit(a, rw.next());
174 		assertCommit(b, rw.next());
175 		assertCommit(c1, rw.next());
176 		assertCommit(c2, rw.next());
177 		assertCommit(d, rw.next());
178 		assertNull(rw.next());
179 	}
180 }