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 soneAndPostCanBeParsedFromTheSameText() {
97 SoneTextParser parser = new SoneTextParser(new TestSoneProvider(), new TestPostProvider());
98 Iterable<Part> parts = parser.parse("Text sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU more text post://f3757817-b45a-497a-803f-9c5aafc10dc6 even more text", null);
99 assertThat("Part Text", convertText(parts), is("Text [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] more text [Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text] even more text"));
103 public void postLinkIsRenderedAsPlainTextIfPostIdIsTooShort() {
104 Iterable<Part> parts = soneTextParser.parse("post://too-short", null);
105 assertThat("Part Text", convertText(parts), is("post://too-short"));
109 public void postLinkIsRenderedCorrectlyIfPostIsPresent() {
110 SoneTextParser parser = new SoneTextParser(null, new TestPostProvider());
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|text]"));
116 public void postLinkIsRenderedAsPlainTextIfPostIsAbsent() {
117 SoneTextParser parser = new SoneTextParser(null, new AbsentPostProvider());
118 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
119 assertThat("Part Text", convertText(parts), is("post://f3757817-b45a-497a-803f-9c5aafc10dc6"));
123 public void nameOfFreenetLinkDoesNotContainUrlParameters() {
124 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null);
125 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]"));
129 public void trailingSlashInFreenetLinkIsRemovedForName() {
130 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt/", null);
131 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]"));
135 public void lastMetaStringOfFreenetLinkIsUsedAsName() {
136 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null);
137 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]"));
141 public void freenetLinkWithoutMetaStringsAndDocNameGetsFirstNineCharactersOfKeyAsName() {
142 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null);
143 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]"));
147 public void malformedKeyIsRenderedAsPlainText() {
148 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU", null);
149 assertThat("Part Text", convertText(parts), is("CHK@qM1nmgU"));
153 public void httpsLinkHasItsPathsShortened() {
154 Iterable<Part> parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null);
155 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]"));
159 public void httpLinksHaveTheirLastSlashRemoved() {
160 Iterable<Part> parts = soneTextParser.parse("http://test.test/test/", null);
161 assertThat("Part Text", convertText(parts), is("[http://test.test/test/|http://test.test/test/|test.test/…]"));
165 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndNoPath() {
166 Iterable<Part> parts = soneTextParser.parse("http://www.test.test", null);
167 assertThat("Part Text", convertText(parts), is("[http://www.test.test|http://www.test.test|test.test]"));
171 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndAPath() {
172 Iterable<Part> parts = soneTextParser.parse("http://www.test.test/test.html", null);
173 assertThat("Part Text", convertText(parts), is("[http://www.test.test/test.html|http://www.test.test/test.html|test.test/test.html]"));
177 public void hostnameIsKeptIntactIfNotBeginningWithWww() {
178 Iterable<Part> parts = soneTextParser.parse("http://test.test.test/test.html", null);
179 assertThat("Part Text", convertText(parts), is("[http://test.test.test/test.html|http://test.test.test/test.html|test.test.test/test.html]"));
183 public void hostnameWithOneDotButNoSlashIsKeptIntact() {
184 Iterable<Part> parts = soneTextParser.parse("http://test.test", null);
185 assertThat("Part Text", convertText(parts), is("[http://test.test|http://test.test|test.test]"));
189 public void urlParametersAreRemovedForHttpLinks() {
190 Iterable<Part> parts = soneTextParser.parse("http://test.test?foo=bar", null);
191 assertThat("Part Text", convertText(parts), is("[http://test.test?foo=bar|http://test.test?foo=bar|test.test]"));
195 public void emptyStringIsParsedCorrectly() {
196 Iterable<Part> parts = soneTextParser.parse("", null);
197 assertThat("Part Text", convertText(parts), is(""));
201 public void linksAreParsedInCorrectOrder() {
202 Iterable<Part> parts = soneTextParser.parse("KSK@ CHK@", null);
203 assertThat("Part Text", convertText(parts), is("KSK@ CHK@"));
207 public void invalidSskAndUskLinkIsParsedAsText() {
208 Iterable<Part> parts = soneTextParser.parse("SSK@a USK@a", null);
209 assertThat("Part Text", convertText(parts), is("SSK@a USK@a"));
213 public void sskLinkWithoutContextIsNotTrusted() {
214 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
215 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]"));
219 public void sskLinkWithContextWithoutSoneIsNotTrusted() {
220 SoneTextParserContext context = new SoneTextParserContext(null);
221 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
222 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]"));
226 public void sskLinkWithContextWithDifferentSoneIsNotTrusted() {
227 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
228 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
229 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]"));
233 public void sskLinkWithContextWithCorrectSoneIsTrusted() {
234 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
235 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
236 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]"));
240 public void uskLinkWithContextWithCorrectSoneIsTrusted() {
241 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
242 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
243 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]"));
246 @SuppressWarnings("static-method")
248 public void testKSKLinks() throws IOException {
249 /* check basic links. */
250 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
251 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
253 /* check embedded links. */
254 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
255 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."));
257 /* check embedded links and line breaks. */
258 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
259 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]"));
262 @SuppressWarnings({ "synthetic-access", "static-method" })
264 public void testEmptyLinesAndSoneLinks() throws IOException {
265 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
267 /* check basic links. */
268 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
269 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
272 @SuppressWarnings({ "synthetic-access", "static-method" })
274 public void testEmpyHttpLinks() throws IOException {
275 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
277 /* check empty http links. */
278 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
279 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
283 public void httpLinkWithoutParensEndsAtNextClosingParen() {
284 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
285 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!"));
289 public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
290 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null);
291 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"));
295 public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
296 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
297 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!"));
301 public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
302 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
303 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!"));
307 public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
308 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
309 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!"));
313 public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
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 exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
320 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
321 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"));
325 public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
326 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc?", null);
327 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"));
331 public void correctFreemailAddressIsLinkedToCorrectly() {
332 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
333 assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"));
337 public void freemailAddressWithInvalidFreemailIdIsParsedAsText() {
338 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null);
339 assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"));
343 public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() {
344 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
345 assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
349 public void freemailAddressWithoutLocalPartIsParsedAsText() {
350 Iterable<Part> parts = soneTextParser.parse(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
351 assertThat("Part Text", convertText(parts), is(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
355 public void correctFreemailAddressIsParsedCorrectly() {
356 Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
357 assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
361 public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
362 Iterable<Part> parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
363 assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
367 * Converts all given {@link Part}s into a string, validating that the
368 * part’s classes match only the expected classes.
371 * The parts to convert to text
372 * @param validClasses
373 * The valid classes; if no classes are given, all classes are
375 * @return The converted text
377 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
378 StringBuilder text = new StringBuilder();
379 for (Part part : parts) {
380 assertThat("Part", part, notNullValue());
381 if (validClasses.length != 0) {
382 assertThat("Part’s class", part.getClass(), isIn(validClasses));
384 if (part instanceof PlainTextPart) {
385 text.append(((PlainTextPart) part).getText());
386 } else if (part instanceof FreenetLinkPart) {
387 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
388 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
389 } else if (part instanceof FreemailPart) {
390 FreemailPart freemailPart = (FreemailPart) part;
391 text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId()));
392 } else if (part instanceof LinkPart) {
393 LinkPart linkPart = (LinkPart) part;
394 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
395 } else if (part instanceof SonePart) {
396 SonePart sonePart = (SonePart) part;
397 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
398 } else if (part instanceof PostPart) {
399 PostPart postPart = (PostPart) part;
400 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
403 return text.toString();
407 * Mock Sone provider.
409 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
411 private static class TestSoneProvider implements SoneProvider {
414 public Function<String, Optional<Sone>> soneLoader() {
415 return new Function<String, Optional<Sone>>() {
417 public Optional<Sone> apply(String soneId) {
418 return getSone(soneId);
427 public Optional<Sone> getSone(final String soneId) {
428 return Optional.<Sone>of(new IdOnlySone(soneId));
435 public Collection<Sone> getSones() {
443 public Collection<Sone> getLocalSones() {
451 public Collection<Sone> getRemoteSones() {
457 private static class AbsentSoneProvider extends TestSoneProvider {
460 public Optional<Sone> getSone(String soneId) {
461 return Optional.absent();
466 private static class TestPostProvider implements PostProvider {
469 public Optional<Post> getPost(final String postId) {
470 return Optional.<Post>of(new Post() {
472 public String getId() {
477 public boolean isLoaded() {
482 public Sone getSone() {
487 public Optional<String> getRecipientId() {
492 public Optional<Sone> getRecipient() {
497 public long getTime() {
502 public String getText() {
507 public boolean isKnown() {
512 public Post setKnown(boolean known) {
519 public Collection<Post> getPosts(String soneId) {
524 public Collection<Post> getDirectedPosts(String recipientId) {
530 private static class AbsentPostProvider extends TestPostProvider {
533 public Optional<Post> getPost(String postId) {
534 return Optional.absent();