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