446ef1bc4c27e73b151042481c636b1a37209b8a
[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 uskLinkWithFilenameShowsTheFilename() {
311                 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg). Nice", null);
312                 assertThat("Part Text", convertText(parts), is("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg|image.jpg]). Nice"));
313         }
314
315         @Test
316         public void uskLinkWithoutFilenameButEndingInSlashShowsThePath() {
317                 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/). Nice", null);
318                 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"));
319         }
320
321         @Test
322         public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
323                 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
324                 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!"));
325         }
326
327         @Test
328         public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
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 multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
335                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
336                 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!"));
337         }
338
339         @Test
340         public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
341                 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
342                 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!"));
343         }
344
345         @Test
346         public void exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
347                 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
348                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"));
349         }
350
351         @Test
352         public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
353                 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc?", null);
354                 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"));
355         }
356
357         @Test
358         public void correctFreemailAddressIsLinkedToCorrectly() {
359                 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
360                 assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"));
361         }
362
363         @Test
364         public void freemailAddressWithInvalidFreemailIdIsParsedAsText() {
365                 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null);
366                 assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"));
367         }
368
369         @Test
370         public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() {
371                 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
372                 assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
373         }
374
375         @Test
376         public void freemailAddressWithoutLocalPartIsParsedAsText() {
377                 Iterable<Part> parts = soneTextParser.parse("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
378                 assertThat("Part Text", convertText(parts), is("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
379         }
380
381         @Test
382         public void correctFreemailAddressIsParsedCorrectly() {
383                 Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
384                 assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
385         }
386
387         @Test
388         public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
389                 Iterable<Part> parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
390                 assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
391         }
392
393         /**
394          * Converts all given {@link Part}s into a string, validating that the
395          * part’s classes match only the expected classes.
396          *
397          * @param parts
398          *            The parts to convert to text
399          * @param validClasses
400          *            The valid classes; if no classes are given, all classes are
401          *            valid
402          * @return The converted text
403          */
404         private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
405                 StringBuilder text = new StringBuilder();
406                 for (Part part : parts) {
407                         assertThat("Part", part, notNullValue());
408                         if (validClasses.length != 0) {
409                                 assertThat("Part’s class", part.getClass(), isIn(validClasses));
410                         }
411                         if (part instanceof PlainTextPart) {
412                                 text.append(((PlainTextPart) part).getText());
413                         } else if (part instanceof FreenetLinkPart) {
414                                 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
415                                 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
416                         } else if (part instanceof FreemailPart) {
417                                 FreemailPart freemailPart = (FreemailPart) part;
418                                 text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId()));
419                         } else if (part instanceof LinkPart) {
420                                 LinkPart linkPart = (LinkPart) part;
421                                 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
422                         } else if (part instanceof SonePart) {
423                                 SonePart sonePart = (SonePart) part;
424                                 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
425                         } else if (part instanceof PostPart) {
426                                 PostPart postPart = (PostPart) part;
427                                 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
428                         }
429                 }
430                 return text.toString();
431         }
432
433         @Test
434         public void parserCanBeCreatedByGuice() {
435                 Injector injector = createInjector(
436                                 supply(SoneProvider.class).byMock(),
437                                 supply(PostProvider.class).byMock()
438                 );
439                 assertThat(injector.getInstance(SoneTextParser.class), notNullValue());
440         }
441
442         /**
443          * Mock Sone provider.
444          */
445         private static class TestSoneProvider implements SoneProvider {
446
447                 @Nonnull
448                 @Override
449                 public Function1<String, Sone> getSoneLoader() {
450                         return new Function1<String, Sone>() {
451                                 @Override
452                                 public Sone invoke(String soneId) {
453                                         return getSone(soneId);
454                                 }
455                         };
456                 }
457
458                 @Nullable
459                 @Override
460                 public Sone getSone(final String soneId) {
461                         return new IdOnlySone(soneId);
462                 }
463
464                 /**
465                  * {@inheritDocs}
466                  */
467                 @Override
468                 public Collection<Sone> getSones() {
469                         return null;
470                 }
471
472                 /**
473                  * {@inheritDocs}
474                  */
475                 @Override
476                 public Collection<Sone> getLocalSones() {
477                         return null;
478                 }
479
480                 /**
481                  * {@inheritDocs}
482                  */
483                 @Override
484                 public Collection<Sone> getRemoteSones() {
485                         return null;
486                 }
487
488         }
489
490         private static class AbsentSoneProvider extends TestSoneProvider {
491
492                 @Override
493                 public Sone getSone(String soneId) {
494                         return null;
495                 }
496
497         }
498
499         private static class TestPostProvider implements PostProvider {
500
501                 @Nullable
502                 @Override
503                 public Post getPost(@Nonnull final String postId) {
504                         return new Post() {
505                                 @Override
506                                 public String getId() {
507                                         return postId;
508                                 }
509
510                                 @Override
511                                 public boolean isLoaded() {
512                                         return false;
513                                 }
514
515                                 @Override
516                                 public Sone getSone() {
517                                         return null;
518                                 }
519
520                                 @Override
521                                 public Optional<String> getRecipientId() {
522                                         return null;
523                                 }
524
525                                 @Override
526                                 public Optional<Sone> getRecipient() {
527                                         return null;
528                                 }
529
530                                 @Override
531                                 public long getTime() {
532                                         return 0;
533                                 }
534
535                                 @Override
536                                 public String getText() {
537                                         return "text";
538                                 }
539
540                                 @Override
541                                 public boolean isKnown() {
542                                         return false;
543                                 }
544
545                                 @Override
546                                 public Post setKnown(boolean known) {
547                                         return null;
548                                 }
549                         };
550                 }
551
552                 @Override
553                 public Collection<Post> getPosts(String soneId) {
554                         return null;
555                 }
556
557                 @Override
558                 public Collection<Post> getDirectedPosts(String recipientId) {
559                         return null;
560                 }
561
562         }
563
564         private static class AbsentPostProvider extends TestPostProvider {
565
566                 @Nullable
567                 @Override
568                 public Post getPost(@Nonnull String postId) {
569                         return null;
570                 }
571
572         }
573
574 }