You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IterativeConnectivityCheckerTest.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2019, Google LLC and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * http://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.transport.connectivity;
  11. import static org.mockito.Mockito.doThrow;
  12. import static org.mockito.Mockito.verify;
  13. import java.util.Arrays;
  14. import java.util.Collections;
  15. import java.util.HashSet;
  16. import java.util.Set;
  17. import org.eclipse.jgit.errors.MissingObjectException;
  18. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  19. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  20. import org.eclipse.jgit.junit.TestRepository;
  21. import org.eclipse.jgit.lib.Constants;
  22. import org.eclipse.jgit.lib.ObjectId;
  23. import org.eclipse.jgit.lib.ProgressMonitor;
  24. import org.eclipse.jgit.revwalk.RevCommit;
  25. import org.eclipse.jgit.transport.PackParser;
  26. import org.eclipse.jgit.transport.ReceiveCommand;
  27. import org.eclipse.jgit.transport.ConnectivityChecker;
  28. import org.eclipse.jgit.transport.ConnectivityChecker.ConnectivityCheckInfo;
  29. import org.junit.Before;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.mockito.Mock;
  33. import org.mockito.junit.MockitoJUnit;
  34. import org.mockito.junit.MockitoRule;
  35. public class IterativeConnectivityCheckerTest {
  36. @Rule
  37. public MockitoRule rule = MockitoJUnit.rule();
  38. private ObjectId branchHeadObjectId;
  39. private ObjectId openRewiewObjectId;
  40. private ObjectId newCommitObjectId;
  41. private ObjectId otherHaveObjectId = ObjectId
  42. .fromString("DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF");
  43. private Set<ObjectId> advertisedHaves;
  44. @Mock
  45. private ConnectivityChecker connectivityCheckerDelegate;
  46. @Mock
  47. private ProgressMonitor pm;
  48. @Mock
  49. private PackParser parser;
  50. private RevCommit branchHeadCommitObject;
  51. private RevCommit openReviewCommitObject;
  52. private RevCommit newCommitObject;
  53. private ConnectivityCheckInfo connectivityCheckInfo;
  54. private IterativeConnectivityChecker connectivityChecker;
  55. private TestRepository tr;
  56. @Before
  57. public void setUp() throws Exception {
  58. tr = new TestRepository<>(
  59. new InMemoryRepository(new DfsRepositoryDescription("test")));
  60. connectivityChecker = new IterativeConnectivityChecker(
  61. connectivityCheckerDelegate);
  62. connectivityCheckInfo = new ConnectivityCheckInfo();
  63. connectivityCheckInfo.setParser(parser);
  64. connectivityCheckInfo.setRepository(tr.getRepository());
  65. connectivityCheckInfo.setWalk(tr.getRevWalk());
  66. branchHeadCommitObject = tr.commit().create();
  67. branchHeadObjectId = branchHeadCommitObject.getId();
  68. openReviewCommitObject = tr.commit().create();
  69. openRewiewObjectId = openReviewCommitObject.getId();
  70. advertisedHaves = wrap(branchHeadObjectId, openRewiewObjectId,
  71. otherHaveObjectId);
  72. }
  73. @Test
  74. public void testSuccessfulNewBranchBasedOnOld() throws Exception {
  75. createNewCommit(branchHeadCommitObject);
  76. connectivityCheckInfo.setCommands(
  77. Collections.singletonList(createNewBrachCommand()));
  78. connectivityChecker.checkConnectivity(connectivityCheckInfo,
  79. advertisedHaves, pm);
  80. verify(connectivityCheckerDelegate).checkConnectivity(
  81. connectivityCheckInfo,
  82. wrap(branchHeadObjectId /* as direct parent */),
  83. pm);
  84. }
  85. @Test
  86. public void testSuccessfulNewBranchBasedOnOldWithTip() throws Exception {
  87. createNewCommit(branchHeadCommitObject);
  88. connectivityCheckInfo.setCommands(
  89. Collections.singletonList(createNewBrachCommand()));
  90. connectivityChecker.setForcedHaves(wrap(openRewiewObjectId));
  91. connectivityChecker.checkConnectivity(connectivityCheckInfo,
  92. advertisedHaves, pm);
  93. verify(connectivityCheckerDelegate).checkConnectivity(
  94. connectivityCheckInfo,
  95. wrap(branchHeadObjectId /* as direct parent */,
  96. openRewiewObjectId),
  97. pm);
  98. }
  99. @Test
  100. public void testSuccessfulNewBranchMerge() throws Exception {
  101. createNewCommit(branchHeadCommitObject, openReviewCommitObject);
  102. connectivityCheckInfo.setCommands(
  103. Collections.singletonList(createNewBrachCommand()));
  104. connectivityChecker.checkConnectivity(connectivityCheckInfo,
  105. advertisedHaves, pm);
  106. verify(connectivityCheckerDelegate).checkConnectivity(
  107. connectivityCheckInfo,
  108. wrap(branchHeadObjectId /* as direct parent */,
  109. openRewiewObjectId),
  110. pm);
  111. }
  112. @Test
  113. public void testSuccessfulNewBranchBasedOnNewWithTip() throws Exception {
  114. createNewCommit();
  115. connectivityCheckInfo.setCommands(
  116. Collections.singletonList(createNewBrachCommand()));
  117. connectivityChecker.setForcedHaves(wrap(openRewiewObjectId));
  118. connectivityChecker.checkConnectivity(connectivityCheckInfo,
  119. advertisedHaves, pm);
  120. verify(connectivityCheckerDelegate).checkConnectivity(
  121. connectivityCheckInfo, wrap(openRewiewObjectId), pm);
  122. }
  123. @Test
  124. public void testSuccessfulPushOldBranch() throws Exception {
  125. createNewCommit(branchHeadCommitObject);
  126. connectivityCheckInfo.setCommands(
  127. Collections.singletonList(pushOldBranchCommand()));
  128. connectivityChecker.checkConnectivity(connectivityCheckInfo,
  129. advertisedHaves, pm);
  130. verify(connectivityCheckerDelegate).checkConnectivity(
  131. connectivityCheckInfo, wrap(branchHeadObjectId /* as direct parent */),
  132. pm);
  133. }
  134. @Test
  135. public void testSuccessfulPushOldBranchMergeCommit() throws Exception {
  136. createNewCommit(branchHeadCommitObject, openReviewCommitObject);
  137. connectivityCheckInfo.setCommands(
  138. Collections.singletonList(pushOldBranchCommand()));
  139. connectivityChecker.checkConnectivity(connectivityCheckInfo,
  140. advertisedHaves, pm);
  141. verify(connectivityCheckerDelegate).checkConnectivity(
  142. connectivityCheckInfo,
  143. wrap(branchHeadObjectId /* as direct parent */,
  144. openRewiewObjectId),
  145. pm);
  146. }
  147. @Test
  148. public void testNoChecksIfCantFindSubset() throws Exception {
  149. createNewCommit();
  150. connectivityCheckInfo.setCommands(
  151. Collections.singletonList(createNewBrachCommand()));
  152. connectivityChecker.checkConnectivity(connectivityCheckInfo,
  153. advertisedHaves, pm);
  154. verify(connectivityCheckerDelegate)
  155. .checkConnectivity(connectivityCheckInfo, advertisedHaves, pm);
  156. }
  157. @Test
  158. public void testReiterateInCaseNotSuccessful() throws Exception {
  159. createNewCommit(branchHeadCommitObject);
  160. connectivityCheckInfo.setCommands(
  161. Collections.singletonList(createNewBrachCommand()));
  162. doThrow(new MissingObjectException(branchHeadCommitObject,
  163. Constants.OBJ_COMMIT)).when(connectivityCheckerDelegate)
  164. .checkConnectivity(connectivityCheckInfo,
  165. wrap(branchHeadObjectId /* as direct parent */), pm);
  166. connectivityChecker.checkConnectivity(connectivityCheckInfo,
  167. advertisedHaves, pm);
  168. verify(connectivityCheckerDelegate)
  169. .checkConnectivity(connectivityCheckInfo, advertisedHaves, pm);
  170. }
  171. @Test
  172. public void testDependOnGrandparent() throws Exception {
  173. RevCommit grandparent = tr.commit(new RevCommit[] {});
  174. RevCommit parent = tr.commit(grandparent);
  175. createNewCommit(parent);
  176. branchHeadCommitObject = tr.commit(grandparent);
  177. branchHeadObjectId = branchHeadCommitObject.getId();
  178. tr.getRevWalk().dispose();
  179. connectivityCheckInfo.setCommands(
  180. Collections.singletonList(createNewBrachCommand()));
  181. connectivityChecker.checkConnectivity(connectivityCheckInfo,
  182. advertisedHaves, pm);
  183. verify(connectivityCheckerDelegate)
  184. .checkConnectivity(connectivityCheckInfo, advertisedHaves, pm);
  185. }
  186. private static Set<ObjectId> wrap(ObjectId... objectIds) {
  187. return new HashSet<>(Arrays.asList(objectIds));
  188. }
  189. private ReceiveCommand createNewBrachCommand() {
  190. return new ReceiveCommand(ObjectId.zeroId(), newCommitObjectId,
  191. "totally/a/new/branch");
  192. }
  193. private ReceiveCommand pushOldBranchCommand() {
  194. return new ReceiveCommand(branchHeadObjectId, newCommitObjectId,
  195. "push/to/an/old/branch");
  196. }
  197. private void createNewCommit(RevCommit... parents) throws Exception {
  198. newCommitObject = tr.commit(parents);
  199. newCommitObjectId = newCommitObject.getId();
  200. }
  201. }