blob: 84f5f4fc022dfda5e9f821f33e404880c43de7c9 [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
Shawn Pearceb43b2d52013-03-18 10:55:15 -070017import javax.servlet.http.HttpServletRequest;
Dave Borowitz9de65952012-08-13 16:09:45 -070018
19import junit.framework.TestCase;
20
Shawn Pearceb43b2d52013-03-18 10:55:15 -070021import com.google.common.collect.ImmutableList;
22import com.google.common.collect.ImmutableMap;
Dave Borowitz9de65952012-08-13 16:09:45 -070023
24/** Tests for {@link Linkifier}. */
25public class LinkifierTest extends TestCase {
26 private static final HttpServletRequest REQ = FakeHttpServletRequest.newRequest();
27
28 @Override
29 protected void setUp() throws Exception {
30 }
31
32 public void testlinkifyMessageNoMatch() throws Exception {
33 Linkifier l = new Linkifier(TestGitilesUrls.URLS);
34 assertEquals(ImmutableList.of(ImmutableMap.of("text", "some message text")),
35 l.linkify(FakeHttpServletRequest.newRequest(), "some message text"));
36 }
37
38 public void testlinkifyMessageUrl() throws Exception {
39 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
60 public void testlinkifyMessageChangeIdNoGerrit() throws Exception {
61 Linkifier l = new Linkifier(new GitilesUrls() {
62 @Override
63 public String getBaseGerritUrl(HttpServletRequest req) {
64 return null;
65 }
66
67 @Override
68 public String getHostName(HttpServletRequest req) {
69 throw new UnsupportedOperationException();
70 }
71
72 @Override
73 public String getBaseGitUrl(HttpServletRequest req) {
74 throw new UnsupportedOperationException();
75 }
76 });
77 assertEquals(ImmutableList.of(ImmutableMap.of("text", "I0123456789")),
78 l.linkify(REQ, "I0123456789"));
79 assertEquals(ImmutableList.of(ImmutableMap.of("text", "Change-Id: I0123456789")),
80 l.linkify(REQ, "Change-Id: I0123456789"));
81 assertEquals(ImmutableList.of(ImmutableMap.of("text", "Change-Id: I0123456789 does not exist")),
82 l.linkify(REQ, "Change-Id: I0123456789 does not exist"));
83 }
84
85 public void testlinkifyMessageChangeId() throws Exception {
86 Linkifier l = new Linkifier(TestGitilesUrls.URLS);
87 assertEquals(ImmutableList.of(
88 ImmutableMap.of("text", "I0123456789",
89 "url", "http://test-host-review/foo/#/q/I0123456789,n,z")),
90 l.linkify(REQ, "I0123456789"));
91 assertEquals(ImmutableList.of(
92 ImmutableMap.of("text", "Change-Id: "),
93 ImmutableMap.of("text", "I0123456789",
94 "url", "http://test-host-review/foo/#/q/I0123456789,n,z")),
95 l.linkify(REQ, "Change-Id: I0123456789"));
96 assertEquals(ImmutableList.of(
97 ImmutableMap.of("text", "Change-Id: "),
98 ImmutableMap.of("text", "I0123456789",
99 "url", "http://test-host-review/foo/#/q/I0123456789,n,z"),
100 ImmutableMap.of("text", " exists")),
101 l.linkify(REQ, "Change-Id: I0123456789 exists"));
102 }
103
104 public void testlinkifyMessageUrlAndChangeId() throws Exception {
105 Linkifier l = new Linkifier(TestGitilesUrls.URLS);
106 assertEquals(ImmutableList.of(
107 ImmutableMap.of("text", "http://my/url/I0123456789", "url", "http://my/url/I0123456789"),
108 ImmutableMap.of("text", " is not change "),
109 ImmutableMap.of("text", "I0123456789",
110 "url", "http://test-host-review/foo/#/q/I0123456789,n,z")),
111 l.linkify(REQ, "http://my/url/I0123456789 is not change I0123456789"));
112 }
Dave Borowitz5871ac82013-03-21 12:14:28 -0700113
114 public void testLinkifyAmpersand() throws Exception {
115 Linkifier l = new Linkifier(TestGitilesUrls.URLS);
116 assertEquals(ImmutableList.of(
117 ImmutableMap.of("text", "http://my/url?a&b", "url", "http://my/url?a&b")),
118 l.linkify(REQ, "http://my/url?a&b"));
119 assertEquals(ImmutableList.of(
120 ImmutableMap.of("text", "http://weird/htmlified/?url",
121 "url", "http://weird/htmlified/?url"),
122 ImmutableMap.of("text", "<p&rt;")),
123 l.linkify(REQ, "http://weird/htmlified/?url<p&rt;"));
124 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700125}