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