| 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 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | |
| 24 | import javax.servlet.http.HttpServletRequest; |
| 25 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 26 | /** Tests for {@link Linkifier}. */ |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 27 | public class LinkifierTest { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 28 | private static final HttpServletRequest REQ = FakeHttpServletRequest.newRequest(); |
| 29 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 30 | @Test |
| 31 | public void linkifyMessageNoMatch() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 32 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 37 | @Test |
| 38 | public void linkifyMessageUrl() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 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 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 60 | @Test |
| 61 | public void linkifyMessageChangeIdNoGerrit() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 62 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 86 | @Test |
| 87 | public void linkifyMessageChangeId() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 88 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 106 | @Test |
| 107 | public void linkifyMessageUrlAndChangeId() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 108 | 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 Borowitz | 5871ac8 | 2013-03-21 12:14:28 -0700 | [diff] [blame] | 116 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 117 | @Test |
| 118 | public void linkifyAmpersand() throws Exception { |
| Dave Borowitz | 5871ac8 | 2013-03-21 12:14:28 -0700 | [diff] [blame] | 119 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 129 | } |