blob: baa97274a70fd50947905947210d0b00025179a2 [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 Borowitz3b744b12016-08-19 16:11:10 -040020import javax.servlet.http.HttpServletRequest;
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
Dave Borowitz9de65952012-08-13 16:09:45 -070026/** Tests for {@link Linkifier}. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080027@RunWith(JUnit4.class)
Dave Borowitzd40bdf12014-04-19 19:33:56 -070028public class LinkifierTest {
Dave Borowitz9de65952012-08-13 16:09:45 -070029 private static final HttpServletRequest REQ = FakeHttpServletRequest.newRequest();
30
Dave Borowitzd40bdf12014-04-19 19:33:56 -070031 @Test
32 public void linkifyMessageNoMatch() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -080033 Config config = new Config();
34 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040035 assertThat(l.linkify(FakeHttpServletRequest.newRequest(), "some message text"))
36 .containsExactly(ImmutableMap.of("text", "some message text"));
Dave Borowitz9de65952012-08-13 16:09:45 -070037 }
38
Dave Borowitzd40bdf12014-04-19 19:33:56 -070039 @Test
40 public void linkifyMessageUrl() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -080041 Config config = new Config();
42 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040043 assertThat(l.linkify(REQ, "http://my/url"))
Dave Borowitzcf38c032016-05-02 11:06:23 -040044 .containsExactly(ImmutableMap.of("text", "http://my/url", "url", "http://my/url"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -040045 .inOrder();
46 assertThat(l.linkify(REQ, "https://my/url"))
Dave Borowitzcf38c032016-05-02 11:06:23 -040047 .containsExactly(ImmutableMap.of("text", "https://my/url", "url", "https://my/url"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -040048 .inOrder();
49 assertThat(l.linkify(REQ, "foo http://my/url bar"))
50 .containsExactly(
51 ImmutableMap.of("text", "foo "),
52 ImmutableMap.of("text", "http://my/url", "url", "http://my/url"),
53 ImmutableMap.of("text", " bar"))
54 .inOrder();
55 assertThat(l.linkify(REQ, "foo http://my/url bar http://my/other/url baz"))
56 .containsExactly(
57 ImmutableMap.of("text", "foo "),
58 ImmutableMap.of("text", "http://my/url", "url", "http://my/url"),
59 ImmutableMap.of("text", " bar "),
60 ImmutableMap.of("text", "http://my/other/url", "url", "http://my/other/url"),
61 ImmutableMap.of("text", " baz"))
62 .inOrder();
Dave Borowitz9de65952012-08-13 16:09:45 -070063 }
64
Dave Borowitzd40bdf12014-04-19 19:33:56 -070065 @Test
66 public void linkifyMessageChangeIdNoGerrit() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -080067 Config config = new Config();
Dave Borowitzcf38c032016-05-02 11:06:23 -040068 Linkifier l =
69 new Linkifier(
70 new GitilesUrls() {
71 @Override
72 public String getBaseGerritUrl(HttpServletRequest req) {
73 return null;
74 }
Dave Borowitz9de65952012-08-13 16:09:45 -070075
Dave Borowitzcf38c032016-05-02 11:06:23 -040076 @Override
77 public String getHostName(HttpServletRequest req) {
78 throw new UnsupportedOperationException();
79 }
Dave Borowitz9de65952012-08-13 16:09:45 -070080
Dave Borowitzcf38c032016-05-02 11:06:23 -040081 @Override
82 public String getBaseGitUrl(HttpServletRequest req) {
83 throw new UnsupportedOperationException();
84 }
85 },
86 config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040087 assertThat(l.linkify(REQ, "I0123456789"))
Dave Borowitzcf38c032016-05-02 11:06:23 -040088 .containsExactly(ImmutableMap.of("text", "I0123456789"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -040089 .inOrder();
90 assertThat(l.linkify(REQ, "Change-Id: I0123456789"))
Dave Borowitzcf38c032016-05-02 11:06:23 -040091 .containsExactly(ImmutableMap.of("text", "Change-Id: I0123456789"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -040092 .inOrder();
93 assertThat(l.linkify(REQ, "Change-Id: I0123456789 does not exist"))
Dave Borowitzcf38c032016-05-02 11:06:23 -040094 .containsExactly(ImmutableMap.of("text", "Change-Id: I0123456789 does not exist"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -040095 .inOrder();
Dave Borowitz9de65952012-08-13 16:09:45 -070096 }
97
Dave Borowitzd40bdf12014-04-19 19:33:56 -070098 @Test
99 public void linkifyMessageChangeId() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -0800100 Config config = new Config();
101 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400102 assertThat(l.linkify(REQ, "I0123456789"))
103 .containsExactly(
Dave Borowitzcf38c032016-05-02 11:06:23 -0400104 ImmutableMap.of(
Andrew Bonventref52c7682016-09-08 12:07:45 -0400105 "text", "I0123456789", "url", "http://test-host-review/foo/#/q/I0123456789"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400106 .inOrder();
107 assertThat(l.linkify(REQ, "Change-Id: I0123456789"))
108 .containsExactly(
109 ImmutableMap.of("text", "Change-Id: "),
Dave Borowitzcf38c032016-05-02 11:06:23 -0400110 ImmutableMap.of(
Andrew Bonventref52c7682016-09-08 12:07:45 -0400111 "text", "I0123456789", "url", "http://test-host-review/foo/#/q/I0123456789"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400112 .inOrder();
113 assertThat(l.linkify(REQ, "Change-Id: I0123456789 exists"))
114 .containsExactly(
115 ImmutableMap.of("text", "Change-Id: "),
Dave Borowitzcf38c032016-05-02 11:06:23 -0400116 ImmutableMap.of(
Andrew Bonventref52c7682016-09-08 12:07:45 -0400117 "text", "I0123456789", "url", "http://test-host-review/foo/#/q/I0123456789"),
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400118 ImmutableMap.of("text", " exists"))
119 .inOrder();
Dave Borowitz9de65952012-08-13 16:09:45 -0700120 }
121
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700122 @Test
Stefan Beller6307b3d2014-11-13 17:53:03 -0800123 public void linkifyMessageCommentLinks() throws Exception {
124 Config config = new Config();
125 config.setString("commentlink", "buglink", "match", "(bug\\s+#?)(\\d+)");
126 config.setString("commentlink", "buglink", "link", "http://bugs/$2");
127 config.setString("commentlink", "featurelink", "match", "(Feature:\\s+)(\\d+)");
128 config.setString("commentlink", "featurelink", "link", "http://features/$2");
129 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400130 assertThat(
131 l.linkify(REQ, "There is a new Feature: 103, which is similar to the reported bug 100"))
132 .containsExactly(
133 ImmutableMap.of("text", "There is a new "),
134 ImmutableMap.of("text", "Feature: 103", "url", "http://features/103"),
135 ImmutableMap.of("text", ", which is similar to the reported "),
136 ImmutableMap.of("text", "bug 100", "url", "http://bugs/100"))
137 .inOrder();
Stefan Beller6307b3d2014-11-13 17:53:03 -0800138 }
139
140 @Test
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700141 public void linkifyMessageUrlAndChangeId() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -0800142 Config config = new Config();
143 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400144 assertThat(l.linkify(REQ, "http://my/url/I0123456789 is not change I0123456789"))
145 .containsExactly(
Dave Borowitzcf38c032016-05-02 11:06:23 -0400146 ImmutableMap.of(
147 "text", "http://my/url/I0123456789", "url", "http://my/url/I0123456789"),
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400148 ImmutableMap.of("text", " is not change "),
Dave Borowitzcf38c032016-05-02 11:06:23 -0400149 ImmutableMap.of(
Andrew Bonventref52c7682016-09-08 12:07:45 -0400150 "text", "I0123456789", "url", "http://test-host-review/foo/#/q/I0123456789"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400151 .inOrder();
Dave Borowitz9de65952012-08-13 16:09:45 -0700152 }
Dave Borowitz5871ac82013-03-21 12:14:28 -0700153
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700154 @Test
155 public void linkifyAmpersand() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -0800156 Config config = new Config();
157 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400158 assertThat(l.linkify(REQ, "http://my/url?a&b"))
Dave Borowitzcf38c032016-05-02 11:06:23 -0400159 .containsExactly(ImmutableMap.of("text", "http://my/url?a&b", "url", "http://my/url?a&b"))
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400160 .inOrder();
161 assertThat(l.linkify(REQ, "http://weird/htmlified/?url<p&rt;"))
162 .containsExactly(
Dave Borowitzcf38c032016-05-02 11:06:23 -0400163 ImmutableMap.of(
164 "text", "http://weird/htmlified/?url", "url", "http://weird/htmlified/?url"),
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400165 ImmutableMap.of("text", "<p&rt;"))
166 .inOrder();
Dave Borowitz5871ac82013-03-21 12:14:28 -0700167 }
Nodir Turakulov56a226e2015-08-28 09:49:31 -0700168
169 @Test
170 public void invalidCommentlinkMatchRegex() throws Exception {
171 Config config = new Config();
172 config.setString("commentlink", "foo", "match", "bad-regex(");
173 new Linkifier(TestGitilesUrls.URLS, config);
174 // Must not throw an exception
175 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700176}