From 62c784c521d116ee569023f2fdd1b2bea8a86b7b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 25 Jun 2019 17:27:39 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Replace=20SoneTextParserTest=20with?= =?utf8?q?=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/text/SoneTextParserTest.java | 574 --------------------- .../pterodactylus/sone/text/SoneTextParserTest.kt | 445 ++++++++++++++++ 2 files changed, 445 insertions(+), 574 deletions(-) delete mode 100644 src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/text/SoneTextParserTest.kt diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java deleted file mode 100644 index 446ef1b..0000000 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ /dev/null @@ -1,574 +0,0 @@ -/* - * Sone - SoneTextParserTest.java - Copyright © 2011–2019 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.text; - -import static com.google.inject.Guice.createInjector; -import static java.lang.String.format; -import static net.pterodactylus.sone.test.GuiceKt.supply; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.isIn; -import static org.hamcrest.Matchers.notNullValue; - -import java.io.IOException; -import java.util.Collection; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.data.impl.IdOnlySone; -import net.pterodactylus.sone.database.PostProvider; -import net.pterodactylus.sone.database.SoneProvider; - -import com.google.common.base.Optional; -import com.google.inject.Injector; -import kotlin.jvm.functions.Function1; -import org.junit.Test; - -/** - * JUnit test case for {@link SoneTextParser}. - */ -public class SoneTextParserTest { - - private final SoneTextParser soneTextParser = new SoneTextParser(null, null); - - @SuppressWarnings("static-method") - @Test - public void testPlainText() throws IOException { - /* check basic operation. */ - Iterable parts = soneTextParser.parse("Test.", null); - assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.")); - - /* check empty lines at start and end. */ - parts = soneTextParser.parse("\nTest.\n\n", null); - assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.")); - - /* check duplicate empty lines in the text. */ - parts = soneTextParser.parse("\nTest.\n\n\nTest.", null); - assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest.")); - } - - @Test - public void consecutiveLinesAreSeparatedByLinefeed() { - Iterable parts = soneTextParser.parse("Text.\nText", null); - assertThat("Part Text", convertText(parts), is("Text.\nText")); - } - - @Test - public void freenetLinksHaveTheFreenetPrefixRemoved() { - Iterable parts = soneTextParser.parse("freenet:KSK@gpl.txt", null); - assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]")); - } - - @Test - public void onlyTheFirstItemInALineIsPrefixedWithALineBreak() { - Iterable parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null); - 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]")); - } - - @Test - public void soneLinkWithTooShortSoneIdIsRenderedAsPlainText() { - Iterable parts = soneTextParser.parse("sone://too-short", null); - assertThat("Part Text", convertText(parts), is("sone://too-short")); - } - - @Test - public void soneLinkIsRenderedCorrectlyIfSoneIsNotPresent() { - SoneTextParser parser = new SoneTextParser(new AbsentSoneProvider(), null); - Iterable parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null); - assertThat("Part Text", convertText(parts), is("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]")); - } - - @Test - public void soneAndPostCanBeParsedFromTheSameText() { - SoneTextParser parser = new SoneTextParser(new TestSoneProvider(), new TestPostProvider()); - Iterable parts = parser.parse("Text sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU more text post://f3757817-b45a-497a-803f-9c5aafc10dc6 even more text", null); - assertThat("Part Text", convertText(parts), is("Text [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] more text [Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text] even more text")); - } - - @Test - public void postLinkIsRenderedAsPlainTextIfPostIdIsTooShort() { - Iterable parts = soneTextParser.parse("post://too-short", null); - assertThat("Part Text", convertText(parts), is("post://too-short")); - } - - @Test - public void postLinkIsRenderedCorrectlyIfPostIsPresent() { - SoneTextParser parser = new SoneTextParser(null, new TestPostProvider()); - Iterable parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null); - assertThat("Part Text", convertText(parts), is("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]")); - } - - @Test - public void postLinkIsRenderedAsPlainTextIfPostIsAbsent() { - SoneTextParser parser = new SoneTextParser(null, new AbsentPostProvider()); - Iterable parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null); - assertThat("Part Text", convertText(parts), is("post://f3757817-b45a-497a-803f-9c5aafc10dc6")); - } - - @Test - public void nameOfFreenetLinkDoesNotContainUrlParameters() { - Iterable parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null); - assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]")); - } - - @Test - public void trailingSlashInFreenetLinkIsRemovedForName() { - Iterable parts = soneTextParser.parse("KSK@gpl.txt/", null); - assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]")); - } - - @Test - public void lastMetaStringOfFreenetLinkIsUsedAsName() { - Iterable parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null); - 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]")); - } - - @Test - public void freenetLinkWithoutMetaStringsAndDocNameGetsFirstNineCharactersOfKeyAsName() { - Iterable parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null); - 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]")); - } - - @Test - public void malformedKeyIsRenderedAsPlainText() { - Iterable parts = soneTextParser.parse("CHK@qM1nmgU", null); - assertThat("Part Text", convertText(parts), is("CHK@qM1nmgU")); - } - - @Test - public void httpsLinkHasItsPathsShortened() { - Iterable parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null); - 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]")); - } - - @Test - public void httpLinksHaveTheirLastSlashRemoved() { - Iterable parts = soneTextParser.parse("http://test.test/test/", null); - assertThat("Part Text", convertText(parts), is("[http://test.test/test/|http://test.test/test/|test.test/…]")); - } - - @Test - public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndNoPath() { - Iterable parts = soneTextParser.parse("http://www.test.test", null); - assertThat("Part Text", convertText(parts), is("[http://www.test.test|http://www.test.test|test.test]")); - } - - @Test - public void wwwPrefixIsRemovedForHostnameWithTwoDotsAndAPath() { - Iterable parts = soneTextParser.parse("http://www.test.test/test.html", null); - assertThat("Part Text", convertText(parts), is("[http://www.test.test/test.html|http://www.test.test/test.html|test.test/test.html]")); - } - - @Test - public void hostnameIsKeptIntactIfNotBeginningWithWww() { - Iterable parts = soneTextParser.parse("http://test.test.test/test.html", null); - assertThat("Part Text", convertText(parts), is("[http://test.test.test/test.html|http://test.test.test/test.html|test.test.test/test.html]")); - } - - @Test - public void hostnameWithOneDotButNoSlashIsKeptIntact() { - Iterable parts = soneTextParser.parse("http://test.test", null); - assertThat("Part Text", convertText(parts), is("[http://test.test|http://test.test|test.test]")); - } - - @Test - public void urlParametersAreRemovedForHttpLinks() { - Iterable parts = soneTextParser.parse("http://test.test?foo=bar", null); - assertThat("Part Text", convertText(parts), is("[http://test.test?foo=bar|http://test.test?foo=bar|test.test]")); - } - - @Test - public void emptyStringIsParsedCorrectly() { - Iterable parts = soneTextParser.parse("", null); - assertThat("Part Text", convertText(parts), is("")); - } - - @Test - public void linksAreParsedInCorrectOrder() { - Iterable parts = soneTextParser.parse("KSK@ CHK@", null); - assertThat("Part Text", convertText(parts), is("KSK@ CHK@")); - } - - @Test - public void invalidSskAndUskLinkIsParsedAsText() { - Iterable parts = soneTextParser.parse("SSK@a USK@a", null); - assertThat("Part Text", convertText(parts), is("SSK@a USK@a")); - } - - @Test - public void sskWithoutDocumentNameIsParsedCorrectly() { - Iterable parts = soneTextParser.parse( - "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", - null); - assertThat("Part Text", convertText(parts), - is("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|" - + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|" - + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU]")); - } - - @Test - public void sskLinkWithoutContextIsNotTrusted() { - Iterable parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null); - 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]")); - } - - @Test - public void sskLinkWithContextWithoutSoneIsNotTrusted() { - SoneTextParserContext context = new SoneTextParserContext(null); - Iterable parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context); - 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]")); - } - - @Test - public void sskLinkWithContextWithDifferentSoneIsNotTrusted() { - SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU")); - Iterable parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context); - 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]")); - } - - @Test - public void sskLinkWithContextWithCorrectSoneIsTrusted() { - SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU")); - Iterable parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context); - 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]")); - } - - @Test - public void uskLinkWithContextWithCorrectSoneIsTrusted() { - SoneTextParserContext context = new SoneTextParserContext(new IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU")); - Iterable parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context); - 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]")); - } - - @SuppressWarnings("static-method") - @Test - public void testKSKLinks() throws IOException { - /* check basic links. */ - Iterable parts = soneTextParser.parse("KSK@gpl.txt", null); - assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]")); - - /* check embedded links. */ - parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null); - assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b.")); - - /* check embedded links and line breaks. */ - parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null); - 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]")); - } - - @SuppressWarnings({ "synthetic-access", "static-method" }) - @Test - public void testEmptyLinesAndSoneLinks() throws IOException { - SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null); - - /* check basic links. */ - Iterable parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null); - assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.")); - } - - @SuppressWarnings({ "synthetic-access", "static-method" }) - @Test - public void testEmpyHttpLinks() throws IOException { - SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null); - - /* check empty http links. */ - Iterable parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null); - assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!")); - } - - @Test - public void httpLinkWithoutParensEndsAtNextClosingParen() { - Iterable parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null); - 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!")); - } - - @Test - public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() { - Iterable parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null); - 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")); - } - - @Test - public void uskLinkWithFilenameShowsTheFilename() { - Iterable parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg). Nice", null); - 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")); - } - - @Test - public void uskLinkWithoutFilenameButEndingInSlashShowsThePath() { - Iterable parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/). Nice", null); - 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")); - } - - @Test - public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() { - Iterable parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null); - 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!")); - } - - @Test - public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() { - Iterable parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null); - 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!")); - } - - @Test - public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() { - Iterable parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null); - 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!")); - } - - @Test - public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() { - Iterable parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null); - 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!")); - } - - @Test - public void exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() { - Iterable parts = soneTextParser.parse("A link: http://example.sone/abc!", null); - assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!")); - } - - @Test - public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() { - Iterable parts = soneTextParser.parse("A link: http://example.sone/abc?", null); - assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?")); - } - - @Test - public void correctFreemailAddressIsLinkedToCorrectly() { - Iterable parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null); - assertThat("Part Text", convertText(parts), is("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!")); - } - - @Test - public void freemailAddressWithInvalidFreemailIdIsParsedAsText() { - Iterable parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null); - assertThat("Part Text", convertText(parts), is("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!")); - } - - @Test - public void freemailAddressWithInvalidSizedFreemailIdIsParsedAsText() { - Iterable parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null); - assertThat("Part Text", convertText(parts), is("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!")); - } - - @Test - public void freemailAddressWithoutLocalPartIsParsedAsText() { - Iterable parts = soneTextParser.parse(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null); - assertThat("Part Text", convertText(parts), is(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!")); - } - - @Test - public void correctFreemailAddressIsParsedCorrectly() { - Iterable parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null); - assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]")); - } - - @Test - public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() { - Iterable parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null); - assertThat("Part Text", convertText(parts), is("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]")); - } - - /** - * Converts all given {@link Part}s into a string, validating that the - * part’s classes match only the expected classes. - * - * @param parts - * The parts to convert to text - * @param validClasses - * The valid classes; if no classes are given, all classes are - * valid - * @return The converted text - */ - private static String convertText(Iterable parts, Class... validClasses) { - StringBuilder text = new StringBuilder(); - for (Part part : parts) { - assertThat("Part", part, notNullValue()); - if (validClasses.length != 0) { - assertThat("Part’s class", part.getClass(), isIn(validClasses)); - } - if (part instanceof PlainTextPart) { - text.append(((PlainTextPart) part).getText()); - } else if (part instanceof FreenetLinkPart) { - FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part; - text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.getTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']'); - } else if (part instanceof FreemailPart) { - FreemailPart freemailPart = (FreemailPart) part; - text.append(format("[Freemail|%s|%s|%s]", freemailPart.getEmailLocalPart(), freemailPart.getFreemailId(), freemailPart.getIdentityId())); - } else if (part instanceof LinkPart) { - LinkPart linkPart = (LinkPart) part; - text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']'); - } else if (part instanceof SonePart) { - SonePart sonePart = (SonePart) part; - text.append("[Sone|").append(sonePart.getSone().getId()).append(']'); - } else if (part instanceof PostPart) { - PostPart postPart = (PostPart) part; - text.append("[Post|").append(postPart.getPost().getId()).append("|").append(postPart.getPost().getText()).append("]"); - } - } - return text.toString(); - } - - @Test - public void parserCanBeCreatedByGuice() { - Injector injector = createInjector( - supply(SoneProvider.class).byMock(), - supply(PostProvider.class).byMock() - ); - assertThat(injector.getInstance(SoneTextParser.class), notNullValue()); - } - - /** - * Mock Sone provider. - */ - private static class TestSoneProvider implements SoneProvider { - - @Nonnull - @Override - public Function1 getSoneLoader() { - return new Function1() { - @Override - public Sone invoke(String soneId) { - return getSone(soneId); - } - }; - } - - @Nullable - @Override - public Sone getSone(final String soneId) { - return new IdOnlySone(soneId); - } - - /** - * {@inheritDocs} - */ - @Override - public Collection getSones() { - return null; - } - - /** - * {@inheritDocs} - */ - @Override - public Collection getLocalSones() { - return null; - } - - /** - * {@inheritDocs} - */ - @Override - public Collection getRemoteSones() { - return null; - } - - } - - private static class AbsentSoneProvider extends TestSoneProvider { - - @Override - public Sone getSone(String soneId) { - return null; - } - - } - - private static class TestPostProvider implements PostProvider { - - @Nullable - @Override - public Post getPost(@Nonnull final String postId) { - return new Post() { - @Override - public String getId() { - return postId; - } - - @Override - public boolean isLoaded() { - return false; - } - - @Override - public Sone getSone() { - return null; - } - - @Override - public Optional getRecipientId() { - return null; - } - - @Override - public Optional getRecipient() { - return null; - } - - @Override - public long getTime() { - return 0; - } - - @Override - public String getText() { - return "text"; - } - - @Override - public boolean isKnown() { - return false; - } - - @Override - public Post setKnown(boolean known) { - return null; - } - }; - } - - @Override - public Collection getPosts(String soneId) { - return null; - } - - @Override - public Collection getDirectedPosts(String recipientId) { - return null; - } - - } - - private static class AbsentPostProvider extends TestPostProvider { - - @Nullable - @Override - public Post getPost(@Nonnull String postId) { - return null; - } - - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/text/SoneTextParserTest.kt b/src/test/kotlin/net/pterodactylus/sone/text/SoneTextParserTest.kt new file mode 100644 index 0000000..9ea97d9 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/text/SoneTextParserTest.kt @@ -0,0 +1,445 @@ +/* + * Sone - SoneTextParserTest.java - Copyright © 2011–2019 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.text + +import com.google.inject.Guice.* +import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.data.impl.* +import net.pterodactylus.sone.database.* +import net.pterodactylus.sone.test.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* + +/** + * JUnit test case for [SoneTextParser]. + */ +class SoneTextParserTest { + + private val soneTextParser = SoneTextParser(null, null) + + @Test + fun `basic operation`() { + val parts = soneTextParser.parse("Test.", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Test.")) + } + + @Test + fun `empty lines at start and end are stripped`() { + val parts = soneTextParser.parse("\nTest.\n\n", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Test.")) + } + + @Test + fun `duplicate empty lines in the text are stripped`() { + val parts = soneTextParser.parse("\nTest.\n\n\nTest.", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Test.\n\nTest.")) + } + + @Test + fun `consecutive lines are separated by linefeed`() { + val parts = soneTextParser.parse("Text.\nText", null) + assertThat("Part Text", convertText(parts), equalTo("Text.\nText")) + } + + @Test + fun `freenet links have the freenet prefix removed`() { + val parts = soneTextParser.parse("freenet:KSK@gpl.txt", null) + assertThat("Part Text", convertText(parts), equalTo("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]")) + } + + @Test + fun `only the first item in a line is prefixed with a line break`() { + val parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null) + assertThat("Part Text", convertText(parts), equalTo("Text.\n[KSK@gpl.txt|KSK@gpl.txt|gpl.txt] and [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]")) + } + + @Test + fun `sone link with too short sone ID is rendered as plain text`() { + val parts = soneTextParser.parse("sone://too-short", null) + assertThat("Part Text", convertText(parts), equalTo("sone://too-short")) + } + + @Test + fun `sone link is rendered correctly if sone is not present`() { + val parser = SoneTextParser(AbsentSoneProvider(), null) + val parts = parser.parse("sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU", null) + assertThat("Part Text", convertText(parts), equalTo("[Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU]")) + } + + @Test + fun `sone and post can be parsed from the same text`() { + val parser = SoneTextParser(TestSoneProvider(), TestPostProvider()) + val parts = parser.parse("Text sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU more text post://f3757817-b45a-497a-803f-9c5aafc10dc6 even more text", null) + assertThat("Part Text", convertText(parts), equalTo("Text [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] more text [Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text] even more text")) + } + + @Test + fun `post link is rendered as plain text if post ID is too short`() { + val parts = soneTextParser.parse("post://too-short", null) + assertThat("Part Text", convertText(parts), equalTo("post://too-short")) + } + + @Test + fun `post link is rendered correctly if post is present`() { + val parser = SoneTextParser(null, TestPostProvider()) + val parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null) + assertThat("Part Text", convertText(parts), equalTo("[Post|f3757817-b45a-497a-803f-9c5aafc10dc6|text]")) + } + + @Test + fun `post link is rendered as plain text if post is absent`() { + val parser = SoneTextParser(null, AbsentPostProvider()) + val parts = parser.parse("post://f3757817-b45a-497a-803f-9c5aafc10dc6", null) + assertThat("Part Text", convertText(parts), equalTo("post://f3757817-b45a-497a-803f-9c5aafc10dc6")) + } + + @Test + fun `name of freenet link does not contain url parameters`() { + val parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null) + assertThat("Part Text", convertText(parts), equalTo("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]")) + } + + @Test + fun `trailing slash in freenet link is removed for name`() { + val parts = soneTextParser.parse("KSK@gpl.txt/", null) + assertThat("Part Text", convertText(parts), equalTo("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]")) + } + + @Test + fun `last meta string of freenet link is used as name`() { + val parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING", null) + assertThat("Part Text", convertText(parts), equalTo("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/COPYING|COPYING]")) + } + + @Test + fun `freenet link without meta strings and doc name gets first nine characters of key as name`() { + val parts = soneTextParser.parse("CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null) + assertThat("Part Text", convertText(parts), equalTo("[CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|CHK@qM1nm]")) + } + + @Test + fun `malformed key is rendered as plain text`() { + val parts = soneTextParser.parse("CHK@qM1nmgU", null) + assertThat("Part Text", convertText(parts), equalTo("CHK@qM1nmgU")) + } + + @Test + fun `https link has its paths shortened`() { + val parts = soneTextParser.parse("https://test.test/some-long-path/file.txt", null) + assertThat("Part Text", convertText(parts), equalTo("[https://test.test/some-long-path/file.txt|https://test.test/some-long-path/file.txt|test.test/…/file.txt]")) + } + + @Test + fun `http links have their last slash removed`() { + val parts = soneTextParser.parse("http://test.test/test/", null) + assertThat("Part Text", convertText(parts), equalTo("[http://test.test/test/|http://test.test/test/|test.test/…]")) + } + + @Test + fun `www prefix is removed for hostname with two dots and no path`() { + val parts = soneTextParser.parse("http://www.test.test", null) + assertThat("Part Text", convertText(parts), equalTo("[http://www.test.test|http://www.test.test|test.test]")) + } + + @Test + fun `www prefix is removed for hostname with two dots and a path`() { + val parts = soneTextParser.parse("http://www.test.test/test.html", null) + assertThat("Part Text", convertText(parts), equalTo("[http://www.test.test/test.html|http://www.test.test/test.html|test.test/test.html]")) + } + + @Test + fun `hostname is kept intact if not beginning with www`() { + val parts = soneTextParser.parse("http://test.test.test/test.html", null) + assertThat("Part Text", convertText(parts), equalTo("[http://test.test.test/test.html|http://test.test.test/test.html|test.test.test/test.html]")) + } + + @Test + fun `hostname with one dot but no slash is kept intact`() { + val parts = soneTextParser.parse("http://test.test", null) + assertThat("Part Text", convertText(parts), equalTo("[http://test.test|http://test.test|test.test]")) + } + + @Test + fun `url parameters are removed for http links`() { + val parts = soneTextParser.parse("http://test.test?foo=bar", null) + assertThat("Part Text", convertText(parts), equalTo("[http://test.test?foo=bar|http://test.test?foo=bar|test.test]")) + } + + @Test + fun `empty string is parsed correctly`() { + val parts = soneTextParser.parse("", null) + assertThat("Part Text", convertText(parts), equalTo("")) + } + + @Test + fun `links are parsed in correct order`() { + val parts = soneTextParser.parse("KSK@ CHK@", null) + assertThat("Part Text", convertText(parts), equalTo("KSK@ CHK@")) + } + + @Test + fun `invalid ssk and usk link is parsed as text`() { + val parts = soneTextParser.parse("SSK@a USK@a", null) + assertThat("Part Text", convertText(parts), equalTo("SSK@a USK@a")) + } + + @Test + fun `ssk without document name is parsed correctly`() { + val parts = soneTextParser.parse( + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8", null) + assertThat("Part Text", convertText(parts), + equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|" + + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8|" + + "SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU]")) + } + + @Test + fun `ssk link without context is not trusted`() { + val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", null) + assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]")) + } + + @Test + fun `ssk link with context without sone is not trusted`() { + val context = SoneTextParserContext(null) + val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context) + assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]")) + } + + @Test + fun `ssk link with context with different sone is not trusted`() { + val context = SoneTextParserContext(IdOnlySone("DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU")) + val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context) + assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]")) + } + + @Test + fun `ssk link with context with correct sone is trusted`() { + val context = SoneTextParserContext(IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU")) + val parts = soneTextParser.parse("SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test", context) + assertThat("Part Text", convertText(parts), equalTo("[SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|trusted|SSK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test|test]")) + } + + @Test + fun `usk link with context with correct sone is trusted`() { + val context = SoneTextParserContext(IdOnlySone("qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU")) + val parts = soneTextParser.parse("USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0", context) + assertThat("Part Text", convertText(parts), equalTo("[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]")) + } + + @Test + fun `test basic ksk links`() { + val parts: Iterable = soneTextParser.parse("KSK@gpl.txt", null) + assertThat("Part Text", convertText(parts, FreenetLinkPart::class.java), equalTo("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]")) + } + + @Test + fun `embedded ksk links are parsed correctly`() { + val parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java, FreenetLinkPart::class.java), equalTo("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\u200b.")) + } + + @Test + fun `embedded ksk links and line breaks are parsed correctly`() { + val parts = soneTextParser.parse("Link is KSK@gpl.txt\nKSK@test.dat\n", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java, FreenetLinkPart::class.java), equalTo("Link is [KSK@gpl.txt|KSK@gpl.txt|gpl.txt]\n[KSK@test.dat|KSK@test.dat|test.dat]")) + } + + @Test + fun `test empty lines and sone links`() { + val soneTextParser = SoneTextParser(TestSoneProvider(), null) + + /* check basic links. */ + val parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java, SonePart::class.java), equalTo("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.")) + } + + @Test + fun `test empy http links`() { + val soneTextParser = SoneTextParser(TestSoneProvider(), null) + + /* check empty http links. */ + val parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java), equalTo("Some text. Empty link: http:// – nice!")) + } + + @Test + fun `http link without parens ends at next closing paren`() { + val parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text (and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]) – nice!")) + } + + @Test + fun `usk link ends at first non numeric non slash character after version number`() { + val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0). Nice", null) + assertThat("Part Text", convertText(parts), equalTo("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")) + } + + @Test + fun `usk link with filename shows the filename`() { + val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/images/image.jpg). Nice", null) + assertThat("Part Text", convertText(parts), equalTo("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")) + } + + @Test + fun `usk link without filename but ending in slash shows the path`() { + val parts = soneTextParser.parse("Some link (USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0/). Nice", null) + assertThat("Part Text", convertText(parts), equalTo("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")) + } + + @Test + fun `http link with opened and closed parens ends at next closing paren`() { + val parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text (and a link: [http://example.sone/abc_(def)|http://example.sone/abc_(def)|example.sone/abc_(def)]) – nice!")) + } + + @Test + fun `punctuation is ignored at end of link before whitespace`() { + val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]. Nice!")) + } + + @Test + fun `multiple punctuation characters are ignored at end of link before whitespace`() { + val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]... Nice!")) + } + + @Test + fun `commas are ignored at end of link before whitespace`() { + val parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("Some text and a link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc], nice!")) + } + + @Test + fun `exclamation marks are ignored at end of link before whitespace`() { + val parts = soneTextParser.parse("A link: http://example.sone/abc!", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]!")) + } + + @Test + fun `question marks are ignored at end of link before whitespace`() { + val parts = soneTextParser.parse("A link: http://example.sone/abc?", null) + assertThat("Part Text", convertText(parts, PlainTextPart::class.java, LinkPart::class.java), equalTo("A link: [http://example.sone/abc|http://example.sone/abc|example.sone/abc]?")) + } + + @Test + fun `correct freemail address is linked to correctly`() { + val parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null) + assertThat("Part Text", convertText(parts), equalTo("Mail me at [Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]!")) + } + + @Test + fun `freemail address with invalid freemail id is parsed as text`() { + val parts = soneTextParser.parse("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!", null) + assertThat("Part Text", convertText(parts), equalTo("Mail me at sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqr8.freemail!")) + } + + @Test + fun `freemail address with invalid sized freemail id is parsed as text`() { + val parts = soneTextParser.parse("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null) + assertThat("Part Text", convertText(parts), equalTo("Mail me at sone@4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!")) + } + + @Test + fun `freemail address without local part is parsed as text`() { + val parts = soneTextParser.parse(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null) + assertThat("Part Text", convertText(parts), equalTo(" @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!")) + } + + @Test + fun `local part of freemail address can contain letters digits minus dot underscore`() { + val parts = soneTextParser.parse("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null) + assertThat("Part Text", convertText(parts), equalTo("[Freemail|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]")) + } + + private fun convertText(parts: Iterable, vararg validClasses: Class<*>): String { + if (validClasses.isNotEmpty()) { + assertThat(parts.map { it.javaClass }.distinct() - validClasses.distinct(), empty()) + } + return parts.joinToString("") { part -> + when (part) { + is PlainTextPart -> part.text + is FreenetLinkPart -> "[${part.link}|${if (part.trusted) "trusted|" else ""}${part.title}|${part.text}]" + is FreemailPart -> "[Freemail|${part.emailLocalPart}|${part.freemailId}|${part.identityId}]" + is LinkPart -> "[${part.link}|${part.title}|${part.text}]" + is SonePart -> "[Sone|${part.sone.id}]" + is PostPart -> "[Post|${part.post.id}|${part.post.text}]" + else -> throw NoSuchElementException() + } + } + } + + @Test + fun `parser can be created by guice`() { + val injector = createInjector( + SoneProvider::class.isProvidedByMock(), + PostProvider::class.isProvidedByMock() + ) + assertThat(injector.getInstance(), notNullValue()) + } + + /** + * Mock Sone provider. + */ + private open class TestSoneProvider : SoneProvider { + + override val soneLoader = this::getSone + override val sones: Collection = emptySet() + override val localSones: Collection = emptySet() + override val remoteSones: Collection = emptySet() + + override fun getSone(soneId: String): Sone? = IdOnlySone(soneId) + + } + + private class AbsentSoneProvider : TestSoneProvider() { + + override fun getSone(soneId: String): Sone? = null + + } + + private open class TestPostProvider : PostProvider { + + override fun getPost(postId: String): Post? { + return object : Post { + override val id = postId + override fun isLoaded() = false + override fun getSone() = null + override fun getRecipientId() = null + override fun getRecipient() = null + override fun getTime() = 0L + override fun getText() = "text" + override fun isKnown() = false + override fun setKnown(known: Boolean) = null + } + } + + override fun getPosts(soneId: String) = emptySet() + override fun getDirectedPosts(recipientId: String) = emptySet() + + } + + private class AbsentPostProvider : TestPostProvider() { + + override fun getPost(postId: String): Post? = null + + } + +} -- 2.7.4