blob: f239b8267a77bc3e0af8c78fec89e47941aacf33 [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;
18
Dave Borowitz54271462013-11-11 11:43:11 -080019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableMap;
Dave Borowitz9de65952012-08-13 16:09:45 -070021
Shawn Pearceb43b2d52013-03-18 10:55:15 -070022import junit.framework.TestCase;
23
24import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
25import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
26import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
27import org.eclipse.jgit.junit.TestRepository;
28
Dave Borowitz54271462013-11-11 11:43:11 -080029import java.io.IOException;
30import java.util.Map;
31
32import javax.servlet.http.HttpServletRequest;
Shawn Pearceb43b2d52013-03-18 10:55:15 -070033
Dave Borowitz9de65952012-08-13 16:09:45 -070034/** Tests for {@link RepositoryIndexServlet}. */
35public class RepositoryIndexServletTest extends TestCase {
36 private TestRepository<DfsRepository> repo;
37 private RepositoryIndexServlet servlet;
38
39 @Override
40 protected void setUp() throws Exception {
41 repo = new TestRepository<DfsRepository>(
42 new InMemoryRepository(new DfsRepositoryDescription("test")));
43 servlet = new RepositoryIndexServlet(
Dave Borowitz14ce8282012-12-20 14:08:25 -080044 new TestGitilesAccess(repo.getRepository()),
Dave Borowitz8d6d6872014-03-16 15:18:14 -070045 new DefaultRenderer(),
Dave Borowitz14ce8282012-12-20 14:08:25 -080046 new TimeCache());
Dave Borowitz9de65952012-08-13 16:09:45 -070047 }
48
49 public void testEmpty() throws Exception {
50 Map<String, ?> data = buildData();
51 assertEquals(ImmutableList.of(), data.get("branches"));
52 assertEquals(ImmutableList.of(), data.get("tags"));
53 }
54
55 public void testBranchesAndTags() throws Exception {
56 repo.branch("refs/heads/foo").commit().create();
57 repo.branch("refs/heads/bar").commit().create();
58 repo.branch("refs/tags/baz").commit().create();
59 repo.branch("refs/nope/quux").commit().create();
60 Map<String, ?> data = buildData();
61
62 assertEquals(
63 ImmutableList.of(
64 ref("/b/test/+/bar", "bar"),
65 ref("/b/test/+/foo", "foo")),
66 data.get("branches"));
67 assertEquals(
68 ImmutableList.of(
69 ref("/b/test/+/baz", "baz")),
70 data.get("tags"));
71 }
72
73 public void testAmbiguousBranch() throws Exception {
74 repo.branch("refs/heads/foo").commit().create();
75 repo.branch("refs/heads/bar").commit().create();
76 repo.branch("refs/tags/foo").commit().create();
77 Map<String, ?> data = buildData();
78
79 assertEquals(
80 ImmutableList.of(
81 ref("/b/test/+/bar", "bar"),
82 ref("/b/test/+/refs/heads/foo", "foo")),
83 data.get("branches"));
84 assertEquals(
85 ImmutableList.of(
86 // refs/tags/ is searched before refs/heads/, so this does not
87 // appear ambiguous.
88 ref("/b/test/+/foo", "foo")),
89 data.get("tags"));
90 }
91
92 public void testAmbiguousRelativeToNonBranchOrTag() throws Exception {
93 repo.branch("refs/foo").commit().create();
94 repo.branch("refs/heads/foo").commit().create();
95 repo.branch("refs/tags/foo").commit().create();
96 Map<String, ?> data = buildData();
97
98 assertEquals(
99 ImmutableList.of(
100 ref("/b/test/+/refs/heads/foo", "foo")),
101 data.get("branches"));
102 assertEquals(
103 ImmutableList.of(
104 ref("/b/test/+/refs/tags/foo", "foo")),
105 data.get("tags"));
106 }
107
108 public void testRefsHeads() throws Exception {
109 repo.branch("refs/heads/foo").commit().create();
110 repo.branch("refs/heads/refs/heads/foo").commit().create();
111 Map<String, ?> data = buildData();
112
113 assertEquals(
114 ImmutableList.of(
115 ref("/b/test/+/foo", "foo"),
116 ref("/b/test/+/refs/heads/refs/heads/foo", "refs/heads/foo")),
117 data.get("branches"));
118 }
119
120 private Map<String, ?> buildData() throws IOException {
121 HttpServletRequest req = FakeHttpServletRequest.newRequest(repo.getRepository());
122 ViewFilter.setView(req, GitilesView.repositoryIndex()
123 .setHostName(URLS.getHostName(req))
124 .setServletPath(req.getServletPath())
125 .setRepositoryName("test")
126 .build());
127 return servlet.buildData(req);
128 }
129
130 private Map<String, String> ref(String url, String name) {
131 return ImmutableMap.of("url", url, "name", name);
132 }
133}