2 * Sone - SoneTextParserTest.java - Copyright © 2011–2019 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.text;
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;
26 import java.io.IOException;
27 import java.util.Collection;
29 import javax.annotation.Nonnull;
30 import javax.annotation.Nullable;
32 import net.pterodactylus.sone.data.Post;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.data.impl.IdOnlySone;
35 import net.pterodactylus.sone.database.PostProvider;
36 import net.pterodactylus.sone.database.SoneProvider;
38 import com.google.common.base.Optional;
39 import kotlin.jvm.functions.Function1;
40 import org.junit.Test;
43 * JUnit test case for {@link SoneTextParser}.
45 public class SoneTextParserTest {
47 private final SoneTextParser soneTextParser = new SoneTextParser(null, null);
49 @SuppressWarnings("static-method")
51 public void testPlainText() throws IOException {
52 /* check basic operation. */
53 Iterable<Part> parts = soneTextParser.parse("Test.", null);
54 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
56 /* check empty lines at start and end. */
57 parts = soneTextParser.parse("\nTest.\n\n", null);
58 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
60 /* check duplicate empty lines in the text. */
61 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
62 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
66 public void consecutiveLinesAreSeparatedByLinefeed() {
67 Iterable<Part> parts = soneTextParser.parse("Text.\nText", null);
68 assertThat("Part Text", convertText(parts), is("Text.\nText"));
72 public void freenetLinksHaveTheFreenetPrefixRemoved() {
73 Iterable<Part> parts = soneTextParser.parse("freenet:KSK@gpl.txt", null);
74 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
78 public void onlyTheFirstItemInALineIsPrefixedWithALineBreak() {
79 Iterable<Part> parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null);
80 assertThat("Part Text", convertText(parts), is("Text.\n[KSK@gpl.txt|KSK@gpl.txt|gpl.txt] and [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
84 public void soneLinkWithTooShortSoneIdIsRenderedAsPlainText() {
85 Iterable<Part> parts = soneTextParser.parse("sone://too-short", null);
86 assertThat("Part Text", convertText(parts), is("sone://too-short"));
90 public void soneLinkIsRenderedCorrectlyIfSoneIsNotPresent() {
91 SoneTextParser parser = new SoneTextParser(new AbsentSoneProvider(), null);
92 Iterable<Part> parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null);
93 assertThat("Part Text", convertText(parts), is("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]"));
97 public void soneAndPostCanBeParsedFromTheSameText() {
98 SoneTextParser parser = new SoneTextParser(new TestSoneProvider(), new TestPostProvider());
99 Iterable<Part> parts = parser.parse("Text sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU more text post://f3757817-b45a-497a-803f-9c5aafc10dc6 even more text", null);
100 assertThat("Part Text", convertText(parts), is("Text [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] more text [Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text] even more text"));
104 public void postLinkIsRenderedAsPlainTextIfPostIdIsTooShort() {
105 Iterable<Part> parts = soneTextParser.parse("post://too-short", null);
106 assertThat("Part Text", convertText(parts), is("post://too-short"));
110 public void postLinkIsRenderedCorrectlyIfPostIsPresent() {
111 SoneTextParser parser = new SoneTextParser(null, new TestPostProvider());
112 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
113 assertThat("Part Text", convertText(parts), is("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]"));
117 public void postLinkIsRenderedAsPlainTextIfPostIsAbsent() {
118 SoneTextParser parser = new SoneTextParser(null, new AbsentPostProvider());
119 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
120 assertThat("Part Text", convertText(parts), is("post://f3757817-b45a-497a-803f-9c5aafc10dc6"));
124 public void nameOfFreenetLinkDoesNotContainUrlParameters() {
125 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null);
126 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]"));
130 public void trailingSlashInFreenetLinkIsRemovedForName() {
131 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt/", null);
132 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]"));
136 public void lastMetaStringOfFreenetLinkIsUsedAsName() {
137 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null);
138 assertThat("Part Text", convertText(parts), is("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|COPYING]"));
142 public void freenetLinkWithoutMetaStringsAndDocNameGetsFirstNineCharactersOfKeyAsName() {
143 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null);
144 assertThat("Part Text", convertText(parts), is("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nm]"));
148 public void malformedKeyIsRenderedAsPlainText() {
149 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU", null);
150 assertThat("Part Text", convertText(parts), is("CHK@qM1nmgU"));
154 public void httpsLinkHasItsPathsShortened() {
155 Iterable<Part> parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null);
156 assertThat("Part Text", convertText(parts), is("[https://test.test/some-long-path/file.txt|https://test.test/some-long-path/file.txt|test.test/…/file.txt]"));
160 public void httpLinksHaveTheirLastSlashRemoved() {
161 Iterable<Part> parts = soneTextParser.parse("http://test.test/test/", null);
162 assertThat("Part Text", convertText(parts), is("[http://test.test/test/|http://test.test/test/|test.test/…]"));
166 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndNoPath() {
167 Iterable<Part> parts = soneTextParser.parse("http://www.test.test", null);
168 assertThat("Part Text", convertText(parts), is("[http://www.test.test|http://www.test.test|test.test]"));
172 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndAPath() {
173 Iterable<Part> parts = soneTextParser.parse("http://www.test.test/test.html", null);
174 assertThat("Part Text", convertText(parts), is("[http://www.test.test/test.html|http://www.test.test/test.html|test.test/test.html]"));
178 public void hostnameIsKeptIntactIfNotBeginningWithWww() {
179 Iterable<Part> parts = soneTextParser.parse("http://test.test.test/test.html", null);
180 assertThat("Part Text", convertText(parts), is("[http://test.test.test/test.html|http://test.test.test/test.html|test.test.test/test.html]"));
184 public void hostnameWithOneDotButNoSlashIsKeptIntact() {
185 Iterable<Part> parts = soneTextParser.parse("http://test.test", null);
186 assertThat("Part Text", convertText(parts), is("[http://test.test|http://test.test|test.test]"));
190 public void urlParametersAreRemovedForHttpLinks() {
191 Iterable<Part> parts = soneTextParser.parse("http://test.test?foo=bar", null);
192 assertThat("Part Text", convertText(parts), is("[http://test.test?foo=bar|http://test.test?foo=bar|test.test]"));
196 public void emptyStringIsParsedCorrectly() {
197 Iterable<Part> parts = soneTextParser.parse("", null);
198 assertThat("Part Text", convertText(parts), is(""));
202 public void linksAreParsedInCorrectOrder() {
203 Iterable<Part> parts = soneTextParser.parse("KSK@ CHK@", null);
204 assertThat("Part Text", convertText(parts), is("KSK@ CHK@"));
208 public void invalidSskAndUskLinkIsParsedAsText() {
209 Iterable<Part> parts = soneTextParser.parse("SSK@a USK@a", null);
210 assertThat("Part Text", convertText(parts), is("SSK@a USK@a"));
214 public void sskWithoutDocumentNameIsParsedCorrectly() {
215 Iterable<Part> parts = soneTextParser.parse(
216 "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8",
218 assertThat("Part Text", convertText(parts),
219 is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|"
220 + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|"
221 + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU]"));
225 public void sskLinkWithoutContextIsNotTrusted() {
226 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
227 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 public void sskLinkWithContextWithoutSoneIsNotTrusted() {
232 SoneTextParserContext context = new SoneTextParserContext(null);
233 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
234 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 public void sskLinkWithContextWithDifferentSoneIsNotTrusted() {
239 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
240 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
241 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 public void sskLinkWithContextWithCorrectSoneIsTrusted() {
246 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
247 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
248 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 public void uskLinkWithContextWithCorrectSoneIsTrusted() {
253 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
254 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
255 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]"));
258 @SuppressWarnings("static-method")
260 public void testKSKLinks() throws IOException {
261 /* check basic links. */
262 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
263 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
265 /* check embedded links. */
266 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
267 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."));
269 /* check embedded links and line breaks. */
270 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
271 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]"));
274 @SuppressWarnings({ "synthetic-access", "static-method" })
276 public void testEmptyLinesAndSoneLinks() throws IOException {
277 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
279 /* check basic links. */
280 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
281 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
284 @SuppressWarnings({ "synthetic-access", "static-method" })
286 public void testEmpyHttpLinks() throws IOException {
287 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
289 /* check empty http links. */
290 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
291 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
295 public void httpLinkWithoutParensEndsAtNextClosingParen() {
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!"));
301 public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
302 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null);
303 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 public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
308 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
309 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 public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
314 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
315 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]. Nice!"));
319 public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
320 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
321 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 public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
326 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
327 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 public void exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
332 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
333 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"));
337 public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
338 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc?", null);
339 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"));
343 public void correctFreemailAddressIsLinkedToCorrectly() {
344 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
345 assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"));
349 public void freemailAddressWithInvalidFreemailIdIsParsedAsText() {
350 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null);
351 assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"));
355 public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() {
356 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
357 assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
361 public void freemailAddressWithoutLocalPartIsParsedAsText() {
362 Iterable<Part> parts = soneTextParser.parse(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
363 assertThat("Part Text", convertText(parts), is(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
367 public void correctFreemailAddressIsParsedCorrectly() {
368 Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
369 assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
373 public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
374 Iterable<Part> parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
375 assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
379 * Converts all given {@link Part}s into a string, validating that the
380 * part’s classes match only the expected classes.
383 * The parts to convert to text
384 * @param validClasses
385 * The valid classes; if no classes are given, all classes are
387 * @return The converted text
389 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
390 StringBuilder text = new StringBuilder();
391 for (Part part : parts) {
392 assertThat("Part", part, notNullValue());
393 if (validClasses.length != 0) {
394 assertThat("Part’s class", part.getClass(), isIn(validClasses));
396 if (part instanceof PlainTextPart) {
397 text.append(((PlainTextPart) part).getText());
398 } else if (part instanceof FreenetLinkPart) {
399 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
400 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
401 } else if (part instanceof FreemailPart) {
402 FreemailPart freemailPart = (FreemailPart) part;
403 text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId()));
404 } else if (part instanceof LinkPart) {
405 LinkPart linkPart = (LinkPart) part;
406 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
407 } else if (part instanceof SonePart) {
408 SonePart sonePart = (SonePart) part;
409 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
410 } else if (part instanceof PostPart) {
411 PostPart postPart = (PostPart) part;
412 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
415 return text.toString();
419 * Mock Sone provider.
421 private static class TestSoneProvider implements SoneProvider {
425 public Function1<String, Sone> getSoneLoader() {
426 return new Function1<String, Sone>() {
428 public Sone invoke(String soneId) {
429 return getSone(soneId);
436 public Sone getSone(final String soneId) {
437 return new IdOnlySone(soneId);
444 public Collection<Sone> getSones() {
452 public Collection<Sone> getLocalSones() {
460 public Collection<Sone> getRemoteSones() {
466 private static class AbsentSoneProvider extends TestSoneProvider {
469 public Sone getSone(String soneId) {
475 private static class TestPostProvider implements PostProvider {
479 public Post getPost(@Nonnull final String postId) {
482 public String getId() {
487 public boolean isLoaded() {
492 public Sone getSone() {
497 public Optional<String> getRecipientId() {
502 public Optional<Sone> getRecipient() {
507 public long getTime() {
512 public String getText() {
517 public boolean isKnown() {
522 public Post setKnown(boolean known) {
529 public Collection<Post> getPosts(String soneId) {
534 public Collection<Post> getDirectedPosts(String recipientId) {
540 private static class AbsentPostProvider extends TestPostProvider {
544 public Post getPost(@Nonnull String postId) {