blob: cfd2423f9c54722708f569073ad6200cd96a7f86 [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
Dave Borowitzd40bdf12014-04-19 19:33:56 -070022import org.junit.Test;
23
24import javax.servlet.http.HttpServletRequest;
25
Dave Borowitz9de65952012-08-13 16:09:45 -070026/** Tests for {@link Linkifier}. */
Dave Borowitzd40bdf12014-04-19 19:33:56 -070027public class LinkifierTest {
Dave Borowitz9de65952012-08-13 16:09:45 -070028 private static final HttpServletRequest REQ = FakeHttpServletRequest.newRequest();
29
Dave Borowitzd40bdf12014-04-19 19:33:56 -070030 @Test
31 public void linkifyMessageNoMatch() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070032 Linkifier l = new Linkifier(TestGitilesUrls.URLS);
33 assertEquals(ImmutableList.of(ImmutableMap.of("text", "some message text")),
34 l.linkify(FakeHttpServletRequest.newRequest(), "some message text"));
35 }
36
Dave Borowitzd40bdf12014-04-19 19:33:56 -070037 @Test
38 public void linkifyMessageUrl() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070039 Linkifier l = new Linkifier(TestGitilesUrls.URLS);
40 assertEquals(ImmutableList.of(
41 ImmutableMap.of("text", "http://my/url", "url", "http://my/url")),
42 l.linkify(REQ, "http://my/url"));
43 assertEquals(ImmutableList.of(
44 ImmutableMap.of("text", "https://my/url", "url", "https://my/url")),
45 l.linkify(REQ, "https://my/url"));
46 assertEquals(ImmutableList.of(
47 ImmutableMap.of("text", "foo "),
48 ImmutableMap.of("text", "http://my/url", "url", "http://my/url"),
49 ImmutableMap.of("text", " bar")),
50 l.linkify(REQ, "foo http://my/url bar"));
51 assertEquals(ImmutableList.of(
52 ImmutableMap.of("text", "foo "),
53 ImmutableMap.of("text", "http://my/url", "url", "http://my/url"),
54 ImmutableMap.of("text", " bar "),
55 ImmutableMap.of("text", "http://my/other/url", "url", "http://my/other/url"),
56 ImmutableMap.of("text", " baz")),
57 l.linkify(REQ, "foo http://my/url bar http://my/other/url baz"));
58 }
59
Dave Borowitzd40bdf12014-04-19 19:33:56 -070060 @Test
61 public void linkifyMessageChangeIdNoGerrit() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070062 Linkifier l = new Linkifier(new GitilesUrls() {
63 @Override
64 public String getBaseGerritUrl(HttpServletRequest req) {
65 return null;
66 }
67
68 @Override
69 public String getHostName(HttpServletRequest req) {
70 throw new UnsupportedOperationException();
71 }
72
73 @Override
74 public String getBaseGitUrl(HttpServletRequest req) {
75 throw new UnsupportedOperationException();
76 }
77 });
78 assertEquals(ImmutableList.of(ImmutableMap.of("text", "I0123456789")),
79 l.linkify(REQ, "I0123456789"));
80 assertEquals(ImmutableList.of(ImmutableMap.of("text", "Change-Id: I0123456789")),
81 l.linkify(REQ, "Change-Id: I0123456789"));
82 assertEquals(ImmutableList.of(ImmutableMap.of("text", "Change-Id: I0123456789 does not exist")),
83 l.linkify(REQ, "Change-Id: I0123456789 does not exist"));
84 }
85
Dave Borowitzd40bdf12014-04-19 19:33:56 -070086 @Test
87 public void linkifyMessageChangeId() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070088 Linkifier l = new Linkifier(TestGitilesUrls.URLS);
89 assertEquals(ImmutableList.of(
90 ImmutableMap.of("text", "I0123456789",
91 "url", "http://test-host-review/foo/#/q/I0123456789,n,z")),
92 l.linkify(REQ, "I0123456789"));
93 assertEquals(ImmutableList.of(
94 ImmutableMap.of("text", "Change-Id: "),
95 ImmutableMap.of("text", "I0123456789",
96 "url", "http://test-host-review/foo/#/q/I0123456789,n,z")),
97 l.linkify(REQ, "Change-Id: I0123456789"));
98 assertEquals(ImmutableList.of(
99 ImmutableMap.of("text", "Change-Id: "),
100 ImmutableMap.of("text", "I0123456789",
101 "url", "http://test-host-review/foo/#/q/I0123456789,n,z"),
102 ImmutableMap.of("text", " exists")),
103 l.linkify(REQ, "Change-Id: I0123456789 exists"));
104 }
105
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700106 @Test
107 public void linkifyMessageUrlAndChangeId() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -0700108 Linkifier l = new Linkifier(TestGitilesUrls.URLS);
109 assertEquals(ImmutableList.of(
110 ImmutableMap.of("text", "http://my/url/I0123456789", "url", "http://my/url/I0123456789"),
111 ImmutableMap.of("text", " is not change "),
112 ImmutableMap.of("text", "I0123456789",
113 "url", "http://test-host-review/foo/#/q/I0123456789,n,z")),
114 l.linkify(REQ, "http://my/url/I0123456789 is not change I0123456789"));
115 }
Dave Borowitz5871ac82013-03-21 12:14:28 -0700116
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700117 @Test
118 public void linkifyAmpersand() throws Exception {
Dave Borowitz5871ac82013-03-21 12:14:28 -0700119 Linkifier l = new Linkifier(TestGitilesUrls.URLS);
120 assertEquals(ImmutableList.of(
121 ImmutableMap.of("text", "http://my/url?a&b", "url", "http://my/url?a&b")),
122 l.linkify(REQ, "http://my/url?a&b"));
123 assertEquals(ImmutableList.of(
124 ImmutableMap.of("text", "http://weird/htmlified/?url",
125 "url", "http://weird/htmlified/?url"),
126 ImmutableMap.of("text", "<p&rt;")),
127 l.linkify(REQ, "http://weird/htmlified/?url<p&rt;"));
128 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700129}