blob: b4243c40f76822ba612f870ba635155f0beeee31 [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.gitiles.TestGitilesUrls.URLS;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070018import static org.junit.Assert.assertEquals;
Dave Borowitz9de65952012-08-13 16:09:45 -070019
Dave Borowitz54271462013-11-11 11:43:11 -080020import com.google.common.collect.ImmutableList;
21import com.google.common.collect.ImmutableMap;
Dave Borowitz9de65952012-08-13 16:09:45 -070022
Shawn Pearceb43b2d52013-03-18 10:55:15 -070023import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
24import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
25import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
26import org.eclipse.jgit.junit.TestRepository;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070027import org.junit.Before;
28import org.junit.Test;
Shawn Pearceb43b2d52013-03-18 10:55:15 -070029
Dave Borowitz54271462013-11-11 11:43:11 -080030import java.io.IOException;
31import java.util.Map;
32
33import javax.servlet.http.HttpServletRequest;
Shawn Pearceb43b2d52013-03-18 10:55:15 -070034
Dave Borowitz9de65952012-08-13 16:09:45 -070035/** Tests for {@link RepositoryIndexServlet}. */
Dave Borowitzd40bdf12014-04-19 19:33:56 -070036public class RepositoryIndexServletTest {
Dave Borowitz9de65952012-08-13 16:09:45 -070037 private TestRepository<DfsRepository> repo;
38 private RepositoryIndexServlet servlet;
39
Dave Borowitzd40bdf12014-04-19 19:33:56 -070040 @Before
41 public void setUp() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070042 repo = new TestRepository<DfsRepository>(
43 new InMemoryRepository(new DfsRepositoryDescription("test")));
44 servlet = new RepositoryIndexServlet(
Dave Borowitz14ce8282012-12-20 14:08:25 -080045 new TestGitilesAccess(repo.getRepository()),
Dave Borowitz8d6d6872014-03-16 15:18:14 -070046 new DefaultRenderer(),
Dave Borowitz14ce8282012-12-20 14:08:25 -080047 new TimeCache());
Dave Borowitz9de65952012-08-13 16:09:45 -070048 }
49
Dave Borowitzd40bdf12014-04-19 19:33:56 -070050 @Test
51 public void empty() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070052 Map<String, ?> data = buildData();
53 assertEquals(ImmutableList.of(), data.get("branches"));
54 assertEquals(ImmutableList.of(), data.get("tags"));
55 }
56
Dave Borowitzd40bdf12014-04-19 19:33:56 -070057 @Test
58 public void branchesAndTags() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070059 repo.branch("refs/heads/foo").commit().create();
60 repo.branch("refs/heads/bar").commit().create();
61 repo.branch("refs/tags/baz").commit().create();
62 repo.branch("refs/nope/quux").commit().create();
63 Map<String, ?> data = buildData();
64
65 assertEquals(
66 ImmutableList.of(
67 ref("/b/test/+/bar", "bar"),
68 ref("/b/test/+/foo", "foo")),
69 data.get("branches"));
70 assertEquals(
71 ImmutableList.of(
72 ref("/b/test/+/baz", "baz")),
73 data.get("tags"));
74 }
75
Dave Borowitzd40bdf12014-04-19 19:33:56 -070076 @Test
77 public void ambiguousBranch() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070078 repo.branch("refs/heads/foo").commit().create();
79 repo.branch("refs/heads/bar").commit().create();
80 repo.branch("refs/tags/foo").commit().create();
81 Map<String, ?> data = buildData();
82
83 assertEquals(
84 ImmutableList.of(
85 ref("/b/test/+/bar", "bar"),
86 ref("/b/test/+/refs/heads/foo", "foo")),
87 data.get("branches"));
88 assertEquals(
89 ImmutableList.of(
90 // refs/tags/ is searched before refs/heads/, so this does not
91 // appear ambiguous.
92 ref("/b/test/+/foo", "foo")),
93 data.get("tags"));
94 }
95
Dave Borowitzd40bdf12014-04-19 19:33:56 -070096 @Test
97 public void ambiguousRelativeToNonBranchOrTag() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070098 repo.branch("refs/foo").commit().create();
99 repo.branch("refs/heads/foo").commit().create();
100 repo.branch("refs/tags/foo").commit().create();
101 Map<String, ?> data = buildData();
102
103 assertEquals(
104 ImmutableList.of(
105 ref("/b/test/+/refs/heads/foo", "foo")),
106 data.get("branches"));
107 assertEquals(
108 ImmutableList.of(
109 ref("/b/test/+/refs/tags/foo", "foo")),
110 data.get("tags"));
111 }
112
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700113 @Test
114 public void refsHeads() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -0700115 repo.branch("refs/heads/foo").commit().create();
116 repo.branch("refs/heads/refs/heads/foo").commit().create();
117 Map<String, ?> data = buildData();
118
119 assertEquals(
120 ImmutableList.of(
121 ref("/b/test/+/foo", "foo"),
122 ref("/b/test/+/refs/heads/refs/heads/foo", "refs/heads/foo")),
123 data.get("branches"));
124 }
125
126 private Map<String, ?> buildData() throws IOException {
127 HttpServletRequest req = FakeHttpServletRequest.newRequest(repo.getRepository());
128 ViewFilter.setView(req, GitilesView.repositoryIndex()
129 .setHostName(URLS.getHostName(req))
130 .setServletPath(req.getServletPath())
131 .setRepositoryName("test")
132 .build());
133 return servlet.buildData(req);
134 }
135
136 private Map<String, String> ref(String url, String name) {
137 return ImmutableMap.of("url", url, "name", name);
138 }
139}