blob: e271e714e38f2f52de5adf7269806a48743dcf15 [file] [log] [blame]
Jonathan Niederc16518b2019-11-25 15:48:35 -08001// 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 Frade553e9872019-05-24 21:19:48 -070014package com.google.gitiles;
Ivan Fradec2a110d2019-04-08 15:51:03 -070015
16import static com.google.common.truth.Truth.assertThat;
17
18import java.io.IOException;
19import java.util.Map;
20import java.util.Set;
21import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
22import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
23import org.eclipse.jgit.junit.TestRepository;
24import org.eclipse.jgit.lib.Config;
25import org.eclipse.jgit.lib.ObjectId;
26import org.eclipse.jgit.revwalk.RevCommit;
27import org.eclipse.jgit.revwalk.RevWalk;
28import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
29import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
30import org.junit.After;
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.junit.runners.JUnit4;
35
36@RunWith(JUnit4.class)
37public 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 Fradec2a110d2019-04-08 15:51:03 -070055 *
Ivan Frade553e9872019-05-24 21:19:48 -070056 *
57 * <pre>
58 * commitC
59 * |
60 * commit2 commitB
61 * | |
62 * commit1 commitA <--- refs/tags/v0.1
63 * \ /
64 * \ /
65 * baseCommit
66 * </pre>
Ivan Fradec2a110d2019-04-08 15:51:03 -070067 */
68 repo = new InMemoryRepository(new DfsRepositoryDescription());
David Pursehouse68a7bd52019-06-01 15:16:34 +090069 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 Fradec2a110d2019-04-08 15:51:03 -070073
David Pursehouse68a7bd52019-06-01 15:16:34 +090074 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 Fradec2a110d2019-04-08 15:51:03 -070077
David Pursehouse68a7bd52019-06-01 15:16:34 +090078 git.update("master", commit2);
79 git.update("refs/tags/v0.1", commitA);
80 }
Ivan Fradec2a110d2019-04-08 15:51:03 -070081
Ivan Frade9ca395d2019-11-25 13:29:49 -080082 visibilityCache = new VisibilityCache();
Ivan Fradec2a110d2019-04-08 15:51:03 -070083 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}