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 sskLinkWithoutContextIsNotTrusted() {
208 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
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 sskLinkWithContextWithoutSoneIsNotTrusted() {
214 SoneTextParserContext context = new SoneTextParserContext(null);
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 sskLinkWithContextWithDifferentSoneIsNotTrusted() {
221 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
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 sskLinkWithContextWithCorrectSoneIsTrusted() {
228 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
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|trusted|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));
234 public void uskLinkWithContextWithCorrectSoneIsTrusted() {
235 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
236 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
237 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]"));
240 @SuppressWarnings("static-method")
242 public void testKSKLinks() throws IOException {
243 /* check basic links. */
244 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
245 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
247 /* check embedded links. */
248 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
249 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."));
251 /* check embedded links and line breaks. */
252 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
253 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]"));
256 @SuppressWarnings({ "synthetic-access", "static-method" })
258 public void testEmptyLinesAndSoneLinks() throws IOException {
259 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
261 /* check basic links. */
262 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
263 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
266 @SuppressWarnings({ "synthetic-access", "static-method" })
268 public void testEmpyHttpLinks() throws IOException {
269 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
271 /* check empty http links. */
272 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
273 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
277 public void httpLinkWithoutParensEndsAtNextClosingParen() {
278 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
279 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!"));
283 public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
284 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null);
285 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"));
289 public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
290 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
291 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!"));
295 public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
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 multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
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 commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
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 exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
314 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
315 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"));
319 public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
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 correctFreemailAddressIsLinkedToCorrectly() {
326 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
327 assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"));
331 public void freemailAddressWithInvalidFreemailIdIsParsedAsText() {
332 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null);
333 assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"));
337 public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() {
338 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
339 assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
343 public void freemailAddressWithoutLocalPartIsParsedAsText() {
344 Iterable<Part> parts = soneTextParser.parse(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
345 assertThat("Part Text", convertText(parts), is(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
349 public void correctFreemailAddressIsParsedCorrectly() {
350 Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
351 assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
355 public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
356 Iterable<Part> parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
357 assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
361 * Converts all given {@link Part}s into a string, validating that the
362 * part’s classes match only the expected classes.
365 * The parts to convert to text
366 * @param validClasses
367 * The valid classes; if no classes are given, all classes are
369 * @return The converted text
371 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
372 StringBuilder text = new StringBuilder();
373 for (Part part : parts) {
374 assertThat("Part", part, notNullValue());
375 if (validClasses.length != 0) {
376 assertThat("Part’s class", part.getClass(), isIn(validClasses));
378 if (part instanceof PlainTextPart) {
379 text.append(((PlainTextPart) part).getText());
380 } else if (part instanceof FreenetLinkPart) {
381 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
382 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
383 } else if (part instanceof FreemailPart) {
384 FreemailPart freemailPart = (FreemailPart) part;
385 text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId()));
386 } else if (part instanceof LinkPart) {
387 LinkPart linkPart = (LinkPart) part;
388 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
389 } else if (part instanceof SonePart) {
390 SonePart sonePart = (SonePart) part;
391 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
392 } else if (part instanceof PostPart) {
393 PostPart postPart = (PostPart) part;
394 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
397 return text.toString();
401 * Mock Sone provider.
403 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
405 private static class TestSoneProvider implements SoneProvider {
408 public Function<String, Optional<Sone>> soneLoader() {
409 return new Function<String, Optional<Sone>>() {
411 public Optional<Sone> apply(String soneId) {
412 return getSone(soneId);
421 public Optional<Sone> getSone(final String soneId) {
422 return Optional.<Sone>of(new IdOnlySone(soneId));
429 public Collection<Sone> getSones() {
437 public Collection<Sone> getLocalSones() {
445 public Collection<Sone> getRemoteSones() {
451 private static class AbsentSoneProvider extends TestSoneProvider {
454 public Optional<Sone> getSone(String soneId) {
455 return Optional.absent();
460 private static class TestPostProvider implements PostProvider {
463 public Optional<Post> getPost(final String postId) {
464 return Optional.<Post>of(new Post() {
466 public String getId() {
471 public boolean isLoaded() {
476 public Sone getSone() {
481 public Optional<String> getRecipientId() {
486 public Optional<Sone> getRecipient() {
491 public long getTime() {
496 public String getText() {
501 public boolean isKnown() {
506 public Post setKnown(boolean known) {
513 public Collection<Post> getPosts(String soneId) {
518 public Collection<Post> getDirectedPosts(String recipientId) {
524 private static class AbsentPostProvider extends TestPostProvider {
527 public Optional<Post> getPost(String postId) {
528 return Optional.absent();