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.

ReachabilityCheckerTestCase.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.revwalk;
  11. import static org.junit.Assert.assertFalse;
  12. import static org.junit.Assert.assertTrue;
  13. import java.util.Arrays;
  14. import java.util.Optional;
  15. import java.util.stream.Stream;
  16. import org.eclipse.jgit.internal.storage.file.FileRepository;
  17. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  18. import org.eclipse.jgit.junit.TestRepository;
  19. import org.eclipse.jgit.revwalk.ReachabilityChecker;
  20. import org.eclipse.jgit.revwalk.RevCommit;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. public abstract class ReachabilityCheckerTestCase
  24. extends LocalDiskRepositoryTestCase {
  25. protected abstract ReachabilityChecker getChecker(
  26. TestRepository<FileRepository> repository) throws Exception;
  27. TestRepository<FileRepository> repo;
  28. /** {@inheritDoc} */
  29. @Override
  30. @Before
  31. public void setUp() throws Exception {
  32. super.setUp();
  33. FileRepository db = createWorkRepository();
  34. repo = new TestRepository<>(db);
  35. }
  36. @Test
  37. public void reachable() throws Exception {
  38. RevCommit a = repo.commit().create();
  39. RevCommit b1 = repo.commit(a);
  40. RevCommit b2 = repo.commit(b1);
  41. RevCommit c1 = repo.commit(a);
  42. RevCommit c2 = repo.commit(c1);
  43. repo.update("refs/heads/checker", b2);
  44. ReachabilityChecker checker = getChecker(repo);
  45. assertReachable("reachable from one tip",
  46. checker.areAllReachable(Arrays.asList(a), Stream.of(c2)));
  47. assertReachable("reachable from another tip",
  48. checker.areAllReachable(Arrays.asList(a), Stream.of(b2)));
  49. assertReachable("reachable from itself",
  50. checker.areAllReachable(Arrays.asList(a), Stream.of(b2)));
  51. }
  52. @Test
  53. public void reachable_merge() throws Exception {
  54. RevCommit a = repo.commit().create();
  55. RevCommit b1 = repo.commit(a);
  56. RevCommit b2 = repo.commit(b1);
  57. RevCommit c1 = repo.commit(a);
  58. RevCommit c2 = repo.commit(c1);
  59. RevCommit merge = repo.commit(c2, b2);
  60. repo.update("refs/heads/checker", merge);
  61. ReachabilityChecker checker = getChecker(repo);
  62. assertReachable("reachable through one branch",
  63. checker.areAllReachable(Arrays.asList(b1),
  64. Stream.of(merge)));
  65. assertReachable("reachable through another branch",
  66. checker.areAllReachable(Arrays.asList(c1),
  67. Stream.of(merge)));
  68. assertReachable("reachable, before the branching",
  69. checker.areAllReachable(Arrays.asList(a),
  70. Stream.of(merge)));
  71. }
  72. @Test
  73. public void unreachable_isLaterCommit() throws Exception {
  74. RevCommit a = repo.commit().create();
  75. RevCommit b1 = repo.commit(a);
  76. RevCommit b2 = repo.commit(b1);
  77. repo.update("refs/heads/checker", b2);
  78. ReachabilityChecker checker = getChecker(repo);
  79. assertUnreachable("unreachable from the future",
  80. checker.areAllReachable(Arrays.asList(b2), Stream.of(b1)));
  81. }
  82. @Test
  83. public void unreachable_differentBranch() throws Exception {
  84. RevCommit a = repo.commit().create();
  85. RevCommit b1 = repo.commit(a);
  86. RevCommit b2 = repo.commit(b1);
  87. RevCommit c1 = repo.commit(a);
  88. repo.update("refs/heads/checker", b2);
  89. ReachabilityChecker checker = getChecker(repo);
  90. assertUnreachable("unreachable from different branch",
  91. checker.areAllReachable(Arrays.asList(c1), Stream.of(b2)));
  92. }
  93. @Test
  94. public void reachable_longChain() throws Exception {
  95. RevCommit root = repo.commit().create();
  96. RevCommit head = root;
  97. for (int i = 0; i < 10000; i++) {
  98. head = repo.commit(head);
  99. }
  100. repo.update("refs/heads/master", head);
  101. ReachabilityChecker checker = getChecker(repo);
  102. assertReachable("reachable with long chain in the middle", checker
  103. .areAllReachable(Arrays.asList(root), Stream.of(head)));
  104. }
  105. private static void assertReachable(String msg,
  106. Optional<RevCommit> result) {
  107. assertFalse(msg, result.isPresent());
  108. }
  109. private static void assertUnreachable(String msg,
  110. Optional<RevCommit> result) {
  111. assertTrue(msg, result.isPresent());
  112. }
  113. }