Fix parsing of SSK links without document name
[Sone.git] / src / test / java / net / pterodactylus / sone / text / SoneTextParserTest.java
1 /*
2  * Sone - SoneTextParserTest.java - Copyright © 2011–2016 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 static java.lang.String.format;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.is;
23 import static org.hamcrest.Matchers.isIn;
24 import static org.hamcrest.Matchers.notNullValue;
25
26 import java.io.IOException;
27 import java.util.Collection;
28
29 import javax.annotation.Nonnull;
30 import javax.annotation.Nullable;
31
32 import net.pterodactylus.sone.data.Post;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.data.impl.IdOnlySone;
35 import net.pterodactylus.sone.database.PostProvider;
36 import net.pterodactylus.sone.database.SoneProvider;
37
38 import com.google.common.base.Optional;
39 import kotlin.jvm.functions.Function1;
40 import org.junit.Test;
41
42 /**
43  * JUnit test case for {@link SoneTextParser}.
44  */
45 public class SoneTextParserTest {
46
47         private final SoneTextParser soneTextParser = new SoneTextParser(null, null);
48
49         @SuppressWarnings("static-method")
50         @Test
51         public void testPlainText() throws IOException {
52                 /* check basic operation. */
53                 Iterable<Part> parts = soneTextParser.parse("Test.", null);
54                 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
55
56                 /* check empty lines at start and end. */
57                 parts = soneTextParser.parse("\nTest.\n\n", null);
58                 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
59
60                 /* check duplicate empty lines in the text. */
61                 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
62                 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
63         }
64
65         @Test
66         public void consecutiveLinesAreSeparatedByLinefeed() {
67                 Iterable<Part> parts = soneTextParser.parse("Text.\nText", null);
68                 assertThat("Part Text", convertText(parts), is("Text.\nText"));
69         }
70
71         @Test
72         public void freenetLinksHaveTheFreenetPrefixRemoved() {
73                 Iterable<Part> parts = soneTextParser.parse("freenet:KSK@gpl.txt", null);
74                 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
75         }
76
77         @Test
78         public void onlyTheFirstItemInALineIsPrefixedWithALineBreak() {
79                 Iterable<Part> parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null);
80                 assertThat("Part Text", convertText(parts), is("Text.\n[KSK@gpl.txt|KSK@gpl.txt|gpl.txt] and [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
81         }
82
83         @Test
84         public void soneLinkWithTooShortSoneIdIsRenderedAsPlainText() {
85                 Iterable<Part> parts = soneTextParser.parse("sone://too-short", null);
86                 assertThat("Part Text", convertText(parts), is("sone://too-short"));
87         }
88
89         @Test
90         public void soneLinkIsRenderedCorrectlyIfSoneIsNotPresent() {
91                 SoneTextParser parser = new SoneTextParser(new AbsentSoneProvider(), null);
92                 Iterable<Part> parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null);
93                 assertThat("Part Text", convertText(parts), is("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]"));
94         }
95
96         @Test
97         public void soneAndPostCanBeParsedFromTheSameText() {
98                 SoneTextParser parser = new SoneTextParser(new TestSoneProvider(), new TestPostProvider());
99                 Iterable<Part> parts = parser.parse("Text sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU more text post://f3757817-b45a-497a-803f-9c5aafc10dc6 even more text", null);
100                 assertThat("Part Text", convertText(parts), is("Text [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] more text [Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text] even more text"));
101         }
102
103         @Test
104         public void postLinkIsRenderedAsPlainTextIfPostIdIsTooShort() {
105                 Iterable<Part> parts = soneTextParser.parse("post://too-short", null);
106                 assertThat("Part Text", convertText(parts), is("post://too-short"));
107         }
108
109         @Test
110         public void postLinkIsRenderedCorrectlyIfPostIsPresent() {
111                 SoneTextParser parser = new SoneTextParser(null, new TestPostProvider());
112                 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
113                 assertThat("Part Text", convertText(parts), is("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]"));
114         }
115
116         @Test
117         public void postLinkIsRenderedAsPlainTextIfPostIsAbsent() {
118                 SoneTextParser parser = new SoneTextParser(null, new AbsentPostProvider());
119                 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
120                 assertThat("Part Text", convertText(parts), is("post://f3757817-b45a-497a-803f-9c5aafc10dc6"));
121         }
122
123         @Test
124         public void nameOfFreenetLinkDoesNotContainUrlParameters() {
125                 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null);
126                 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]"));
127         }
128
129         @Test
130         public void trailingSlashInFreenetLinkIsRemovedForName() {
131                 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt/", null);
132                 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]"));
133         }
134
135         @Test
136         public void lastMetaStringOfFreenetLinkIsUsedAsName() {
137                 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null);
138                 assertThat("Part Text", convertText(parts), is("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|COPYING]"));
139         }
140
141         @Test
142         public void freenetLinkWithoutMetaStringsAndDocNameGetsFirstNineCharactersOfKeyAsName() {
143                 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null);
144                 assertThat("Part Text", convertText(parts), is("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nm]"));
145         }
146
147         @Test
148         public void malformedKeyIsRenderedAsPlainText() {
149                 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU", null);
150                 assertThat("Part Text", convertText(parts), is("CHK@qM1nmgU"));
151         }
152
153         @Test
154         public void httpsLinkHasItsPathsShortened() {
155                 Iterable<Part> parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null);
156                 assertThat("Part Text", convertText(parts), is("[https://test.test/some-long-path/file.txt|https://test.test/some-long-path/file.txt|test.test/…/file.txt]"));
157         }
158
159         @Test
160         public void httpLinksHaveTheirLastSlashRemoved() {
161                 Iterable<Part> parts = soneTextParser.parse("http://test.test/test/", null);
162                 assertThat("Part Text", convertText(parts), is("[http://test.test/test/|http://test.test/test/|test.test/…]"));
163         }
164
165         @Test
166         public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndNoPath() {
167                 Iterable<Part> parts = soneTextParser.parse("http://www.test.test", null);
168                 assertThat("Part Text", convertText(parts), is("[http://www.test.test|http://www.test.test|test.test]"));
169         }
170
171         @Test
172         public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndAPath() {
173                 Iterable<Part> parts = soneTextParser.parse("http://www.test.test/test.html", null);
174                 assertThat("Part Text", convertText(parts), is("[http://www.test.test/test.html|http://www.test.test/test.html|test.test/test.html]"));
175         }
176
177         @Test
178         public void hostnameIsKeptIntactIfNotBeginningWithWww() {
179                 Iterable<Part> parts = soneTextParser.parse("http://test.test.test/test.html", null);
180                 assertThat("Part Text", convertText(parts), is("[http://test.test.test/test.html|http://test.test.test/test.html|test.test.test/test.html]"));
181         }
182
183         @Test
184         public void hostnameWithOneDotButNoSlashIsKeptIntact() {
185                 Iterable<Part> parts = soneTextParser.parse("http://test.test", null);
186                 assertThat("Part Text", convertText(parts), is("[http://test.test|http://test.test|test.test]"));
187         }
188
189         @Test
190         public void urlParametersAreRemovedForHttpLinks() {
191                 Iterable<Part> parts = soneTextParser.parse("http://test.test?foo=bar", null);
192                 assertThat("Part Text", convertText(parts), is("[http://test.test?foo=bar|http://test.test?foo=bar|test.test]"));
193         }
194
195         @Test
196         public void emptyStringIsParsedCorrectly() {
197                 Iterable<Part> parts = soneTextParser.parse("", null);
198                 assertThat("Part Text", convertText(parts), is(""));
199         }
200
201         @Test
202         public void linksAreParsedInCorrectOrder() {
203                 Iterable<Part> parts = soneTextParser.parse("KSK@ CHK@", null);
204                 assertThat("Part Text", convertText(parts), is("KSK@ CHK@"));
205         }
206
207         @Test
208         public void invalidSskAndUskLinkIsParsedAsText() {
209                 Iterable<Part> parts = soneTextParser.parse("SSK@a USK@a", null);
210                 assertThat("Part Text", convertText(parts), is("SSK@a USK@a"));
211         }
212
213         @Test
214         public void sskWithoutDocumentNameIsParsedCorrectly() {
215                 Iterable<Part> parts = soneTextParser.parse(
216                                 "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8",
217                                 null);
218                 assertThat("Part Text", convertText(parts),
219                                 is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|"
220                                                 + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|"
221                                                 + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU]"));
222         }
223
224         @Test
225         public void sskLinkWithoutContextIsNotTrusted() {
226                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
227                 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));
228         }
229
230         @Test
231         public void sskLinkWithContextWithoutSoneIsNotTrusted() {
232                 SoneTextParserContext context = new SoneTextParserContext(null);
233                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
234                 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));
235         }
236
237         @Test
238         public void sskLinkWithContextWithDifferentSoneIsNotTrusted() {
239                 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
240                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
241                 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));
242         }
243
244         @Test
245         public void sskLinkWithContextWithCorrectSoneIsTrusted() {
246                 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
247                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
248                 assertThat("Part Text", convertText(parts), is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|trusted|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));
249         }
250
251         @Test
252         public void uskLinkWithContextWithCorrectSoneIsTrusted() {
253                 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
254                 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
255                 assertThat("Part Text", convertText(parts), is("[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]"));
256         }
257
258         @SuppressWarnings("static-method")
259         @Test
260         public void testKSKLinks() throws IOException {
261                 /* check basic links. */
262                 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
263                 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
264
265                 /* check embedded links. */
266                 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
267                 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."));
268
269                 /* check embedded links and line breaks. */
270                 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
271                 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\n[KSK@test.dat|KSK@test.dat|test.dat]"));
272         }
273
274         @SuppressWarnings({ "synthetic-access", "static-method" })
275         @Test
276         public void testEmptyLinesAndSoneLinks() throws IOException {
277                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
278
279                 /* check basic links. */
280                 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
281                 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
282         }
283
284         @SuppressWarnings({ "synthetic-access", "static-method" })
285         @Test
286         public void testEmpyHttpLinks() throws IOException {
287                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
288
289                 /* check empty http links. */
290                 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
291                 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
292         }
293
294         @Test
295         public void httpLinkWithoutParensEndsAtNextClosingParen() {
296                 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
297                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text (and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]) – nice!"));
298         }
299
300         @Test
301         public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
302                 Iterable<Part> 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), is("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         public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
308                 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
309                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("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         public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
314                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
315                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]. Nice!"));
316         }
317
318         @Test
319         public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
320                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
321                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]... Nice!"));
322         }
323
324         @Test
325         public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
326                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
327                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc], nice!"));
328         }
329
330         @Test
331         public void exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
332                 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
333                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"));
334         }
335
336         @Test
337         public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
338                 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc?", null);
339                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"));
340         }
341
342         @Test
343         public void correctFreemailAddressIsLinkedToCorrectly() {
344                 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
345                 assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"));
346         }
347
348         @Test
349         public void freemailAddressWithInvalidFreemailIdIsParsedAsText() {
350                 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null);
351                 assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"));
352         }
353
354         @Test
355         public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() {
356                 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
357                 assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
358         }
359
360         @Test
361         public void freemailAddressWithoutLocalPartIsParsedAsText() {
362                 Iterable<Part> parts = soneTextParser.parse("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
363                 assertThat("Part Text", convertText(parts), is("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
364         }
365
366         @Test
367         public void correctFreemailAddressIsParsedCorrectly() {
368                 Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
369                 assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
370         }
371
372         @Test
373         public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
374                 Iterable<Part> parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
375                 assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
376         }
377
378         /**
379          * Converts all given {@link Part}s into a string, validating that the
380          * part’s classes match only the expected classes.
381          *
382          * @param parts
383          *            The parts to convert to text
384          * @param validClasses
385          *            The valid classes; if no classes are given, all classes are
386          *            valid
387          * @return The converted text
388          */
389         private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
390                 StringBuilder text = new StringBuilder();
391                 for (Part part : parts) {
392                         assertThat("Part", part, notNullValue());
393                         if (validClasses.length != 0) {
394                                 assertThat("Part’s class", part.getClass(), isIn(validClasses));
395                         }
396                         if (part instanceof PlainTextPart) {
397                                 text.append(((PlainTextPart) part).getText());
398                         } else if (part instanceof FreenetLinkPart) {
399                                 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
400                                 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
401                         } else if (part instanceof FreemailPart) {
402                                 FreemailPart freemailPart = (FreemailPart) part;
403                                 text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId()));
404                         } else if (part instanceof LinkPart) {
405                                 LinkPart linkPart = (LinkPart) part;
406                                 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
407                         } else if (part instanceof SonePart) {
408                                 SonePart sonePart = (SonePart) part;
409                                 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
410                         } else if (part instanceof PostPart) {
411                                 PostPart postPart = (PostPart) part;
412                                 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
413                         }
414                 }
415                 return text.toString();
416         }
417
418         /**
419          * Mock Sone provider.
420          */
421         private static class TestSoneProvider implements SoneProvider {
422
423                 @Nonnull
424                 @Override
425                 public Function1<String, Sone> getSoneLoader() {
426                         return new Function1<String, Sone>() {
427                                 @Override
428                                 public Sone invoke(String soneId) {
429                                         return getSone(soneId);
430                                 }
431                         };
432                 }
433
434                 @Nullable
435                 @Override
436                 public Sone getSone(final String soneId) {
437                         return new IdOnlySone(soneId);
438                 }
439
440                 /**
441                  * {@inheritDocs}
442                  */
443                 @Override
444                 public Collection<Sone> getSones() {
445                         return null;
446                 }
447
448                 /**
449                  * {@inheritDocs}
450                  */
451                 @Override
452                 public Collection<Sone> getLocalSones() {
453                         return null;
454                 }
455
456                 /**
457                  * {@inheritDocs}
458                  */
459                 @Override
460                 public Collection<Sone> getRemoteSones() {
461                         return null;
462                 }
463
464         }
465
466         private static class AbsentSoneProvider extends TestSoneProvider {
467
468                 @Override
469                 public Sone getSone(String soneId) {
470                         return null;
471                 }
472
473         }
474
475         private static class TestPostProvider implements PostProvider {
476
477                 @Nullable
478                 @Override
479                 public Post getPost(@Nonnull final String postId) {
480                         return new Post() {
481                                 @Override
482                                 public String getId() {
483                                         return postId;
484                                 }
485
486                                 @Override
487                                 public boolean isLoaded() {
488                                         return false;
489                                 }
490
491                                 @Override
492                                 public Sone getSone() {
493                                         return null;
494                                 }
495
496                                 @Override
497                                 public Optional<String> getRecipientId() {
498                                         return null;
499                                 }
500
501                                 @Override
502                                 public Optional<Sone> getRecipient() {
503                                         return null;
504                                 }
505
506                                 @Override
507                                 public long getTime() {
508                                         return 0;
509                                 }
510
511                                 @Override
512                                 public String getText() {
513                                         return "text";
514                                 }
515
516                                 @Override
517                                 public boolean isKnown() {
518                                         return false;
519                                 }
520
521                                 @Override
522                                 public Post setKnown(boolean known) {
523                                         return null;
524                                 }
525                         };
526                 }
527
528                 @Override
529                 public Collection<Post> getPosts(String soneId) {
530                         return null;
531                 }
532
533                 @Override
534                 public Collection<Post> getDirectedPosts(String recipientId) {
535                         return null;
536                 }
537
538         }
539
540         private static class AbsentPostProvider extends TestPostProvider {
541
542                 @Nullable
543                 @Override
544                 public Post getPost(@Nonnull String postId) {
545                         return null;
546                 }
547
548         }
549
550 }