Fixes after ea27f5d - test results and the sorting of the workspace revision
[egit/zawir.git] / org.spearce.jgit / tst / org / spearce / jgit / lib / T0007_WalkerTest.java
blobe410691472b4982021a9c89de6dcb953b7e4457f
1 /*
2 * Copyright (C) 2006, 2007 Robin Rosenberg
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.lib;
19 import java.io.IOException;
21 import junit.textui.TestRunner;
23 public class T0007_WalkerTest extends RepositoryTestCase {
25 private static String[] parsePath(String path) {
26 String[] ret = path.split("/");
27 if (ret.length==1 && ret[0].equals(""))
28 return new String[0];
29 return ret;
32 Commit[] mapCommits(String[] name) throws IOException {
33 Commit[] ret = new Commit[name.length];
34 for (int i=0; i<name.length; ++i)
35 ret[i] = db.mapCommit(name[i]);
36 return ret;
39 class MyWalker extends TopologicalWalker {
41 public MyWalker(String[] starts, String path, boolean leafIsBlob,boolean followMainOnly, Boolean merges, ObjectId activeDiffLeafId) throws IOException {
42 super(db, mapCommits(starts), parsePath(path), leafIsBlob, followMainOnly, merges, activeDiffLeafId);
45 protected boolean isCancelled() {
46 return false;
49 @Override
50 protected void collect(Commit commit, int count, int breadth) {
51 ObjectId commitId = commit.getCommitId();
52 if (commitId == null)
53 commitId = ObjectId.zeroId();
54 super.collect(commit, count, breadth);
57 ObjectId[] collect() {
58 return (ObjectId[]) collectHistory().toArray(new ObjectId[0]);
59 // ObjectId[] r = (ObjectId[]) ret.toArray(new ObjectId[0]);
60 // Arrays.sort(r);
61 // return r;
65 MyWalker newWalker(String[] starts, String path, boolean leafIsBlob,boolean followMainOnly, Boolean merges, ObjectId activeDiffLeafId) {
66 try {
67 return new MyWalker(starts,path,leafIsBlob,followMainOnly,merges,activeDiffLeafId);
68 } catch (IOException e) {
69 throw new RuntimeException(e);
73 MyWalker newWalker(String start, String path, boolean leafIsBlob,boolean followMainOnly, Boolean merges, ObjectId activeDiffLeafId) {
74 return newWalker(new String[] { start }, path, leafIsBlob, followMainOnly, merges, activeDiffLeafId);
77 /** These are simple case, but account for the <em>last</em> commit */
78 static final String FIRST_COMMIT_ON_MASTER = "6c8b137b1c652731597c89668f417b8695f28dd7";
80 /** We are using a path with no match */
81 public void testHistoryScanLast_1_nomatch() {
82 MyWalker walker = newWalker(FIRST_COMMIT_ON_MASTER,"master.x",true,true,null,null);
83 assertEquals(0, walker.collect().length);
86 /** We only want merges, but the path is valid so we do not get any */
87 public void testHistoryScanLast_2_onlymerges() {
88 MyWalker walker = newWalker(FIRST_COMMIT_ON_MASTER,"master.txt",true,true,Boolean.TRUE,null);
89 assertEquals(0, walker.collect().length);
92 /** We do not want merges, the path is valid so we get a null (which means that the commit itself
93 * introduces a change, plus the commit which introduces a change against nothing.
95 public void testHistoryScanLast_2_nomerges() {
96 MyWalker walker = newWalker(FIRST_COMMIT_ON_MASTER,"master.txt",true,true,Boolean.FALSE,null);
97 ObjectId[] h = walker.collect();
98 assertEquals(1, h.length);
99 assertEquals(FIRST_COMMIT_ON_MASTER, h[0].toString());
102 /** We accept merges and non-merges, the path is valid so we get a null (which means that the
103 * commit itself introduces a change, plus the commit which introduces a change against nothing.
104 * Since there are no merges here this means nothing on practice.
106 public void testHistoryScanLast_2_any() {
107 MyWalker walker = newWalker(FIRST_COMMIT_ON_MASTER,"master.txt",true,true,null,null);
108 ObjectId[] h = walker.collect();
109 assertEquals(1, h.length);
110 assertEquals(FIRST_COMMIT_ON_MASTER, h[0].toString());
113 /** We add a reference to compare with. In this case the data does not match the blob so the state
114 * after the commit is a change, as is the change introduced by the commit.
116 public void testHistoryScanLast_2_any_with_change_from_reference() {
117 MyWalker walker = newWalker(FIRST_COMMIT_ON_MASTER,"master.txt",true,true,null,new ObjectId("82b1d08466e9505f8666b778744f9a3471a70c81"));
118 ObjectId[] h = walker.collect();
119 assertEquals(2, h.length);
120 assertEquals("0000000000000000000000000000000000000000", h[0].toString());
121 assertEquals(FIRST_COMMIT_ON_MASTER, h[1].toString());
124 /** We add a reference to compare with. In this case the reference SHA-1 matches the state in the commit
125 * so the commit doesn't change anything, but it does relative to nothing, i.e. log the commit.
127 public void testHistoryScanLast_2_any_with_no_change_from_reference() {
128 MyWalker walker = newWalker(FIRST_COMMIT_ON_MASTER,"master.txt",true,true,null,new ObjectId("8230f48330e0055d9e0bc5a2a77718f6dd9324b8"));
129 ObjectId[] h = walker.collect();
130 assertEquals(1,h.length);
131 assertEquals(FIRST_COMMIT_ON_MASTER, h[0].toString());
134 /** Give the null id as a reference, which means the state of the commit vs "index" is a change as is
135 * the change vs nothing.
137 public void testHistoryScanLast_2_any_file_dropped() {
138 MyWalker walker = newWalker(FIRST_COMMIT_ON_MASTER,"master.txt",true,true,null,ObjectId.zeroId());
139 ObjectId[] h = walker.collect();
140 assertEquals(2, h.length);
141 assertEquals("0000000000000000000000000000000000000000", h[0].toString());
142 assertEquals(FIRST_COMMIT_ON_MASTER, h[1].toString());
145 /** A simple first parent scan of all commits
147 public void testSimpleScanFirstParentIncludingMerges() {
148 MyWalker walker = newWalker("master^1^2" /* Second b/b2 */, "", false, true, null, null);
149 ObjectId[] h = walker.collect();
150 assertEquals(7, h.length);
151 assertEquals("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", h[0].toString());
152 assertEquals("59706a11bde2b9899a278838ef20a97e8f8795d2", h[1].toString());
153 assertEquals("c070ad8c08840c8116da865b2d65593a6bb9cd2a", h[2].toString());
154 assertEquals("d31f5a60d406e831d056b8ac2538d515100c2df2", h[3].toString());
155 assertEquals("83d2f0431bcdc9c2fd2c17b828143be6ee4fbe80", h[4].toString());
156 assertEquals("58be4659bb571194ed4562d04b359d26216f526e", h[5].toString());
157 assertEquals("6c8b137b1c652731597c89668f417b8695f28dd7", h[6].toString());
160 /** A simple first parent scan of all commits, no merges
162 public void testSimpleScanFirstParentExcludingMerges() {
163 MyWalker walker = newWalker("master^1^2" /* Second b/b2 */, "", false, true, Boolean.FALSE, null);
164 ObjectId[] h = walker.collect();
165 assertEquals(6, h.length);
166 assertEquals("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", h[0].toString());
167 assertEquals("59706a11bde2b9899a278838ef20a97e8f8795d2", h[1].toString());
168 assertEquals("d31f5a60d406e831d056b8ac2538d515100c2df2", h[2].toString());
169 assertEquals("83d2f0431bcdc9c2fd2c17b828143be6ee4fbe80", h[3].toString());
170 assertEquals("58be4659bb571194ed4562d04b359d26216f526e", h[4].toString());
171 assertEquals("6c8b137b1c652731597c89668f417b8695f28dd7", h[5].toString());
174 /** A simple first parent scan of all commits, only merges
176 public void testSimpleScanFirstParentOnlyMerges() {
177 MyWalker walker = newWalker("master^1^2" /* Second b/b2 */, "", false, true, Boolean.TRUE, null);
178 ObjectId[] h = walker.collect();
179 assertEquals(1, h.length);
180 assertEquals("c070ad8c08840c8116da865b2d65593a6bb9cd2a", h[0].toString());
183 /** Select all commits from a point, starting at a merge
185 public void testSimpleScanMultiplePaths() {
186 MyWalker walker = newWalker("a^^" /* 0966a434eb1a025db6b71485ab63a3bfbea520b6 */ , "", false, false, null, null);
187 ObjectId[] h = walker.collect();
188 assertEquals(5, h.length);
189 assertEquals("0966a434eb1a025db6b71485ab63a3bfbea520b6", h[0].toString());
190 assertEquals("2c349335b7f797072cf729c4f3bb0914ecb6dec9", h[1].toString());
191 assertEquals("ac7e7e44c1885efb472ad54a78327d66bfc4ecef", h[2].toString());
192 assertEquals("58be4659bb571194ed4562d04b359d26216f526e", h[3].toString());
193 assertEquals("6c8b137b1c652731597c89668f417b8695f28dd7", h[4].toString());
196 /** Select all commits from a point, starting at a normal commit
198 public void testSimpleScanMultiplePaths_2() {
199 MyWalker walker = newWalker("a^" /* d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864 */ , "", false, false, null, null);
200 ObjectId[] h = walker.collect();
201 assertEquals(6, h.length);
202 assertEquals("d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864", h[0].toString());
203 assertEquals("0966a434eb1a025db6b71485ab63a3bfbea520b6", h[1].toString());
204 assertEquals("2c349335b7f797072cf729c4f3bb0914ecb6dec9", h[2].toString());
205 assertEquals("ac7e7e44c1885efb472ad54a78327d66bfc4ecef", h[3].toString());
206 assertEquals("58be4659bb571194ed4562d04b359d26216f526e", h[4].toString());
207 assertEquals("6c8b137b1c652731597c89668f417b8695f28dd7", h[5].toString());
210 public static void main(String[] args) {
211 TestRunner.run(T0007_WalkerTest.class);