| 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 | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 20 | import javax.servlet.http.HttpServletRequest; |
| 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 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 26 | /** Tests for {@link Linkifier}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 27 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 28 | public class LinkifierTest { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | private static final HttpServletRequest REQ = FakeHttpServletRequest.newRequest(); |
| 30 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 31 | @Test |
| 32 | public void linkifyMessageNoMatch() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 33 | Config config = new Config(); |
| 34 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 35 | assertThat(l.linkify(FakeHttpServletRequest.newRequest(), "some message text")) |
| 36 | .containsExactly(ImmutableMap.of("text", "some message text")); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 39 | @Test |
| 40 | public void linkifyMessageUrl() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 41 | Config config = new Config(); |
| 42 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 43 | assertThat(l.linkify(REQ, "http://my/url")) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 44 | .containsExactly(ImmutableMap.of("text", "http://my/url", "url", "http://my/url")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 45 | .inOrder(); |
| 46 | assertThat(l.linkify(REQ, "https://my/url")) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 47 | .containsExactly(ImmutableMap.of("text", "https://my/url", "url", "https://my/url")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 48 | .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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 65 | @Test |
| 66 | public void linkifyMessageChangeIdNoGerrit() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 67 | Config config = new Config(); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 68 | Linkifier l = |
| 69 | new Linkifier( |
| 70 | new GitilesUrls() { |
| 71 | @Override |
| 72 | public String getBaseGerritUrl(HttpServletRequest req) { |
| 73 | return null; |
| 74 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 75 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 76 | @Override |
| 77 | public String getHostName(HttpServletRequest req) { |
| 78 | throw new UnsupportedOperationException(); |
| 79 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 80 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 81 | @Override |
| 82 | public String getBaseGitUrl(HttpServletRequest req) { |
| 83 | throw new UnsupportedOperationException(); |
| 84 | } |
| 85 | }, |
| 86 | config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 87 | assertThat(l.linkify(REQ, "I0123456789")) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 88 | .containsExactly(ImmutableMap.of("text", "I0123456789")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 89 | .inOrder(); |
| 90 | assertThat(l.linkify(REQ, "Change-Id: I0123456789")) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 91 | .containsExactly(ImmutableMap.of("text", "Change-Id: I0123456789")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 92 | .inOrder(); |
| 93 | assertThat(l.linkify(REQ, "Change-Id: I0123456789 does not exist")) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 94 | .containsExactly(ImmutableMap.of("text", "Change-Id: I0123456789 does not exist")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 95 | .inOrder(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 98 | @Test |
| 99 | public void linkifyMessageChangeId() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 100 | Config config = new Config(); |
| 101 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 102 | assertThat(l.linkify(REQ, "I0123456789")) |
| 103 | .containsExactly( |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 104 | ImmutableMap.of( |
| Andrew Bonventre | f52c768 | 2016-09-08 12:07:45 -0400 | [diff] [blame] | 105 | "text", "I0123456789", "url", "http://test-host-review/foo/#/q/I0123456789")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 106 | .inOrder(); |
| 107 | assertThat(l.linkify(REQ, "Change-Id: I0123456789")) |
| 108 | .containsExactly( |
| 109 | ImmutableMap.of("text", "Change-Id: "), |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 110 | ImmutableMap.of( |
| Andrew Bonventre | f52c768 | 2016-09-08 12:07:45 -0400 | [diff] [blame] | 111 | "text", "I0123456789", "url", "http://test-host-review/foo/#/q/I0123456789")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 112 | .inOrder(); |
| 113 | assertThat(l.linkify(REQ, "Change-Id: I0123456789 exists")) |
| 114 | .containsExactly( |
| 115 | ImmutableMap.of("text", "Change-Id: "), |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 116 | ImmutableMap.of( |
| Andrew Bonventre | f52c768 | 2016-09-08 12:07:45 -0400 | [diff] [blame] | 117 | "text", "I0123456789", "url", "http://test-host-review/foo/#/q/I0123456789"), |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 118 | ImmutableMap.of("text", " exists")) |
| 119 | .inOrder(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 122 | @Test |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 123 | 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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 130 | 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 Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Test |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 141 | public void linkifyMessageUrlAndChangeId() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 142 | Config config = new Config(); |
| 143 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 144 | assertThat(l.linkify(REQ, "http://my/url/I0123456789 is not change I0123456789")) |
| 145 | .containsExactly( |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 146 | ImmutableMap.of( |
| 147 | "text", "http://my/url/I0123456789", "url", "http://my/url/I0123456789"), |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 148 | ImmutableMap.of("text", " is not change "), |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 149 | ImmutableMap.of( |
| Andrew Bonventre | f52c768 | 2016-09-08 12:07:45 -0400 | [diff] [blame] | 150 | "text", "I0123456789", "url", "http://test-host-review/foo/#/q/I0123456789")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 151 | .inOrder(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 152 | } |
| Dave Borowitz | 5871ac8 | 2013-03-21 12:14:28 -0700 | [diff] [blame] | 153 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 154 | @Test |
| 155 | public void linkifyAmpersand() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 156 | Config config = new Config(); |
| 157 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 158 | assertThat(l.linkify(REQ, "http://my/url?a&b")) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 159 | .containsExactly(ImmutableMap.of("text", "http://my/url?a&b", "url", "http://my/url?a&b")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 160 | .inOrder(); |
| 161 | assertThat(l.linkify(REQ, "http://weird/htmlified/?url<p&rt;")) |
| 162 | .containsExactly( |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 163 | ImmutableMap.of( |
| 164 | "text", "http://weird/htmlified/?url", "url", "http://weird/htmlified/?url"), |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 165 | ImmutableMap.of("text", "<p&rt;")) |
| 166 | .inOrder(); |
| Dave Borowitz | 5871ac8 | 2013-03-21 12:14:28 -0700 | [diff] [blame] | 167 | } |
| Nodir Turakulov | 56a226e | 2015-08-28 09:49:31 -0700 | [diff] [blame] | 168 | |
| 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 176 | } |