9ea97d9edc73a603ea8756cb9fb6820e70450f8c
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / text / SoneTextParserTest.kt
1 /*
2  * Sone - SoneTextParserTest.java - Copyright © 2011–2019 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.text
19
20 import com.google.inject.Guice.*
21 import net.pterodactylus.sone.data.*
22 import net.pterodactylus.sone.data.impl.*
23 import net.pterodactylus.sone.database.*
24 import net.pterodactylus.sone.test.*
25 import org.hamcrest.MatcherAssert.*
26 import org.hamcrest.Matchers.*
27 import org.junit.*
28
29 /**
30  * JUnit test case for [SoneTextParser].
31  */
32 class SoneTextParserTest {
33
34         private val soneTextParser = SoneTextParser(null, null)
35
36         @Test
37         fun `basic operation`() {
38                 val parts = soneTextParser.parse("Test.", null)
39                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Test."))
40         }
41
42         @Test
43         fun `empty lines at start and end are stripped`() {
44                 val parts = soneTextParser.parse("\nTest.\n\n", null)
45                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Test."))
46         }
47
48         @Test
49         fun `duplicate empty lines in the text are stripped`() {
50                 val parts = soneTextParser.parse("\nTest.\n\n\nTest.", null)
51                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Test.\n\nTest."))
52         }
53
54         @Test
55         fun `consecutive lines are separated by linefeed`() {
56                 val parts = soneTextParser.parse("Text.\nText", null)
57                 assertThat("Part Text", convertText(parts), equalTo("Text.\nText"))
58         }
59
60         @Test
61         fun `freenet links have the freenet prefix removed`() {
62                 val parts = soneTextParser.parse("freenet:KSK@gpl.txt", null)
63                 assertThat("Part Text", convertText(parts), equalTo("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"))
64         }
65
66         @Test
67         fun `only the first item in a line is prefixed with a line break`() {
68                 val parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null)
69                 assertThat("Part Text", convertText(parts), equalTo("Text.\n[KSK@gpl.txt|KSK@gpl.txt|gpl.txt] and [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"))
70         }
71
72         @Test
73         fun `sone link with too short sone ID is rendered as plain text`() {
74                 val parts = soneTextParser.parse("sone://too-short", null)
75                 assertThat("Part Text", convertText(parts), equalTo("sone://too-short"))
76         }
77
78         @Test
79         fun `sone link is rendered correctly if sone is not present`() {
80                 val parser = SoneTextParser(AbsentSoneProvider(), null)
81                 val parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null)
82                 assertThat("Part Text", convertText(parts), equalTo("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]"))
83         }
84
85         @Test
86         fun `sone and post can be parsed from the same text`() {
87                 val parser = SoneTextParser(TestSoneProvider(), TestPostProvider())
88                 val parts = parser.parse("Text sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU more text post://f3757817-b45a-497a-803f-9c5aafc10dc6 even more text", null)
89                 assertThat("Part Text", convertText(parts), equalTo("Text [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] more text [Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text] even more text"))
90         }
91
92         @Test
93         fun `post link is rendered as plain text if post ID is too short`() {
94                 val parts = soneTextParser.parse("post://too-short", null)
95                 assertThat("Part Text", convertText(parts), equalTo("post://too-short"))
96         }
97
98         @Test
99         fun `post link is rendered correctly if post is present`() {
100                 val parser = SoneTextParser(null, TestPostProvider())
101                 val parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null)
102                 assertThat("Part Text", convertText(parts), equalTo("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]"))
103         }
104
105         @Test
106         fun `post link is rendered as plain text if post is absent`() {
107                 val parser = SoneTextParser(null, AbsentPostProvider())
108                 val parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null)
109                 assertThat("Part Text", convertText(parts), equalTo("post://f3757817-b45a-497a-803f-9c5aafc10dc6"))
110         }
111
112         @Test
113         fun `name of freenet link does not contain url parameters`() {
114                 val parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null)
115                 assertThat("Part Text", convertText(parts), equalTo("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]"))
116         }
117
118         @Test
119         fun `trailing slash in freenet link is removed for name`() {
120                 val parts = soneTextParser.parse("KSK@gpl.txt/", null)
121                 assertThat("Part Text", convertText(parts), equalTo("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]"))
122         }
123
124         @Test
125         fun `last meta string of freenet link is used as name`() {
126                 val parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null)
127                 assertThat("Part Text", convertText(parts), equalTo("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|COPYING]"))
128         }
129
130         @Test
131         fun `freenet link without meta strings and doc name gets first nine characters of key as name`() {
132                 val parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null)
133                 assertThat("Part Text", convertText(parts), equalTo("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nm]"))
134         }
135
136         @Test
137         fun `malformed key is rendered as plain text`() {
138                 val parts = soneTextParser.parse("CHK@qM1nmgU", null)
139                 assertThat("Part Text", convertText(parts), equalTo("CHK@qM1nmgU"))
140         }
141
142         @Test
143         fun `https link has its paths shortened`() {
144                 val parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null)
145                 assertThat("Part Text", convertText(parts), equalTo("[https://test.test/some-long-path/file.txt|https://test.test/some-long-path/file.txt|test.test/…/file.txt]"))
146         }
147
148         @Test
149         fun `http links have their last slash removed`() {
150                 val parts = soneTextParser.parse("http://test.test/test/", null)
151                 assertThat("Part Text", convertText(parts), equalTo("[http://test.test/test/|http://test.test/test/|test.test/…]"))
152         }
153
154         @Test
155         fun `www prefix is removed for hostname with two dots and no path`() {
156                 val parts = soneTextParser.parse("http://www.test.test", null)
157                 assertThat("Part Text", convertText(parts), equalTo("[http://www.test.test|http://www.test.test|test.test]"))
158         }
159
160         @Test
161         fun `www prefix is removed for hostname with two dots and a path`() {
162                 val parts = soneTextParser.parse("http://www.test.test/test.html", null)
163                 assertThat("Part Text", convertText(parts), equalTo("[http://www.test.test/test.html|http://www.test.test/test.html|test.test/test.html]"))
164         }
165
166         @Test
167         fun `hostname is kept intact if not beginning with www`() {
168                 val parts = soneTextParser.parse("http://test.test.test/test.html", null)
169                 assertThat("Part Text", convertText(parts), equalTo("[http://test.test.test/test.html|http://test.test.test/test.html|test.test.test/test.html]"))
170         }
171
172         @Test
173         fun `hostname with one dot but no slash is kept intact`() {
174                 val parts = soneTextParser.parse("http://test.test", null)
175                 assertThat("Part Text", convertText(parts), equalTo("[http://test.test|http://test.test|test.test]"))
176         }
177
178         @Test
179         fun `url parameters are removed for http links`() {
180                 val parts = soneTextParser.parse("http://test.test?foo=bar", null)
181                 assertThat("Part Text", convertText(parts), equalTo("[http://test.test?foo=bar|http://test.test?foo=bar|test.test]"))
182         }
183
184         @Test
185         fun `empty string is parsed correctly`() {
186                 val parts = soneTextParser.parse("", null)
187                 assertThat("Part Text", convertText(parts), equalTo(""))
188         }
189
190         @Test
191         fun `links are parsed in correct order`() {
192                 val parts = soneTextParser.parse("KSK@ CHK@", null)
193                 assertThat("Part Text", convertText(parts), equalTo("KSK@ CHK@"))
194         }
195
196         @Test
197         fun `invalid ssk and usk link is parsed as text`() {
198                 val parts = soneTextParser.parse("SSK@a USK@a", null)
199                 assertThat("Part Text", convertText(parts), equalTo("SSK@a USK@a"))
200         }
201
202         @Test
203         fun `ssk without document name is parsed correctly`() {
204                 val parts = soneTextParser.parse(
205                                 "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null)
206                 assertThat("Part Text", convertText(parts),
207                                 equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|"
208                                                 + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|"
209                                                 + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU]"))
210         }
211
212         @Test
213         fun `ssk link without context is not trusted`() {
214                 val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null)
215                 assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"))
216         }
217
218         @Test
219         fun `ssk link with context without sone is not trusted`() {
220                 val context = SoneTextParserContext(null)
221                 val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context)
222                 assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"))
223         }
224
225         @Test
226         fun `ssk link with context with different sone is not trusted`() {
227                 val context = SoneTextParserContext(IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"))
228                 val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context)
229                 assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"))
230         }
231
232         @Test
233         fun `ssk link with context with correct sone is trusted`() {
234                 val context = SoneTextParserContext(IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"))
235                 val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context)
236                 assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|trusted|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"))
237         }
238
239         @Test
240         fun `usk link with context with correct sone is trusted`() {
241                 val context = SoneTextParserContext(IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"))
242                 val parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context)
243                 assertThat("Part Text", convertText(parts), equalTo("[USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|trusted|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|test]"))
244         }
245
246         @Test
247         fun `test basic ksk links`() {
248                 val parts: Iterable<Part> = soneTextParser.parse("KSK@gpl.txt", null)
249                 assertThat("Part Text", convertText(parts, FreenetLinkPart::class.java), equalTo("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"))
250         }
251
252         @Test
253         fun `embedded ksk links are parsed correctly`() {
254                 val parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null)
255                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, FreenetLinkPart::class.java), equalTo("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."))
256         }
257
258         @Test
259         fun `embedded ksk links and line breaks are parsed correctly`() {
260                 val parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null)
261                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, FreenetLinkPart::class.java), equalTo("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\n[KSK@test.dat|KSK@test.dat|test.dat]"))
262         }
263
264         @Test
265         fun `test empty lines and sone links`() {
266                 val soneTextParser = SoneTextParser(TestSoneProvider(), null)
267
268                 /* check basic links. */
269                 val parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null)
270                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, SonePart::class.java), equalTo("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."))
271         }
272
273         @Test
274         fun `test empy http links`() {
275                 val soneTextParser = SoneTextParser(TestSoneProvider(), null)
276
277                 /* check empty http links. */
278                 val parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null)
279                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Some text. Empty link: http:// – nice!"))
280         }
281
282         @Test
283         fun `http link without parens ends at next closing paren`() {
284                 val parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null)
285                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text (and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]) – nice!"))
286         }
287
288         @Test
289         fun `usk link ends at first non numeric non slash character after version number`() {
290                 val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null)
291                 assertThat("Part Text", convertText(parts), equalTo("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|test]). Nice"))
292         }
293
294         @Test
295         fun `usk link with filename shows the filename`() {
296                 val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg). Nice", null)
297                 assertThat("Part Text", convertText(parts), equalTo("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg|image.jpg]). Nice"))
298         }
299
300         @Test
301         fun `usk link without filename but ending in slash shows the path`() {
302                 val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/). Nice", null)
303                 assertThat("Part Text", convertText(parts), equalTo("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|test]). Nice"))
304         }
305
306         @Test
307         fun `http link with opened and closed parens ends at next closing paren`() {
308                 val parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null)
309                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text (and a link: [http://example.sone/abc_(def)|http://example.sone/abc_(def)|example.sone/abc_(def)]) – nice!"))
310         }
311
312         @Test
313         fun `punctuation is ignored at end of link before whitespace`() {
314                 val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null)
315                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]. Nice!"))
316         }
317
318         @Test
319         fun `multiple punctuation characters are ignored at end of link before whitespace`() {
320                 val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null)
321                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]... Nice!"))
322         }
323
324         @Test
325         fun `commas are ignored at end of link before whitespace`() {
326                 val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null)
327                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc], nice!"))
328         }
329
330         @Test
331         fun `exclamation marks are ignored at end of link before whitespace`() {
332                 val parts = soneTextParser.parse("A link: http://example.sone/abc!", null)
333                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"))
334         }
335
336         @Test
337         fun `question marks are ignored at end of link before whitespace`() {
338                 val parts = soneTextParser.parse("A link: http://example.sone/abc?", null)
339                 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"))
340         }
341
342         @Test
343         fun `correct freemail address is linked to correctly`() {
344                 val parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null)
345                 assertThat("Part Text", convertText(parts), equalTo("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"))
346         }
347
348         @Test
349         fun `freemail address with invalid freemail id is parsed as text`() {
350                 val parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null)
351                 assertThat("Part Text", convertText(parts), equalTo("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"))
352         }
353
354         @Test
355         fun `freemail address with invalid sized freemail id is parsed as text`() {
356                 val parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null)
357                 assertThat("Part Text", convertText(parts), equalTo("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"))
358         }
359
360         @Test
361         fun `freemail address without local part is parsed as text`() {
362                 val parts = soneTextParser.parse("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null)
363                 assertThat("Part Text", convertText(parts), equalTo("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"))
364         }
365
366         @Test
367         fun `local part of freemail address can contain letters digits minus dot underscore`() {
368                 val parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null)
369                 assertThat("Part Text", convertText(parts), equalTo("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"))
370         }
371
372         private fun convertText(parts: Iterable<Part>, vararg validClasses: Class<*>): String {
373                 if (validClasses.isNotEmpty()) {
374                         assertThat(parts.map { it.javaClass }.distinct() - validClasses.distinct(), empty())
375                 }
376                 return parts.joinToString("") { part ->
377                         when (part) {
378                                 is PlainTextPart -> part.text
379                                 is FreenetLinkPart -> "[${part.link}|${if (part.trusted) "trusted|" else ""}${part.title}|${part.text}]"
380                                 is FreemailPart -> "[Freemail|${part.emailLocalPart}|${part.freemailId}|${part.identityId}]"
381                                 is LinkPart -> "[${part.link}|${part.title}|${part.text}]"
382                                 is SonePart -> "[Sone|${part.sone.id}]"
383                                 is PostPart -> "[Post|${part.post.id}|${part.post.text}]"
384                                 else -> throw NoSuchElementException()
385                         }
386                 }
387         }
388
389         @Test
390         fun `parser can be created by guice`() {
391                 val injector = createInjector(
392                                 SoneProvider::class.isProvidedByMock(),
393                                 PostProvider::class.isProvidedByMock()
394                 )
395                 assertThat(injector.getInstance<SoneTextParser>(), notNullValue())
396         }
397
398         /**
399          * Mock Sone provider.
400          */
401         private open class TestSoneProvider : SoneProvider {
402
403                 override val soneLoader = this::getSone
404                 override val sones: Collection<Sone> = emptySet()
405                 override val localSones: Collection<Sone> = emptySet()
406                 override val remoteSones: Collection<Sone> = emptySet()
407
408                 override fun getSone(soneId: String): Sone? = IdOnlySone(soneId)
409
410         }
411
412         private class AbsentSoneProvider : TestSoneProvider() {
413
414                 override fun getSone(soneId: String): Sone? = null
415
416         }
417
418         private open class TestPostProvider : PostProvider {
419
420                 override fun getPost(postId: String): Post? {
421                         return object : Post {
422                                 override val id = postId
423                                 override fun isLoaded() = false
424                                 override fun getSone() = null
425                                 override fun getRecipientId() = null
426                                 override fun getRecipient() = null
427                                 override fun getTime() = 0L
428                                 override fun getText() = "text"
429                                 override fun isKnown() = false
430                                 override fun setKnown(known: Boolean) = null
431                         }
432                 }
433
434                 override fun getPosts(soneId: String) = emptySet<Post>()
435                 override fun getDirectedPosts(recipientId: String) = emptySet<Post>()
436
437         }
438
439         private class AbsentPostProvider : TestPostProvider() {
440
441                 override fun getPost(postId: String): Post? = null
442
443         }
444
445 }