| 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 | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 17 | import static org.junit.Assert.assertEquals; |
| 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.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 22 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 23 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 24 | import org.junit.runner.RunWith; |
| 25 | import org.junit.runners.JUnit4; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 26 | |
| 27 | import javax.servlet.http.HttpServletRequest; |
| 28 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | /** Tests for {@link Linkifier}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 30 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 31 | public class LinkifierTest { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 32 | private static final HttpServletRequest REQ = FakeHttpServletRequest.newRequest(); |
| 33 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 34 | @Test |
| 35 | public void linkifyMessageNoMatch() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 36 | Config config = new Config(); |
| 37 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 38 | assertEquals(ImmutableList.of(ImmutableMap.of("text", "some message text")), |
| 39 | l.linkify(FakeHttpServletRequest.newRequest(), "some message text")); |
| 40 | } |
| 41 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 42 | @Test |
| 43 | public void linkifyMessageUrl() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 44 | Config config = new Config(); |
| 45 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 66 | @Test |
| 67 | public void linkifyMessageChangeIdNoGerrit() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 68 | Config config = new Config(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 69 | 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 Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 84 | }, config); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 85 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 93 | @Test |
| 94 | public void linkifyMessageChangeId() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 95 | Config config = new Config(); |
| 96 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 97 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 114 | @Test |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 115 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 131 | public void linkifyMessageUrlAndChangeId() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 132 | Config config = new Config(); |
| 133 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 134 | 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 Borowitz | 5871ac8 | 2013-03-21 12:14:28 -0700 | [diff] [blame] | 141 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 142 | @Test |
| 143 | public void linkifyAmpersand() throws Exception { |
| Stefan Beller | 6307b3d | 2014-11-13 17:53:03 -0800 | [diff] [blame] | 144 | Config config = new Config(); |
| 145 | Linkifier l = new Linkifier(TestGitilesUrls.URLS, config); |
| Dave Borowitz | 5871ac8 | 2013-03-21 12:14:28 -0700 | [diff] [blame] | 146 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 155 | } |