2 * Sone - SoneTextParserTest.java - Copyright © 2011–2016 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 sskLinkWithoutContextIsNotTrusted() {
215 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
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]"));
220 public void sskLinkWithContextWithoutSoneIsNotTrusted() {
221 SoneTextParserContext context = new SoneTextParserContext(null);
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]"));
227 public void sskLinkWithContextWithDifferentSoneIsNotTrusted() {
228 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
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|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));
234 public void sskLinkWithContextWithCorrectSoneIsTrusted() {
235 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
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|trusted|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));
241 public void uskLinkWithContextWithCorrectSoneIsTrusted() {
242 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
243 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
244 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]"));
247 @SuppressWarnings("static-method")
249 public void testKSKLinks() throws IOException {
250 /* check basic links. */
251 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
252 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
254 /* check embedded links. */
255 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
256 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."));
258 /* check embedded links and line breaks. */
259 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
260 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]"));
263 @SuppressWarnings({ "synthetic-access", "static-method" })
265 public void testEmptyLinesAndSoneLinks() throws IOException {
266 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
268 /* check basic links. */
269 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
270 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
273 @SuppressWarnings({ "synthetic-access", "static-method" })
275 public void testEmpyHttpLinks() throws IOException {
276 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
278 /* check empty http links. */
279 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
280 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
284 public void httpLinkWithoutParensEndsAtNextClosingParen() {
285 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
286 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!"));
290 public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
291 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null);
292 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"));
296 public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
297 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
298 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!"));
302 public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
303 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
304 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!"));
308 public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
309 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
310 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!"));
314 public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
315 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
316 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!"));
320 public void exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
321 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
322 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"));
326 public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
327 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc?", null);
328 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"));
332 public void correctFreemailAddressIsLinkedToCorrectly() {
333 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
334 assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"));
338 public void freemailAddressWithInvalidFreemailIdIsParsedAsText() {
339 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null);
340 assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"));
344 public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() {
345 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
346 assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
350 public void freemailAddressWithoutLocalPartIsParsedAsText() {
351 Iterable<Part> parts = soneTextParser.parse(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
352 assertThat("Part Text", convertText(parts), is(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
356 public void correctFreemailAddressIsParsedCorrectly() {
357 Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
358 assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
362 public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
363 Iterable<Part> parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
364 assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
368 * Converts all given {@link Part}s into a string, validating that the
369 * part’s classes match only the expected classes.
372 * The parts to convert to text
373 * @param validClasses
374 * The valid classes; if no classes are given, all classes are
376 * @return The converted text
378 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
379 StringBuilder text = new StringBuilder();
380 for (Part part : parts) {
381 assertThat("Part", part, notNullValue());
382 if (validClasses.length != 0) {
383 assertThat("Part’s class", part.getClass(), isIn(validClasses));
385 if (part instanceof PlainTextPart) {
386 text.append(((PlainTextPart) part).getText());
387 } else if (part instanceof FreenetLinkPart) {
388 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
389 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
390 } else if (part instanceof FreemailPart) {
391 FreemailPart freemailPart = (FreemailPart) part;
392 text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId()));
393 } else if (part instanceof LinkPart) {
394 LinkPart linkPart = (LinkPart) part;
395 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
396 } else if (part instanceof SonePart) {
397 SonePart sonePart = (SonePart) part;
398 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
399 } else if (part instanceof PostPart) {
400 PostPart postPart = (PostPart) part;
401 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
404 return text.toString();
408 * Mock Sone provider.
410 private static class TestSoneProvider implements SoneProvider {
414 public Function1<String, Sone> getSoneLoader() {
415 return new Function1<String, Sone>() {
417 public Sone invoke(String soneId) {
418 return getSone(soneId);
425 public Sone getSone(final String soneId) {
426 return new IdOnlySone(soneId);
433 public Collection<Sone> getSones() {
441 public Collection<Sone> getLocalSones() {
449 public Collection<Sone> getRemoteSones() {
455 private static class AbsentSoneProvider extends TestSoneProvider {
458 public Sone getSone(String soneId) {
464 private static class TestPostProvider implements PostProvider {
468 public Post getPost(@Nonnull final String postId) {
471 public String getId() {
476 public boolean isLoaded() {
481 public Sone getSone() {
486 public Optional<String> getRecipientId() {
491 public Optional<Sone> getRecipient() {
496 public long getTime() {
501 public String getText() {
506 public boolean isKnown() {
511 public Post setKnown(boolean known) {
518 public Collection<Post> getPosts(String soneId) {
523 public Collection<Post> getDirectedPosts(String recipientId) {
529 private static class AbsentPostProvider extends TestPostProvider {
533 public Post getPost(@Nonnull String postId) {