| 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 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | import static com.google.gitiles.TreeSoyData.getTargetDisplayName; |
| 19 | import static com.google.gitiles.TreeSoyData.resolveTargetUrl; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 20 | |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 21 | import com.google.common.base.Strings; |
| 22 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 23 | import org.eclipse.jgit.lib.ObjectId; |
| 24 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 27 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 28 | /** Tests for {@link TreeSoyData}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 29 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 30 | public class TreeSoyDataTest { |
| 31 | @Test |
| 32 | public void getTargetDisplayNameReturnsDisplayName() throws Exception { |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 33 | assertThat(getTargetDisplayName("foo")).isEqualTo("foo"); |
| 34 | assertThat(getTargetDisplayName("foo/bar")).isEqualTo("foo/bar"); |
| 35 | assertThat(getTargetDisplayName(Strings.repeat("a/", 10) + "bar")).isEqualTo( |
| 36 | "a/a/a/a/a/a/a/a/a/a/bar"); |
| 37 | assertThat(getTargetDisplayName(Strings.repeat("a/", 34) + "bar")).isEqualTo( |
| 38 | "a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/bar"); |
| 39 | assertThat(getTargetDisplayName(Strings.repeat("a/", 35) + "bar")).isEqualTo(".../bar"); |
| 40 | assertThat(getTargetDisplayName(Strings.repeat("a/", 100) + "bar")).isEqualTo(".../bar"); |
| 41 | assertThat(getTargetDisplayName(Strings.repeat("a", 80))).isEqualTo( |
| 42 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 45 | @Test |
| 46 | public void resolveTargetUrlReturnsUrl() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 47 | ObjectId id = ObjectId.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234"); |
| 48 | GitilesView view = GitilesView.path() |
| 49 | .setServletPath("/x") |
| 50 | .setHostName("host") |
| 51 | .setRepositoryName("repo") |
| 52 | .setRevision(Revision.unpeeled("m", id)) |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 53 | .setPathPart("a/b/c") |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 54 | .build(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 55 | assertThat(resolveTargetUrl(view, "/foo")).isNull(); |
| 56 | assertThat(resolveTargetUrl(view, "../../")).isEqualTo("/x/repo/+/m/a"); |
| 57 | assertThat(resolveTargetUrl(view, ".././../")).isEqualTo("/x/repo/+/m/a"); |
| 58 | assertThat(resolveTargetUrl(view, "..//../")).isEqualTo("/x/repo/+/m/a"); |
| 59 | assertThat(resolveTargetUrl(view, "../../d")).isEqualTo("/x/repo/+/m/a/d"); |
| 60 | assertThat(resolveTargetUrl(view, "../../..")).isEqualTo("/x/repo/+/m/"); |
| 61 | assertThat(resolveTargetUrl(view, "../../d/e")).isEqualTo("/x/repo/+/m/a/d/e"); |
| 62 | assertThat(resolveTargetUrl(view, "../d/../e/../")).isEqualTo("/x/repo/+/m/a/b"); |
| 63 | assertThat(resolveTargetUrl(view, "../../../../")).isNull(); |
| 64 | assertThat(resolveTargetUrl(view, "../../a/../../..")).isNull(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 65 | } |
| 66 | } |