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