2 * Sone - SoneTextParserTest.kt - Copyright © 2011–2020 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.text
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.*
30 * JUnit test case for [SoneTextParser].
32 class SoneTextParserTest {
34 private val soneTextParser = SoneTextParser(null, null)
37 fun `basic operation`() {
38 val parts = soneTextParser.parse("Test.", null)
39 assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("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."))
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."))
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"))
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]"))
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]"))
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"))
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]"))
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"))
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"))
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]"))
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"))
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]"))
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]"))
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]"))
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]"))
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"))
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]"))
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/…]"))
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]"))
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]"))
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]"))
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]"))
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]"))
185 fun `empty string is parsed correctly`() {
186 val parts = soneTextParser.parse("", null)
187 assertThat("Part Text", convertText(parts), equalTo(""))
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@"))
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"))
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]"))
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]"))
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]"))
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]"))
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]"))
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]"))
247 fun `usk links with backlinks is parsed correctly`() {
248 val context = SoneTextParserContext(IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"))
249 val parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/../../../USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/78/", context)
250 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]"))
254 fun `test basic ksk links`() {
255 val parts: Iterable<Part> = soneTextParser.parse("KSK@gpl.txt", null)
256 assertThat("Part Text", convertText(parts, FreenetLinkPart::class.java), equalTo("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"))
260 fun `embedded ksk links are parsed correctly`() {
261 val parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null)
262 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, FreenetLinkPart::class.java), equalTo("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."))
266 fun `embedded ksk links and line breaks are parsed correctly`() {
267 val parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null)
268 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]"))
272 fun `ksk links with backlinks are parsed correctly`() {
273 val parts = soneTextParser.parse("KSK@gallery/../Sone/imageBrowser.html?album=30c930ee-97cd-11e9-bd44-f3e595768b77", null)
274 assertThat("Part Text", convertText(parts, FreenetLinkPart::class.java), equalTo("[KSK@gallery|KSK@gallery|gallery]"))
278 fun `test empty lines and sone links`() {
279 val soneTextParser = SoneTextParser(TestSoneProvider(), null)
281 /* check basic links. */
282 val parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null)
283 assertThat("Part Text", convertText(parts, PlainTextPart::class.java, SonePart::class.java), equalTo("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."))
287 fun `test empy http links`() {
288 val soneTextParser = SoneTextParser(TestSoneProvider(), null)
290 /* check empty http links. */
291 val parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null)
292 assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Some text. Empty link: http:// – nice!"))
296 fun `http link without parens ends at next closing paren`() {
297 val parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null)
298 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!"))
302 fun `usk link ends at first non numeric non slash character after version number`() {
303 val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null)
304 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"))
308 fun `usk link with filename shows the filename`() {
309 val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg). Nice", null)
310 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"))
314 fun `usk link without filename but ending in slash shows the path`() {
315 val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/). Nice", null)
316 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"))
320 fun `http link with opened and closed parens ends at next closing paren`() {
321 val parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null)
322 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!"))
326 fun `punctuation is ignored at end of link before whitespace`() {
327 val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null)
328 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!"))
332 fun `multiple punctuation characters are ignored at end of link before whitespace`() {
333 val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null)
334 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!"))
338 fun `commas are ignored at end of link before whitespace`() {
339 val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null)
340 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!"))
344 fun `exclamation marks are ignored at end of link before whitespace`() {
345 val parts = soneTextParser.parse("A link: http://example.sone/abc!", null)
346 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]!"))
350 fun `question marks are ignored at end of link before whitespace`() {
351 val parts = soneTextParser.parse("A link: http://example.sone/abc?", null)
352 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]?"))
356 fun `correct freemail address is linked to correctly`() {
357 val parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null)
358 assertThat("Part Text", convertText(parts), equalTo("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"))
362 fun `freemail address with invalid freemail id is parsed as text`() {
363 val parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null)
364 assertThat("Part Text", convertText(parts), equalTo("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"))
368 fun `freemail address with invalid sized freemail id is parsed as text`() {
369 val parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null)
370 assertThat("Part Text", convertText(parts), equalTo("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"))
374 fun `freemail address without local part is parsed as text`() {
375 val parts = soneTextParser.parse(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null)
376 assertThat("Part Text", convertText(parts), equalTo(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"))
380 fun `local part of freemail address can contain letters digits minus dot underscore`() {
381 val parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null)
382 assertThat("Part Text", convertText(parts), equalTo("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"))
385 private fun convertText(parts: Iterable<Part>, vararg validClasses: Class<*>): String {
386 if (validClasses.isNotEmpty()) {
387 assertThat(parts.map { it.javaClass }.distinct() - validClasses.distinct(), empty())
389 return parts.joinToString("") { part ->
391 is PlainTextPart -> part.text
392 is FreenetLinkPart -> "[${part.link}|${if (part.trusted) "trusted|" else ""}${part.title}|${part.text}]"
393 is FreemailPart -> "[Freemail|${part.emailLocalPart}|${part.freemailId}|${part.identityId}]"
394 is LinkPart -> "[${part.link}|${part.title}|${part.text}]"
395 is SonePart -> "[Sone|${part.sone.id}]"
396 is PostPart -> "[Post|${part.post.id}|${part.post.text}]"
397 else -> throw NoSuchElementException()
403 fun `parser can be created by guice`() {
404 val injector = createInjector(
405 SoneProvider::class.isProvidedByMock(),
406 PostProvider::class.isProvidedByMock()
408 assertThat(injector.getInstance<SoneTextParser>(), notNullValue())
412 * Mock Sone provider.
414 private open class TestSoneProvider : SoneProvider {
416 override val soneLoader = this::getSone
417 override val sones: Collection<Sone> = emptySet()
418 override val localSones: Collection<Sone> = emptySet()
419 override val remoteSones: Collection<Sone> = emptySet()
421 override fun getSone(soneId: String): Sone? = IdOnlySone(soneId)
425 private class AbsentSoneProvider : TestSoneProvider() {
427 override fun getSone(soneId: String): Sone? = null
431 private open class TestPostProvider : PostProvider {
433 override fun getPost(postId: String): Post? {
434 return object : Post {
435 override val id = postId
436 override fun isLoaded() = false
437 override fun getSone() = null
438 override fun getRecipientId() = null
439 override fun getRecipient() = null
440 override fun getTime() = 0L
441 override fun getText() = "text"
442 override fun isKnown() = false
443 override fun setKnown(known: Boolean) = null
447 override fun getPosts(soneId: String) = emptySet<Post>()
448 override fun getDirectedPosts(recipientId: String) = emptySet<Post>()
452 private class AbsentPostProvider : TestPostProvider() {
454 override fun getPost(postId: String): Post? = null