| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import static com.google.gitiles.TestGitilesUrls.URLS; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 19 | |
| Dave Borowitz | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 22 | |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 23 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 24 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 25 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| 26 | import org.eclipse.jgit.junit.TestRepository; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 29 | |
| Dave Borowitz | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 30 | import java.io.IOException; |
| 31 | import java.util.Map; |
| 32 | |
| 33 | import javax.servlet.http.HttpServletRequest; |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 34 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 35 | /** Tests for {@link RepositoryIndexServlet}. */ |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 36 | public class RepositoryIndexServletTest { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 37 | private TestRepository<DfsRepository> repo; |
| 38 | private RepositoryIndexServlet servlet; |
| 39 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 40 | @Before |
| 41 | public void setUp() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 42 | repo = new TestRepository<DfsRepository>( |
| 43 | new InMemoryRepository(new DfsRepositoryDescription("test"))); |
| 44 | servlet = new RepositoryIndexServlet( |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 45 | new TestGitilesAccess(repo.getRepository()), |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 46 | new DefaultRenderer(), |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 47 | new TimeCache()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 50 | @Test |
| 51 | public void empty() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 52 | Map<String, ?> data = buildData(); |
| 53 | assertEquals(ImmutableList.of(), data.get("branches")); |
| 54 | assertEquals(ImmutableList.of(), data.get("tags")); |
| 55 | } |
| 56 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 57 | @Test |
| 58 | public void branchesAndTags() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 59 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 76 | @Test |
| 77 | public void ambiguousBranch() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 78 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 96 | @Test |
| 97 | public void ambiguousRelativeToNonBranchOrTag() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 98 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 113 | @Test |
| 114 | public void refsHeads() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 115 | 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 | } |