2 * Sone - SoneTextParserTest.java - Copyright © 2011–2019 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 com.google.inject.Guice.createInjector;
21 import static java.lang.String.format;
22 import static net.pterodactylus.sone.test.GuiceKt.supply;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.is;
25 import static org.hamcrest.Matchers.isIn;
26 import static org.hamcrest.Matchers.notNullValue;
28 import java.io.IOException;
29 import java.util.Collection;
31 import javax.annotation.Nonnull;
32 import javax.annotation.Nullable;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.Sone;
36 import net.pterodactylus.sone.data.impl.IdOnlySone;
37 import net.pterodactylus.sone.database.PostProvider;
38 import net.pterodactylus.sone.database.SoneProvider;
40 import com.google.common.base.Optional;
41 import com.google.inject.Injector;
42 import kotlin.jvm.functions.Function1;
43 import org.junit.Test;
46 * JUnit test case for {@link SoneTextParser}.
48 public class SoneTextParserTest {
50 private final SoneTextParser soneTextParser = new SoneTextParser(null, null);
52 @SuppressWarnings("static-method")
54 public void testPlainText() throws IOException {
55 /* check basic operation. */
56 Iterable<Part> parts = soneTextParser.parse("Test.", null);
57 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
59 /* check empty lines at start and end. */
60 parts = soneTextParser.parse("\nTest.\n\n", null);
61 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
63 /* check duplicate empty lines in the text. */
64 parts = soneTextParser.parse("\nTest.\n\n\nTest.", null);
65 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
69 public void consecutiveLinesAreSeparatedByLinefeed() {
70 Iterable<Part> parts = soneTextParser.parse("Text.\nText", null);
71 assertThat("Part Text", convertText(parts), is("Text.\nText"));
75 public void freenetLinksHaveTheFreenetPrefixRemoved() {
76 Iterable<Part> parts = soneTextParser.parse("freenet:KSK@gpl.txt", null);
77 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
81 public void onlyTheFirstItemInALineIsPrefixedWithALineBreak() {
82 Iterable<Part> parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null);
83 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]"));
87 public void soneLinkWithTooShortSoneIdIsRenderedAsPlainText() {
88 Iterable<Part> parts = soneTextParser.parse("sone://too-short", null);
89 assertThat("Part Text", convertText(parts), is("sone://too-short"));
93 public void soneLinkIsRenderedCorrectlyIfSoneIsNotPresent() {
94 SoneTextParser parser = new SoneTextParser(new AbsentSoneProvider(), null);
95 Iterable<Part> parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null);
96 assertThat("Part Text", convertText(parts), is("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]"));
100 public void soneAndPostCanBeParsedFromTheSameText() {
101 SoneTextParser parser = new SoneTextParser(new TestSoneProvider(), new TestPostProvider());
102 Iterable<Part> parts = parser.parse("Text sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU more text post://f3757817-b45a-497a-803f-9c5aafc10dc6 even more text", null);
103 assertThat("Part Text", convertText(parts), is("Text [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] more text [Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text] even more text"));
107 public void postLinkIsRenderedAsPlainTextIfPostIdIsTooShort() {
108 Iterable<Part> parts = soneTextParser.parse("post://too-short", null);
109 assertThat("Part Text", convertText(parts), is("post://too-short"));
113 public void postLinkIsRenderedCorrectlyIfPostIsPresent() {
114 SoneTextParser parser = new SoneTextParser(null, new TestPostProvider());
115 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
116 assertThat("Part Text", convertText(parts), is("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]"));
120 public void postLinkIsRenderedAsPlainTextIfPostIsAbsent() {
121 SoneTextParser parser = new SoneTextParser(null, new AbsentPostProvider());
122 Iterable<Part> parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null);
123 assertThat("Part Text", convertText(parts), is("post://f3757817-b45a-497a-803f-9c5aafc10dc6"));
127 public void nameOfFreenetLinkDoesNotContainUrlParameters() {
128 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null);
129 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]"));
133 public void trailingSlashInFreenetLinkIsRemovedForName() {
134 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt/", null);
135 assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]"));
139 public void lastMetaStringOfFreenetLinkIsUsedAsName() {
140 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null);
141 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]"));
145 public void freenetLinkWithoutMetaStringsAndDocNameGetsFirstNineCharactersOfKeyAsName() {
146 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null);
147 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]"));
151 public void malformedKeyIsRenderedAsPlainText() {
152 Iterable<Part> parts = soneTextParser.parse("CHK@qM1nmgU", null);
153 assertThat("Part Text", convertText(parts), is("CHK@qM1nmgU"));
157 public void httpsLinkHasItsPathsShortened() {
158 Iterable<Part> parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null);
159 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]"));
163 public void httpLinksHaveTheirLastSlashRemoved() {
164 Iterable<Part> parts = soneTextParser.parse("http://test.test/test/", null);
165 assertThat("Part Text", convertText(parts), is("[http://test.test/test/|http://test.test/test/|test.test/…]"));
169 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndNoPath() {
170 Iterable<Part> parts = soneTextParser.parse("http://www.test.test", null);
171 assertThat("Part Text", convertText(parts), is("[http://www.test.test|http://www.test.test|test.test]"));
175 public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndAPath() {
176 Iterable<Part> parts = soneTextParser.parse("http://www.test.test/test.html", null);
177 assertThat("Part Text", convertText(parts), is("[http://www.test.test/test.html|http://www.test.test/test.html|test.test/test.html]"));
181 public void hostnameIsKeptIntactIfNotBeginningWithWww() {
182 Iterable<Part> parts = soneTextParser.parse("http://test.test.test/test.html", null);
183 assertThat("Part Text", convertText(parts), is("[http://test.test.test/test.html|http://test.test.test/test.html|test.test.test/test.html]"));
187 public void hostnameWithOneDotButNoSlashIsKeptIntact() {
188 Iterable<Part> parts = soneTextParser.parse("http://test.test", null);
189 assertThat("Part Text", convertText(parts), is("[http://test.test|http://test.test|test.test]"));
193 public void urlParametersAreRemovedForHttpLinks() {
194 Iterable<Part> parts = soneTextParser.parse("http://test.test?foo=bar", null);
195 assertThat("Part Text", convertText(parts), is("[http://test.test?foo=bar|http://test.test?foo=bar|test.test]"));
199 public void emptyStringIsParsedCorrectly() {
200 Iterable<Part> parts = soneTextParser.parse("", null);
201 assertThat("Part Text", convertText(parts), is(""));
205 public void linksAreParsedInCorrectOrder() {
206 Iterable<Part> parts = soneTextParser.parse("KSK@ CHK@", null);
207 assertThat("Part Text", convertText(parts), is("KSK@ CHK@"));
211 public void invalidSskAndUskLinkIsParsedAsText() {
212 Iterable<Part> parts = soneTextParser.parse("SSK@a USK@a", null);
213 assertThat("Part Text", convertText(parts), is("SSK@a USK@a"));
217 public void sskWithoutDocumentNameIsParsedCorrectly() {
218 Iterable<Part> parts = soneTextParser.parse(
219 "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8",
221 assertThat("Part Text", convertText(parts),
222 is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|"
223 + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|"
224 + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU]"));
228 public void sskLinkWithoutContextIsNotTrusted() {
229 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null);
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 sskLinkWithContextWithoutSoneIsNotTrusted() {
235 SoneTextParserContext context = new SoneTextParserContext(null);
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|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]"));
241 public void sskLinkWithContextWithDifferentSoneIsNotTrusted() {
242 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU"));
243 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
244 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]"));
248 public void sskLinkWithContextWithCorrectSoneIsTrusted() {
249 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
250 Iterable<Part> parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context);
251 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]"));
255 public void uskLinkWithContextWithCorrectSoneIsTrusted() {
256 SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU"));
257 Iterable<Part> parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context);
258 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]"));
261 @SuppressWarnings("static-method")
263 public void testKSKLinks() throws IOException {
264 /* check basic links. */
265 Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
266 assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
268 /* check embedded links. */
269 parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
270 assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b."));
272 /* check embedded links and line breaks. */
273 parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null);
274 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]"));
277 @SuppressWarnings({ "synthetic-access", "static-method" })
279 public void testEmptyLinesAndSoneLinks() throws IOException {
280 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
282 /* check basic links. */
283 Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
284 assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
287 @SuppressWarnings({ "synthetic-access", "static-method" })
289 public void testEmpyHttpLinks() throws IOException {
290 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
292 /* check empty http links. */
293 Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
294 assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
298 public void httpLinkWithoutParensEndsAtNextClosingParen() {
299 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
300 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!"));
304 public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
305 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null);
306 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"));
310 public void uskLinkWithFilenameShowsTheFilename() {
311 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg). Nice", null);
312 assertThat("Part Text", convertText(parts), is("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg|USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg|image.jpg]). Nice"));
316 public void uskLinkWithoutFilenameButEndingInSlashShowsThePath() {
317 Iterable<Part> parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/). Nice", null);
318 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"));
322 public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
323 Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
324 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!"));
328 public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
329 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
330 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!"));
334 public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
335 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
336 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!"));
340 public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
341 Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
342 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!"));
346 public void exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
347 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
348 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!"));
352 public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
353 Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc?", null);
354 assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?"));
358 public void correctFreemailAddressIsLinkedToCorrectly() {
359 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
360 assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!"));
364 public void freemailAddressWithInvalidFreemailIdIsParsedAsText() {
365 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null);
366 assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!"));
370 public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() {
371 Iterable<Part> parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
372 assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
376 public void freemailAddressWithoutLocalPartIsParsedAsText() {
377 Iterable<Part> parts = soneTextParser.parse(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
378 assertThat("Part Text", convertText(parts), is(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
382 public void correctFreemailAddressIsParsedCorrectly() {
383 Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
384 assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
388 public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
389 Iterable<Part> parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
390 assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
394 * Converts all given {@link Part}s into a string, validating that the
395 * part’s classes match only the expected classes.
398 * The parts to convert to text
399 * @param validClasses
400 * The valid classes; if no classes are given, all classes are
402 * @return The converted text
404 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
405 StringBuilder text = new StringBuilder();
406 for (Part part : parts) {
407 assertThat("Part", part, notNullValue());
408 if (validClasses.length != 0) {
409 assertThat("Part’s class", part.getClass(), isIn(validClasses));
411 if (part instanceof PlainTextPart) {
412 text.append(((PlainTextPart) part).getText());
413 } else if (part instanceof FreenetLinkPart) {
414 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
415 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
416 } else if (part instanceof FreemailPart) {
417 FreemailPart freemailPart = (FreemailPart) part;
418 text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId()));
419 } else if (part instanceof LinkPart) {
420 LinkPart linkPart = (LinkPart) part;
421 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
422 } else if (part instanceof SonePart) {
423 SonePart sonePart = (SonePart) part;
424 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
425 } else if (part instanceof PostPart) {
426 PostPart postPart = (PostPart) part;
427 text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]");
430 return text.toString();
434 public void parserCanBeCreatedByGuice() {
435 Injector injector = createInjector(
436 supply(SoneProvider.class).byMock(),
437 supply(PostProvider.class).byMock()
439 assertThat(injector.getInstance(SoneTextParser.class), notNullValue());
443 * Mock Sone provider.
445 private static class TestSoneProvider implements SoneProvider {
449 public Function1<String, Sone> getSoneLoader() {
450 return new Function1<String, Sone>() {
452 public Sone invoke(String soneId) {
453 return getSone(soneId);
460 public Sone getSone(final String soneId) {
461 return new IdOnlySone(soneId);
468 public Collection<Sone> getSones() {
476 public Collection<Sone> getLocalSones() {
484 public Collection<Sone> getRemoteSones() {
490 private static class AbsentSoneProvider extends TestSoneProvider {
493 public Sone getSone(String soneId) {
499 private static class TestPostProvider implements PostProvider {
503 public Post getPost(@Nonnull final String postId) {
506 public String getId() {
511 public boolean isLoaded() {
516 public Sone getSone() {
521 public Optional<String> getRecipientId() {
526 public Optional<Sone> getRecipient() {
531 public long getTime() {
536 public String getText() {
541 public boolean isKnown() {
546 public Post setKnown(boolean known) {
553 public Collection<Post> getPosts(String soneId) {
558 public Collection<Post> getDirectedPosts(String recipientId) {
564 private static class AbsentPostProvider extends TestPostProvider {
568 public Post getPost(@Nonnull String postId) {