X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftext%2FSoneTextParserTest.java;h=4f9f002f0d14934c01f3e88ba37675d927bad4d0;hb=927de326e0af0f11a27b3444f4e25b0796c877db;hp=568c466449d1575aed49c8bfaafde28e02f5d644;hpb=b8cac39598e016ad4cf4bcc2ccd262b4fda26516;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index 568c466..4f9f002 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -1,5 +1,5 @@ /* - * Sone - SoneTextParserTest.java - Copyright © 2011 David Roden + * Sone - SoneTextParserTest.java - Copyright © 2011–2012 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 @@ -21,6 +21,9 @@ import java.io.IOException; import java.io.StringReader; import junit.framework.TestCase; +import net.pterodactylus.sone.core.SoneProvider; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.memory.MemorySone; /** * JUnit test case for {@link SoneTextParser}. @@ -29,6 +32,10 @@ import junit.framework.TestCase; */ public class SoneTextParserTest extends TestCase { + // + // ACTIONS + // + /** * Tests basic plain-text operation of the parser. * @@ -55,6 +62,49 @@ public class SoneTextParserTest extends TestCase { assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class)); } + /** + * Tests parsing of KSK links. + * + * @throws IOException + * if an I/O error occurs + */ + public void testKSKLinks() throws IOException { + SoneTextParser soneTextParser = new SoneTextParser(null, null); + Iterable parts; + + /* check basic links. */ + parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt")); + assertNotNull("Parts", parts); + assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class)); + + /* check embedded links. */ + parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b.")); + assertNotNull("Parts", parts); + assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class)); + + /* check embedded links and line breaks. */ + parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n")); + assertNotNull("Parts", parts); + assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]", convertText(parts, PlainTextPart.class, FreenetLinkPart.class)); + } + + /** + * Test case for a bug that was discovered in 0.6.7. + * + * @throws IOException + * if an I/O error occurs + */ + @SuppressWarnings("synthetic-access") + public void testEmptyLinesAndSoneLinks() throws IOException { + SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null); + Iterable parts; + + /* check basic links. */ + parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff.")); + assertNotNull("Parts", parts); + assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class)); + } + // // PRIVATE METHODS // @@ -88,13 +138,42 @@ public class SoneTextParserTest extends TestCase { 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.getText()).append(']'); + text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']'); } else if (part instanceof LinkPart) { LinkPart linkPart = (LinkPart) part; - text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getText()).append(']'); + 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(']'); } } return text.toString(); } + /** + * Mock Sone provider. + * + * @author David ‘Bombe’ Roden + */ + private static class TestSoneProvider implements SoneProvider { + + /** + * {@inheritDoc} + */ + @Override + public Sone getSone(final String soneId, boolean create) { + return new MemorySone(soneId, false) { + + /** + * {@inheritDoc} + */ + @Override + public String getName() { + return soneId; + } + }; + } + + } + }