| 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; |
| 18 | |
| Dave Borowitz | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 22 | import junit.framework.TestCase; |
| 23 | |
| 24 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 25 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 26 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| 27 | import org.eclipse.jgit.junit.TestRepository; |
| 28 | |
| Dave Borowitz | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 29 | import java.io.IOException; |
| 30 | import java.util.Map; |
| 31 | |
| 32 | import javax.servlet.http.HttpServletRequest; |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 33 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 34 | /** Tests for {@link RepositoryIndexServlet}. */ |
| 35 | public 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 Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 44 | new TestGitilesAccess(repo.getRepository()), |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 45 | new DefaultRenderer(), |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 46 | new TimeCache()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 47 | } |
| 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 | } |