ff2a175a30db9c22cbb5940fffb47f1ad117bf99
[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 sskLinkWithoutContextIsNotTrusted() {
215                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
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 sskLinkWithContextWithoutSoneIsNotTrusted() {
221                 SoneTextParserContext context = new SoneTextParserContext(null);
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|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));
224         }
225
226         @Test
227         public void sskLinkWithContextWithDifferentSoneIsNotTrusted() {
228                 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
229                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
230                 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]"));
231         }
232
233         @Test
234         public void sskLinkWithContextWithCorrectSoneIsTrusted() {
235                 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
236                 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
237                 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]"));
238         }
239
240         @Test
241         public void uskLinkWithContextWithCorrectSoneIsTrusted() {
242                 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
243                 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
244                 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]"));
245         }
246
247         @SuppressWarnings("static-method")
248         @Test
249         public void testKSKLinks() throws IOException {
250                 /* check basic links. */
251                 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
252                 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
253
254                 /* check embedded links. */
255                 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
256                 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."));
257
258                 /* check embedded links and line breaks. */
259                 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
260                 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]"));
261         }
262
263         @SuppressWarnings({ "synthetic-access", "static-method" })
264         @Test
265         public void testEmptyLinesAndSoneLinks() throws IOException {
266                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
267
268                 /* check basic links. */
269                 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
270                 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
271         }
272
273         @SuppressWarnings({ "synthetic-access", "static-method" })
274         @Test
275         public void testEmpyHttpLinks() throws IOException {
276                 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
277
278                 /* check empty http links. */
279                 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
280                 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
281         }
282
283         @Test
284         public void httpLinkWithoutParensEndsAtNextClosingParen() {
285                 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
286                 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!"));
287         }
288
289         @Test
290         public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
291                 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null);
292                 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"));
293         }
294
295         @Test
296         public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
297                 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
298                 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!"));
299         }
300
301         @Test
302         public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
303                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
304                 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!"));
305         }
306
307         @Test
308         public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
309                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
310                 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!"));
311         }
312
313         @Test
314         public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
315                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
316                 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!"));
317         }
318
319         @Test
320         public void exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
321                 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
322                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"));
323         }
324
325         @Test
326         public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
327                 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc?", null);
328                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"));
329         }
330
331         @Test
332         public void correctFreemailAddressIsLinkedToCorrectly() {
333                 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
334                 assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"));
335         }
336
337         @Test
338         public void freemailAddressWithInvalidFreemailIdIsParsedAsText() {
339                 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null);
340                 assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"));
341         }
342
343         @Test
344         public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() {
345                 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
346                 assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
347         }
348
349         @Test
350         public void freemailAddressWithoutLocalPartIsParsedAsText() {
351                 Iterable<Part> parts = soneTextParser.parse("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
352                 assertThat("Part Text", convertText(parts), is("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
353         }
354
355         @Test
356         public void correctFreemailAddressIsParsedCorrectly() {
357                 Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
358                 assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
359         }
360
361         @Test
362         public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
363                 Iterable<Part> parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
364                 assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
365         }
366
367         /**
368          * Converts all given {@link Part}s into a string, validating that the
369          * part’s classes match only the expected classes.
370          *
371          * @param parts
372          *            The parts to convert to text
373          * @param validClasses
374          *            The valid classes; if no classes are given, all classes are
375          *            valid
376          * @return The converted text
377          */
378         private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
379                 StringBuilder text = new StringBuilder();
380                 for (Part part : parts) {
381                         assertThat("Part", part, notNullValue());
382                         if (validClasses.length != 0) {
383                                 assertThat("Part’s class", part.getClass(), isIn(validClasses));
384                         }
385                         if (part instanceof PlainTextPart) {
386                                 text.append(((PlainTextPart) part).getText());
387                         } else if (part instanceof FreenetLinkPart) {
388                                 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
389                                 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
390                         } else if (part instanceof FreemailPart) {
391                                 FreemailPart freemailPart = (FreemailPart) part;
392                                 text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId()));
393                         } else if (part instanceof LinkPart) {
394                                 LinkPart linkPart = (LinkPart) part;
395                                 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
396                         } else if (part instanceof SonePart) {
397                                 SonePart sonePart = (SonePart) part;
398                                 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
399                         } else if (part instanceof PostPart) {
400                                 PostPart postPart = (PostPart) part;
401                                 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
402                         }
403                 }
404                 return text.toString();
405         }
406
407         /**
408          * Mock Sone provider.
409          */
410         private static class TestSoneProvider implements SoneProvider {
411
412                 @Nonnull
413                 @Override
414                 public Function1<String, Sone> getSoneLoader() {
415                         return new Function1<String, Sone>() {
416                                 @Override
417                                 public Sone invoke(String soneId) {
418                                         return getSone(soneId);
419                                 }
420                         };
421                 }
422
423                 @Nullable
424                 @Override
425                 public Sone getSone(final String soneId) {
426                         return new IdOnlySone(soneId);
427                 }
428
429                 /**
430                  * {@inheritDocs}
431                  */
432                 @Override
433                 public Collection<Sone> getSones() {
434                         return null;
435                 }
436
437                 /**
438                  * {@inheritDocs}
439                  */
440                 @Override
441                 public Collection<Sone> getLocalSones() {
442                         return null;
443                 }
444
445                 /**
446                  * {@inheritDocs}
447                  */
448                 @Override
449                 public Collection<Sone> getRemoteSones() {
450                         return null;
451                 }
452
453         }
454
455         private static class AbsentSoneProvider extends TestSoneProvider {
456
457                 @Override
458                 public Sone getSone(String soneId) {
459                         return null;
460                 }
461
462         }
463
464         private static class TestPostProvider implements PostProvider {
465
466                 @Nullable
467                 @Override
468                 public Post getPost(@Nonnull final String postId) {
469                         return new Post() {
470                                 @Override
471                                 public String getId() {
472                                         return postId;
473                                 }
474
475                                 @Override
476                                 public boolean isLoaded() {
477                                         return false;
478                                 }
479
480                                 @Override
481                                 public Sone getSone() {
482                                         return null;
483                                 }
484
485                                 @Override
486                                 public Optional<String> getRecipientId() {
487                                         return null;
488                                 }
489
490                                 @Override
491                                 public Optional<Sone> getRecipient() {
492                                         return null;
493                                 }
494
495                                 @Override
496                                 public long getTime() {
497                                         return 0;
498                                 }
499
500                                 @Override
501                                 public String getText() {
502                                         return "text";
503                                 }
504
505                                 @Override
506                                 public boolean isKnown() {
507                                         return false;
508                                 }
509
510                                 @Override
511                                 public Post setKnown(boolean known) {
512                                         return null;
513                                 }
514                         };
515                 }
516
517                 @Override
518                 public Collection<Post> getPosts(String soneId) {
519                         return null;
520                 }
521
522                 @Override
523                 public Collection<Post> getDirectedPosts(String recipientId) {
524                         return null;
525                 }
526
527         }
528
529         private static class AbsentPostProvider extends TestPostProvider {
530
531                 @Nullable
532                 @Override
533                 public Post getPost(@Nonnull String postId) {
534                         return null;
535                 }
536
537         }
538
539 }