blob: 5497291a9bbf9c2cd47f8e979d5f1ba7dd70f090 [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 -070018
Shawn Pearceb43b2d52013-03-18 10:55:15 -070019import com.google.common.collect.ImmutableMap;
Dave Borowitz9de65952012-08-13 16:09:45 -070020
Stefan Beller6307b3d2014-11-13 17:53:03 -080021import org.eclipse.jgit.lib.Config;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070022import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080023import org.junit.runner.RunWith;
24import org.junit.runners.JUnit4;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070025
26import javax.servlet.http.HttpServletRequest;
27
Dave Borowitz9de65952012-08-13 16:09:45 -070028/** Tests for {@link Linkifier}. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080029@RunWith(JUnit4.class)
Dave Borowitzd40bdf12014-04-19 19:33:56 -070030public class LinkifierTest {
Dave Borowitz9de65952012-08-13 16:09:45 -070031 private static final HttpServletRequest REQ = FakeHttpServletRequest.newRequest();
32
Dave Borowitzd40bdf12014-04-19 19:33:56 -070033 @Test
34 public void linkifyMessageNoMatch() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -080035 Config config = new Config();
36 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040037 assertThat(l.linkify(FakeHttpServletRequest.newRequest(), "some message text"))
38 .containsExactly(ImmutableMap.of("text", "some message text"));
Dave Borowitz9de65952012-08-13 16:09:45 -070039 }
40
Dave Borowitzd40bdf12014-04-19 19:33:56 -070041 @Test
42 public void linkifyMessageUrl() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -080043 Config config = new Config();
44 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040045 assertThat(l.linkify(REQ, "http://my/url"))
46 .containsExactly(
47 ImmutableMap.of("text", "http://my/url", "url", "http://my/url"))
48 .inOrder();
49 assertThat(l.linkify(REQ, "https://my/url"))
50 .containsExactly(
51 ImmutableMap.of("text", "https://my/url", "url", "https://my/url"))
52 .inOrder();
53 assertThat(l.linkify(REQ, "foo http://my/url bar"))
54 .containsExactly(
55 ImmutableMap.of("text", "foo "),
56 ImmutableMap.of("text", "http://my/url", "url", "http://my/url"),
57 ImmutableMap.of("text", " bar"))
58 .inOrder();
59 assertThat(l.linkify(REQ, "foo http://my/url bar http://my/other/url baz"))
60 .containsExactly(
61 ImmutableMap.of("text", "foo "),
62 ImmutableMap.of("text", "http://my/url", "url", "http://my/url"),
63 ImmutableMap.of("text", " bar "),
64 ImmutableMap.of("text", "http://my/other/url", "url", "http://my/other/url"),
65 ImmutableMap.of("text", " baz"))
66 .inOrder();
Dave Borowitz9de65952012-08-13 16:09:45 -070067 }
68
Dave Borowitzd40bdf12014-04-19 19:33:56 -070069 @Test
70 public void linkifyMessageChangeIdNoGerrit() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -080071 Config config = new Config();
Dave Borowitz9de65952012-08-13 16:09:45 -070072 Linkifier l = new Linkifier(new GitilesUrls() {
73 @Override
74 public String getBaseGerritUrl(HttpServletRequest req) {
75 return null;
76 }
77
78 @Override
79 public String getHostName(HttpServletRequest req) {
80 throw new UnsupportedOperationException();
81 }
82
83 @Override
84 public String getBaseGitUrl(HttpServletRequest req) {
85 throw new UnsupportedOperationException();
86 }
Stefan Beller6307b3d2014-11-13 17:53:03 -080087 }, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040088 assertThat(l.linkify(REQ, "I0123456789"))
89 .containsExactly(
90 ImmutableMap.of("text", "I0123456789"))
91 .inOrder();
92 assertThat(l.linkify(REQ, "Change-Id: I0123456789"))
93 .containsExactly(
94 ImmutableMap.of("text", "Change-Id: I0123456789"))
95 .inOrder();
96 assertThat(l.linkify(REQ, "Change-Id: I0123456789 does not exist"))
97 .containsExactly(
98 ImmutableMap.of("text", "Change-Id: I0123456789 does not exist"))
99 .inOrder();
Dave Borowitz9de65952012-08-13 16:09:45 -0700100 }
101
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700102 @Test
103 public void linkifyMessageChangeId() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -0800104 Config config = new Config();
105 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400106 assertThat(l.linkify(REQ, "I0123456789"))
107 .containsExactly(
108 ImmutableMap.of("text", "I0123456789",
109 "url", "http://test-host-review/foo/#/q/I0123456789,n,z"))
110 .inOrder();
111 assertThat(l.linkify(REQ, "Change-Id: I0123456789"))
112 .containsExactly(
113 ImmutableMap.of("text", "Change-Id: "),
114 ImmutableMap.of("text", "I0123456789",
115 "url", "http://test-host-review/foo/#/q/I0123456789,n,z"))
116 .inOrder();
117 assertThat(l.linkify(REQ, "Change-Id: I0123456789 exists"))
118 .containsExactly(
119 ImmutableMap.of("text", "Change-Id: "),
120 ImmutableMap.of("text", "I0123456789",
121 "url", "http://test-host-review/foo/#/q/I0123456789,n,z"),
122 ImmutableMap.of("text", " exists"))
123 .inOrder();
Dave Borowitz9de65952012-08-13 16:09:45 -0700124 }
125
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700126 @Test
Stefan Beller6307b3d2014-11-13 17:53:03 -0800127 public void linkifyMessageCommentLinks() throws Exception {
128 Config config = new Config();
129 config.setString("commentlink", "buglink", "match", "(bug\\s+#?)(\\d+)");
130 config.setString("commentlink", "buglink", "link", "http://bugs/$2");
131 config.setString("commentlink", "featurelink", "match", "(Feature:\\s+)(\\d+)");
132 config.setString("commentlink", "featurelink", "link", "http://features/$2");
133 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400134 assertThat(
135 l.linkify(REQ, "There is a new Feature: 103, which is similar to the reported bug 100"))
136 .containsExactly(
137 ImmutableMap.of("text", "There is a new "),
138 ImmutableMap.of("text", "Feature: 103", "url", "http://features/103"),
139 ImmutableMap.of("text", ", which is similar to the reported "),
140 ImmutableMap.of("text", "bug 100", "url", "http://bugs/100"))
141 .inOrder();
Stefan Beller6307b3d2014-11-13 17:53:03 -0800142 }
143
144 @Test
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700145 public void linkifyMessageUrlAndChangeId() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -0800146 Config config = new Config();
147 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400148 assertThat(l.linkify(REQ, "http://my/url/I0123456789 is not change I0123456789"))
149 .containsExactly(
150 ImmutableMap.of("text", "http://my/url/I0123456789",
151 "url", "http://my/url/I0123456789"),
152 ImmutableMap.of("text", " is not change "),
153 ImmutableMap.of("text", "I0123456789",
154 "url", "http://test-host-review/foo/#/q/I0123456789,n,z"))
155 .inOrder();
Dave Borowitz9de65952012-08-13 16:09:45 -0700156 }
Dave Borowitz5871ac82013-03-21 12:14:28 -0700157
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700158 @Test
159 public void linkifyAmpersand() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -0800160 Config config = new Config();
161 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400162 assertThat(l.linkify(REQ, "http://my/url?a&b"))
163 .containsExactly(
164 ImmutableMap.of("text", "http://my/url?a&b", "url", "http://my/url?a&b"))
165 .inOrder();
166 assertThat(l.linkify(REQ, "http://weird/htmlified/?url<p&rt;"))
167 .containsExactly(
168 ImmutableMap.of("text", "http://weird/htmlified/?url",
169 "url", "http://weird/htmlified/?url"),
170 ImmutableMap.of("text", "<p&rt;"))
171 .inOrder();
Dave Borowitz5871ac82013-03-21 12:14:28 -0700172 }
Nodir Turakulov56a226e2015-08-28 09:49:31 -0700173
174 @Test
175 public void invalidCommentlinkMatchRegex() throws Exception {
176 Config config = new Config();
177 config.setString("commentlink", "foo", "match", "bad-regex(");
178 new Linkifier(TestGitilesUrls.URLS, config);
179 // Must not throw an exception
180 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700181}