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