🚸 Format USK links with filenames better
[Sone.git] / src / test / java / net / pterodactylus / sone / text / SoneTextParserTest.java
index 40e8124..69cc658 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - SoneTextParserTest.java - Copyright Â© 2011–2016 David Roden
+ * 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
@@ -17,7 +17,9 @@
 
 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;
@@ -26,20 +28,22 @@ 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.Function;
 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}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
  */
 public class SoneTextParserTest {
 
@@ -70,13 +74,13 @@ public class SoneTextParserTest {
        @Test
        public void freenetLinksHaveTheFreenetPrefixRemoved() {
                Iterable<Part> parts = soneTextParser.parse("freenet:KSK@gpl.txt", null);
-               assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
+               assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt|KSK@gpl.txt|gpl.txt]"));
        }
 
        @Test
        public void onlyTheFirstItemInALineIsPrefixedWithALineBreak() {
                Iterable<Part> parts = soneTextParser.parse("Text.\nKSK@gpl.txt and KSK@gpl.txt", null);
-               assertThat("Part Text", convertText(parts), is("Text.\n[KSK@gpl.txt|gpl.txt|gpl.txt] and [KSK@gpl.txt|gpl.txt|gpl.txt]"));
+               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
@@ -93,6 +97,13 @@ public class SoneTextParserTest {
        }
 
        @Test
+       public void soneAndPostCanBeParsedFromTheSameText() {
+               SoneTextParser parser = new SoneTextParser(new TestSoneProvider(), new TestPostProvider());
+               Iterable<Part> 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<Part> parts = soneTextParser.parse("post://too-short", null);
                assertThat("Part Text", convertText(parts), is("post://too-short"));
@@ -115,25 +126,25 @@ public class SoneTextParserTest {
        @Test
        public void nameOfFreenetLinkDoesNotContainUrlParameters() {
                Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null);
-               assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|gpl.txt|gpl.txt]"));
+               assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt?max-size=12345|KSK@gpl.txt|gpl.txt]"));
        }
 
        @Test
        public void trailingSlashInFreenetLinkIsRemovedForName() {
                Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt/", null);
-               assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|gpl.txt|gpl.txt]"));
+               assertThat("Part Text", convertText(parts), is("[KSK@gpl.txt/|KSK@gpl.txt/|gpl.txt]"));
        }
 
        @Test
        public void lastMetaStringOfFreenetLinkIsUsedAsName() {
                Iterable<Part> 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|COPYING|COPYING]"));
+               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<Part> 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@qM1nm|CHK@qM1nm]"));
+               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
@@ -197,37 +208,54 @@ public class SoneTextParserTest {
        }
 
        @Test
+       public void invalidSskAndUskLinkIsParsedAsText() {
+               Iterable<Part> parts = soneTextParser.parse("SSK@a USK@a", null);
+               assertThat("Part Text", convertText(parts), is("SSK@a USK@a"));
+       }
+
+       @Test
+       public void sskWithoutDocumentNameIsParsedCorrectly() {
+               Iterable<Part> 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<Part> 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|test|test]"));
+               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<Part> 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|test|test]"));
+               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<Part> 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|test|test]"));
+               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<Part> 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|test|test]"));
+               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<Part> 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|test|test]"));
+               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")
@@ -235,15 +263,15 @@ public class SoneTextParserTest {
        public void testKSKLinks() throws IOException {
                /* check basic links. */
                Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
-               assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
+               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|gpl.txt|gpl.txt]\u200b."));
+               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|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]"));
+               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" })
@@ -275,7 +303,13 @@ public class SoneTextParserTest {
        @Test
        public void uskLinkEndsAtFirstNonNumericNonSlashCharacterAfterVersionNumber() {
                Iterable<Part> 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|test|test]). Nice"));
+               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<Part> 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
@@ -372,7 +406,7 @@ public class SoneTextParserTest {
                                text.append(((PlainTextPart) part).getText());
                        } else if (part instanceof FreenetLinkPart) {
                                FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
-                               text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
+                               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()));
@@ -390,29 +424,35 @@ public class SoneTextParserTest {
                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.
-        *
-        * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
         */
        private static class TestSoneProvider implements SoneProvider {
 
+               @Nonnull
                @Override
-               public Function<String, Optional<Sone>> soneLoader() {
-                       return new Function<String, Optional<Sone>>() {
+               public Function1<String, Sone> getSoneLoader() {
+                       return new Function1<String, Sone>() {
                                @Override
-                               public Optional<Sone> apply(String soneId) {
+                               public Sone invoke(String soneId) {
                                        return getSone(soneId);
                                }
                        };
                }
 
-               /**
-                * {@inheritDoc}
-                */
+               @Nullable
                @Override
-               public Optional<Sone> getSone(final String soneId) {
-                       return Optional.<Sone>of(new IdOnlySone(soneId));
+               public Sone getSone(final String soneId) {
+                       return new IdOnlySone(soneId);
                }
 
                /**
@@ -444,17 +484,18 @@ public class SoneTextParserTest {
        private static class AbsentSoneProvider extends TestSoneProvider {
 
                @Override
-               public Optional<Sone> getSone(String soneId) {
-                       return Optional.absent();
+               public Sone getSone(String soneId) {
+                       return null;
                }
 
        }
 
        private static class TestPostProvider implements PostProvider {
 
+               @Nullable
                @Override
-               public Optional<Post> getPost(final String postId) {
-                       return Optional.<Post>of(new Post() {
+               public Post getPost(@Nonnull final String postId) {
+                       return new Post() {
                                @Override
                                public String getId() {
                                        return postId;
@@ -499,7 +540,7 @@ public class SoneTextParserTest {
                                public Post setKnown(boolean known) {
                                        return null;
                                }
-                       });
+                       };
                }
 
                @Override
@@ -516,9 +557,10 @@ public class SoneTextParserTest {
 
        private static class AbsentPostProvider extends TestPostProvider {
 
+               @Nullable
                @Override
-               public Optional<Post> getPost(String postId) {
-                       return Optional.absent();
+               public Post getPost(@Nonnull String postId) {
+                       return null;
                }
 
        }