blob: 27e10cc1f2c398553ec980f0c53c6c3306f31ba5 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// Copyright 2012 Google Inc. All Rights Reserved.
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// http://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.
14
15package com.google.gitiles;
16
17import static com.google.common.base.Preconditions.checkNotNull;
18
Shawn Pearcec709c4c2015-08-28 15:30:42 -070019import com.google.common.base.CharMatcher;
AJ Ross001ea9b2016-08-23 13:40:04 -070020import com.google.common.collect.ImmutableList;
Dave Borowitzded109a2014-03-03 15:25:39 -050021import com.google.common.collect.ImmutableMap;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070022import java.util.Collections;
Dave Borowitz9de65952012-08-13 16:09:45 -070023import java.util.Map;
24import java.util.Set;
Dave Borowitz9de65952012-08-13 16:09:45 -070025import javax.servlet.http.HttpServletRequest;
Dave Borowitz3b744b12016-08-19 16:11:10 -040026import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
27import org.eclipse.jgit.lib.Config;
Dave Borowitz9de65952012-08-13 16:09:45 -070028
29/** Gitiles access for testing. */
30public class TestGitilesAccess implements GitilesAccess.Factory {
31 private final DfsRepository repo;
32
33 public TestGitilesAccess(DfsRepository repo) {
34 this.repo = checkNotNull(repo);
35 }
36
37 @Override
38 public GitilesAccess forRequest(final HttpServletRequest req) {
39 return new GitilesAccess() {
40 @Override
Dave Borowitzcf38c032016-05-02 11:06:23 -040041 public Map<String, RepositoryDescription> listRepositories(
42 String prefix, Set<String> branches) {
Shawn Pearcec709c4c2015-08-28 15:30:42 -070043 String name = repo.getDescription().getRepositoryName();
44 if (prefix != null) {
45 String pattern = CharMatcher.is('/').trimFrom(prefix) + '/';
46 if (!name.startsWith(pattern)) {
47 return Collections.emptyMap();
48 }
49 }
Dave Borowitzd91bdf72013-01-10 20:07:32 -080050 if (branches != null && !branches.isEmpty()) {
51 throw new UnsupportedOperationException("branches set not yet supported");
52 }
53 RepositoryDescription desc = new RepositoryDescription();
Shawn Pearcec709c4c2015-08-28 15:30:42 -070054 desc.name = name;
Dave Borowitzd91bdf72013-01-10 20:07:32 -080055 desc.cloneUrl = TestGitilesUrls.URLS.getBaseGitUrl(req) + "/" + desc.name;
56 return ImmutableMap.of(desc.name, desc);
Dave Borowitz9de65952012-08-13 16:09:45 -070057 }
58
59 @Override
60 public Object getUserKey() {
61 return "a user";
62 }
63
64 @Override
65 public String getRepositoryName() {
66 return repo.getDescription().getRepositoryName();
67 }
68
69 @Override
70 public RepositoryDescription getRepositoryDescription() {
71 RepositoryDescription d = new RepositoryDescription();
72 d.name = getRepositoryName();
73 d.description = "a test data set";
74 d.cloneUrl = TestGitilesUrls.URLS.getBaseGitUrl(req) + "/" + d.name;
75 return d;
76 }
Dave Borowitzded109a2014-03-03 15:25:39 -050077
78 @Override
79 public Config getConfig() {
AJ Ross001ea9b2016-08-23 13:40:04 -070080 Config config = new Config();
Shawn Pearceb55cf2b2017-06-29 21:56:37 -070081 config.setBoolean("markdown", null, "blocknote", true);
82 config.setBoolean("markdown", null, "multicolumn", true);
83 config.setBoolean("markdown", null, "namedanchor", true);
84 config.setBoolean("markdown", null, "smartquote", true);
AJ Ross001ea9b2016-08-23 13:40:04 -070085 config.setStringList(
86 "gitiles", null, "allowOriginRegex", ImmutableList.of("http://localhost"));
87 return config;
Dave Borowitzded109a2014-03-03 15:25:39 -050088 }
Dave Borowitz9de65952012-08-13 16:09:45 -070089 };
90 }
91}