blob: 98d416afa0f52489878b5e219af2fad2e5464cfd [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;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070022import org.eclipse.jgit.lib.ObjectId;
23import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080024import org.junit.runner.RunWith;
25import org.junit.runners.JUnit4;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070026
Dave Borowitz9de65952012-08-13 16:09:45 -070027/** Tests for {@link TreeSoyData}. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080028@RunWith(JUnit4.class)
Dave Borowitzd40bdf12014-04-19 19:33:56 -070029public class TreeSoyDataTest {
30 @Test
31 public void getTargetDisplayNameReturnsDisplayName() throws Exception {
Dave Borowitzfde41fd2015-09-16 15:14:38 -040032 assertThat(getTargetDisplayName("foo")).isEqualTo("foo");
33 assertThat(getTargetDisplayName("foo/bar")).isEqualTo("foo/bar");
Dave Borowitzcf38c032016-05-02 11:06:23 -040034 assertThat(getTargetDisplayName(Strings.repeat("a/", 10) + "bar"))
35 .isEqualTo("a/a/a/a/a/a/a/a/a/a/bar");
36 assertThat(getTargetDisplayName(Strings.repeat("a/", 34) + "bar"))
37 .isEqualTo("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");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040038 assertThat(getTargetDisplayName(Strings.repeat("a/", 35) + "bar")).isEqualTo(".../bar");
39 assertThat(getTargetDisplayName(Strings.repeat("a/", 100) + "bar")).isEqualTo(".../bar");
Dave Borowitzcf38c032016-05-02 11:06:23 -040040 assertThat(getTargetDisplayName(Strings.repeat("a", 80)))
41 .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");
Dave Borowitzcf38c032016-05-02 11:06:23 -040048 GitilesView view =
49 GitilesView.path()
50 .setServletPath("/x")
51 .setHostName("host")
52 .setRepositoryName("repo")
53 .setRevision(Revision.unpeeled("m", id))
54 .setPathPart("a/b/c")
55 .build();
Dave Borowitzfde41fd2015-09-16 15:14:38 -040056 assertThat(resolveTargetUrl(view, "/foo")).isNull();
57 assertThat(resolveTargetUrl(view, "../../")).isEqualTo("/x/repo/+/m/a");
58 assertThat(resolveTargetUrl(view, ".././../")).isEqualTo("/x/repo/+/m/a");
59 assertThat(resolveTargetUrl(view, "..//../")).isEqualTo("/x/repo/+/m/a");
60 assertThat(resolveTargetUrl(view, "../../d")).isEqualTo("/x/repo/+/m/a/d");
61 assertThat(resolveTargetUrl(view, "../../..")).isEqualTo("/x/repo/+/m/");
62 assertThat(resolveTargetUrl(view, "../../d/e")).isEqualTo("/x/repo/+/m/a/d/e");
63 assertThat(resolveTargetUrl(view, "../d/../e/../")).isEqualTo("/x/repo/+/m/a/b");
64 assertThat(resolveTargetUrl(view, "../../../../")).isNull();
65 assertThat(resolveTargetUrl(view, "../../a/../../..")).isNull();
Dave Borowitz9de65952012-08-13 16:09:45 -070066 }
67}