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 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class SoneTextParserTest {
49 private final SoneTextParser soneTextParser = new SoneTextParser(null, null);
51 @SuppressWarnings("static-method")
53 public void testPlainText() throws IOException {
54 /* check basic operation. */
55 Iterable<Part> parts = soneTextParser.parse("Test.", null);
56 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
58 /* check empty lines at start and end. */
59 parts = soneTextParser.parse("\nTest.\n\n", null);
60 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
62 /* check duplicate empty lines in the text. */
63 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
64 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
68 public void consecutiveLinesAreSeparatedByLinefeed() {
69 Iterable<Part> parts = soneTextParser.parse("Text.\nText", null);
70 assertThat("Part Text", convertText(parts), is("Text.\nText"));
74 public void freenetLinksHaveTheFreenetPrefixRemoved() {
75 Iterable<Part> parts = soneTextParser.parse("freenet:KSK@gpl.txt", null);
76 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
80 public void onlyTheFirstItemInALineIsPrefixedWithALineBreak() {
81 Iterable<Part> parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null);
82 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]"));
86 public void soneLinkWithTooShortSoneIdIsRenderedAsPlainText() {
87 Iterable<Part> parts = soneTextParser.parse("sone://too-short", null);
88 assertThat("Part Text", convertText(parts), is("sone://too-short"));
92 public void soneLinkIsRenderedCorrectlyIfSoneIsNotPresent() {
93 SoneTextParser parser = new SoneTextParser(new AbsentSoneProvider(), null);
94 Iterable<Part> parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null);
95 assertThat("Part Text", convertText(parts), is("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]"));
99 public void soneAndPostCanBeParsedFromTheSameText() {
100 SoneTextParser parser = new SoneTextParser(new TestSoneProvider(), new TestPostProvider());
101 Iterable<Part> parts = parser.parse("Text sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU more text post://f3757817-b45a-497a-803f-9c5aafc10dc6 even more text", null);
102 assertThat("Part Text", convertText(parts), is("Text [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] more text [Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text] even more text"));
106 public void postLinkIsRenderedAsPlainTextIfPostIdIsTooShort() {
107 Iterable<Part> parts = soneTextParser.parse("post://too-short", null);
108 assertThat("Part Text", convertText(parts), is("post://too-short"));
112 public void postLinkIsRenderedCorrectlyIfPostIsPresent() {
113 SoneTextParser parser = new SoneTextParser(null, new TestPostProvider());
114 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
115 assertThat("Part Text", convertText(parts), is("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]"));
119 public void postLinkIsRenderedAsPlainTextIfPostIsAbsent() {
120 SoneTextParser parser = new SoneTextParser(null, new AbsentPostProvider());
121 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
122 assertThat("Part Text", convertText(parts), is("post://f3757817-b45a-497a-803f-9c5aafc10dc6"));
126 public void nameOfFreenetLinkDoesNotContainUrlParameters() {
127 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null);
128 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]"));
132 public void trailingSlashInFreenetLinkIsRemovedForName() {
133 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt/", null);
134 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]"));
138 public void lastMetaStringOfFreenetLinkIsUsedAsName() {
139 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null);
140 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]"));
144 public void freenetLinkWithoutMetaStringsAndDocNameGetsFirstNineCharactersOfKeyAsName() {
145 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null);
146 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]"));
150 public void malformedKeyIsRenderedAsPlainText() {
151 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU", null);
152 assertThat("Part Text", convertText(parts), is("CHK@qM1nmgU"));
156 public void httpsLinkHasItsPathsShortened() {
157 Iterable<Part> parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null);
158 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]"));
162 public void httpLinksHaveTheirLastSlashRemoved() {
163 Iterable<Part> parts = soneTextParser.parse("http://test.test/test/", null);
164 assertThat("Part Text", convertText(parts), is("[http://test.test/test/|http://test.test/test/|test.test/…]"));
168 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndNoPath() {
169 Iterable<Part> parts = soneTextParser.parse("http://www.test.test", null);
170 assertThat("Part Text", convertText(parts), is("[http://www.test.test|http://www.test.test|test.test]"));
174 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndAPath() {
175 Iterable<Part> parts = soneTextParser.parse("http://www.test.test/test.html", null);
176 assertThat("Part Text", convertText(parts), is("[http://www.test.test/test.html|http://www.test.test/test.html|test.test/test.html]"));
180 public void hostnameIsKeptIntactIfNotBeginningWithWww() {
181 Iterable<Part> parts = soneTextParser.parse("http://test.test.test/test.html", null);
182 assertThat("Part Text", convertText(parts), is("[http://test.test.test/test.html|http://test.test.test/test.html|test.test.test/test.html]"));
186 public void hostnameWithOneDotButNoSlashIsKeptIntact() {
187 Iterable<Part> parts = soneTextParser.parse("http://test.test", null);
188 assertThat("Part Text", convertText(parts), is("[http://test.test|http://test.test|test.test]"));
192 public void urlParametersAreRemovedForHttpLinks() {
193 Iterable<Part> parts = soneTextParser.parse("http://test.test?foo=bar", null);
194 assertThat("Part Text", convertText(parts), is("[http://test.test?foo=bar|http://test.test?foo=bar|test.test]"));
198 public void emptyStringIsParsedCorrectly() {
199 Iterable<Part> parts = soneTextParser.parse("", null);
200 assertThat("Part Text", convertText(parts), is(""));
204 public void linksAreParsedInCorrectOrder() {
205 Iterable<Part> parts = soneTextParser.parse("KSK@ CHK@", null);
206 assertThat("Part Text", convertText(parts), is("KSK@ CHK@"));
210 public void invalidSskAndUskLinkIsParsedAsText() {
211 Iterable<Part> parts = soneTextParser.parse("SSK@a USK@a", null);
212 assertThat("Part Text", convertText(parts), is("SSK@a USK@a"));
216 public void sskLinkWithoutContextIsNotTrusted() {
217 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
218 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]"));
222 public void sskLinkWithContextWithoutSoneIsNotTrusted() {
223 SoneTextParserContext context = new SoneTextParserContext(null);
224 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
225 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]"));
229 public void sskLinkWithContextWithDifferentSoneIsNotTrusted() {
230 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
231 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
232 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]"));
236 public void sskLinkWithContextWithCorrectSoneIsTrusted() {
237 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
238 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
239 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]"));
243 public void uskLinkWithContextWithCorrectSoneIsTrusted() {
244 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
245 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
246 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]"));
249 @SuppressWarnings("static-method")
251 public void testKSKLinks() throws IOException {
252 /* check basic links. */
253 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
254 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
256 /* check embedded links. */
257 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
258 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."));
260 /* check embedded links and line breaks. */
261 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
262 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]"));
265 @SuppressWarnings({ "synthetic-access", "static-method" })
267 public void testEmptyLinesAndSoneLinks() throws IOException {
268 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
270 /* check basic links. */
271 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
272 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
275 @SuppressWarnings({ "synthetic-access", "static-method" })
277 public void testEmpyHttpLinks() throws IOException {
278 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
280 /* check empty http links. */
281 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
282 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
286 public void httpLinkWithoutParensEndsAtNextClosingParen() {
287 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
288 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!"));
292 public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
293 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null);
294 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"));
298 public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
299 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
300 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!"));
304 public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
305 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
306 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]. Nice!"));
310 public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
311 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
312 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!"));
316 public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
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!"));
322 public void exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
323 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
324 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"));
328 public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
329 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc?", null);
330 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"));
334 public void correctFreemailAddressIsLinkedToCorrectly() {
335 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
336 assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"));
340 public void freemailAddressWithInvalidFreemailIdIsParsedAsText() {
341 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null);
342 assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"));
346 public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() {
347 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
348 assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
352 public void freemailAddressWithoutLocalPartIsParsedAsText() {
353 Iterable<Part> parts = soneTextParser.parse(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
354 assertThat("Part Text", convertText(parts), is(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
358 public void correctFreemailAddressIsParsedCorrectly() {
359 Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
360 assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
364 public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
365 Iterable<Part> parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
366 assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
370 * Converts all given {@link Part}s into a string, validating that the
371 * part’s classes match only the expected classes.
374 * The parts to convert to text
375 * @param validClasses
376 * The valid classes; if no classes are given, all classes are
378 * @return The converted text
380 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
381 StringBuilder text = new StringBuilder();
382 for (Part part : parts) {
383 assertThat("Part", part, notNullValue());
384 if (validClasses.length != 0) {
385 assertThat("Part’s class", part.getClass(), isIn(validClasses));
387 if (part instanceof PlainTextPart) {
388 text.append(((PlainTextPart) part).getText());
389 } else if (part instanceof FreenetLinkPart) {
390 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
391 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
392 } else if (part instanceof FreemailPart) {
393 FreemailPart freemailPart = (FreemailPart) part;
394 text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId()));
395 } else if (part instanceof LinkPart) {
396 LinkPart linkPart = (LinkPart) part;
397 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
398 } else if (part instanceof SonePart) {
399 SonePart sonePart = (SonePart) part;
400 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
401 } else if (part instanceof PostPart) {
402 PostPart postPart = (PostPart) part;
403 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
406 return text.toString();
410 * Mock Sone provider.
412 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
414 private static class TestSoneProvider implements SoneProvider {
418 public Function1<String, Sone> getSoneLoader() {
419 return new Function1<String, Sone>() {
421 public Sone invoke(String soneId) {
422 return getSone(soneId);
429 public Sone getSone(final String soneId) {
430 return new IdOnlySone(soneId);
437 public Collection<Sone> getSones() {
445 public Collection<Sone> getLocalSones() {
453 public Collection<Sone> getRemoteSones() {
459 private static class AbsentSoneProvider extends TestSoneProvider {
462 public Sone getSone(String soneId) {
468 private static class TestPostProvider implements PostProvider {
471 public Optional<Post> getPost(final String postId) {
472 return Optional.<Post>of(new Post() {
474 public String getId() {
479 public boolean isLoaded() {
484 public Sone getSone() {
489 public Optional<String> getRecipientId() {
494 public Optional<Sone> getRecipient() {
499 public long getTime() {
504 public String getText() {
509 public boolean isKnown() {
514 public Post setKnown(boolean known) {
521 public Collection<Post> getPosts(String soneId) {
526 public Collection<Post> getDirectedPosts(String recipientId) {
532 private static class AbsentPostProvider extends TestPostProvider {
535 public Optional<Post> getPost(String postId) {
536 return Optional.absent();