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