| 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; |
| Pontus Jaensson | f08cb4a | 2021-11-11 14:06:57 +0100 | [diff] [blame] | 20 | import static com.google.gitiles.TreeSoyData.sortByTypeAlpha; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 22 | import com.google.common.base.Strings; |
| Pontus Jaensson | 4c5c8d2 | 2021-11-10 12:46:16 +0100 | [diff] [blame] | 23 | import java.util.HashMap; |
| Pontus Jaensson | f08cb4a | 2021-11-11 14:06:57 +0100 | [diff] [blame] | 24 | import java.util.Map; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 25 | import org.eclipse.jgit.lib.ObjectId; |
| 26 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 27 | import org.junit.runner.RunWith; |
| 28 | import org.junit.runners.JUnit4; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 29 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 30 | /** Tests for {@link TreeSoyData}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 31 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 32 | public class TreeSoyDataTest { |
| 33 | @Test |
| 34 | public void getTargetDisplayNameReturnsDisplayName() throws Exception { |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 35 | assertThat(getTargetDisplayName("foo")).isEqualTo("foo"); |
| 36 | assertThat(getTargetDisplayName("foo/bar")).isEqualTo("foo/bar"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 37 | assertThat(getTargetDisplayName(Strings.repeat("a/", 10) + "bar")) |
| 38 | .isEqualTo("a/a/a/a/a/a/a/a/a/a/bar"); |
| 39 | assertThat(getTargetDisplayName(Strings.repeat("a/", 34) + "bar")) |
| 40 | .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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 41 | assertThat(getTargetDisplayName(Strings.repeat("a/", 35) + "bar")).isEqualTo(".../bar"); |
| 42 | assertThat(getTargetDisplayName(Strings.repeat("a/", 100) + "bar")).isEqualTo(".../bar"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 43 | assertThat(getTargetDisplayName(Strings.repeat("a", 80))) |
| 44 | .isEqualTo( |
| 45 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 48 | @Test |
| 49 | public void resolveTargetUrlReturnsUrl() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 50 | ObjectId id = ObjectId.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 51 | GitilesView view = |
| 52 | GitilesView.path() |
| 53 | .setServletPath("/x") |
| 54 | .setHostName("host") |
| 55 | .setRepositoryName("repo") |
| 56 | .setRevision(Revision.unpeeled("m", id)) |
| 57 | .setPathPart("a/b/c") |
| 58 | .build(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 59 | assertThat(resolveTargetUrl(view, "/foo")).isNull(); |
| 60 | assertThat(resolveTargetUrl(view, "../../")).isEqualTo("/x/repo/+/m/a"); |
| 61 | assertThat(resolveTargetUrl(view, ".././../")).isEqualTo("/x/repo/+/m/a"); |
| 62 | assertThat(resolveTargetUrl(view, "..//../")).isEqualTo("/x/repo/+/m/a"); |
| 63 | assertThat(resolveTargetUrl(view, "../../d")).isEqualTo("/x/repo/+/m/a/d"); |
| 64 | assertThat(resolveTargetUrl(view, "../../..")).isEqualTo("/x/repo/+/m/"); |
| 65 | assertThat(resolveTargetUrl(view, "../../d/e")).isEqualTo("/x/repo/+/m/a/d/e"); |
| 66 | assertThat(resolveTargetUrl(view, "../d/../e/../")).isEqualTo("/x/repo/+/m/a/b"); |
| 67 | assertThat(resolveTargetUrl(view, "../../../../")).isNull(); |
| 68 | assertThat(resolveTargetUrl(view, "../../a/../../..")).isNull(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 69 | } |
| Pontus Jaensson | 4c5c8d2 | 2021-11-10 12:46:16 +0100 | [diff] [blame] | 70 | |
| 71 | @Test |
| 72 | public void sortByTypeSortsCorrect() throws Exception { |
| Pontus Jaensson | f08cb4a | 2021-11-11 14:06:57 +0100 | [diff] [blame] | 73 | Map<String, String> m1 = new HashMap<>(); |
| 74 | Map<String, String> m2 = new HashMap<>(); |
| 75 | Map<String, String> m3 = new HashMap<>(); |
| 76 | Map<String, String> m4 = new HashMap<>(); |
| 77 | Map<String, String> m5 = new HashMap<>(); |
| 78 | Map<String, String> m6 = new HashMap<>(); |
| Pontus Jaensson | 4c5c8d2 | 2021-11-10 12:46:16 +0100 | [diff] [blame] | 79 | m1.put("type", "TREE"); |
| Pontus Jaensson | f08cb4a | 2021-11-11 14:06:57 +0100 | [diff] [blame] | 80 | m1.put("name", "aa"); |
| Pontus Jaensson | 4c5c8d2 | 2021-11-10 12:46:16 +0100 | [diff] [blame] | 81 | m2.put("type", "TREE"); |
| Pontus Jaensson | f08cb4a | 2021-11-11 14:06:57 +0100 | [diff] [blame] | 82 | m2.put("name", "BB"); |
| Pontus Jaensson | 4c5c8d2 | 2021-11-10 12:46:16 +0100 | [diff] [blame] | 83 | m3.put("type", "SYMLINK"); |
| 84 | m4.put("type", "REGULAR_FILE"); |
| 85 | m5.put("type", "GITLINK"); |
| Pontus Jaensson | f08cb4a | 2021-11-11 14:06:57 +0100 | [diff] [blame] | 86 | m6.put("type", "TREE"); |
| 87 | m6.put("name", "AA"); |
| 88 | assertThat(sortByTypeAlpha(m1, m2)).isEqualTo(-1); |
| 89 | assertThat(sortByTypeAlpha(m2, m3)).isEqualTo(-1); |
| 90 | assertThat(sortByTypeAlpha(m3, m4)).isEqualTo(-1); |
| 91 | assertThat(sortByTypeAlpha(m4, m1)).isEqualTo(1); |
| 92 | assertThat(sortByTypeAlpha(m1, m4)).isEqualTo(-1); |
| 93 | assertThat(sortByTypeAlpha(m5, m2)).isEqualTo(1); |
| 94 | assertThat(sortByTypeAlpha(m2, m5)).isEqualTo(-1); |
| 95 | assertThat(sortByTypeAlpha(m1, m6)).isEqualTo(0); |
| 96 | assertThat(sortByTypeAlpha(m2, m1)).isEqualTo(1); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void sortByShortestPathFirst() throws Exception { |
| 101 | Map<String, String> p1 = new HashMap<>(); |
| 102 | Map<String, String> p2 = new HashMap<>(); |
| 103 | Map<String, String> p3 = new HashMap<>(); |
| 104 | p1.put("type", "TREE"); |
| 105 | p1.put("name", "short/"); |
| 106 | p2.put("type", "TREE"); |
| 107 | p2.put("name", "shortpath/"); |
| 108 | p3.put("type", "TREE"); |
| 109 | p3.put("name", "short.path/"); |
| 110 | assertThat(sortByTypeAlpha(p1, p2)).isLessThan(0); |
| 111 | assertThat(sortByTypeAlpha(p1, p3)).isLessThan(0); |
| Pontus Jaensson | 4c5c8d2 | 2021-11-10 12:46:16 +0100 | [diff] [blame] | 112 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 113 | } |