2 * Sone - SoneTextParserTest.java - Copyright © 2011–2012 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 java.io.IOException;
21 import java.io.StringReader;
22 import java.util.Arrays;
24 import junit.framework.TestCase;
25 import net.pterodactylus.sone.core.SoneProvider;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.database.memory.MemorySone;
30 * JUnit test case for {@link SoneTextParser}.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class SoneTextParserTest extends TestCase {
41 * Tests basic plain-text operation of the parser.
44 * if an I/O error occurs
46 @SuppressWarnings("static-method")
47 public void testPlainText() throws IOException {
48 SoneTextParser soneTextParser = new SoneTextParser(null, null);
51 /* check basic operation. */
52 parts = soneTextParser.parse(null, new StringReader("Test."));
53 assertNotNull("Parts", parts);
54 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
56 /* check empty lines at start and end. */
57 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
58 assertNotNull("Parts", parts);
59 assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
61 /* check duplicate empty lines in the text. */
62 parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
63 assertNotNull("Parts", parts);
64 assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
68 * Tests parsing of KSK links.
71 * if an I/O error occurs
73 @SuppressWarnings("static-method")
74 public void testKSKLinks() throws IOException {
75 SoneTextParser soneTextParser = new SoneTextParser(null, null);
78 /* check basic links. */
79 parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
80 assertNotNull("Parts", parts);
81 assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
83 /* check embedded links. */
84 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
85 assertNotNull("Parts", parts);
86 assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
88 /* check embedded links and line breaks. */
89 parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
90 assertNotNull("Parts", parts);
91 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));
95 * Test case for a bug that was discovered in 0.6.7.
98 * if an I/O error occurs
100 @SuppressWarnings({ "synthetic-access", "static-method" })
101 public void testEmptyLinesAndSoneLinks() throws IOException {
102 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
103 Iterable<Part> parts;
105 /* check basic links. */
106 parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."));
107 assertNotNull("Parts", parts);
108 assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class));
112 * Test for a bug discovered in Sone 0.8.4 where a plain “http://” would be
113 * parsed into a link.
115 * @throws IOException
116 * if an I/O error occurs
118 @SuppressWarnings({ "synthetic-access", "static-method" })
119 public void testEmpyHttpLinks() throws IOException {
120 SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
121 Iterable<Part> parts;
123 /* check empty http links. */
124 parts = soneTextParser.parse(null, new StringReader("Some text. Empty link: http:// – nice!"));
125 assertNotNull("Parts", parts);
126 assertEquals("Part Text", "Some text. Empty link: http:// – nice!", convertText(parts, PlainTextPart.class));
134 * Converts all given {@link Part}s into a string, validating that the
135 * part’s classes match only the expected classes.
138 * The parts to convert to text
139 * @param validClasses
140 * The valid classes; if no classes are given, all classes are
142 * @return The converted text
144 private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
145 StringBuilder text = new StringBuilder();
146 for (Part part : parts) {
147 assertNotNull("Part", part);
148 boolean classValid = validClasses.length == 0;
149 for (Class<?> validClass : validClasses) {
150 if (validClass.isAssignableFrom(part.getClass())) {
156 fail("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses));
158 if (part instanceof PlainTextPart) {
159 text.append(((PlainTextPart) part).getText());
160 } else if (part instanceof FreenetLinkPart) {
161 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
162 text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
163 } else if (part instanceof LinkPart) {
164 LinkPart linkPart = (LinkPart) part;
165 text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
166 } else if (part instanceof SonePart) {
167 SonePart sonePart = (SonePart) part;
168 text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
171 return text.toString();
175 * Mock Sone provider.
177 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
179 private static class TestSoneProvider implements SoneProvider {
185 public Sone getSone(final String soneId, boolean create) {
186 return new MemorySone(soneId, false) {
192 public String getName() {