| Jonathan Nieder | c16518b | 2019-11-25 15:48:35 -0800 | [diff] [blame] | 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| Ivan Frade | 553e987 | 2019-05-24 21:19:48 -0700 | [diff] [blame] | 14 | package com.google.gitiles; |
| Ivan Frade | c2a110d | 2019-04-08 15:51:03 -0700 | [diff] [blame] | 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.util.Map; |
| 20 | import java.util.Set; |
| 21 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 22 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| 23 | import org.eclipse.jgit.junit.TestRepository; |
| 24 | import org.eclipse.jgit.lib.Config; |
| 25 | import org.eclipse.jgit.lib.ObjectId; |
| 26 | import org.eclipse.jgit.revwalk.RevCommit; |
| 27 | import org.eclipse.jgit.revwalk.RevWalk; |
| 28 | import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; |
| 29 | import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; |
| 30 | import org.junit.After; |
| 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | import org.junit.runners.JUnit4; |
| 35 | |
| 36 | @RunWith(JUnit4.class) |
| 37 | public class VisibilityCacheTest { |
| 38 | |
| 39 | private InMemoryRepository repo; |
| 40 | private GitilesAccess access = new FakeGitilesAccess(); |
| 41 | |
| 42 | private RevCommit baseCommit; |
| 43 | private RevCommit commit1; |
| 44 | private RevCommit commit2; |
| 45 | private RevCommit commitA; |
| 46 | private RevCommit commitB; |
| 47 | private RevCommit commitC; |
| 48 | |
| 49 | private VisibilityCache visibilityCache; |
| 50 | private RevWalk walk; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() throws Exception { |
| 54 | /** |
| Ivan Frade | c2a110d | 2019-04-08 15:51:03 -0700 | [diff] [blame] | 55 | * |
| Ivan Frade | 553e987 | 2019-05-24 21:19:48 -0700 | [diff] [blame] | 56 | * |
| 57 | * <pre> |
| 58 | * commitC |
| 59 | * | |
| 60 | * commit2 commitB |
| 61 | * | | |
| 62 | * commit1 commitA <--- refs/tags/v0.1 |
| 63 | * \ / |
| 64 | * \ / |
| 65 | * baseCommit |
| 66 | * </pre> |
| Ivan Frade | c2a110d | 2019-04-08 15:51:03 -0700 | [diff] [blame] | 67 | */ |
| 68 | repo = new InMemoryRepository(new DfsRepositoryDescription()); |
| David Pursehouse | 68a7bd5 | 2019-06-01 15:16:34 +0900 | [diff] [blame] | 69 | try (TestRepository<InMemoryRepository> git = new TestRepository<>(repo)) { |
| 70 | baseCommit = git.commit().message("baseCommit").create(); |
| 71 | commit1 = git.commit().parent(baseCommit).message("commit1").create(); |
| 72 | commit2 = git.commit().parent(commit1).message("commit2").create(); |
| Ivan Frade | c2a110d | 2019-04-08 15:51:03 -0700 | [diff] [blame] | 73 | |
| David Pursehouse | 68a7bd5 | 2019-06-01 15:16:34 +0900 | [diff] [blame] | 74 | commitA = git.commit().parent(baseCommit).message("commitA").create(); |
| 75 | commitB = git.commit().parent(commitA).message("commitB").create(); |
| 76 | commitC = git.commit().parent(commitB).message("commitC").create(); |
| Ivan Frade | c2a110d | 2019-04-08 15:51:03 -0700 | [diff] [blame] | 77 | |
| David Pursehouse | 68a7bd5 | 2019-06-01 15:16:34 +0900 | [diff] [blame] | 78 | git.update("master", commit2); |
| 79 | git.update("refs/tags/v0.1", commitA); |
| 80 | } |
| Ivan Frade | c2a110d | 2019-04-08 15:51:03 -0700 | [diff] [blame] | 81 | |
| Ivan Frade | 9ca395d | 2019-11-25 13:29:49 -0800 | [diff] [blame] | 82 | visibilityCache = new VisibilityCache(); |
| Ivan Frade | c2a110d | 2019-04-08 15:51:03 -0700 | [diff] [blame] | 83 | walk = new RevWalk(repo); |
| 84 | walk.setRetainBody(false); |
| 85 | } |
| 86 | |
| 87 | @After |
| 88 | public void tearDown() { |
| 89 | repo.close(); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void isTip() throws IOException { |
| 94 | ObjectId[] known = new ObjectId[0]; |
| 95 | assertThat(visibilityCache.isVisible(repo, walk, access, commit2.getId(), known)).isTrue(); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void reachableFromHeads() throws Exception { |
| 100 | ObjectId[] known = new ObjectId[0]; |
| 101 | assertThat(visibilityCache.isVisible(repo, walk, access, commit1.getId(), known)).isTrue(); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void reachableFromTags() throws Exception { |
| 106 | ObjectId[] known = new ObjectId[0]; |
| 107 | assertThat(visibilityCache.isVisible(repo, walk, access, commitA.getId(), known)).isTrue(); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void unreachableFromAnyTip() throws Exception { |
| 112 | ObjectId[] known = new ObjectId[0]; |
| 113 | assertThat(visibilityCache.isVisible(repo, walk, access, commitB.getId(), known)).isFalse(); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void reachableFromAnotherId() throws Exception { |
| 118 | ObjectId[] known = new ObjectId[] {commitC.getId()}; |
| 119 | assertThat(visibilityCache.isVisible(repo, walk, access, commitB.getId(), known)).isTrue(); |
| 120 | } |
| 121 | |
| 122 | private static class FakeGitilesAccess implements GitilesAccess { |
| 123 | @Override |
| 124 | public Map<String, RepositoryDescription> listRepositories(String prefix, Set<String> branches) |
| 125 | throws ServiceNotEnabledException, ServiceNotAuthorizedException, IOException { |
| 126 | throw new UnsupportedOperationException(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Object getUserKey() { |
| 131 | return "Test"; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public String getRepositoryName() { |
| 136 | return "VisibilityCache-Test"; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public RepositoryDescription getRepositoryDescription() throws IOException { |
| 141 | throw new UnsupportedOperationException(); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public Config getConfig() throws IOException { |
| 146 | throw new UnsupportedOperationException(); |
| 147 | } |
| 148 | } |
| 149 | } |