1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.revwalk;
44
45 import static org.junit.Assert.assertFalse;
46 import static org.junit.Assert.assertTrue;
47
48 import java.util.Arrays;
49 import java.util.Optional;
50
51 import org.eclipse.jgit.internal.storage.file.FileRepository;
52 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
53 import org.eclipse.jgit.junit.TestRepository;
54 import org.junit.Before;
55 import org.junit.Test;
56
57 public abstract class ReachabilityCheckerTestCase
58 extends LocalDiskRepositoryTestCase {
59
60 protected abstract ReachabilityChecker getChecker(
61 TestRepository<FileRepository> repository) throws Exception;
62
63 TestRepository<FileRepository> repo;
64
65
66 @Override
67 @Before
68 public void setUp() throws Exception {
69 super.setUp();
70 FileRepository db = createWorkRepository();
71 repo = new TestRepository<>(db);
72 }
73
74 @Test
75 public void reachable() throws Exception {
76 RevCommit a = repo.commit().create();
77 RevCommit b1 = repo.commit(a);
78 RevCommit b2 = repo.commit(b1);
79 RevCommit c1 = repo.commit(a);
80 RevCommit c2 = repo.commit(c1);
81 repo.update("refs/heads/checker", b2);
82
83 ReachabilityChecker checker = getChecker(repo);
84
85 assertReachable("reachable from one tip",
86 checker.areAllReachable(Arrays.asList(a), Arrays.asList(c2)));
87 assertReachable("reachable from another tip",
88 checker.areAllReachable(Arrays.asList(a), Arrays.asList(b2)));
89 assertReachable("reachable from itself",
90 checker.areAllReachable(Arrays.asList(a), Arrays.asList(b2)));
91 }
92
93 @Test
94 public void reachable_merge() throws Exception {
95 RevCommit a = repo.commit().create();
96 RevCommit b1 = repo.commit(a);
97 RevCommit b2 = repo.commit(b1);
98 RevCommit c1 = repo.commit(a);
99 RevCommit c2 = repo.commit(c1);
100 RevCommit merge = repo.commit(c2, b2);
101 repo.update("refs/heads/checker", merge);
102
103 ReachabilityChecker checker = getChecker(repo);
104
105 assertReachable("reachable through one branch",
106 checker.areAllReachable(Arrays.asList(b1),
107 Arrays.asList(merge)));
108 assertReachable("reachable through another branch",
109 checker.areAllReachable(Arrays.asList(c1),
110 Arrays.asList(merge)));
111 assertReachable("reachable, before the branching",
112 checker.areAllReachable(Arrays.asList(a),
113 Arrays.asList(merge)));
114 }
115
116 @Test
117 public void unreachable_isLaterCommit() throws Exception {
118 RevCommit a = repo.commit().create();
119 RevCommit b1 = repo.commit(a);
120 RevCommit b2 = repo.commit(b1);
121 repo.update("refs/heads/checker", b2);
122
123 ReachabilityChecker checker = getChecker(repo);
124
125 assertUnreachable("unreachable from the future",
126 checker.areAllReachable(Arrays.asList(b2), Arrays.asList(b1)));
127 }
128
129 @Test
130 public void unreachable_differentBranch() throws Exception {
131 RevCommit a = repo.commit().create();
132 RevCommit b1 = repo.commit(a);
133 RevCommit b2 = repo.commit(b1);
134 RevCommit c1 = repo.commit(a);
135 repo.update("refs/heads/checker", b2);
136
137 ReachabilityChecker checker = getChecker(repo);
138
139 assertUnreachable("unreachable from different branch",
140 checker.areAllReachable(Arrays.asList(c1), Arrays.asList(b2)));
141 }
142
143 @Test
144 public void reachable_longChain() throws Exception {
145 RevCommit root = repo.commit().create();
146 RevCommit head = root;
147 for (int i = 0; i < 10000; i++) {
148 head = repo.commit(head);
149 }
150 repo.update("refs/heads/master", head);
151
152 ReachabilityChecker checker = getChecker(repo);
153
154 assertReachable("reachable with long chain in the middle", checker
155 .areAllReachable(Arrays.asList(root), Arrays.asList(head)));
156 }
157
158 private static void assertReachable(String msg,
159 Optional<RevCommit> result) {
160 assertFalse(msg, result.isPresent());
161 }
162
163 private static void assertUnreachable(String msg,
164 Optional<RevCommit> result) {
165 assertTrue(msg, result.isPresent());
166 }
167
168 }