| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 1 | // Copyright (C) 2014 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 | |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame^] | 17 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame^] | 19 | import static org.junit.Assert.assertNull; |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 20 | |
| 21 | import com.google.common.collect.ImmutableList; |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame^] | 22 | import com.google.common.io.BaseEncoding; |
| 23 | import com.google.common.net.HttpHeaders; |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 24 | import com.google.template.soy.data.SoyListData; |
| 25 | import com.google.template.soy.data.restricted.StringData; |
| 26 | |
| 27 | import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit; |
| 28 | import org.eclipse.jgit.dircache.DirCacheEntry; |
| 29 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 30 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 31 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| 32 | import org.eclipse.jgit.junit.TestRepository; |
| 33 | import org.eclipse.jgit.lib.FileMode; |
| 34 | import org.eclipse.jgit.lib.ObjectId; |
| 35 | import org.eclipse.jgit.revwalk.RevBlob; |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | |
| 39 | import java.net.URL; |
| 40 | import java.util.List; |
| 41 | import java.util.Map; |
| 42 | |
| 43 | /** Tests for {@PathServlet}. */ |
| 44 | @SuppressWarnings("unchecked") |
| 45 | public class PathServletTest { |
| 46 | private static final Renderer RENDERER = |
| 47 | new DefaultRenderer("/+static", ImmutableList.<URL> of(), "Test"); |
| 48 | |
| 49 | private TestRepository<DfsRepository> repo; |
| 50 | private PathServlet servlet; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() throws Exception { |
| 54 | DfsRepository r = new InMemoryRepository(new DfsRepositoryDescription("repo")); |
| 55 | repo = new TestRepository<DfsRepository>(r); |
| 56 | servlet = new PathServlet( |
| 57 | new TestGitilesAccess(repo.getRepository()), RENDERER, TestGitilesUrls.URLS); |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void rootTreeHtml() throws Exception { |
| 62 | repo.branch("master").commit().add("foo", "contents").create(); |
| 63 | |
| 64 | Map<String, ?> data = buildData("/repo/+/master/"); |
| 65 | assertEquals("TREE", data.get("type")); |
| 66 | List<Map<String, ?>> entries = getTreeEntries(data); |
| 67 | assertEquals(1, entries.size()); |
| 68 | assertEquals("foo", entries.get(0).get("name")); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void subTreeHtml() throws Exception { |
| 73 | repo.branch("master").commit() |
| 74 | .add("foo/bar", "bar contents") |
| 75 | .add("baz", "baz contents") |
| 76 | .create(); |
| 77 | |
| 78 | Map<String, ?> data = buildData("/repo/+/master/"); |
| 79 | assertEquals("TREE", data.get("type")); |
| 80 | List<Map<String, ?>> entries = getTreeEntries(data); |
| 81 | assertEquals(2, entries.size()); |
| 82 | assertEquals("baz", entries.get(0).get("name")); |
| 83 | assertEquals("foo/", entries.get(1).get("name")); |
| 84 | |
| 85 | data = buildData("/repo/+/master/foo"); |
| 86 | assertEquals("TREE", data.get("type")); |
| 87 | entries = getTreeEntries(data); |
| 88 | assertEquals(1, entries.size()); |
| 89 | assertEquals("bar", entries.get(0).get("name")); |
| 90 | |
| 91 | data = buildData("/repo/+/master/foo/"); |
| 92 | assertEquals("TREE", data.get("type")); |
| 93 | entries = getTreeEntries(data); |
| 94 | assertEquals(1, entries.size()); |
| 95 | assertEquals("bar", entries.get(0).get("name")); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void fileHtml() throws Exception { |
| 100 | repo.branch("master").commit().add("foo", "foo\ncontents\n").create(); |
| 101 | |
| 102 | Map<String, ?> data = buildData("/repo/+/master/foo"); |
| 103 | assertEquals("REGULAR_FILE", data.get("type")); |
| 104 | |
| 105 | SoyListData lines = (SoyListData) getBlobData(data).get("lines"); |
| 106 | assertEquals(2, lines.length()); |
| 107 | |
| 108 | SoyListData spans = lines.getListData(0); |
| 109 | assertEquals(1, spans.length()); |
| 110 | assertEquals(StringData.forValue("pln"), spans.getMapData(0).get("classes")); |
| 111 | assertEquals(StringData.forValue("foo"), spans.getMapData(0).get("text")); |
| 112 | |
| 113 | spans = lines.getListData(1); |
| 114 | assertEquals(1, spans.length()); |
| 115 | assertEquals(StringData.forValue("pln"), spans.getMapData(0).get("classes")); |
| 116 | assertEquals(StringData.forValue("contents"), spans.getMapData(0).get("text")); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void symlinkHtml() throws Exception { |
| 121 | final RevBlob link = repo.blob("foo"); |
| 122 | repo.branch("master").commit().add("foo", "contents") |
| 123 | .edit(new PathEdit("bar") { |
| 124 | @Override |
| 125 | public void apply(DirCacheEntry ent) { |
| 126 | ent.setFileMode(FileMode.SYMLINK); |
| 127 | ent.setObjectId(link); |
| 128 | } |
| 129 | }).create(); |
| 130 | |
| 131 | Map<String, ?> data = buildData("/repo/+/master/bar"); |
| 132 | assertEquals("SYMLINK", data.get("type")); |
| 133 | assertEquals("foo", getBlobData(data).get("target")); |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void gitlinkHtml() throws Exception { |
| 138 | String gitmodules = "[submodule \"gitiles\"]\n" |
| 139 | + " path = gitiles\n" |
| 140 | + " url = https://gerrit.googlesource.com/gitiles\n"; |
| 141 | final String gitilesSha = "2b2f34bba3c2be7e2506ce6b1f040949da350cf9"; |
| 142 | repo.branch("master").commit().add(".gitmodules", gitmodules) |
| 143 | .edit(new PathEdit("gitiles") { |
| 144 | @Override |
| 145 | public void apply(DirCacheEntry ent) { |
| 146 | ent.setFileMode(FileMode.GITLINK); |
| 147 | ent.setObjectId(ObjectId.fromString(gitilesSha)); |
| 148 | } |
| 149 | }).create(); |
| 150 | |
| 151 | Map<String, ?> data = buildData("/repo/+/master/gitiles"); |
| 152 | assertEquals("GITLINK", data.get("type")); |
| 153 | |
| 154 | Map<String, ?> linkData = getBlobData(data); |
| 155 | assertEquals(gitilesSha, linkData.get("sha")); |
| 156 | assertEquals("https://gerrit.googlesource.com/gitiles", linkData.get("remoteUrl")); |
| 157 | assertEquals("https://gerrit.googlesource.com/gitiles", linkData.get("httpUrl")); |
| 158 | } |
| 159 | |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame^] | 160 | @Test |
| 161 | public void blobText() throws Exception { |
| 162 | repo.branch("master").commit().add("foo", "contents").create(); |
| 163 | String text = buildText("/repo/+/master/foo?format=TEXT", "100644"); |
| 164 | assertEquals("contents", new String(BaseEncoding.base64().decode(text), UTF_8)); |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void symlinkText() throws Exception { |
| 169 | final RevBlob link = repo.blob("foo"); |
| 170 | repo.branch("master").commit() |
| 171 | .edit(new PathEdit("baz") { |
| 172 | @Override |
| 173 | public void apply(DirCacheEntry ent) { |
| 174 | ent.setFileMode(FileMode.SYMLINK); |
| 175 | ent.setObjectId(link); |
| 176 | } |
| 177 | }).create(); |
| 178 | String text = buildText("/repo/+/master/baz?format=TEXT", "120000"); |
| 179 | assertEquals("foo", new String(BaseEncoding.base64().decode(text), UTF_8)); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void nonBlobText() throws Exception { |
| 184 | String gitmodules = "[submodule \"gitiles\"]\n" |
| 185 | + " path = gitiles\n" |
| 186 | + " url = https://gerrit.googlesource.com/gitiles\n"; |
| 187 | final String gitilesSha = "2b2f34bba3c2be7e2506ce6b1f040949da350cf9"; |
| 188 | repo.branch("master").commit() |
| 189 | .add("foo/bar", "contents") |
| 190 | .add(".gitmodules", gitmodules) |
| 191 | .edit(new PathEdit("gitiles") { |
| 192 | @Override |
| 193 | public void apply(DirCacheEntry ent) { |
| 194 | ent.setFileMode(FileMode.GITLINK); |
| 195 | ent.setObjectId(ObjectId.fromString(gitilesSha)); |
| 196 | } |
| 197 | }).create(); |
| 198 | |
| 199 | assertNotFound("/repo/+/master/nonexistent?format=TEXT"); |
| 200 | assertNotFound("/repo/+/master/?format=TEXT"); |
| 201 | assertNotFound("/repo/+/master/foo?format=TEXT"); |
| 202 | assertNotFound("/repo/+/master/foo/?format=TEXT"); |
| 203 | assertNotFound("/repo/+/master/gitiles?format=TEXT"); |
| 204 | } |
| 205 | |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 206 | private Map<String, ?> getBlobData(Map<String, ?> data) { |
| 207 | return ((Map<String, Map<String, ?>>) data).get("data"); |
| 208 | } |
| 209 | |
| 210 | private List<Map<String, ?>> getTreeEntries(Map<String, ?> data) { |
| 211 | return ((Map<String, List<Map<String, ?>>>) data.get("data")).get("entries"); |
| 212 | } |
| 213 | |
| 214 | private TestViewFilter.Result service(String pathAndQuery) throws Exception { |
| 215 | TestViewFilter.Result res = TestViewFilter.service(repo, pathAndQuery); |
| 216 | assertEquals(200, res.getResponse().getStatus()); |
| 217 | assertEquals(GitilesView.Type.PATH, res.getView().getType()); |
| 218 | servlet.service(res.getRequest(), res.getResponse()); |
| 219 | return res; |
| 220 | } |
| 221 | |
| Dave Borowitz | 4f56870 | 2014-05-01 19:54:57 -0700 | [diff] [blame^] | 222 | private void assertNotFound(String pathAndQuery) throws Exception { |
| 223 | assertEquals(404, service(pathAndQuery).getResponse().getStatus()); |
| 224 | } |
| 225 | |
| 226 | private String buildText(String pathAndQuery, String expectedMode) throws Exception { |
| 227 | TestViewFilter.Result res = service(pathAndQuery); |
| 228 | assertNull(res.getResponse().getHeader(HttpHeaders.CONTENT_TYPE)); |
| 229 | assertEquals(expectedMode, res.getResponse().getHeader(PathServlet.MODE_HEADER)); |
| 230 | return res.getResponse().getActualBodyString(); |
| 231 | } |
| 232 | |
| Dave Borowitz | b7fd3f3 | 2014-05-01 12:31:25 -0700 | [diff] [blame] | 233 | private Map<String, ?> buildData(String pathAndQuery) throws Exception { |
| 234 | // Render the page through Soy to ensure templates are valid, then return |
| 235 | // the Soy data for introspection. |
| 236 | return BaseServlet.getData(service(pathAndQuery).getRequest()); |
| 237 | } |
| 238 | } |