blob: 9a04f9d6599c67db6e8f30e6fd7a1f1a82b1a6bc [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 Borowitzd40bdf12014-04-19 19:33:56 -070017import static org.junit.Assert.assertEquals;
Dave Borowitz9de65952012-08-13 16:09:45 -070018
Shawn Pearceb43b2d52013-03-18 10:55:15 -070019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableMap;
Dave Borowitz9de65952012-08-13 16:09:45 -070021
Stefan Beller6307b3d2014-11-13 17:53:03 -080022import org.eclipse.jgit.lib.Config;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070023import 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
27import javax.servlet.http.HttpServletRequest;
28
Dave Borowitz9de65952012-08-13 16:09:45 -070029/** Tests for {@link Linkifier}. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080030@RunWith(JUnit4.class)
Dave Borowitzd40bdf12014-04-19 19:33:56 -070031public class LinkifierTest {
Dave Borowitz9de65952012-08-13 16:09:45 -070032 private static final HttpServletRequest REQ = FakeHttpServletRequest.newRequest();
33
Dave Borowitzd40bdf12014-04-19 19:33:56 -070034 @Test
35 public void linkifyMessageNoMatch() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -080036 Config config = new Config();
37 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitz9de65952012-08-13 16:09:45 -070038 assertEquals(ImmutableList.of(ImmutableMap.of("text", "some message text")),
39 l.linkify(FakeHttpServletRequest.newRequest(), "some message text"));
40 }
41
Dave Borowitzd40bdf12014-04-19 19:33:56 -070042 @Test
43 public void linkifyMessageUrl() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -080044 Config config = new Config();
45 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitz9de65952012-08-13 16:09:45 -070046 assertEquals(ImmutableList.of(
47 ImmutableMap.of("text", "http://my/url", "url", "http://my/url")),
48 l.linkify(REQ, "http://my/url"));
49 assertEquals(ImmutableList.of(
50 ImmutableMap.of("text", "https://my/url", "url", "https://my/url")),
51 l.linkify(REQ, "https://my/url"));
52 assertEquals(ImmutableList.of(
53 ImmutableMap.of("text", "foo "),
54 ImmutableMap.of("text", "http://my/url", "url", "http://my/url"),
55 ImmutableMap.of("text", " bar")),
56 l.linkify(REQ, "foo http://my/url bar"));
57 assertEquals(ImmutableList.of(
58 ImmutableMap.of("text", "foo "),
59 ImmutableMap.of("text", "http://my/url", "url", "http://my/url"),
60 ImmutableMap.of("text", " bar "),
61 ImmutableMap.of("text", "http://my/other/url", "url", "http://my/other/url"),
62 ImmutableMap.of("text", " baz")),
63 l.linkify(REQ, "foo http://my/url bar http://my/other/url baz"));
64 }
65
Dave Borowitzd40bdf12014-04-19 19:33:56 -070066 @Test
67 public void linkifyMessageChangeIdNoGerrit() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -080068 Config config = new Config();
Dave Borowitz9de65952012-08-13 16:09:45 -070069 Linkifier l = new Linkifier(new GitilesUrls() {
70 @Override
71 public String getBaseGerritUrl(HttpServletRequest req) {
72 return null;
73 }
74
75 @Override
76 public String getHostName(HttpServletRequest req) {
77 throw new UnsupportedOperationException();
78 }
79
80 @Override
81 public String getBaseGitUrl(HttpServletRequest req) {
82 throw new UnsupportedOperationException();
83 }
Stefan Beller6307b3d2014-11-13 17:53:03 -080084 }, config);
Dave Borowitz9de65952012-08-13 16:09:45 -070085 assertEquals(ImmutableList.of(ImmutableMap.of("text", "I0123456789")),
86 l.linkify(REQ, "I0123456789"));
87 assertEquals(ImmutableList.of(ImmutableMap.of("text", "Change-Id: I0123456789")),
88 l.linkify(REQ, "Change-Id: I0123456789"));
89 assertEquals(ImmutableList.of(ImmutableMap.of("text", "Change-Id: I0123456789 does not exist")),
90 l.linkify(REQ, "Change-Id: I0123456789 does not exist"));
91 }
92
Dave Borowitzd40bdf12014-04-19 19:33:56 -070093 @Test
94 public void linkifyMessageChangeId() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -080095 Config config = new Config();
96 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitz9de65952012-08-13 16:09:45 -070097 assertEquals(ImmutableList.of(
98 ImmutableMap.of("text", "I0123456789",
99 "url", "http://test-host-review/foo/#/q/I0123456789,n,z")),
100 l.linkify(REQ, "I0123456789"));
101 assertEquals(ImmutableList.of(
102 ImmutableMap.of("text", "Change-Id: "),
103 ImmutableMap.of("text", "I0123456789",
104 "url", "http://test-host-review/foo/#/q/I0123456789,n,z")),
105 l.linkify(REQ, "Change-Id: I0123456789"));
106 assertEquals(ImmutableList.of(
107 ImmutableMap.of("text", "Change-Id: "),
108 ImmutableMap.of("text", "I0123456789",
109 "url", "http://test-host-review/foo/#/q/I0123456789,n,z"),
110 ImmutableMap.of("text", " exists")),
111 l.linkify(REQ, "Change-Id: I0123456789 exists"));
112 }
113
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700114 @Test
Stefan Beller6307b3d2014-11-13 17:53:03 -0800115 public void linkifyMessageCommentLinks() throws Exception {
116 Config config = new Config();
117 config.setString("commentlink", "buglink", "match", "(bug\\s+#?)(\\d+)");
118 config.setString("commentlink", "buglink", "link", "http://bugs/$2");
119 config.setString("commentlink", "featurelink", "match", "(Feature:\\s+)(\\d+)");
120 config.setString("commentlink", "featurelink", "link", "http://features/$2");
121 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
122 assertEquals(ImmutableList.of(
123 ImmutableMap.of("text", "There is a new "),
124 ImmutableMap.of("text", "Feature: 103", "url", "http://features/103"),
125 ImmutableMap.of("text", ", which is similar to the reported "),
126 ImmutableMap.of("text", "bug 100", "url", "http://bugs/100")),
127 l.linkify(REQ, "There is a new Feature: 103, which is similar to the reported bug 100"));
128 }
129
130 @Test
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700131 public void linkifyMessageUrlAndChangeId() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -0800132 Config config = new Config();
133 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitz9de65952012-08-13 16:09:45 -0700134 assertEquals(ImmutableList.of(
135 ImmutableMap.of("text", "http://my/url/I0123456789", "url", "http://my/url/I0123456789"),
136 ImmutableMap.of("text", " is not change "),
137 ImmutableMap.of("text", "I0123456789",
138 "url", "http://test-host-review/foo/#/q/I0123456789,n,z")),
139 l.linkify(REQ, "http://my/url/I0123456789 is not change I0123456789"));
140 }
Dave Borowitz5871ac82013-03-21 12:14:28 -0700141
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700142 @Test
143 public void linkifyAmpersand() throws Exception {
Stefan Beller6307b3d2014-11-13 17:53:03 -0800144 Config config = new Config();
145 Linkifier l = new Linkifier(TestGitilesUrls.URLS, config);
Dave Borowitz5871ac82013-03-21 12:14:28 -0700146 assertEquals(ImmutableList.of(
147 ImmutableMap.of("text", "http://my/url?a&b", "url", "http://my/url?a&b")),
148 l.linkify(REQ, "http://my/url?a&b"));
149 assertEquals(ImmutableList.of(
150 ImmutableMap.of("text", "http://weird/htmlified/?url",
151 "url", "http://weird/htmlified/?url"),
152 ImmutableMap.of("text", "<p&rt;")),
153 l.linkify(REQ, "http://weird/htmlified/?url<p&rt;"));
154 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700155}