| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 20 | |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 21 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 22 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 23 | import org.junit.runner.RunWith; |
| 24 | import org.junit.runners.JUnit4; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 25 | |
| 26 | import javax.servlet.http.HttpServletRequest; |
| 27 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 28 | /** Tests for {@link Linkifier}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 29 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 30 | public class LinkifierTest { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 31 | private static final HttpServletRequest REQ = FakeHttpServletRequest.newRequest(); |
| 32 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 33 | @Test |
| 34 | public void linkifyMessageNoMatch() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 35 | Config config = new Config(); |
| 36 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 37 | assertThat(l.linkify(FakeHttpServletRequest.newRequest(), "some message text")) |
| 38 | .containsExactly(ImmutableMap.of("text", "some message text")); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 41 | @Test |
| 42 | public void linkifyMessageUrl() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 43 | Config config = new Config(); |
| 44 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 45 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 69 | @Test |
| 70 | public void linkifyMessageChangeIdNoGerrit() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 71 | Config config = new Config(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 72 | 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 Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 87 | }, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 88 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 102 | @Test |
| 103 | public void linkifyMessageChangeId() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 104 | Config config = new Config(); |
| 105 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 106 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 126 | @Test |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 127 | 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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 134 | 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 Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 145 | public void linkifyMessageUrlAndChangeId() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 146 | Config config = new Config(); |
| 147 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 148 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 156 | } |
| Dave Borowitz | 5871ac8 | 2013-03-21 12:14:28 -0700 | [diff] [blame] | 157 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 158 | @Test |
| 159 | public void linkifyAmpersand() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 160 | Config config = new Config(); |
| 161 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 162 | 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 Borowitz | 5871ac8 | 2013-03-21 12:14:28 -0700 | [diff] [blame] | 172 | } |
| Nodir Turakulov | 56a226e | 2015-08-28 09:49:31 -0700 | [diff] [blame] | 173 | |
| 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 181 | } |