Parse freemail addresses
[Sone.git] / src / test / java / net / pterodactylus / sone / text / SoneTextParserTest.java
index bb8c711..8956ba4 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.text;
 
+import static java.lang.String.format;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.isIn;
@@ -49,17 +50,14 @@ public class SoneTextParserTest {
        public void testPlainText() throws IOException {
                /* check basic operation. */
                Iterable<Part> parts = soneTextParser.parse("Test.", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test."));
 
                /* check empty lines at start and end. */
                parts = soneTextParser.parse("\nTest.\n\n", null);
-               assertThat("Parts", parts, notNullValue());
                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("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
        }
 
@@ -116,7 +114,7 @@ public class SoneTextParserTest {
 
        @Test
        public void nameOfFreenetLinkDoesNotContainUrlParameters() {
-           Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt?max-size=12345", null);
+               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]"));
        }
 
@@ -152,7 +150,7 @@ public class SoneTextParserTest {
 
        @Test
        public void httpLinksHaveTheirLastSlashRemoved() {
-           Iterable<Part> parts = soneTextParser.parse("http://test.test/test/", null);
+               Iterable<Part> parts = soneTextParser.parse("http://test.test/test/", null);
                assertThat("Part Text", convertText(parts), is("[http://test.test/test/|test.test/…|test.test/…]"));
        }
 
@@ -237,17 +235,14 @@ public class SoneTextParserTest {
        public void testKSKLinks() throws IOException {
                /* check basic links. */
                Iterable<Part> parts = soneTextParser.parse("KSK@gpl.txt", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
 
                /* check embedded links. */
                parts = soneTextParser.parse("Link is KSK@gpl.txt\u200b.", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|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("Parts", parts, notNullValue());
                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]"));
        }
 
@@ -258,7 +253,6 @@ public class SoneTextParserTest {
 
                /* check basic links. */
                Iterable<Part> parts = soneTextParser.parse("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class, SonePart.class), is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
        }
 
@@ -269,70 +263,93 @@ public class SoneTextParserTest {
 
                /* check empty http links. */
                Iterable<Part> parts = soneTextParser.parse("Some text. Empty link: http:// – nice!", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
        }
 
        @Test
        public void httpLinkWithoutParensEndsAtNextClosingParen() {
                Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc) – nice!", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text (and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]) – nice!"));
        }
 
        @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("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts), is("Some link ([USK@qM1nmgU-YUnIttmEhqjTl7ifAF3Z6o~5EPwQW03uEQU,aztSUkT-VT1dWvfSUt9YpfyW~Flmf5yXpBnIE~v8sAg,AAMC--8/test/0|test|test]). Nice"));
        }
 
        @Test
        public void httpLinkWithOpenedAndClosedParensEndsAtNextClosingParen() {
                Iterable<Part> parts = soneTextParser.parse("Some text (and a link: http://example.sone/abc_(def)) – nice!", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text (and a link: [http://example.sone/abc_(def)|example.sone/abc_(def)|example.sone/abc_(def)]) – nice!"));
        }
 
        @Test
        public void punctuationIsIgnoredAtEndOfLinkBeforeWhitespace() {
-               SoneTextParser soneTextParser = new SoneTextParser(null, null);
                Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc. Nice!", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]. Nice!"));
        }
 
        @Test
        public void multiplePunctuationCharactersAreIgnoredAtEndOfLinkBeforeWhitespace() {
                Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc... Nice!", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc]... Nice!"));
        }
 
        @Test
        public void commasAreIgnoredAtEndOfLinkBeforeWhitespace() {
-               SoneTextParser soneTextParser = new SoneTextParser(null, null);
                Iterable<Part> parts = soneTextParser.parse("Some text and a link: http://example.sone/abc, nice!", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("Some text and a link: [http://example.sone/abc|example.sone/abc|example.sone/abc], nice!"));
        }
 
        @Test
        public void exclamationMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
-               SoneTextParser soneTextParser = new SoneTextParser(null, null);
                Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc!", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|example.sone/abc|example.sone/abc]!"));
        }
 
        @Test
        public void questionMarksAreIgnoredAtEndOfLinkBeforeWhitespace() {
-               SoneTextParser soneTextParser = new SoneTextParser(null, null);
                Iterable<Part> parts = soneTextParser.parse("A link: http://example.sone/abc?", null);
-               assertThat("Parts", parts, notNullValue());
                assertThat("Part Text", convertText(parts, PlainTextPart.class, LinkPart.class), is("A link: [http://example.sone/abc|example.sone/abc|example.sone/abc]?"));
        }
 
+       @Test
+       public void correctFreemailAddressIsLinkedToCorrectly() {
+               Iterable<Part> 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<Part> 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<Part> 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<Part> parts = soneTextParser.parse("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!", null);
+               assertThat("Part Text", convertText(parts), is("     @t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail!"));
+       }
+
+       @Test
+       public void correctFreemailAddressIsParsedCorrectly() {
+               Iterable<Part> parts = soneTextParser.parse("sone@t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra.freemail", null);
+               assertThat("Part Text", convertText(parts), is("[Freemail|sone|t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra|nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI]"));
+       }
+
+       @Test
+       public void localPartOfFreemailAddressCanContainLettersDigitsMinusDotUnderscore() {
+               Iterable<Part> 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.
@@ -356,6 +373,9 @@ public class SoneTextParserTest {
                        } 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(']');
+                       } 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(']');