blob: bbf5d37ee267edb6365c8a53633ef3009723a92e [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// 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
15package com.google.gitiles;
16
Dave Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
Dave Borowitz9de65952012-08-13 16:09:45 -070018import static com.google.gitiles.TreeSoyData.getTargetDisplayName;
19import static com.google.gitiles.TreeSoyData.resolveTargetUrl;
Dave Borowitz9de65952012-08-13 16:09:45 -070020
Shawn Pearceb43b2d52013-03-18 10:55:15 -070021import com.google.common.base.Strings;
22
Dave Borowitzd40bdf12014-04-19 19:33:56 -070023import org.eclipse.jgit.lib.ObjectId;
24import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080025import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070027
Dave Borowitz9de65952012-08-13 16:09:45 -070028/** Tests for {@link TreeSoyData}. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080029@RunWith(JUnit4.class)
Dave Borowitzd40bdf12014-04-19 19:33:56 -070030public class TreeSoyDataTest {
31 @Test
32 public void getTargetDisplayNameReturnsDisplayName() throws Exception {
Dave Borowitzfde41fd2015-09-16 15:14:38 -040033 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 Borowitz9de65952012-08-13 16:09:45 -070043 }
44
Dave Borowitzd40bdf12014-04-19 19:33:56 -070045 @Test
46 public void resolveTargetUrlReturnsUrl() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070047 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 Borowitzdd3c3d92013-03-11 16:38:41 -070053 .setPathPart("a/b/c")
Dave Borowitz9de65952012-08-13 16:09:45 -070054 .build();
Dave Borowitzfde41fd2015-09-16 15:14:38 -040055 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 Borowitz9de65952012-08-13 16:09:45 -070065 }
66}