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 net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.sone.data.impl.IdOnlySone;
32 import net.pterodactylus.sone.database.PostProvider;
33 import net.pterodactylus.sone.database.SoneProvider;
35 import com.google.common.base.Function;
36 import com.google.common.base.Optional;
37 import org.junit.Test;
40 * JUnit test case for {@link SoneTextParser}.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class SoneTextParserTest {
46 private final SoneTextParser soneTextParser = new SoneTextParser(null, null);
48 @SuppressWarnings("static-method")
50 public void testPlainText() throws IOException {
51 /* check basic operation. */
52 Iterable<Part> parts = soneTextParser.parse("Test.", null);
53 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
55 /* check empty lines at start and end. */
56 parts = soneTextParser.parse("\nTest.\n\n", null);
57 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
59 /* check duplicate empty lines in the text. */
60 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
61 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
65 public void consecutiveLinesAreSeparatedByLinefeed() {
66 Iterable<Part> parts = soneTextParser.parse("Text.\nText", null);
67 assertThat("Part Text", convertText(parts), is("Text.\nText"));
71 public void freenetLinksHaveTheFreenetPrefixRemoved() {
72 Iterable<Part> parts = soneTextParser.parse("freenet:KSK@gpl.txt", null);
73 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
77 public void onlyTheFirstItemInALineIsPrefixedWithALineBreak() {
78 Iterable<Part> parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null);
79 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]"));
83 public void soneLinkWithTooShortSoneIdIsRenderedAsPlainText() {
84 Iterable<Part> parts = soneTextParser.parse("sone://too-short", null);
85 assertThat("Part Text", convertText(parts), is("sone://too-short"));
89 public void soneLinkIsRenderedCorrectlyIfSoneIsNotPresent() {
90 SoneTextParser parser = new SoneTextParser(new AbsentSoneProvider(), null);
91 Iterable<Part> parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null);
92 assertThat("Part Text", convertText(parts), is("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]"));
96 public void postLinkIsRenderedAsPlainTextIfPostIdIsTooShort() {
97 Iterable<Part> parts = soneTextParser.parse("post://too-short", null);
98 assertThat("Part Text", convertText(parts), is("post://too-short"));
102 public void postLinkIsRenderedCorrectlyIfPostIsPresent() {
103 SoneTextParser parser = new SoneTextParser(null, new TestPostProvider());
104 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
105 assertThat("Part Text", convertText(parts), is("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]"));
109 public void postLinkIsRenderedAsPlainTextIfPostIsAbsent() {
110 SoneTextParser parser = new SoneTextParser(null, new AbsentPostProvider());
111 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
112 assertThat("Part Text", convertText(parts), is("post://f3757817-b45a-497a-803f-9c5aafc10dc6"));
116 public void nameOfFreenetLinkDoesNotContainUrlParameters() {
117 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null);
118 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]"));
122 public void trailingSlashInFreenetLinkIsRemovedForName() {
123 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt/", null);
124 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]"));
128 public void lastMetaStringOfFreenetLinkIsUsedAsName() {
129 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null);
130 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]"));
134 public void freenetLinkWithoutMetaStringsAndDocNameGetsFirstNineCharactersOfKeyAsName() {
135 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null);
136 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]"));
140 public void malformedKeyIsRenderedAsPlainText() {
141 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU", null);
142 assertThat("Part Text", convertText(parts), is("CHK@qM1nmgU"));
146 public void httpsLinkHasItsPathsShortened() {
147 Iterable<Part> parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null);
148 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]"));
152 public void httpLinksHaveTheirLastSlashRemoved() {
153 Iterable<Part> parts = soneTextParser.parse("http://test.test/test/", null);
154 assertThat("Part Text", convertText(parts), is("[http://test.test/test/|http://test.test/test/|test.test/…]"));
158 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndNoPath() {
159 Iterable<Part> parts = soneTextParser.parse("http://www.test.test", null);
160 assertThat("Part Text", convertText(parts), is("[http://www.test.test|http://www.test.test|test.test]"));
164 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndAPath() {
165 Iterable<Part> parts = soneTextParser.parse("http://www.test.test/test.html", null);
166 assertThat("Part Text", convertText(parts), is("[http://www.test.test/test.html|http://www.test.test/test.html|test.test/test.html]"));
170 public void hostnameIsKeptIntactIfNotBeginningWithWww() {
171 Iterable<Part> parts = soneTextParser.parse("http://test.test.test/test.html", null);
172 assertThat("Part Text", convertText(parts), is("[http://test.test.test/test.html|http://test.test.test/test.html|test.test.test/test.html]"));
176 public void hostnameWithOneDotButNoSlashIsKeptIntact() {
177 Iterable<Part> parts = soneTextParser.parse("http://test.test", null);
178 assertThat("Part Text", convertText(parts), is("[http://test.test|http://test.test|test.test]"));
182 public void urlParametersAreRemovedForHttpLinks() {
183 Iterable<Part> parts = soneTextParser.parse("http://test.test?foo=bar", null);
184 assertThat("Part Text", convertText(parts), is("[http://test.test?foo=bar|http://test.test?foo=bar|test.test]"));
188 public void emptyStringIsParsedCorrectly() {
189 Iterable<Part> parts = soneTextParser.parse("", null);
190 assertThat("Part Text", convertText(parts), is(""));
194 public void linksAreParsedInCorrectOrder() {
195 Iterable<Part> parts = soneTextParser.parse("KSK@ CHK@", null);
196 assertThat("Part Text", convertText(parts), is("KSK@ CHK@"));
200 public void sskLinkWithoutContextIsNotTrusted() {
201 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
202 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]"));
206 public void sskLinkWithContextWithoutSoneIsNotTrusted() {
207 SoneTextParserContext context = new SoneTextParserContext(null);
208 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
209 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]"));
213 public void sskLinkWithContextWithDifferentSoneIsNotTrusted() {
214 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
215 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
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 sskLinkWithContextWithCorrectSoneIsTrusted() {
221 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
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|trusted|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));
227 public void uskLinkWithContextWithCorrectSoneIsTrusted() {
228 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
229 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
230 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]"));
233 @SuppressWarnings("static-method")
235 public void testKSKLinks() throws IOException {
236 /* check basic links. */
237 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
238 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
240 /* check embedded links. */
241 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
242 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."));
244 /* check embedded links and line breaks. */
245 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
246 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]"));
249 @SuppressWarnings({ "synthetic-access", "static-method" })
251 public void testEmptyLinesAndSoneLinks() throws IOException {
252 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
254 /* check basic links. */
255 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
256 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
259 @SuppressWarnings({ "synthetic-access", "static-method" })
261 public void testEmpyHttpLinks() throws IOException {
262 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
264 /* check empty http links. */
265 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
266 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
270 public void httpLinkWithoutParensEndsAtNextClosingParen() {
271 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
272 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!"));
276 public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
277 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null);
278 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"));
282 public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
283 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
284 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!"));
288 public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
289 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
290 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!"));
294 public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
295 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
296 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!"));
300 public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
301 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
302 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!"));
306 public void exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
307 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
308 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"));
312 public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
313 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc?", null);
314 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"));
318 public void correctFreemailAddressIsLinkedToCorrectly() {
319 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
320 assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"));
324 public void freemailAddressWithInvalidFreemailIdIsParsedAsText() {
325 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null);
326 assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"));
330 public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() {
331 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
332 assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
336 public void freemailAddressWithoutLocalPartIsParsedAsText() {
337 Iterable<Part> parts = soneTextParser.parse(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
338 assertThat("Part Text", convertText(parts), is(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
342 public void correctFreemailAddressIsParsedCorrectly() {
343 Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
344 assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
348 public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
349 Iterable<Part> parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
350 assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
354 * Converts all given {@link Part}s into a string, validating that the
355 * part’s classes match only the expected classes.
358 * The parts to convert to text
359 * @param validClasses
360 * The valid classes; if no classes are given, all classes are
362 * @return The converted text
364 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
365 StringBuilder text = new StringBuilder();
366 for (Part part : parts) {
367 assertThat("Part", part, notNullValue());
368 if (validClasses.length != 0) {
369 assertThat("Part’s class", part.getClass(), isIn(validClasses));
371 if (part instanceof PlainTextPart) {
372 text.append(((PlainTextPart) part).getText());
373 } else if (part instanceof FreenetLinkPart) {
374 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
375 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
376 } else if (part instanceof FreemailPart) {
377 FreemailPart freemailPart = (FreemailPart) part;
378 text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId()));
379 } else if (part instanceof LinkPart) {
380 LinkPart linkPart = (LinkPart) part;
381 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
382 } else if (part instanceof SonePart) {
383 SonePart sonePart = (SonePart) part;
384 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
385 } else if (part instanceof PostPart) {
386 PostPart postPart = (PostPart) part;
387 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
390 return text.toString();
394 * Mock Sone provider.
396 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
398 private static class TestSoneProvider implements SoneProvider {
401 public Function<String, Optional<Sone>> soneLoader() {
402 return new Function<String, Optional<Sone>>() {
404 public Optional<Sone> apply(String soneId) {
405 return getSone(soneId);
414 public Optional<Sone> getSone(final String soneId) {
415 return Optional.<Sone>of(new IdOnlySone(soneId));
422 public Collection<Sone> getSones() {
430 public Collection<Sone> getLocalSones() {
438 public Collection<Sone> getRemoteSones() {
444 private static class AbsentSoneProvider extends TestSoneProvider {
447 public Optional<Sone> getSone(String soneId) {
448 return Optional.absent();
453 private static class TestPostProvider implements PostProvider {
456 public Optional<Post> getPost(final String postId) {
457 return Optional.<Post>of(new Post() {
459 public String getId() {
464 public boolean isLoaded() {
469 public Sone getSone() {
474 public Optional<String> getRecipientId() {
479 public Optional<Sone> getRecipient() {
484 public long getTime() {
489 public String getText() {
494 public boolean isKnown() {
499 public Post setKnown(boolean known) {
506 public Collection<Post> getPosts(String soneId) {
511 public Collection<Post> getDirectedPosts(String recipientId) {
517 private static class AbsentPostProvider extends TestPostProvider {
520 public Optional<Post> getPost(String postId) {
521 return Optional.absent();