View Javadoc
1   /*
2    * Copyright (C) 2019, Google LLC.
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.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  	/** {@inheritDoc} */
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 }