|
@@ -0,0 +1,143 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2014 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.util.text;
|
|
24
|
+
|
|
25
|
+import java.util.Arrays;
|
|
26
|
+import java.util.List;
|
|
27
|
+
|
|
28
|
+import org.junit.Before;
|
|
29
|
+import org.junit.Ignore;
|
|
30
|
+import org.junit.Test;
|
|
31
|
+
|
|
32
|
+import static org.junit.Assert.assertEquals;
|
|
33
|
+
|
|
34
|
+public class LinkExtractorTest {
|
|
35
|
+
|
|
36
|
+ private LinkExtractor extractor;
|
|
37
|
+
|
|
38
|
+ @Before
|
|
39
|
+ public void setup() {
|
|
40
|
+ extractor = new LinkExtractor();
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ private void testLinks(final CharSequence input, final Link ... expected) {
|
|
44
|
+ final List<Link> result = extractor.findLinks(input);
|
|
45
|
+ assertEquals(Arrays.asList(expected), result);
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ @Test
|
|
49
|
+ public void testEmptyString() {
|
|
50
|
+ testLinks("");
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ @Test
|
|
54
|
+ public void testNoLinks() {
|
|
55
|
+ testLinks("no links here!");
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ @Test
|
|
59
|
+ public void testPlainWwwLink() {
|
|
60
|
+ testLinks("www.google.com", new Link(0, 14, "www.google.com"));
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ @Test
|
|
64
|
+ @Ignore("Not implemented yet")
|
|
65
|
+ public void testPlainWwwLinkInSentence() {
|
|
66
|
+ testLinks("word www.google.com word", new Link(5, 19, "www.google.com"));
|
|
67
|
+ testLinks("word www.google.com, word", new Link(5, 19, "www.google.com"));
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ @Test
|
|
71
|
+ @Ignore("Not implemented yet")
|
|
72
|
+ public void testPlainWwwLinkWithPunctuation() {
|
|
73
|
+ testLinks("www.google.com.", new Link(0, 14, "www.google.com"));
|
|
74
|
+ testLinks("www.google.com!", new Link(0, 14, "www.google.com"));
|
|
75
|
+ testLinks("www.google.com?", new Link(0, 14, "www.google.com"));
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ @Test
|
|
79
|
+ @Ignore("Not implemented yet")
|
|
80
|
+ public void testPlainWwwLinkWithBrackets() {
|
|
81
|
+ testLinks("(www.google.com)", new Link(1, 15, "www.google.com"));
|
|
82
|
+ testLinks("[www.google.com]", new Link(1, 15, "www.google.com"));
|
|
83
|
+ testLinks("<www.google.com>", new Link(1, 15, "www.google.com"));
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ @Test
|
|
87
|
+ @Ignore("Not implemented yet")
|
|
88
|
+ public void testPlainWwwLinkWithComplicatedBrackets() {
|
|
89
|
+ testLinks("(foo: www.google.com)", new Link(6, 21, "www.google.com"));
|
|
90
|
+ testLinks("[foo: www.google.com]", new Link(6, 21, "www.google.com"));
|
|
91
|
+ testLinks("<foo: www.google.com>", new Link(6, 21, "www.google.com"));
|
|
92
|
+
|
|
93
|
+ testLinks("(foo: 'www.google.com')", new Link(7, 22, "www.google.com"));
|
|
94
|
+ testLinks("('www.google.com')", new Link(2, 17, "www.google.com"));
|
|
95
|
+ testLinks("[\"www.google.com\"]", new Link(2, 17, "www.google.com"));
|
|
96
|
+
|
|
97
|
+ testLinks("www.example.com/blah(foobar)", new Link(0, 28, "www.example.com/blah(foobar)"));
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ @Test
|
|
101
|
+ @Ignore("Not implemented yet")
|
|
102
|
+ public void testPlainWwwLinkWithQuotes() {
|
|
103
|
+ testLinks("\"www.google.com\"", new Link(1, 15, "www.google.com"));
|
|
104
|
+ testLinks("'www.google.com'", new Link(1, 15, "www.google.com"));
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ @Test
|
|
108
|
+ public void testPrefixedLinks() {
|
|
109
|
+ testLinks("http://www.google.com", new Link(0, 21, "http://www.google.com"));
|
|
110
|
+ testLinks("http://www.google.com:80/test#flub",
|
|
111
|
+ new Link(0, 34, "http://www.google.com:80/test#flub"));
|
|
112
|
+
|
|
113
|
+ testLinks("svn+ssh://foo:bar", new Link(0, 17, "svn+ssh://foo:bar"));
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ @Test
|
|
117
|
+ @Ignore("Not implemented yet")
|
|
118
|
+ public void testMultipleLinks() {
|
|
119
|
+ testLinks("www.foo.com www.bar.com",
|
|
120
|
+ new Link(0, 11, "www.foo.com"),
|
|
121
|
+ new Link(12, 23, "www.bar.com"));
|
|
122
|
+
|
|
123
|
+ testLinks("(www.foo.com www.bar.com)",
|
|
124
|
+ new Link(1, 12, "www.foo.com"),
|
|
125
|
+ new Link(13, 24, "www.bar.com"));
|
|
126
|
+
|
|
127
|
+ testLinks("('www.foo.com')->x(\"www.bar.com\")",
|
|
128
|
+ new Link(2, 13, "www.foo.com"),
|
|
129
|
+ new Link(20, 31, "www.bar.com"));
|
|
130
|
+
|
|
131
|
+ testLinks("\"foo\" www.foo.com \"www.bar.com\"",
|
|
132
|
+ new Link(6, 17, "www.foo.com"),
|
|
133
|
+ new Link(19, 30, "www.bar.com"));
|
|
134
|
+ }
|
|
135
|
+
|
|
136
|
+ @Test
|
|
137
|
+ public void testIncompleteLinks() {
|
|
138
|
+ testLinks("www...");
|
|
139
|
+ testLinks("http://...");
|
|
140
|
+ testLinks("/var/web/www.google.com/blah");
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+}
|