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  import java.util.stream.Stream;
51  
52  import org.eclipse.jgit.internal.storage.file.FileRepository;
53  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
54  import org.eclipse.jgit.junit.TestRepository;
55  import org.junit.Before;
56  import org.junit.Test;
57  
58  public abstract class ReachabilityCheckerTestCase
59  		extends LocalDiskRepositoryTestCase {
60  
61  	protected abstract ReachabilityChecker getChecker(
62  			TestRepository<FileRepository> repository) throws Exception;
63  
64  	TestRepository<FileRepository> repo;
65  
66  	/** {@inheritDoc} */
67  	@Override
68  	@Before
69  	public void setUp() throws Exception {
70  		super.setUp();
71  		FileRepository db = createWorkRepository();
72  		repo = new TestRepository<>(db);
73  	}
74  
75  	@Test
76  	public void reachable() throws Exception {
77  		RevCommit a = repo.commit().create();
78  		RevCommit b1 = repo.commit(a);
79  		RevCommit b2 = repo.commit(b1);
80  		RevCommit c1 = repo.commit(a);
81  		RevCommit c2 = repo.commit(c1);
82  		repo.update("refs/heads/checker", b2);
83  
84  		ReachabilityChecker checker = getChecker(repo);
85  
86  		assertReachable("reachable from one tip",
87  				checker.areAllReachable(Arrays.asList(a), Stream.of(c2)));
88  		assertReachable("reachable from another tip",
89  				checker.areAllReachable(Arrays.asList(a), Stream.of(b2)));
90  		assertReachable("reachable from itself",
91  				checker.areAllReachable(Arrays.asList(a), Stream.of(b2)));
92  	}
93  
94  	@Test
95  	public void reachable_merge() throws Exception {
96  		RevCommit a = repo.commit().create();
97  		RevCommit b1 = repo.commit(a);
98  		RevCommit b2 = repo.commit(b1);
99  		RevCommit c1 = repo.commit(a);
100 		RevCommit c2 = repo.commit(c1);
101 		RevCommit merge = repo.commit(c2, b2);
102 		repo.update("refs/heads/checker", merge);
103 
104 		ReachabilityChecker checker = getChecker(repo);
105 
106 		assertReachable("reachable through one branch",
107 				checker.areAllReachable(Arrays.asList(b1),
108 						Stream.of(merge)));
109 		assertReachable("reachable through another branch",
110 				checker.areAllReachable(Arrays.asList(c1),
111 						Stream.of(merge)));
112 		assertReachable("reachable, before the branching",
113 				checker.areAllReachable(Arrays.asList(a),
114 						Stream.of(merge)));
115 	}
116 
117 	@Test
118 	public void unreachable_isLaterCommit() throws Exception {
119 		RevCommit a = repo.commit().create();
120 		RevCommit b1 = repo.commit(a);
121 		RevCommit b2 = repo.commit(b1);
122 		repo.update("refs/heads/checker", b2);
123 
124 		ReachabilityChecker checker = getChecker(repo);
125 
126 		assertUnreachable("unreachable from the future",
127 				checker.areAllReachable(Arrays.asList(b2), Stream.of(b1)));
128 	}
129 
130 	@Test
131 	public void unreachable_differentBranch() throws Exception {
132 		RevCommit a = repo.commit().create();
133 		RevCommit b1 = repo.commit(a);
134 		RevCommit b2 = repo.commit(b1);
135 		RevCommit c1 = repo.commit(a);
136 		repo.update("refs/heads/checker", b2);
137 
138 		ReachabilityChecker checker = getChecker(repo);
139 
140 		assertUnreachable("unreachable from different branch",
141 				checker.areAllReachable(Arrays.asList(c1), Stream.of(b2)));
142 	}
143 
144 	@Test
145 	public void reachable_longChain() throws Exception {
146 		RevCommit root = repo.commit().create();
147 		RevCommit head = root;
148 		for (int i = 0; i < 10000; i++) {
149 			head = repo.commit(head);
150 		}
151 		repo.update("refs/heads/master", head);
152 
153 		ReachabilityChecker checker = getChecker(repo);
154 
155 		assertReachable("reachable with long chain in the middle", checker
156 				.areAllReachable(Arrays.asList(root), Stream.of(head)));
157 	}
158 
159 	private static void assertReachable(String msg,
160 			Optional<RevCommit> result) {
161 		assertFalse(msg, result.isPresent());
162 	}
163 
164 	private static void assertUnreachable(String msg,
165 			Optional<RevCommit> result) {
166 		assertTrue(msg, result.isPresent());
167 	}
168 }