| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [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 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 18 | import static com.google.gitiles.TestGitilesUrls.URLS; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 19 | |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 22 | import com.google.gitiles.RefServlet.RefJsonData; |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 23 | import com.google.gson.reflect.TypeToken; |
| 24 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 25 | import org.eclipse.jgit.lib.ObjectId; |
| 26 | import org.eclipse.jgit.lib.Repository; |
| 27 | import org.eclipse.jgit.revwalk.RevCommit; |
| 28 | import org.eclipse.jgit.revwalk.RevTag; |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 29 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 30 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 31 | import org.junit.runner.RunWith; |
| 32 | import org.junit.runners.JUnit4; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 33 | |
| 34 | import java.io.IOException; |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 35 | import java.util.List; |
| 36 | import java.util.Map; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 37 | |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 38 | import javax.servlet.http.HttpServletRequest; |
| 39 | |
| Nodir Turakulov | 08cdc23 | 2015-08-28 12:02:16 -0700 | [diff] [blame] | 40 | /** Tests for {@link RefServlet}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 41 | @RunWith(JUnit4.class) |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 42 | public class RefServletTest extends ServletTest { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 43 | private void setUpSimpleRefs() throws Exception { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 44 | RevCommit commit = repo.branch("refs/heads/master").commit().create(); |
| 45 | repo.update("refs/heads/branch", commit); |
| 46 | repo.update("refs/tags/ctag", commit); |
| 47 | RevTag tag = repo.tag("atag", commit); |
| 48 | repo.update("refs/tags/atag", tag); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 49 | repo.getRepository().updateRef("HEAD").link("refs/heads/master"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 52 | @Test |
| 53 | public void evilRefName() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 54 | setUpSimpleRefs(); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 55 | String evilRefName = "refs/evil/<script>window.close();</script>/&foo"; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 56 | assertThat(Repository.isValidRefName(evilRefName)).isTrue(); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 57 | repo.branch(evilRefName).commit().create(); |
| 58 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 59 | FakeHttpServletResponse res = buildText("/repo/+refs/evil"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 60 | assertThat(res.getActualBodyString()) |
| 61 | .isEqualTo( |
| 62 | id(evilRefName) + " refs/evil/<script>window.close();</script>/&foo\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 65 | @Test |
| 66 | public void getRefsTextAll() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 67 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 68 | FakeHttpServletResponse res = buildText("/repo/+refs"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 69 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 70 | assertThat(res.getActualBodyString()) |
| 71 | .isEqualTo( |
| 72 | id("HEAD") |
| 73 | + " HEAD\n" |
| 74 | + id("refs/heads/branch") |
| 75 | + " refs/heads/branch\n" |
| 76 | + id("refs/heads/master") |
| 77 | + " refs/heads/master\n" |
| 78 | + id("refs/tags/atag") |
| 79 | + " refs/tags/atag\n" |
| 80 | + peeled("refs/tags/atag") |
| 81 | + " refs/tags/atag^{}\n" |
| 82 | + id("refs/tags/ctag") |
| 83 | + " refs/tags/ctag\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 86 | @Test |
| 87 | public void getRefsTextAllTrailingSlash() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 88 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 89 | FakeHttpServletResponse res = buildText("/repo/+refs/"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 90 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 91 | assertThat(res.getActualBodyString()) |
| 92 | .isEqualTo( |
| 93 | id("HEAD") |
| 94 | + " HEAD\n" |
| 95 | + id("refs/heads/branch") |
| 96 | + " refs/heads/branch\n" |
| 97 | + id("refs/heads/master") |
| 98 | + " refs/heads/master\n" |
| 99 | + id("refs/tags/atag") |
| 100 | + " refs/tags/atag\n" |
| 101 | + peeled("refs/tags/atag") |
| 102 | + " refs/tags/atag^{}\n" |
| 103 | + id("refs/tags/ctag") |
| 104 | + " refs/tags/ctag\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 107 | @Test |
| 108 | public void getRefsHeadsText() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 109 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 110 | FakeHttpServletResponse res = buildText("/repo/+refs/heads"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 111 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 112 | assertThat(res.getActualBodyString()) |
| 113 | .isEqualTo( |
| 114 | id("refs/heads/branch") |
| 115 | + " refs/heads/branch\n" |
| 116 | + id("refs/heads/master") |
| 117 | + " refs/heads/master\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 120 | @Test |
| 121 | public void getRefsHeadsTextTrailingSlash() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 122 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 123 | FakeHttpServletResponse res = buildText("/repo/+refs/heads/"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 124 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 125 | assertThat(res.getActualBodyString()) |
| 126 | .isEqualTo( |
| 127 | id("refs/heads/branch") |
| 128 | + " refs/heads/branch\n" |
| 129 | + id("refs/heads/master") |
| 130 | + " refs/heads/master\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 133 | @Test |
| 134 | public void noHeadText() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 135 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 136 | FakeHttpServletResponse res = buildText("/repo/+refs/HEAD"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 137 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 138 | // /+refs/foo means refs/foo(/*), so this is empty. |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 139 | assertThat(res.getActualBodyString()).isEqualTo(""); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 142 | @Test |
| 143 | public void singleHeadText() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 144 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 145 | FakeHttpServletResponse res = buildText("/repo/+refs/heads/master"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 146 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 147 | assertThat(res.getActualBodyString()) |
| 148 | .isEqualTo(id("refs/heads/master") + " refs/heads/master\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 151 | @Test |
| 152 | public void singlePeeledTagText() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 153 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 154 | FakeHttpServletResponse res = buildText("/repo/+refs/tags/atag"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 155 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 156 | assertThat(res.getActualBodyString()) |
| 157 | .isEqualTo( |
| 158 | id("refs/tags/atag") |
| 159 | + " refs/tags/atag\n" |
| 160 | + peeled("refs/tags/atag") |
| 161 | + " refs/tags/atag^{}\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 162 | } |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 163 | |
| 164 | @Test |
| 165 | public void getRefsJsonAll() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 166 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 167 | Map<String, RefJsonData> result = buildRefJson("/repo/+refs"); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 168 | List<String> keys = ImmutableList.copyOf(result.keySet()); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 169 | assertThat(keys) |
| 170 | .containsExactly( |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 171 | "HEAD", "refs/heads/branch", "refs/heads/master", "refs/tags/atag", "refs/tags/ctag") |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 172 | .inOrder(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 173 | |
| 174 | RefJsonData head = result.get(keys.get(0)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 175 | assertThat(head.value).isEqualTo(id("HEAD")); |
| 176 | assertThat(head.peeled).isNull(); |
| 177 | assertThat(head.target).isEqualTo("refs/heads/master"); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 178 | |
| 179 | RefJsonData branch = result.get(keys.get(1)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 180 | assertThat(branch.value).isEqualTo(id("refs/heads/branch")); |
| 181 | assertThat(branch.peeled).isNull(); |
| 182 | assertThat(branch.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 183 | |
| 184 | RefJsonData master = result.get(keys.get(2)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 185 | assertThat(master.value).isEqualTo(id("refs/heads/master")); |
| 186 | assertThat(master.peeled).isNull(); |
| 187 | assertThat(master.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 188 | |
| 189 | RefJsonData atag = result.get(keys.get(3)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 190 | assertThat(atag.value).isEqualTo(id("refs/tags/atag")); |
| 191 | assertThat(atag.peeled).isEqualTo(peeled("refs/tags/atag")); |
| 192 | assertThat(atag.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 193 | |
| 194 | RefJsonData ctag = result.get(keys.get(4)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 195 | assertThat(ctag.value).isEqualTo(id("refs/tags/ctag")); |
| 196 | assertThat(ctag.peeled).isNull(); |
| 197 | assertThat(ctag.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void getRefsHeadsJson() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 202 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 203 | Map<String, RefJsonData> result = buildRefJson("/repo/+refs/heads"); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 204 | List<String> keys = ImmutableList.copyOf(result.keySet()); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 205 | assertThat(keys).containsExactly("branch", "master").inOrder(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 206 | |
| 207 | RefJsonData branch = result.get(keys.get(0)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 208 | assertThat(branch.value).isEqualTo(id("refs/heads/branch")); |
| 209 | assertThat(branch.peeled).isNull(); |
| 210 | assertThat(branch.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 211 | |
| 212 | RefJsonData master = result.get(keys.get(1)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 213 | assertThat(master.value).isEqualTo(id("refs/heads/master")); |
| 214 | assertThat(master.peeled).isNull(); |
| 215 | assertThat(master.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 218 | private Map<String, RefJsonData> buildRefJson(String path) throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 219 | return buildJson(new TypeToken<Map<String, RefJsonData>>() {}, path); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 220 | } |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 221 | |
| 222 | @Test |
| 223 | public void emptySoy() throws Exception { |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 224 | assertThat(buildBranchesSoyData()).isEmpty(); |
| 225 | assertThat(buildTagsSoyData()).isEmpty(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Test |
| 229 | public void branchesAndTagsSoy() throws Exception { |
| 230 | repo.branch("refs/heads/foo").commit().create(); |
| 231 | repo.branch("refs/heads/bar").commit().create(); |
| 232 | repo.branch("refs/tags/baz").commit().create(); |
| 233 | repo.branch("refs/nope/quux").commit().create(); |
| 234 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 235 | assertThat(buildBranchesSoyData()) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 236 | .containsExactly(ref("/b/test/+/bar", "bar"), ref("/b/test/+/foo", "foo")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 237 | .inOrder(); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 238 | assertThat(buildTagsSoyData()).containsExactly(ref("/b/test/+/baz", "baz")).inOrder(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Test |
| 242 | public void ambiguousBranchSoy() throws Exception { |
| 243 | repo.branch("refs/heads/foo").commit().create(); |
| 244 | repo.branch("refs/heads/bar").commit().create(); |
| 245 | repo.branch("refs/tags/foo").commit().create(); |
| 246 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 247 | assertThat(buildBranchesSoyData()) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 248 | .containsExactly(ref("/b/test/+/bar", "bar"), ref("/b/test/+/refs/heads/foo", "foo")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 249 | .inOrder(); |
| 250 | assertThat(buildTagsSoyData()) |
| 251 | .containsExactly( |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 252 | // refs/tags/ is searched before refs/heads/, so this does not |
| 253 | // appear ambiguous. |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 254 | ref("/b/test/+/foo", "foo")) |
| 255 | .inOrder(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Test |
| 259 | public void ambiguousRelativeToNonBranchOrTagSoy() throws Exception { |
| 260 | repo.branch("refs/foo").commit().create(); |
| 261 | repo.branch("refs/heads/foo").commit().create(); |
| 262 | repo.branch("refs/tags/foo").commit().create(); |
| 263 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 264 | assertThat(buildBranchesSoyData()) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 265 | .containsExactly(ref("/b/test/+/refs/heads/foo", "foo")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 266 | .inOrder(); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 267 | assertThat(buildTagsSoyData()).containsExactly(ref("/b/test/+/refs/tags/foo", "foo")).inOrder(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Test |
| 271 | public void refsHeadsSoy() throws Exception { |
| 272 | repo.branch("refs/heads/foo").commit().create(); |
| 273 | repo.branch("refs/heads/refs/heads/foo").commit().create(); |
| 274 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 275 | assertThat(buildBranchesSoyData()) |
| 276 | .containsExactly( |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 277 | ref("/b/test/+/foo", "foo"), |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 278 | ref("/b/test/+/refs/heads/refs/heads/foo", "refs/heads/foo")) |
| 279 | .inOrder(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | private HttpServletRequest buildSoyRequest() { |
| 283 | HttpServletRequest req = FakeHttpServletRequest.newRequest(repo.getRepository()); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 284 | ViewFilter.setView( |
| 285 | req, |
| 286 | GitilesView.repositoryIndex() |
| 287 | .setHostName(URLS.getHostName(req)) |
| 288 | .setServletPath(req.getServletPath()) |
| 289 | .setRepositoryName("test") |
| 290 | .build()); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 291 | return req; |
| 292 | } |
| 293 | |
| 294 | private List<?> buildBranchesSoyData() throws Exception { |
| 295 | return RefServlet.getBranchesSoyData(buildSoyRequest(), Integer.MAX_VALUE); |
| 296 | } |
| 297 | |
| 298 | private List<?> buildTagsSoyData() throws Exception { |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 299 | try (RevWalk rw = new RevWalk(repo.getRepository())) { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 300 | return RefServlet.getTagsSoyData( |
| 301 | buildSoyRequest(), new TimeCache(TimeCache.defaultBuilder()), rw, Integer.MAX_VALUE); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | private String id(String refName) throws IOException { |
| 306 | return ObjectId.toString(repo.getRepository().getRef(refName).getObjectId()); |
| 307 | } |
| 308 | |
| 309 | private String peeled(String refName) throws IOException { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame^] | 310 | return ObjectId.toString( |
| 311 | repo.getRepository().peel(repo.getRepository().getRef(refName)).getPeeledObjectId()); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | private Map<String, String> ref(String url, String name) { |
| 315 | return ImmutableMap.of("url", url, "name", name); |
| 316 | } |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 317 | } |